Mercurial > dirlist-php
view index.php @ 3:9ea8be5b28d1
"refactor".
author | edogawaconan <me@myconan.net> |
---|---|
date | Tue, 21 Oct 2014 18:01:43 +0900 |
parents | 9b5884ba214a |
children | 7812af1c23ae |
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 path_tree_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)."/"; } } return "Index of $path_tree"; } function title() { global $link_base; return "Index of ".h(rtrim($link_base,"/")."/"); } function nice_size($size) { $thousand_units = array('ko', 'Mo', 'Go', 'To', 'Po'); $return_format = "%d %s"; if ($size <= 1) { $return_unit = "octet"; } elseif ($size < 10000) { $return_unit = "octets"; } else { $size /= 1000; for ($i = 0; $size >= 1000 && $i < count($thousand_units); $i++) { $size /= 1000; } $return_format = "%.2f %s"; $return_unit = $thousand_units[$i]; } return sprintf($return_format, $size, $return_unit); } function file_rows($files, $is_dir) { global $path, $link_base, $link_prefix; $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>"; $file_rows .= "<td>".link_to("$link_base/".$file.$file_suffix, $file.$file_suffix)."</td>"; $file_rows .= "<td>"; if ($is_dir) { $file_rows .= "[dir]"; } else { $file_rows .= nice_size($file_stat['size']); } $file_rows .= "</td>"; $file_rows .= "<td>".h(strftime("%Y-%m-%d %H:%M %Z", $file_stat['mtime']))."</td>"; $file_rows .= "</tr>"; } return $file_rows; } ?> <?php header('Content-Type: text/html; charset=utf-8'); ?> <!doctype html> <head> <meta http-equiv="Content-Type" content="text/html;charset=UTF-8"> <title><?php echo title(); ?></title> <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0"> <style type="text/css"> * { box-sizing: border-box; } body { font-family: Segoe UI, sans-serif; font-size: 14px; } h1 { margin: 5px; } table { width: 100%; } th:first-child, td:first-child { width: 100%; white-space: pre-wrap; word-wrap: break-word; word-break: break-all; } tr { position: relative; } th, td { white-space: nowrap; padding: 2px 5px; } @media (min-width: 768px) { th { background: #ccc; } tr:nth-child(even) { background: #eee; } tr:hover { background: #ddd; } } @media (max-width: 767px) { table { border-spacing: 0 10px; } th { display: none; } tr { background: #eee; } td { display: inline-block; } td:first-child { background: #ddd; padding: 5px; } table a { font-size: 18px; display: block; } } </style> </head> <body> <h1><?php echo path_tree_header(); ?></h1> <table> <thead><tr> <th>File</th> <th>Size</th> <th>Date</th> </tr></thead> <tbody> <?php echo file_rows($dirs, true); ?> <?php echo file_rows($files, false); ?> </tbody> </table> </body>