/**
  * Creates the cache directory on demand
  *
  * @param sfFilebasePluginDirectory | string: The pathname of the cache directory.
  * @return sfFilebasePluginDirectory $cache_directory
  */
 public function initCacheDirectory($cache_directory)
 {
     $this->cacheDirectory = $this->filebase->getFilebaseFile($cache_directory);
     if (!$this->cacheDirectory->fileExists()) {
         $this->cacheDirectory = $this->filebase->mkDir($this->cacheDirectory);
     }
     return $this->cacheDirectory;
 }
 /**
  * Returns filename for a cached thumbnail, calculated
  * by its properties and dimensions.
  *
  * @param sfFilebasePluginFile $file
  * @param array $thumbnail_properties
  * @return sfFilebasePluginImage $filename
  */
 public function getThumbnailFileinfo(sfFilebasePluginImage $file, $dimensions, $mime)
 {
     $thumbnail_properties = $this->getScaledImageData($file, $dimensions);
     // Wrap in sfFilebasePluginImage because isImage may return false if file does not exist.
     return new sfFilebasePluginThumbnail($this->filebase->getFilebaseFile($this->filebase->getCacheDirectory() . DIRECTORY_SEPARATOR . $this->filebase->getHashForFile($file) . '_' . $thumbnail_properties['new_width'] . '_' . $thumbnail_properties['new_height'] . '.' . (sfFilebasePluginUtil::getExtensionByMime($mime) === null ? $thumbnail_properties['extension'] : sfFilebasePluginUtil::getExtensionByMime($mime))), $this->filebase, $file);
 }
 /**
  * Moves an uploaded File to specified Destination.
  * Inclusion and exclusion rules consist of regex-strings,
  * they are used to check the target filenames against them.
  * Exclusion:   Matched filenames throw exceptions.
  * Inclusion:  Matched filenames will be passed as valid filenames.
  *
  * You should wrap this method into a try-catch block because many
  * things can go wrong during or after the upload, so if you upload
  * many files and did not validate them before, this would be a good
  * starting point do to some "low level" validation.
  *
  * $destination can be a string (absolute or relative pathname) or an
  * instance of sfFilebasePluginFile, it is the directory, not the full pathName of
  * the new file. The file can be renamed by setting $file_name, otherwise
  * the original name will be taken as filename.
  *
  * @param mixed $tmp_file
  * @param mixed $destination_directory: The directory the file will be moved in.
  * @param boolean $allow_overwrite True if existing files should be overwritten
  * @param array $inclusion_rules
  * @param array $exclusion_rules
  * @param string $file_name: If given, file will be renamed when moving.
  * @throws sfFilebasePluginException
  * @return sfFilebasePluginFile $moved_file
  */
 public function moveUploadedFile(sfFilebasePluginUploadedFile $tmp_file, $destination_directory, $allow_overwrite = true, $chmod = 0777, array $inclusion_rules = array(), $exclusion_rules = array(), $file_name = null)
 {
     $destination_directory = $this->filebase->getFilebaseFile($destination_directory);
     // Error handling
     if ($tmp_file->isError()) {
         switch ($tmp_file->getError()) {
             case sfFilebasePluginUploadedFile::UPLOAD_ERR_EXTENSION:
                 throw new sfFilebasePluginException('Upload error thrown by extension.', sfFilebasePluginUploadedFile::UPLOAD_ERR_EXTENSION);
             case sfFilebasePluginUploadedFile::UPLOAD_ERR_PARTIAL:
                 throw new sfFilebasePluginException('Upload error: Upload interrupted.', sfFilebasePluginUploadedFile::UPLOAD_ERR_PARTIAL);
             case sfFilebasePluginUploadedFile::UPLOAD_ERR_CANT_WRITE:
                 throw new sfFilebasePluginException('Upload error: Cannot write temporary file.', sfFilebasePluginUploadedFile::UPLOAD_ERR_CANT_WRITE);
             case sfFilebasePluginUploadedFile::UPLOAD_ERR_EXTENSION:
                 throw new sfFilebasePluginException('Upload error: Extension error.', sfFilebasePluginUploadedFile::UPLOAD_ERR_EXTENSION);
             case sfFilebasePluginUploadedFile::UPLOAD_ERR_FORM_SIZE:
                 throw new sfFilebasePluginException('Upload error: Form size exceeded.', sfFilebasePluginUploadedFile::UPLOAD_ERR_FORM_SIZE);
             case sfFilebasePluginUploadedFile::UPLOAD_ERR_INI_SIZE:
                 throw new sfFilebasePluginException('Upload error: Ini size exceeded.', sfFilebasePluginUploadedFile::UPLOAD_ERR_INI_SIZE);
             case sfFilebasePluginUploadedFile::UPLOAD_ERR_NO_FILE:
                 throw new sfFilebasePluginException('Upload error: no file was uploaded.', sfFilebasePluginUploadedFile::UPLOAD_ERR_NO_FILE);
             case sfFilebasePluginUploadedFile::UPLOAD_ERR_NO_TMP_DIR:
                 throw new sfFilebasePluginException('Upload error: There is no temp dir.', sfFilebasePluginUploadedFile::UPLOAD_ERR_NO_TMP_DIR);
                 return false;
         }
     }
     // Validation, excuting inclusion rules
     foreach ($inclusion_rules as $rule) {
         if (!preg_match($rule, $tmp_file->getOriginalName())) {
             throw new sfFilebasePluginException(sprintf('Inclusion-Validation failed while uploading file %s: Rule %s', $tmp_file->getOriginalName(), $rule));
         }
     }
     // Valdating using exclusion rules
     foreach ($exclusion_rules as $rule) {
         if (preg_match($rule, $tmp_file->getOriginalName())) {
             throw new sfFilebasePluginException(sprintf('Exclusion-Validation failed while uploading file %s: Rule %s', $tmp_file->getOriginalName(), $rule));
         }
     }
     if (!$destination_directory instanceof sfFilebasePluginDirectory) {
         throw new sfFilebasePluginException(sprintf('Destination %s is not a directory.', $destination_directory->getPathname()));
     }
     $destination = null;
     // Filename given? Rename file...
     if ($file_name === null) {
         $destination = $this->filebase->getFilebaseFile($destination_directory->getPathname() . '/' . $tmp_file->getOriginalName());
     } else {
         $file_name = (string) $file_name;
         if (strlen($file_name > 0)) {
             $destination = $this->filebase->getFilebaseFile($destination_directory->getPathname() . '/' . $file_name);
         }
     }
     if (!$this->filebase->isInFilebase($destination)) {
         throw new sfFilebasePluginException(sprintf('Destination %s does not lie within Filebase %s, access denied due to security reasons.', $destination->getPathname(), $this->filebase->getPathname()));
     }
     if ($destination->fileExists()) {
         if ($allow_overwrite) {
             if (!$destination->isWritable()) {
                 throw new sfFilebasePluginException(sprintf('File %s is write protected.', $destination->getPathname()));
             }
         } else {
             throw new sfFilebasePluginException(sprintf('Destination file %s already exists', $destination->getPathname()));
         }
     } else {
         if (!$destination_directory->isWritable()) {
             throw new sfFilebasePluginException(sprintf('Destination directory %s is write protected', $destination_directory->getPathname()));
         }
     }
     if (!@move_uploaded_file($tmp_file->getTempName(), $destination->getPathname())) {
         throw new sfFilebasePluginException(sprintf('Error while moving uploaded file %s', $tmp_file->getOriginalName()));
     }
     $destination->chmod($chmod);
     return $destination;
 }
Esempio n. 4
0
$t->isa_ok($f->mkDir('my_dir'), 'sfFilebasePluginDirectory', 'Directory "my_dir" successfully created by sfFilebasePlugin::mkDir().');
$t->isa_ok($dir = $dir->rename('my_other_dir'), 'sfFilebasePluginDirectory', 'Directory "my_dir" was successfully renamed to "my_other_dir"');
$t->isa_ok($dir = $dir->rename('my_dir'), 'sfFilebasePluginDirectory', 'Directory re-renamed to "my_dir"');
###  CHMOD
$t->diag('Let\'s chmod the directory');
$t->isa_ok($dir->chmod(0755), 'sfFilebasePluginDirectory', 'chmod() 0755 successful.');
$t->isa_ok($dir->chmod(0777), 'sfFilebasePluginDirectory', 'chmod() 0777 successful.');
### File paths
$t->diag('I want to get the paths');
$t->isa_ok($path = $dir->getRelativePathFromFilebaseDirectory(), 'string', 'getRelativePathFromFilebaseDirectory() returns string');
$t->is(file_exists($f . '/' . $path), true, 'The path was successfully rendered: file_exists() on ' . $f . '/' . $path . ' returns true');
$t->isa_ok($path = $dir->getAbsolutePathFromWebroot(), 'string', 'getAbsolutePathFromWebroot() returns string');
### Copy Files
$t->diag('I want to perform some file operations, but before that i have to copy a file into filebase...');
$t->diag('Fetching test-image');
$t->isa_ok($image = $f->getFilebaseFile(dirname(__FILE__) . '/' . 'test.JPG'), 'sfFilebasePluginImage', 'Image fetched ./test.JPG as instance of sfFilebasePluginImage');
$t->diag('Copying test-image to filebase');
$t->isa_ok($copy = $image->copy($dir . '/test.jpg'), 'sfFilebasePluginImage', 'copy() returns instanceof sfFilebasePluginImage');
$t->diag('Does the copied image exist at the destinated location?');
$t->is($copy->fileExists(), true, 'fileExists() returns true');
$t->diag('Let\'s create a thumbnail...');
$t->isa_ok($tn = $copy->getThumbnail(array('width' => 20)), 'sfFilebasePluginThumbnail', 'Thumbnail created as instanceof sfFilebasePluginThumbnail');
$t->diag('This is nonsense: Open thumbnail and print its content to stdout:');
$t->diag($tn->openFile()->fileGetContents() . "..");
$t->is($tn->fileExists(), true, 'sfFilebasePluginThumbnail::fileExists() returns true.');
$t->isa_ok($copy = $copy->rename('renamed_image.jpg'), 'sfFilebasePluginImage', 'Performing a rename(), returns instanceof sfFilebasePluginImage.');
$t->is($copy->fileExists(), true, 'fileExists() returns true, so renamed image exists in file system.');
$t->diag('Lets resize the original image');
$t->isa_ok($copy->resize(array('1000')), 'sfFilebasePluginImage', 'Resize ok, resize() returns instance of sfFilebasePluginImage');
$t->is($copy->getWidth(), 1000, 'Image now has 1000 pixels width.');
$t->is($copy->getMimeType(), 'image/jpeg', 'getMimeType() says: Image has mime-type image/jpeg');
Esempio n. 5
0
 /**
  * Sets the pathname for one tuple
  *
  * @param sfFilebasePluginFile | string $file
  * @return Doctrine_Template_File
  */
 public function setFile($file)
 {
     $file = $this->filebase->getFilebaseFile($file);
     $this->_invoker[$this->getOption('name')] = $file->getRelativePathFromFilebaseDirectory();
     return $this;
 }