annotate index.php @ 3:9ea8be5b28d1

"refactor".
author edogawaconan <me@myconan.net>
date Tue, 21 Oct 2014 18:01:43 +0900
parents 9b5884ba214a
children 7812af1c23ae
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 }
3
9ea8be5b28d1 "refactor".
edogawaconan <me@myconan.net>
parents: 2
diff changeset
27 function path_tree_header() {
2
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 }
3
9ea8be5b28d1 "refactor".
edogawaconan <me@myconan.net>
parents: 2
diff changeset
37 return "Index of $path_tree";
9ea8be5b28d1 "refactor".
edogawaconan <me@myconan.net>
parents: 2
diff changeset
38 }
9ea8be5b28d1 "refactor".
edogawaconan <me@myconan.net>
parents: 2
diff changeset
39 function title() {
9ea8be5b28d1 "refactor".
edogawaconan <me@myconan.net>
parents: 2
diff changeset
40 global $link_base;
9ea8be5b28d1 "refactor".
edogawaconan <me@myconan.net>
parents: 2
diff changeset
41 return "Index of ".h(rtrim($link_base,"/")."/");
2
9b5884ba214a Fix indentation.
edogawaconan <me@myconan.net>
parents: 0
diff changeset
42 }
3
9ea8be5b28d1 "refactor".
edogawaconan <me@myconan.net>
parents: 2
diff changeset
43
2
9b5884ba214a Fix indentation.
edogawaconan <me@myconan.net>
parents: 0
diff changeset
44 function nice_size($size) {
3
9ea8be5b28d1 "refactor".
edogawaconan <me@myconan.net>
parents: 2
diff changeset
45 $thousand_units = array('ko', 'Mo', 'Go', 'To', 'Po');
9ea8be5b28d1 "refactor".
edogawaconan <me@myconan.net>
parents: 2
diff changeset
46
9ea8be5b28d1 "refactor".
edogawaconan <me@myconan.net>
parents: 2
diff changeset
47 $return_format = "%d %s";
9ea8be5b28d1 "refactor".
edogawaconan <me@myconan.net>
parents: 2
diff changeset
48
9ea8be5b28d1 "refactor".
edogawaconan <me@myconan.net>
parents: 2
diff changeset
49 if ($size <= 1) {
9ea8be5b28d1 "refactor".
edogawaconan <me@myconan.net>
parents: 2
diff changeset
50 $return_unit = "octet";
9ea8be5b28d1 "refactor".
edogawaconan <me@myconan.net>
parents: 2
diff changeset
51 } elseif ($size < 10000) {
9ea8be5b28d1 "refactor".
edogawaconan <me@myconan.net>
parents: 2
diff changeset
52 $return_unit = "octets";
9ea8be5b28d1 "refactor".
edogawaconan <me@myconan.net>
parents: 2
diff changeset
53 } else {
9ea8be5b28d1 "refactor".
edogawaconan <me@myconan.net>
parents: 2
diff changeset
54 $size /= 1000;
9ea8be5b28d1 "refactor".
edogawaconan <me@myconan.net>
parents: 2
diff changeset
55 for ($i = 0; $size >= 1000 && $i < count($thousand_units); $i++) { $size /= 1000; }
9ea8be5b28d1 "refactor".
edogawaconan <me@myconan.net>
parents: 2
diff changeset
56 $return_format = "%.2f %s";
9ea8be5b28d1 "refactor".
edogawaconan <me@myconan.net>
parents: 2
diff changeset
57 $return_unit = $thousand_units[$i];
9ea8be5b28d1 "refactor".
edogawaconan <me@myconan.net>
parents: 2
diff changeset
58 }
9ea8be5b28d1 "refactor".
edogawaconan <me@myconan.net>
parents: 2
diff changeset
59 return sprintf($return_format, $size, $return_unit);
2
9b5884ba214a Fix indentation.
edogawaconan <me@myconan.net>
parents: 0
diff changeset
60 }
3
9ea8be5b28d1 "refactor".
edogawaconan <me@myconan.net>
parents: 2
diff changeset
61
2
9b5884ba214a Fix indentation.
edogawaconan <me@myconan.net>
parents: 0
diff changeset
62 function file_rows($files, $is_dir) {
9b5884ba214a Fix indentation.
edogawaconan <me@myconan.net>
parents: 0
diff changeset
63 global $path, $link_base, $link_prefix;
3
9ea8be5b28d1 "refactor".
edogawaconan <me@myconan.net>
parents: 2
diff changeset
64
2
9b5884ba214a Fix indentation.
edogawaconan <me@myconan.net>
parents: 0
diff changeset
65 $file_rows = "";
9b5884ba214a Fix indentation.
edogawaconan <me@myconan.net>
parents: 0
diff changeset
66 if ($is_dir) {
9b5884ba214a Fix indentation.
edogawaconan <me@myconan.net>
parents: 0
diff changeset
67 $file_suffix = "/";
9b5884ba214a Fix indentation.
edogawaconan <me@myconan.net>
parents: 0
diff changeset
68 if($link_base != $link_prefix) {
3
9ea8be5b28d1 "refactor".
edogawaconan <me@myconan.net>
parents: 2
diff changeset
69 $file_rows .= "<tr><td colspan=3>".link_to(dirname($link_base)."/","..")."</td></tr>";
2
9b5884ba214a Fix indentation.
edogawaconan <me@myconan.net>
parents: 0
diff changeset
70 }
9b5884ba214a Fix indentation.
edogawaconan <me@myconan.net>
parents: 0
diff changeset
71 } else { $file_suffix = ""; }
3
9ea8be5b28d1 "refactor".
edogawaconan <me@myconan.net>
parents: 2
diff changeset
72
2
9b5884ba214a Fix indentation.
edogawaconan <me@myconan.net>
parents: 0
diff changeset
73 foreach($files as $file) {
9b5884ba214a Fix indentation.
edogawaconan <me@myconan.net>
parents: 0
diff changeset
74 $file_stat = stat("$path/".$file);
3
9ea8be5b28d1 "refactor".
edogawaconan <me@myconan.net>
parents: 2
diff changeset
75
9ea8be5b28d1 "refactor".
edogawaconan <me@myconan.net>
parents: 2
diff changeset
76 $file_rows .= "<tr>";
9ea8be5b28d1 "refactor".
edogawaconan <me@myconan.net>
parents: 2
diff changeset
77 $file_rows .= "<td>".link_to("$link_base/".$file.$file_suffix, $file.$file_suffix)."</td>";
9ea8be5b28d1 "refactor".
edogawaconan <me@myconan.net>
parents: 2
diff changeset
78
9ea8be5b28d1 "refactor".
edogawaconan <me@myconan.net>
parents: 2
diff changeset
79 $file_rows .= "<td>";
2
9b5884ba214a Fix indentation.
edogawaconan <me@myconan.net>
parents: 0
diff changeset
80 if ($is_dir) { $file_rows .= "[dir]"; }
9b5884ba214a Fix indentation.
edogawaconan <me@myconan.net>
parents: 0
diff changeset
81 else { $file_rows .= nice_size($file_stat['size']); }
3
9ea8be5b28d1 "refactor".
edogawaconan <me@myconan.net>
parents: 2
diff changeset
82 $file_rows .= "</td>";
9ea8be5b28d1 "refactor".
edogawaconan <me@myconan.net>
parents: 2
diff changeset
83
9ea8be5b28d1 "refactor".
edogawaconan <me@myconan.net>
parents: 2
diff changeset
84 $file_rows .= "<td>".h(strftime("%Y-%m-%d %H:%M %Z", $file_stat['mtime']))."</td>";
9ea8be5b28d1 "refactor".
edogawaconan <me@myconan.net>
parents: 2
diff changeset
85
2
9b5884ba214a Fix indentation.
edogawaconan <me@myconan.net>
parents: 0
diff changeset
86 $file_rows .= "</tr>";
9b5884ba214a Fix indentation.
edogawaconan <me@myconan.net>
parents: 0
diff changeset
87 }
9b5884ba214a Fix indentation.
edogawaconan <me@myconan.net>
parents: 0
diff changeset
88 return $file_rows;
9b5884ba214a Fix indentation.
edogawaconan <me@myconan.net>
parents: 0
diff changeset
89 }
0
8cba59d07798 Initial import.
edogawaconan <me@myconan.net>
parents:
diff changeset
90 ?>
3
9ea8be5b28d1 "refactor".
edogawaconan <me@myconan.net>
parents: 2
diff changeset
91 <?php header('Content-Type: text/html; charset=utf-8'); ?>
9ea8be5b28d1 "refactor".
edogawaconan <me@myconan.net>
parents: 2
diff changeset
92 <!doctype html>
9ea8be5b28d1 "refactor".
edogawaconan <me@myconan.net>
parents: 2
diff changeset
93 <head>
9ea8be5b28d1 "refactor".
edogawaconan <me@myconan.net>
parents: 2
diff changeset
94 <meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
9ea8be5b28d1 "refactor".
edogawaconan <me@myconan.net>
parents: 2
diff changeset
95 <title><?php echo title(); ?></title>
9ea8be5b28d1 "refactor".
edogawaconan <me@myconan.net>
parents: 2
diff changeset
96 <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0">
9ea8be5b28d1 "refactor".
edogawaconan <me@myconan.net>
parents: 2
diff changeset
97 <style type="text/css">
9ea8be5b28d1 "refactor".
edogawaconan <me@myconan.net>
parents: 2
diff changeset
98 * { box-sizing: border-box; }
9ea8be5b28d1 "refactor".
edogawaconan <me@myconan.net>
parents: 2
diff changeset
99 body {
9ea8be5b28d1 "refactor".
edogawaconan <me@myconan.net>
parents: 2
diff changeset
100 font-family: Segoe UI, sans-serif;
9ea8be5b28d1 "refactor".
edogawaconan <me@myconan.net>
parents: 2
diff changeset
101 font-size: 14px;
9ea8be5b28d1 "refactor".
edogawaconan <me@myconan.net>
parents: 2
diff changeset
102 }
9ea8be5b28d1 "refactor".
edogawaconan <me@myconan.net>
parents: 2
diff changeset
103 h1 { margin: 5px; }
9ea8be5b28d1 "refactor".
edogawaconan <me@myconan.net>
parents: 2
diff changeset
104 table {
9ea8be5b28d1 "refactor".
edogawaconan <me@myconan.net>
parents: 2
diff changeset
105 width: 100%;
9ea8be5b28d1 "refactor".
edogawaconan <me@myconan.net>
parents: 2
diff changeset
106 }
9ea8be5b28d1 "refactor".
edogawaconan <me@myconan.net>
parents: 2
diff changeset
107 th:first-child, td:first-child {
9ea8be5b28d1 "refactor".
edogawaconan <me@myconan.net>
parents: 2
diff changeset
108 width: 100%;
9ea8be5b28d1 "refactor".
edogawaconan <me@myconan.net>
parents: 2
diff changeset
109 white-space: pre-wrap;
9ea8be5b28d1 "refactor".
edogawaconan <me@myconan.net>
parents: 2
diff changeset
110 word-wrap: break-word;
9ea8be5b28d1 "refactor".
edogawaconan <me@myconan.net>
parents: 2
diff changeset
111 word-break: break-all;
9ea8be5b28d1 "refactor".
edogawaconan <me@myconan.net>
parents: 2
diff changeset
112 }
9ea8be5b28d1 "refactor".
edogawaconan <me@myconan.net>
parents: 2
diff changeset
113 tr {
9ea8be5b28d1 "refactor".
edogawaconan <me@myconan.net>
parents: 2
diff changeset
114 position: relative;
9ea8be5b28d1 "refactor".
edogawaconan <me@myconan.net>
parents: 2
diff changeset
115 }
9ea8be5b28d1 "refactor".
edogawaconan <me@myconan.net>
parents: 2
diff changeset
116 th, td {
9ea8be5b28d1 "refactor".
edogawaconan <me@myconan.net>
parents: 2
diff changeset
117 white-space: nowrap;
9ea8be5b28d1 "refactor".
edogawaconan <me@myconan.net>
parents: 2
diff changeset
118 padding: 2px 5px;
9ea8be5b28d1 "refactor".
edogawaconan <me@myconan.net>
parents: 2
diff changeset
119 }
9ea8be5b28d1 "refactor".
edogawaconan <me@myconan.net>
parents: 2
diff changeset
120
9ea8be5b28d1 "refactor".
edogawaconan <me@myconan.net>
parents: 2
diff changeset
121 @media (min-width: 768px) {
9ea8be5b28d1 "refactor".
edogawaconan <me@myconan.net>
parents: 2
diff changeset
122 th { background: #ccc; }
9ea8be5b28d1 "refactor".
edogawaconan <me@myconan.net>
parents: 2
diff changeset
123 tr:nth-child(even) { background: #eee; }
9ea8be5b28d1 "refactor".
edogawaconan <me@myconan.net>
parents: 2
diff changeset
124 tr:hover { background: #ddd; }
9ea8be5b28d1 "refactor".
edogawaconan <me@myconan.net>
parents: 2
diff changeset
125 }
9ea8be5b28d1 "refactor".
edogawaconan <me@myconan.net>
parents: 2
diff changeset
126
9ea8be5b28d1 "refactor".
edogawaconan <me@myconan.net>
parents: 2
diff changeset
127 @media (max-width: 767px) {
9ea8be5b28d1 "refactor".
edogawaconan <me@myconan.net>
parents: 2
diff changeset
128 table {
9ea8be5b28d1 "refactor".
edogawaconan <me@myconan.net>
parents: 2
diff changeset
129 border-spacing: 0 10px;
9ea8be5b28d1 "refactor".
edogawaconan <me@myconan.net>
parents: 2
diff changeset
130 }
9ea8be5b28d1 "refactor".
edogawaconan <me@myconan.net>
parents: 2
diff changeset
131 th { display: none; }
9ea8be5b28d1 "refactor".
edogawaconan <me@myconan.net>
parents: 2
diff changeset
132 tr {
9ea8be5b28d1 "refactor".
edogawaconan <me@myconan.net>
parents: 2
diff changeset
133 background: #eee;
9ea8be5b28d1 "refactor".
edogawaconan <me@myconan.net>
parents: 2
diff changeset
134 }
9ea8be5b28d1 "refactor".
edogawaconan <me@myconan.net>
parents: 2
diff changeset
135 td {
9ea8be5b28d1 "refactor".
edogawaconan <me@myconan.net>
parents: 2
diff changeset
136 display: inline-block;
9ea8be5b28d1 "refactor".
edogawaconan <me@myconan.net>
parents: 2
diff changeset
137 }
9ea8be5b28d1 "refactor".
edogawaconan <me@myconan.net>
parents: 2
diff changeset
138 td:first-child {
9ea8be5b28d1 "refactor".
edogawaconan <me@myconan.net>
parents: 2
diff changeset
139 background: #ddd;
9ea8be5b28d1 "refactor".
edogawaconan <me@myconan.net>
parents: 2
diff changeset
140 padding: 5px;
9ea8be5b28d1 "refactor".
edogawaconan <me@myconan.net>
parents: 2
diff changeset
141 }
9ea8be5b28d1 "refactor".
edogawaconan <me@myconan.net>
parents: 2
diff changeset
142 table a {
9ea8be5b28d1 "refactor".
edogawaconan <me@myconan.net>
parents: 2
diff changeset
143 font-size: 18px;
9ea8be5b28d1 "refactor".
edogawaconan <me@myconan.net>
parents: 2
diff changeset
144 display: block;
9ea8be5b28d1 "refactor".
edogawaconan <me@myconan.net>
parents: 2
diff changeset
145 }
9ea8be5b28d1 "refactor".
edogawaconan <me@myconan.net>
parents: 2
diff changeset
146 }
9ea8be5b28d1 "refactor".
edogawaconan <me@myconan.net>
parents: 2
diff changeset
147 </style>
9ea8be5b28d1 "refactor".
edogawaconan <me@myconan.net>
parents: 2
diff changeset
148 </head>
9ea8be5b28d1 "refactor".
edogawaconan <me@myconan.net>
parents: 2
diff changeset
149 <body>
9ea8be5b28d1 "refactor".
edogawaconan <me@myconan.net>
parents: 2
diff changeset
150 <h1><?php echo path_tree_header(); ?></h1>
9ea8be5b28d1 "refactor".
edogawaconan <me@myconan.net>
parents: 2
diff changeset
151
9ea8be5b28d1 "refactor".
edogawaconan <me@myconan.net>
parents: 2
diff changeset
152 <table>
9ea8be5b28d1 "refactor".
edogawaconan <me@myconan.net>
parents: 2
diff changeset
153 <thead><tr>
9ea8be5b28d1 "refactor".
edogawaconan <me@myconan.net>
parents: 2
diff changeset
154 <th>File</th>
9ea8be5b28d1 "refactor".
edogawaconan <me@myconan.net>
parents: 2
diff changeset
155 <th>Size</th>
9ea8be5b28d1 "refactor".
edogawaconan <me@myconan.net>
parents: 2
diff changeset
156 <th>Date</th>
9ea8be5b28d1 "refactor".
edogawaconan <me@myconan.net>
parents: 2
diff changeset
157 </tr></thead>
9ea8be5b28d1 "refactor".
edogawaconan <me@myconan.net>
parents: 2
diff changeset
158 <tbody>
9ea8be5b28d1 "refactor".
edogawaconan <me@myconan.net>
parents: 2
diff changeset
159 <?php echo file_rows($dirs, true); ?>
9ea8be5b28d1 "refactor".
edogawaconan <me@myconan.net>
parents: 2
diff changeset
160 <?php echo file_rows($files, false); ?>
9ea8be5b28d1 "refactor".
edogawaconan <me@myconan.net>
parents: 2
diff changeset
161 </tbody>
9ea8be5b28d1 "refactor".
edogawaconan <me@myconan.net>
parents: 2
diff changeset
162 </table>
9ea8be5b28d1 "refactor".
edogawaconan <me@myconan.net>
parents: 2
diff changeset
163 </body>