slitaz-forge diff mirror/root/dir-generator.php @ rev 166
Add mirror/root for file at mirror.slitaz.org (from slitaz-dev-tools/mirror-tools)
author | Christophe Lincoln <pankso@slitaz.org> |
---|---|
date | Tue Mar 20 15:44:51 2012 +0100 (2012-03-20) |
parents | |
children | e5baa4584135 |
line diff
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/mirror/root/dir-generator.php Tue Mar 20 15:44:51 2012 +0100 1.3 @@ -0,0 +1,464 @@ 1.4 +<?php 1.5 +function redirect() 1.6 +{ 1.7 +?> 1.8 +<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 1.9 + "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 1.10 +<html xml:lang="en" xmlns="http://www.w3.org/1999/xhtml" lang="en"> 1.11 +<head> 1.12 + <title>SliTaz mirror redirection</title> 1.13 + <meta http-equiv="content-type" content="text/html; charset=ISO-8859-1" /> 1.14 + <meta name="description" content="slitaz mirror redirection" /> 1.15 + <meta name="robots" content="index, nofollow" /> 1.16 + <meta name="author" content="SliTaz Contributors" /> 1.17 + <meta http-equiv="Refresh" content="0;url=http://mirror.slitaz.org/"> 1.18 +</head> 1.19 +<?php 1.20 +} 1.21 + 1.22 +$VERSION = "0.2-slitaz"; 1.23 + 1.24 +/* Lighttpd Enhanced Directory Listing Script 1.25 + * ------------------------------------------ 1.26 + * Authors: Evan Fosmark <me@evanfosmark.com>, 1.27 + * Pascal Bellard <pascal.bellard@slitaz.org> 1.28 + * Christophe Lincoln <pankso@slitaz.org> 1.29 + * 1.30 + * 1.31 + * GNU License Agreement 1.32 + * --------------------- 1.33 + * This program is free software; you can redistribute it and/or modify 1.34 + * it under the terms of the GNU General Public License as published by 1.35 + * the Free Software Foundation; either version 2 of the License, or 1.36 + * (at your option) any later version. 1.37 + * 1.38 + * This program is distributed in the hope that it will be useful, 1.39 + * but WITHOUT ANY WARRANTY; without even the implied warranty of 1.40 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 1.41 + * GNU General Public License for more details. 1.42 + * 1.43 + * You should have received a copy of the GNU General Public License 1.44 + * along with this program; if not, write to the Free Software 1.45 + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 1.46 + * 1.47 + * http://www.gnu.org/licenses/gpl.txt 1.48 + */ 1.49 + 1.50 +// Get the path (cut out the query string from the request_uri) 1.51 +list($path) = explode('?', $_SERVER['REQUEST_URI']); 1.52 + 1.53 + 1.54 +// Get the path that we're supposed to show. 1.55 +$path = ltrim(rawurldecode($path), '/'); 1.56 + 1.57 + 1.58 +if(strlen($path) == 0) { 1.59 + $path = "./"; 1.60 +} 1.61 + 1.62 + 1.63 +// Can't call the script directly since REQUEST_URI won't be a directory 1.64 +if($_SERVER['PHP_SELF'] == '/'.$path) { 1.65 + redirect(); 1.66 +// die("Unable to call " . $path . " directly."); 1.67 +} 1.68 + 1.69 + 1.70 +$vpath = ($path != "./")?$path:""; 1.71 +// Make sure it is valid. 1.72 +if(!is_dir($path)) { 1.73 +// die("<b>" . $path . "</b> is not a valid path."); 1.74 + $path = dirname($_SERVER["SCRIPT_FILENAME"]); 1.75 + list($vpath) = explode('?', $_SERVER['REQUEST_URI']); 1.76 + $vpath = ltrim(rawurldecode($vpath), '/'); 1.77 +} 1.78 + 1.79 + 1.80 +// 1.81 +// This function returns the file size of a specified $file. 1.82 +// 1.83 +function format_bytes($size, $precision=1) { 1.84 + $sizes = array('Y', 'Z', 'E', 'P', 'T', 'G', 'M', 'K', ''); 1.85 + $total = count($sizes); 1.86 + 1.87 + while($total-- && $size > 1024) $size /= 1024; 1.88 + if ($sizes[$total] == '') { 1.89 + $size /= 1024; 1.90 + $total--; 1.91 + } 1.92 + return sprintf('%.'.$precision.'f', $size).$sizes[$total]; 1.93 +} 1.94 + 1.95 +// 1.96 +// Get some variables from /etc/lighttpd/lighttpd.conf 1.97 +// 1.98 +$conf_lightty = file_get_contents("/etc/lighttpd/lighttpd.conf"); 1.99 + 1.100 +function get_conf($var,$start,$stop,$default='') { 1.101 + global $conf_lightty; 1.102 + 1.103 + if (!preg_match('/'.$var.'/',$conf_lightty)) return $default; 1.104 + $filter = '/(.*\n)*'.$var.'\s*=\s*'.$start.'(([^'.$stop.']*\n*)*)'.$stop.'(.*\n)*/'; 1.105 + return preg_replace($filter,'$2',$conf_lightty); 1.106 +} 1.107 + 1.108 +$encoding = get_conf('dir-listing.encoding','"','"','ascii'); 1.109 +$external_css = get_conf('dir-listing.external-css','"','"'); 1.110 + 1.111 +$show_hidden_files = false; 1.112 +if (get_conf('dir-listing.hide-dotfile','"','"','disable') == "disable") { 1.113 + $show_hidden_files = true; 1.114 +} 1.115 +// get_conf('dir-listing.exclude','\(','\)'); 1.116 +// get_conf('dir-listing.set-footer','"','"'); 1.117 + 1.118 +$mime_types = array(); 1.119 +foreach (explode(',',get_conf('mimetype.assign','\(','\)')) as $item) { 1.120 + $filter = '/\s*"(.*)"\s*=>\s*"(.*)".*/'; 1.121 + $val = explode(',',preg_replace($filter,'$1,$2',$item)); 1.122 + if (isset($val[1])) $mime_types[$val[0]] = $val[1]; 1.123 +} 1.124 + 1.125 +// 1.126 +// This function returns the mime type of $file. 1.127 +// 1.128 +function get_file_type($file) { 1.129 + global $mime_types; 1.130 + 1.131 + $file = basename($file); 1.132 + $default_type = "application/octet-stream"; 1.133 + if (isset($mime_types[$file])) { 1.134 + return $mime_types[$file]; 1.135 + } 1.136 + $pos = strrpos($file, "."); 1.137 + if ($pos === false) { 1.138 + return $default_type; 1.139 + } 1.140 +//FIXME .tar.gz 1.141 + $ext = '.'.rtrim(substr($file, $pos+1), "~"); 1.142 + if (isset($mime_types[$ext])) { 1.143 + return $mime_types[$ext]; 1.144 + } 1.145 + return $default_type; 1.146 +} 1.147 + 1.148 +//$slitaz_style = (dirname($_SERVER["PHP_SELF"]) == '/'); 1.149 +//$slitaz_style = ($_SERVER["SERVER_NAME"] == "mirror.slitaz.org"); 1.150 +$slitaz_style = preg_match("/mirror\.slitaz\./",$_SERVER["SERVER_NAME"]); 1.151 +if ($slitaz_style) { 1.152 + $fvalue = ""; 1.153 + if (isset($_GET['f'])) $fvalue = 'value="'.$_GET['f'].'"'; 1.154 + print <<<EOT 1.155 +<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 1.156 + "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 1.157 +<html xml:lang="en" xmlns="http://www.w3.org/1999/xhtml" lang="en"> 1.158 +<head> 1.159 + <title>Index of /$vpath</title> 1.160 + <meta http-equiv="content-type" content="text/html; charset=ISO-8859-1" /> 1.161 + <meta name="description" content=">Index of /$vpath" /> 1.162 + <meta name="robots" content="index, nofollow" /> 1.163 + <meta name="author" content="SliTaz Contributors" /> 1.164 + <link rel="shortcut icon" href="http://mf.slitaz.org//favicon.ico" /> 1.165 + <link rel="stylesheet" type="text/css" href="http://mf.slitaz.org/css/slitaz.css" /> 1.166 + <link rel="stylesheet" type="text/css" href="http://mf.slitaz.org/css/mirror.css" /> 1.167 +</head> 1.168 +<body> 1.169 + 1.170 +<!-- Header --> 1.171 +<div id="header"> 1.172 + <div id="logo"></div> 1.173 + <div id="network"> 1.174 + <a href="http://www.slitaz.org/"> 1.175 + <img src="http://mf.slitaz.org/images/home.png" alt="[ Home ]" /> 1.176 + Home</a> 1.177 + <a href="http://scn.slitaz.org/">Community</a> 1.178 + <a href="http://doc.slitaz.org/">Doc</a> 1.179 + <a href="http://forum.slitaz.org/">Forum</a> 1.180 + <a href="http://pro.slitaz.org/">Pro</a> 1.181 + <a href="https://slitaz.spreadshirt.net/">Shop</a> 1.182 + <a href="http://bugs.slitaz.org">Bugs</a> 1.183 + <a href="http://hg.slitaz.org/">Hg</a> 1.184 + <a href="http://cook.slitaz.org/">BB</a> 1.185 + </div> 1.186 + <h1><a href="http://mirror.slitaz.org/">SliTaz Mirror</a> /${vpath}</h1> 1.187 +</div> 1.188 + 1.189 +<!-- Block --> 1.190 +<div id="block"> 1.191 + <!-- Navigation --> 1.192 + <div id="block_nav"> 1.193 + <h4>Online Tools</h4> 1.194 + <ul> 1.195 + <li><a href="http://pizza.slitaz.org/">Live Builder</a></li> 1.196 + <li><a href="http://boot.slitaz.org/">Web Boot</a></li> 1.197 + </ul> 1.198 + </div> 1.199 + <!-- Information/image --> 1.200 + <div id="block_info"> 1.201 + <h4>Welcome to Open Source!</h4> 1.202 +EOT; 1.203 + if (preg_match("/mirror\.slitaz\./",$_SERVER["SERVER_NAME"])) print <<<EOT 1.204 + <p>This is the SliTaz GNU/Linux main mirror. The server runs naturally 1.205 + SliTaz (stable) in an lguest virtual machine provided by 1.206 + <a href="http://www.ads-lu.com/">ADS</a>. 1.207 + <a href="/info/">Mirror info...</a></p> 1.208 +EOT; 1.209 + print <<<EOT 1.210 + <form action="${_SERVER["REQUEST_URI"]}" method="get" style="width: 300px;"> 1.211 + <p><input type="text" name="f" $fvalue 1.212 + style="width: auto;" /></p> 1.213 + </form> 1.214 + </div> 1.215 +</div> 1.216 + 1.217 +<div id="lang"> 1.218 +EOT; 1.219 + 1.220 +// Mirror list 1.221 +$mirrors = array(); 1.222 +$fp = @fopen(dirname($_SERVER["SCRIPT_FILENAME"])."/mirrors","r"); 1.223 +if ($fp) { 1.224 + while (($line = fgets($fp)) !== false) { 1.225 + $line = chop($line); 1.226 + $url = parse_url($line); 1.227 + if ($_SERVER["SERVER_NAME"] == $url['host']) continue; 1.228 + $host = explode('.',$url['host']); 1.229 + $mirrors[$host[count($host)-2].".". 1.230 + $host[count($host)-1]] = $line; 1.231 + } 1.232 +} 1.233 +fclose($fp); 1.234 +foreach($mirrors as $name => $url) { 1.235 + echo "<a href=\"$url$vpath\" title=\"$name mirror\">$name</a>\n"; 1.236 +} 1.237 + 1.238 +print <<<EOT 1.239 +</div> 1.240 + 1.241 +<!-- Content --> 1.242 +<div id="content"> 1.243 + 1.244 +EOT; 1.245 +} 1.246 +else { 1.247 + 1.248 +// Print the heading stuff 1.249 +print "<?xml version='1.0' encoding='$encoding'?> 1.250 +<!DOCTYPE html PUBLIC '-//W3C//DTD XHTML 1.1//EN' 'http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd'> 1.251 +<html xmlns='http://www.w3.org/1999/xhtml' xml:lang='en'> 1.252 + <head> 1.253 + <title>Index of /" .$vpath. "</title> 1.254 +"; 1.255 +if ($external_css != '') { 1.256 +print " <link rel='stylesheet' type='text/css' href='".$external_css."' /> 1.257 +"; 1.258 +} 1.259 +else { 1.260 +print " <style type='text/css'> 1.261 + a, a:active {text-decoration: none; color: blue;} 1.262 + a:visited {color: #48468F;} 1.263 + a:hover, a:focus {text-decoration: underline; color: red;} 1.264 + body {background-color: #F5F5F5;} 1.265 + h2 {margin-bottom: 12px;} 1.266 + table {margin-left: 12px;} 1.267 + th, td { font: 90% monospace; text-align: left;} 1.268 + th { font-weight: bold; padding-right: 14px; padding-bottom: 3px;} 1.269 + td {padding-right: 14px;} 1.270 + td.s, th.s {text-align: right;} 1.271 + div.list { background-color: white; border-top: 1px solid #646464; border-bottom: 1px solid #646464; padding-top: 10px; padding-bottom: 14px;} 1.272 + div.foot { font: 90% monospace; color: #787878; padding-top: 4px;} 1.273 + </style> 1.274 +"; 1.275 +} 1.276 +print " </head> 1.277 + <body> 1.278 + <h2>Index of /" . $vpath ."</h2> 1.279 +"; 1.280 +} 1.281 + 1.282 +print " <div class='list'> 1.283 + <table summary='Directory Listing' cellpadding='0' cellspacing='0'> 1.284 +"; 1.285 + 1.286 +function my_is_file($path) // 2G+ file support 1.287 +{ 1.288 + exec("[ -f '".$path."' ]", $tmp, $ret); 1.289 + return $ret == 0; 1.290 +} 1.291 + 1.292 +function my_filesize($path) // 2G+ file support 1.293 +{ 1.294 + return rtrim(shell_exec("stat -Lc %s '".$path."'")); 1.295 +} 1.296 + 1.297 +function my_filemtime($path) // 2G+ file support 1.298 +{ 1.299 + return rtrim(shell_exec("stat -Lc %Y '".$path."'")); 1.300 +} 1.301 + 1.302 +function my_filemtimeasc($path) // 2G+ file support 1.303 +{ 1.304 + return rtrim(shell_exec("date -r '".$path."' '+%Y-%b-%d %H:%M:%S'")); 1.305 +} 1.306 + 1.307 +// Get all of the folders and files. 1.308 +$folderlist = array(); 1.309 +$filelist = array(); 1.310 +if($handle = @opendir($path)) { 1.311 + while(($item = readdir($handle)) !== false) { 1.312 + if ($item == "index.php") continue; 1.313 + if ($item == "dir-generator.php") continue; 1.314 + if (isset($_GET['f'])) { 1.315 + $filter = $_GET['f']; 1.316 + if (substr($filter,0,1) != '/') 1.317 + $filter = '/'.$filter.'/i'; 1.318 + if (!preg_match($filter,$item)) continue; 1.319 + } 1.320 + if(is_dir($path.'/'.$item) and $item != '.' and $item != '..') { 1.321 + $folderlist[] = array( 1.322 + 'name' => $item, 1.323 + 'size' => 0, 1.324 + 'modtime'=> filemtime($path.'/'.$item), 1.325 + 'modtimeasc'=> my_filemtimeasc($path.'/'.$item), 1.326 + 'file_type' => "Directory" 1.327 + ); 1.328 + } 1.329 + elseif(my_is_file($path.'/'.$item)) { 1.330 + if(!$show_hidden_files) { 1.331 + if(substr($item, 0, 1) == "." or substr($item, -1) == "~") { 1.332 + continue; 1.333 + } 1.334 + } 1.335 + $filelist[] = array( 1.336 + 'name'=> $item, 1.337 + 'size'=> my_filesize($path.'/'.$item), 1.338 + 'modtime'=> my_filemtime($path.'/'.$item), 1.339 + 'modtimeasc'=> my_filemtimeasc($path.'/'.$item), 1.340 + 'file_type' => get_file_type($path.'/'.$item) 1.341 + ); 1.342 + } 1.343 + } 1.344 + closedir($handle); 1.345 +} 1.346 + 1.347 + 1.348 +if(!isset($_GET['s'])) { 1.349 + $_GET['s'] = 'name'; 1.350 +} 1.351 + 1.352 +// Figure out what to sort files by 1.353 +$file_order_by = array(); 1.354 +foreach ($filelist as $key=>$row) { 1.355 + $file_order_by[$key] = $row[$_GET['s']]; 1.356 +} 1.357 + 1.358 +// Figure out what to sort folders by 1.359 +$folder_order_by = array(); 1.360 +foreach ($folderlist as $key=>$row) { 1.361 + $folder_order_by[$key] = $row[$_GET['s']]; 1.362 +} 1.363 + 1.364 +// Order the files and folders 1.365 +$sort_type = SORT_ASC; 1.366 +$order = "&o=d"; 1.367 +if(isset($_GET['o'])) { 1.368 + $sort_type = SORT_DESC; 1.369 + $order = ""; 1.370 +} 1.371 +array_multisort($folder_order_by, $sort_type, $folderlist); 1.372 +array_multisort($file_order_by, $sort_type, $filelist); 1.373 + 1.374 + 1.375 +// Show sort methods 1.376 +print "<thead><tr>"; 1.377 + 1.378 +$sort_methods = array(); 1.379 +$sort_methods['name'] = "Name"; 1.380 +$sort_methods['modtime'] = "Last Modified"; 1.381 +$sort_methods['size'] = "Size"; 1.382 +$sort_methods['file_type'] = "Type"; 1.383 + 1.384 +foreach($sort_methods as $key=>$item) { 1.385 + if ($_GET['s'] == $key) $key = "$key$order"; 1.386 + print "<th class='n'><a href='?s=$key'>$item</a></th>"; 1.387 +} 1.388 +print "</tr></thead>\n<tbody>\n"; 1.389 + 1.390 + 1.391 + 1.392 +// Parent directory link 1.393 +if($path != "./") { 1.394 + print "<tr><td class='n'><a href='..'>Parent Directory</a>/</td>"; 1.395 + print "<td class='m'> </td>"; 1.396 + print "<td class='s'>- </td>"; 1.397 + print "<td class='t'>Directory</td></tr>\n"; 1.398 +} 1.399 + 1.400 + 1.401 + 1.402 +// Print folder information 1.403 +foreach($folderlist as $folder) { 1.404 + print "<tr><td class='n'><a href='" . addslashes($folder['name']). "'>" .htmlentities($folder['name']). "</a>/</td>"; 1.405 + print "<td class='m'>" . $folder['modtimeasc'] . "</td>"; 1.406 + print "<td class='s'>- </td>"; 1.407 + print "<td class='t'>" . $folder['file_type'] . "</td></tr>\n"; 1.408 +} 1.409 + 1.410 + 1.411 +// Print file information 1.412 +foreach($filelist as $file) { 1.413 + print "<tr><td class='n'><a href='" . addslashes($file['name']). "'>" .htmlentities($file['name']). "</a></td>"; 1.414 + print "<td class='m'>" . $file['modtimeasc'] . "</td>"; 1.415 + print "<td class='s'>" . format_bytes($file['size']) . "</td>"; 1.416 + print "<td class='t'>" . $file['file_type'] . "</td></tr>\n"; 1.417 +} 1.418 + 1.419 +// Print ending stuff 1.420 +$soft = explode('/',$_SERVER["SERVER_SOFTWARE"]); 1.421 +$tag = get_conf('server.tag','"','"',$soft[0].' <'.$soft[1].'>'); 1.422 +print "</tbody> 1.423 + </table> 1.424 + </div>"; 1.425 +if ($slitaz_style) { ?> 1.426 + 1.427 +<!-- End of content --> 1.428 +</div> 1.429 + 1.430 +<!-- Footer --> 1.431 +<div id="footer"> 1.432 + Copyright © <span class="year"></span> 1.433 + <a href="http://www.slitaz.org/">SliTaz</a> - Network: 1.434 + <a href="http://scn.slitaz.org/">Community</a> 1.435 + <a href="http://doc.slitaz.org/">Doc</a> 1.436 + <a href="http://forum.slitaz.org/">Forum</a> 1.437 + <a href="http://pkgs.slitaz.org/">Packages</a> 1.438 + <a href="http://bugs.slitaz.org">Bugs</a> 1.439 + <a href="http://hg.slitaz.org/">Hg</a> 1.440 + <p> 1.441 + SliTaz @ 1.442 + <a href="http://twitter.com/slitaz">Twitter</a> 1.443 + <a href="http://www.facebook.com/slitaz">Facebook</a> 1.444 + <a href="http://distrowatch.com/slitaz">Distrowatch</a> 1.445 + <a href="http://en.wikipedia.org/wiki/SliTaz">Wikipedia</a> 1.446 + <a href="http://flattr.com/profile/slitaz">Flattr</a> 1.447 + </p> 1.448 + <p> 1.449 + <a href="http://validator.w3.org/check?uri=referer"> 1.450 + <img src="/css/pics/website/xhtml10.png" 1.451 + alt="Valid XHTML 1.0" title="Code validé XHTML 1.0" 1.452 + style="width: 80px; height: 15px;" /></a> 1.453 + </p> 1.454 +</div> 1.455 + 1.456 +<?php } 1.457 +else print " 1.458 + <form action='".$_SERVER["REQUEST_URI"]."' method='get'> 1.459 + <div class='foot'>".$tag." 1.460 + <input type='text' name='f'/> 1.461 + <!-- <input type='submit' value='Filter' /> --> 1.462 + </div> 1.463 + </form> 1.464 +"; 1.465 +print "</body> 1.466 + </html>"; 1.467 +?>