slitaz-dev-tools view mirror-tools/slitaz/mirror/dir-generator.php @ rev 162

mirror-tools: move all css files/images into slitaz-forge/mf
author Christophe Lincoln <pankso@slitaz.org>
date Tue Mar 20 12:46:15 2012 +0100 (2012-03-20)
parents 5f1d82daad41
children 82ee66991963
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 $fvalue = "";
150 if (isset($_GET['f'])) $fvalue = 'value="'.$_GET['f'].'"';
151 print <<<EOT
152 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
153 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
154 <html xml:lang="en" xmlns="http://www.w3.org/1999/xhtml" lang="en">
155 <head>
156 <title>Index of /$vpath</title>
157 <meta http-equiv="content-type" content="text/html; charset=ISO-8859-1" />
158 <meta name="description" content=">Index of /$vpath" />
159 <meta name="robots" content="index, nofollow" />
160 <meta name="author" content="SliTaz Contributors" />
161 <link rel="shortcut icon" href="http://mf.slitaz.org//favicon.ico" />
162 <link rel="stylesheet" type="text/css" href="mf.slitaz.org/css/slitaz.css" />
163 <link rel="stylesheet" type="text/css" href="mf.slitaz.org/css/mirror.css" />
164 </head>
165 <body>
167 <!-- Header -->
168 <div id="header">
169 <div id="logo"></div>
170 <div id="network">
171 <a href="http://www.slitaz.org/">
172 <img src="http://mf.slitaz.org/images/home.png" alt="[ Home ]" />
173 Home</a>
174 <a href="http://scn.slitaz.org/">Community</a>
175 <a href="http://doc.slitaz.org/">Doc</a>
176 <a href="http://forum.slitaz.org/">Forum</a>
177 <a href="http://pro.slitaz.org/">Pro</a>
178 <a href="https://slitaz.spreadshirt.net/">Shop</a>
179 <a href="http://bugs.slitaz.org">Bugs</a>
180 <a href="http://hg.slitaz.org/">Hg</a>
181 <a href="http://cook.slitaz.org/">BB</a>
182 </div>
183 <h1><a href="http://mirror.slitaz.org/">SliTaz Mirror</a> /${vpath}</h1>
184 </div>
186 <!-- Block -->
187 <div id="block">
188 <!-- Navigation -->
189 <div id="block_nav">
190 <h4><img src="/css/pics/network.png" alt=".png" />Mirrors</h4>
191 <div>
192 EOT;
194 print <<<EOT
195 </div>
196 <p>
197 <strong>Online tools:</strong>
198 <a href="http://pizza.slitaz.org/">Live Builder</a> -
199 <a href="http://boot.slitaz.org/">Web Boot</a>
200 </p>
201 </div>
202 <!-- Information/image -->
203 <div id="block_info">
204 <h4>Welcome to Open Source!</h4>
205 EOT;
206 if (preg_match("/mirror\.slitaz\./",$_SERVER["SERVER_NAME"])) print <<<EOT
207 <p>This is the SliTaz GNU/Linux main mirror. The server runs naturally
208 SliTaz (stable) in an lguest virtual machine provided by
209 <a href="http://www.ads-lu.com/">ADS</a>.
210 <a href="/info/">Mirror info...</a></p>
211 EOT;
212 print <<<EOT
213 <form action="${_SERVER["REQUEST_URI"]}" method="get" style="width: 300px;">
214 <p><input type="text" name="f" $fvalue
215 style="height: 14px; border-radius: 4px;" /></p>
216 </form>
217 </div>
218 </div>
220 <div id="lang">
221 EOT
223 // Mirror list
224 $mirrors = array();
225 $fp = @fopen(dirname($_SERVER["SCRIPT_FILENAME"])."/mirrors","r");
226 if ($fp) {
227 while (($line = fgets($fp)) !== false) {
228 $line = chop($line);
229 $url = parse_url($line);
230 if ($_SERVER["SERVER_NAME"] == $url['host']) continue;
231 $host = explode('.',$url['host']);
232 $mirrors[$host[count($host)-2].".".
233 $host[count($host)-1]] = $line;
234 }
235 }
236 fclose($fp);
237 foreach($mirrors as $name => $url) {
238 echo "<a href=\"$url$vpath\" title=\"$name mirror\">$name</a>\n";
239 }
241 print <<<EOT
242 </div>
244 <!-- Content -->
245 <div id="content">
247 EOT;
248 }
249 else {
251 // Print the heading stuff
252 print "<?xml version='1.0' encoding='$encoding'?>
253 <!DOCTYPE html PUBLIC '-//W3C//DTD XHTML 1.1//EN' 'http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd'>
254 <html xmlns='http://www.w3.org/1999/xhtml' xml:lang='en'>
255 <head>
256 <title>Index of /" .$vpath. "</title>
257 ";
258 if ($external_css != '') {
259 print " <link rel='stylesheet' type='text/css' href='".$external_css."' />
260 ";
261 }
262 else {
263 print " <style type='text/css'>
264 a, a:active {text-decoration: none; color: blue;}
265 a:visited {color: #48468F;}
266 a:hover, a:focus {text-decoration: underline; color: red;}
267 body {background-color: #F5F5F5;}
268 h2 {margin-bottom: 12px;}
269 table {margin-left: 12px;}
270 th, td { font: 90% monospace; text-align: left;}
271 th { font-weight: bold; padding-right: 14px; padding-bottom: 3px;}
272 td {padding-right: 14px;}
273 td.s, th.s {text-align: right;}
274 div.list { background-color: white; border-top: 1px solid #646464; border-bottom: 1px solid #646464; padding-top: 10px; padding-bottom: 14px;}
275 div.foot { font: 90% monospace; color: #787878; padding-top: 4px;}
276 </style>
277 ";
278 }
279 print " </head>
280 <body>
281 <h2>Index of /" . $vpath ."</h2>
282 ";
283 }
285 print " <div class='list'>
286 <table summary='Directory Listing' cellpadding='0' cellspacing='0'>
287 ";
289 function my_is_file($path) // 2G+ file support
290 {
291 exec("[ -f '".$path."' ]", $tmp, $ret);
292 return $ret == 0;
293 }
295 function my_filesize($path) // 2G+ file support
296 {
297 return rtrim(shell_exec("stat -Lc %s '".$path."'"));
298 }
300 function my_filemtime($path) // 2G+ file support
301 {
302 return rtrim(shell_exec("stat -Lc %Y '".$path."'"));
303 }
305 function my_filemtimeasc($path) // 2G+ file support
306 {
307 return rtrim(shell_exec("date -r '".$path."' '+%Y-%b-%d %H:%M:%S'"));
308 }
310 // Get all of the folders and files.
311 $folderlist = array();
312 $filelist = array();
313 if($handle = @opendir($path)) {
314 while(($item = readdir($handle)) !== false) {
315 if ($item == "index.php") continue;
316 if ($item == "dir-generator.php") continue;
317 if (isset($_GET['f'])) {
318 $filter = $_GET['f'];
319 if (substr($filter,0,1) != '/')
320 $filter = '/'.$filter.'/i';
321 if (!preg_match($filter,$item)) continue;
322 }
323 if(is_dir($path.'/'.$item) and $item != '.' and $item != '..') {
324 $folderlist[] = array(
325 'name' => $item,
326 'size' => 0,
327 'modtime'=> filemtime($path.'/'.$item),
328 'modtimeasc'=> my_filemtimeasc($path.'/'.$item),
329 'file_type' => "Directory"
330 );
331 }
332 elseif(my_is_file($path.'/'.$item)) {
333 if(!$show_hidden_files) {
334 if(substr($item, 0, 1) == "." or substr($item, -1) == "~") {
335 continue;
336 }
337 }
338 $filelist[] = array(
339 'name'=> $item,
340 'size'=> my_filesize($path.'/'.$item),
341 'modtime'=> my_filemtime($path.'/'.$item),
342 'modtimeasc'=> my_filemtimeasc($path.'/'.$item),
343 'file_type' => get_file_type($path.'/'.$item)
344 );
345 }
346 }
347 closedir($handle);
348 }
351 if(!isset($_GET['s'])) {
352 $_GET['s'] = 'name';
353 }
355 // Figure out what to sort files by
356 $file_order_by = array();
357 foreach ($filelist as $key=>$row) {
358 $file_order_by[$key] = $row[$_GET['s']];
359 }
361 // Figure out what to sort folders by
362 $folder_order_by = array();
363 foreach ($folderlist as $key=>$row) {
364 $folder_order_by[$key] = $row[$_GET['s']];
365 }
367 // Order the files and folders
368 $sort_type = SORT_ASC;
369 $order = "&amp;o=d";
370 if(isset($_GET['o'])) {
371 $sort_type = SORT_DESC;
372 $order = "";
373 }
374 array_multisort($folder_order_by, $sort_type, $folderlist);
375 array_multisort($file_order_by, $sort_type, $filelist);
378 // Show sort methods
379 print "<thead><tr>";
381 $sort_methods = array();
382 $sort_methods['name'] = "Name";
383 $sort_methods['modtime'] = "Last Modified";
384 $sort_methods['size'] = "Size";
385 $sort_methods['file_type'] = "Type";
387 foreach($sort_methods as $key=>$item) {
388 if ($_GET['s'] == $key) $key = "$key$order";
389 print "<th class='n'><a href='?s=$key'>$item</a></th>";
390 }
391 print "</tr></thead>\n<tbody>\n";
395 // Parent directory link
396 if($path != "./") {
397 print "<tr><td class='n'><a href='..'>Parent Directory</a>/</td>";
398 print "<td class='m'>&nbsp;</td>";
399 print "<td class='s'>- &nbsp;</td>";
400 print "<td class='t'>Directory</td></tr>\n";
401 }
405 // Print folder information
406 foreach($folderlist as $folder) {
407 print "<tr><td class='n'><a href='" . addslashes($folder['name']). "'>" .htmlentities($folder['name']). "</a>/</td>";
408 print "<td class='m'>" . $folder['modtimeasc'] . "</td>";
409 print "<td class='s'>- &nbsp;</td>";
410 print "<td class='t'>" . $folder['file_type'] . "</td></tr>\n";
411 }
414 // Print file information
415 foreach($filelist as $file) {
416 print "<tr><td class='n'><a href='" . addslashes($file['name']). "'>" .htmlentities($file['name']). "</a></td>";
417 print "<td class='m'>" . $file['modtimeasc'] . "</td>";
418 print "<td class='s'>" . format_bytes($file['size']) . "</td>";
419 print "<td class='t'>" . $file['file_type'] . "</td></tr>\n";
420 }
422 // Print ending stuff
423 $soft = explode('/',$_SERVER["SERVER_SOFTWARE"]);
424 $tag = get_conf('server.tag','"','"',$soft[0].' &lt;'.$soft[1].'&gt;');
425 print "</tbody>
426 </table>
427 </div>";
428 if ($slitaz_style) { ?>
430 <!-- End of content -->
431 </div>
433 <!-- Footer -->
434 <div id="footer">
435 Copyright &copy; <span class="year"></span>
436 <a href="http://www.slitaz.org/">SliTaz</a> - Network:
437 <a href="http://scn.slitaz.org/">Community</a>
438 <a href="http://doc.slitaz.org/">Doc</a>
439 <a href="http://forum.slitaz.org/">Forum</a>
440 <a href="http://pkgs.slitaz.org/">Packages</a>
441 <a href="http://bugs.slitaz.org">Bugs</a>
442 <a href="http://hg.slitaz.org/">Hg</a>
443 <p>
444 SliTaz @
445 <a href="http://twitter.com/slitaz">Twitter</a>
446 <a href="http://www.facebook.com/slitaz">Facebook</a>
447 <a href="http://distrowatch.com/slitaz">Distrowatch</a>
448 <a href="http://en.wikipedia.org/wiki/SliTaz">Wikipedia</a>
449 <a href="http://flattr.com/profile/slitaz">Flattr</a>
450 </p>
451 <p>
452 <a href="http://validator.w3.org/check?uri=referer">
453 <img src="/css/pics/website/xhtml10.png"
454 alt="Valid XHTML 1.0" title="Code validé XHTML 1.0"
455 style="width: 80px; height: 15px;" /></a>
456 </p>
457 </div>
459 <?php }
460 else print "
461 <form action='".$_SERVER["REQUEST_URI"]."' method='get'>
462 <div class='foot'>".$tag."
463 <input type='text' name='f'/>
464 <!-- <input type='submit' value='Filter' /> -->
465 </div>
466 </form>
467 ";
468 print "</body>
469 </html>";
470 ?>