Folder.php 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  4. * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  5. *
  6. * Licensed under The MIT License
  7. * For full copyright and license information, please see the LICENSE.txt
  8. * Redistributions of files must retain the above copyright notice.
  9. *
  10. * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  11. * @link http://cakephp.org CakePHP(tm) Project
  12. * @package Cake.Utility
  13. * @since CakePHP(tm) v 0.2.9
  14. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  15. */
  16. /**
  17. * Folder structure browser, lists folders and files.
  18. * Provides an Object interface for Common directory related tasks.
  19. *
  20. * @package Cake.Utility
  21. */
  22. class Folder {
  23. /**
  24. * Default scheme for Folder::copy
  25. * Recursively merges subfolders with the same name
  26. *
  27. * @var string
  28. */
  29. const MERGE = 'merge';
  30. /**
  31. * Overwrite scheme for Folder::copy
  32. * subfolders with the same name will be replaced
  33. *
  34. * @var string
  35. */
  36. const OVERWRITE = 'overwrite';
  37. /**
  38. * Skip scheme for Folder::copy
  39. * if a subfolder with the same name exists it will be skipped
  40. *
  41. * @var string
  42. */
  43. const SKIP = 'skip';
  44. /**
  45. * Path to Folder.
  46. *
  47. * @var string
  48. * @link http://book.cakephp.org/2.0/en/core-utility-libraries/file-folder.html#Folder::$path
  49. */
  50. public $path = null;
  51. /**
  52. * Sortedness. Whether or not list results
  53. * should be sorted by name.
  54. *
  55. * @var bool
  56. * @link http://book.cakephp.org/2.0/en/core-utility-libraries/file-folder.html#Folder::$sort
  57. */
  58. public $sort = false;
  59. /**
  60. * Mode to be used on create. Does nothing on windows platforms.
  61. *
  62. * @var int
  63. * http://book.cakephp.org/2.0/en/core-utility-libraries/file-folder.html#Folder::$mode
  64. */
  65. public $mode = 0755;
  66. /**
  67. * Holds messages from last method.
  68. *
  69. * @var array
  70. */
  71. protected $_messages = array();
  72. /**
  73. * Holds errors from last method.
  74. *
  75. * @var array
  76. */
  77. protected $_errors = array();
  78. /**
  79. * Holds array of complete directory paths.
  80. *
  81. * @var array
  82. */
  83. protected $_directories;
  84. /**
  85. * Holds array of complete file paths.
  86. *
  87. * @var array
  88. */
  89. protected $_files;
  90. /**
  91. * Constructor.
  92. *
  93. * @param string $path Path to folder
  94. * @param bool $create Create folder if not found
  95. * @param string|bool $mode Mode (CHMOD) to apply to created folder, false to ignore
  96. * @link http://book.cakephp.org/2.0/en/core-utility-libraries/file-folder.html#Folder
  97. */
  98. public function __construct($path = false, $create = false, $mode = false) {
  99. if (empty($path)) {
  100. $path = TMP;
  101. }
  102. if ($mode) {
  103. $this->mode = $mode;
  104. }
  105. if (!file_exists($path) && $create === true) {
  106. $this->create($path, $this->mode);
  107. }
  108. if (!Folder::isAbsolute($path)) {
  109. $path = realpath($path);
  110. }
  111. if (!empty($path)) {
  112. $this->cd($path);
  113. }
  114. }
  115. /**
  116. * Return current path.
  117. *
  118. * @return string Current path
  119. * @link http://book.cakephp.org/2.0/en/core-utility-libraries/file-folder.html#Folder::pwd
  120. */
  121. public function pwd() {
  122. return $this->path;
  123. }
  124. /**
  125. * Change directory to $path.
  126. *
  127. * @param string $path Path to the directory to change to
  128. * @return string The new path. Returns false on failure
  129. * @link http://book.cakephp.org/2.0/en/core-utility-libraries/file-folder.html#Folder::cd
  130. */
  131. public function cd($path) {
  132. $path = $this->realpath($path);
  133. if (is_dir($path)) {
  134. return $this->path = $path;
  135. }
  136. return false;
  137. }
  138. /**
  139. * Returns an array of the contents of the current directory.
  140. * The returned array holds two arrays: One of directories and one of files.
  141. *
  142. * @param bool $sort Whether you want the results sorted, set this and the sort property
  143. * to false to get unsorted results.
  144. * @param array|bool $exceptions Either an array or boolean true will not grab dot files
  145. * @param bool $fullPath True returns the full path
  146. * @return mixed Contents of current directory as an array, an empty array on failure
  147. * @link http://book.cakephp.org/2.0/en/core-utility-libraries/file-folder.html#Folder::read
  148. */
  149. public function read($sort = true, $exceptions = false, $fullPath = false) {
  150. $dirs = $files = array();
  151. if (!$this->pwd()) {
  152. return array($dirs, $files);
  153. }
  154. if (is_array($exceptions)) {
  155. $exceptions = array_flip($exceptions);
  156. }
  157. $skipHidden = isset($exceptions['.']) || $exceptions === true;
  158. try {
  159. $iterator = new DirectoryIterator($this->path);
  160. } catch (Exception $e) {
  161. return array($dirs, $files);
  162. }
  163. foreach ($iterator as $item) {
  164. if ($item->isDot()) {
  165. continue;
  166. }
  167. $name = $item->getFileName();
  168. if ($skipHidden && $name[0] === '.' || isset($exceptions[$name])) {
  169. continue;
  170. }
  171. if ($fullPath) {
  172. $name = $item->getPathName();
  173. }
  174. if ($item->isDir()) {
  175. $dirs[] = $name;
  176. } else {
  177. $files[] = $name;
  178. }
  179. }
  180. if ($sort || $this->sort) {
  181. sort($dirs);
  182. sort($files);
  183. }
  184. return array($dirs, $files);
  185. }
  186. /**
  187. * Returns an array of all matching files in current directory.
  188. *
  189. * @param string $regexpPattern Preg_match pattern (Defaults to: .*)
  190. * @param bool $sort Whether results should be sorted.
  191. * @return array Files that match given pattern
  192. * @link http://book.cakephp.org/2.0/en/core-utility-libraries/file-folder.html#Folder::find
  193. */
  194. public function find($regexpPattern = '.*', $sort = false) {
  195. list(, $files) = $this->read($sort);
  196. return array_values(preg_grep('/^' . $regexpPattern . '$/i', $files));
  197. }
  198. /**
  199. * Returns an array of all matching files in and below current directory.
  200. *
  201. * @param string $pattern Preg_match pattern (Defaults to: .*)
  202. * @param bool $sort Whether results should be sorted.
  203. * @return array Files matching $pattern
  204. * @link http://book.cakephp.org/2.0/en/core-utility-libraries/file-folder.html#Folder::findRecursive
  205. */
  206. public function findRecursive($pattern = '.*', $sort = false) {
  207. if (!$this->pwd()) {
  208. return array();
  209. }
  210. $startsOn = $this->path;
  211. $out = $this->_findRecursive($pattern, $sort);
  212. $this->cd($startsOn);
  213. return $out;
  214. }
  215. /**
  216. * Private helper function for findRecursive.
  217. *
  218. * @param string $pattern Pattern to match against
  219. * @param bool $sort Whether results should be sorted.
  220. * @return array Files matching pattern
  221. */
  222. protected function _findRecursive($pattern, $sort = false) {
  223. list($dirs, $files) = $this->read($sort);
  224. $found = array();
  225. foreach ($files as $file) {
  226. if (preg_match('/^' . $pattern . '$/i', $file)) {
  227. $found[] = Folder::addPathElement($this->path, $file);
  228. }
  229. }
  230. $start = $this->path;
  231. foreach ($dirs as $dir) {
  232. $this->cd(Folder::addPathElement($start, $dir));
  233. $found = array_merge($found, $this->findRecursive($pattern, $sort));
  234. }
  235. return $found;
  236. }
  237. /**
  238. * Returns true if given $path is a Windows path.
  239. *
  240. * @param string $path Path to check
  241. * @return bool true if windows path, false otherwise
  242. * @link http://book.cakephp.org/2.0/en/core-utility-libraries/file-folder.html#Folder::isWindowsPath
  243. */
  244. public static function isWindowsPath($path) {
  245. return (preg_match('/^[A-Z]:\\\\/i', $path) || substr($path, 0, 2) === '\\\\');
  246. }
  247. /**
  248. * Returns true if given $path is an absolute path.
  249. *
  250. * @param string $path Path to check
  251. * @return bool true if path is absolute.
  252. * @link http://book.cakephp.org/2.0/en/core-utility-libraries/file-folder.html#Folder::isAbsolute
  253. */
  254. public static function isAbsolute($path) {
  255. return !empty($path) && ($path[0] === '/' || preg_match('/^[A-Z]:\\\\/i', $path) || substr($path, 0, 2) === '\\\\');
  256. }
  257. /**
  258. * Returns a correct set of slashes for given $path. (\\ for Windows paths and / for other paths.)
  259. *
  260. * @param string $path Path to check
  261. * @return string Set of slashes ("\\" or "/")
  262. * @link http://book.cakephp.org/2.0/en/core-utility-libraries/file-folder.html#Folder::normalizePath
  263. */
  264. public static function normalizePath($path) {
  265. return Folder::correctSlashFor($path);
  266. }
  267. /**
  268. * Returns a correct set of slashes for given $path. (\\ for Windows paths and / for other paths.)
  269. *
  270. * @param string $path Path to check
  271. * @return string Set of slashes ("\\" or "/")
  272. * @link http://book.cakephp.org/2.0/en/core-utility-libraries/file-folder.html#Folder::correctSlashFor
  273. */
  274. public static function correctSlashFor($path) {
  275. return (Folder::isWindowsPath($path)) ? '\\' : '/';
  276. }
  277. /**
  278. * Returns $path with added terminating slash (corrected for Windows or other OS).
  279. *
  280. * @param string $path Path to check
  281. * @return string Path with ending slash
  282. * @link http://book.cakephp.org/2.0/en/core-utility-libraries/file-folder.html#Folder::slashTerm
  283. */
  284. public static function slashTerm($path) {
  285. if (Folder::isSlashTerm($path)) {
  286. return $path;
  287. }
  288. return $path . Folder::correctSlashFor($path);
  289. }
  290. /**
  291. * Returns $path with $element added, with correct slash in-between.
  292. *
  293. * @param string $path Path
  294. * @param string|array $element Element to add at end of path
  295. * @return string Combined path
  296. * @link http://book.cakephp.org/2.0/en/core-utility-libraries/file-folder.html#Folder::addPathElement
  297. */
  298. public static function addPathElement($path, $element) {
  299. $element = (array)$element;
  300. array_unshift($element, rtrim($path, DS));
  301. return implode(DS, $element);
  302. }
  303. /**
  304. * Returns true if the File is in a given CakePath.
  305. *
  306. * @param string $path The path to check.
  307. * @return bool
  308. * @link http://book.cakephp.org/2.0/en/core-utility-libraries/file-folder.html#Folder::inCakePath
  309. */
  310. public function inCakePath($path = '') {
  311. $dir = substr(Folder::slashTerm(ROOT), 0, -1);
  312. $newdir = $dir . $path;
  313. return $this->inPath($newdir);
  314. }
  315. /**
  316. * Returns true if the File is in given path.
  317. *
  318. * @param string $path The path to check that the current pwd() resides with in.
  319. * @param bool $reverse Reverse the search, check that pwd() resides within $path.
  320. * @return bool
  321. * @link http://book.cakephp.org/2.0/en/core-utility-libraries/file-folder.html#Folder::inPath
  322. */
  323. public function inPath($path = '', $reverse = false) {
  324. $dir = Folder::slashTerm($path);
  325. $current = Folder::slashTerm($this->pwd());
  326. if (!$reverse) {
  327. $return = preg_match('/^(.*)' . preg_quote($dir, '/') . '(.*)/', $current);
  328. } else {
  329. $return = preg_match('/^(.*)' . preg_quote($current, '/') . '(.*)/', $dir);
  330. }
  331. return (bool)$return;
  332. }
  333. /**
  334. * Change the mode on a directory structure recursively. This includes changing the mode on files as well.
  335. *
  336. * @param string $path The path to chmod
  337. * @param int $mode octal value 0755
  338. * @param bool $recursive chmod recursively, set to false to only change the current directory.
  339. * @param array $exceptions array of files, directories to skip
  340. * @return bool Returns TRUE on success, FALSE on failure
  341. * @link http://book.cakephp.org/2.0/en/core-utility-libraries/file-folder.html#Folder::chmod
  342. */
  343. public function chmod($path, $mode = false, $recursive = true, $exceptions = array()) {
  344. if (!$mode) {
  345. $mode = $this->mode;
  346. }
  347. if ($recursive === false && is_dir($path)) {
  348. //@codingStandardsIgnoreStart
  349. if (@chmod($path, intval($mode, 8))) {
  350. //@codingStandardsIgnoreEnd
  351. $this->_messages[] = __d('cake_dev', '%s changed to %s', $path, $mode);
  352. return true;
  353. }
  354. $this->_errors[] = __d('cake_dev', '%s NOT changed to %s', $path, $mode);
  355. return false;
  356. }
  357. if (is_dir($path)) {
  358. $paths = $this->tree($path);
  359. foreach ($paths as $type) {
  360. foreach ($type as $fullpath) {
  361. $check = explode(DS, $fullpath);
  362. $count = count($check);
  363. if (in_array($check[$count - 1], $exceptions)) {
  364. continue;
  365. }
  366. //@codingStandardsIgnoreStart
  367. if (@chmod($fullpath, intval($mode, 8))) {
  368. //@codingStandardsIgnoreEnd
  369. $this->_messages[] = __d('cake_dev', '%s changed to %s', $fullpath, $mode);
  370. } else {
  371. $this->_errors[] = __d('cake_dev', '%s NOT changed to %s', $fullpath, $mode);
  372. }
  373. }
  374. }
  375. if (empty($this->_errors)) {
  376. return true;
  377. }
  378. }
  379. return false;
  380. }
  381. /**
  382. * Returns an array of nested directories and files in each directory
  383. *
  384. * @param string $path the directory path to build the tree from
  385. * @param array|bool $exceptions Either an array of files/folder to exclude
  386. * or boolean true to not grab dot files/folders
  387. * @param string $type either 'file' or 'dir'. null returns both files and directories
  388. * @return mixed array of nested directories and files in each directory
  389. * @link http://book.cakephp.org/2.0/en/core-utility-libraries/file-folder.html#Folder::tree
  390. */
  391. public function tree($path = null, $exceptions = false, $type = null) {
  392. if (!$path) {
  393. $path = $this->path;
  394. }
  395. $files = array();
  396. $directories = array($path);
  397. if (is_array($exceptions)) {
  398. $exceptions = array_flip($exceptions);
  399. }
  400. $skipHidden = false;
  401. if ($exceptions === true) {
  402. $skipHidden = true;
  403. } elseif (isset($exceptions['.'])) {
  404. $skipHidden = true;
  405. unset($exceptions['.']);
  406. }
  407. try {
  408. $directory = new RecursiveDirectoryIterator($path, RecursiveDirectoryIterator::KEY_AS_PATHNAME | RecursiveDirectoryIterator::CURRENT_AS_SELF);
  409. $iterator = new RecursiveIteratorIterator($directory, RecursiveIteratorIterator::SELF_FIRST);
  410. } catch (Exception $e) {
  411. if ($type === null) {
  412. return array(array(), array());
  413. }
  414. return array();
  415. }
  416. foreach ($iterator as $itemPath => $fsIterator) {
  417. if ($skipHidden) {
  418. $subPathName = $fsIterator->getSubPathname();
  419. if ($subPathName{0} === '.' || strpos($subPathName, DS . '.') !== false) {
  420. continue;
  421. }
  422. }
  423. $item = $fsIterator->current();
  424. if (!empty($exceptions) && isset($exceptions[$item->getFilename()])) {
  425. continue;
  426. }
  427. if ($item->isFile()) {
  428. $files[] = $itemPath;
  429. } elseif ($item->isDir() && !$item->isDot()) {
  430. $directories[] = $itemPath;
  431. }
  432. }
  433. if ($type === null) {
  434. return array($directories, $files);
  435. }
  436. if ($type === 'dir') {
  437. return $directories;
  438. }
  439. return $files;
  440. }
  441. /**
  442. * Create a directory structure recursively. Can be used to create
  443. * deep path structures like `/foo/bar/baz/shoe/horn`
  444. *
  445. * @param string $pathname The directory structure to create
  446. * @param int $mode octal value 0755
  447. * @return bool Returns TRUE on success, FALSE on failure
  448. * @link http://book.cakephp.org/2.0/en/core-utility-libraries/file-folder.html#Folder::create
  449. */
  450. public function create($pathname, $mode = false) {
  451. if (is_dir($pathname) || empty($pathname)) {
  452. return true;
  453. }
  454. if (!$mode) {
  455. $mode = $this->mode;
  456. }
  457. if (is_file($pathname)) {
  458. $this->_errors[] = __d('cake_dev', '%s is a file', $pathname);
  459. return false;
  460. }
  461. $pathname = rtrim($pathname, DS);
  462. $nextPathname = substr($pathname, 0, strrpos($pathname, DS));
  463. if ($this->create($nextPathname, $mode)) {
  464. if (!file_exists($pathname)) {
  465. $old = umask(0);
  466. if (mkdir($pathname, $mode)) {
  467. umask($old);
  468. $this->_messages[] = __d('cake_dev', '%s created', $pathname);
  469. return true;
  470. }
  471. umask($old);
  472. $this->_errors[] = __d('cake_dev', '%s NOT created', $pathname);
  473. return false;
  474. }
  475. }
  476. return false;
  477. }
  478. /**
  479. * Returns the size in bytes of this Folder and its contents.
  480. *
  481. * @return int size in bytes of current folder
  482. * @link http://book.cakephp.org/2.0/en/core-utility-libraries/file-folder.html#Folder::dirsize
  483. */
  484. public function dirsize() {
  485. $size = 0;
  486. $directory = Folder::slashTerm($this->path);
  487. $stack = array($directory);
  488. $count = count($stack);
  489. for ($i = 0, $j = $count; $i < $j; ++$i) {
  490. if (is_file($stack[$i])) {
  491. $size += filesize($stack[$i]);
  492. } elseif (is_dir($stack[$i])) {
  493. $dir = dir($stack[$i]);
  494. if ($dir) {
  495. while (false !== ($entry = $dir->read())) {
  496. if ($entry === '.' || $entry === '..') {
  497. continue;
  498. }
  499. $add = $stack[$i] . $entry;
  500. if (is_dir($stack[$i] . $entry)) {
  501. $add = Folder::slashTerm($add);
  502. }
  503. $stack[] = $add;
  504. }
  505. $dir->close();
  506. }
  507. }
  508. $j = count($stack);
  509. }
  510. return $size;
  511. }
  512. /**
  513. * Recursively Remove directories if the system allows.
  514. *
  515. * @param string $path Path of directory to delete
  516. * @return bool Success
  517. * @link http://book.cakephp.org/2.0/en/core-utility-libraries/file-folder.html#Folder::delete
  518. */
  519. public function delete($path = null) {
  520. if (!$path) {
  521. $path = $this->pwd();
  522. }
  523. if (!$path) {
  524. return null;
  525. }
  526. $path = Folder::slashTerm($path);
  527. if (is_dir($path)) {
  528. try {
  529. $directory = new RecursiveDirectoryIterator($path, RecursiveDirectoryIterator::CURRENT_AS_SELF);
  530. $iterator = new RecursiveIteratorIterator($directory, RecursiveIteratorIterator::CHILD_FIRST);
  531. } catch (Exception $e) {
  532. return false;
  533. }
  534. foreach ($iterator as $item) {
  535. $filePath = $item->getPathname();
  536. if ($item->isFile() || $item->isLink()) {
  537. //@codingStandardsIgnoreStart
  538. if (@unlink($filePath)) {
  539. //@codingStandardsIgnoreEnd
  540. $this->_messages[] = __d('cake_dev', '%s removed', $filePath);
  541. } else {
  542. $this->_errors[] = __d('cake_dev', '%s NOT removed', $filePath);
  543. }
  544. } elseif ($item->isDir() && !$item->isDot()) {
  545. //@codingStandardsIgnoreStart
  546. if (@rmdir($filePath)) {
  547. //@codingStandardsIgnoreEnd
  548. $this->_messages[] = __d('cake_dev', '%s removed', $filePath);
  549. } else {
  550. $this->_errors[] = __d('cake_dev', '%s NOT removed', $filePath);
  551. return false;
  552. }
  553. }
  554. }
  555. $path = rtrim($path, DS);
  556. //@codingStandardsIgnoreStart
  557. if (@rmdir($path)) {
  558. //@codingStandardsIgnoreEnd
  559. $this->_messages[] = __d('cake_dev', '%s removed', $path);
  560. } else {
  561. $this->_errors[] = __d('cake_dev', '%s NOT removed', $path);
  562. return false;
  563. }
  564. }
  565. return true;
  566. }
  567. /**
  568. * Recursive directory copy.
  569. *
  570. * ### Options
  571. *
  572. * - `to` The directory to copy to.
  573. * - `from` The directory to copy from, this will cause a cd() to occur, changing the results of pwd().
  574. * - `mode` The mode to copy the files/directories with.
  575. * - `skip` Files/directories to skip.
  576. * - `scheme` Folder::MERGE, Folder::OVERWRITE, Folder::SKIP
  577. *
  578. * @param array|string $options Either an array of options (see above) or a string of the destination directory.
  579. * @return bool Success
  580. * @link http://book.cakephp.org/2.0/en/core-utility-libraries/file-folder.html#Folder::copy
  581. */
  582. public function copy($options) {
  583. if (!$this->pwd()) {
  584. return false;
  585. }
  586. $to = null;
  587. if (is_string($options)) {
  588. $to = $options;
  589. $options = array();
  590. }
  591. $options += array('to' => $to, 'from' => $this->path, 'mode' => $this->mode, 'skip' => array(), 'scheme' => Folder::MERGE);
  592. $fromDir = $options['from'];
  593. $toDir = $options['to'];
  594. $mode = $options['mode'];
  595. if (!$this->cd($fromDir)) {
  596. $this->_errors[] = __d('cake_dev', '%s not found', $fromDir);
  597. return false;
  598. }
  599. if (!is_dir($toDir)) {
  600. $this->create($toDir, $mode);
  601. }
  602. if (!is_writable($toDir)) {
  603. $this->_errors[] = __d('cake_dev', '%s not writable', $toDir);
  604. return false;
  605. }
  606. $exceptions = array_merge(array('.', '..', '.svn'), $options['skip']);
  607. //@codingStandardsIgnoreStart
  608. if ($handle = @opendir($fromDir)) {
  609. //@codingStandardsIgnoreEnd
  610. while (($item = readdir($handle)) !== false) {
  611. $to = Folder::addPathElement($toDir, $item);
  612. if (($options['scheme'] != Folder::SKIP || !is_dir($to)) && !in_array($item, $exceptions)) {
  613. $from = Folder::addPathElement($fromDir, $item);
  614. if (is_file($from)) {
  615. if (copy($from, $to)) {
  616. chmod($to, intval($mode, 8));
  617. touch($to, filemtime($from));
  618. $this->_messages[] = __d('cake_dev', '%s copied to %s', $from, $to);
  619. } else {
  620. $this->_errors[] = __d('cake_dev', '%s NOT copied to %s', $from, $to);
  621. }
  622. }
  623. if (is_dir($from) && file_exists($to) && $options['scheme'] === Folder::OVERWRITE) {
  624. $this->delete($to);
  625. }
  626. if (is_dir($from) && !file_exists($to)) {
  627. $old = umask(0);
  628. if (mkdir($to, $mode)) {
  629. umask($old);
  630. $old = umask(0);
  631. chmod($to, $mode);
  632. umask($old);
  633. $this->_messages[] = __d('cake_dev', '%s created', $to);
  634. $options = array('to' => $to, 'from' => $from) + $options;
  635. $this->copy($options);
  636. } else {
  637. $this->_errors[] = __d('cake_dev', '%s not created', $to);
  638. }
  639. } elseif (is_dir($from) && $options['scheme'] === Folder::MERGE) {
  640. $options = array('to' => $to, 'from' => $from) + $options;
  641. $this->copy($options);
  642. }
  643. }
  644. }
  645. closedir($handle);
  646. } else {
  647. return false;
  648. }
  649. if (!empty($this->_errors)) {
  650. return false;
  651. }
  652. return true;
  653. }
  654. /**
  655. * Recursive directory move.
  656. *
  657. * ### Options
  658. *
  659. * - `to` The directory to copy to.
  660. * - `from` The directory to copy from, this will cause a cd() to occur, changing the results of pwd().
  661. * - `chmod` The mode to copy the files/directories with.
  662. * - `skip` Files/directories to skip.
  663. * - `scheme` Folder::MERGE, Folder::OVERWRITE, Folder::SKIP
  664. *
  665. * @param array $options (to, from, chmod, skip, scheme)
  666. * @return bool Success
  667. * @link http://book.cakephp.org/2.0/en/core-utility-libraries/file-folder.html#Folder::move
  668. */
  669. public function move($options) {
  670. $to = null;
  671. if (is_string($options)) {
  672. $to = $options;
  673. $options = (array)$options;
  674. }
  675. $options += array('to' => $to, 'from' => $this->path, 'mode' => $this->mode, 'skip' => array());
  676. if ($this->copy($options)) {
  677. if ($this->delete($options['from'])) {
  678. return (bool)$this->cd($options['to']);
  679. }
  680. }
  681. return false;
  682. }
  683. /**
  684. * get messages from latest method
  685. *
  686. * @param bool $reset Reset message stack after reading
  687. * @return array
  688. * @link http://book.cakephp.org/2.0/en/core-utility-libraries/file-folder.html#Folder::messages
  689. */
  690. public function messages($reset = true) {
  691. $messages = $this->_messages;
  692. if ($reset) {
  693. $this->_messages = array();
  694. }
  695. return $messages;
  696. }
  697. /**
  698. * get error from latest method
  699. *
  700. * @param bool $reset Reset error stack after reading
  701. * @return array
  702. * @link http://book.cakephp.org/2.0/en/core-utility-libraries/file-folder.html#Folder::errors
  703. */
  704. public function errors($reset = true) {
  705. $errors = $this->_errors;
  706. if ($reset) {
  707. $this->_errors = array();
  708. }
  709. return $errors;
  710. }
  711. /**
  712. * Get the real path (taking ".." and such into account)
  713. *
  714. * @param string $path Path to resolve
  715. * @return string The resolved path
  716. * @link http://book.cakephp.org/2.0/en/core-utility-libraries/file-folder.html#Folder::realpath
  717. */
  718. public function realpath($path) {
  719. $path = str_replace('/', DS, trim($path));
  720. if (strpos($path, '..') === false) {
  721. if (!Folder::isAbsolute($path)) {
  722. $path = Folder::addPathElement($this->path, $path);
  723. }
  724. return $path;
  725. }
  726. $parts = explode(DS, $path);
  727. $newparts = array();
  728. $newpath = '';
  729. if ($path[0] === DS) {
  730. $newpath = DS;
  731. }
  732. while (($part = array_shift($parts)) !== null) {
  733. if ($part === '.' || $part === '') {
  734. continue;
  735. }
  736. if ($part === '..') {
  737. if (!empty($newparts)) {
  738. array_pop($newparts);
  739. continue;
  740. }
  741. return false;
  742. }
  743. $newparts[] = $part;
  744. }
  745. $newpath .= implode(DS, $newparts);
  746. return Folder::slashTerm($newpath);
  747. }
  748. /**
  749. * Returns true if given $path ends in a slash (i.e. is slash-terminated).
  750. *
  751. * @param string $path Path to check
  752. * @return bool true if path ends with slash, false otherwise
  753. * @link http://book.cakephp.org/2.0/en/core-utility-libraries/file-folder.html#Folder::isSlashTerm
  754. */
  755. public static function isSlashTerm($path) {
  756. $lastChar = $path[strlen($path) - 1];
  757. return $lastChar === '/' || $lastChar === '\\';
  758. }
  759. }