* @version 1.0.1 (June 30, 2004) * @package AutoIndex */ class DirectoryList implements Iterator { /** * @var string The directory this object represents */ protected $dir_name; /** * @var array The list of filesname in this directory (strings) */ protected $contents; /** * @var int The size of the $contents array */ private $list_count; //begin implementation of Iterator /** * @var int $i is used to keep track of the current pointer inside the array when implementing Iterator */ private $i; /** * @return string The element $i currently points to in the array */ #[\ReturnTypeWillChange] public function current() { if ($this -> i < count($this -> contents)) { return $this -> contents[$this -> i]; } return false; } /** * Increments the internal array pointer, then returns the value * at that new position. * * @return string The current position of the pointer in the array */ #[\ReturnTypeWillChange] public function next() { $this -> i++; return $this -> current(); } /** * Sets the internal array pointer to 0 */ #[\ReturnTypeWillChange] public function rewind() { $this -> i = 0; } /** * @return bool True if $i is a valid array index */ #[\ReturnTypeWillChange] public function valid() { return ($this -> i < count($this -> contents)); } /** * @return int Returns $i, the key of the array */ #[\ReturnTypeWillChange] public function key() { return $this -> i; } //end implementation of Iterator /** * @return int The total size in bytes of the folder (recursive) */ public function size_recursive() { $total_size = 0; foreach ($this as $current) { $t = $this -> dir_name . $current; if (@is_dir($t)) { if ($current != '..') { $temp = new DirectoryList($t); $total_size += $temp -> size_recursive(); } } else { $total_size += @filesize($t); } } return $total_size; } /** * @return int The total number of files in this directory (recursive) */ public function num_files() { $count = 0; foreach ($this as $current) { $t = $this -> dir_name . $current; if (@is_dir($t)) { if ($current != '..') { $temp = new DirectoryList($t); $count += $temp -> num_files(); } } else { $count++; } } return $count; } /** * @param string $string The string to search for * @param array $array The array to search * @return bool True if $string matches any elements in $array */ public static function match_in_array($string, &$array) { $string = Item::get_basename($string); static $replace = array( '\*' => '[^\/]*', '\+' => '[^\/]+', '\?' => '[^\/]?'); foreach ($array as $m) { if (preg_match('/^' . strtr(preg_quote(Item::get_basename($m), '/'), $replace) . '$/i', $string)) { return true; } } return false; } /** * @param string $t The file or folder name * @param bool $is_file * @return bool True if $t is listed as a hidden file */ public static function is_hidden($t, $is_file = true) { $t = Item::get_basename($t); if ($t == '.' || $t == '') { return true; } global $you; if ($you -> level >= ADMIN) //allow admins to view hidden files { return false; } global $hidden_files, $show_only_these_files; if ($is_file && count($show_only_these_files)) { return (!self::match_in_array($t, $show_only_these_files)); } return self::match_in_array($t, $hidden_files); } /** * @param string $var The key to look for * @return mixed The data stored at the key */ public function __get($var) { if (isset($this -> $var)) { return $this -> $var; } throw new ExceptionDisplay('Variable ' . Url::html_output($var) . ' not set in DirectoryList class.'); } /** * @param string $path */ public function __construct($path) { $path = Item::make_sure_slash($path); if (!@is_dir($path)) { throw new ExceptionDisplay('Directory ' . Url::html_output($path) . ' does not exist.'); } $temp_list = @scandir($path); if ($temp_list === false) { throw new ExceptionDisplay('Error reading from directory ' . Url::html_output($path) . '.'); } $this -> dir_name = $path; $this -> contents = array(); foreach ($temp_list as $t) { if (!self::is_hidden($t, !@is_dir($path . $t))) { $this -> contents[] = $t; } } $this -> list_count = count($this -> contents); $this -> i = 0; } } ?>