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

pkgs: add cooker links
author Pascal Bellard <pascal.bellard@slitaz.org>
date Mon Feb 17 14:29:16 2014 +0000 (2014-02-17)
parents f53863f3e839
children 40ad99b9e839
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 $modified = gmdate("D, d M Y H:i:s e", strtotime("-1 hour"));
150 $expires = gmdate("D, d M Y H:i:s e", strtotime("+1 hour"));
151 $fvalue = "";
152 if (isset($_GET['f'])) $fvalue = 'value="'.$_GET['f'].'"';
153 header("Expires: ".$expires);
154 header("Last-Modified: " . $modified);
155 header("Pragma: cache");
156 // header("Cache-Control: public");
157 // <meta http-equiv="cache-control" content="public" />
158 // <meta http-equiv="last-modified" content="$modified" />
159 // <meta http-equiv="expires" content="$expires" />
160 print <<<EOT
161 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
162 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
163 <html xml:lang="en" xmlns="http://www.w3.org/1999/xhtml" lang="en">
164 <head>
165 <title>Index of /$vpath</title>
166 <meta http-equiv="content-type" content="text/html; charset=ISO-8859-1" />
167 <meta name="description" content=">Index of /$vpath" />
168 <meta name="robots" content="index, nofollow" />
169 <meta name="author" content="SliTaz Contributors" />
170 <link rel="shortcut icon" href="/static/favicon.ico" />
171 <link rel="stylesheet" type="text/css" href="/static/slitaz.css" />
172 <style type="text/css">
173 #block { min-height: 160px; }
174 div.list { background-color: white; padding-bottom: 14px; }
175 th, td { font: 90% monospace; text-align: left; }
176 th { font-weight: bold; padding-right: 14px; padding-bottom: 3px; }
177 td { padding-right: 14px; }
178 td.s, th.s { text-align: right; }
179 </style>
180 </head>
181 <body>
183 <!-- Header -->
184 <div id="header">
185 <div id="logo"></div>
186 <div id="network">
187 <a href="http://www.slitaz.org/">
188 <img src="/static/home.png" alt="[ Home ]" /></a>
189 <a href="http://scn.slitaz.org/">Community</a>
190 <a href="http://doc.slitaz.org/">Doc</a>
191 <a href="http://forum.slitaz.org/">Forum</a>
192 <a href="http://pro.slitaz.org/">Pro</a>
193 <a href="https://slitaz.spreadshirt.net/">Shop</a>
194 <a href="http://bugs.slitaz.org">Bugs</a>
195 <a href="http://hg.slitaz.org/?sort=lastchange">Hg</a>
196 <a href="http://cook.slitaz.org/">Cook</a>
197 </div>
198 <h1><a href="http://mirror.slitaz.org/">SliTaz Mirror</a></h1>
199 </div>
201 <!-- Block -->
202 <div id="block">
203 <!-- Navigation -->
204 <div id="block_nav">
205 <h4>Online Tools</h4>
206 <ul>
207 <li><a href="http://pizza.slitaz.me/">Live ISO Builder</a></li>
208 <li><a href="http://pizza.slitaz.org/">Live flavor Builder</a></li>
209 <li><a href="http://tiny.slitaz.org/">Tiny SliTaz Builder</a></li>
210 <li><a href="http://boot.slitaz.org/">Web Boot</a></li>
211 </ul>
212 </div>
213 <!-- Information/image -->
214 <div id="block_info">
215 <h4>Welcome to Open Source!</h4>
216 EOT;
217 if (preg_match("/mirror\.slitaz\./",$_SERVER["SERVER_NAME"])) print <<<EOT
218 <p>This is the SliTaz GNU/Linux main mirror. The server runs naturally
219 SliTaz (stable) in an lguest virtual machine provided by
220 <a href="http://www.ads-lu.com/">ADS</a> and is located in France.
221 <a href="/info/">Mirror info...</a></p>
222 EOT;
223 print <<<EOT
224 <form action="${_SERVER["REQUEST_URI"]}" method="get" style="width: 300px;">
225 <p><input type="text" name="f" $fvalue
226 style="width: 260px;" /></p>
227 </form>
228 </div>
229 </div>
231 <!-- script type="text/javascript" src="/static/qrcode.js"></script -->
232 <script type="text/javascript">
233 function QRCodePNG(str, obj) {
234 try {
235 return QRCode.generatePNG(str, {ecclevel: 'H'});
236 }
237 catch (any) {
238 var element = document.createElement("script");
239 element.src = "/static/qrcode.js";
240 element.type ="text/javascript";
241 element.onload = function() {
242 obj.src = QRCode.generatePNG(str, {ecclevel: 'H'});
243 };
244 document.body.appendChild(element);
245 }
246 }
247 </script>
248 <div id="lang">
249 Path: /${vpath}
250 </div>
252 <!-- Content -->
253 <div id="content">
255 <div id="mirrors">
256 <img src="/static/network.png" alt="[ Mirrors ]" style="margin-right: 5px;" />
257 EOT;
259 // Mirror list
260 $mirrors = array();
261 $fp = @fopen(dirname($_SERVER["SCRIPT_FILENAME"])."/mirrors","r");
262 if ($fp) {
263 while (($line = fgets($fp)) !== false) {
264 $line = chop($line);
265 $url = parse_url($line);
266 if ($_SERVER["SERVER_NAME"] == $url['host']) continue;
267 $host = explode('.',$url['host']);
268 $mirrors[$host[count($host)-2].".".
269 $host[count($host)-1]] = $line;
270 }
271 }
272 fclose($fp);
273 foreach($mirrors as $name => $url) {
274 echo "<a href=\"$url$vpath\" title=\"$name mirror\">$name</a>\n";
275 }
276 echo "</div>";
277 }
278 else {
280 // Print the heading stuff
281 print "<?xml version='1.0' encoding='$encoding'?>
282 <!DOCTYPE html PUBLIC '-//W3C//DTD XHTML 1.1//EN' 'http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd'>
283 <html xmlns='http://www.w3.org/1999/xhtml' xml:lang='en'>
284 <head>
285 <title>Index of /" .$vpath. "</title>
286 ";
287 if ($external_css != '') {
288 print " <link rel='stylesheet' type='text/css' href='".$external_css."' />
289 ";
290 }
291 else {
292 print " <style type='text/css'>
293 a, a:active {text-decoration: none; color: blue;}
294 a:visited {color: #48468F;}
295 a:hover, a:focus {text-decoration: underline; color: red;}
296 body {background-color: #F5F5F5;}
297 h2 {margin-bottom: 12px;}
298 table {margin-left: 12px;}
299 th, td { font: 90% monospace; text-align: left;}
300 th { font-weight: bold; padding-right: 14px; padding-bottom: 3px;}
301 td {padding-right: 14px;}
302 td.s, th.s {text-align: right;}
303 div.list { background-color: white; border-top: 1px solid #646464; border-bottom: 1px solid #646464; padding-top: 10px; padding-bottom: 14px;}
304 div.foot { font: 90% monospace; color: #787878; padding-top: 4px;}
305 </style>
306 ";
307 }
308 print " </head>
309 <body>
310 <h2>Index of /" . $vpath ."</h2>
311 ";
312 }
314 print " <div class='list'>
315 <table summary='Directory Listing' cellpadding='0' cellspacing='0'>
316 ";
318 function my_is_file($path) // 2G+ file support
319 {
320 exec("[ -f '".$path."' ]", $tmp, $ret);
321 return $ret == 0;
322 //return is_file($path);
323 }
325 function my_filesize($path) // 2G+ file support
326 {
327 return rtrim(shell_exec("stat -Lc %s '".$path."'"));
328 //return filesize($path);
329 }
331 function my_filemtime($path) // 2G+ file support
332 {
333 return rtrim(shell_exec("stat -Lc %Y '".$path."'"));
334 //return filemtime($path);
335 }
337 function my_filemtimeasc($path) // 2G+ file support
338 {
339 return rtrim(shell_exec("LC_ALL=C date -r '".$path."' '+%Y-%b-%d %H:%M:%S'"));
340 //return date('Y-M-d H:m:s', filemtime($path));
341 }
343 if (filesize($path.".folderlist") > 0 && filesize($path.".filelist") > 0) {
344 $folderlist = unserialize(file_get_contents($path.".folderlist"));
345 $filelist = unserialize(file_get_contents($path.".filelist"));
346 }
347 else {
349 proc_nice(10);
350 // Get all of the folders and files.
351 $folderlist = array();
352 $filelist = array();
353 if($handle = @opendir($path)) {
354 while(($item = readdir($handle)) !== false) {
355 if ($item == "index.php") continue;
356 if ($item == ".folderlist") continue;
357 if ($item == ".filelist") continue;
358 if ($item == "dir-generator.php") continue;
359 if ($item == "robots.txt") continue;
360 if ($item == "humans.txt") continue;
361 if(is_dir($path.'/'.$item) and $item != '.' and $item != '..') {
362 $folderlist[] = array(
363 'name' => $item,
364 'size' => 0,
365 'modtime'=> filemtime($path.'/'.$item),
366 'modtimeasc'=> my_filemtimeasc($path.'/'.$item),
367 'file_type' => "Directory"
368 );
369 }
370 elseif(my_is_file($path.'/'.$item)) {
371 if(!$show_hidden_files) {
372 if(substr($item, 0, 1) == "." or substr($item, -1) == "~") {
373 continue;
374 }
375 }
376 $filelist[] = array(
377 'name'=> $item,
378 'size'=> my_filesize($path.'/'.$item),
379 'modtime'=> my_filemtime($path.'/'.$item),
380 'modtimeasc'=> my_filemtimeasc($path.'/'.$item),
381 'file_type' => get_file_type($path.'/'.$item)
382 );
383 }
384 }
385 closedir($handle);
386 file_put_contents($path.".folderlist",serialize($folderlist),LOCK_EX);
387 file_put_contents($path.".filelist",serialize($filelist),LOCK_EX);
388 }
389 }
391 if (isset($_GET['f'])) {
392 $filter = $_GET['f'];
393 if (substr($filter,0,1) != '/')
394 $filter = '/'.$filter.'/i';
395 foreach ($filelist as $key => $value)
396 if (!preg_match($filter,$value['name']))
397 unset($filelist[$key]);
398 foreach ($folderlist as $key => $value)
399 if (!preg_match($filter,$value['name']))
400 unset($folderlist[$key]);
401 }
403 if(!isset($_GET['s'])) {
404 $_GET['s'] = 'name';
405 }
407 // Figure out what to sort files by
408 $file_order_by = array();
409 foreach ($filelist as $key=>$row) {
410 $file_order_by[$key] = $row[$_GET['s']];
411 }
413 // Figure out what to sort folders by
414 $folder_order_by = array();
415 foreach ($folderlist as $key=>$row) {
416 $folder_order_by[$key] = $row[$_GET['s']];
417 }
419 // Order the files and folders
420 $sort_type = SORT_ASC;
421 $order = "&amp;o=d";
422 if(isset($_GET['o'])) {
423 $sort_type = SORT_DESC;
424 $order = "";
425 }
426 array_multisort($folder_order_by, $sort_type, $folderlist);
427 array_multisort($file_order_by, $sort_type, $filelist);
430 // Show sort methods
431 print "<thead><tr>";
433 $sort_methods = array();
434 $sort_methods['name'] = "Name";
435 $sort_methods['modtime'] = "Last Modified";
436 $sort_methods['size'] = "Size";
437 $sort_methods['file_type'] = "Type";
439 foreach($sort_methods as $key=>$item) {
440 if ($_GET['s'] == $key) $key = "$key$order";
441 print "<th class='n'><a href='?s=$key'>$item</a></th>";
442 }
443 print "</tr></thead>\n<tbody>\n";
447 // Parent directory link
448 if($path != "./") {
449 print "<tr><td class='n'><a href='..'>Parent Directory</a>/</td>";
450 print "<td class='m'>&nbsp;</td>";
451 print "<td class='s'>- &nbsp;</td>";
452 print "<td class='t'>Directory</td></tr>\n";
453 }
457 // Print folder information
458 foreach($folderlist as $folder) {
459 print "<tr><td class='n'><a href='" . addslashes($folder['name']). "'>" .htmlentities($folder['name']). "</a>/</td>";
460 print "<td class='m'>" . $folder['modtimeasc'] . "</td>";
461 print "<td class='s'>- &nbsp;</td>";
462 print "<td class='t'>" . $folder['file_type'] . "</td></tr>\n";
463 }
466 // Print file information
467 foreach($filelist as $file) {
468 $url = addslashes($file['name']);
469 print "<tr><td class='n'><a href='$url'>" .htmlentities($file['name']). "</a></td>";
470 print "<td class='m'>" . $file['modtimeasc'] .
471 " <img src=\"/static/qr.png\" alt=\"@\" onmouseover=" .
472 "\"this.title = location.href+'$url'\" onclick=" .
473 "\"this.src = QRCodePNG(location.href+'$url', this) \"/></td>";
474 print "<td class='s'>" . format_bytes($file['size']) . "</td>";
475 print "<td class='t'>" . $file['file_type'] . "</td></tr>\n";
476 }
478 // Print ending stuff
479 $soft = explode('/',$_SERVER["SERVER_SOFTWARE"]);
480 $tag = get_conf('server.tag','"','"',$soft[0].' &lt;'.$soft[1].'&gt;');
481 print "</tbody>
482 </table>
483 ".count($folderlist)." folders and ".count($filelist)." files.</div>";
484 if ($slitaz_style) { ?>
486 <!-- End of content -->
487 </div>
489 <!-- Footer -->
490 <div id="footer">
491 Copyright &copy; <span class="year"></span>
492 <a href="http://www.slitaz.org/">SliTaz</a> - Network:
493 <a href="http://scn.slitaz.org/">Community</a>
494 <a href="http://doc.slitaz.org/">Doc</a>
495 <a href="http://forum.slitaz.org/">Forum</a>
496 <a href="http://pkgs.slitaz.org/">Packages</a>
497 <a href="http://bugs.slitaz.org">Bugs</a>
498 <a href="http://hg.slitaz.org/?sort=lastchange">Hg</a>
499 <p>
500 <img src="/static/qr.png" alt="#" onmouseover="this.title = location.href"
501 onclick="this.src = QRCodePNG(location.href, this)" />
502 SliTaz @
503 <a href="http://twitter.com/slitaz">Twitter</a>
504 <a href="http://www.facebook.com/slitaz">Facebook</a>
505 <a href="http://distrowatch.com/slitaz">Distrowatch</a>
506 <a href="http://en.wikipedia.org/wiki/SliTaz">Wikipedia</a>
507 <a href="http://flattr.com/profile/slitaz">Flattr</a>
508 </p>
509 <p>
510 <a href="http://validator.w3.org/check?uri=referer">
511 <img src="/static/xhtml10.png"
512 alt="Valid XHTML 1.0" title="Code validé XHTML 1.0"
513 style="width: 80px; height: 15px;" /></a>
514 </p>
515 </div>
517 <?php }
518 else print "
519 <form action='".$_SERVER["REQUEST_URI"]."' method='get'>
520 <div class='foot'>".$tag."
521 <input type='text' name='f'/>
522 <!-- <input type='submit' value='Filter' /> -->
523 </div>
524 </form>
525 ";
526 print "</body>
527 </html>";
528 ?>