| 0 | 1 <?php | 
| 2 | 2   //real men use UTC for internets | 
|  | 3   date_default_timezone_set('UTC'); | 
|  | 4   $wwwroot = $_ENV["DOCUMENT_ROOT"]; | 
|  | 5   $link_prefix = "/"; | 
|  | 6   $link_base = rtrim(preg_replace("#/+#", "/", urldecode($_ENV["REQUEST_URI"])),'/'); | 
|  | 7   $link_path = substr($link_base,strlen(utf8_decode($link_prefix))); | 
|  | 8   $path = realpath("$wwwroot/".$link_path); | 
|  | 9   if (!$path) { die("fail"); } | 
|  | 10   $dir_handle = @opendir($path); | 
|  | 11   $to_dir = explode('/', trim($link_path,'/')); | 
|  | 12   array_unshift($to_dir, trim($link_prefix,'/')); | 
|  | 13   $files = array(); | 
|  | 14   $dirs = array(); | 
|  | 15   while ($file = readdir($dir_handle)) { | 
|  | 16     if ($file == '.' or $file == '..') { continue; } | 
|  | 17     elseif (is_dir("$path/$file")) { $dirs[] = $file; } | 
|  | 18     else { $files[] = $file; } | 
|  | 19   } | 
|  | 20   sort($files); | 
|  | 21   sort($dirs); | 
|  | 22   function h($string) { return htmlspecialchars($string, ENT_QUOTES, 'UTF-8'); } | 
|  | 23   function a($string) { return preg_replace("#(%2F)+#", "/", rawurlencode($string)); } | 
|  | 24   function link_to($target, $title) { | 
|  | 25     return("<a href=\"".a($target)."\">".h($title)."</a>"); | 
|  | 26   } | 
|  | 27   function print_header() { | 
|  | 28     global $link_prefix, $link_base, $to_dir; | 
|  | 29     $path_tree = link_to('/', '[root]') . "/"; | 
|  | 30     foreach ($to_dir as $level => $dir) { | 
|  | 31       if($dir) { | 
|  | 32         $link = "/"; | 
|  | 33         for ($i = 0; $i <= $level; $i++) { $link .= "$to_dir[$i]/"; } | 
|  | 34         $path_tree .= link_to($link, $dir)."/"; | 
|  | 35       } | 
|  | 36     } | 
|  | 37     header('Content-Type: text/html; charset=utf-8'); | 
|  | 38     echo "<!doctype html> | 
|  | 39       <meta http-equiv=\"Content-type\" content=\"text/html;charset=UTF-8\"> | 
|  | 40       <title>Index of ", h(rtrim($link_base,"/")."/"), "</title> | 
|  | 41       <body> | 
|  | 42       <h1>Index of $path_tree</h1>"; | 
|  | 43   } | 
|  | 44   function nice_size($size) { | 
|  | 45     static $unit = array('B', 'KB', 'MB', 'GB', 'TB'); | 
|  | 46     for ($i = 0; $size >= 1000 && $i < count($unit); $i++) { $size /= 1000; } | 
|  | 47     if ($i == 0) { return(sprintf("%d %s", $size, $unit[$i])); } | 
|  | 48     else { return(sprintf("%.2f %s", $size, $unit[$i])); } | 
|  | 49   } | 
|  | 50   function file_rows($files, $is_dir) { | 
|  | 51     global $path, $link_base, $link_prefix; | 
|  | 52     static $unit = array('B', 'KB', 'MB', 'GB', 'TB'); | 
|  | 53     $file_rows = ""; | 
|  | 54     if ($is_dir) { | 
|  | 55       $file_suffix = "/"; | 
|  | 56       if($link_base != $link_prefix) { | 
|  | 57         $file_rows .= "<tr><td colspan=3>".link_to(dirname($link_base),"..")."</td></tr>"; | 
|  | 58       } | 
|  | 59     } else { $file_suffix = ""; } | 
|  | 60     foreach($files as $file) { | 
|  | 61       $file_stat = stat("$path/".$file); | 
|  | 62       $file_rows .= "<tr><td>". | 
|  | 63         link_to("$link_base/".$file, $file.$file_suffix)."</td><td>". | 
|  | 64         h(strftime("%a %d-%b-%G %H:%M", $file_stat['mtime']))."</td><td>"; | 
|  | 65       if ($is_dir) { $file_rows .= "[dir]"; } | 
|  | 66       else { $file_rows .= nice_size($file_stat['size']); } | 
|  | 67       $file_rows .= "</tr>"; | 
|  | 68     } | 
|  | 69     return $file_rows; | 
|  | 70   } | 
|  | 71   function print_list() { | 
|  | 72     global $dirs, $files; | 
|  | 73     echo "<table><thead><tr>", | 
|  | 74          "<td>File</td>", | 
|  | 75          "<td>Date</td>", | 
|  | 76          "<td>Size</td>", | 
|  | 77          "</tr></thead>", | 
|  | 78          "<tbody>", file_rows($dirs, true), file_rows($files, false), "</tbody>", | 
|  | 79          "</table>"; | 
|  | 80   } | 
|  | 81   function print_footer() { | 
|  | 82     echo "</body>"; | 
|  | 83   } | 
|  | 84   print_header(); | 
|  | 85   print_list(); | 
|  | 86   print_footer(); | 
| 0 | 87 ?> |