alberthrocks

test

Feb 10th, 2014
193
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?php
  2.  
  3. //
  4. // Simple Index - sindex.php
  5. //
  6. // Project home: http://jomppa.net/projects/simple-index/
  7. // Version: 0.1
  8. // Date: 2011-02-20
  9. // Author: Joni Rantala 2011
  10. // http://jomppa.net/
  11. //
  12.  
  13. main();
  14.  
  15. function main() {
  16. $args = &$_GET;
  17. if (empty($_GET["s"])) {
  18. $uri = $_SERVER["REQUEST_URI"];
  19. if (($pos = strpos($uri, "?")) !== false) {
  20. parse_str(substr($uri, $pos + 1), $a);
  21. $args = &$a;
  22. }
  23. }
  24.  
  25. $path = !empty($_GET["p"]) ? "./{$_GET["p"]}" : "./";
  26. $sort = !empty($args["s"]) && in_array($args["s"], array("n", "s", "m")) ? $args["s"] : "n";
  27. $direction = $args["d"] == "d" ? SORT_DESC : SORT_ASC;
  28.  
  29. header("Content-Type: text/html; charset=utf-8");
  30. printHeader();
  31. printFileListing(
  32. $path,
  33. "*",
  34. array(
  35. basename(__FILE__)
  36. ),
  37. $sort,
  38. $direction
  39. );
  40. printFooter();
  41. }
  42.  
  43. function printHeader() {
  44. ?>
  45. <!DOCTYPE html>
  46. <html>
  47. <head>
  48. <title>Index of <?php echo currentPath(); ?></title>
  49. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  50. <meta name="generator" content="Simple Index v0.1 - http://jomppa.net/projects/sindex/">
  51. <style type="text/css">
  52. body { margin: 20px; padding: 0; background: #cccccc; }
  53. a { color: #0088ff; }
  54. a:visited { color: #8800aa; }
  55. a img { border: none; }
  56. body, td, th { font-family: sans-serif; font-size: 12pt; text-align: left; vertical-align: top; color: #333333; }
  57. h1 { font-size: 16pt; text-align: center; }
  58. h1 a { color: #000000 !important; text-decoration: none; }
  59. p { text-align: center; font-size: 9pt; }
  60. p a { color: #666666 !important; }
  61. table { margin: 0 auto; border-collapse: collapse; border: 5px solid #ffffff; min-width: 400px; }
  62. th, td { padding: 5px 10px; }
  63. th { background: #888888; color: #ffffff; }
  64. th a { color: #ffffff !important; text-decoration: none; }
  65. th img { position: relative; top: -3px; left: 2px; }
  66. td { border-bottom: 1px dotted #cccccc; background: #ffffff; }
  67. tr.odd td { background: #f5f5f5; }
  68. small { font-size: 9pt; }
  69. </style>
  70. </head>
  71. <body>
  72. <?php
  73. }
  74.  
  75. function printFooter() {
  76. ?>
  77. </body>
  78. </html>
  79. <?php
  80. }
  81.  
  82. function currentPath() {
  83. return rtrim(dirname($_SERVER["SCRIPT_NAME"]) . "/" . ltrim($_GET["p"], "/"), "/");
  84. }
  85.  
  86. function parentDirectory() {
  87. $path = currentPath();
  88. $path = substr($path, 0, strrpos($path, "/"));
  89.  
  90. return $path == "" ? "/" : $path;
  91. }
  92.  
  93. function formatSize($size) {
  94. $units = array("B", "K", "M", "G", "T");
  95. for ($i = 0; $size >= 1024 && $i < 4; $i++) $size /= 1024;
  96. return round($size, 2) . " " . $units[$i];
  97. }
  98.  
  99. function printFileListing($path = "./", $pattern = "*", $excluded = array(), $sort = "n", $direction = SORT_ASC) {
  100. $files = glob("$path$pattern");
  101.  
  102. $sizes = array();
  103. $timestamps = array();
  104.  
  105. if (!is_array($files)) $files = array();
  106.  
  107. foreach ($files as $index => $file) {
  108. foreach ($excluded as $regexPattern) {
  109. if (preg_match("@$regexPattern@", $file)) {
  110. unset($files[$index]);
  111. continue 2;
  112. }
  113. }
  114.  
  115. $sizes[] = filesize($file);
  116. $timestamps[] = filemtime($file);
  117. }
  118.  
  119. switch ($sort) {
  120. // size
  121. case "s":
  122. array_multisort($sizes, $direction, $files, $timestamps);
  123. break;
  124. // modified
  125. case "m":
  126. array_multisort($timestamps, $direction, $files, $sizes);
  127. break;
  128. // name
  129. case "n":
  130. default:
  131. array_multisort($files, $direction, $sizes, $timestamps);
  132. break;
  133. }
  134.  
  135. $iconFile = "iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAMAAAAoLQ9TAAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJbWFnZVJlYWR5ccllPAAAAA9QTFRFAAAA5PX6yez1/////////SHUhAAAAAV0Uk5T/////wD7tg5TAAAAQUlEQVR42mJgQQMMIAwFCAEmJkZGRgZmBnQBsAhEAKieEaoJoQKkCLcAxBZGyrQwoWuBiDCg+QXuMAzfogCAAAMAPGcCZJ/5Pw0AAAAASUVORK5CYII=";
  136. $iconDir = "iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAMAAAAoLQ9TAAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJbWFnZVJlYWR5ccllPAAAABJQTFRF//IAAAAA8ucA////ysAA////eQEvdgAAAAZ0Uk5T//////8As7+kvwAAADtJREFUeNpiYEUDDKyMyAAswMLExAAFzIyMDCgKGJmBAkgKGOgrwIQsAHYpTAjkUlS/gAQwfIsGAAIMAN7QAc8Ckh4aAAAAAElFTkSuQmCC";
  137. $iconAsc = "iVBORw0KGgoAAAANSUhEUgAAAAcAAAAECAMAAAB1GNVPAAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJbWFnZVJlYWR5ccllPAAAAAZQTFRF////////VXz1bAAAAAJ0Uk5T/wDltzBKAAAAGklEQVR42mJgZGRkgGAGBhABohigJBAABBgAATAADUnnWMkAAAAASUVORK5CYII=";
  138. $iconDesc = "iVBORw0KGgoAAAANSUhEUgAAAAcAAAAECAMAAAB1GNVPAAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJbWFnZVJlYWR5ccllPAAAAAZQTFRF////////VXz1bAAAAAJ0Uk5T/wDltzBKAAAAF0lEQVR42mJggAJGKMkIYjCCaDAGCDAAAJAADcpaiWkAAAAASUVORK5CYII=";
  139.  
  140. $parent = explode("/", currentPath());
  141.  
  142. echo "<h1><a href=\"" . currentPath() . "\">Index of " . currentPath() . "</a></h1>\n";
  143. echo "<p><a href=\"" . parentDirectory() . "\">&larr; Parent directory</a></p>\n";
  144. echo "<table>\n";
  145. echo "\t<tr>\n\t\t<th></th>\n";
  146.  
  147. $labels = array(
  148. "n" => "Name",
  149. "s" => "Size",
  150. "m" => "Modified"
  151. );
  152.  
  153. foreach ($labels as $key => $label) {
  154. $link = "?s=$key";
  155. $image = "";
  156.  
  157. if ($sort == $key) {
  158. $d = $direction == SORT_DESC ? "a" : "d";
  159. $link .= "&amp;d=$d";
  160. $icon = $d == "a" ? $iconDesc : $iconAsc;
  161. $image = " <img src=\"data:image/png;base64,$icon\" alt=\"\" />";
  162. }
  163.  
  164. echo "\t\t<th><a href=\"$link\">$label$image</a></th>\n";
  165. }
  166.  
  167. echo "\t</tr>\n";
  168.  
  169. foreach ($files as $index => $file) {
  170. $name = basename($file);
  171. $url = currentPath() . "/" . rawurlencode($name);
  172. $isDir = is_dir($file);
  173. $type = $isDir ? "Directory" : "File";
  174. $icon = $isDir ? $iconDir : $iconFile;
  175. $size = $isDir ? "-" : formatSize($sizes[$index]);
  176. $modified = date("Y-m-d H:i:s", $timestamps[$index]);
  177. $class = $class == "even" ? "odd" : "even";
  178.  
  179. echo "\t<tr class=\"$class\">\n" .
  180. "\t\t<td><img src=\"data:image/png;base64,$icon\" title=\"$type\" alt=\"$type\" /></td>\n" .
  181. "\t\t<td><a href=\"$url\">$name</a></td>\n" .
  182. "\t\t<td><small>$size</small></td>\n" .
  183. "\t\t<td><small>$modified</small></td>\n" .
  184. "\t</tr>\n";
  185. }
  186.  
  187. echo "</table>\n";
  188. }
Add Comment
Please, Sign In to add comment