Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 9th, 2012  |  syntax: None  |  size: 6.33 KB  |  hits: 12  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. <?php
  2. error_reporting(E_ALL|E_STRICT);
  3. if (defined('E_DEPRECATED')) {
  4.         error_reporting(error_reporting() & ~E_DEPRECATED);
  5. }
  6. mb_detect_order('ASCII,EUC-JP,SJIS,JIS,UTF-8');
  7.  
  8. class U
  9. {
  10.         public static function NL($data) {
  11.                 return str_replace("\r", "\n", str_replace("\r\n", "\n", $data));
  12.         }
  13.         public static function H($data, $callback=null) {
  14.                 if (isset($data) && strcmp($data, '') != 0) {
  15.                         if (isset($callback)) {
  16.                                 $data = call_user_func($callback, $data);
  17.                         }
  18.                         $data = htmlspecialchars(self::NL($data), ENT_QUOTES, 'UTF-8');
  19.                 }
  20.                 return $data;
  21.         }
  22. }
  23.  
  24. class Holy_FileIterator implements Iterator, Countable
  25. {
  26.         protected $isDocumentRoot = false;
  27.         protected $index = 0;
  28.         protected $count = 0;
  29.         protected $files = array();
  30.         public $directory;
  31.         public $sortKey;
  32.         public $ignorePrefixes;
  33.  
  34.         public function __construct($dir, $types = null, $recursive = false, $ignorePrefixes = '.svn')
  35.         {
  36.                 $documentRoot = $_SERVER['DOCUMENT_ROOT'];
  37.                 if (DIRECTORY_SEPARATOR === '\\') {
  38.                         $dir = str_replace('\\', '/', $dir);
  39.                         $documentRoot = str_replace('\\', '/', $documentRoot);
  40.                 }
  41.                 if (is_string($types)) {
  42.                         $types = explode(',', $types);
  43.                 }
  44.                 $this->directory = $dir;
  45.                 $this->isDocumentRoot = ($documentRoot === $dir);
  46.                 $this->ignorePrefixes = $ignorePrefixes;
  47.                 $this->files = $this->getFiles($dir, $types, $recursive);
  48.                 $this->count = count($this->files);
  49.         }
  50.  
  51.         public function getFiles($dir, $types = null, $recursive = false, $baseDir = '')
  52.         {
  53.                 $files = array();
  54.                 $dir = rtrim($dir, DIRECTORY_SEPARATOR);
  55.                 if (DIRECTORY_SEPARATOR === '\\') {
  56.                         $dir = str_replace('\\', '/', $dir);
  57.                 }
  58.                 if (DIRECTORY_SEPARATOR === '\\') {
  59.                         $baseDir = str_replace('\\', '/', $baseDir);
  60.                 }
  61.                 if ($dir_handle = opendir($dir)) {
  62.                         while (false !== ($filename = readdir($dir_handle))) {
  63.                                 if ($filename === '.' || $filename === '..') {
  64.                                         continue;
  65.                                 }
  66.                                 if (isset($this->ignorePrefixes)) {
  67.                                         $prefixes = explode(',', $this->ignorePrefixes);
  68.                                         $ignore = false;
  69.                                         foreach ($prefixes as $prefix) {
  70.                                                 if (strpos($filename, $prefix) === 0) {
  71.                                                         $ignore = true;
  72.                                                         break;
  73.                                                 }
  74.                                         }
  75.                                         if ($ignore) {
  76.                                                 continue;
  77.                                         }
  78.                                 }
  79.                                 $path = $dir . DIRECTORY_SEPARATOR . $filename;
  80.                                 if (is_file($path) && is_array($types) &&
  81.                                         !in_array(pathinfo($path, PATHINFO_EXTENSION), $types, true)
  82.                                 ) {
  83.                                         continue;
  84.                                 }
  85.                                 $isDir = is_dir($path);
  86.                                 $file = array();
  87.                                 $file['path'    ] = $path;
  88.                                 $file['name'    ] = $baseDir . $filename;
  89.                                 $file['size'    ] = filesize($path);
  90.                                 $file['mtime'   ] = filemtime($path);
  91.                                 $file['isDir'   ] = $isDir;
  92.                                 $file['encoding'] = (file_exists($path) && is_file($path) && is_readable($path))
  93.                                         ? mb_detect_encoding(file_get_contents($path))
  94.                                         : null;
  95.                                 $files[] = $file;
  96.                                 if ($recursive && $isDir) {
  97.                                         $subFiles = $this->getFiles($path, $types, $recursive,
  98.                                                 $baseDir . $filename . DIRECTORY_SEPARATOR);
  99.                                         if (!empty($subFiles)) {
  100.                                                 $files = array_merge($files, $subFiles);
  101.                                         }
  102.                                 }
  103.                         }
  104.                         closedir($dir_handle);
  105.                 }
  106.                 return $files;
  107.         }
  108.  
  109.         public function sort($key, $desc=false)
  110.         {
  111.                 if (empty($this->files)) {
  112.                         return;
  113.                 }
  114.                 if (!array_key_exists($key, $this->files[0])) {
  115.                         throw new RuntimeException(
  116.                                 sprintf('Undefined sort key "%s" is specified.', $key));
  117.                 }
  118.                 usort($this->files, function ($row1, $row2) use ($key, $desc) {
  119.                         return ($desc)
  120.                                 ? strnatcasecmp($row2[$key], $row1[$key])
  121.                                 : strnatcasecmp($row1[$key], $row2[$key]);
  122.                 });
  123.         }
  124.  
  125.         public function source($filename)
  126.         {
  127.                 if (strpos($filename, '..') !== false) {
  128.                         throw new RuntimeException('invalid access.');
  129.                 }
  130.                 $path = $this->directory . DIRECTORY_SEPARATOR . $filename;
  131.                 if (file_exists($path) && is_file($path) && is_readable($path)) {
  132.                         return file_get_contents($path);
  133.                 }
  134.                 return null;
  135.         }
  136.  
  137.         public function isDocumentRoot()
  138.         {
  139.                 return $this->isDocumentRoot;
  140.         }
  141.  
  142.         public function current()
  143.         {
  144.                 return $this->files[$this->index];
  145.         }
  146.  
  147.         public function key()
  148.         {
  149.                 return $this->index;
  150.         }
  151.  
  152.         public function next()
  153.         {
  154.                 $this->index++;
  155.         }
  156.  
  157.         public function rewind()
  158.         {
  159.                 $this->index = 0;
  160.         }
  161.  
  162.         public function valid()
  163.         {
  164.                 return ($this->index < $this->count);
  165.         }
  166.  
  167.         public function count()
  168.         {
  169.                 return $this->count;
  170.         }
  171.  
  172. }
  173.  
  174. $fileIterator = new Holy_FileIterator(dirname(__FILE__), 'html,php', true, '.svn');
  175.  
  176. $sort = 'name';
  177. $desc = (isset($_GET['desc'])) ? (bool)$_GET['desc'] : false;
  178. if (isset($_GET['sort']) && strlen($_GET['sort']) >= 1) {
  179.         $sort = $_GET['sort'];
  180. }
  181. $fileIterator->sort($sort, $desc);
  182.  
  183. if (isset($_GET['source'])) {
  184.         $source = $fileIterator->source($_GET['source']);
  185.         $source_path = $_GET['source'];
  186. }
  187. $encoding = (isset($source)) ? mb_detect_encoding($source)
  188.         : mb_detect_encoding(file_get_contents(__FILE__));
  189.  
  190. $title = sprintf('%s @%s(%s)', dirname(__FILE__), $_SERVER['SERVER_NAME'], $_SERVER['SERVER_ADDR']);
  191.  
  192. ?>
  193. <!DOCTYPE html>
  194. <html>
  195. <head>
  196. <meta charset="<?=U::H($encoding)?>" />
  197. <title><?=U::H($title)?></title>
  198. <style type="text/css">
  199. body  {font-family:monospace;}
  200. table {border-collapse:collapse;border-spacing:0px;}
  201. th,td {padding:4px; border:solid 1px #999999;}
  202. div.source {border:dotted 1px #999999;padding:5px;}
  203. </style>
  204. </head>
  205. <body>
  206. <h1><?=U::H($title)?></h1>
  207.  
  208. <?php if (isset($source)) : ?>
  209. <h2><?=U::H($source_path)?></h2>
  210. <div class="source"><?=highlight_string($source, true)?></div>
  211. <?php endif ?>
  212.  
  213. <table border="0" cellspacing="0">
  214. <caption>Total <?=U::H(count($fileIterator))?> Files</caption>
  215. <thead>
  216. <tr>
  217. <th><a href="?sort=name">[asc]</a>NAME<a href="?sort=name&desc=1">[desc]</a></th>
  218. <th><a href="?sort=mtime">[asc]</a>MTIME<a href="?sort=mtime&desc=1">[desc]</a></th>
  219. <th><a href="?sort=size">[asc]</a>SIZE<a href="?sort=size&desc=1">[desc]</a></th>
  220. <th><a href="?sort=encoding">[asc]</a>ENC<a href="?sort=encoding&desc=1">[desc]</a></th>
  221. <th>ACTION</th>
  222. </tr>
  223. </thead>
  224. <tbody>
  225. <?php foreach ($fileIterator as $file) : ?>
  226. <tr>
  227. <td><a href="./<?=U::H($file['name' ])?>"><?=U::H($file['name'])?><?=U::H(($file['isDir']) ? '/' : '')?></a></td>
  228. <td align="center"><?=U::H($file['mtime'], function ($var) { return date('Y-m-d H:i:s', $var); })?></td>
  229. <td align="right"><?=U::H($file['size'], function ($var) { return number_format(sprintf('%u', $var)); })?></td>
  230. <td><?=U::H($file['encoding'])?></td>
  231. <td><a href="?source=<?=U::H($file['name'])?>">source</a></td>
  232. <?php endforeach ?>
  233. </tbody>
  234. </table>
  235.  
  236. <?php if (false === $fileIterator->isDocumentRoot()) :?>
  237. <p><a href="../">../(parent directory)</a></p>
  238. <?php endif ?>
  239.  
  240. </body>
  241. </html>