view index.php @ 1:9ba6983b7c19

Copied readme from blog.
author edogawaconan <me@myconan.net>
date Thu, 16 Oct 2014 21:41:30 +0900
parents 8cba59d07798
children 9b5884ba214a
line wrap: on
line source

<?php
//real men use UTC for internets
date_default_timezone_set('UTC');
$wwwroot = $_ENV["DOCUMENT_ROOT"];
$link_prefix = "/";
$link_base = rtrim(preg_replace("#/+#", "/", urldecode($_ENV["REQUEST_URI"])),'/');
$link_path = substr($link_base,strlen(utf8_decode($link_prefix)));
$path = realpath("$wwwroot/".$link_path);
if (!$path) { die("fail"); }
$dir_handle = @opendir($path);
$to_dir = explode('/', trim($link_path,'/'));
array_unshift($to_dir, trim($link_prefix,'/'));
$files = array();
$dirs = array();
while ($file = readdir($dir_handle))
{
	if ($file == '.' or $file == '..') { continue; }
	elseif (is_dir("$path/$file")) { $dirs[] = $file; }
	else { $files[] = $file; }
}
sort($files);
sort($dirs);
function h($string) { return htmlspecialchars($string, ENT_QUOTES, 'UTF-8'); }
function a($string) { return preg_replace("#(%2F)+#", "/", rawurlencode($string)); }
function link_to($target, $title)
{
	return("<a href=\"".a($target)."\">".h($title)."</a>");
}
function print_header()
{
	global $link_prefix, $link_base, $to_dir;
	$path_tree = link_to('/', '[root]') . "/";
	foreach ($to_dir as $level => $dir)
	{
		if($dir)
		{
			$link = "/";
			for ($i = 0; $i <= $level; $i++) { $link .= "$to_dir[$i]/"; }
			$path_tree .= link_to($link, $dir)."/";
		}
	}
	header('Content-Type: text/html; charset=utf-8');
	echo "<!doctype html>
	      <meta http-equiv=\"Content-type\" content=\"text/html;charset=UTF-8\"> 
              <title>Index of ", h(rtrim($link_base,"/")."/"), "</title>
              <body>
              <h1>Index of $path_tree</h1>";
}
function nice_size($size)
{
	static $unit = array('B', 'KB', 'MB', 'GB', 'TB');
	for ($i = 0; $size >= 1000 && $i < count($unit); $i++) { $size /= 1000; }
	if ($i == 0) { return(sprintf("%d %s", $size, $unit[$i])); } 
	else { return(sprintf("%.2f %s", $size, $unit[$i])); }
}
function file_rows($files, $is_dir)
{
	global $path, $link_base, $link_prefix;
	static $unit = array('B', 'KB', 'MB', 'GB', 'TB');
	$file_rows = "";
	if ($is_dir)
	{
		$file_suffix = "/";
		if($link_base != $link_prefix) 
		{
			$file_rows .= "<tr><td colspan=3>".link_to(dirname($link_base),"..")."</td></tr>";
		}
	} else { $file_suffix = ""; }
	foreach($files as $file)
	{
		$file_stat = stat("$path/".$file);
		$file_rows .= "<tr><td>".
                              link_to("$link_base/".$file, $file.$file_suffix)."</td><td>".
                              h(strftime("%a %d-%b-%G %H:%M", $file_stat['mtime']))."</td><td>";
		if ($is_dir) { $file_rows .= "[dir]"; }
		else { $file_rows .= nice_size($file_stat['size']); }
		$file_rows .= "</tr>";
	}
	return $file_rows;
}
function print_list()
{
	global $dirs, $files;
	echo "<table><thead><tr>",
	     "<td>File</td>",
	     "<td>Date</td>",
	     "<td>Size</td>",
	     "</tr></thead>",
	     "<tbody>", file_rows($dirs, true), file_rows($files, false), "</tbody>",
	     "</table>";
}
function print_footer()
{
echo "</body>";
}
print_header();
print_list();
print_footer();
?>