wok-next view mirror-tools/stuff/var/www/slitaz/mirror/dir-generator.php @ rev 8066

mirror-tools: add dir-generator.php
author Pascal Bellard <pascal.bellard@slitaz.org>
date Fri Jan 21 11:09:19 2011 +0100 (2011-01-21)
parents
children c2e157dc12a2
line source
1 <?php
3 $VERSION = "0.2-slitaz";
5 /* Lighttpd Enhanced Directory Listing Script
6 * ------------------------------------------
7 * Authors: Evan Fosmark <me@evanfosmark.com>,
8 * Pascal Bellard <pascal.bellard@slitaz.org>
9 * Christophe Lincoln <pankso@slitaz.org>
10 *
11 *
12 * GNU License Agreement
13 * ---------------------
14 * This program is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation; either version 2 of the License, or
17 * (at your option) any later version.
18 *
19 * This program is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
23 *
24 * You should have received a copy of the GNU General Public License
25 * along with this program; if not, write to the Free Software
26 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
27 *
28 * http://www.gnu.org/licenses/gpl.txt
29 */
31 // Get the path (cut out the query string from the request_uri)
32 list($path) = explode('?', $_SERVER['REQUEST_URI']);
35 // Get the path that we're supposed to show.
36 $path = ltrim(rawurldecode($path), '/');
39 if(strlen($path) == 0) {
40 $path = "./";
41 }
44 // Can't call the script directly since REQUEST_URI won't be a directory
45 if($_SERVER['PHP_SELF'] == '/'.$path) {
46 die("Unable to call " . $path . " directly.");
47 }
50 $vpath = ($path != "./")?$path:"";
51 // Make sure it is valid.
52 if(!is_dir($path)) {
53 // die("<b>" . $path . "</b> is not a valid path.");
54 $path = dirname($_SERVER["SCRIPT_FILENAME"]);
55 list($vpath) = explode('?', $_SERVER['REQUEST_URI']);
56 $vpath = ltrim(rawurldecode($vpath), '/');
57 }
60 //
61 // This function returns the file size of a specified $file.
62 //
63 function format_bytes($size, $precision=1) {
64 $sizes = array('Y', 'Z', 'E', 'P', 'T', 'G', 'M', 'K', '');
65 $total = count($sizes);
67 while($total-- && $size > 1024) $size /= 1024;
68 if ($sizes[$total] == '') {
69 $size /= 1024;
70 $total--;
71 }
72 return sprintf('%.'.$precision.'f', $size).$sizes[$total];
73 }
76 //
77 // Get some variables from /etc/lighttpd/lighttpd.conf
78 //
79 $conf_lightty = file_get_contents("/etc/lighttpd/lighttpd.conf");
81 function get_conf($var,$start,$stop,$default='') {
82 global $conf_lightty;
84 if (!preg_match('/'.$var.'/',$conf_lightty)) return $default;
85 $filter = '/(.*\n)*'.$var.'\s*=\s*'.$start.'(([^'.$stop.']*\n*)*)'.$stop.'(.*\n)*/';
86 return preg_replace($filter,'$2',$conf_lightty);
87 }
89 $encoding = get_conf('dir-listing.encoding','"','"','ascii');
90 $external_css = get_conf('dir-listing.external-css','"','"');
92 $show_hidden_files = false;
93 if (get_conf('dir-listing.hide-dotfile','"','"','disable') == "disable") {
94 $show_hidden_files = true;
95 }
96 // get_conf('dir-listing.exclude','\(','\)');
97 // get_conf('dir-listing.set-footer','"','"');
99 $mime_types = array();
100 foreach (explode(',',get_conf('mimetype.assign','\(','\)')) as $item) {
101 $filter = '/\s*"(.*)"\s*=>\s*"(.*)".*/';
102 $val = explode(',',preg_replace($filter,'$1,$2',$item));
103 if (isset($val[1])) $mime_types[$val[0]] = $val[1];
104 }
106 //
107 // This function returns the mime type of $file.
108 //
109 function get_file_type($file) {
110 global $mime_types;
112 $file = basename($file);
113 $default_type = "application/octet-stream";
114 if (isset($mime_types[$file])) {
115 return $mime_types[$file];
116 }
117 $pos = strrpos($file, ".");
118 if ($pos === false) {
119 return $default_type;
120 }
121 //FIXME .tar.gz
122 $ext = '.'.rtrim(substr($file, $pos+1), "~");
123 if (isset($mime_types[$ext])) {
124 return $mime_types[$ext];
125 }
126 return $default_type;
127 }
129 //$slitaz_style = (dirname($_SERVER["PHP_SELF"]) == '/');
130 $slitaz_style = ($_SERVER["SERVER_NAME"] == "mirror.slitaz.org");
131 if ($slitaz_style) {
132 $fvalue = "";
133 if (isset($_GET[f])) $fvalue = 'value="'.$_GET[f].'"';
134 print <<<EOT
135 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
136 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
137 <html xml:lang="en" xmlns="http://www.w3.org/1999/xhtml" lang="en">
138 <head>
139 <title>Index of /$vpath</title>
140 <meta http-equiv="content-type" content="text/html; charset=ISO-8859-1" />
141 <meta name="description" content=">Index of /$vpath" />
142 <meta name="robots" content="index, nofollow" />
143 <meta name="author" content="SliTaz Contributors" />
144 <link rel="shortcut icon" href="/css/favicon.ico" />
145 <link rel="stylesheet" type="text/css" href="/css/slitaz.css" />
146 <style type='text/css'>
147 div.list { background-color: white; padding-bottom: 14px;}
148 table {width: 100% ;}
149 th, td { font: 90% monospace; text-align: left;}
150 th { font-weight: bold; padding-right: 14px; padding-bottom: 3px;}
151 td {padding-right: 14px;}
152 td.s, th.s {text-align: right;}
153 </style>
154 </head>
155 <body>
157 <!-- Header -->
158 <div id="header">
159 <a href="http://mirror.slitaz.org/"><img id="logo"
160 src="/css/pics/website/logo.png"
161 title="mirror.slitaz.org" alt="mirror.slitaz.org" /></a>
162 <p id="titre">#!/Mirror/${vpath}</p>
163 </div>
165 <!-- Content -->
166 <div id="content-full">
168 <!-- Block begin -->
169 <div class="block">
170 <!-- Nav block begin -->
171 <div id="block_nav">
172 <h3><img src="/css/pics/website/users.png" alt="users.png" />Community</h3>
173 <ul>
174 <li><a href="http://pizza.slitaz.org/">Live Builder</a></li>
175 <li><a href="http://boot.slitaz.org/">Web Boot</a></li>
176 </ul>
177 <h3>Search</h3>
178 <form class="search" action="${_SERVER["REQUEST_URI"]}" method="get" >
179 <p><input type="text" name="f" $fvalue /></p>
180 </form>
181 <!-- Nav block end -->
182 </div>
183 <!-- Top block begin -->
184 <div id="block_top">
185 <h1>About Mirror</h1>
186 <p>Welcome to Open Source!
187 EOT;
188 if ($_SERVER["SERVER_NAME"] == "mirror.slitaz.org") print <<<EOT
189 This is the SliTaz GNU/Linux main mirror. The server runs naturally SliTaz
190 (stable) in an lguest virtual machine provided by
191 <a href="http://www.ads-lu.com/">ADS</a>.
192 EOT;
193 print <<<EOT
194 </p>
195 <p><img src="/css/pics/website/network.png"
196 alt=".png" style="vertical-align:middle;"/>Mirrors:
197 EOT;
198 $mirrors = array(
199 "switch.ch" => "http://mirror.switch.ch/ftp/mirror/slitaz/",
200 "gatech.edu" => "http://www.gtlib.gatech.edu/pub/slitaz/",
201 "tuxfamily.org" => "http://download.tuxfamily.org/slitaz/",
202 "lupaworld.com" => "http://mirror.lupaworld.com/slitaz/",
203 "ufpr.br" => "http://slitaz.c3sl.ufpr.br/",
204 "pina.si" => "ftp://ftp.pina.si/slitaz/",
205 "ibiblio.org" => "http://distro.ibiblio.org/pub/linux/distributions/slitaz/",
206 "vim.org" => "http://ftp.vim.org/ftp/os/Linux/distr/slitaz/",
207 "nedit.org" => "http://ftp.nedit.org/ftp/ftp/pub/os/Linux/distr/slitaz/",
208 "xemacs.org" => "http://ftp.ch.xemacs.org/ftp/pool/2/mirror/slitaz/",
209 "garr.it" => "http://slitaz.mirror.garr.it/mirrors/slitaz/",
210 );
211 foreach($mirrors as $name => $url) {
212 echo "<a href=\"$url$vpath\" title=\"$name mirror\">$name</a>\n";
213 }
214 print <<<EOT
215 </p>
216 <!-- Top block end -->
217 </div>
218 <!-- Block end -->
219 </div>
221 EOT;
222 }
223 else {
225 // Print the heading stuff
226 print "<?xml version='1.0' encoding='$encoding'?>
227 <!DOCTYPE html PUBLIC '-//W3C//DTD XHTML 1.1//EN' 'http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd'>
228 <html xmlns='http://www.w3.org/1999/xhtml' xml:lang='en'>
229 <head>
230 <title>Index of /" .$vpath. "</title>
231 ";
232 if ($external_css != '') {
233 print " <link rel='stylesheet' type='text/css' href='".$external_css."' />
234 ";
235 }
236 else {
237 print " <style type='text/css'>
238 a, a:active {text-decoration: none; color: blue;}
239 a:visited {color: #48468F;}
240 a:hover, a:focus {text-decoration: underline; color: red;}
241 body {background-color: #F5F5F5;}
242 h2 {margin-bottom: 12px;}
243 table {margin-left: 12px;}
244 th, td { font: 90% monospace; text-align: left;}
245 th { font-weight: bold; padding-right: 14px; padding-bottom: 3px;}
246 td {padding-right: 14px;}
247 td.s, th.s {text-align: right;}
248 div.list { background-color: white; border-top: 1px solid #646464; border-bottom: 1px solid #646464; padding-top: 10px; padding-bottom: 14px;}
249 div.foot { font: 90% monospace; color: #787878; padding-top: 4px;}
250 </style>
251 ";
252 }
253 print " </head>
254 <boby>
255 <h2>Index of /" . $vpath ."</h2>
256 ";
257 }
259 print " <div class='list'>
260 <table summary='Directory Listing' cellpadding='0' cellspacing='0'>
261 ";
265 // Get all of the folders and files.
266 $folderlist = array();
267 $filelist = array();
268 if($handle = @opendir($path)) {
269 while(($item = readdir($handle)) !== false) {
270 if ($item == "index.php") continue;
271 if ($item == "dir-generator.php") continue;
272 if (isset($_GET['f'])) {
273 $filter = $_GET['f'];
274 if (substr($filter,0,1) != '/')
275 $filter = '/'.$filter.'/i';
276 if (!preg_match($filter,$item)) continue;
277 }
278 if(is_dir($path.'/'.$item) and $item != '.' and $item != '..') {
279 $folderlist[] = array(
280 'name' => $item,
281 'size' => 0,
282 'modtime'=> filemtime($path.'/'.$item),
283 'file_type' => "Directory"
284 );
285 }
286 elseif(is_file($path.'/'.$item)) {
287 if(!$show_hidden_files) {
288 if(substr($item, 0, 1) == "." or substr($item, -1) == "~") {
289 continue;
290 }
291 }
292 $filelist[] = array(
293 'name'=> $item,
294 'size'=> filesize($path.'/'.$item),
295 'modtime'=> filemtime($path.'/'.$item),
296 'file_type' => get_file_type($path.'/'.$item)
297 );
298 }
299 }
300 closedir($handle);
301 }
304 if(!isset($_GET['s'])) {
305 $_GET['s'] = 'name';
306 }
308 // Figure out what to sort files by
309 $file_order_by = array();
310 foreach ($filelist as $key=>$row) {
311 $file_order_by[$key] = $row[$_GET['s']];
312 }
314 // Figure out what to sort folders by
315 $folder_order_by = array();
316 foreach ($folderlist as $key=>$row) {
317 $folder_order_by[$key] = $row[$_GET['s']];
318 }
320 // Order the files and folders
321 $sort_type = SORT_ASC;
322 $order = "&amp;o=d";
323 if(isset($_GET['o'])) {
324 $sort_type = SORT_DESC;
325 $order = "";
326 }
327 array_multisort($folder_order_by, $sort_type, $folderlist);
328 array_multisort($file_order_by, $sort_type, $filelist);
331 // Show sort methods
332 print "<thead><tr>";
334 $sort_methods = array();
335 $sort_methods['name'] = "Name";
336 $sort_methods['modtime'] = "Last Modified";
337 $sort_methods['size'] = "Size";
338 $sort_methods['file_type'] = "Type";
340 foreach($sort_methods as $key=>$item) {
341 if ($_GET['s'] == $key) $key = "$key$order";
342 print "<th class='n'><a href='?s=$key'>$item</a></th>";
343 }
344 print "</tr></thead>\n<tbody>\n";
348 // Parent directory link
349 if($path != "./") {
350 print "<tr><td class='n'><a href='..'>Parent Directory</a>/</td>";
351 print "<td class='m'>&nbsp;</td>";
352 print "<td class='s'>- &nbsp;</td>";
353 print "<td class='t'>Directory</td></tr>\n";
354 }
358 // Print folder information
359 foreach($folderlist as $folder) {
360 print "<tr><td class='n'><a href='" . addslashes($folder['name']). "'>" .htmlentities($folder['name']). "</a>/</td>";
361 print "<td class='m'>" . date('Y-M-d H:m:s', $folder['modtime']) . "</td>";
362 print "<td class='s'>- &nbsp;</td>";
363 print "<td class='t'>" . $folder['file_type'] . "</td></tr>\n";
364 }
367 // Print file information
368 foreach($filelist as $file) {
369 print "<tr><td class='n'><a href='" . addslashes($file['name']). "'>" .htmlentities($file['name']). "</a></td>";
370 print "<td class='m'>" . date('Y-M-d H:m:s', $file['modtime']) . "</td>";
371 print "<td class='s'>" . format_bytes($file['size']) . "</td>";
372 print "<td class='t'>" . $file['file_type'] . "</td></tr>\n";
373 }
375 // Print ending stuff
376 $soft = explode('/',$_SERVER["SERVER_SOFTWARE"]);
377 $tag = get_conf('server.tag','"','"',$soft[0].' &lt;'.$soft[1].'&gt;');
378 print "</tbody>
379 </table>
380 </div>";
381 if ($slitaz_style) { ?>
383 <!-- End of content -->
384 </div>
386 <!-- Footer -->
387 <div id="footer">
388 <div class="right_box">
389 <h4>SliTaz Network</h4>
390 <ul>
391 <li><a href="http://www.slitaz.org/">Main Website</a></li>
392 <li><a href="http://doc.slitaz.org/">Documentation</a></li>
393 <li><a href="http://forum.slitaz.org/">Support Forum</a></li>
394 <li><a href="http://scn.slitaz.org/">Community Network</a></li>
395 <li><a href="http://pkgs.slitaz.org/">Packages</a></li>
396 <li><a href="http://labs.slitaz.org/">Laboratories</a></li>
397 </ul>
398 </div>
399 <h4>SliTaz Website</h4>
400 <ul>
401 <li><a href="#header">Top of the page</a></li>
402 <li>Copyright &copy; <span class="year"></span>
403 <a href="http://www.slitaz.org/">SliTaz</a></li>
404 <li><a href="about/">About the project</a></li>
405 <li><a href="netmap.php">Network Map</a></li>
406 <li>Page modified the <?php echo date('r'); ?></li>
407 <li><a href="http://validator.w3.org/check?uri=referer"><img
408 src="pics/website/xhtml10.png" alt="Valid XHTML 1.0"
409 title="Code validé XHTML 1.0"
410 style="width: 80px; height: 15px; vertical-align: middle;" /></a></li>
411 </ul>
412 </div>
414 <?php }
415 else print "
416 <form action='".$_SERVER["REQUEST_URI"]."' method='get'>
417 <div class='foot'>".$tag."
418 <input type='text' name='f'/>
419 <!-- <input type='submit' value='Filter' /> -->
420 </div>
421 </form>
422 ";
423 print "</body>
424 </html>";
425 ?>