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

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