annotate index.php @ 1:9ba6983b7c19

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