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

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