Source code for /home/gipetto1/top-frog.com/public_html/script_src/files/ListDirectory.class.php
- <?php
-
- @category
- @package
- @license
- @version
- class AP_File_ListDirectory
- {
- private static $regex_keep = false;
- private static $regex_discard = false;
- private static $regex_set = false;
-
-
- @todo
- @param
- @param
- @param
- @param
- @param
- @return
- public static function get_directory_contents($dir,$mode=1,$regex_keep=false,$regex_discard=false,$regex_dirs=false)
- {
-
- if(self::$regex_set === false)
- {
- if(self::$regex_keep !== $regex_keep) { self::$regex_keep = $regex_keep; }
- if(self::$regex_discard != $regex_discard) { self::$regex_discard = $regex_discard; }
- self::$regex_set = true;
- }
-
-
- if(!($dir instanceof RecursiveDirectoryIterator))
- {
- if(is_dir($dir))
- {
- $dir = new RecursiveDirectoryIterator($dir);
- $contents = array();
- }
- else
- {
- throw new Exception('Directory is invalid or does not exist');
- }
- }
-
- for($dir->rewind(); $dir->valid(); $dir->next())
- {
- $filename = $dir->getFilename();
-
- if($dir->isDot() || $filename{0} == '.') { continue; }
-
-
- if($dir->isDir())
- {
-
- if($mode === 3)
- {
- if($regex_dirs === false || ($regex_dirs === true && self::regex_entry($filename) === true))
- {
-
- if (!$dir->isLink())
- {
- $contents[$filename] = $dir->hasChildren() ? self::get_directory_contents($dir->getChildren(),$mode,$regex_keep,$regex_discard,$regex_dirs) : $dir->current();
- }
- else
- {
- $tmp_dir = self::get_directory_contents($dir->getPathname(),$mode,$regex_keep,$regex_discard,$regex_dirs);
- $contents[$filename] = count($tmp_dir) ? $tmp_dir : array();
- }
- }
- }
- else
- {
-
- if($mode === 2) { $contents[$filename] = $dir->current(); }
- }
- }
- elseif($dir->isFile())
- {
-
- $keep_entry = self::regex_entry($filename);
- if ($keep_entry === true) { $contents[$filename] = self::get_spl_fileinfo($dir->current()); }
- }
- }
-
-
- if(isset($contents) && is_array($contents)) { uksort($contents, 'strnatcmp'); }
-
- return $contents;
- }
-
- private static function regex_entry($filename)
- {
- $keep_entry = null;
-
- if (self::$regex_keep !== false && preg_match(self::$regex_keep,$filename)) { $keep_entry = true; }
- elseif (self::$regex_keep !== false) { $keep_entry = false; }
-
-
- if (self::$regex_discard !== false && preg_match(self::$regex_discard,$filename)) { $keep_entry = false; }
-
-
- if (is_null($keep_entry)) { $keep_entry = true; }
-
- return $keep_entry;
- }
-
-
- @param
- @return
- private static function get_spl_fileinfo(SplFileInfo $item)
- {
- $info = array(
- 'filename' => $item->getFilename(),
- 'size' => $item->getSize(),
- 'sizef' => AP_File_Size::show_file_size($item->getSize()),
- 'type' => $item->getType(),
- 'ext' => pathinfo($item,PATHINFO_EXTENSION),
- 'pathname' => $item->getPathname(),
- 'owner' => $item->getOwner(),
- 'group' => $item->getGroup(),
- 'perms' => $item->getPerms(),
- 'inode' => $item->getInode(),
- 'atime' => $item->getAtime(),
- 'ctime' => $item->getCtime(),
- 'mtime' => $item->getMtime()
- );
-
-
- if(phpversion() <= '5.1.3') { $info['filename'] = basename($info['filename']); }
-
-
- if(phpversion() >= '5.1.4') { $info['path'] = $item->getPath(); }
-
- return $info;
- }
-
-
- }
- ?>