PageRenderTime 94ms CodeModel.GetById 28ms RepoModel.GetById 1ms app.codeStats 0ms

/system/Zend/Filter/File/Rename.php

https://gitlab.com/Ltaimao/wecenter
PHP | 309 lines | 160 code | 40 blank | 109 comment | 39 complexity | 2669c308336ca7df630db9fb3727b61f MD5 | raw file
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_Filter
  17. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  18. * @license http://framework.zend.com/license/new-bsd New BSD License
  19. * @version $Id$
  20. */
  21. /**
  22. * @see Zend_Filter_Interface
  23. */
  24. //require_once 'Zend/Filter/Interface.php';
  25. /**
  26. * @category Zend
  27. * @package Zend_Filter
  28. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  29. * @license http://framework.zend.com/license/new-bsd New BSD License
  30. */
  31. class Zend_Filter_File_Rename implements Zend_Filter_Interface
  32. {
  33. /**
  34. * Internal array of array(source, target, overwrite)
  35. */
  36. protected $_files = array();
  37. /**
  38. * Class constructor
  39. *
  40. * Options argument may be either a string, a Zend_Config object, or an array.
  41. * If an array or Zend_Config object, it accepts the following keys:
  42. * 'source' => Source filename or directory which will be renamed
  43. * 'target' => Target filename or directory, the new name of the sourcefile
  44. * 'overwrite' => Shall existing files be overwritten ?
  45. *
  46. * @param string|array $options Target file or directory to be renamed
  47. * @param string $target Source filename or directory (deprecated)
  48. * @param bool $overwrite Should existing files be overwritten (deprecated)
  49. * @return void
  50. */
  51. public function __construct($options)
  52. {
  53. if ($options instanceof Zend_Config) {
  54. $options = $options->toArray();
  55. } elseif (is_string($options)) {
  56. $options = array('target' => $options);
  57. } elseif (!is_array($options)) {
  58. //require_once 'Zend/Filter/Exception.php';
  59. throw new Zend_Filter_Exception('Invalid options argument provided to filter');
  60. }
  61. if (1 < func_num_args()) {
  62. $argv = func_get_args();
  63. array_shift($argv);
  64. $source = array_shift($argv);
  65. $overwrite = false;
  66. if (!empty($argv)) {
  67. $overwrite = array_shift($argv);
  68. }
  69. $options['source'] = $source;
  70. $options['overwrite'] = $overwrite;
  71. }
  72. $this->setFile($options);
  73. }
  74. /**
  75. * Returns the files to rename and their new name and location
  76. *
  77. * @return array
  78. */
  79. public function getFile()
  80. {
  81. return $this->_files;
  82. }
  83. /**
  84. * Sets a new file or directory as target, deleting existing ones
  85. *
  86. * Array accepts the following keys:
  87. * 'source' => Source filename or directory which will be renamed
  88. * 'target' => Target filename or directory, the new name of the sourcefile
  89. * 'overwrite' => Shall existing files be overwritten ?
  90. *
  91. * @param string|array $options Old file or directory to be rewritten
  92. * @return Zend_Filter_File_Rename
  93. */
  94. public function setFile($options)
  95. {
  96. $this->_files = array();
  97. $this->addFile($options);
  98. return $this;
  99. }
  100. /**
  101. * Adds a new file or directory as target to the existing ones
  102. *
  103. * Array accepts the following keys:
  104. * 'source' => Source filename or directory which will be renamed
  105. * 'target' => Target filename or directory, the new name of the sourcefile
  106. * 'overwrite' => Shall existing files be overwritten ?
  107. *
  108. * @param string|array $options Old file or directory to be rewritten
  109. * @return Zend_Filter_File_Rename
  110. */
  111. public function addFile($options)
  112. {
  113. if (is_string($options)) {
  114. $options = array('target' => $options);
  115. } elseif (!is_array($options)) {
  116. //require_once 'Zend/Filter/Exception.php';
  117. throw new Zend_Filter_Exception ('Invalid options to rename filter provided');
  118. }
  119. $this->_convertOptions($options);
  120. return $this;
  121. }
  122. /**
  123. * Returns only the new filename without moving it
  124. * But existing files will be erased when the overwrite option is true
  125. *
  126. * @param string $value Full path of file to change
  127. * @param boolean $source Return internal informations
  128. * @return string The new filename which has been set
  129. */
  130. public function getNewName($value, $source = false)
  131. {
  132. $file = $this->_getFileName($value);
  133. if (!is_array($file) || !array_key_exists('source', $file) || !array_key_exists('target', $file)) {
  134. return $value;
  135. }
  136. if ($file['source'] == $file['target']) {
  137. return $value;
  138. }
  139. if (!file_exists($file['source'])) {
  140. return $value;
  141. }
  142. if (($file['overwrite'] == true) && (file_exists($file['target']))) {
  143. unlink($file['target']);
  144. }
  145. if (file_exists($file['target'])) {
  146. //require_once 'Zend/Filter/Exception.php';
  147. throw new Zend_Filter_Exception(sprintf("File '%s' could not be renamed. It already exists.", $value));
  148. }
  149. if ($source) {
  150. return $file;
  151. }
  152. return $file['target'];
  153. }
  154. /**
  155. * Defined by Zend_Filter_Interface
  156. *
  157. * Renames the file $value to the new name set before
  158. * Returns the file $value, removing all but digit characters
  159. *
  160. * @param string $value Full path of file to change
  161. * @throws Zend_Filter_Exception
  162. * @return string The new filename which has been set, or false when there were errors
  163. */
  164. public function filter($value)
  165. {
  166. $file = $this->getNewName($value, true);
  167. if (is_string($file)) {
  168. return $file;
  169. }
  170. $result = rename($file['source'], $file['target']);
  171. if ($result === true) {
  172. return $file['target'];
  173. }
  174. //require_once 'Zend/Filter/Exception.php';
  175. throw new Zend_Filter_Exception(sprintf("File '%s' could not be renamed. An error occured while processing the file.", $value));
  176. }
  177. /**
  178. * Internal method for creating the file array
  179. * Supports single and nested arrays
  180. *
  181. * @param array $options
  182. * @return array
  183. */
  184. protected function _convertOptions($options) {
  185. $files = array();
  186. foreach ($options as $key => $value) {
  187. if (is_array($value)) {
  188. $this->_convertOptions($value);
  189. continue;
  190. }
  191. switch ($key) {
  192. case "source":
  193. $files['source'] = (string) $value;
  194. break;
  195. case 'target' :
  196. $files['target'] = (string) $value;
  197. break;
  198. case 'overwrite' :
  199. $files['overwrite'] = (boolean) $value;
  200. break;
  201. default:
  202. break;
  203. }
  204. }
  205. if (empty($files)) {
  206. return $this;
  207. }
  208. if (empty($files['source'])) {
  209. $files['source'] = '*';
  210. }
  211. if (empty($files['target'])) {
  212. $files['target'] = '*';
  213. }
  214. if (empty($files['overwrite'])) {
  215. $files['overwrite'] = false;
  216. }
  217. $found = false;
  218. foreach ($this->_files as $key => $value) {
  219. if ($value['source'] == $files['source']) {
  220. $this->_files[$key] = $files;
  221. $found = true;
  222. }
  223. }
  224. if (!$found) {
  225. $count = count($this->_files);
  226. $this->_files[$count] = $files;
  227. }
  228. return $this;
  229. }
  230. /**
  231. * Internal method to resolve the requested source
  232. * and return all other related parameters
  233. *
  234. * @param string $file Filename to get the informations for
  235. * @return array
  236. */
  237. protected function _getFileName($file)
  238. {
  239. $rename = array();
  240. foreach ($this->_files as $value) {
  241. if ($value['source'] == '*') {
  242. if (!isset($rename['source'])) {
  243. $rename = $value;
  244. $rename['source'] = $file;
  245. }
  246. }
  247. if ($value['source'] == $file) {
  248. $rename = $value;
  249. }
  250. }
  251. if (!isset($rename['source'])) {
  252. return $file;
  253. }
  254. if (!isset($rename['target']) or ($rename['target'] == '*')) {
  255. $rename['target'] = $rename['source'];
  256. }
  257. if (is_dir($rename['target'])) {
  258. $name = basename($rename['source']);
  259. $last = $rename['target'][strlen($rename['target']) - 1];
  260. if (($last != '/') and ($last != '\\')) {
  261. $rename['target'] .= DIRECTORY_SEPARATOR;
  262. }
  263. $rename['target'] .= $name;
  264. }
  265. return $rename;
  266. }
  267. }