slitaz-forge view mirror/root/dir-generator.php @ rev 348

mirror: add QR code
author Pascal Bellard <pascal.bellard@slitaz.org>
date Wed Mar 20 18:32:44 2013 +0100 (2013-03-20)
parents 0af8c6e40dcd
children 41bfd0397973
line source
1 <?php
2 function redirect()
3 {
4 ?>
5 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
6 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
7 <html xml:lang="en" xmlns="http://www.w3.org/1999/xhtml" lang="en">
8 <head>
9 <title>SliTaz mirror redirection</title>
10 <meta http-equiv="content-type" content="text/html; charset=ISO-8859-1" />
11 <meta name="description" content="slitaz mirror redirection" />
12 <meta name="robots" content="index, nofollow" />
13 <meta name="author" content="SliTaz Contributors" />
14 <meta http-equiv="Refresh" content="0;url=http://mirror.slitaz.org/">
15 </head>
16 <?php
17 }
19 $VERSION = "0.2-slitaz";
21 /* Lighttpd Enhanced Directory Listing Script
22 * ------------------------------------------
23 * Authors: Evan Fosmark <me@evanfosmark.com>,
24 * Pascal Bellard <pascal.bellard@slitaz.org>
25 * Christophe Lincoln <pankso@slitaz.org>
26 *
27 *
28 * GNU License Agreement
29 * ---------------------
30 * This program is free software; you can redistribute it and/or modify
31 * it under the terms of the GNU General Public License as published by
32 * the Free Software Foundation; either version 2 of the License, or
33 * (at your option) any later version.
34 *
35 * This program is distributed in the hope that it will be useful,
36 * but WITHOUT ANY WARRANTY; without even the implied warranty of
37 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
38 * GNU General Public License for more details.
39 *
40 * You should have received a copy of the GNU General Public License
41 * along with this program; if not, write to the Free Software
42 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
43 *
44 * http://www.gnu.org/licenses/gpl.txt
45 */
47 // Get the path (cut out the query string from the request_uri)
48 list($path) = explode('?', $_SERVER['REQUEST_URI']);
51 // Get the path that we're supposed to show.
52 $path = ltrim(rawurldecode($path), '/');
55 if(strlen($path) == 0) {
56 $path = "./";
57 }
60 // Can't call the script directly since REQUEST_URI won't be a directory
61 if($_SERVER['PHP_SELF'] == '/'.$path) {
62 redirect();
63 // die("Unable to call " . $path . " directly.");
64 }
67 $vpath = ($path != "./")?$path:"";
68 // Make sure it is valid.
69 if(!is_dir($path)) {
70 // die("<b>" . $path . "</b> is not a valid path.");
71 $path = dirname($_SERVER["SCRIPT_FILENAME"]);
72 list($vpath) = explode('?', $_SERVER['REQUEST_URI']);
73 $vpath = ltrim(rawurldecode($vpath), '/');
74 }
77 //
78 // This function returns the file size of a specified $file.
79 //
80 function format_bytes($size, $precision=1) {
81 $sizes = array('Y', 'Z', 'E', 'P', 'T', 'G', 'M', 'K', '');
82 $total = count($sizes);
84 while($total-- && $size > 1024) $size /= 1024;
85 if ($sizes[$total] == '') {
86 $size /= 1024;
87 $total--;
88 }
89 return sprintf('%.'.$precision.'f', $size).$sizes[$total];
90 }
92 //
93 // Get some variables from /etc/lighttpd/lighttpd.conf
94 //
95 $conf_lightty = file_get_contents("/etc/lighttpd/lighttpd.conf");
97 function get_conf($var,$start,$stop,$default='') {
98 global $conf_lightty;
100 if (!preg_match('/'.$var.'/',$conf_lightty)) return $default;
101 $filter = '/(.*\n)*'.$var.'\s*=\s*'.$start.'(([^'.$stop.']*\n*)*)'.$stop.'(.*\n)*/';
102 return preg_replace($filter,'$2',$conf_lightty);
103 }
105 $encoding = get_conf('dir-listing.encoding','"','"','ascii');
106 $external_css = get_conf('dir-listing.external-css','"','"');
108 $show_hidden_files = false;
109 if (get_conf('dir-listing.hide-dotfile','"','"','disable') == "disable") {
110 $show_hidden_files = true;
111 }
112 // get_conf('dir-listing.exclude','\(','\)');
113 // get_conf('dir-listing.set-footer','"','"');
115 $mime_types = array();
116 foreach (explode(',',get_conf('mimetype.assign','\(','\)')) as $item) {
117 $filter = '/\s*"(.*)"\s*=>\s*"(.*)".*/';
118 $val = explode(',',preg_replace($filter,'$1,$2',$item));
119 if (isset($val[1])) $mime_types[$val[0]] = $val[1];
120 }
122 //
123 // This function returns the mime type of $file.
124 //
125 function get_file_type($file) {
126 global $mime_types;
128 $file = basename($file);
129 $default_type = "application/octet-stream";
130 if (isset($mime_types[$file])) {
131 return $mime_types[$file];
132 }
133 $pos = strrpos($file, ".");
134 if ($pos === false) {
135 return $default_type;
136 }
137 //FIXME .tar.gz
138 $ext = '.'.rtrim(substr($file, $pos+1), "~");
139 if (isset($mime_types[$ext])) {
140 return $mime_types[$ext];
141 }
142 return $default_type;
143 }
145 //$slitaz_style = (dirname($_SERVER["PHP_SELF"]) == '/');
146 //$slitaz_style = ($_SERVER["SERVER_NAME"] == "mirror.slitaz.org");
147 $slitaz_style = preg_match("/mirror\.slitaz\./",$_SERVER["SERVER_NAME"]);
148 if ($slitaz_style) {
149 $fvalue = "";
150 if (isset($_GET['f'])) $fvalue = 'value="'.$_GET['f'].'"';
151 print <<<EOT
152 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
153 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
154 <html xml:lang="en" xmlns="http://www.w3.org/1999/xhtml" lang="en">
155 <head>
156 <title>Index of /$vpath</title>
157 <meta http-equiv="content-type" content="text/html; charset=ISO-8859-1" />
158 <meta name="description" content=">Index of /$vpath" />
159 <meta name="robots" content="index, nofollow" />
160 <meta name="author" content="SliTaz Contributors" />
161 <link rel="shortcut icon" href="/static/favicon.ico" />
162 <link rel="stylesheet" type="text/css" href="/static/slitaz.css" />
163 <style type="text/css">
164 #block { min-height: 160px; }
165 div.list { background-color: white; padding-bottom: 14px; }
166 th, td { font: 90% monospace; text-align: left; }
167 th { font-weight: bold; padding-right: 14px; padding-bottom: 3px; }
168 td { padding-right: 14px; }
169 td.s, th.s { text-align: right; }
170 </style>
171 </head>
172 <body>
174 <!-- Header -->
175 <div id="header">
176 <div id="logo"></div>
177 <div id="network">
178 <a href="http://www.slitaz.org/">
179 <img src="/static/home.png" alt="[ Home ]" /></a>
180 <a href="http://scn.slitaz.org/">Community</a>
181 <a href="http://doc.slitaz.org/">Doc</a>
182 <a href="http://forum.slitaz.org/">Forum</a>
183 <a href="http://pro.slitaz.org/">Pro</a>
184 <a href="https://slitaz.spreadshirt.net/">Shop</a>
185 <a href="http://bugs.slitaz.org">Bugs</a>
186 <a href="http://hg.slitaz.org/?sort=lastchange">Hg</a>
187 <a href="http://cook.slitaz.org/">Cook</a>
188 </div>
189 <h1><a href="http://mirror.slitaz.org/">SliTaz Mirror</a></h1>
190 </div>
192 <!-- Block -->
193 <div id="block">
194 <!-- Navigation -->
195 <div id="block_nav">
196 <h4>Online Tools</h4>
197 <ul>
198 <li><a href="http://pizza.slitaz.org/">Live Builder</a></li>
199 <li><a href="http://tiny.slitaz.org/">Tiny Builder</a></li>
200 <li><a href="http://boot.slitaz.org/">Web Boot</a></li>
201 </ul>
202 </div>
203 <!-- Information/image -->
204 <div id="block_info">
205 <h4>Welcome to Open Source!</h4>
206 EOT;
207 if (preg_match("/mirror\.slitaz\./",$_SERVER["SERVER_NAME"])) print <<<EOT
208 <p>This is the SliTaz GNU/Linux main mirror. The server runs naturally
209 SliTaz (stable) in an lguest virtual machine provided by
210 <a href="http://www.ads-lu.com/">ADS</a> and is located in France.
211 <a href="/info/">Mirror info...</a></p>
212 EOT;
213 print <<<EOT
214 <form action="${_SERVER["REQUEST_URI"]}" method="get" style="width: 300px;">
215 <p><input type="text" name="f" $fvalue
216 style="width: 260px;" /></p>
217 </form>
218 </div>
219 </div>
221 <div id="lang">
222 Path: /${vpath}
223 </div>
225 <!-- Content -->
226 <div id="content">
228 <div id="mirrors">
229 <img src="/static/network.png" alt="[ Mirrors ]" style="margin-right: 5px;" />
230 EOT;
232 // Mirror list
233 $mirrors = array();
234 $fp = @fopen(dirname($_SERVER["SCRIPT_FILENAME"])."/mirrors","r");
235 if ($fp) {
236 while (($line = fgets($fp)) !== false) {
237 $line = chop($line);
238 $url = parse_url($line);
239 if ($_SERVER["SERVER_NAME"] == $url['host']) continue;
240 $host = explode('.',$url['host']);
241 $mirrors[$host[count($host)-2].".".
242 $host[count($host)-1]] = $line;
243 }
244 }
245 fclose($fp);
246 foreach($mirrors as $name => $url) {
247 echo "<a href=\"$url$vpath\" title=\"$name mirror\">$name</a>\n";
248 }
249 echo "</div>";
250 }
251 else {
253 // Print the heading stuff
254 print "<?xml version='1.0' encoding='$encoding'?>
255 <!DOCTYPE html PUBLIC '-//W3C//DTD XHTML 1.1//EN' 'http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd'>
256 <html xmlns='http://www.w3.org/1999/xhtml' xml:lang='en'>
257 <head>
258 <title>Index of /" .$vpath. "</title>
259 ";
260 if ($external_css != '') {
261 print " <link rel='stylesheet' type='text/css' href='".$external_css."' />
262 ";
263 }
264 else {
265 print " <style type='text/css'>
266 a, a:active {text-decoration: none; color: blue;}
267 a:visited {color: #48468F;}
268 a:hover, a:focus {text-decoration: underline; color: red;}
269 body {background-color: #F5F5F5;}
270 h2 {margin-bottom: 12px;}
271 table {margin-left: 12px;}
272 th, td { font: 90% monospace; text-align: left;}
273 th { font-weight: bold; padding-right: 14px; padding-bottom: 3px;}
274 td {padding-right: 14px;}
275 td.s, th.s {text-align: right;}
276 div.list { background-color: white; border-top: 1px solid #646464; border-bottom: 1px solid #646464; padding-top: 10px; padding-bottom: 14px;}
277 div.foot { font: 90% monospace; color: #787878; padding-top: 4px;}
278 </style>
279 ";
280 }
281 print " </head>
282 <body>
283 <h2>Index of /" . $vpath ."</h2>
284 ";
285 }
287 print " <div class='list'>
288 <table summary='Directory Listing' cellpadding='0' cellspacing='0'>
289 ";
291 function my_is_file($path) // 2G+ file support
292 {
293 exec("[ -f '".$path."' ]", $tmp, $ret);
294 return $ret == 0;
295 //return is_file($path);
296 }
298 function my_filesize($path) // 2G+ file support
299 {
300 return rtrim(shell_exec("stat -Lc %s '".$path."'"));
301 //return filesize($path);
302 }
304 function my_filemtime($path) // 2G+ file support
305 {
306 return rtrim(shell_exec("stat -Lc %Y '".$path."'"));
307 //return filemtime($path);
308 }
310 function my_filemtimeasc($path) // 2G+ file support
311 {
312 return rtrim(shell_exec("LC_ALL=C date -r '".$path."' '+%Y-%b-%d %H:%M:%S'"));
313 //return date('Y-M-d H:m:s', filemtime($path));
314 }
316 if (filesize($path.".folderlist") > 0 && filesize($path.".filelist") > 0) {
317 $folderlist = unserialize(file_get_contents($path.".folderlist"));
318 $filelist = unserialize(file_get_contents($path.".filelist"));
319 }
320 else {
322 // Get all of the folders and files.
323 $folderlist = array();
324 $filelist = array();
325 if($handle = @opendir($path)) {
326 while(($item = readdir($handle)) !== false) {
327 if ($item == "index.php") continue;
328 if ($item == ".folderlist") continue;
329 if ($item == ".filelist") continue;
330 if ($item == "dir-generator.php") continue;
331 if(is_dir($path.'/'.$item) and $item != '.' and $item != '..') {
332 $folderlist[] = array(
333 'name' => $item,
334 'size' => 0,
335 'modtime'=> filemtime($path.'/'.$item),
336 'modtimeasc'=> my_filemtimeasc($path.'/'.$item),
337 'file_type' => "Directory"
338 );
339 }
340 elseif(my_is_file($path.'/'.$item)) {
341 if(!$show_hidden_files) {
342 if(substr($item, 0, 1) == "." or substr($item, -1) == "~") {
343 continue;
344 }
345 }
346 $filelist[] = array(
347 'name'=> $item,
348 'size'=> my_filesize($path.'/'.$item),
349 'modtime'=> my_filemtime($path.'/'.$item),
350 'modtimeasc'=> my_filemtimeasc($path.'/'.$item),
351 'file_type' => get_file_type($path.'/'.$item)
352 );
353 }
354 }
355 closedir($handle);
356 file_put_contents($path.".folderlist",serialize($folderlist),LOCK_EX);
357 file_put_contents($path.".filelist",serialize($filelist),LOCK_EX);
358 }
359 }
361 if (isset($_GET['f'])) {
362 $filter = $_GET['f'];
363 if (substr($filter,0,1) != '/')
364 $filter = '/'.$filter.'/i';
365 foreach ($filelist as $key => $value)
366 if (!preg_match($filter,$value['name']))
367 unset($filelist[$key]);
368 foreach ($folderlist as $key => $value)
369 if (!preg_match($filter,$value['name']))
370 unset($folderlist[$key]);
371 }
373 if(!isset($_GET['s'])) {
374 $_GET['s'] = 'name';
375 }
377 // Figure out what to sort files by
378 $file_order_by = array();
379 foreach ($filelist as $key=>$row) {
380 $file_order_by[$key] = $row[$_GET['s']];
381 }
383 // Figure out what to sort folders by
384 $folder_order_by = array();
385 foreach ($folderlist as $key=>$row) {
386 $folder_order_by[$key] = $row[$_GET['s']];
387 }
389 // Order the files and folders
390 $sort_type = SORT_ASC;
391 $order = "&amp;o=d";
392 if(isset($_GET['o'])) {
393 $sort_type = SORT_DESC;
394 $order = "";
395 }
396 array_multisort($folder_order_by, $sort_type, $folderlist);
397 array_multisort($file_order_by, $sort_type, $filelist);
400 // Show sort methods
401 print "<thead><tr>";
403 $sort_methods = array();
404 $sort_methods['name'] = "Name";
405 $sort_methods['modtime'] = "Last Modified";
406 $sort_methods['size'] = "Size";
407 $sort_methods['file_type'] = "Type";
409 foreach($sort_methods as $key=>$item) {
410 if ($_GET['s'] == $key) $key = "$key$order";
411 print "<th class='n'><a href='?s=$key'>$item</a></th>";
412 }
413 print "</tr></thead>\n<tbody>\n";
417 // Parent directory link
418 if($path != "./") {
419 print "<tr><td class='n'><a href='..'>Parent Directory</a>/</td>";
420 print "<td class='m'>&nbsp;</td>";
421 print "<td class='s'>- &nbsp;</td>";
422 print "<td class='t'>Directory</td></tr>\n";
423 }
427 // Print folder information
428 foreach($folderlist as $folder) {
429 print "<tr><td class='n'><a href='" . addslashes($folder['name']). "'>" .htmlentities($folder['name']). "</a>/</td>";
430 print "<td class='m'>" . $folder['modtimeasc'] . "</td>";
431 print "<td class='s'>- &nbsp;</td>";
432 print "<td class='t'>" . $folder['file_type'] . "</td></tr>\n";
433 }
436 // Print file information
437 foreach($filelist as $file) {
438 print "<tr><td class='n'><a href='" . addslashes($file['name']). "'>" .htmlentities($file['name']). "</a></td>";
439 print "<td class='m'>" . $file['modtimeasc'] . "</td>";
440 print "<td class='s'>" . format_bytes($file['size']) . "</td>";
441 print "<td class='t'>" . $file['file_type'] . "</td></tr>\n";
442 }
444 // Print ending stuff
445 $soft = explode('/',$_SERVER["SERVER_SOFTWARE"]);
446 $tag = get_conf('server.tag','"','"',$soft[0].' &lt;'.$soft[1].'&gt;');
447 $filenum = exec("cat $path/.filelist | sed 's|^a:||g' | sed 's|:{.*||g'");
448 $foldernum = exec("cat $path/.folderlist | sed 's|^a:||g' | sed 's|:{.*||g'");
449 print "</tbody>
450 </table>
451 ".$foldernum." folders and ".$filenum." files.</div>";
452 if ($slitaz_style) { ?>
454 <!-- End of content -->
455 </div>
457 <!-- Footer -->
458 <div id="footer">
459 Copyright &copy; <span class="year"></span>
460 <a href="http://www.slitaz.org/">SliTaz</a> - Network:
461 <a href="http://scn.slitaz.org/">Community</a>
462 <a href="http://doc.slitaz.org/">Doc</a>
463 <a href="http://forum.slitaz.org/">Forum</a>
464 <a href="http://pkgs.slitaz.org/">Packages</a>
465 <a href="http://bugs.slitaz.org">Bugs</a>
466 <a href="http://hg.slitaz.org/">Hg</a>
467 <p>
468 SliTaz @
469 <a href="http://twitter.com/slitaz">Twitter</a>
470 <a href="http://www.facebook.com/slitaz">Facebook</a>
471 <a href="http://distrowatch.com/slitaz">Distrowatch</a>
472 <a href="http://en.wikipedia.org/wiki/SliTaz">Wikipedia</a>
473 <a href="http://flattr.com/profile/slitaz">Flattr</a>
474 </p>
475 <p>
476 <a href="http://validator.w3.org/check?uri=referer">
477 <img src="/static/xhtml10.png"
478 alt="Valid XHTML 1.0" title="Code validé XHTML 1.0"
479 style="width: 80px; height: 15px;" /></a>
480 </p>
481 <p>
482 <img src="#" id="qrcodeimg" alt="#" width="60" height="60"
483 onclick= "this.width = this.height = 300;" />
484 <script type="text/javascript" src="/static/qrcode.js"></script>
485 <script type="text/javascript">
486 document.getElementById('qrcodeimg').src =
487 QRCode.generatePNG(location.href, {ecclevel: 'H'});
488 </script>
489 </p>
490 </div>
492 <?php }
493 else print "
494 <form action='".$_SERVER["REQUEST_URI"]."' method='get'>
495 <div class='foot'>".$tag."
496 <input type='text' name='f'/>
497 <!-- <input type='submit' value='Filter' /> -->
498 </div>
499 </form>
500 ";
501 print "</body>
502 </html>";
503 ?>