public function __construct(Gaufrette\Adapter $adapter, sfUrlResolverInterface $resolver)
 {
     parent::__construct($adapter);
     $this->resolver = $resolver;
 }
예제 #2
0
파일: Upload.php 프로젝트: skybird/phalcon
 public function uploadByEncodedData($data, $originalName, $mimeType = null)
 {
     if (!($headPos = strpos($data, ','))) {
         throw new Exception\InvalidArgumentException('ERR_FILE_ENCODED_UPLOAD_FORMAT_INCORRECT');
     }
     $fileHead = substr($data, 0, $headPos + 1);
     $fileEncodedData = trim(substr($data, $headPos + 1));
     $data = base64_decode($fileEncodedData);
     $tmpName = Text::random(\Phalcon\Text::RANDOM_ALNUM, 6);
     $tmpPath = $this->getUploadTmpPath();
     $tmp = $tmpPath . '/' . $tmpName;
     $adapter = new \Gaufrette\Adapter\Local($tmpPath);
     $filesystem = new \Gaufrette\Filesystem($adapter);
     $filesystem->write($tmpName, $data);
     $fileSize = filesize($tmp);
     $type = $mimeType;
     $filenameArray = explode(".", $originalName);
     $fileExtension = strtolower(array_pop($filenameArray));
     $originalFileName = implode('.', $filenameArray);
     $fileName = Tag::friendlyTitle($originalFileName);
     if ($fileName == '-') {
         $fileName = Text::random(Text::RANDOM_ALNUM, 6);
     }
     //hash file less then 10M
     if ($fileSize < 1048576 * 10) {
         $fileHash = hash_file('CRC32', $tmp, false);
     }
     if (false === strpos($type, 'image')) {
         $isImage = 0;
     } else {
         $isImage = 1;
     }
     $fileinfo = array('title' => $originalFileName, 'status' => 'published', 'storageAdapter' => 'local', 'originalName' => $originalName, 'fileSize' => $fileSize, 'mimeType' => $type, 'fileExtension' => $fileExtension, 'fileHash' => $fileHash, 'isImage' => $isImage, 'fileName' => $fileName . '.' . $fileExtension, 'createdAt' => time());
     if ($isImage) {
         $image = getimagesize($tmp);
         $fileinfo['imageWidth'] = $image[0];
         $fileinfo['imageHeight'] = $image[1];
     }
     $filesystem = $this->getDI()->getFileSystem();
     $path = md5(time());
     $path = str_split($path, 2);
     $pathlevel = $this->getUploadPathLevel();
     $pathlevel = $pathlevel > 6 ? 6 : $pathlevel;
     $path = array_slice($path, 0, $pathlevel);
     $filePath = implode('/', $path);
     $path = $filePath . '/' . $fileName . '.' . $fileExtension;
     $fileinfo['filePath'] = $filePath;
     $this->assign($fileinfo);
     if ($this->save()) {
         if (!$filesystem->has($path)) {
             if ($filesystem->write($path, file_get_contents($tmp))) {
                 unlink($tmp);
             } else {
                 throw new Exception\IOException('ERR_FILE_MOVE_TO_STORAGE_FAILED');
             }
         } else {
             throw new Exception\ResourceConflictException('ERR_FILE_UPLOAD_BY_CONFLICT_NAME');
         }
     } else {
         throw new Exception\RuntimeException('ERR_FILE_SAVE_TO_DB_FAILED');
     }
     return $this;
 }
예제 #3
0
function databasebackup_ALL(Web $w)
{
    $w->Admin->navigation($w, "Database Backup");
    $datestamp = date("Y-m-d-H-i");
    $filedir = ROOT_PATH . "/backups/";
    $dir = new DirectoryIterator($filedir);
    foreach ($dir as $fileinfo) {
        if (!$fileinfo->isDot()) {
            $filename = $fileinfo->getFilename();
            try {
                $datepart = substr($filename, 0, strpos($filename, ".sql"));
                $backuptime = DateTime::createFromFormat("Y-m-d-H-i", $datepart);
                if ($backuptime) {
                    if (time() - $backuptime->getTimestamp() < 60 * 60 * 4) {
                        $w->out("You cannot backup more than once every 4 hours");
                        return;
                    }
                }
            } catch (Exception $e) {
                // Invalid timestamp
            }
        }
    }
    $backupformat = Config::get('admin.database.output');
    $filename = "{$datestamp}.{$backupformat}";
    $command = NULL;
    if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {
        $command = Config::get('admin.database.command.windows');
    } else {
        $command = Config::get('admin.database.command.unix');
    }
    if (!empty($command)) {
        $command = str_replace(array('$username', '$password', '$dbname', '$filename'), array(Config::get('database.username'), Config::get('database.password'), Config::get('database.database'), $filedir . $filename), $command);
        $w->out(shell_exec($command));
        $w->out("Backup completed to: {$filedir}{$filename}");
        // Save elsewhere if defined
        $backuplocations = Config::get('admin.database.backuplocations');
        if (!empty($backuplocations)) {
            foreach ($backuplocations as $location => $data) {
                $adapter = null;
                // Create adapter
                switch ($location) {
                    case 'dropbox':
                        // Dropbox requires the OAuth extension
                        if (!class_exists("OAuth")) {
                            $w->out("You need the OAuth extension installed to backup to dropbox");
                            continue;
                        }
                        $dropboxapi = new Dropbox_API(new Dropbox_OAuth_PHP($data['key'], $data['secret']));
                        $dropboxadapter = new Gaufrette\Adapter\Dropbox($dropboxapi);
                        $adapter = new \Gaufrette\Adapter($dropboxadapter);
                        break;
                }
                // Use adapter to save to external source
                if (!empty($adapter)) {
                    $filesystem = new Gaufrette\Filesystem($adapter);
                    $filesystem->write($filedir . $filename, file_get_contents($filedir . $filename));
                }
            }
        }
    } else {
        $w->out("Could not find backup command");
    }
}