Base for a static organization website

Folder.php 24KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868
  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. if (empty($path)) {
  256. return false;
  257. }
  258. return $path[0] === '/' ||
  259. preg_match('/^[A-Z]:\\\\/i', $path) ||
  260. substr($path, 0, 2) === '\\\\' ||
  261. static::isRegisteredStreamWrapper($path);
  262. }
  263. /**
  264. * Returns true if given $path is a registered stream wrapper.
  265. *
  266. * @param string $path Path to check
  267. * @return boo true If path is registered stream wrapper.
  268. */
  269. public static function isRegisteredStreamWrapper($path) {
  270. if (preg_match('/^[A-Z]+(?=:\/\/)/i', $path, $matches) &&
  271. in_array($matches[0], stream_get_wrappers())
  272. ) {
  273. return true;
  274. }
  275. return false;
  276. }
  277. /**
  278. * Returns a correct set of slashes for given $path. (\\ for Windows paths and / for other paths.)
  279. *
  280. * @param string $path Path to check
  281. * @return string Set of slashes ("\\" or "/")
  282. * @link http://book.cakephp.org/2.0/en/core-utility-libraries/file-folder.html#Folder::normalizePath
  283. */
  284. public static function normalizePath($path) {
  285. return Folder::correctSlashFor($path);
  286. }
  287. /**
  288. * Returns a correct set of slashes for given $path. (\\ for Windows paths and / for other paths.)
  289. *
  290. * @param string $path Path to check
  291. * @return string Set of slashes ("\\" or "/")
  292. * @link http://book.cakephp.org/2.0/en/core-utility-libraries/file-folder.html#Folder::correctSlashFor
  293. */
  294. public static function correctSlashFor($path) {
  295. return (Folder::isWindowsPath($path)) ? '\\' : '/';
  296. }
  297. /**
  298. * Returns $path with added terminating slash (corrected for Windows or other OS).
  299. *
  300. * @param string $path Path to check
  301. * @return string Path with ending slash
  302. * @link http://book.cakephp.org/2.0/en/core-utility-libraries/file-folder.html#Folder::slashTerm
  303. */
  304. public static function slashTerm($path) {
  305. if (Folder::isSlashTerm($path)) {
  306. return $path;
  307. }
  308. return $path . Folder::correctSlashFor($path);
  309. }
  310. /**
  311. * Returns $path with $element added, with correct slash in-between.
  312. *
  313. * @param string $path Path
  314. * @param string|array $element Element to add at end of path
  315. * @return string Combined path
  316. * @link http://book.cakephp.org/2.0/en/core-utility-libraries/file-folder.html#Folder::addPathElement
  317. */
  318. public static function addPathElement($path, $element) {
  319. $element = (array)$element;
  320. array_unshift($element, rtrim($path, DS));
  321. return implode(DS, $element);
  322. }
  323. /**
  324. * Returns true if the File is in a given CakePath.
  325. *
  326. * @param string $path The path to check.
  327. * @return bool
  328. * @link http://book.cakephp.org/2.0/en/core-utility-libraries/file-folder.html#Folder::inCakePath
  329. */
  330. public function inCakePath($path = '') {
  331. $dir = substr(Folder::slashTerm(ROOT), 0, -1);
  332. $newdir = $dir . $path;
  333. return $this->inPath($newdir);
  334. }
  335. /**
  336. * Returns true if the File is in given path.
  337. *
  338. * @param string $path The path to check that the current pwd() resides with in.
  339. * @param bool $reverse Reverse the search, check that pwd() resides within $path.
  340. * @return bool
  341. * @link http://book.cakephp.org/2.0/en/core-utility-libraries/file-folder.html#Folder::inPath
  342. */
  343. public function inPath($path = '', $reverse = false) {
  344. $dir = Folder::slashTerm($path);
  345. $current = Folder::slashTerm($this->pwd());
  346. if (!$reverse) {
  347. $return = preg_match('/^(.*)' . preg_quote($dir, '/') . '(.*)/', $current);
  348. } else {
  349. $return = preg_match('/^(.*)' . preg_quote($current, '/') . '(.*)/', $dir);
  350. }
  351. return (bool)$return;
  352. }
  353. /**
  354. * Change the mode on a directory structure recursively. This includes changing the mode on files as well.
  355. *
  356. * @param string $path The path to chmod.
  357. * @param int $mode Octal value, e.g. 0755.
  358. * @param bool $recursive Chmod recursively, set to false to only change the current directory.
  359. * @param array $exceptions Array of files, directories to skip.
  360. * @return bool Success.
  361. * @link http://book.cakephp.org/2.0/en/core-utility-libraries/file-folder.html#Folder::chmod
  362. */
  363. public function chmod($path, $mode = false, $recursive = true, $exceptions = array()) {
  364. if (!$mode) {
  365. $mode = $this->mode;
  366. }
  367. if ($recursive === false && is_dir($path)) {
  368. //@codingStandardsIgnoreStart
  369. if (@chmod($path, intval($mode, 8))) {
  370. //@codingStandardsIgnoreEnd
  371. $this->_messages[] = __d('cake_dev', '%s changed to %s', $path, $mode);
  372. return true;
  373. }
  374. $this->_errors[] = __d('cake_dev', '%s NOT changed to %s', $path, $mode);
  375. return false;
  376. }
  377. if (is_dir($path)) {
  378. $paths = $this->tree($path);
  379. foreach ($paths as $type) {
  380. foreach ($type as $fullpath) {
  381. $check = explode(DS, $fullpath);
  382. $count = count($check);
  383. if (in_array($check[$count - 1], $exceptions)) {
  384. continue;
  385. }
  386. //@codingStandardsIgnoreStart
  387. if (@chmod($fullpath, intval($mode, 8))) {
  388. //@codingStandardsIgnoreEnd
  389. $this->_messages[] = __d('cake_dev', '%s changed to %s', $fullpath, $mode);
  390. } else {
  391. $this->_errors[] = __d('cake_dev', '%s NOT changed to %s', $fullpath, $mode);
  392. }
  393. }
  394. }
  395. if (empty($this->_errors)) {
  396. return true;
  397. }
  398. }
  399. return false;
  400. }
  401. /**
  402. * Returns an array of nested directories and files in each directory
  403. *
  404. * @param string $path the directory path to build the tree from
  405. * @param array|bool $exceptions Either an array of files/folder to exclude
  406. * or boolean true to not grab dot files/folders
  407. * @param string $type either 'file' or 'dir'. null returns both files and directories
  408. * @return mixed array of nested directories and files in each directory
  409. * @link http://book.cakephp.org/2.0/en/core-utility-libraries/file-folder.html#Folder::tree
  410. */
  411. public function tree($path = null, $exceptions = false, $type = null) {
  412. if (!$path) {
  413. $path = $this->path;
  414. }
  415. $files = array();
  416. $directories = array($path);
  417. if (is_array($exceptions)) {
  418. $exceptions = array_flip($exceptions);
  419. }
  420. $skipHidden = false;
  421. if ($exceptions === true) {
  422. $skipHidden = true;
  423. } elseif (isset($exceptions['.'])) {
  424. $skipHidden = true;
  425. unset($exceptions['.']);
  426. }
  427. try {
  428. $directory = new RecursiveDirectoryIterator($path, RecursiveDirectoryIterator::KEY_AS_PATHNAME | RecursiveDirectoryIterator::CURRENT_AS_SELF);
  429. $iterator = new RecursiveIteratorIterator($directory, RecursiveIteratorIterator::SELF_FIRST);
  430. } catch (Exception $e) {
  431. if ($type === null) {
  432. return array(array(), array());
  433. }
  434. return array();
  435. }
  436. foreach ($iterator as $itemPath => $fsIterator) {
  437. if ($skipHidden) {
  438. $subPathName = $fsIterator->getSubPathname();
  439. if ($subPathName{0} === '.' || strpos($subPathName, DS . '.') !== false) {
  440. continue;
  441. }
  442. }
  443. $item = $fsIterator->current();
  444. if (!empty($exceptions) && isset($exceptions[$item->getFilename()])) {
  445. continue;
  446. }
  447. if ($item->isFile()) {
  448. $files[] = $itemPath;
  449. } elseif ($item->isDir() && !$item->isDot()) {
  450. $directories[] = $itemPath;
  451. }
  452. }
  453. if ($type === null) {
  454. return array($directories, $files);
  455. }
  456. if ($type === 'dir') {
  457. return $directories;
  458. }
  459. return $files;
  460. }
  461. /**
  462. * Create a directory structure recursively.
  463. *
  464. * Can be used to create deep path structures like `/foo/bar/baz/shoe/horn`
  465. *
  466. * @param string $pathname The directory structure to create. Either an absolute or relative
  467. * path. If the path is relative and exists in the process' cwd it will not be created.
  468. * Otherwise relative paths will be prefixed with the current pwd().
  469. * @param int $mode octal value 0755
  470. * @return bool Returns TRUE on success, FALSE on failure
  471. * @link http://book.cakephp.org/2.0/en/core-utility-libraries/file-folder.html#Folder::create
  472. */
  473. public function create($pathname, $mode = false) {
  474. if (is_dir($pathname) || empty($pathname)) {
  475. return true;
  476. }
  477. if (!static::isAbsolute($pathname)) {
  478. $pathname = static::addPathElement($this->pwd(), $pathname);
  479. }
  480. if (!$mode) {
  481. $mode = $this->mode;
  482. }
  483. if (is_file($pathname)) {
  484. $this->_errors[] = __d('cake_dev', '%s is a file', $pathname);
  485. return false;
  486. }
  487. $pathname = rtrim($pathname, DS);
  488. $nextPathname = substr($pathname, 0, strrpos($pathname, DS));
  489. if ($this->create($nextPathname, $mode)) {
  490. if (!file_exists($pathname)) {
  491. $old = umask(0);
  492. if (mkdir($pathname, $mode)) {
  493. umask($old);
  494. $this->_messages[] = __d('cake_dev', '%s created', $pathname);
  495. return true;
  496. }
  497. umask($old);
  498. $this->_errors[] = __d('cake_dev', '%s NOT created', $pathname);
  499. return false;
  500. }
  501. }
  502. return false;
  503. }
  504. /**
  505. * Returns the size in bytes of this Folder and its contents.
  506. *
  507. * @return int size in bytes of current folder
  508. * @link http://book.cakephp.org/2.0/en/core-utility-libraries/file-folder.html#Folder::dirsize
  509. */
  510. public function dirsize() {
  511. $size = 0;
  512. $directory = Folder::slashTerm($this->path);
  513. $stack = array($directory);
  514. $count = count($stack);
  515. for ($i = 0, $j = $count; $i < $j; ++$i) {
  516. if (is_file($stack[$i])) {
  517. $size += filesize($stack[$i]);
  518. } elseif (is_dir($stack[$i])) {
  519. $dir = dir($stack[$i]);
  520. if ($dir) {
  521. while (false !== ($entry = $dir->read())) {
  522. if ($entry === '.' || $entry === '..') {
  523. continue;
  524. }
  525. $add = $stack[$i] . $entry;
  526. if (is_dir($stack[$i] . $entry)) {
  527. $add = Folder::slashTerm($add);
  528. }
  529. $stack[] = $add;
  530. }
  531. $dir->close();
  532. }
  533. }
  534. $j = count($stack);
  535. }
  536. return $size;
  537. }
  538. /**
  539. * Recursively Remove directories if the system allows.
  540. *
  541. * @param string $path Path of directory to delete
  542. * @return bool Success
  543. * @link http://book.cakephp.org/2.0/en/core-utility-libraries/file-folder.html#Folder::delete
  544. */
  545. public function delete($path = null) {
  546. if (!$path) {
  547. $path = $this->pwd();
  548. }
  549. if (!$path) {
  550. return false;
  551. }
  552. $path = Folder::slashTerm($path);
  553. if (is_dir($path)) {
  554. try {
  555. $directory = new RecursiveDirectoryIterator($path, RecursiveDirectoryIterator::CURRENT_AS_SELF);
  556. $iterator = new RecursiveIteratorIterator($directory, RecursiveIteratorIterator::CHILD_FIRST);
  557. } catch (Exception $e) {
  558. return false;
  559. }
  560. foreach ($iterator as $item) {
  561. $filePath = $item->getPathname();
  562. if ($item->isFile() || $item->isLink()) {
  563. //@codingStandardsIgnoreStart
  564. if (@unlink($filePath)) {
  565. //@codingStandardsIgnoreEnd
  566. $this->_messages[] = __d('cake_dev', '%s removed', $filePath);
  567. } else {
  568. $this->_errors[] = __d('cake_dev', '%s NOT removed', $filePath);
  569. }
  570. } elseif ($item->isDir() && !$item->isDot()) {
  571. //@codingStandardsIgnoreStart
  572. if (@rmdir($filePath)) {
  573. //@codingStandardsIgnoreEnd
  574. $this->_messages[] = __d('cake_dev', '%s removed', $filePath);
  575. } else {
  576. $this->_errors[] = __d('cake_dev', '%s NOT removed', $filePath);
  577. return false;
  578. }
  579. }
  580. }
  581. $path = rtrim($path, DS);
  582. //@codingStandardsIgnoreStart
  583. if (@rmdir($path)) {
  584. //@codingStandardsIgnoreEnd
  585. $this->_messages[] = __d('cake_dev', '%s removed', $path);
  586. } else {
  587. $this->_errors[] = __d('cake_dev', '%s NOT removed', $path);
  588. return false;
  589. }
  590. }
  591. return true;
  592. }
  593. /**
  594. * Recursive directory copy.
  595. *
  596. * ### Options
  597. *
  598. * - `to` The directory to copy to.
  599. * - `from` The directory to copy from, this will cause a cd() to occur, changing the results of pwd().
  600. * - `mode` The mode to copy the files/directories with as integer, e.g. 0775.
  601. * - `skip` Files/directories to skip.
  602. * - `scheme` Folder::MERGE, Folder::OVERWRITE, Folder::SKIP
  603. *
  604. * @param array|string $options Either an array of options (see above) or a string of the destination directory.
  605. * @return bool Success.
  606. * @link http://book.cakephp.org/2.0/en/core-utility-libraries/file-folder.html#Folder::copy
  607. */
  608. public function copy($options) {
  609. if (!$this->pwd()) {
  610. return false;
  611. }
  612. $to = null;
  613. if (is_string($options)) {
  614. $to = $options;
  615. $options = array();
  616. }
  617. $options += array(
  618. 'to' => $to,
  619. 'from' => $this->path,
  620. 'mode' => $this->mode,
  621. 'skip' => array(),
  622. 'scheme' => Folder::MERGE
  623. );
  624. $fromDir = $options['from'];
  625. $toDir = $options['to'];
  626. $mode = $options['mode'];
  627. if (!$this->cd($fromDir)) {
  628. $this->_errors[] = __d('cake_dev', '%s not found', $fromDir);
  629. return false;
  630. }
  631. if (!is_dir($toDir)) {
  632. $this->create($toDir, $mode);
  633. }
  634. if (!is_writable($toDir)) {
  635. $this->_errors[] = __d('cake_dev', '%s not writable', $toDir);
  636. return false;
  637. }
  638. $exceptions = array_merge(array('.', '..', '.svn'), $options['skip']);
  639. //@codingStandardsIgnoreStart
  640. if ($handle = @opendir($fromDir)) {
  641. //@codingStandardsIgnoreEnd
  642. while (($item = readdir($handle)) !== false) {
  643. $to = Folder::addPathElement($toDir, $item);
  644. if (($options['scheme'] != Folder::SKIP || !is_dir($to)) && !in_array($item, $exceptions)) {
  645. $from = Folder::addPathElement($fromDir, $item);
  646. if (is_file($from) && (!is_file($to) || $options['scheme'] != Folder::SKIP)) {
  647. if (copy($from, $to)) {
  648. chmod($to, intval($mode, 8));
  649. touch($to, filemtime($from));
  650. $this->_messages[] = __d('cake_dev', '%s copied to %s', $from, $to);
  651. } else {
  652. $this->_errors[] = __d('cake_dev', '%s NOT copied to %s', $from, $to);
  653. }
  654. }
  655. if (is_dir($from) && file_exists($to) && $options['scheme'] === Folder::OVERWRITE) {
  656. $this->delete($to);
  657. }
  658. if (is_dir($from) && !file_exists($to)) {
  659. $old = umask(0);
  660. if (mkdir($to, $mode)) {
  661. umask($old);
  662. $old = umask(0);
  663. chmod($to, $mode);
  664. umask($old);
  665. $this->_messages[] = __d('cake_dev', '%s created', $to);
  666. $options = array('to' => $to, 'from' => $from) + $options;
  667. $this->copy($options);
  668. } else {
  669. $this->_errors[] = __d('cake_dev', '%s not created', $to);
  670. }
  671. } elseif (is_dir($from) && $options['scheme'] === Folder::MERGE) {
  672. $options = array('to' => $to, 'from' => $from) + $options;
  673. $this->copy($options);
  674. }
  675. }
  676. }
  677. closedir($handle);
  678. } else {
  679. return false;
  680. }
  681. if (!empty($this->_errors)) {
  682. return false;
  683. }
  684. return true;
  685. }
  686. /**
  687. * Recursive directory move.
  688. *
  689. * ### Options
  690. *
  691. * - `to` The directory to copy to.
  692. * - `from` The directory to copy from, this will cause a cd() to occur, changing the results of pwd().
  693. * - `chmod` The mode to copy the files/directories with.
  694. * - `skip` Files/directories to skip.
  695. * - `scheme` Folder::MERGE, Folder::OVERWRITE, Folder::SKIP
  696. *
  697. * @param array $options (to, from, chmod, skip, scheme)
  698. * @return bool Success
  699. * @link http://book.cakephp.org/2.0/en/core-utility-libraries/file-folder.html#Folder::move
  700. */
  701. public function move($options) {
  702. $to = null;
  703. if (is_string($options)) {
  704. $to = $options;
  705. $options = (array)$options;
  706. }
  707. $options += array('to' => $to, 'from' => $this->path, 'mode' => $this->mode, 'skip' => array());
  708. if ($this->copy($options)) {
  709. if ($this->delete($options['from'])) {
  710. return (bool)$this->cd($options['to']);
  711. }
  712. }
  713. return false;
  714. }
  715. /**
  716. * get messages from latest method
  717. *
  718. * @param bool $reset Reset message stack after reading
  719. * @return array
  720. * @link http://book.cakephp.org/2.0/en/core-utility-libraries/file-folder.html#Folder::messages
  721. */
  722. public function messages($reset = true) {
  723. $messages = $this->_messages;
  724. if ($reset) {
  725. $this->_messages = array();
  726. }
  727. return $messages;
  728. }
  729. /**
  730. * get error from latest method
  731. *
  732. * @param bool $reset Reset error stack after reading
  733. * @return array
  734. * @link http://book.cakephp.org/2.0/en/core-utility-libraries/file-folder.html#Folder::errors
  735. */
  736. public function errors($reset = true) {
  737. $errors = $this->_errors;
  738. if ($reset) {
  739. $this->_errors = array();
  740. }
  741. return $errors;
  742. }
  743. /**
  744. * Get the real path (taking ".." and such into account)
  745. *
  746. * @param string $path Path to resolve
  747. * @return string The resolved path
  748. * @link http://book.cakephp.org/2.0/en/core-utility-libraries/file-folder.html#Folder::realpath
  749. */
  750. public function realpath($path) {
  751. $path = str_replace('/', DS, trim($path));
  752. if (strpos($path, '..') === false) {
  753. if (!Folder::isAbsolute($path)) {
  754. $path = Folder::addPathElement($this->path, $path);
  755. }
  756. return $path;
  757. }
  758. $parts = explode(DS, $path);
  759. $newparts = array();
  760. $newpath = '';
  761. if ($path[0] === DS) {
  762. $newpath = DS;
  763. }
  764. while (($part = array_shift($parts)) !== null) {
  765. if ($part === '.' || $part === '') {
  766. continue;
  767. }
  768. if ($part === '..') {
  769. if (!empty($newparts)) {
  770. array_pop($newparts);
  771. continue;
  772. }
  773. return false;
  774. }
  775. $newparts[] = $part;
  776. }
  777. $newpath .= implode(DS, $newparts);
  778. return Folder::slashTerm($newpath);
  779. }
  780. /**
  781. * Returns true if given $path ends in a slash (i.e. is slash-terminated).
  782. *
  783. * @param string $path Path to check
  784. * @return bool true if path ends with slash, false otherwise
  785. * @link http://book.cakephp.org/2.0/en/core-utility-libraries/file-folder.html#Folder::isSlashTerm
  786. */
  787. public static function isSlashTerm($path) {
  788. $lastChar = $path[strlen($path) - 1];
  789. return $lastChar === '/' || $lastChar === '\\';
  790. }
  791. }