File indexing completed on 2024-12-22 05:36:41

0001 <?php
0002 /**
0003  * Zend Framework
0004  *
0005  * LICENSE
0006  *
0007  * This source file is subject to the new BSD license that is bundled
0008  * with this package in the file LICENSE.txt.
0009  * It is also available through the world-wide-web at this URL:
0010  * http://framework.zend.com/license/new-bsd
0011  * If you did not receive a copy of the license and are unable to
0012  * obtain it through the world-wide-web, please send an email
0013  * to license@zend.com so we can send you a copy immediately.
0014  *
0015  * @category   Zend
0016  * @package    Zend_Filter
0017  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
0018  * @license    http://framework.zend.com/license/new-bsd     New BSD License
0019  * @version    $Id$
0020  */
0021 
0022 /**
0023  * @see Zend_Filter_Interface
0024  */
0025 // require_once 'Zend/Filter/Interface.php';
0026 
0027 /**
0028  * @category   Zend
0029  * @package    Zend_Filter
0030  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
0031  * @license    http://framework.zend.com/license/new-bsd     New BSD License
0032  */
0033 class Zend_Filter_File_Rename implements Zend_Filter_Interface
0034 {
0035     /**
0036      * Internal array of array(source, target, overwrite)
0037      */
0038     protected $_files = array();
0039 
0040     /**
0041      * Class constructor
0042      *
0043      * Options argument may be either a string, a Zend_Config object, or an array.
0044      * If an array or Zend_Config object, it accepts the following keys:
0045      * 'source'    => Source filename or directory which will be renamed
0046      * 'target'    => Target filename or directory, the new name of the sourcefile
0047      * 'overwrite' => Shall existing files be overwritten ?
0048      *
0049      * @param  string|array $options Target file or directory to be renamed
0050      * @param  string $target Source filename or directory (deprecated)
0051      * @param  bool $overwrite Should existing files be overwritten (deprecated)
0052      * @return void
0053      */
0054     public function __construct($options)
0055     {
0056         if ($options instanceof Zend_Config) {
0057             $options = $options->toArray();
0058         } elseif (is_string($options)) {
0059             $options = array('target' => $options);
0060         } elseif (!is_array($options)) {
0061             // require_once 'Zend/Filter/Exception.php';
0062             throw new Zend_Filter_Exception('Invalid options argument provided to filter');
0063         }
0064 
0065         if (1 < func_num_args()) {
0066             $argv = func_get_args();
0067             array_shift($argv);
0068             $source    = array_shift($argv);
0069             $overwrite = false;
0070             if (!empty($argv)) {
0071                 $overwrite = array_shift($argv);
0072             }
0073             $options['source']    = $source;
0074             $options['overwrite'] = $overwrite;
0075         }
0076 
0077         $this->setFile($options);
0078     }
0079 
0080     /**
0081      * Returns the files to rename and their new name and location
0082      *
0083      * @return array
0084      */
0085     public function getFile()
0086     {
0087         return $this->_files;
0088     }
0089 
0090     /**
0091      * Sets a new file or directory as target, deleting existing ones
0092      *
0093      * Array accepts the following keys:
0094      * 'source'    => Source filename or directory which will be renamed
0095      * 'target'    => Target filename or directory, the new name of the sourcefile
0096      * 'overwrite' => Shall existing files be overwritten ?
0097      *
0098      * @param  string|array $options Old file or directory to be rewritten
0099      * @return Zend_Filter_File_Rename
0100      */
0101     public function setFile($options)
0102     {
0103         $this->_files = array();
0104         $this->addFile($options);
0105 
0106         return $this;
0107     }
0108 
0109     /**
0110      * Adds a new file or directory as target to the existing ones
0111      *
0112      * Array accepts the following keys:
0113      * 'source'    => Source filename or directory which will be renamed
0114      * 'target'    => Target filename or directory, the new name of the sourcefile
0115      * 'overwrite' => Shall existing files be overwritten ?
0116      *
0117      * @param  string|array $options Old file or directory to be rewritten
0118      * @return Zend_Filter_File_Rename
0119      */
0120     public function addFile($options)
0121     {
0122         if (is_string($options)) {
0123             $options = array('target' => $options);
0124         } elseif (!is_array($options)) {
0125             // require_once 'Zend/Filter/Exception.php';
0126             throw new Zend_Filter_Exception ('Invalid options to rename filter provided');
0127         }
0128 
0129         $this->_convertOptions($options);
0130 
0131         return $this;
0132     }
0133 
0134     /**
0135      * Returns only the new filename without moving it
0136      * But existing files will be erased when the overwrite option is true
0137      *
0138      * @param  string  $value  Full path of file to change
0139      * @param  boolean $source Return internal informations
0140      * @return string The new filename which has been set
0141      */
0142     public function getNewName($value, $source = false)
0143     {
0144         $file = $this->_getFileName($value);
0145         
0146         if (!is_array($file) || !array_key_exists('source', $file) || !array_key_exists('target', $file)) {
0147             return $value;
0148         }
0149         
0150         if ($file['source'] == $file['target']) {
0151             return $value;
0152         }
0153 
0154         if (!file_exists($file['source'])) {
0155             return $value;
0156         }
0157 
0158         if (($file['overwrite'] == true) && (file_exists($file['target']))) {
0159             unlink($file['target']);
0160         }
0161 
0162         if (file_exists($file['target'])) {
0163             // require_once 'Zend/Filter/Exception.php';
0164             throw new Zend_Filter_Exception(sprintf("File '%s' could not be renamed. It already exists.", $value));
0165         }
0166 
0167         if ($source) {
0168             return $file;
0169         }
0170 
0171         return $file['target'];
0172     }
0173 
0174     /**
0175      * Defined by Zend_Filter_Interface
0176      *
0177      * Renames the file $value to the new name set before
0178      * Returns the file $value, removing all but digit characters
0179      *
0180      * @param  string $value Full path of file to change
0181      * @throws Zend_Filter_Exception
0182      * @return string The new filename which has been set, or false when there were errors
0183      */
0184     public function filter($value)
0185     {
0186         $file   = $this->getNewName($value, true);
0187         if (is_string($file)) {
0188             return $file;
0189         }
0190 
0191         $result = rename($file['source'], $file['target']);
0192 
0193         if ($result === true) {
0194             return $file['target'];
0195         }
0196 
0197         // require_once 'Zend/Filter/Exception.php';
0198         throw new Zend_Filter_Exception(sprintf("File '%s' could not be renamed. An error occured while processing the file.", $value));
0199     }
0200 
0201     /**
0202      * Internal method for creating the file array
0203      * Supports single and nested arrays
0204      *
0205      * @param  array $options
0206      * @return array
0207      */
0208     protected function _convertOptions($options) {
0209         $files = array();
0210         foreach ($options as $key => $value) {
0211             if (is_array($value)) {
0212                 $this->_convertOptions($value);
0213                 continue;
0214             }
0215 
0216             switch ($key) {
0217                 case "source":
0218                     $files['source'] = (string) $value;
0219                     break;
0220 
0221                 case 'target' :
0222                     $files['target'] = (string) $value;
0223                     break;
0224 
0225                 case 'overwrite' :
0226                     $files['overwrite'] = (boolean) $value;
0227                     break;
0228 
0229                 default:
0230                     break;
0231             }
0232         }
0233 
0234         if (empty($files)) {
0235             return $this;
0236         }
0237 
0238         if (empty($files['source'])) {
0239             $files['source'] = '*';
0240         }
0241 
0242         if (empty($files['target'])) {
0243             $files['target'] = '*';
0244         }
0245 
0246         if (empty($files['overwrite'])) {
0247             $files['overwrite'] = false;
0248         }
0249 
0250         $found = false;
0251         foreach ($this->_files as $key => $value) {
0252             if ($value['source'] == $files['source']) {
0253                 $this->_files[$key] = $files;
0254                 $found              = true;
0255             }
0256         }
0257 
0258         if (!$found) {
0259             $count                = count($this->_files);
0260             $this->_files[$count] = $files;
0261         }
0262 
0263         return $this;
0264     }
0265 
0266     /**
0267      * Internal method to resolve the requested source
0268      * and return all other related parameters
0269      *
0270      * @param  string $file Filename to get the informations for
0271      * @return array
0272      */
0273     protected function _getFileName($file)
0274     {
0275         $rename = array();
0276         foreach ($this->_files as $value) {
0277             if ($value['source'] == '*') {
0278                 if (!isset($rename['source'])) {
0279                     $rename           = $value;
0280                     $rename['source'] = $file;
0281                 }
0282             }
0283 
0284             if ($value['source'] == $file) {
0285                 $rename = $value;
0286             }
0287         }
0288 
0289         if (!isset($rename['source'])) {
0290             return $file;
0291         }
0292 
0293         if (!isset($rename['target']) or ($rename['target'] == '*')) {
0294             $rename['target'] = $rename['source'];
0295         }
0296 
0297         if (is_dir($rename['target'])) {
0298             $name = basename($rename['source']);
0299             $last = $rename['target'][strlen($rename['target']) - 1];
0300             if (($last != '/') and ($last != '\\')) {
0301                 $rename['target'] .= DIRECTORY_SEPARATOR;
0302             }
0303 
0304             $rename['target'] .= $name;
0305         }
0306 
0307         return $rename;
0308     }
0309 }