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

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