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