PageRenderTime 26ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/cake/libs/folder.php

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