annotate index.php @ 2:9b5884ba214a

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