2008-05-30 11:40:08 +00:00
|
|
|
<?php
|
|
|
|
/**
|
2009-11-06 06:46:59 +00:00
|
|
|
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
|
2013-02-08 11:59:49 +00:00
|
|
|
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
|
|
|
* Licensed under The MIT License
|
|
|
|
* Redistributions of files must retain the above copyright notice.
|
|
|
|
*
|
2013-02-08 11:59:49 +00:00
|
|
|
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
|
2009-11-06 06:00:11 +00:00
|
|
|
* @link http://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
|
2009-11-06 06:51:51 +00:00
|
|
|
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
|
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
|
|
|
*
|
|
|
|
* @constant MERGE
|
|
|
|
*/
|
|
|
|
const MERGE = 'merge';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Overwrite scheme for Folder::copy
|
|
|
|
* subfolders with the same name will be replaced
|
|
|
|
*
|
|
|
|
* @constant OVERWRITE
|
|
|
|
*/
|
|
|
|
const OVERWRITE = 'overwrite';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Skip scheme for Folder::copy
|
|
|
|
* if a subfolder with the same name exists it will be skipped
|
|
|
|
*
|
|
|
|
* @constant SKIP
|
|
|
|
*/
|
|
|
|
const SKIP = 'skip';
|
2012-09-14 01:05:10 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Path to Folder.
|
|
|
|
*
|
|
|
|
* @var string
|
2011-11-18 19:05:30 +00:00
|
|
|
* @link http://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
|
|
|
*
|
|
|
|
* @var boolean
|
2011-11-18 19:05:30 +00:00
|
|
|
* @link http://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
|
|
|
/**
|
2010-01-25 14:42:17 +00:00
|
|
|
* Mode to be used on create. Does nothing on windows platforms.
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2010-01-25 14:42:17 +00:00
|
|
|
* @var integer
|
2011-11-18 19:05:30 +00:00
|
|
|
* http://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
|
|
|
|
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
|
|
|
|
* @param boolean $create Create folder if not found
|
2012-05-13 00:43:31 +00:00
|
|
|
* @param string|boolean $mode Mode (CHMOD) to apply to created folder, false to ignore
|
2011-11-15 06:40:02 +00:00
|
|
|
* @link http://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
|
2011-11-15 06:40:02 +00:00
|
|
|
* @link http://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
|
2011-11-15 06:40:02 +00:00
|
|
|
* @link http://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
|
|
|
*
|
2010-01-25 14:42:17 +00:00
|
|
|
* @param boolean $sort Whether you want the results sorted, set this and the sort property
|
|
|
|
* to false to get unsorted results.
|
2012-05-13 00:43:31 +00:00
|
|
|
* @param array|boolean $exceptions Either an array or boolean true will not grab dot files
|
2008-11-05 02:18:49 +00:00
|
|
|
* @param boolean $fullPath True returns the full path
|
|
|
|
* @return mixed Contents of current directory as an array, an empty array on failure
|
2011-11-15 06:40:02 +00:00
|
|
|
* @link http://book.cakephp.org/2.0/en/core-utility-libraries/file-folder.html#Folder::read
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-04-05 03:19:38 +00:00
|
|
|
public function read($sort = true, $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);
|
|
|
|
}
|
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()) {
|
|
|
|
$dirs[] = $name;
|
2008-11-05 02:18:49 +00:00
|
|
|
} else {
|
2011-08-27 02:10:29 +00:00
|
|
|
$files[] = $name;
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
}
|
2008-11-05 02:18:49 +00:00
|
|
|
if ($sort || $this->sort) {
|
|
|
|
sort($dirs);
|
|
|
|
sort($files);
|
|
|
|
}
|
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: .*)
|
2010-01-25 14:42:17 +00:00
|
|
|
* @param boolean $sort Whether results should be sorted.
|
2008-05-30 11:40:08 +00:00
|
|
|
* @return array Files that match given pattern
|
2011-11-15 06:40:02 +00:00
|
|
|
* @link http://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: .*)
|
2010-01-25 14:42:17 +00:00
|
|
|
* @param boolean $sort Whether results should be sorted.
|
2008-05-30 11:40:08 +00:00
|
|
|
* @return array Files matching $pattern
|
2011-11-15 06:40:02 +00:00
|
|
|
* @link http://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
|
2010-01-25 14:42:17 +00:00
|
|
|
* @param boolean $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
|
|
|
|
* @return boolean true if windows path, false otherwise
|
2011-11-15 06:40:02 +00:00
|
|
|
* @link http://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) {
|
2010-11-20 18:26:30 +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
|
2011-08-01 02:57:17 +00:00
|
|
|
* @return boolean true if path is absolute.
|
2011-11-15 06:40:02 +00:00
|
|
|
* @link http://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) {
|
2010-11-20 18:26:30 +00:00
|
|
|
return !empty($path) && ($path[0] === '/' || 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 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 "/")
|
2011-11-15 06:40:02 +00:00
|
|
|
* @link http://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 "/")
|
2011-11-15 06:40:02 +00:00
|
|
|
* @link http://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
|
2011-11-15 06:40:02 +00:00
|
|
|
* @link http://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
|
|
|
|
* @param string $element Element to and at end of path
|
|
|
|
* @return string Combined path
|
2011-11-15 06:40:02 +00:00
|
|
|
* @link http://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) {
|
2009-10-28 01:06:58 +00:00
|
|
|
return rtrim($path, DS) . 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
|
|
|
/**
|
|
|
|
* Returns true if the File is in a given CakePath.
|
|
|
|
*
|
2010-01-25 14:42:17 +00:00
|
|
|
* @param string $path The path to check.
|
2011-08-01 02:57:17 +00:00
|
|
|
* @return boolean
|
2011-11-15 06:40:02 +00:00
|
|
|
* @link http://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
|
|
|
/**
|
|
|
|
* Returns true if the File is in given path.
|
|
|
|
*
|
2010-01-25 14:42:17 +00:00
|
|
|
* @param string $path The path to check that the current pwd() resides with in.
|
2012-03-27 18:55:13 +00:00
|
|
|
* @param boolean $reverse Reverse the search, check that pwd() resides within $path.
|
2011-08-01 02:57:17 +00:00
|
|
|
* @return boolean
|
2011-11-15 06:40:02 +00:00
|
|
|
* @link http://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) {
|
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) {
|
|
|
|
$return = preg_match('/^(.*)' . preg_quote($dir, '/') . '(.*)/', $current);
|
|
|
|
} else {
|
|
|
|
$return = preg_match('/^(.*)' . preg_quote($current, '/') . '(.*)/', $dir);
|
|
|
|
}
|
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
|
|
|
*
|
|
|
|
* @param string $path The path to chmod
|
|
|
|
* @param integer $mode octal value 0755
|
2010-01-25 14:42:17 +00:00
|
|
|
* @param boolean $recursive chmod recursively, set to false to only change the current directory.
|
2008-05-30 11:40:08 +00:00
|
|
|
* @param array $exceptions array of files, directories to skip
|
|
|
|
* @return boolean Returns TRUE on success, FALSE on failure
|
2011-11-15 06:40:02 +00:00
|
|
|
* @link http://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
|
2012-05-13 00:43:31 +00:00
|
|
|
* @param array|boolean $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
|
2011-11-15 06:40:02 +00:00
|
|
|
* @link http://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();
|
|
|
|
if ($subPathName{0} == '.' || strpos($subPathName, DS . '.') !== false) {
|
|
|
|
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
|
|
|
/**
|
2010-01-25 14:42:17 +00:00
|
|
|
* Create a directory structure recursively. Can be used to create
|
|
|
|
* deep path structures like `/foo/bar/baz/shoe/horn`
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
|
|
|
* @param string $pathname The directory structure to create
|
|
|
|
* @param integer $mode octal value 0755
|
|
|
|
* @return boolean Returns TRUE on success, FALSE on failure
|
2011-11-15 06:40:02 +00:00
|
|
|
* @link http://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;
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
} else {
|
2008-09-17 21:54:36 +00:00
|
|
|
umask($old);
|
2011-08-20 04:43:34 +00:00
|
|
|
$this->_errors[] = __d('cake_dev', '%s NOT created', $pathname);
|
2008-05-30 11:40:08 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
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
|
|
|
*
|
2011-08-01 02:57:17 +00:00
|
|
|
* @return integer size in bytes of current folder
|
2011-11-15 06:40:02 +00:00
|
|
|
* @link http://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
|
|
|
|
* @return boolean Success
|
2011-11-15 06:40:02 +00:00
|
|
|
* @link http://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) {
|
|
|
|
return null;
|
|
|
|
}
|
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().
|
2011-06-25 06:32:59 +00:00
|
|
|
* - `mode` The mode to copy the files/directories with.
|
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.
|
2011-08-01 02:57:17 +00:00
|
|
|
* @return boolean Success
|
2011-11-15 06:40:02 +00:00
|
|
|
* @link http://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();
|
|
|
|
}
|
2012-09-10 08:23:52 +00:00
|
|
|
$options = array_merge(array('to' => $to, 'from' => $this->path, 'mode' => $this->mode, 'skip' => array(), 'scheme' => Folder::MERGE), $options);
|
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);
|
2008-05-30 11:40:08 +00:00
|
|
|
if (is_file($from)) {
|
|
|
|
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
|
|
|
|
2012-09-10 08:23:52 +00:00
|
|
|
if (is_dir($from) && file_exists($to) && $options['scheme'] == Folder::OVERWRITE) {
|
|
|
|
$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);
|
2011-12-01 07:21:31 +00:00
|
|
|
$options = array_merge($options, array('to' => $to, 'from' => $from));
|
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
|
|
|
}
|
2012-09-10 08:23:52 +00:00
|
|
|
} elseif (is_dir($from) && $options['scheme'] == Folder::MERGE) {
|
|
|
|
$options = array_merge($options, array('to' => $to, 'from' => $from));
|
|
|
|
$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)
|
2008-05-30 11:40:08 +00:00
|
|
|
* @return boolean Success
|
2011-11-15 06:40:02 +00:00
|
|
|
* @link http://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;
|
|
|
|
}
|
2010-05-31 02:57:28 +00:00
|
|
|
$options = array_merge(
|
2011-03-19 17:23:01 +00:00
|
|
|
array('to' => $to, 'from' => $this->path, 'mode' => $this->mode, 'skip' => array()),
|
2010-05-31 02:57:28 +00:00
|
|
|
$options
|
|
|
|
);
|
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
|
|
|
|
*
|
2012-10-01 11:42:14 +00:00
|
|
|
* @param boolean $reset Reset message stack after reading
|
2008-05-30 11:40:08 +00:00
|
|
|
* @return array
|
2011-11-15 06:40:02 +00:00
|
|
|
* @link http://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
|
|
|
|
*
|
2012-10-01 11:42:14 +00:00
|
|
|
* @param boolean $reset Reset error stack after reading
|
2008-05-30 11:40:08 +00:00
|
|
|
* @return array
|
2011-11-15 06:40:02 +00:00
|
|
|
* @link http://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
|
2011-11-15 06:40:02 +00:00
|
|
|
* @link http://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
|
|
|
$path = str_replace('/', DS, trim($path));
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
$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;
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$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
|
|
|
|
* @return boolean true if path ends with slash, false otherwise
|
2011-11-15 06:40:02 +00:00
|
|
|
* @link http://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
|
|
|
}
|