Exemple #1
0
 /**
  * Begins a transaction, or increases the level
  * 
  * @internal
  * 
  * @return void
  */
 public static function begin()
 {
     // If the transaction was started by something else, don't even track it
     if (self::$transaction_level == 0 && fFilesystem::isInsideTransaction()) {
         return;
     }
     self::$transaction_level++;
     if (!fFilesystem::isInsideTransaction()) {
         fFilesystem::begin();
     }
 }
Exemple #2
0
 /**
  * Writes the provided data to the file
  * 
  * Requires all previous data to be stored in memory if inside a
  * transaction, use with caution on large files!
  * 
  * If a filesystem transaction is in progress and is rolled back, the
  * previous data will be restored.
  * 
  * @param  mixed $data  The data to write to the file
  * @return fFile  The file object, to allow for method chaining
  */
 public function write($data)
 {
     $this->tossIfDeleted();
     if (!$this->isWritable()) {
         throw new fEnvironmentException('This file, %s, can not be written to because it is not writable', $this->file);
     }
     // Allow filesystem transactions
     if (fFilesystem::isInsideTransaction()) {
         fFilesystem::recordWrite($this);
     }
     file_put_contents($this->file, $data);
     return $this;
 }
 /**
  * Renames the current directory
  * 
  * This operation will NOT be performed until the filesystem transaction
  * has been committed, if a transaction is in progress. Any non-Flourish
  * code (PHP or system) will still see this directory (and all contained
  * files/dirs) as existing with the old paths until that point.
  * 
  * @param  string  $new_dirname  The new full path to the directory or a new name in the current parent directory
  * @param  boolean $overwrite    If the new dirname already exists, TRUE will cause the file to be overwritten, FALSE will cause the new filename to change
  * @return void
  */
 public function rename($new_dirname, $overwrite)
 {
     $this->tossIfDeleted();
     if (!$this->getParent()->isWritable()) {
         throw new fEnvironmentException('The directory, %s, can not be renamed because the directory containing it is not writable', $this->directory);
     }
     // If the dirname does not contain any folder traversal, rename the dir in the current parent directory
     if (preg_match('#^[^/\\\\]+$#D', $new_dirname)) {
         $new_dirname = $this->getParent()->getPath() . $new_dirname;
     }
     $info = fFilesystem::getPathInfo($new_dirname);
     if (!file_exists($info['dirname'])) {
         throw new fProgrammerException('The new directory name specified, %s, is inside of a directory that does not exist', $new_dirname);
     }
     if (file_exists($new_dirname)) {
         if (!is_writable($new_dirname)) {
             throw new fEnvironmentException('The new directory name specified, %s, already exists, but is not writable', $new_dirname);
         }
         if (!$overwrite) {
             $new_dirname = fFilesystem::makeUniqueName($new_dirname);
         }
     } else {
         $parent_dir = new fDirectory($info['dirname']);
         if (!$parent_dir->isWritable()) {
             throw new fEnvironmentException('The new directory name specified, %s, is inside of a directory that is not writable', $new_dirname);
         }
     }
     rename($this->directory, $new_dirname);
     // Make the dirname absolute
     $new_dirname = fDirectory::makeCanonical(realpath($new_dirname));
     // Allow filesystem transactions
     if (fFilesystem::isInsideTransaction()) {
         fFilesystem::rename($this->directory, $new_dirname);
     }
     fFilesystem::updateFilenameMapForDirectory($this->directory, $new_dirname);
 }
Exemple #4
0
 /**
  * Saves any changes to the image
  * 
  * If the file type is different than the current one, removes the current
  * file once the new one is created.
  * 
  * This operation will be reverted by a filesystem transaction being rolled
  * back. If a transaction is in progress and the new image type causes a
  * new file to be created, the old file will not be deleted until the
  * transaction is committed.
  * 
  * @param  string  $new_image_type  The new file format for the image: 'NULL` (no change), `'jpg'`, `'gif'`, `'png'`
  * @param  integer $jpeg_quality    The quality setting to use for JPEG images - this may be ommitted
  * @param  boolean $overwrite       If an existing file with the same name and extension should be overwritten
  * @param  string  :$new_image_type
  * @param  boolean :$overwrite
  * @return fImage  The image object, to allow for method chaining
  */
 public function saveChanges($new_image_type = NULL, $jpeg_quality = 90, $overwrite = FALSE)
 {
     // This allows ommitting the $jpeg_quality parameter, which is very useful for non-jpegs
     $args = func_get_args();
     if (count($args) == 2 && is_bool($args[1])) {
         $overwrite = $args[1];
         $jpeg_quality = 90;
     }
     $this->tossIfDeleted();
     self::determineProcessor();
     if (self::$processor == 'none') {
         throw new fEnvironmentException("The changes to the image can't be saved because neither the GD extension or ImageMagick appears to be installed on the server");
     }
     $type = self::getImageType($this->file);
     if ($type == 'tif' && self::$processor == 'gd') {
         throw new fEnvironmentException('The image specified, %s, is a TIFF file and the GD extension can not handle TIFF files. Please install ImageMagick if you wish to manipulate TIFF files.', $this->file);
     }
     $valid_image_types = array('jpg', 'gif', 'png');
     if ($new_image_type !== NULL && !in_array($new_image_type, $valid_image_types)) {
         throw new fProgrammerException('The new image type specified, %1$s, is invalid. Must be one of: %2$s.', $new_image_type, join(', ', $valid_image_types));
     }
     if (is_numeric($jpeg_quality)) {
         $jpeg_quality = (int) round($jpeg_quality);
     }
     if (!is_integer($jpeg_quality) || $jpeg_quality < 1 || $jpeg_quality > 100) {
         throw new fProgrammerException('The JPEG quality specified, %1$s, is either not an integer, less than %2$s or greater than %3$s.', $jpeg_quality, 1, 100);
     }
     if ($new_image_type && fFilesystem::getPathInfo($this->file, 'extension') != $new_image_type) {
         if ($overwrite) {
             $path_info = fFilesystem::getPathInfo($this->file);
             $output_file = $path_info['dirname'] . $path_info['filename'] . '.' . $new_image_type;
         } else {
             $output_file = fFilesystem::makeUniqueName($this->file, $new_image_type);
         }
         if (file_exists($output_file)) {
             if (!is_writable($output_file)) {
                 throw new fEnvironmentException('Changes to the image can not be saved because the file, %s, is not writable', $output_file);
             }
         } else {
             $output_dir = dirname($output_file);
             if (!is_writable($output_dir)) {
                 throw new fEnvironmentException('Changes to the image can not be saved because the directory to save the new file, %s, is not writable', $output_dir);
             }
         }
     } else {
         $output_file = $this->file;
         if (!is_writable($output_file)) {
             throw new fEnvironmentException('Changes to the image can not be saved because the file, %s, is not writable', $output_file);
         }
     }
     // If we don't have any changes and no name change, just exit
     if (!$this->pending_modifications && $output_file == $this->file) {
         return $this;
     }
     // Wrap changes to the image into the filesystem transaction
     if ($output_file == $this->file && fFilesystem::isInsideTransaction()) {
         fFilesystem::recordWrite($this);
     }
     if (self::$processor == 'gd') {
         $this->processWithGD($output_file, $jpeg_quality);
     } elseif (self::$processor == 'imagemagick') {
         $this->processWithImageMagick($output_file, $jpeg_quality);
     }
     $old_file = $this->file;
     fFilesystem::updateFilenameMap($this->file, $output_file);
     // If we created a new image, delete the old one
     if ($output_file != $old_file) {
         $old_image = new fImage($old_file);
         $old_image->delete();
     }
     $this->pending_modifications = array();
     return $this;
 }