0
|
1 <?php
|
|
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 {
|
|
17 if ($file == '.' or $file == '..') { continue; }
|
|
18 elseif (is_dir("$path/$file")) { $dirs[] = $file; }
|
|
19 else { $files[] = $file; }
|
|
20 }
|
|
21 sort($files);
|
|
22 sort($dirs);
|
|
23 function h($string) { return htmlspecialchars($string, ENT_QUOTES, 'UTF-8'); }
|
|
24 function a($string) { return preg_replace("#(%2F)+#", "/", rawurlencode($string)); }
|
|
25 function link_to($target, $title)
|
|
26 {
|
|
27 return("<a href=\"".a($target)."\">".h($title)."</a>");
|
|
28 }
|
|
29 function print_header()
|
|
30 {
|
|
31 global $link_prefix, $link_base, $to_dir;
|
|
32 $path_tree = link_to('/', '[root]') . "/";
|
|
33 foreach ($to_dir as $level => $dir)
|
|
34 {
|
|
35 if($dir)
|
|
36 {
|
|
37 $link = "/";
|
|
38 for ($i = 0; $i <= $level; $i++) { $link .= "$to_dir[$i]/"; }
|
|
39 $path_tree .= link_to($link, $dir)."/";
|
|
40 }
|
|
41 }
|
|
42 header('Content-Type: text/html; charset=utf-8');
|
|
43 echo "<!doctype html>
|
|
44 <meta http-equiv=\"Content-type\" content=\"text/html;charset=UTF-8\">
|
|
45 <title>Index of ", h(rtrim($link_base,"/")."/"), "</title>
|
|
46 <body>
|
|
47 <h1>Index of $path_tree</h1>";
|
|
48 }
|
|
49 function nice_size($size)
|
|
50 {
|
|
51 static $unit = array('B', 'KB', 'MB', 'GB', 'TB');
|
|
52 for ($i = 0; $size >= 1000 && $i < count($unit); $i++) { $size /= 1000; }
|
|
53 if ($i == 0) { return(sprintf("%d %s", $size, $unit[$i])); }
|
|
54 else { return(sprintf("%.2f %s", $size, $unit[$i])); }
|
|
55 }
|
|
56 function file_rows($files, $is_dir)
|
|
57 {
|
|
58 global $path, $link_base, $link_prefix;
|
|
59 static $unit = array('B', 'KB', 'MB', 'GB', 'TB');
|
|
60 $file_rows = "";
|
|
61 if ($is_dir)
|
|
62 {
|
|
63 $file_suffix = "/";
|
|
64 if($link_base != $link_prefix)
|
|
65 {
|
|
66 $file_rows .= "<tr><td colspan=3>".link_to(dirname($link_base),"..")."</td></tr>";
|
|
67 }
|
|
68 } else { $file_suffix = ""; }
|
|
69 foreach($files as $file)
|
|
70 {
|
|
71 $file_stat = stat("$path/".$file);
|
|
72 $file_rows .= "<tr><td>".
|
|
73 link_to("$link_base/".$file, $file.$file_suffix)."</td><td>".
|
|
74 h(strftime("%a %d-%b-%G %H:%M", $file_stat['mtime']))."</td><td>";
|
|
75 if ($is_dir) { $file_rows .= "[dir]"; }
|
|
76 else { $file_rows .= nice_size($file_stat['size']); }
|
|
77 $file_rows .= "</tr>";
|
|
78 }
|
|
79 return $file_rows;
|
|
80 }
|
|
81 function print_list()
|
|
82 {
|
|
83 global $dirs, $files;
|
|
84 echo "<table><thead><tr>",
|
|
85 "<td>File</td>",
|
|
86 "<td>Date</td>",
|
|
87 "<td>Size</td>",
|
|
88 "</tr></thead>",
|
|
89 "<tbody>", file_rows($dirs, true), file_rows($files, false), "</tbody>",
|
|
90 "</table>";
|
|
91 }
|
|
92 function print_footer()
|
|
93 {
|
|
94 echo "</body>";
|
|
95 }
|
|
96 print_header();
|
|
97 print_list();
|
|
98 print_footer();
|
|
99 ?>
|