folder.php

Go to the documentation of this file.
00001 <?php
00002 /* SVN FILE: $Id: folder_8php-source.html 675 2008-12-26 00:27:14Z gwoo $ */
00003 /**
00004  * Convenience class for handling directories.
00005  *
00006  * PHP versions 4 and 5
00007  *
00008  * CakePHP(tm) :  Rapid Development Framework <http://www.cakephp.org/>
00009  * Copyright 2005-2008, Cake Software Foundation, Inc.
00010  *                              1785 E. Sahara Avenue, Suite 490-204
00011  *                              Las Vegas, Nevada 89104
00012  *
00013  * Licensed under The MIT License
00014  * Redistributions of files must retain the above copyright notice.
00015  *
00016  * @filesource
00017  * @copyright       Copyright 2005-2008, Cake Software Foundation, Inc.
00018  * @link                http://www.cakefoundation.org/projects/info/cakephp CakePHP(tm) Project
00019  * @package         cake
00020  * @subpackage      cake.cake.libs
00021  * @since           CakePHP(tm) v 0.2.9
00022  * @version         $Revision: 675 $
00023  * @modifiedby      $LastChangedBy: gwoo $
00024  * @lastmodified    $Date: 2008-12-25 16:27:14 -0800 (Thu, 25 Dec 2008) $
00025  * @license         http://www.opensource.org/licenses/mit-license.php The MIT License
00026  */
00027 /**
00028  * Included libraries.
00029  *
00030  */
00031     if (!class_exists('Object')) {
00032          uses ('object');
00033     }
00034 /**
00035  * Folder structure browser, lists folders and files.
00036  *
00037  * Long description for class
00038  *
00039  * @package     cake
00040  * @subpackage  cake.cake.libs
00041  */
00042 class Folder extends Object{
00043 /**
00044  * Path to Folder.
00045  *
00046  * @var string
00047  */
00048     var $path = null;
00049 /**
00050  * Sortedness.
00051  *
00052  * @var boolean
00053  */
00054     var $sort = false;
00055 /**
00056  * Constructor.
00057  *
00058  * @param string $path
00059  * @param boolean $path
00060  */
00061     function __construct($path = false, $create = false, $mode = false) {
00062         parent::__construct();
00063         if (empty($path)) {
00064             $path = getcwd();
00065         }
00066 
00067         if (!file_exists($path) && $create == true) {
00068             $this->mkdirr($path, $mode);
00069         }
00070         $this->cd($path);
00071     }
00072 /**
00073  * Return current path.
00074  *
00075  * @return string Current path
00076  */
00077     function pwd() {
00078         return $this->path;
00079     }
00080 /**
00081  * Change directory to $desired_path.
00082  *
00083  * @param string $desired_path Path to the directory to change to
00084  * @return string The new path. Returns false on failure
00085  */
00086     function cd($desiredPath) {
00087         $desiredPath = realpath($desiredPath);
00088         $newPath = $this->isAbsolute($desiredPath) ? $desiredPath : $this->addPathElement($this->path, $desiredPath);
00089         $isDir = (is_dir($newPath) && file_exists($newPath)) ? $this->path = $newPath : false;
00090         return $isDir;
00091      }
00092 /**
00093  * Returns an array of the contents of the current directory, or false on failure.
00094  * The returned array holds two arrays: one of dirs and one of files.
00095  *
00096  * @param boolean $sort
00097  * @param boolean $noDotFiles
00098  * @return array
00099  */
00100     function ls($sort = true, $noDotFiles = false) {
00101         $dirs = $files = array();
00102         $dir = opendir($this->path);
00103         if ($dir) {
00104             while (false !== ($n = readdir($dir))) {
00105                 if ((!preg_match('#^\.+$#', $n) && $noDotFiles == false) || ($noDotFiles == true && !preg_match('#^\.(.*)$#', $n))) {
00106                     if (is_dir($this->addPathElement($this->path, $n))) {
00107                         $dirs[] = $n;
00108                     } else {
00109                         $files[] = $n;
00110                     }
00111                 }
00112             }
00113 
00114             if ($sort || $this->sort) {
00115                 sort ($dirs);
00116                 sort ($files);
00117             }
00118             closedir ($dir);
00119         }
00120         return array($dirs,$files);
00121     }
00122 /**
00123  * Returns an array of all matching files in current directory.
00124  *
00125  * @param string $pattern Preg_match pattern (Defaults to: .*)
00126  * @return array
00127  */
00128     function find($regexp_pattern = '.*') {
00129         $data = $this->ls();
00130 
00131         if (!is_array($data)) {
00132             return array();
00133         }
00134 
00135         list($dirs, $files) = $data;
00136         $found =  array();
00137 
00138         foreach ($files as $file) {
00139             if (preg_match("/^{$regexp_pattern}$/i", $file)) {
00140                 $found[] = $file;
00141             }
00142         }
00143         return $found;
00144     }
00145 /**
00146  * Returns an array of all matching files in and below current directory.
00147  *
00148  * @param string $pattern Preg_match pattern (Defaults to: .*)
00149  * @return array Files matching $pattern
00150  */
00151     function findRecursive($pattern = '.*') {
00152         $startsOn = $this->path;
00153         $out = $this->_findRecursive($pattern);
00154         $this->cd($startsOn);
00155         return $out;
00156     }
00157 /**
00158  * Private helper function for findRecursive.
00159  *
00160  * @param string $pattern
00161  * @return array Files matching pattern
00162  * @access private
00163  */
00164     function _findRecursive($pattern) {
00165         list($dirs, $files) = $this->ls();
00166 
00167         $found = array();
00168         foreach ($files as $file) {
00169             if (preg_match("/^{$pattern}$/i", $file)) {
00170                 $found[] = $this->addPathElement($this->path, $file);
00171             }
00172         }
00173         $start = $this->path;
00174         foreach ($dirs as $dir) {
00175             $this->cd($this->addPathElement($start, $dir));
00176             $found = array_merge($found, $this->findRecursive($pattern));
00177         }
00178         return $found;
00179     }
00180 /**
00181  * Returns true if given $path is a Windows path.
00182  *
00183  * @param string $path Path to check
00184  * @return boolean
00185  * @static
00186  */
00187     function isWindowsPath($path) {
00188         $match = preg_match('#^[A-Z]:\\\#i', $path) ? true : false;
00189         return $match;
00190     }
00191 /**
00192  * Returns true if given $path is an absolute path.
00193  *
00194  * @param string $path Path to check
00195  * @return boolean
00196  * @static
00197  */
00198     function isAbsolute($path) {
00199         $match = preg_match('#^\/#', $path) || preg_match('#^[A-Z]:\\\#i', $path);
00200         return $match;
00201     }
00202 /**
00203  * Returns true if given $path ends in a slash (i.e. is slash-terminated).
00204  *
00205  * @param string $path Path to check
00206  * @return boolean
00207  * @static
00208  */
00209     function isSlashTerm($path) {
00210         $match = preg_match('#[\\\/]$#', $path) ? true : false;
00211         return $match;
00212     }
00213 /**
00214  * Returns a correct set of slashes for given $path. (\\ for Windows paths and / for other paths.)
00215  *
00216  * @param string $path Path to check
00217  * @return string Set of slashes ("\\" or "/")
00218  * @static
00219  */
00220     function correctSlashFor($path) {
00221         return $this->isWindowsPath($path) ? '\\' : '/';
00222     }
00223 /**
00224  * Returns $path with added terminating slash (corrected for Windows or other OS).
00225  *
00226  * @param string $path Path to check
00227  * @return string
00228  * @static
00229  */
00230 function slashTerm($path) {
00231           return $path . ($this->isSlashTerm($path) ? null : $this->correctSlashFor($path));
00232      }
00233 /**
00234  * Returns $path with $element added, with correct slash in-between.
00235  *
00236  * @param string $path
00237  * @param string $element
00238  * @return string
00239  * @static
00240  */
00241     function addPathElement($path, $element) {
00242         return $this->slashTerm($path) . $element;
00243     }
00244 /**
00245  * Returns true if the File is in a given CakePath.
00246  *
00247  * @return boolean
00248  */
00249     function inCakePath($path = '') {
00250         $dir = substr($this->slashTerm(ROOT), 0, -1);
00251         $newdir = $this->slashTerm($dir . $path);
00252         return $this->inPath($newdir);
00253      }
00254 /**
00255  * Returns true if the File is in given path.
00256  *
00257  * @return boolean
00258  */
00259     function inPath($path = '') {
00260         $dir = substr($this->slashTerm($path), 0, -1);
00261         $return = preg_match('/^' . preg_quote($this->slashTerm($dir), '/') . '(.*)/', $this->slashTerm($this->pwd()));
00262         if ($return == 1) {
00263             return true;
00264         } else {
00265             return false;
00266         }
00267     }
00268 /**
00269  * Create a directory structure recursively.
00270  *
00271  * @param string $pathname The directory structure to create
00272  * @return bool Returns TRUE on success, FALSE on failure
00273  */
00274     function mkdirr($pathname, $mode = null) {
00275         if (is_dir($pathname) || empty($pathname)) {
00276             return true;
00277         }
00278 
00279         if (is_file($pathname)) {
00280             trigger_error('mkdirr() File exists', E_USER_WARNING);
00281             return false;
00282         }
00283         $nextPathname = substr($pathname, 0, strrpos($pathname, DIRECTORY_SEPARATOR));
00284 
00285         if ($this->mkdirr($nextPathname, $mode)) {
00286             if (!file_exists($pathname)) {
00287                 umask (0);
00288                 $mkdir = mkdir($pathname, $mode);
00289                 return $mkdir;
00290             }
00291         }
00292         return false;
00293     }
00294 /**
00295  * Returns the size in bytes of this Folder.
00296  *
00297  * @param string $directory Path to directory
00298  */
00299     function dirsize() {
00300         $size = 0;
00301         $directory = $this->slashTerm($this->path);
00302         $stack = array($directory);
00303         $count = count($stack);
00304         for ($i = 0, $j = $count; $i < $j; ++$i) {
00305             if (is_file($stack[$i])) {
00306                 $size += filesize($stack[$i]);
00307             } elseif (is_dir($stack[$i])) {
00308                 $dir = dir($stack[$i]);
00309 
00310                 while (false !== ($entry = $dir->read())) {
00311                     if ($entry == '.' || $entry == '..') {
00312                         continue;
00313                     }
00314                     $add = $stack[$i] . $entry;
00315 
00316                     if (is_dir($stack[$i] . $entry)) {
00317                         $add = $this->slashTerm($add);
00318                     }
00319                     $stack[ ]= $add;
00320                 }
00321                 $dir->close();
00322             }
00323             $j = count($stack);
00324         }
00325         return $size;
00326     }
00327 }
00328 ?>