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

Prevent sending the Referer header
author Pascal Bellard <pascal.bellard@slitaz.org>
date Thu Aug 20 08:44:15 2020 +0000 (2020-08-20)
parents 17ab49508a9f
children 9e7953989d74
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"]);
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('Referrer-policy: "no-referrer"');
179 header("Pragma: cache");
180 // header("Cache-Control: public");
181 // <meta http-equiv="cache-control" content="public" />
182 // <meta http-equiv="last-modified" content="$modified" />
183 // <meta http-equiv="expires" content="$expires" />
184 print "
185 <!DOCTYPE html>
186 <html lang=\"en\">
187 <head>
188 <title>Index of /$vpath</title>
189 <meta charset=\"UTF-8\">
190 <meta name=\"description\" content=\"Index of /$vpath\">
191 ";
192 ?>
193 <meta name="robots" content="index, nofollow">
194 <meta name="referrer" content="no-referrer">
195 <meta name="author" content="SliTaz Contributors">
196 <meta name="viewport" content="width=device-width, initial-scale=1.0">
197 <link rel="shortcut icon" href="/static/favicon.ico">
198 <link rel="stylesheet" type="text/css" href="/static/slitaz.min.css">
199 </head>
200 <body>
202 <script>de=document.documentElement;de.className+=(("ontouchstart" in de)?' touch':' no-touch');</script>
204 <header>
205 <h1><a href="http://mirror1.slitaz.org/">SliTaz Mirror</a></h1>
206 <div class="network">
207 <a class="home" href="http://www.slitaz.org/"></a>
208 <a href="http://scn.slitaz.org/">Community</a>
209 <a href="http://doc.slitaz.org/">Doc</a>
210 <a href="http://forum.slitaz.org/">Forum</a>
211 <a href="http://pro.slitaz.org/">Pro</a>
212 <a href="https://slitaz.spreadshirt.net/">Shop</a>
213 <a href="http://bugs.slitaz.org">Bugs</a>
214 <a href="http://hg.slitaz.org/?sort=lastchange">Hg</a>
215 <a href="http://cook.slitaz.org/">Cook</a>
216 </div>
217 </header>
219 <div class="block"><div>
220 <!-- Information/image -->
221 <div class="block_info">
222 <header>Welcome to Open Source!</header>
223 <?php
225 if (preg_match("/mirror1\.slitaz\./", $_SERVER["SERVER_NAME"]))
226 { ?>
227 <p>This is the SliTaz GNU/Linux main mirror. The server runs naturally
228 SliTaz (stable) in an uml virtual machine provided by
229 <a href="http://www.ads-lu.com/">ADS</a> and is located in France.</p>
230 <p><a href="/info/">Mirror info...</a></p>
231 <?php
232 }
234 ?>
235 <form action="/" method="get">
236 <input type="search" name="f"/>
237 </form>
238 </div>
239 <!-- Navigation -->
240 <nav>
241 <header>Online Tools</header>
242 <ul>
243 <li><a href="http://mypizza.slitaz.org/">Live ISO Builder</a></li>
244 <li><a href="http://pizza.slitaz.org/">Live flavor Builder</a></li>
245 <li><a href="http://tiny.slitaz.org/">Tiny SliTaz Builder</a></li>
246 <li><a href="http://boot.slitaz.org/">Web Boot</a></li>
247 <li><a href="http://web.archive.org/web/*/http://mirror.slitaz.org">WebArchive</a></li>
248 </ul>
249 </nav>
250 </div></div>
252 <script>
253 function QRCodePNG(str, obj) {
254 try {
255 obj.height = obj.width += 200;
256 return QRCode.generatePNG(str, {ecclevel: 'H'});
257 }
258 catch (any) {
259 var element = document.createElement("script");
260 element.src = "/static/qrcode.min.js";
261 element.type ="text/javascript";
262 element.onload = function() {
263 obj.src = QRCode.generatePNG(str, {ecclevel: 'H'});
264 };
265 document.body.appendChild(element);
266 }
267 }
268 </script>
270 <div class="mirrors">
271 <?php
273 // Mirror list
274 $mirrors = array();
275 $fp = @fopen(dirname($_SERVER["SCRIPT_FILENAME"]) . "/mirrors.html", "r");
276 if ($fp) {
277 // Parse mirrors.html
278 while (($line = fgets($fp)) !== false) {
279 // string /" is the end of mirrors url
280 $fullline = str_replace('/"', "/" . $vpath . '"', $line);
281 print $fullline;
282 }
283 fclose($fp);
284 } else {
285 $fp = @fopen(dirname($_SERVER["SCRIPT_FILENAME"]) . "/mirrors", "r");
286 if ($fp) {
287 while (($line = fgets($fp)) !== false) {
288 $line = chop($line);
289 $url = parse_url($line);
290 if ($_SERVER["SERVER_NAME"] == $url['host'])
291 continue;
292 $host = explode('.', $url['host']);
293 $mirrors[$host[count($host)-2] . "." .
294 $host[count($host)-1]] = $line;
295 }
296 }
297 fclose($fp);
298 foreach($mirrors as $name => $url) {
299 print "<a href=\"$url$vpath\" title=\"$name mirror\">$name</a>\n";
300 }
301 }
303 print "</div>";
304 // end SliTaz Style
305 } else {
306 // not SliTaz Style
308 // Print the heading stuff
309 print "<?xml version='1.0' encoding='$encoding'?>
310 <!DOCTYPE html PUBLIC '-//W3C//DTD XHTML 1.1//EN' 'http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd'>
311 <html xmlns='http://www.w3.org/1999/xhtml' xml:lang='en'>
312 <head>
313 <title>Index of /$vpath</title>
314 ";
315 if ($external_css != '') {
316 print " <link rel='stylesheet' type='text/css' href='$external_css' />";
317 } else {
318 print "<style type='text/css'>
319 a, a:active {text-decoration: none; color: blue;}
320 a:visited {color: #48468F;}
321 a:hover, a:focus {text-decoration: underline; color: red;}
322 body {background-color: #F5F5F5;}
323 h2 {margin-bottom: 12px;}
324 table {margin-left: 12px;}
325 th, td {font: 90% monospace; text-align: left;}
326 th {font-weight: bold; padding-right: 14px; padding-bottom: 3px;}
327 td {padding-right: 14px;}
328 td.s, th.s {text-align: right;}
329 div.list {background-color: white; border-top: 1px solid #646464; border-bottom: 1px solid #646464; padding-top: 10px; padding-bottom: 14px;}
330 div.foot { font: 90% monospace; color: #787878; padding-top: 4px;}
331 </style>
332 ";
333 }
335 print " </head>
336 <body>
337 <h2>Index of /$vpath</h2>
338 ";
339 // end not SliTaz Style
340 }
345 print "<!-- Content -->
346 <main>
347 <div class='list'>
348 <div class='lang'>Path: /$vpath</div>
349 <table>";
352 function my_is_file($path) {
353 // 2G+ file support
354 exec("[ -f '" . $path . "' ]", $tmp, $ret);
355 return $ret == 0;
356 //return is_file($path);
357 }
360 function my_filesize($path) {
361 // 2G+ file support
362 return rtrim(shell_exec("stat -Lc %s '" . $path . "'"));
363 //return filesize($path);
364 }
367 function my_filemtime($path) {
368 // 2G+ file support
369 return rtrim(shell_exec("stat -Lc %Y '" . $path . "'"));
370 //return filemtime($path);
371 }
374 function my_filemtimeasc($path) {
375 // 2G+ file support
376 return rtrim(shell_exec("LC_ALL=C date -r '" . $path . "' '+%Y-%b-%d %H:%M:%S'"));
377 //return date('Y-M-d H:m:s', filemtime($path));
378 }
381 if (filesize($path . "/.folderlist") > 0 &&
382 filesize($path . "/.filelist") > 0 &&
383 filemtime($path . "/.filelist") > filemtime($path)) {
384 $folderlist = unserialize(file_get_contents($path . "/.folderlist"));
385 $filelist = unserialize(file_get_contents($path . "/.filelist"));
386 } else {
388 proc_nice(10);
389 // Get all of the folders and files.
390 $folderlist = array();
391 $filelist = array();
392 if($handle = @opendir($path)) {
393 while(($item = readdir($handle)) !== false) {
394 if ($item == "index.php") continue;
395 if ($item == ".folderlist") continue;
396 if ($item == ".filelist") continue;
397 if ($item == "dir-generator.php") continue;
398 if ($item == "robots.txt") continue;
399 if ($item == "humans.txt") continue;
400 if ($item == "mirrors.html") continue;
401 if (is_dir($path.'/'.$item) and $item != '.' and $item != '..') {
402 $folderlist[] = array(
403 'name' => $item,
404 'size' => 0,
405 'modtime'=> filemtime($path . '/' . $item),
406 'modtimeasc'=> my_filemtimeasc($path . '/' . $item),
407 'file_type' => "Directory"
408 );
409 } elseif (my_is_file($path . '/' . $item)) {
410 if (!$show_hidden_files) {
411 if (substr($item, 0, 1) == "." or substr($item, -1) == "~")
412 continue;
413 }
414 $filelist[] = array(
415 'name'=> $item,
416 'size'=> my_filesize($path . '/' . $item),
417 'modtime'=> my_filemtime($path . '/' . $item),
418 'modtimeasc'=> my_filemtimeasc($path . '/' . $item),
419 'file_type' => get_file_type($path . '/' . $item)
420 );
421 }
422 }
423 closedir($handle);
424 file_put_contents($path . "/.folderlist", serialize($folderlist), LOCK_EX);
425 file_put_contents($path . "/.filelist", serialize($filelist), LOCK_EX);
426 }
427 }
429 if (isset($_GET['f'])) {
430 $filter = $_GET['f'];
431 if (substr($filter, 0, 1) != '/')
432 $filter = '/' . $filter . '/i';
433 foreach ($filelist as $key => $value)
434 if (!preg_match($filter, $value['name']))
435 unset($filelist[$key]);
436 foreach ($folderlist as $key => $value)
437 if (!preg_match($filter, $value['name']))
438 unset($folderlist[$key]);
439 }
441 if (!isset($_GET['s']))
442 $_GET['s'] = 'name';
445 // Figure out what to sort files by
446 $file_order_by = array();
447 foreach ($filelist as $key => $row)
448 $file_order_by[$key] = $row[$_GET['s']];
451 // Figure out what to sort folders by
452 $folder_order_by = array();
453 foreach ($folderlist as $key => $row)
454 $folder_order_by[$key] = $row[$_GET['s']];
457 // Order the files and folders
458 $sort_type = SORT_ASC;
459 $order = "&amp;o=d";
460 if (isset($_GET['o'])) {
461 $sort_type = SORT_DESC;
462 $order = "";
463 }
464 array_multisort($folder_order_by, $sort_type, $folderlist);
465 array_multisort($file_order_by, $sort_type, $filelist);
468 // Table caption: number of folders and files
469 print "<caption>" . count($folderlist) . " folders and " . count($filelist) . " files.</caption>";
472 // Show sort methods
473 print "<thead><tr>";
475 $sort_methods = array();
476 $sort_methods['name'] = "Name";
477 $sort_methods['modtime'] = "Last Modified";
478 $sort_methods['size'] = "Size";
480 foreach($sort_methods as $key => $item) {
481 if ($_GET['s'] == $key)
482 $key = "$key$order";
483 print "<th><a href='?s=$key'>$item</a></th>";
484 }
485 print "</tr></thead>\n<tbody>\n";
490 // Parent directory link
491 if ($path != "./")
492 print "<tr><td class='up'><a href='..'>Parent Directory</a>/</td>" .
493 "<td>&nbsp;</td>" .
494 "<td>- &nbsp;</td></tr>\n";
499 // Print folder information
500 foreach($folderlist as $folder)
501 print "<tr><td class='dir'><a href='" . addslashes($folder['name']). "'>" .
502 htmlentities($folder['name']) . "</a>/</td>" .
503 "<td>" . $folder['modtimeasc'] . "</td>" .
504 "<td>- &nbsp;</td></tr>\n";
508 // Print file information
509 foreach($filelist as $file) {
510 $filename = $file['name'];
511 $url = addslashes($filename);
513 if (preg_match('/\.(tazpkg|deb)$/', $filename))
514 $class = "pkg";
515 elseif (preg_match('/\.iso$/', $filename))
516 $class = "iso";
517 elseif (preg_match('/\.(exe|com)$/', $filename))
518 $class = "exe";
519 elseif (preg_match('/^README$/', $filename))
520 $class = "rme";
521 elseif (preg_match('/^bzImage$/', $filename))
522 $class = "krn";
523 elseif (preg_match('/\.zip$/', $filename))
524 $class = "zip";
525 elseif (preg_match('/\.log$/', $filename))
526 $class = "log";
527 else {
528 $classes = explode('/', $file['file_type']);
529 $class = $classes[1];
530 }
533 print "<tr><td class='$class'><a href='$url'>" . htmlentities($filename) . "</a></td>" .
534 "<td>" . $file['modtimeasc'] .
535 " <img src='/static/qr.png' alt='#' " .
536 "onmouseover=\"this.title = location.href+'$url'\" " .
537 "onclick=\"this.src = QRCodePNG(location.href+'$url', this)\"/></td>" .
538 "<td>" . format_bytes($file['size']) . "</td></tr>\n";
539 }
541 // Print ending stuff
542 print " </tbody>
543 </table>
544 </div>";
546 $soft = explode('/', $_SERVER["SERVER_SOFTWARE"]);
547 $tag = get_conf('server.tag', '"', '"', $soft[0] . ' &lt;' . $soft[1] . '&gt;');
550 if (filesize($path . "/README"))
551 print "<pre>" .
552 preg_replace('!(((f|ht)tp(s)?://)[-a-zA-Z()0-9@:%_+.~#?&;//=]+)!i',
553 '<a href="$1">$1</a>', file_get_contents($path . "/README")) .
554 "</pre>\n";
558 if ($slitaz_style) {
559 // SliTaz Style
560 ?>
562 <!-- End of content -->
563 </main>
565 <footer>
566 <div>
567 Copyright &copy; <span class="year"></span>
568 <a href="http://www.slitaz.org/">SliTaz</a>
569 </div>
570 <div>
571 Network:
572 <a href="http://scn.slitaz.org/">Community</a> ·
573 <a href="http://doc.slitaz.org/">Doc</a> ·
574 <a href="http://forum.slitaz.org/">Forum</a> ·
575 <a href="http://pkgs.slitaz.org/">Packages</a> ·
576 <a href="http://bugs.slitaz.org">Bugs</a> ·
577 <a href="http://hg.slitaz.org/?sort=lastchange">Hg</a>
578 </div>
579 <div>
580 SliTaz @
581 <a href="http://twitter.com/slitaz">Twitter</a> ·
582 <a href="http://www.facebook.com/slitaz">Facebook</a> ·
583 <a href="http://distrowatch.com/slitaz">Distrowatch</a> ·
584 <a href="http://en.wikipedia.org/wiki/SliTaz">Wikipedia</a> ·
585 <a href="http://flattr.com/profile/slitaz">Flattr</a>
586 </div>
587 <img src="static/qr.png" alt="#" onmouseover="this.title = location.href"
588 onclick="this.src = QRCodePNG(location.href, this)"/>
589 </footer>
591 <?php
592 // end SliTaz Style
593 } else {
594 // not SliTaz Style
595 print "
596 <form action='" . $_SERVER["REQUEST_URI"] . "' method='get'>
597 <div class='foot'>" . $tag . "
598 <input type='text' name='f'/>
599 <!-- <input type='submit' value='Filter' /> -->
600 </div>
601 </form>
602 ";
603 // end not SliTaz Style
604 }
606 print "</body>
607 </html>";
608 ?>