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

s/pizza.slitaz.me/mypizza.slitaz.org/
author Pascal Bellard <pascal.bellard@slitaz.org>
date Sat Dec 14 17:16:32 2019 +0100 (2019-12-14)
parents 8f7ddeaffa3b
children b48416ef76bb
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;
129 // get_conf('dir-listing.exclude','\(','\)');
130 // get_conf('dir-listing.set-footer','"','"');
132 $mime_types = array();
133 foreach (explode(',', get_conf('mimetype.assign','\(','\)')) as $item) {
134 $filter = '/\s*"(.*)"\s*=>\s*"(.*)".*/';
135 $val = explode(',', preg_replace($filter, '$1,$2', $item));
136 if (isset($val[1]))
137 $mime_types[$val[0]] = $val[1];
138 }
141 //
142 // This function returns the mime type of $file.
143 //
144 function get_file_type($file) {
145 global $mime_types;
147 $file = basename($file);
148 $default_type = "application/octet-stream";
149 if (isset($mime_types[$file]))
150 return $mime_types[$file];
151 $pos = strrpos($file, ".");
152 if ($pos === false)
153 return $default_type;
154 //FIXME .tar.gz
155 $ext = '.' . rtrim(substr($file, $pos+1), "~");
156 if (isset($mime_types[$ext]))
157 return $mime_types[$ext];
158 return $default_type;
159 }
164 //$slitaz_style = (dirname($_SERVER["PHP_SELF"]) == '/');
165 //$slitaz_style = ($_SERVER["SERVER_NAME"] == "mirror1.slitaz.org");
166 $slitaz_style = preg_match("/mirror1\.slitaz\./", $_SERVER["SERVER_NAME"]);
168 if ($slitaz_style) {
169 // SliTaz Style
170 $modified = gmdate("D, d M Y H:i:s e", strtotime("-1 hour"));
171 $expires = gmdate("D, d M Y H:i:s e", strtotime("+1 hour"));
172 $fvalue = "";
173 if (isset($_GET['f']))
174 $fvalue = 'value="' . $_GET['f'] . '"';
175 header("Expires: " . $expires);
176 header("Last-Modified: " . $modified);
177 header("Pragma: cache");
178 // header("Cache-Control: public");
179 // <meta http-equiv="cache-control" content="public" />
180 // <meta http-equiv="last-modified" content="$modified" />
181 // <meta http-equiv="expires" content="$expires" />
182 print "
183 <!DOCTYPE html>
184 <html lang=\"en\">
185 <head>
186 <title>Index of /$vpath</title>
187 <meta charset=\"UTF-8\">
188 <meta name=\"description\" content=\"Index of /$vpath\">
189 ";
190 ?>
191 <meta name="robots" content="index, nofollow">
192 <meta name="author" content="SliTaz Contributors">
193 <meta name="viewport" content="width=device-width, initial-scale=1.0">
194 <link rel="shortcut icon" href="/static/favicon.ico">
195 <link rel="stylesheet" type="text/css" href="/static/slitaz.min.css">
196 </head>
197 <body>
199 <script>de=document.documentElement;de.className+=(("ontouchstart" in de)?' touch':' no-touch');</script>
201 <header>
202 <h1><a href="http://mirror1.slitaz.org/">SliTaz Mirror</a></h1>
203 <div class="network">
204 <a class="home" href="http://www.slitaz.org/"></a>
205 <a href="http://scn.slitaz.org/">Community</a>
206 <a href="http://doc.slitaz.org/">Doc</a>
207 <a href="http://forum.slitaz.org/">Forum</a>
208 <a href="http://pro.slitaz.org/">Pro</a>
209 <a href="https://slitaz.spreadshirt.net/">Shop</a>
210 <a href="http://bugs.slitaz.org">Bugs</a>
211 <a href="http://hg.slitaz.org/?sort=lastchange">Hg</a>
212 <a href="http://cook.slitaz.org/">Cook</a>
213 </div>
214 </header>
216 <div class="block"><div>
217 <!-- Information/image -->
218 <div class="block_info">
219 <header>Welcome to Open Source!</header>
220 <?php
222 if (preg_match("/mirror1\.slitaz\./", $_SERVER["SERVER_NAME"]))
223 { ?>
224 <p>This is the SliTaz GNU/Linux main mirror. The server runs naturally
225 SliTaz (stable) in an uml virtual machine provided by
226 <a href="http://www.ads-lu.com/">ADS</a> and is located in France.</p>
227 <p><a href="/info/">Mirror info...</a></p>
228 <?php
229 }
231 ?>
232 <form action="/" method="get">
233 <input type="search" name="f"/>
234 </form>
235 </div>
236 <!-- Navigation -->
237 <nav>
238 <header>Online Tools</header>
239 <ul>
240 <li><a href="http://mypizza.slitaz.org/">Live ISO Builder</a></li>
241 <li><a href="http://pizza.slitaz.org/">Live flavor Builder</a></li>
242 <li><a href="http://tiny.slitaz.org/">Tiny SliTaz Builder</a></li>
243 <li><a href="http://boot.slitaz.org/">Web Boot</a></li>
244 <li><a href="http://web.archive.org/web/*/http://mirror.slitaz.org">WebArchive</a></li>
245 </ul>
246 </nav>
247 </div></div>
249 <script>
250 function QRCodePNG(str, obj) {
251 try {
252 obj.height = obj.width += 200;
253 return QRCode.generatePNG(str, {ecclevel: 'H'});
254 }
255 catch (any) {
256 var element = document.createElement("script");
257 element.src = "/static/qrcode.min.js";
258 element.type ="text/javascript";
259 element.onload = function() {
260 obj.src = QRCode.generatePNG(str, {ecclevel: 'H'});
261 };
262 document.body.appendChild(element);
263 }
264 }
265 </script>
267 <div class="mirrors">
268 <?php
270 // Mirror list
271 $mirrors = array();
272 $fp = @fopen(dirname($_SERVER["SCRIPT_FILENAME"]) . "/mirrors.html", "r");
273 if ($fp) {
274 // Parse mirrors.html
275 while (($line = fgets($fp)) !== false) {
276 // string /" is the end of mirrors url
277 $fullline = str_replace('/"', "/" . $vpath . '"', $line);
278 print $fullline;
279 }
280 fclose($fp);
281 } else {
282 $fp = @fopen(dirname($_SERVER["SCRIPT_FILENAME"]) . "/mirrors", "r");
283 if ($fp) {
284 while (($line = fgets($fp)) !== false) {
285 $line = chop($line);
286 $url = parse_url($line);
287 if ($_SERVER["SERVER_NAME"] == $url['host'])
288 continue;
289 $host = explode('.', $url['host']);
290 $mirrors[$host[count($host)-2] . "." .
291 $host[count($host)-1]] = $line;
292 }
293 }
294 fclose($fp);
295 foreach($mirrors as $name => $url) {
296 print "<a href=\"$url$vpath\" title=\"$name mirror\">$name</a>\n";
297 }
298 }
300 print "</div>";
301 // end SliTaz Style
302 } else {
303 // not SliTaz Style
305 // Print the heading stuff
306 print "<?xml version='1.0' encoding='$encoding'?>
307 <!DOCTYPE html PUBLIC '-//W3C//DTD XHTML 1.1//EN' 'http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd'>
308 <html xmlns='http://www.w3.org/1999/xhtml' xml:lang='en'>
309 <head>
310 <title>Index of /$vpath</title>
311 ";
312 if ($external_css != '') {
313 print " <link rel='stylesheet' type='text/css' href='$external_css' />";
314 } else {
315 print "<style type='text/css'>
316 a, a:active {text-decoration: none; color: blue;}
317 a:visited {color: #48468F;}
318 a:hover, a:focus {text-decoration: underline; color: red;}
319 body {background-color: #F5F5F5;}
320 h2 {margin-bottom: 12px;}
321 table {margin-left: 12px;}
322 th, td {font: 90% monospace; text-align: left;}
323 th {font-weight: bold; padding-right: 14px; padding-bottom: 3px;}
324 td {padding-right: 14px;}
325 td.s, th.s {text-align: right;}
326 div.list {background-color: white; border-top: 1px solid #646464; border-bottom: 1px solid #646464; padding-top: 10px; padding-bottom: 14px;}
327 div.foot { font: 90% monospace; color: #787878; padding-top: 4px;}
328 </style>
329 ";
330 }
332 print " </head>
333 <body>
334 <h2>Index of /$vpath</h2>
335 ";
336 // end not SliTaz Style
337 }
342 print "<!-- Content -->
343 <main>
344 <div class='list'>
345 <div class='lang'>Path: /$vpath</div>
346 <table>";
349 function my_is_file($path) {
350 // 2G+ file support
351 exec("[ -f '" . $path . "' ]", $tmp, $ret);
352 return $ret == 0;
353 //return is_file($path);
354 }
357 function my_filesize($path) {
358 // 2G+ file support
359 return rtrim(shell_exec("stat -Lc %s '" . $path . "'"));
360 //return filesize($path);
361 }
364 function my_filemtime($path) {
365 // 2G+ file support
366 return rtrim(shell_exec("stat -Lc %Y '" . $path . "'"));
367 //return filemtime($path);
368 }
371 function my_filemtimeasc($path) {
372 // 2G+ file support
373 return rtrim(shell_exec("LC_ALL=C date -r '" . $path . "' '+%Y-%b-%d %H:%M:%S'"));
374 //return date('Y-M-d H:m:s', filemtime($path));
375 }
378 if (filesize($path . "/.folderlist") > 0 &&
379 filesize($path . "/.filelist") > 0 &&
380 filemtime($path . "/.filelist") > filemtime($path)) {
381 $folderlist = unserialize(file_get_contents($path . "/.folderlist"));
382 $filelist = unserialize(file_get_contents($path . "/.filelist"));
383 } else {
385 proc_nice(10);
386 // Get all of the folders and files.
387 $folderlist = array();
388 $filelist = array();
389 if($handle = @opendir($path)) {
390 while(($item = readdir($handle)) !== false) {
391 if ($item == "index.php") continue;
392 if ($item == ".folderlist") continue;
393 if ($item == ".filelist") continue;
394 if ($item == "dir-generator.php") continue;
395 if ($item == "robots.txt") continue;
396 if ($item == "humans.txt") continue;
397 if ($item == "mirrors.html") continue;
398 if (is_dir($path.'/'.$item) and $item != '.' and $item != '..') {
399 $folderlist[] = array(
400 'name' => $item,
401 'size' => 0,
402 'modtime'=> filemtime($path . '/' . $item),
403 'modtimeasc'=> my_filemtimeasc($path . '/' . $item),
404 'file_type' => "Directory"
405 );
406 } elseif (my_is_file($path . '/' . $item)) {
407 if (!$show_hidden_files) {
408 if (substr($item, 0, 1) == "." or substr($item, -1) == "~")
409 continue;
410 }
411 $filelist[] = array(
412 'name'=> $item,
413 'size'=> my_filesize($path . '/' . $item),
414 'modtime'=> my_filemtime($path . '/' . $item),
415 'modtimeasc'=> my_filemtimeasc($path . '/' . $item),
416 'file_type' => get_file_type($path . '/' . $item)
417 );
418 }
419 }
420 closedir($handle);
421 file_put_contents($path . "/.folderlist", serialize($folderlist), LOCK_EX);
422 file_put_contents($path . "/.filelist", serialize($filelist), LOCK_EX);
423 }
424 }
426 if (isset($_GET['f'])) {
427 $filter = $_GET['f'];
428 if (substr($filter, 0, 1) != '/')
429 $filter = '/' . $filter . '/i';
430 foreach ($filelist as $key => $value)
431 if (!preg_match($filter, $value['name']))
432 unset($filelist[$key]);
433 foreach ($folderlist as $key => $value)
434 if (!preg_match($filter, $value['name']))
435 unset($folderlist[$key]);
436 }
438 if (!isset($_GET['s']))
439 $_GET['s'] = 'name';
442 // Figure out what to sort files by
443 $file_order_by = array();
444 foreach ($filelist as $key => $row)
445 $file_order_by[$key] = $row[$_GET['s']];
448 // Figure out what to sort folders by
449 $folder_order_by = array();
450 foreach ($folderlist as $key => $row)
451 $folder_order_by[$key] = $row[$_GET['s']];
454 // Order the files and folders
455 $sort_type = SORT_ASC;
456 $order = "&amp;o=d";
457 if (isset($_GET['o'])) {
458 $sort_type = SORT_DESC;
459 $order = "";
460 }
461 array_multisort($folder_order_by, $sort_type, $folderlist);
462 array_multisort($file_order_by, $sort_type, $filelist);
465 // Table caption: number of folders and files
466 print "<caption>" . count($folderlist) . " folders and " . count($filelist) . " files.</caption>";
469 // Show sort methods
470 print "<thead><tr>";
472 $sort_methods = array();
473 $sort_methods['name'] = "Name";
474 $sort_methods['modtime'] = "Last Modified";
475 $sort_methods['size'] = "Size";
477 foreach($sort_methods as $key => $item) {
478 if ($_GET['s'] == $key)
479 $key = "$key$order";
480 print "<th><a href='?s=$key'>$item</a></th>";
481 }
482 print "</tr></thead>\n<tbody>\n";
487 // Parent directory link
488 if ($path != "./")
489 print "<tr><td class='up'><a href='..'>Parent Directory</a>/</td>" .
490 "<td>&nbsp;</td>" .
491 "<td>- &nbsp;</td></tr>\n";
496 // Print folder information
497 foreach($folderlist as $folder)
498 print "<tr><td class='dir'><a href='" . addslashes($folder['name']). "'>" .
499 htmlentities($folder['name']) . "</a>/</td>" .
500 "<td>" . $folder['modtimeasc'] . "</td>" .
501 "<td>- &nbsp;</td></tr>\n";
505 // Print file information
506 foreach($filelist as $file) {
507 $filename = $file['name'];
508 $url = addslashes($filename);
510 if (preg_match('/\.(tazpkg|deb)$/', $filename))
511 $class = "pkg";
512 elseif (preg_match('/\.iso$/', $filename))
513 $class = "iso";
514 elseif (preg_match('/\.(exe|com)$/', $filename))
515 $class = "exe";
516 elseif (preg_match('/^README$/', $filename))
517 $class = "rme";
518 elseif (preg_match('/^bzImage$/', $filename))
519 $class = "krn";
520 elseif (preg_match('/\.zip$/', $filename))
521 $class = "zip";
522 elseif (preg_match('/\.log$/', $filename))
523 $class = "log";
524 else {
525 $classes = explode('/', $file['file_type']);
526 $class = $classes[1];
527 }
530 print "<tr><td class='$class'><a href='$url'>" . htmlentities($filename) . "</a></td>" .
531 "<td>" . $file['modtimeasc'] .
532 " <img src='/static/qr.png' alt='#' " .
533 "onmouseover=\"this.title = location.href+'$url'\" " .
534 "onclick=\"this.src = QRCodePNG(location.href+'$url', this)\"/></td>" .
535 "<td>" . format_bytes($file['size']) . "</td></tr>\n";
536 }
538 // Print ending stuff
539 print " </tbody>
540 </table>
541 </div>";
543 $soft = explode('/', $_SERVER["SERVER_SOFTWARE"]);
544 $tag = get_conf('server.tag', '"', '"', $soft[0] . ' &lt;' . $soft[1] . '&gt;');
547 if (filesize($path . "/README"))
548 print "<pre>" .
549 preg_replace('!(((f|ht)tp(s)?://)[-a-zA-Z()0-9@:%_+.~#?&;//=]+)!i',
550 '<a href="$1">$1</a>', file_get_contents($path . "/README")) .
551 "</pre>\n";
555 if ($slitaz_style) {
556 // SliTaz Style
557 ?>
559 <!-- End of content -->
560 </main>
562 <footer>
563 <div>
564 Copyright &copy; <span class="year"></span>
565 <a href="http://www.slitaz.org/">SliTaz</a>
566 </div>
567 <div>
568 Network:
569 <a href="http://scn.slitaz.org/">Community</a> ·
570 <a href="http://doc.slitaz.org/">Doc</a> ·
571 <a href="http://forum.slitaz.org/">Forum</a> ·
572 <a href="http://pkgs.slitaz.org/">Packages</a> ·
573 <a href="http://bugs.slitaz.org">Bugs</a> ·
574 <a href="http://hg.slitaz.org/?sort=lastchange">Hg</a>
575 </div>
576 <div>
577 SliTaz @
578 <a href="http://twitter.com/slitaz">Twitter</a> ·
579 <a href="http://www.facebook.com/slitaz">Facebook</a> ·
580 <a href="http://distrowatch.com/slitaz">Distrowatch</a> ·
581 <a href="http://en.wikipedia.org/wiki/SliTaz">Wikipedia</a> ·
582 <a href="http://flattr.com/profile/slitaz">Flattr</a>
583 </div>
584 <img src="static/qr.png" alt="#" onmouseover="this.title = location.href"
585 onclick="this.src = QRCodePNG(location.href, this)"/>
586 </footer>
588 <?php
589 // end SliTaz Style
590 } else {
591 // not SliTaz Style
592 print "
593 <form action='" . $_SERVER["REQUEST_URI"] . "' method='get'>
594 <div class='foot'>" . $tag . "
595 <input type='text' name='f'/>
596 <!-- <input type='submit' value='Filter' /> -->
597 </div>
598 </form>
599 ";
600 // end not SliTaz Style
601 }
603 print "</body>
604 </html>";
605 ?>