public function setup() { parent::setup(); unset($this->widgetSchema['type']); unset($this->widgetSchema['lft']); unset($this->widgetSchema['rgt']); unset($this->widgetSchema['level']); unset($this->validatorSchema['type']); unset($this->validatorSchema['lft']); unset($this->validatorSchema['rgt']); unset($this->validatorSchema['level']); $this->validatorSchema['tags'] = new sfValidatorAnd(array(new sfValidatorString(), new sfValidatorRegex(array('pattern' => '#^[^, ;]([^, ;]+[,; ] ?)*?[^, ;]+$#'))), array('required' => false)); $directory_choices = sfFilebaseDirectoryTable::getChoices(); $this->widgetSchema['directory'] = new sfWidgetFormTree(array('value_key' => 'id', 'choices' => $directory_choices, 'label_key' => 'filename')); $this->validatorSchema['directory'] = new sfValidatorTree(array('value_key' => 'id', 'label_key' => 'filename', 'choices' => $directory_choices)); if ($this->isNew()) { unset($this->widgetSchema['filename']); unset($this->validatorSchema['filename']); $this->widgetSchema['hash'] = new sfWidgetFormInputSWFUpload(array('require_yui' => true, 'custom_javascripts' => array(public_path('/sfFilebasePlugin/js/fileupload.js')))); $this->validatorSchema['hash'] = $this->filebase->getUploadedFilesManager()->createFileUploadValidator(array('path' => $this->filebase->getPathname(), 'allow_overwrite' => true, 'required' => true)); $this->widgetSchema['directory']->setDefault($directory_choices[0]['id']); } else { unset($this->widgetSchema['hash']); unset($this->validatorSchema['hash']); $this->validatorSchema['filename'] = new sfValidatorString(); $p = $this->getObject()->getNode()->getParent(); $parent_id = $p instanceof sfFilebaseDirectory ? $p->getId() : 0; $this->widgetSchema['directory']->setDefault($parent_id); } $this->validatorSchema->setPostValidator(new sfValidatorCallback(array('callback' => array($this, 'checkUpload')))); }
/** * Constructs a filebase cache * @param sfFilebasePlugin $filebase */ public function __construct(sfFilebasePlugin $filebase, $cache_directory = null) { $this->filebase = $filebase; if ($cache_directory === null) { $cache_directory = $this->filebase->getPathname() . '/.' . md5(get_class($this)); } // CREATE THE CACHE DIRECTORY IF NEEDED $this->initCacheDirectory($cache_directory); }
/** * Creates a Thumbnail named by md5-hash of the image * and its file ending. * * @param mixed sfFilebasePluginImage $fileinfo * @param array $dimensions = array(width, height) */ public function createThumbnail(sfFilebasePluginImage $fileinfo, array $dimensions, $quality, $mime, $preserve_transparency = true) { // Check cache directory if (!$this->filebase->getCacheDirectory()->fileExists()) { throw new sfFilebasePluginException(sprintf('Cache directory %s does not exist.', $this->filebase->getCacheDirectory()->getPathname())); } // Check if original file is writable... if (!$fileinfo->fileExists()) { throw new sfFilebasePluginException(sprintf('File %s does not exist', $fileinfo->getPathname())); } if (!$fileinfo->isReadable()) { throw new sfFilebasePluginException(sprintf('File %s is write protected.', $fileinfo->getPathname())); } if (!$this->filebase->getIsSupportedImage($fileinfo)) { throw new sfFilebasePluginException(sprintf('File %s is not an image.', $fileinfo)); } if (!$fileinfo->isImage()) { throw new sfFilebasePluginException(sprintf('Image format %s is unsupported.', $fileinfo)); } if (!$this->filebase->isInFilebase($fileinfo)) { throw new sfFilebasePluginException(sprintf('FilebaseFile %s does not belong to Filebase %s, cannot be deleted due to security issues', $fileinfo->getPathname(), $this->filebase->getPathname())); } $destination = $this->getThumbnailFileinfo($fileinfo, $dimensions, $mime); return new sfFilebasePluginThumbnail($this->imageCopyResampled($fileinfo, $destination, $dimensions, true, $preserve_transparency), $this->filebase, $fileinfo); }
/** * 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; }