/station-games/vendor/cakephp/cakephp/src/Filesystem/Folder.php

https://gitlab.com/ViniciusP/project-games · PHP · 872 lines · 510 code · 77 blank · 285 comment · 117 complexity · a3265103e5452ebafaf2de36420a2aa9 MD5 · raw file

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