Source code for /home/gipetto1/top-frog.com/public_html/script_src/files/ListDirectory.class.php

  1.  <?php
  2.   /**
  3.   * AP_File_ListDirectory
  4.   *
  5.   * Directory listing class with regex filtering and contollable recursive listing
  6.   *
  7.   * LICENSE
  8.   *
  9.   * This source file is subject to the new BSD license that is bundled
  10.   * with this package in the file LICENSE.txt.
  11.   *
  12.   * @category AP_File
  13.   * @package AP_File_ListDirectory
  14.   * @license New BSD License
  15.   * @version 1.2 2007-03-30, PHP Version 5.2+
  16.   */
  17.   class AP_File_ListDirectory
  18.   {
  19.   private static $regex_keep = false;
  20.   private static $regex_discard = false;
  21.   private static $regex_set = false;
  22.  
  23.   /**
  24.   * Get directory contents as array
  25.   *
  26.   * @todo add a limit to how many levels it should record
  27.   *
  28.   * @param string/object $dir - path to search
  29.   * @param int $mode - search mode: 1 - single directory, files only; 2 - single directory, show included directories; 3 - recursive (no limits)
  30.   * @param bool/string $regex_keep - regex pattern for keeping entries
  31.   * @param bool/string $regex_discard - regex pattern for discarding entries
  32.   * @param bool $regex_dirs - wether or not to apply regex to dir names
  33.   * @return array
  34.   */
  35.   public static function get_directory_contents($dir,$mode=1,$regex_keep=false,$regex_discard=false,$regex_dirs=false)
  36.   {
  37.   // define regex statements
  38.   if(self::$regex_set === false)
  39.   {
  40.   if(self::$regex_keep !== $regex_keep) { self::$regex_keep = $regex_keep; }
  41.   if(self::$regex_discard != $regex_discard) { self::$regex_discard = $regex_discard; }
  42.   self::$regex_set = true;
  43.   }
  44.  
  45.   // make sure we're working with an SPL RDI object
  46.   if(!($dir instanceof RecursiveDirectoryIterator))
  47.   {
  48.   if(is_dir($dir))
  49.   {
  50.   $dir = new RecursiveDirectoryIterator($dir);
  51.   $contents = array();
  52.   }
  53.   else
  54.   {
  55.   throw new Exception('Directory is invalid or does not exist');
  56.   }
  57.   }
  58.  
  59.   for($dir->rewind(); $dir->valid(); $dir->next())
  60.   {
  61.   $filename = $dir->getFilename();
  62.  
  63.   if($dir->isDot() || $filename{0} == '.') { continue; }
  64.  
  65.   // is it a directory?
  66.   if($dir->isDir())
  67.   {
  68.   // are we recursive?
  69.   if($mode === 3)
  70.   {
  71.   if($regex_dirs === false || ($regex_dirs === true && self::regex_entry($filename) === true))
  72.   {
  73.   // only go recursive if the dir has children
  74.   if (!$dir->isLink())
  75.   {
  76.   $contents[$filename] = $dir->hasChildren() ? self::get_directory_contents($dir->getChildren(),$mode,$regex_keep,$regex_discard,$regex_dirs) : $dir->current();
  77.   }
  78.   else
  79.   {
  80.   $tmp_dir = self::get_directory_contents($dir->getPathname(),$mode,$regex_keep,$regex_discard,$regex_dirs);
  81.   $contents[$filename] = count($tmp_dir) ? $tmp_dir : array();
  82.   }
  83.   }
  84.   }
  85.   else
  86.   {
  87.   // are we including directories in the output?
  88.   if($mode === 2) { $contents[$filename] = $dir->current(); }
  89.   }
  90.   }
  91.   elseif($dir->isFile())
  92.   {
  93.   // log file info?
  94.   $keep_entry = self::regex_entry($filename);
  95.   if ($keep_entry === true) { $contents[$filename] = self::get_spl_fileinfo($dir->current()); }
  96.   }
  97.   }
  98.  
  99.   // are we sorting?
  100.   if(isset($contents) && is_array($contents)) { uksort($contents, 'strnatcmp'); }
  101.  
  102.   return $contents;
  103.   }
  104.  
  105.   private static function regex_entry($filename)
  106.   {
  107.   $keep_entry = null;
  108.  
  109.   if (self::$regex_keep !== false && preg_match(self::$regex_keep,$filename)) { $keep_entry = true; }
  110.   elseif (self::$regex_keep !== false) { $keep_entry = false; }
  111.  
  112.   // are we excluding files?
  113.   if (self::$regex_discard !== false && preg_match(self::$regex_discard,$filename)) { $keep_entry = false; }
  114.  
  115.   // did we even do anything?
  116.   if (is_null($keep_entry)) { $keep_entry = true; }
  117.  
  118.   return $keep_entry;
  119.   }
  120.  
  121.   /**
  122.   * Gather info on the SPL object
  123.   *
  124.   * filename is different under newer versions of php
  125.   * 5.1.2 = full path to file
  126.   * 5.1.3 = not tested
  127.   * 5.1.4 = filename only
  128.   * function accommodates this difference
  129.   *
  130.   * path is available under newer versions of php, tested with 5.1.4,
  131.   * which is the path to the file without the filename in it
  132.   *
  133.   * @param SplFileInfo Object $item - pointer to our file
  134.   * @return array
  135.   */
  136.   private static function get_spl_fileinfo(SplFileInfo $item)
  137.   {
  138.   $info = array(
  139.   'filename' => $item->getFilename(), // file name
  140.   'size' => $item->getSize(), // raw file size
  141.   'sizef' => AP_File_Size::show_file_size($item->getSize()), // formatted file size
  142.   'type' => $item->getType(), // type, ie: dir, file
  143.   'ext' => pathinfo($item,PATHINFO_EXTENSION), // file extension
  144.   'pathname' => $item->getPathname(), // full path to file with filename
  145.   'owner' => $item->getOwner(), // owner uid
  146.   'group' => $item->getGroup(), // group guid
  147.   'perms' => $item->getPerms(), // file permissions
  148.   'inode' => $item->getInode(), // inode
  149.   'atime' => $item->getAtime(), // last access time
  150.   'ctime' => $item->getCtime(), // created time
  151.   'mtime' => $item->getMtime() // last modified time
  152.   );
  153.  
  154.   // just the filename for older versions of php5
  155.   if(phpversion() <= '5.1.3') { $info['filename'] = basename($info['filename']); }
  156.  
  157.   // path without file name
  158.   if(phpversion() >= '5.1.4') { $info['path'] = $item->getPath(); }
  159.  
  160.   return $info;
  161.   }
  162.  
  163.  
  164.   }
  165.  ?>