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

mirror: add support for small screens and touch screens, add icons for mirrors and file types, update mirrors list, minify resources.
author Aleksej Bobylev <al.bobylev@gmail.com>
date Fri Oct 02 01:31:59 2015 +0300 (2015-10-02)
parents fd82e00c58db
children dd8c986dfebf
line source
1 <?php
2 if (substr_count($_SERVER['HTTP_ACCEPT_ENCODING'], 'gzip'))
3 ob_start('ob_gzhandler');
4 else
5 ob_start();
7 function redirect() {
8 ?>
9 <!DOCTYPE html>
10 <html lang="en">
11 <head>
12 <title>SliTaz mirror redirection</title>
13 <meta charset="UTF-8">
14 <meta name="description" content="slitaz mirror redirection">
15 <meta name="robots" content="index, nofollow">
16 <meta name="author" content="SliTaz Contributors">
17 <meta http-equiv="Refresh" content="0;url=http://mirror1.slitaz.org/">
18 </head>
19 <body>
20 <script>window.location.replace('http://mirror1.slitaz.org/')</script>
21 <noscript>
22 <frameset rows="100%">
23 <frame src="http://mirror1.slitaz.org/">
24 <noframes>
25 <body>Please follow <a href="http://mirror1.slitaz.org/">this link</a>.</body>
26 </noframes>
27 </frameset>
28 </noscript>
29 </body>
30 </html>
31 <?php
32 }
34 $VERSION = "0.4-slitaz";
36 /* Lighttpd Enhanced Directory Listing Script
37 * ------------------------------------------
38 * Authors: Evan Fosmark <me@evanfosmark.com>,
39 * Pascal Bellard <pascal.bellard@slitaz.org>
40 * Christophe Lincoln <pankso@slitaz.org>
41 *
42 *
43 * GNU License Agreement
44 * ---------------------
45 * This program is free software; you can redistribute it and/or modify
46 * it under the terms of the GNU General Public License as published by
47 * the Free Software Foundation; either version 2 of the License, or
48 * (at your option) any later version.
49 *
50 * This program is distributed in the hope that it will be useful,
51 * but WITHOUT ANY WARRANTY; without even the implied warranty of
52 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
53 * GNU General Public License for more details.
54 *
55 * You should have received a copy of the GNU General Public License
56 * along with this program; if not, write to the Free Software
57 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
58 *
59 * http://www.gnu.org/licenses/gpl.txt
60 */
63 // Get the path (cut out the query string from the request_uri)
64 list($path) = explode('?', $_SERVER['REQUEST_URI']);
67 // Get the path that we're supposed to show.
68 $path = ltrim(rawurldecode($path), '/');
71 if(strlen($path) == 0)
72 $path = "./";
75 // Can't call the script directly since REQUEST_URI won't be a directory
76 if($_SERVER['PHP_SELF'] == '/' . $path) {
77 redirect();
78 // die("Unable to call " . $path . " directly.");
79 }
82 $vpath = ($path != "./") ? $path : "";
83 // Make sure it is valid.
84 if (!is_dir($path)) {
85 // die("<b>" . $path . "</b> is not a valid path.");
86 $path = dirname($_SERVER["SCRIPT_FILENAME"]);
87 list($vpath) = explode('?', $_SERVER['REQUEST_URI']);
88 $vpath = ltrim(rawurldecode($vpath), '/');
89 }
92 //
93 // This function returns the file size of a specified $file.
94 //
95 function format_bytes($size, $precision=1) {
96 $sizes = array('Y', 'Z', 'E', 'P', 'T', 'G', 'M', 'K', '');
97 $total = count($sizes);
99 while ($total-- && $size > 1024)
100 $size /= 1024;
101 if ($sizes[$total] == '') {
102 $size /= 1024;
103 $total--;
104 }
105 return sprintf('%.' . $precision . 'f', $size) . $sizes[$total];
106 }
109 //
110 // Get some variables from /etc/lighttpd/lighttpd.conf
111 //
112 $conf_lightty = file_get_contents("/etc/lighttpd/lighttpd.conf");
114 function get_conf($var, $start, $stop, $default='') {
115 global $conf_lightty;
117 if (!preg_match('/' . $var . '/', $conf_lightty))
118 return $default;
119 $filter = '/(.*\n)*' . $var . '\s*=\s*' . $start . '(([^' . $stop . ']*\n*)*)' . $stop . '(.*\n)*/';
120 return preg_replace($filter, '$2', $conf_lightty);
121 }
123 $encoding = get_conf('dir-listing.encoding', '"', '"', 'ascii');
124 $external_css = get_conf('dir-listing.external-css', '"', '"');
126 $show_hidden_files = false;
127 if (get_conf('dir-listing.hide-dotfile', '"', '"', 'disable') == "disable")
128 $show_hidden_files = true;
130 // get_conf('dir-listing.exclude','\(','\)');
131 // get_conf('dir-listing.set-footer','"','"');
133 $mime_types = array();
134 foreach (explode(',', get_conf('mimetype.assign','\(','\)')) as $item) {
135 $filter = '/\s*"(.*)"\s*=>\s*"(.*)".*/';
136 $val = explode(',', preg_replace($filter, '$1,$2', $item));
137 if (isset($val[1]))
138 $mime_types[$val[0]] = $val[1];
139 }
142 //
143 // This function returns the mime type of $file.
144 //
145 function get_file_type($file) {
146 global $mime_types;
148 $file = basename($file);
149 $default_type = "application/octet-stream";
150 if (isset($mime_types[$file]))
151 return $mime_types[$file];
152 $pos = strrpos($file, ".");
153 if ($pos === false)
154 return $default_type;
155 //FIXME .tar.gz
156 $ext = '.' . rtrim(substr($file, $pos+1), "~");
157 if (isset($mime_types[$ext]))
158 return $mime_types[$ext];
159 return $default_type;
160 }
165 //$slitaz_style = (dirname($_SERVER["PHP_SELF"]) == '/');
166 //$slitaz_style = ($_SERVER["SERVER_NAME"] == "mirror1.slitaz.org");
167 $slitaz_style = preg_match("/mirror1\.slitaz\./", $_SERVER["SERVER_NAME"]);
169 if ($slitaz_style) {
170 // SliTaz Style
171 $modified = gmdate("D, d M Y H:i:s e", strtotime("-1 hour"));
172 $expires = gmdate("D, d M Y H:i:s e", strtotime("+1 hour"));
173 $fvalue = "";
174 if (isset($_GET['f']))
175 $fvalue = 'value="' . $_GET['f'] . '"';
176 header("Expires: " . $expires);
177 header("Last-Modified: " . $modified);
178 header("Pragma: cache");
179 // header("Cache-Control: public");
180 // <meta http-equiv="cache-control" content="public" />
181 // <meta http-equiv="last-modified" content="$modified" />
182 // <meta http-equiv="expires" content="$expires" />
183 ?>
184 <!DOCTYPE html>
185 <html lang="en">
186 <head>
187 <title>Index of /$vpath</title>
188 <meta charset="UTF-8">
189 <meta name="description" content="Index of /$vpath">
190 <meta name="robots" content="index, nofollow">
191 <meta name="author" content="SliTaz Contributors">
192 <meta name="viewport" content="width=device-width, initial-scale=1.0">
193 <link rel="shortcut icon" href="/static/favicon.ico">
194 <link rel="stylesheet" type="text/css" href="/static/slitaz.min.css">
195 </head>
196 <body>
198 <header>
199 <h1><a href="http://mirror1.slitaz.org/">SliTaz Mirror</a></h1>
200 <div id="network">
201 <a class="home" href="http://www.slitaz.org/"></a>
202 <a href="http://scn.slitaz.org/">Community</a>
203 <a href="http://doc.slitaz.org/">Doc</a>
204 <a href="http://forum.slitaz.org/">Forum</a>
205 <a href="http://pro.slitaz.org/">Pro</a>
206 <a href="https://slitaz.spreadshirt.net/">Shop</a>
207 <a href="http://bugs.slitaz.org">Bugs</a>
208 <a href="http://hg.slitaz.org/?sort=lastchange">Hg</a>
209 <a href="http://cook.slitaz.org/">Cook</a>
210 </div>
211 </header>
213 <div id="block"><div>
214 <!-- Information/image -->
215 <div id="block_info">
216 <header>Welcome to Open Source!</header>
217 <?php
219 if (preg_match("/mirror1\.slitaz\./", $_SERVER["SERVER_NAME"]))
220 { ?>
221 <p>This is the SliTaz GNU/Linux main mirror. The server runs naturally
222 SliTaz (stable) in an uml virtual machine provided by
223 <a href="http://www.ads-lu.com/">ADS</a> and is located in France.</p>
224 <p><a href="/info/">Mirror info...</a></p>
225 <?php
226 }
228 ?>
229 <form action="/" method="get">
230 <input type="search" name="f"/>
231 </form>
232 </div>
233 <!-- Navigation -->
234 <nav>
235 <header>Online Tools</header>
236 <ul>
237 <li><a href="http://pizza.slitaz.me/">Live ISO Builder</a></li>
238 <li><a href="http://pizza.slitaz.org/">Live flavor Builder</a></li>
239 <li><a href="http://tiny.slitaz.org/">Tiny SliTaz Builder</a></li>
240 <li><a href="http://boot.slitaz.org/">Web Boot</a></li>
241 <li><a href="http://web.archive.org/web/*/http://mirror.slitaz.org">WebArchive</a></li>
242 </ul>
243 </nav>
244 </div></div>
246 <script>
247 function QRCodePNG(str, obj) {
248 try {
249 obj.height = obj.width += 200;
250 return QRCode.generatePNG(str, {ecclevel: 'H'});
251 }
252 catch (any) {
253 var element = document.createElement("script");
254 element.src = "/static/qrcode.min.js";
255 element.type ="text/javascript";
256 element.onload = function() {
257 obj.src = QRCode.generatePNG(str, {ecclevel: 'H'});
258 };
259 document.body.appendChild(element);
260 }
261 }
263 document.documentElement.className += (("ontouchstart" in document.documentElement) ? ' touch' : ' no-touch');
264 </script>
266 <!-- Content -->
267 <div id="content">
268 <div id="mirrors">
269 <?php
271 // Mirror list
272 $mirrors = array();
273 $fp = @fopen(dirname($_SERVER["SCRIPT_FILENAME"]) . "/mirrors.html", "r");
274 if ($fp) {
275 // Parse mirrors.html
276 while (($line = fgets($fp)) !== false) {
277 // string /" is the end of mirrors url
278 $fullline = str_replace('/"', "/" . $vpath . '"', $line);
279 print $fullline;
280 }
281 fclose($fp);
282 } else {
283 $fp = @fopen(dirname($_SERVER["SCRIPT_FILENAME"]) . "/mirrors", "r");
284 if ($fp) {
285 while (($line = fgets($fp)) !== false) {
286 $line = chop($line);
287 $url = parse_url($line);
288 if ($_SERVER["SERVER_NAME"] == $url['host'])
289 continue;
290 $host = explode('.', $url['host']);
291 $mirrors[$host[count($host)-2] . "." .
292 $host[count($host)-1]] = $line;
293 }
294 }
295 fclose($fp);
296 foreach($mirrors as $name => $url) {
297 print "<a href=\"$url$vpath\" title=\"$name mirror\">$name</a>\n";
298 }
299 }
301 print "</div>";
302 // end SliTaz Style
303 } else {
304 // not SliTaz Style
306 // Print the heading stuff
307 print "<?xml version='1.0' encoding='$encoding'?>
308 <!DOCTYPE html PUBLIC '-//W3C//DTD XHTML 1.1//EN' 'http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd'>
309 <html xmlns='http://www.w3.org/1999/xhtml' xml:lang='en'>
310 <head>
311 <title>Index of /$vpath</title>
312 ";
313 if ($external_css != '') {
314 print " <link rel='stylesheet' type='text/css' href='$external_css' />";
315 } else {
316 print "<style type='text/css'>
317 a, a:active {text-decoration: none; color: blue;}
318 a:visited {color: #48468F;}
319 a:hover, a:focus {text-decoration: underline; color: red;}
320 body {background-color: #F5F5F5;}
321 h2 {margin-bottom: 12px;}
322 table {margin-left: 12px;}
323 th, td {font: 90% monospace; text-align: left;}
324 th {font-weight: bold; padding-right: 14px; padding-bottom: 3px;}
325 td {padding-right: 14px;}
326 td.s, th.s {text-align: right;}
327 div.list {background-color: white; border-top: 1px solid #646464; border-bottom: 1px solid #646464; padding-top: 10px; padding-bottom: 14px;}
328 div.foot { font: 90% monospace; color: #787878; padding-top: 4px;}
329 </style>
330 ";
331 }
333 print " </head>
334 <body>
335 <h2>Index of /$vpath</h2>
336 ";
337 // end not SliTaz Style
338 }
343 print " <div class='list'>
344 <div id='lang'>Path: /$vpath</div>
345 <table>";
348 function my_is_file($path) {
349 // 2G+ file support
350 exec("[ -f '" . $path . "' ]", $tmp, $ret);
351 return $ret == 0;
352 //return is_file($path);
353 }
356 function my_filesize($path) {
357 // 2G+ file support
358 return rtrim(shell_exec("stat -Lc %s '" . $path . "'"));
359 //return filesize($path);
360 }
363 function my_filemtime($path) {
364 // 2G+ file support
365 return rtrim(shell_exec("stat -Lc %Y '" . $path . "'"));
366 //return filemtime($path);
367 }
370 function my_filemtimeasc($path) {
371 // 2G+ file support
372 return rtrim(shell_exec("LC_ALL=C date -r '" . $path . "' '+%Y-%b-%d %H:%M:%S'"));
373 //return date('Y-M-d H:m:s', filemtime($path));
374 }
377 if (filesize($path . "/.folderlist") > 0 &&
378 filesize($path . "/.filelist") > 0 &&
379 filemtime($path . "/.filelist") > filemtime($path)) {
380 $folderlist = unserialize(file_get_contents($path . "/.folderlist"));
381 $filelist = unserialize(file_get_contents($path . "/.filelist"));
382 } else {
384 proc_nice(10);
385 // Get all of the folders and files.
386 $folderlist = array();
387 $filelist = array();
388 if($handle = @opendir($path)) {
389 while(($item = readdir($handle)) !== false) {
390 if ($item == "index.php") continue;
391 if ($item == ".folderlist") continue;
392 if ($item == ".filelist") continue;
393 if ($item == "dir-generator.php") continue;
394 if ($item == "robots.txt") continue;
395 if ($item == "humans.txt") continue;
396 if ($item == "mirrors.html") continue;
397 if (is_dir($path.'/'.$item) and $item != '.' and $item != '..') {
398 $folderlist[] = array(
399 'name' => $item,
400 'size' => 0,
401 'modtime'=> filemtime($path . '/' . $item),
402 'modtimeasc'=> my_filemtimeasc($path . '/' . $item),
403 'file_type' => "Directory"
404 );
405 } elseif (my_is_file($path . '/' . $item)) {
406 if (!$show_hidden_files) {
407 if (substr($item, 0, 1) == "." or substr($item, -1) == "~")
408 continue;
409 }
410 $filelist[] = array(
411 'name'=> $item,
412 'size'=> my_filesize($path . '/' . $item),
413 'modtime'=> my_filemtime($path . '/' . $item),
414 'modtimeasc'=> my_filemtimeasc($path . '/' . $item),
415 'file_type' => get_file_type($path . '/' . $item)
416 );
417 }
418 }
419 closedir($handle);
420 file_put_contents($path . "/.folderlist", serialize($folderlist), LOCK_EX);
421 file_put_contents($path . "/.filelist", serialize($filelist), LOCK_EX);
422 }
423 }
425 if (isset($_GET['f'])) {
426 $filter = $_GET['f'];
427 if (substr($filter, 0, 1) != '/')
428 $filter = '/' . $filter . '/i';
429 foreach ($filelist as $key => $value)
430 if (!preg_match($filter, $value['name']))
431 unset($filelist[$key]);
432 foreach ($folderlist as $key => $value)
433 if (!preg_match($filter, $value['name']))
434 unset($folderlist[$key]);
435 }
437 if (!isset($_GET['s']))
438 $_GET['s'] = 'name';
441 // Figure out what to sort files by
442 $file_order_by = array();
443 foreach ($filelist as $key => $row)
444 $file_order_by[$key] = $row[$_GET['s']];
447 // Figure out what to sort folders by
448 $folder_order_by = array();
449 foreach ($folderlist as $key => $row)
450 $folder_order_by[$key] = $row[$_GET['s']];
453 // Order the files and folders
454 $sort_type = SORT_ASC;
455 $order = "&amp;o=d";
456 if (isset($_GET['o'])) {
457 $sort_type = SORT_DESC;
458 $order = "";
459 }
460 array_multisort($folder_order_by, $sort_type, $folderlist);
461 array_multisort($file_order_by, $sort_type, $filelist);
464 // Table caption: number of folders and files
465 print "<caption>" . count($folderlist) . " folders and " . count($filelist) . " files.</caption>";
468 // Show sort methods
469 print "<thead><tr>";
471 $sort_methods = array();
472 $sort_methods['name'] = "Name";
473 $sort_methods['modtime'] = "Last Modified";
474 $sort_methods['size'] = "Size";
476 foreach($sort_methods as $key => $item) {
477 if ($_GET['s'] == $key)
478 $key = "$key$order";
479 print "<th><a href='?s=$key'>$item</a></th>";
480 }
481 print "</tr></thead>\n<tbody>\n";
486 // Parent directory link
487 if ($path != "./")
488 print "<tr><td class='up'><a href='..'>Parent Directory</a>/</td>" .
489 "<td>&nbsp;</td>" .
490 "<td>- &nbsp;</td></tr>\n";
495 // Print folder information
496 foreach($folderlist as $folder)
497 print "<tr><td class='dir'><a href='" . addslashes($folder['name']). "'>" .
498 htmlentities($folder['name']) . "</a>/</td>" .
499 "<td" . $folder['modtimeasc'] . "</td>" .
500 "<td>- &nbsp;</td></tr>\n";
504 // Print file information
505 foreach($filelist as $file) {
506 $url = addslashes($file['name']);
508 if (preg_match('/\.tazpkg$/', $file))
509 $class = "pkg";
510 elseif (preg_match('/\.iso$/', $file))
511 $class = "iso";
512 elseif (preg_match('/\.(exe|com)$/', $file))
513 $class = "exe";
514 elseif (preg_match('/^README$/', $file))
515 $class = "rme";
516 elseif (preg_match('/^bzImage$/', $file))
517 $class = "krn";
518 elseif (preg_match('/\.zip$/', $file))
519 $class = "zip";
520 elseif (preg_match('/\.log$/', $file))
521 $class = "log";
522 else {
523 $classes = explode('/', $file['file_type']);
524 $class = $classes[1];
525 }
528 print "<tr><td class='$class'><a href='$url'>" . htmlentities($file['name']) . "</a></td>" .
529 "<td>" . $file['modtimeasc'] .
530 " <img src='/static/qr.png' alt='#' " .
531 "onmouseover=\"this.title = location.href+'$url'\" " .
532 "onclick=\"this.src = QRCodePNG(location.href+'$url', this)\"/></td>" .
533 "<td>" . format_bytes($file['size']) . "</td></tr>\n";
534 }
536 // Print ending stuff
537 print " </tbody>
538 </table>
539 </div>";
541 $soft = explode('/', $_SERVER["SERVER_SOFTWARE"]);
542 $tag = get_conf('server.tag', '"', '"', $soft[0] . ' &lt;' . $soft[1] . '&gt;');
545 if (filesize($path . "/README"))
546 print "<pre>" .
547 preg_replace('!(((f|ht)tp(s)?://)[-a-zA-Z()0-9@:%_+.~#?&;//=]+)!i',
548 '<a href="$1">$1</a>', file_get_contents($path . "/README")) .
549 "</pre>\n";
553 if ($slitaz_style) {
554 // SliTaz Style
555 ?>
557 <!-- End of content -->
558 </div>
560 <footer>
561 <p>Copyright &copy; <span class="year"></span> <a href="http://www.slitaz.org/">SliTaz</a></p>
562 <p>Network:
563 <a href="http://scn.slitaz.org/">Community</a>
564 <a href="http://doc.slitaz.org/">Doc</a>
565 <a href="http://forum.slitaz.org/">Forum</a>
566 <a href="http://pkgs.slitaz.org/">Packages</a>
567 <a href="http://bugs.slitaz.org">Bugs</a>
568 <a href="http://hg.slitaz.org/?sort=lastchange">Hg</a>
569 </p>
570 <p>SliTaz @
571 <a href="http://twitter.com/slitaz">Twitter</a>
572 <a href="http://www.facebook.com/slitaz">Facebook</a>
573 <a href="http://distrowatch.com/slitaz">Distrowatch</a>
574 <a href="http://en.wikipedia.org/wiki/SliTaz">Wikipedia</a>
575 <a href="http://flattr.com/profile/slitaz">Flattr</a>
576 </p>
577 <img src="static/qr.png" alt="#" onmouseover="this.title = location.href"
578 onclick="this.src = QRCodePNG(location.href, this)" />
579 </footer>
581 <?php
582 // end SliTaz Style
583 } else {
584 // not SliTaz Style
585 print "
586 <form action='" . $_SERVER["REQUEST_URI"] . "' method='get'>
587 <div class='foot'>" . $tag . "
588 <input type='text' name='f'/>
589 <!-- <input type='submit' value='Filter' /> -->
590 </div>
591 </form>
592 ";
593 // end not SliTaz Style
594 }
596 print "</body>
597 </html>";
598 ?>