PageRenderTime 46ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

/monica/monica/vendor/zendframework/zendframework/library/Zend/Filter/File/Rename.php

https://bitbucket.org/alexandretaz/maniac_divers
PHP | 326 lines | 197 code | 42 blank | 87 comment | 42 complexity | 7d78dc8f292535d549b99109cc225359 MD5 | raw file
Possible License(s): BSD-3-Clause
  1. <?php
  2. /**
  3. * Zend Framework (http://framework.zend.com/)
  4. *
  5. * @link http://github.com/zendframework/zf2 for the canonical source repository
  6. * @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com)
  7. * @license http://framework.zend.com/license/new-bsd New BSD License
  8. */
  9. namespace Zend\Filter\File;
  10. use Traversable;
  11. use Zend\Filter;
  12. use Zend\Filter\Exception;
  13. use Zend\Stdlib\ArrayUtils;
  14. class Rename extends Filter\AbstractFilter
  15. {
  16. /**
  17. * Internal array of array(source, target, overwrite)
  18. */
  19. protected $files = array();
  20. /**
  21. * Class constructor
  22. *
  23. * Options argument may be either a string, a Zend_Config object, or an array.
  24. * If an array or Zend_Config object, it accepts the following keys:
  25. * 'source' => Source filename or directory which will be renamed
  26. * 'target' => Target filename or directory, the new name of the source file
  27. * 'overwrite' => Shall existing files be overwritten ?
  28. * 'randomize' => Shall target files have a random postfix attached?
  29. *
  30. * @param string|array|Traversable $options Target file or directory to be renamed
  31. * @throws Exception\InvalidArgumentException
  32. */
  33. public function __construct($options)
  34. {
  35. if ($options instanceof Traversable) {
  36. $options = ArrayUtils::iteratorToArray($options);
  37. } elseif (is_string($options)) {
  38. $options = array('target' => $options);
  39. } elseif (!is_array($options)) {
  40. throw new Exception\InvalidArgumentException(
  41. 'Invalid options argument provided to filter'
  42. );
  43. }
  44. $this->setFile($options);
  45. }
  46. /**
  47. * Returns the files to rename and their new name and location
  48. *
  49. * @return array
  50. */
  51. public function getFile()
  52. {
  53. return $this->files;
  54. }
  55. /**
  56. * Sets a new file or directory as target, deleting existing ones
  57. *
  58. * Array accepts the following keys:
  59. * 'source' => Source filename or directory which will be renamed
  60. * 'target' => Target filename or directory, the new name of the sourcefile
  61. * 'overwrite' => Shall existing files be overwritten?
  62. * 'randomize' => Shall target files have a random postfix attached?
  63. *
  64. * @param string|array $options Old file or directory to be rewritten
  65. * @return \Zend\Filter\File\Rename
  66. */
  67. public function setFile($options)
  68. {
  69. $this->files = array();
  70. $this->addFile($options);
  71. return $this;
  72. }
  73. /**
  74. * Adds a new file or directory as target to the existing ones
  75. *
  76. * Array accepts the following keys:
  77. * 'source' => Source filename or directory which will be renamed
  78. * 'target' => Target filename or directory, the new name of the sourcefile
  79. * 'overwrite' => Shall existing files be overwritten?
  80. * 'randomize' => Shall target files have a random postfix attached?
  81. *
  82. * @param string|array $options Old file or directory to be rewritten
  83. * @return Rename
  84. * @throws Exception\InvalidArgumentException
  85. */
  86. public function addFile($options)
  87. {
  88. if (is_string($options)) {
  89. $options = array('target' => $options);
  90. } elseif (!is_array($options)) {
  91. throw new Exception\InvalidArgumentException(
  92. 'Invalid options to rename filter provided'
  93. );
  94. }
  95. $this->_convertOptions($options);
  96. return $this;
  97. }
  98. /**
  99. * Returns only the new filename without moving it
  100. * But existing files will be erased when the overwrite option is true
  101. *
  102. * @param string $value Full path of file to change
  103. * @param bool $source Return internal informations
  104. * @return string The new filename which has been set
  105. * @throws Exception\InvalidArgumentException If the target file already exists.
  106. */
  107. public function getNewName($value, $source = false)
  108. {
  109. $file = $this->_getFileName($value);
  110. if (!is_array($file)) {
  111. return $file;
  112. }
  113. if ($file['source'] == $file['target']) {
  114. return $value;
  115. }
  116. if (!file_exists($file['source'])) {
  117. return $value;
  118. }
  119. if ($file['overwrite'] && file_exists($file['target'])) {
  120. unlink($file['target']);
  121. }
  122. if (file_exists($file['target'])) {
  123. throw new Exception\InvalidArgumentException(
  124. sprintf("File '%s' could not be renamed. It already exists.", $value)
  125. );
  126. }
  127. if ($source) {
  128. return $file;
  129. }
  130. return $file['target'];
  131. }
  132. /**
  133. * Defined by Zend\Filter\Filter
  134. *
  135. * Renames the file $value to the new name set before
  136. * Returns the file $value, removing all but digit characters
  137. *
  138. * @param string|array $value Full path of file to change or $_FILES data array
  139. * @throws Exception\RuntimeException
  140. * @return string|array The new filename which has been set
  141. */
  142. public function filter($value)
  143. {
  144. // An uploaded file? Retrieve the 'tmp_name'
  145. $isFileUpload = (is_array($value) && isset($value['tmp_name']));
  146. if ($isFileUpload) {
  147. $uploadData = $value;
  148. $value = $value['tmp_name'];
  149. }
  150. $file = $this->getNewName($value, true);
  151. if (is_string($file)) {
  152. if ($isFileUpload) {
  153. return $uploadData;
  154. } else {
  155. return $file;
  156. }
  157. }
  158. $result = rename($file['source'], $file['target']);
  159. if ($result !== true) {
  160. throw new Exception\RuntimeException(
  161. sprintf(
  162. "File '%s' could not be renamed. " .
  163. "An error occurred while processing the file.",
  164. $value
  165. )
  166. );
  167. }
  168. if ($isFileUpload) {
  169. $uploadData['tmp_name'] = $file['target'];
  170. return $uploadData;
  171. }
  172. return $file['target'];
  173. }
  174. /**
  175. * Internal method for creating the file array
  176. * Supports single and nested arrays
  177. *
  178. * @param array $options
  179. * @return array
  180. */
  181. protected function _convertOptions($options)
  182. {
  183. $files = array();
  184. foreach ($options as $key => $value) {
  185. if (is_array($value)) {
  186. $this->_convertOptions($value);
  187. continue;
  188. }
  189. switch ($key) {
  190. case "source":
  191. $files['source'] = (string) $value;
  192. break;
  193. case 'target' :
  194. $files['target'] = (string) $value;
  195. break;
  196. case 'overwrite' :
  197. $files['overwrite'] = (bool) $value;
  198. break;
  199. case 'randomize' :
  200. $files['randomize'] = (boolean) $value;
  201. break;
  202. default:
  203. break;
  204. }
  205. }
  206. if (empty($files)) {
  207. return $this;
  208. }
  209. if (empty($files['source'])) {
  210. $files['source'] = '*';
  211. }
  212. if (empty($files['target'])) {
  213. $files['target'] = '*';
  214. }
  215. if (empty($files['overwrite'])) {
  216. $files['overwrite'] = false;
  217. }
  218. if (empty($files['randomize'])) {
  219. $files['randomize'] = false;
  220. }
  221. $found = false;
  222. foreach ($this->files as $key => $value) {
  223. if ($value['source'] == $files['source']) {
  224. $this->files[$key] = $files;
  225. $found = true;
  226. }
  227. }
  228. if (!$found) {
  229. $count = count($this->files);
  230. $this->files[$count] = $files;
  231. }
  232. return $this;
  233. }
  234. /**
  235. * Internal method to resolve the requested source
  236. * and return all other related parameters
  237. *
  238. * @param string $file Filename to get the informations for
  239. * @return array|string
  240. */
  241. protected function _getFileName($file)
  242. {
  243. $rename = array();
  244. foreach ($this->files as $value) {
  245. if ($value['source'] == '*') {
  246. if (!isset($rename['source'])) {
  247. $rename = $value;
  248. $rename['source'] = $file;
  249. }
  250. }
  251. if ($value['source'] == $file) {
  252. $rename = $value;
  253. break;
  254. }
  255. }
  256. if (!isset($rename['source'])) {
  257. return $file;
  258. }
  259. if (!isset($rename['target']) || $rename['target'] == '*') {
  260. $rename['target'] = $rename['source'];
  261. }
  262. if (is_dir($rename['target'])) {
  263. $name = basename($rename['source']);
  264. $last = $rename['target'][strlen($rename['target']) - 1];
  265. if (($last != '/') && ($last != '\\')) {
  266. $rename['target'] .= DIRECTORY_SEPARATOR;
  267. }
  268. $rename['target'] .= $name;
  269. }
  270. if ($rename['randomize']) {
  271. $info = pathinfo($rename['target']);
  272. $newTarget = $info['dirname'] . DIRECTORY_SEPARATOR .
  273. $info['filename'] . uniqid('_');
  274. if (isset($info['extension'])) {
  275. $newTarget .= '.' . $info['extension'];
  276. }
  277. $rename['target'] = $newTarget;
  278. }
  279. return $rename;
  280. }
  281. }