2008-05-30 11:40:08 +00:00
|
|
|
<?php
|
|
|
|
/**
|
2017-06-10 21:33:55 +00:00
|
|
|
* CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
|
2017-06-10 22:10:52 +00:00
|
|
|
* Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
|
|
|
* Licensed under The MIT License
|
2013-02-08 12:22:51 +00:00
|
|
|
* For full copyright and license information, please see the LICENSE.txt
|
2008-05-30 11:40:08 +00:00
|
|
|
* Redistributions of files must retain the above copyright notice.
|
|
|
|
*
|
2017-06-10 22:10:52 +00:00
|
|
|
* @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
|
2017-06-10 21:33:55 +00:00
|
|
|
* @link https://cakephp.org CakePHP(tm) Project
|
2011-07-26 06:16:14 +00:00
|
|
|
* @package Cake.Utility
|
2008-10-30 17:30:26 +00:00
|
|
|
* @since CakePHP(tm) v 0.2.9
|
2017-06-10 22:23:14 +00:00
|
|
|
* @license https://opensource.org/licenses/mit-license.php MIT License
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Folder structure browser, lists folders and files.
|
2010-01-25 14:42:17 +00:00
|
|
|
* Provides an Object interface for Common directory related tasks.
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2011-07-26 06:16:14 +00:00
|
|
|
* @package Cake.Utility
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-04-18 05:12:00 +00:00
|
|
|
class Folder {
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2012-09-10 08:23:52 +00:00
|
|
|
/**
|
|
|
|
* Default scheme for Folder::copy
|
2012-09-14 17:26:30 +00:00
|
|
|
* Recursively merges subfolders with the same name
|
2012-09-10 08:23:52 +00:00
|
|
|
*
|
2014-02-07 14:45:00 +00:00
|
|
|
* @var string
|
2012-09-10 08:23:52 +00:00
|
|
|
*/
|
|
|
|
const MERGE = 'merge';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Overwrite scheme for Folder::copy
|
|
|
|
* subfolders with the same name will be replaced
|
|
|
|
*
|
2014-02-07 14:45:00 +00:00
|
|
|
* @var string
|
2012-09-10 08:23:52 +00:00
|
|
|
*/
|
|
|
|
const OVERWRITE = 'overwrite';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Skip scheme for Folder::copy
|
|
|
|
* if a subfolder with the same name exists it will be skipped
|
|
|
|
*
|
2014-02-07 14:45:00 +00:00
|
|
|
* @var string
|
2012-09-10 08:23:52 +00:00
|
|
|
*/
|
|
|
|
const SKIP = 'skip';
|
2012-09-14 01:05:10 +00:00
|
|
|
|
2016-05-12 14:28:04 +00:00
|
|
|
/**
|
|
|
|
* Sort mode by name
|
|
|
|
*/
|
|
|
|
const SORT_NAME = 'name';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sort mode by time
|
|
|
|
*/
|
|
|
|
const SORT_TIME = 'time';
|
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Path to Folder.
|
|
|
|
*
|
|
|
|
* @var string
|
2017-06-10 22:15:34 +00:00
|
|
|
* @link https://book.cakephp.org/2.0/en/core-utility-libraries/file-folder.html#Folder::$path
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-04-04 07:14:00 +00:00
|
|
|
public $path = null;
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
2010-01-25 14:42:17 +00:00
|
|
|
* Sortedness. Whether or not list results
|
|
|
|
* should be sorted by name.
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2014-07-03 13:36:42 +00:00
|
|
|
* @var bool
|
2017-06-10 22:15:34 +00:00
|
|
|
* @link https://book.cakephp.org/2.0/en/core-utility-libraries/file-folder.html#Folder::$sort
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-04-04 07:14:00 +00:00
|
|
|
public $sort = false;
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
2015-07-09 13:54:03 +00:00
|
|
|
* Mode to be used on create. Does nothing on Windows platforms.
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2014-07-03 13:36:42 +00:00
|
|
|
* @var int
|
2017-06-10 22:15:34 +00:00
|
|
|
* https://book.cakephp.org/2.0/en/core-utility-libraries/file-folder.html#Folder::$mode
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-04-04 07:14:00 +00:00
|
|
|
public $mode = 0755;
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2016-05-12 14:28:04 +00:00
|
|
|
/**
|
|
|
|
* Functions array to be called depending on the sort type chosen.
|
|
|
|
*/
|
|
|
|
protected $_fsorts = array(
|
|
|
|
self::SORT_NAME => 'getPathname',
|
|
|
|
self::SORT_TIME => 'getCTime'
|
|
|
|
);
|
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
2010-01-25 14:42:17 +00:00
|
|
|
* Holds messages from last method.
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
2011-08-20 04:43:34 +00:00
|
|
|
protected $_messages = array();
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
2010-01-25 14:42:17 +00:00
|
|
|
* Holds errors from last method.
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
2011-08-20 04:43:34 +00:00
|
|
|
protected $_errors = array();
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
2010-01-25 14:42:17 +00:00
|
|
|
* Holds array of complete directory paths.
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
2011-08-20 04:43:34 +00:00
|
|
|
protected $_directories;
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
2010-01-25 14:42:17 +00:00
|
|
|
* Holds array of complete file paths.
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
2011-08-20 04:43:34 +00:00
|
|
|
protected $_files;
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Constructor.
|
|
|
|
*
|
|
|
|
* @param string $path Path to folder
|
2014-07-03 13:36:42 +00:00
|
|
|
* @param bool $create Create folder if not found
|
|
|
|
* @param string|bool $mode Mode (CHMOD) to apply to created folder, false to ignore
|
2017-06-10 22:15:34 +00:00
|
|
|
* @link https://book.cakephp.org/2.0/en/core-utility-libraries/file-folder.html#Folder
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2011-05-28 20:38:46 +00:00
|
|
|
public function __construct($path = false, $create = false, $mode = false) {
|
2008-05-30 11:40:08 +00:00
|
|
|
if (empty($path)) {
|
|
|
|
$path = TMP;
|
|
|
|
}
|
|
|
|
if ($mode) {
|
2008-09-17 21:54:36 +00:00
|
|
|
$this->mode = $mode;
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
|
2008-11-06 18:00:56 +00:00
|
|
|
if (!file_exists($path) && $create === true) {
|
2008-05-30 11:40:08 +00:00
|
|
|
$this->create($path, $this->mode);
|
|
|
|
}
|
2008-12-18 00:17:19 +00:00
|
|
|
if (!Folder::isAbsolute($path)) {
|
2008-05-30 11:40:08 +00:00
|
|
|
$path = realpath($path);
|
|
|
|
}
|
|
|
|
if (!empty($path)) {
|
|
|
|
$this->cd($path);
|
|
|
|
}
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Return current path.
|
|
|
|
*
|
|
|
|
* @return string Current path
|
2017-06-10 22:15:34 +00:00
|
|
|
* @link https://book.cakephp.org/2.0/en/core-utility-libraries/file-folder.html#Folder::pwd
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-04-05 03:19:38 +00:00
|
|
|
public function pwd() {
|
2008-05-30 11:40:08 +00:00
|
|
|
return $this->path;
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
2008-10-23 00:10:44 +00:00
|
|
|
* Change directory to $path.
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2008-10-23 00:10:44 +00:00
|
|
|
* @param string $path Path to the directory to change to
|
2008-05-30 11:40:08 +00:00
|
|
|
* @return string The new path. Returns false on failure
|
2017-06-10 22:15:34 +00:00
|
|
|
* @link https://book.cakephp.org/2.0/en/core-utility-libraries/file-folder.html#Folder::cd
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-04-05 03:19:38 +00:00
|
|
|
public function cd($path) {
|
2008-05-30 11:40:08 +00:00
|
|
|
$path = $this->realpath($path);
|
2008-11-06 18:00:56 +00:00
|
|
|
if (is_dir($path)) {
|
2008-05-30 11:40:08 +00:00
|
|
|
return $this->path = $path;
|
|
|
|
}
|
|
|
|
return false;
|
2008-06-20 20:17:23 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
2008-11-05 02:18:49 +00:00
|
|
|
* Returns an array of the contents of the current directory.
|
|
|
|
* The returned array holds two arrays: One of directories and one of files.
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2016-05-12 14:28:04 +00:00
|
|
|
* @param string|bool $sort Whether you want the results sorted, set this and the sort property
|
2010-01-25 14:42:17 +00:00
|
|
|
* to false to get unsorted results.
|
2014-07-03 13:36:42 +00:00
|
|
|
* @param array|bool $exceptions Either an array or boolean true will not grab dot files
|
|
|
|
* @param bool $fullPath True returns the full path
|
2008-11-05 02:18:49 +00:00
|
|
|
* @return mixed Contents of current directory as an array, an empty array on failure
|
2017-06-10 22:15:34 +00:00
|
|
|
* @link https://book.cakephp.org/2.0/en/core-utility-libraries/file-folder.html#Folder::read
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2016-05-12 14:28:04 +00:00
|
|
|
public function read($sort = self::SORT_NAME, $exceptions = false, $fullPath = false) {
|
2008-05-30 11:40:08 +00:00
|
|
|
$dirs = $files = array();
|
|
|
|
|
2009-08-03 00:22:57 +00:00
|
|
|
if (!$this->pwd()) {
|
|
|
|
return array($dirs, $files);
|
|
|
|
}
|
2008-08-25 22:55:10 +00:00
|
|
|
if (is_array($exceptions)) {
|
|
|
|
$exceptions = array_flip($exceptions);
|
|
|
|
}
|
2008-11-05 02:18:49 +00:00
|
|
|
$skipHidden = isset($exceptions['.']) || $exceptions === true;
|
2008-08-25 22:55:10 +00:00
|
|
|
|
2011-08-27 02:10:29 +00:00
|
|
|
try {
|
|
|
|
$iterator = new DirectoryIterator($this->path);
|
2012-01-19 21:54:38 +00:00
|
|
|
} catch (Exception $e) {
|
2008-11-05 02:18:49 +00:00
|
|
|
return array($dirs, $files);
|
|
|
|
}
|
2016-05-12 14:28:04 +00:00
|
|
|
if (!is_bool($sort) && isset($this->_fsorts[$sort])) {
|
|
|
|
$methodName = $this->_fsorts[$sort];
|
|
|
|
} else {
|
|
|
|
$methodName = $this->_fsorts[self::SORT_NAME];
|
|
|
|
}
|
2008-05-30 11:40:08 +00:00
|
|
|
|
2011-08-27 02:10:29 +00:00
|
|
|
foreach ($iterator as $item) {
|
|
|
|
if ($item->isDot()) {
|
2008-11-05 02:18:49 +00:00
|
|
|
continue;
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2011-08-27 02:10:29 +00:00
|
|
|
$name = $item->getFileName();
|
|
|
|
if ($skipHidden && $name[0] === '.' || isset($exceptions[$name])) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if ($fullPath) {
|
|
|
|
$name = $item->getPathName();
|
|
|
|
}
|
|
|
|
if ($item->isDir()) {
|
2016-05-12 14:28:04 +00:00
|
|
|
$dirs[$item->{$methodName}()][] = $name;
|
2008-11-05 02:18:49 +00:00
|
|
|
} else {
|
2016-05-12 14:28:04 +00:00
|
|
|
$files[$item->{$methodName}()][] = $name;
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
}
|
2016-05-12 14:28:04 +00:00
|
|
|
|
2008-11-05 02:18:49 +00:00
|
|
|
if ($sort || $this->sort) {
|
2016-05-12 14:28:04 +00:00
|
|
|
ksort($dirs);
|
|
|
|
ksort($files);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($dirs) {
|
|
|
|
$dirs = call_user_func_array('array_merge', $dirs);
|
|
|
|
}
|
|
|
|
if ($files) {
|
|
|
|
$files = call_user_func_array('array_merge', $files);
|
2008-11-05 02:18:49 +00:00
|
|
|
}
|
2008-05-30 11:40:08 +00:00
|
|
|
return array($dirs, $files);
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Returns an array of all matching files in current directory.
|
|
|
|
*
|
2011-07-29 03:24:31 +00:00
|
|
|
* @param string $regexpPattern Preg_match pattern (Defaults to: .*)
|
2014-07-03 13:36:42 +00:00
|
|
|
* @param bool $sort Whether results should be sorted.
|
2008-05-30 11:40:08 +00:00
|
|
|
* @return array Files that match given pattern
|
2017-06-10 22:15:34 +00:00
|
|
|
* @link https://book.cakephp.org/2.0/en/core-utility-libraries/file-folder.html#Folder::find
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-04-05 03:19:38 +00:00
|
|
|
public function find($regexpPattern = '.*', $sort = false) {
|
2012-12-20 12:47:03 +00:00
|
|
|
list(, $files) = $this->read($sort);
|
2012-03-03 22:31:47 +00:00
|
|
|
return array_values(preg_grep('/^' . $regexpPattern . '$/i', $files));
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Returns an array of all matching files in and below current directory.
|
|
|
|
*
|
|
|
|
* @param string $pattern Preg_match pattern (Defaults to: .*)
|
2014-07-03 13:36:42 +00:00
|
|
|
* @param bool $sort Whether results should be sorted.
|
2008-05-30 11:40:08 +00:00
|
|
|
* @return array Files matching $pattern
|
2017-06-10 22:15:34 +00:00
|
|
|
* @link https://book.cakephp.org/2.0/en/core-utility-libraries/file-folder.html#Folder::findRecursive
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-04-05 03:19:38 +00:00
|
|
|
public function findRecursive($pattern = '.*', $sort = false) {
|
2009-08-02 22:42:27 +00:00
|
|
|
if (!$this->pwd()) {
|
|
|
|
return array();
|
|
|
|
}
|
2008-05-30 11:40:08 +00:00
|
|
|
$startsOn = $this->path;
|
|
|
|
$out = $this->_findRecursive($pattern, $sort);
|
|
|
|
$this->cd($startsOn);
|
|
|
|
return $out;
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Private helper function for findRecursive.
|
|
|
|
*
|
|
|
|
* @param string $pattern Pattern to match against
|
2014-07-03 13:36:42 +00:00
|
|
|
* @param bool $sort Whether results should be sorted.
|
2008-05-30 11:40:08 +00:00
|
|
|
* @return array Files matching pattern
|
|
|
|
*/
|
2011-05-30 20:26:42 +00:00
|
|
|
protected function _findRecursive($pattern, $sort = false) {
|
2008-05-30 11:40:08 +00:00
|
|
|
list($dirs, $files) = $this->read($sort);
|
|
|
|
$found = array();
|
2009-03-19 21:10:13 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
foreach ($files as $file) {
|
2008-11-06 18:00:56 +00:00
|
|
|
if (preg_match('/^' . $pattern . '$/i', $file)) {
|
2008-12-18 00:17:19 +00:00
|
|
|
$found[] = Folder::addPathElement($this->path, $file);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
$start = $this->path;
|
2009-03-19 21:10:13 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
foreach ($dirs as $dir) {
|
2008-12-18 00:17:19 +00:00
|
|
|
$this->cd(Folder::addPathElement($start, $dir));
|
2009-03-19 21:10:13 +00:00
|
|
|
$found = array_merge($found, $this->findRecursive($pattern, $sort));
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
return $found;
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Returns true if given $path is a Windows path.
|
|
|
|
*
|
|
|
|
* @param string $path Path to check
|
2015-07-09 13:54:03 +00:00
|
|
|
* @return bool true if Windows path, false otherwise
|
2017-06-10 22:15:34 +00:00
|
|
|
* @link https://book.cakephp.org/2.0/en/core-utility-libraries/file-folder.html#Folder::isWindowsPath
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-12-13 01:55:33 +00:00
|
|
|
public static function isWindowsPath($path) {
|
2013-02-12 02:38:08 +00:00
|
|
|
return (preg_match('/^[A-Z]:\\\\/i', $path) || substr($path, 0, 2) === '\\\\');
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Returns true if given $path is an absolute path.
|
|
|
|
*
|
|
|
|
* @param string $path Path to check
|
2014-07-03 13:36:42 +00:00
|
|
|
* @return bool true if path is absolute.
|
2017-06-10 22:15:34 +00:00
|
|
|
* @link https://book.cakephp.org/2.0/en/core-utility-libraries/file-folder.html#Folder::isAbsolute
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-12-13 01:55:33 +00:00
|
|
|
public static function isAbsolute($path) {
|
2014-06-22 08:13:14 +00:00
|
|
|
if (empty($path)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $path[0] === '/' ||
|
|
|
|
preg_match('/^[A-Z]:\\\\/i', $path) ||
|
|
|
|
substr($path, 0, 2) === '\\\\' ||
|
2015-07-21 08:22:53 +00:00
|
|
|
static::isRegisteredStreamWrapper($path);
|
2014-06-22 08:13:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns true if given $path is a registered stream wrapper.
|
|
|
|
*
|
|
|
|
* @param string $path Path to check
|
2014-10-02 00:34:33 +00:00
|
|
|
* @return boo true If path is registered stream wrapper.
|
2014-06-22 08:13:14 +00:00
|
|
|
*/
|
|
|
|
public static function isRegisteredStreamWrapper($path) {
|
|
|
|
if (preg_match('/^[A-Z]+(?=:\/\/)/i', $path, $matches) &&
|
|
|
|
in_array($matches[0], stream_get_wrappers())
|
|
|
|
) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Returns a correct set of slashes for given $path. (\\ for Windows paths and / for other paths.)
|
|
|
|
*
|
|
|
|
* @param string $path Path to check
|
|
|
|
* @return string Set of slashes ("\\" or "/")
|
2017-06-10 22:15:34 +00:00
|
|
|
* @link https://book.cakephp.org/2.0/en/core-utility-libraries/file-folder.html#Folder::normalizePath
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-12-13 01:55:33 +00:00
|
|
|
public static function normalizePath($path) {
|
2008-12-18 00:27:10 +00:00
|
|
|
return Folder::correctSlashFor($path);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Returns a correct set of slashes for given $path. (\\ for Windows paths and / for other paths.)
|
|
|
|
*
|
|
|
|
* @param string $path Path to check
|
|
|
|
* @return string Set of slashes ("\\" or "/")
|
2017-06-10 22:15:34 +00:00
|
|
|
* @link https://book.cakephp.org/2.0/en/core-utility-libraries/file-folder.html#Folder::correctSlashFor
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-12-13 01:55:33 +00:00
|
|
|
public static function correctSlashFor($path) {
|
2010-01-25 14:42:17 +00:00
|
|
|
return (Folder::isWindowsPath($path)) ? '\\' : '/';
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Returns $path with added terminating slash (corrected for Windows or other OS).
|
|
|
|
*
|
|
|
|
* @param string $path Path to check
|
|
|
|
* @return string Path with ending slash
|
2017-06-10 22:15:34 +00:00
|
|
|
* @link https://book.cakephp.org/2.0/en/core-utility-libraries/file-folder.html#Folder::slashTerm
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-12-13 01:55:33 +00:00
|
|
|
public static function slashTerm($path) {
|
2008-05-30 11:40:08 +00:00
|
|
|
if (Folder::isSlashTerm($path)) {
|
|
|
|
return $path;
|
|
|
|
}
|
|
|
|
return $path . Folder::correctSlashFor($path);
|
2008-06-20 20:17:23 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Returns $path with $element added, with correct slash in-between.
|
|
|
|
*
|
|
|
|
* @param string $path Path
|
2013-10-03 02:45:22 +00:00
|
|
|
* @param string|array $element Element to add at end of path
|
2008-05-30 11:40:08 +00:00
|
|
|
* @return string Combined path
|
2017-06-10 22:15:34 +00:00
|
|
|
* @link https://book.cakephp.org/2.0/en/core-utility-libraries/file-folder.html#Folder::addPathElement
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-12-13 01:55:33 +00:00
|
|
|
public static function addPathElement($path, $element) {
|
2013-10-03 02:45:22 +00:00
|
|
|
$element = (array)$element;
|
|
|
|
array_unshift($element, rtrim($path, DS));
|
2013-10-03 02:55:09 +00:00
|
|
|
return implode(DS, $element);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
Fix/tighten `Folder::inPath()` checks.
The current checks are way too relaxed, and are more like testing
for a substring, which makes it easy for invalid paths to slip
trough, for example `/foo/var/www` is falsely tested to reside in
`/var/www`.
Passing an empty path never worked properly, it was triggering a
warning, didn't worked on Windows, and the behavior that the current
top level directory would be assumed for empty paths wasn't
documented.
Similar is true for relative paths. While they did match at one point,
this was incorrect behavior, and matching actual path fragments seems
out of scope for this method.
This change makes the `$path` argument required, requires it to be an
absolute path, and throws an exception in case a non-absolute path is
being passed.
2016-08-26 11:28:02 +00:00
|
|
|
* Returns true if the Folder is in the given Cake path.
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2010-01-25 14:42:17 +00:00
|
|
|
* @param string $path The path to check.
|
2014-07-03 13:36:42 +00:00
|
|
|
* @return bool
|
2017-06-10 22:15:34 +00:00
|
|
|
* @link https://book.cakephp.org/2.0/en/core-utility-libraries/file-folder.html#Folder::inCakePath
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-04-05 03:19:38 +00:00
|
|
|
public function inCakePath($path = '') {
|
2008-12-18 00:17:19 +00:00
|
|
|
$dir = substr(Folder::slashTerm(ROOT), 0, -1);
|
2008-05-30 11:40:08 +00:00
|
|
|
$newdir = $dir . $path;
|
|
|
|
|
|
|
|
return $this->inPath($newdir);
|
2008-06-20 20:17:23 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
Fix/tighten `Folder::inPath()` checks.
The current checks are way too relaxed, and are more like testing
for a substring, which makes it easy for invalid paths to slip
trough, for example `/foo/var/www` is falsely tested to reside in
`/var/www`.
Passing an empty path never worked properly, it was triggering a
warning, didn't worked on Windows, and the behavior that the current
top level directory would be assumed for empty paths wasn't
documented.
Similar is true for relative paths. While they did match at one point,
this was incorrect behavior, and matching actual path fragments seems
out of scope for this method.
This change makes the `$path` argument required, requires it to be an
absolute path, and throws an exception in case a non-absolute path is
being passed.
2016-08-26 11:28:02 +00:00
|
|
|
* Returns true if the Folder is in the given path.
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
Fix/tighten `Folder::inPath()` checks.
The current checks are way too relaxed, and are more like testing
for a substring, which makes it easy for invalid paths to slip
trough, for example `/foo/var/www` is falsely tested to reside in
`/var/www`.
Passing an empty path never worked properly, it was triggering a
warning, didn't worked on Windows, and the behavior that the current
top level directory would be assumed for empty paths wasn't
documented.
Similar is true for relative paths. While they did match at one point,
this was incorrect behavior, and matching actual path fragments seems
out of scope for this method.
This change makes the `$path` argument required, requires it to be an
absolute path, and throws an exception in case a non-absolute path is
being passed.
2016-08-26 11:28:02 +00:00
|
|
|
* @param string $path The absolute path to check that the current `pwd()` resides within.
|
|
|
|
* @param bool $reverse Reverse the search, check if the given `$path` resides within the current `pwd()`.
|
2014-07-03 13:36:42 +00:00
|
|
|
* @return bool
|
Fix/tighten `Folder::inPath()` checks.
The current checks are way too relaxed, and are more like testing
for a substring, which makes it easy for invalid paths to slip
trough, for example `/foo/var/www` is falsely tested to reside in
`/var/www`.
Passing an empty path never worked properly, it was triggering a
warning, didn't worked on Windows, and the behavior that the current
top level directory would be assumed for empty paths wasn't
documented.
Similar is true for relative paths. While they did match at one point,
this was incorrect behavior, and matching actual path fragments seems
out of scope for this method.
This change makes the `$path` argument required, requires it to be an
absolute path, and throws an exception in case a non-absolute path is
being passed.
2016-08-26 11:28:02 +00:00
|
|
|
* @throws \InvalidArgumentException When the given `$path` argument is not an absolute path.
|
2017-06-10 22:15:34 +00:00
|
|
|
* @link https://book.cakephp.org/2.0/en/core-utility-libraries/file-folder.html#Folder::inPath
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-04-05 03:19:38 +00:00
|
|
|
public function inPath($path = '', $reverse = false) {
|
Fix/tighten `Folder::inPath()` checks.
The current checks are way too relaxed, and are more like testing
for a substring, which makes it easy for invalid paths to slip
trough, for example `/foo/var/www` is falsely tested to reside in
`/var/www`.
Passing an empty path never worked properly, it was triggering a
warning, didn't worked on Windows, and the behavior that the current
top level directory would be assumed for empty paths wasn't
documented.
Similar is true for relative paths. While they did match at one point,
this was incorrect behavior, and matching actual path fragments seems
out of scope for this method.
This change makes the `$path` argument required, requires it to be an
absolute path, and throws an exception in case a non-absolute path is
being passed.
2016-08-26 11:28:02 +00:00
|
|
|
if (!Folder::isAbsolute($path)) {
|
|
|
|
throw new InvalidArgumentException(__d('cake_dev', 'The $path argument is expected to be an absolute path.'));
|
|
|
|
}
|
|
|
|
|
2008-12-18 00:17:19 +00:00
|
|
|
$dir = Folder::slashTerm($path);
|
|
|
|
$current = Folder::slashTerm($this->pwd());
|
2008-05-30 11:40:08 +00:00
|
|
|
|
|
|
|
if (!$reverse) {
|
Fix/tighten `Folder::inPath()` checks.
The current checks are way too relaxed, and are more like testing
for a substring, which makes it easy for invalid paths to slip
trough, for example `/foo/var/www` is falsely tested to reside in
`/var/www`.
Passing an empty path never worked properly, it was triggering a
warning, didn't worked on Windows, and the behavior that the current
top level directory would be assumed for empty paths wasn't
documented.
Similar is true for relative paths. While they did match at one point,
this was incorrect behavior, and matching actual path fragments seems
out of scope for this method.
This change makes the `$path` argument required, requires it to be an
absolute path, and throws an exception in case a non-absolute path is
being passed.
2016-08-26 11:28:02 +00:00
|
|
|
$return = preg_match('/^' . preg_quote($dir, '/') . '(.*)/', $current);
|
2008-05-30 11:40:08 +00:00
|
|
|
} else {
|
Fix/tighten `Folder::inPath()` checks.
The current checks are way too relaxed, and are more like testing
for a substring, which makes it easy for invalid paths to slip
trough, for example `/foo/var/www` is falsely tested to reside in
`/var/www`.
Passing an empty path never worked properly, it was triggering a
warning, didn't worked on Windows, and the behavior that the current
top level directory would be assumed for empty paths wasn't
documented.
Similar is true for relative paths. While they did match at one point,
this was incorrect behavior, and matching actual path fragments seems
out of scope for this method.
This change makes the `$path` argument required, requires it to be an
absolute path, and throws an exception in case a non-absolute path is
being passed.
2016-08-26 11:28:02 +00:00
|
|
|
$return = preg_match('/^' . preg_quote($current, '/') . '(.*)/', $dir);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2010-01-24 03:03:53 +00:00
|
|
|
return (bool)$return;
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
2008-09-09 04:34:11 +00:00
|
|
|
* Change the mode on a directory structure recursively. This includes changing the mode on files as well.
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2014-09-10 15:56:02 +00:00
|
|
|
* @param string $path The path to chmod.
|
|
|
|
* @param int $mode Octal value, e.g. 0755.
|
|
|
|
* @param bool $recursive Chmod recursively, set to false to only change the current directory.
|
|
|
|
* @param array $exceptions Array of files, directories to skip.
|
|
|
|
* @return bool Success.
|
2017-06-10 22:15:34 +00:00
|
|
|
* @link https://book.cakephp.org/2.0/en/core-utility-libraries/file-folder.html#Folder::chmod
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-04-05 03:19:38 +00:00
|
|
|
public function chmod($path, $mode = false, $recursive = true, $exceptions = array()) {
|
2008-06-20 20:17:23 +00:00
|
|
|
if (!$mode) {
|
|
|
|
$mode = $this->mode;
|
|
|
|
}
|
2008-05-30 11:40:08 +00:00
|
|
|
|
2008-06-20 20:17:23 +00:00
|
|
|
if ($recursive === false && is_dir($path)) {
|
2012-11-14 09:00:15 +00:00
|
|
|
//@codingStandardsIgnoreStart
|
2008-09-08 20:38:55 +00:00
|
|
|
if (@chmod($path, intval($mode, 8))) {
|
2012-11-14 09:00:15 +00:00
|
|
|
//@codingStandardsIgnoreEnd
|
2011-08-20 04:43:34 +00:00
|
|
|
$this->_messages[] = __d('cake_dev', '%s changed to %s', $path, $mode);
|
2008-06-20 20:17:23 +00:00
|
|
|
return true;
|
|
|
|
}
|
2008-09-09 04:34:11 +00:00
|
|
|
|
2011-08-20 04:43:34 +00:00
|
|
|
$this->_errors[] = __d('cake_dev', '%s NOT changed to %s', $path, $mode);
|
2008-09-09 04:34:11 +00:00
|
|
|
return false;
|
2008-06-20 20:17:23 +00:00
|
|
|
}
|
2008-05-30 11:40:08 +00:00
|
|
|
|
2008-06-20 20:17:23 +00:00
|
|
|
if (is_dir($path)) {
|
2008-09-09 04:34:11 +00:00
|
|
|
$paths = $this->tree($path);
|
2008-05-30 11:40:08 +00:00
|
|
|
|
2008-09-09 04:34:11 +00:00
|
|
|
foreach ($paths as $type) {
|
2012-09-05 22:22:30 +00:00
|
|
|
foreach ($type as $fullpath) {
|
2008-09-09 04:34:11 +00:00
|
|
|
$check = explode(DS, $fullpath);
|
|
|
|
$count = count($check);
|
2008-05-30 11:40:08 +00:00
|
|
|
|
2008-09-09 04:34:11 +00:00
|
|
|
if (in_array($check[$count - 1], $exceptions)) {
|
|
|
|
continue;
|
|
|
|
}
|
2008-05-30 11:40:08 +00:00
|
|
|
|
2012-11-14 09:00:15 +00:00
|
|
|
//@codingStandardsIgnoreStart
|
2008-09-09 04:34:11 +00:00
|
|
|
if (@chmod($fullpath, intval($mode, 8))) {
|
2012-11-14 09:00:15 +00:00
|
|
|
//@codingStandardsIgnoreEnd
|
2011-08-20 04:43:34 +00:00
|
|
|
$this->_messages[] = __d('cake_dev', '%s changed to %s', $fullpath, $mode);
|
2008-09-09 04:34:11 +00:00
|
|
|
} else {
|
2011-08-20 04:43:34 +00:00
|
|
|
$this->_errors[] = __d('cake_dev', '%s NOT changed to %s', $fullpath, $mode);
|
2008-09-09 04:34:11 +00:00
|
|
|
}
|
2008-06-20 20:17:23 +00:00
|
|
|
}
|
|
|
|
}
|
2008-09-09 04:34:11 +00:00
|
|
|
|
2011-08-20 04:43:34 +00:00
|
|
|
if (empty($this->_errors)) {
|
2008-06-20 20:17:23 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Returns an array of nested directories and files in each directory
|
|
|
|
*
|
|
|
|
* @param string $path the directory path to build the tree from
|
2014-07-03 13:36:42 +00:00
|
|
|
* @param array|bool $exceptions Either an array of files/folder to exclude
|
2011-12-21 20:33:22 +00:00
|
|
|
* or boolean true to not grab dot files/folders
|
|
|
|
* @param string $type either 'file' or 'dir'. null returns both files and directories
|
2008-05-30 11:40:08 +00:00
|
|
|
* @return mixed array of nested directories and files in each directory
|
2017-06-10 22:15:34 +00:00
|
|
|
* @link https://book.cakephp.org/2.0/en/core-utility-libraries/file-folder.html#Folder::tree
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2011-12-21 20:33:22 +00:00
|
|
|
public function tree($path = null, $exceptions = false, $type = null) {
|
2012-09-14 17:26:30 +00:00
|
|
|
if (!$path) {
|
2011-08-27 14:34:08 +00:00
|
|
|
$path = $this->path;
|
2010-01-25 16:14:18 +00:00
|
|
|
}
|
2011-08-27 02:38:34 +00:00
|
|
|
$files = array();
|
|
|
|
$directories = array($path);
|
2008-05-30 11:40:08 +00:00
|
|
|
|
2011-08-27 02:38:34 +00:00
|
|
|
if (is_array($exceptions)) {
|
|
|
|
$exceptions = array_flip($exceptions);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2011-12-21 20:33:22 +00:00
|
|
|
$skipHidden = false;
|
|
|
|
if ($exceptions === true) {
|
|
|
|
$skipHidden = true;
|
|
|
|
} elseif (isset($exceptions['.'])) {
|
|
|
|
$skipHidden = true;
|
|
|
|
unset($exceptions['.']);
|
|
|
|
}
|
2008-09-09 04:34:11 +00:00
|
|
|
|
2011-08-27 02:38:34 +00:00
|
|
|
try {
|
2012-01-19 21:54:38 +00:00
|
|
|
$directory = new RecursiveDirectoryIterator($path, RecursiveDirectoryIterator::KEY_AS_PATHNAME | RecursiveDirectoryIterator::CURRENT_AS_SELF);
|
2011-08-27 02:38:34 +00:00
|
|
|
$iterator = new RecursiveIteratorIterator($directory, RecursiveIteratorIterator::SELF_FIRST);
|
2012-01-19 21:54:38 +00:00
|
|
|
} catch (Exception $e) {
|
2011-08-27 14:34:08 +00:00
|
|
|
if ($type === null) {
|
|
|
|
return array(array(), array());
|
|
|
|
}
|
2011-08-27 02:38:34 +00:00
|
|
|
return array();
|
|
|
|
}
|
2012-02-17 19:46:52 +00:00
|
|
|
|
2011-12-21 21:25:24 +00:00
|
|
|
foreach ($iterator as $itemPath => $fsIterator) {
|
|
|
|
if ($skipHidden) {
|
|
|
|
$subPathName = $fsIterator->getSubPathname();
|
2013-02-12 02:38:08 +00:00
|
|
|
if ($subPathName{0} === '.' || strpos($subPathName, DS . '.') !== false) {
|
2011-12-21 21:25:24 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$item = $fsIterator->current();
|
|
|
|
if (!empty($exceptions) && isset($exceptions[$item->getFilename()])) {
|
2011-08-27 02:38:34 +00:00
|
|
|
continue;
|
|
|
|
}
|
2011-12-21 21:25:24 +00:00
|
|
|
|
2011-08-27 02:38:34 +00:00
|
|
|
if ($item->isFile()) {
|
2011-12-21 21:25:24 +00:00
|
|
|
$files[] = $itemPath;
|
2012-01-19 21:54:38 +00:00
|
|
|
} elseif ($item->isDir() && !$item->isDot()) {
|
2011-12-21 21:25:24 +00:00
|
|
|
$directories[] = $itemPath;
|
2011-08-27 02:38:34 +00:00
|
|
|
}
|
|
|
|
}
|
2008-05-30 11:40:08 +00:00
|
|
|
if ($type === null) {
|
2011-08-27 02:38:34 +00:00
|
|
|
return array($directories, $files);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
if ($type === 'dir') {
|
|
|
|
return $directories;
|
|
|
|
}
|
2011-08-27 02:38:34 +00:00
|
|
|
return $files;
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
2014-10-28 01:32:28 +00:00
|
|
|
* Create a directory structure recursively.
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2014-10-28 01:32:28 +00:00
|
|
|
* Can be used to create deep path structures like `/foo/bar/baz/shoe/horn`
|
|
|
|
*
|
|
|
|
* @param string $pathname The directory structure to create. Either an absolute or relative
|
|
|
|
* path. If the path is relative and exists in the process' cwd it will not be created.
|
|
|
|
* Otherwise relative paths will be prefixed with the current pwd().
|
2014-07-03 13:36:42 +00:00
|
|
|
* @param int $mode octal value 0755
|
|
|
|
* @return bool Returns TRUE on success, FALSE on failure
|
2017-06-10 22:15:34 +00:00
|
|
|
* @link https://book.cakephp.org/2.0/en/core-utility-libraries/file-folder.html#Folder::create
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-04-05 03:19:38 +00:00
|
|
|
public function create($pathname, $mode = false) {
|
2008-05-30 11:40:08 +00:00
|
|
|
if (is_dir($pathname) || empty($pathname)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-07-21 08:22:53 +00:00
|
|
|
if (!static::isAbsolute($pathname)) {
|
|
|
|
$pathname = static::addPathElement($this->pwd(), $pathname);
|
2014-10-28 01:32:28 +00:00
|
|
|
}
|
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
if (!$mode) {
|
|
|
|
$mode = $this->mode;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (is_file($pathname)) {
|
2011-08-20 04:43:34 +00:00
|
|
|
$this->_errors[] = __d('cake_dev', '%s is a file', $pathname);
|
2008-11-07 01:08:18 +00:00
|
|
|
return false;
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2010-08-10 02:56:41 +00:00
|
|
|
$pathname = rtrim($pathname, DS);
|
2008-05-30 11:40:08 +00:00
|
|
|
$nextPathname = substr($pathname, 0, strrpos($pathname, DS));
|
|
|
|
|
|
|
|
if ($this->create($nextPathname, $mode)) {
|
|
|
|
if (!file_exists($pathname)) {
|
2008-09-17 21:54:36 +00:00
|
|
|
$old = umask(0);
|
|
|
|
if (mkdir($pathname, $mode)) {
|
|
|
|
umask($old);
|
2011-08-20 04:43:34 +00:00
|
|
|
$this->_messages[] = __d('cake_dev', '%s created', $pathname);
|
2008-05-30 11:40:08 +00:00
|
|
|
return true;
|
|
|
|
}
|
2013-07-02 23:14:41 +00:00
|
|
|
umask($old);
|
|
|
|
$this->_errors[] = __d('cake_dev', '%s NOT created', $pathname);
|
|
|
|
return false;
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
}
|
2010-02-20 16:25:46 +00:00
|
|
|
return false;
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
2010-01-25 14:42:17 +00:00
|
|
|
* Returns the size in bytes of this Folder and its contents.
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2014-07-03 13:36:42 +00:00
|
|
|
* @return int size in bytes of current folder
|
2017-06-10 22:15:34 +00:00
|
|
|
* @link https://book.cakephp.org/2.0/en/core-utility-libraries/file-folder.html#Folder::dirsize
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-04-05 03:19:38 +00:00
|
|
|
public function dirsize() {
|
2008-05-30 11:40:08 +00:00
|
|
|
$size = 0;
|
2008-12-18 00:17:19 +00:00
|
|
|
$directory = Folder::slashTerm($this->path);
|
2008-05-30 11:40:08 +00:00
|
|
|
$stack = array($directory);
|
|
|
|
$count = count($stack);
|
|
|
|
for ($i = 0, $j = $count; $i < $j; ++$i) {
|
|
|
|
if (is_file($stack[$i])) {
|
|
|
|
$size += filesize($stack[$i]);
|
|
|
|
} elseif (is_dir($stack[$i])) {
|
|
|
|
$dir = dir($stack[$i]);
|
|
|
|
if ($dir) {
|
|
|
|
while (false !== ($entry = $dir->read())) {
|
2008-11-06 18:00:56 +00:00
|
|
|
if ($entry === '.' || $entry === '..') {
|
2008-05-30 11:40:08 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
$add = $stack[$i] . $entry;
|
|
|
|
|
|
|
|
if (is_dir($stack[$i] . $entry)) {
|
2008-12-18 00:17:19 +00:00
|
|
|
$add = Folder::slashTerm($add);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2008-11-06 18:00:56 +00:00
|
|
|
$stack[] = $add;
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
$dir->close();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$j = count($stack);
|
|
|
|
}
|
|
|
|
return $size;
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
2010-01-25 14:42:17 +00:00
|
|
|
* Recursively Remove directories if the system allows.
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
|
|
|
* @param string $path Path of directory to delete
|
2014-07-03 13:36:42 +00:00
|
|
|
* @return bool Success
|
2017-06-10 22:15:34 +00:00
|
|
|
* @link https://book.cakephp.org/2.0/en/core-utility-libraries/file-folder.html#Folder::delete
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-04-05 03:19:38 +00:00
|
|
|
public function delete($path = null) {
|
2008-05-30 11:40:08 +00:00
|
|
|
if (!$path) {
|
|
|
|
$path = $this->pwd();
|
|
|
|
}
|
2009-08-02 22:42:27 +00:00
|
|
|
if (!$path) {
|
2014-11-08 19:07:47 +00:00
|
|
|
return false;
|
2009-08-02 22:42:27 +00:00
|
|
|
}
|
2008-12-18 00:17:19 +00:00
|
|
|
$path = Folder::slashTerm($path);
|
2011-12-14 20:29:30 +00:00
|
|
|
if (is_dir($path)) {
|
2011-12-20 07:51:08 +00:00
|
|
|
try {
|
2012-01-19 21:54:38 +00:00
|
|
|
$directory = new RecursiveDirectoryIterator($path, RecursiveDirectoryIterator::CURRENT_AS_SELF);
|
2011-12-20 07:51:08 +00:00
|
|
|
$iterator = new RecursiveIteratorIterator($directory, RecursiveIteratorIterator::CHILD_FIRST);
|
2012-01-19 21:54:38 +00:00
|
|
|
} catch (Exception $e) {
|
2011-12-20 07:51:08 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($iterator as $item) {
|
|
|
|
$filePath = $item->getPathname();
|
|
|
|
if ($item->isFile() || $item->isLink()) {
|
2012-11-14 09:00:15 +00:00
|
|
|
//@codingStandardsIgnoreStart
|
2011-12-14 20:29:30 +00:00
|
|
|
if (@unlink($filePath)) {
|
2012-11-14 09:00:15 +00:00
|
|
|
//@codingStandardsIgnoreEnd
|
2011-12-14 20:29:30 +00:00
|
|
|
$this->_messages[] = __d('cake_dev', '%s removed', $filePath);
|
|
|
|
} else {
|
|
|
|
$this->_errors[] = __d('cake_dev', '%s NOT removed', $filePath);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2012-01-19 21:54:38 +00:00
|
|
|
} elseif ($item->isDir() && !$item->isDot()) {
|
2012-11-14 09:00:15 +00:00
|
|
|
//@codingStandardsIgnoreStart
|
2011-12-14 20:29:30 +00:00
|
|
|
if (@rmdir($filePath)) {
|
2012-11-14 09:00:15 +00:00
|
|
|
//@codingStandardsIgnoreEnd
|
2011-12-14 20:29:30 +00:00
|
|
|
$this->_messages[] = __d('cake_dev', '%s removed', $filePath);
|
|
|
|
} else {
|
|
|
|
$this->_errors[] = __d('cake_dev', '%s NOT removed', $filePath);
|
2008-11-07 01:08:18 +00:00
|
|
|
return false;
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2011-12-14 20:29:30 +00:00
|
|
|
|
|
|
|
$path = rtrim($path, DS);
|
2012-11-14 09:00:15 +00:00
|
|
|
//@codingStandardsIgnoreStart
|
2011-12-14 20:29:30 +00:00
|
|
|
if (@rmdir($path)) {
|
2012-11-14 09:00:15 +00:00
|
|
|
//@codingStandardsIgnoreEnd
|
2011-12-14 20:29:30 +00:00
|
|
|
$this->_messages[] = __d('cake_dev', '%s removed', $path);
|
|
|
|
} else {
|
2011-08-20 04:43:34 +00:00
|
|
|
$this->_errors[] = __d('cake_dev', '%s NOT removed', $path);
|
2008-05-30 11:40:08 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Recursive directory copy.
|
|
|
|
*
|
2010-01-25 14:42:17 +00:00
|
|
|
* ### Options
|
|
|
|
*
|
|
|
|
* - `to` The directory to copy to.
|
|
|
|
* - `from` The directory to copy from, this will cause a cd() to occur, changing the results of pwd().
|
2014-09-10 15:57:26 +00:00
|
|
|
* - `mode` The mode to copy the files/directories with as integer, e.g. 0775.
|
2010-01-25 14:42:17 +00:00
|
|
|
* - `skip` Files/directories to skip.
|
2012-09-10 08:23:52 +00:00
|
|
|
* - `scheme` Folder::MERGE, Folder::OVERWRITE, Folder::SKIP
|
2010-01-25 14:42:17 +00:00
|
|
|
*
|
2012-05-13 00:43:31 +00:00
|
|
|
* @param array|string $options Either an array of options (see above) or a string of the destination directory.
|
2014-09-10 15:56:02 +00:00
|
|
|
* @return bool Success.
|
2017-06-10 22:15:34 +00:00
|
|
|
* @link https://book.cakephp.org/2.0/en/core-utility-libraries/file-folder.html#Folder::copy
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2012-11-22 13:28:09 +00:00
|
|
|
public function copy($options) {
|
2009-08-02 22:42:27 +00:00
|
|
|
if (!$this->pwd()) {
|
|
|
|
return false;
|
|
|
|
}
|
2008-05-30 11:40:08 +00:00
|
|
|
$to = null;
|
|
|
|
if (is_string($options)) {
|
|
|
|
$to = $options;
|
|
|
|
$options = array();
|
|
|
|
}
|
2014-09-10 01:33:14 +00:00
|
|
|
$options += array(
|
|
|
|
'to' => $to,
|
|
|
|
'from' => $this->path,
|
|
|
|
'mode' => $this->mode,
|
|
|
|
'skip' => array(),
|
|
|
|
'scheme' => Folder::MERGE
|
|
|
|
);
|
2008-05-30 11:40:08 +00:00
|
|
|
|
|
|
|
$fromDir = $options['from'];
|
|
|
|
$toDir = $options['to'];
|
|
|
|
$mode = $options['mode'];
|
|
|
|
|
|
|
|
if (!$this->cd($fromDir)) {
|
2011-08-20 04:43:34 +00:00
|
|
|
$this->_errors[] = __d('cake_dev', '%s not found', $fromDir);
|
2008-05-30 11:40:08 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!is_dir($toDir)) {
|
2009-09-01 02:47:37 +00:00
|
|
|
$this->create($toDir, $mode);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!is_writable($toDir)) {
|
2011-08-20 04:43:34 +00:00
|
|
|
$this->_errors[] = __d('cake_dev', '%s not writable', $toDir);
|
2008-05-30 11:40:08 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2008-11-06 18:00:56 +00:00
|
|
|
$exceptions = array_merge(array('.', '..', '.svn'), $options['skip']);
|
2012-11-14 09:00:15 +00:00
|
|
|
//@codingStandardsIgnoreStart
|
2008-05-30 11:40:08 +00:00
|
|
|
if ($handle = @opendir($fromDir)) {
|
2012-11-14 09:00:15 +00:00
|
|
|
//@codingStandardsIgnoreEnd
|
2012-09-10 08:23:52 +00:00
|
|
|
while (($item = readdir($handle)) !== false) {
|
|
|
|
$to = Folder::addPathElement($toDir, $item);
|
|
|
|
if (($options['scheme'] != Folder::SKIP || !is_dir($to)) && !in_array($item, $exceptions)) {
|
2012-09-14 01:05:10 +00:00
|
|
|
$from = Folder::addPathElement($fromDir, $item);
|
2014-09-06 22:32:45 +00:00
|
|
|
if (is_file($from) && (!is_file($to) || $options['scheme'] != Folder::SKIP)) {
|
2008-05-30 11:40:08 +00:00
|
|
|
if (copy($from, $to)) {
|
|
|
|
chmod($to, intval($mode, 8));
|
|
|
|
touch($to, filemtime($from));
|
2011-08-20 04:43:34 +00:00
|
|
|
$this->_messages[] = __d('cake_dev', '%s copied to %s', $from, $to);
|
2008-05-30 11:40:08 +00:00
|
|
|
} else {
|
2011-08-20 04:43:34 +00:00
|
|
|
$this->_errors[] = __d('cake_dev', '%s NOT copied to %s', $from, $to);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
}
|
2012-09-14 01:05:10 +00:00
|
|
|
|
2014-04-29 12:19:33 +00:00
|
|
|
if (is_dir($from) && file_exists($to) && $options['scheme'] === Folder::OVERWRITE) {
|
2012-09-10 08:23:52 +00:00
|
|
|
$this->delete($to);
|
|
|
|
}
|
2008-05-30 11:40:08 +00:00
|
|
|
|
|
|
|
if (is_dir($from) && !file_exists($to)) {
|
2008-09-17 21:54:36 +00:00
|
|
|
$old = umask(0);
|
|
|
|
if (mkdir($to, $mode)) {
|
|
|
|
umask($old);
|
|
|
|
$old = umask(0);
|
|
|
|
chmod($to, $mode);
|
|
|
|
umask($old);
|
2011-08-20 04:43:34 +00:00
|
|
|
$this->_messages[] = __d('cake_dev', '%s created', $to);
|
2014-04-07 23:25:14 +00:00
|
|
|
$options = array('to' => $to, 'from' => $from) + $options;
|
2008-05-30 11:40:08 +00:00
|
|
|
$this->copy($options);
|
|
|
|
} else {
|
2011-08-20 04:43:34 +00:00
|
|
|
$this->_errors[] = __d('cake_dev', '%s not created', $to);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2014-04-29 12:19:33 +00:00
|
|
|
} elseif (is_dir($from) && $options['scheme'] === Folder::MERGE) {
|
2014-04-07 23:25:14 +00:00
|
|
|
$options = array('to' => $to, 'from' => $from) + $options;
|
2012-09-10 08:23:52 +00:00
|
|
|
$this->copy($options);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
closedir($handle);
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2011-08-20 04:43:34 +00:00
|
|
|
if (!empty($this->_errors)) {
|
2008-05-30 11:40:08 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Recursive directory move.
|
|
|
|
*
|
2010-01-25 14:42:17 +00:00
|
|
|
* ### Options
|
|
|
|
*
|
|
|
|
* - `to` The directory to copy to.
|
|
|
|
* - `from` The directory to copy from, this will cause a cd() to occur, changing the results of pwd().
|
|
|
|
* - `chmod` The mode to copy the files/directories with.
|
|
|
|
* - `skip` Files/directories to skip.
|
2012-09-10 08:23:52 +00:00
|
|
|
* - `scheme` Folder::MERGE, Folder::OVERWRITE, Folder::SKIP
|
2010-01-25 14:42:17 +00:00
|
|
|
*
|
2012-09-10 08:23:52 +00:00
|
|
|
* @param array $options (to, from, chmod, skip, scheme)
|
2014-07-03 13:36:42 +00:00
|
|
|
* @return bool Success
|
2017-06-10 22:15:34 +00:00
|
|
|
* @link https://book.cakephp.org/2.0/en/core-utility-libraries/file-folder.html#Folder::move
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-04-05 03:19:38 +00:00
|
|
|
public function move($options) {
|
2008-05-30 11:40:08 +00:00
|
|
|
$to = null;
|
|
|
|
if (is_string($options)) {
|
|
|
|
$to = $options;
|
|
|
|
$options = (array)$options;
|
|
|
|
}
|
2014-04-07 23:25:14 +00:00
|
|
|
$options += array('to' => $to, 'from' => $this->path, 'mode' => $this->mode, 'skip' => array());
|
2008-05-30 11:40:08 +00:00
|
|
|
|
|
|
|
if ($this->copy($options)) {
|
|
|
|
if ($this->delete($options['from'])) {
|
2010-05-31 02:57:28 +00:00
|
|
|
return (bool)$this->cd($options['to']);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* get messages from latest method
|
|
|
|
*
|
2014-07-03 13:36:42 +00:00
|
|
|
* @param bool $reset Reset message stack after reading
|
2008-05-30 11:40:08 +00:00
|
|
|
* @return array
|
2017-06-10 22:15:34 +00:00
|
|
|
* @link https://book.cakephp.org/2.0/en/core-utility-libraries/file-folder.html#Folder::messages
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2012-10-01 11:42:14 +00:00
|
|
|
public function messages($reset = true) {
|
|
|
|
$messages = $this->_messages;
|
|
|
|
if ($reset) {
|
|
|
|
$this->_messages = array();
|
|
|
|
}
|
|
|
|
return $messages;
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* get error from latest method
|
|
|
|
*
|
2014-07-03 13:36:42 +00:00
|
|
|
* @param bool $reset Reset error stack after reading
|
2008-05-30 11:40:08 +00:00
|
|
|
* @return array
|
2017-06-10 22:15:34 +00:00
|
|
|
* @link https://book.cakephp.org/2.0/en/core-utility-libraries/file-folder.html#Folder::errors
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2012-10-01 11:42:14 +00:00
|
|
|
public function errors($reset = true) {
|
|
|
|
$errors = $this->_errors;
|
|
|
|
if ($reset) {
|
|
|
|
$this->_errors = array();
|
|
|
|
}
|
|
|
|
return $errors;
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Get the real path (taking ".." and such into account)
|
|
|
|
*
|
|
|
|
* @param string $path Path to resolve
|
|
|
|
* @return string The resolved path
|
2017-06-10 22:15:34 +00:00
|
|
|
* @link https://book.cakephp.org/2.0/en/core-utility-libraries/file-folder.html#Folder::realpath
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2011-05-28 20:38:46 +00:00
|
|
|
public function realpath($path) {
|
2008-05-30 11:40:08 +00:00
|
|
|
if (strpos($path, '..') === false) {
|
2008-12-18 00:17:19 +00:00
|
|
|
if (!Folder::isAbsolute($path)) {
|
|
|
|
$path = Folder::addPathElement($this->path, $path);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
return $path;
|
|
|
|
}
|
2015-12-24 21:19:41 +00:00
|
|
|
$path = str_replace('/', DS, trim($path));
|
2008-05-30 11:40:08 +00:00
|
|
|
$parts = explode(DS, $path);
|
|
|
|
$newparts = array();
|
2008-07-30 20:34:01 +00:00
|
|
|
$newpath = '';
|
2008-11-06 18:00:56 +00:00
|
|
|
if ($path[0] === DS) {
|
2008-07-30 20:34:01 +00:00
|
|
|
$newpath = DS;
|
|
|
|
}
|
2008-05-30 11:40:08 +00:00
|
|
|
|
2012-03-03 22:31:47 +00:00
|
|
|
while (($part = array_shift($parts)) !== null) {
|
2008-11-06 18:00:56 +00:00
|
|
|
if ($part === '.' || $part === '') {
|
2008-05-30 11:40:08 +00:00
|
|
|
continue;
|
|
|
|
}
|
2008-11-06 18:00:56 +00:00
|
|
|
if ($part === '..') {
|
2009-11-21 20:14:21 +00:00
|
|
|
if (!empty($newparts)) {
|
2008-05-30 11:40:08 +00:00
|
|
|
array_pop($newparts);
|
|
|
|
continue;
|
|
|
|
}
|
2013-07-02 23:14:41 +00:00
|
|
|
return false;
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
$newparts[] = $part;
|
|
|
|
}
|
|
|
|
$newpath .= implode(DS, $newparts);
|
|
|
|
|
2008-12-18 00:17:19 +00:00
|
|
|
return Folder::slashTerm($newpath);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Returns true if given $path ends in a slash (i.e. is slash-terminated).
|
|
|
|
*
|
|
|
|
* @param string $path Path to check
|
2014-07-03 13:36:42 +00:00
|
|
|
* @return bool true if path ends with slash, false otherwise
|
2017-06-10 22:15:34 +00:00
|
|
|
* @link https://book.cakephp.org/2.0/en/core-utility-libraries/file-folder.html#Folder::isSlashTerm
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-12-13 01:55:33 +00:00
|
|
|
public static function isSlashTerm($path) {
|
2008-12-18 00:22:57 +00:00
|
|
|
$lastChar = $path[strlen($path) - 1];
|
|
|
|
return $lastChar === '/' || $lastChar === '\\';
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2012-03-03 22:31:47 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|