[ Index ]
 

Code source de CakePHP 1.1.13.4450

Accédez au Source d'autres logiciels libresSoutenez Angelica Josefina !

title

Body

[fermer]

/cake/libs/ -> folder.php (source)

   1  <?php
   2  /* SVN FILE: $Id: folder.php 4409 2007-02-02 13:20:59Z phpnut $ */
   3  /**
   4   * Convenience class for handling directories.
   5   *
   6   * PHP versions 4 and 5
   7   *
   8   * CakePHP(tm) :  Rapid Development Framework <http://www.cakephp.org/>
   9   * Copyright 2005-2007, Cake Software Foundation, Inc.
  10   *                                1785 E. Sahara Avenue, Suite 490-204
  11   *                                Las Vegas, Nevada 89104
  12   *
  13   * Licensed under The MIT License
  14   * Redistributions of files must retain the above copyright notice.
  15   *
  16   * @filesource
  17   * @copyright        Copyright 2005-2007, Cake Software Foundation, Inc.
  18   * @link                http://www.cakefoundation.org/projects/info/cakephp CakePHP(tm) Project
  19   * @package            cake
  20   * @subpackage        cake.cake.libs
  21   * @since            CakePHP(tm) v 0.2.9
  22   * @version            $Revision: 4409 $
  23   * @modifiedby        $LastChangedBy: phpnut $
  24   * @lastmodified    $Date: 2007-02-02 07:20:59 -0600 (Fri, 02 Feb 2007) $
  25   * @license            http://www.opensource.org/licenses/mit-license.php The MIT License
  26   */
  27  /**
  28   * Included libraries.
  29   *
  30   */
  31      if (!class_exists('Object')) {
  32           uses ('object');
  33      }
  34  /**
  35   * Folder structure browser, lists folders and files.
  36   *
  37   * Long description for class
  38   *
  39   * @package        cake
  40   * @subpackage    cake.cake.libs
  41   */
  42  class Folder extends Object{
  43  /**
  44   * Path to Folder.
  45   *
  46   * @var string
  47   */
  48      var $path = null;
  49  /**
  50   * Sortedness.
  51   *
  52   * @var boolean
  53   */
  54      var $sort = false;
  55  /**
  56   * Constructor.
  57   *
  58   * @param string $path
  59   * @param boolean $path
  60   */
  61  	function __construct($path = false, $create = false, $mode = false) {
  62          parent::__construct();
  63          if (empty($path)) {
  64              $path = getcwd();
  65          }
  66  
  67          if (!file_exists($path) && $create == true) {
  68              $this->mkdirr($path, $mode);
  69          }
  70          $this->cd($path);
  71      }
  72  /**
  73   * Return current path.
  74   *
  75   * @return string Current path
  76   */
  77  	function pwd() {
  78          return $this->path;
  79      }
  80  /**
  81   * Change directory to $desired_path.
  82   *
  83   * @param string $desired_path Path to the directory to change to
  84   * @return string The new path. Returns false on failure
  85   */
  86      function cd($desiredPath) {
  87          $desiredPath = realpath($desiredPath);
  88          $newPath = $this->isAbsolute($desiredPath) ? $desiredPath : $this->addPathElement($this->path, $desiredPath);
  89          $isDir = (is_dir($newPath) && file_exists($newPath)) ? $this->path = $newPath : false;
  90          return $isDir;
  91       }
  92  /**
  93   * Returns an array of the contents of the current directory, or false on failure.
  94   * The returned array holds two arrays: one of dirs and one of files.
  95   *
  96   * @param boolean $sort
  97   * @param boolean $noDotFiles
  98   * @return array
  99   */
 100      function ls($sort = true, $noDotFiles = false) {
 101          $dirs = $files = array();
 102          $dir = opendir($this->path);
 103          if ($dir) {
 104              while(false !== ($n = readdir($dir))) {
 105                  if ((!preg_match('#^\.+$#', $n) && $noDotFiles == false) || ($noDotFiles == true && !preg_match('#^\.(.*)$#', $n))) {
 106                      if (is_dir($this->addPathElement($this->path, $n))) {
 107                          $dirs[] = $n;
 108                      } else {
 109                          $files[] = $n;
 110                      }
 111                  }
 112              }
 113  
 114              if ($sort || $this->sort) {
 115                  sort ($dirs);
 116                  sort ($files);
 117              }
 118              closedir ($dir);
 119          } 
 120          return array($dirs,$files);
 121      }
 122  /**
 123   * Returns an array of all matching files in current directory.
 124   *
 125   * @param string $pattern Preg_match pattern (Defaults to: .*)
 126   * @return array
 127   */
 128  	function find($regexp_pattern = '.*') {
 129          $data = $this->ls();
 130  
 131          if (!is_array($data)) {
 132              return array();
 133          }
 134  
 135          list($dirs, $files) = $data;
 136          $found =  array();
 137  
 138          foreach($files as $file) {
 139              if (preg_match("/^{$regexp_pattern}$/i", $file)) {
 140                  $found[] = $file;
 141              }
 142          }
 143          return $found;
 144      }
 145  /**
 146   * Returns an array of all matching files in and below current directory.
 147   *
 148   * @param string $pattern Preg_match pattern (Defaults to: .*)
 149   * @return array Files matching $pattern
 150   */
 151  	function findRecursive($pattern = '.*') {
 152          $startsOn = $this->path;
 153          $out = $this->_findRecursive($pattern);
 154          $this->cd($startsOn);
 155          return $out;
 156      }
 157  /**
 158   * Private helper function for findRecursive.
 159   *
 160   * @param string $pattern
 161   * @return array Files matching pattern
 162   * @access private
 163   */
 164  	function _findRecursive($pattern) {
 165          list($dirs, $files) = $this->ls();
 166          
 167          $found = array();
 168          foreach($files as $file) {
 169              if (preg_match("/^{$pattern}$/i", $file)) {
 170                  $found[] = $this->addPathElement($this->path, $file);
 171              }
 172          }
 173          $start = $this->path;
 174          foreach($dirs as $dir) {
 175              $this->cd($this->addPathElement($start, $dir));
 176              $found = array_merge($found, $this->findRecursive($pattern));
 177          }
 178          return $found;
 179      }
 180  /**
 181   * Returns true if given $path is a Windows path.
 182   *
 183   * @param string $path Path to check
 184   * @return boolean
 185   * @static
 186   */
 187  	function isWindowsPath($path) {
 188          $match = preg_match('#^[A-Z]:\\\#i', $path) ? true : false;
 189          return $match;
 190      }
 191  /**
 192   * Returns true if given $path is an absolute path.
 193   *
 194   * @param string $path Path to check
 195   * @return boolean
 196   * @static
 197   */
 198  	function isAbsolute($path) {
 199          $match = preg_match('#^\/#', $path) || preg_match('#^[A-Z]:\\\#i', $path);
 200          return $match;
 201      }
 202  /**
 203   * Returns true if given $path ends in a slash (i.e. is slash-terminated).
 204   *
 205   * @param string $path Path to check
 206   * @return boolean
 207   * @static
 208   */
 209  	function isSlashTerm($path) {
 210          $match = preg_match('#[\\\/]$#', $path) ? true : false;
 211          return $match;
 212      }
 213  /**
 214   * Returns a correct set of slashes for given $path. (\\ for Windows paths and / for other paths.)
 215   *
 216   * @param string $path Path to check
 217   * @return string Set of slashes ("\\" or "/")
 218   * @static
 219   */
 220  	function correctSlashFor($path) {
 221          return $this->isWindowsPath($path) ? '\\' : '/';
 222      }
 223  /**
 224   * Returns $path with added terminating slash (corrected for Windows or other OS).
 225   *
 226   * @param string $path Path to check
 227   * @return string
 228   * @static
 229   */
 230  function slashTerm($path) {
 231            return $path . ($this->isSlashTerm($path) ? null : $this->correctSlashFor($path));
 232       }
 233  /**
 234   * Returns $path with $element added, with correct slash in-between.
 235   *
 236   * @param string $path
 237   * @param string $element
 238   * @return string
 239   * @static
 240   */
 241  	function addPathElement($path, $element) {
 242          return $this->slashTerm($path) . $element;
 243      }
 244  /**
 245   * Returns true if the File is in a given CakePath.
 246   *
 247   * @return boolean
 248   */
 249  	function inCakePath($path = '') {
 250          $dir = substr($this->slashTerm(ROOT), 0, -1);
 251          $newdir = $this->slashTerm($dir . $path);
 252          return $this->inPath($newdir);
 253       }
 254  /**
 255   * Returns true if the File is in given path.
 256   *
 257   * @return boolean
 258   */
 259  	function inPath($path = '') {
 260          $dir = substr($this->slashTerm($path), 0, -1);
 261          $return = preg_match('/^' . preg_quote($this->slashTerm($dir), '/') . '(.*)/', $this->slashTerm($this->pwd()));
 262          if ($return == 1) {
 263              return true;
 264          } else {
 265              return false;
 266          }
 267      }
 268  /**
 269   * Create a directory structure recursively.
 270   *
 271   * @param string $pathname The directory structure to create
 272   * @return bool Returns TRUE on success, FALSE on failure
 273   */
 274  	function mkdirr($pathname, $mode = null) {
 275          if (is_dir($pathname) || empty($pathname)) {
 276              return true;
 277          }
 278  
 279          if (is_file($pathname)) {
 280              trigger_error('mkdirr() File exists', E_USER_WARNING);
 281              return false;
 282          }
 283          $nextPathname = substr($pathname, 0, strrpos($pathname, DIRECTORY_SEPARATOR));
 284  
 285          if ($this->mkdirr($nextPathname, $mode)) {
 286              if (!file_exists($pathname)) {
 287                  umask (0);
 288                  $mkdir = mkdir($pathname, $mode);
 289                  return $mkdir;
 290              }
 291          }
 292          return false;
 293      }
 294  /**
 295   * Returns the size in bytes of this Folder.
 296   *
 297   * @param string $directory Path to directory
 298   */
 299  	function dirsize() {
 300          $size = 0;
 301          $directory = $this->slashTerm($this->path);
 302          $stack = array($directory);
 303          $count = count($stack);
 304          for($i = 0, $j = $count; $i < $j; ++$i) {
 305              if (is_file($stack[$i])) {
 306                  $size += filesize($stack[$i]);
 307              } elseif (is_dir($stack[$i])) {
 308                  $dir = dir($stack[$i]);
 309  
 310                  while(false !== ($entry = $dir->read())) {
 311                      if ($entry == '.' || $entry == '..') {
 312                          continue;
 313                      }
 314                      $add = $stack[$i] . $entry;
 315  
 316                      if (is_dir($stack[$i] . $entry)) {
 317                          $add = $this->slashTerm($add);
 318                      }
 319                      $stack[ ]= $add;
 320                  }
 321                  $dir->close();
 322              }
 323              $j = count($stack);
 324          }
 325          return $size;
 326      }
 327  }
 328  ?>


Généré le : Sun Feb 25 19:27:47 2007 par Balluche grâce à PHPXref 0.7