1
|
<?php
|
2
|
|
3
|
$VERSION = "0.3";
|
4
|
|
5
|
/* Lighttpd Enhanced Directory Listing Script
|
6
|
* ------------------------------------------
|
7
|
* Author: Evan Fosmark
|
8
|
* Version: 2008.08.07
|
9
|
*
|
10
|
*
|
11
|
* GNU License Agreement
|
12
|
* ---------------------
|
13
|
* This program is free software; you can redistribute it and/or modify
|
14
|
* it under the terms of the GNU General Public License as published by
|
15
|
* the Free Software Foundation; either version 2 of the License, or
|
16
|
* (at your option) any later version.
|
17
|
*
|
18
|
* This program is distributed in the hope that it will be useful,
|
19
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
20
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
21
|
* GNU General Public License for more details.
|
22
|
*
|
23
|
* You should have received a copy of the GNU General Public License
|
24
|
* along with this program; if not, write to the Free Software
|
25
|
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
26
|
*
|
27
|
* http://www.gnu.org/licenses/gpl.txt
|
28
|
*/
|
29
|
|
30
|
/* Revision by KittyKatt
|
31
|
* ---------------------
|
32
|
* E-Mail: kittykatt@archlinux.us
|
33
|
* Website: http://www.archerseven.com/kittykatt/
|
34
|
* Version: 2010.03.01
|
35
|
*
|
36
|
* Revised original code to include hiding for directories prefixed with a "." (or hidden
|
37
|
* directories) as the script was only hiding files prefixed with a "." before. Also included more
|
38
|
* file extensions/definitions.
|
39
|
*
|
40
|
*/
|
41
|
|
42
|
$show_hidden_files = true;
|
43
|
$calculate_folder_size = false;
|
44
|
|
45
|
// Various file type associations
|
46
|
$movie_types = array('mpg','mpeg','avi','asf','mp3','wav','mp4','wma','aif','aiff','ram', 'midi','mid','asf','au','flac');
|
47
|
$image_types = array('jpg','jpeg','gif','png','tif','tiff','bmp','ico');
|
48
|
$archive_types = array('zip','cab','7z','gz','tar.bz2','tar.gz','tar','rar',);
|
49
|
$document_types = array('txt','text','doc','docx','abw','odt','pdf','rtf','tex','texinfo',);
|
50
|
$font_types = array('ttf','otf','abf','afm','bdf','bmf','fnt','fon','mgf','pcf','ttc','tfm','snf','sfd');
|
51
|
|
52
|
|
53
|
// Get the path (cut out the query string from the request_uri)
|
54
|
list($path) = explode('?', $_SERVER['REQUEST_URI']);
|
55
|
|
56
|
|
57
|
// Get the path that we're supposed to show.
|
58
|
$path = ltrim(rawurldecode($path), '/');
|
59
|
|
60
|
|
61
|
if(strlen($path) == 0) {
|
62
|
$path = "./";
|
63
|
}
|
64
|
|
65
|
|
66
|
// Can't call the script directly since REQUEST_URI won't be a directory
|
67
|
if($_SERVER['PHP_SELF'] == '/'.$path) {
|
68
|
die("Unable to call " . $path . " directly.");
|
69
|
}
|
70
|
|
71
|
|
72
|
// Make sure it is valid.
|
73
|
if(!is_dir($path)) {
|
74
|
die("<b>" . $path . "</b> is not a valid path.");
|
75
|
}
|
76
|
|
77
|
|
78
|
//
|
79
|
// Get the size in bytes of a folder
|
80
|
//
|
81
|
function foldersize($path) {
|
82
|
$size = 0;
|
83
|
if($handle = @opendir($path)){
|
84
|
while(($file = readdir($handle)) !== false) {
|
85
|
if(is_file($path."/".$file)){
|
86
|
$size += filesize($path."/".$file);
|
87
|
}
|
88
|
|
89
|
if(is_dir($path."/".$file)){
|
90
|
if($file != "." && $file != "..") {
|
91
|
$size += foldersize($path."/".$file);
|
92
|
}
|
93
|
}
|
94
|
}
|
95
|
}
|
96
|
|
97
|
return $size;
|
98
|
}
|
99
|
|
100
|
|
101
|
//
|
102
|
// This function returns the file size of a specified $file.
|
103
|
//
|
104
|
function format_bytes($size, $precision=0) {
|
105
|
$sizes = array('YB', 'ZB', 'EB', 'PB', 'TB', 'GB', 'MB', 'KB', 'B');
|
106
|
$total = count($sizes);
|
107
|
|
108
|
while($total-- && $size > 1024) $size /= 1024;
|
109
|
return sprintf('%.'.$precision.'f', $size).$sizes[$total];
|
110
|
}
|
111
|
|
112
|
|
113
|
//
|
114
|
// This function returns the mime type of $file.
|
115
|
//
|
116
|
function get_file_type($file) {
|
117
|
global $image_types, $movie_types;
|
118
|
|
119
|
$pos = strrpos($file, ".");
|
120
|
if ($pos === false) {
|
121
|
return "Unknown File";
|
122
|
}
|
123
|
|
124
|
$ext = rtrim(substr($file, $pos+1), "~");
|
125
|
if(in_array($ext, $image_types)) {
|
126
|
$type = "Image File";
|
127
|
|
128
|
} elseif(in_array($ext, $movie_types)) {
|
129
|
$type = "Video File";
|
130
|
|
131
|
} elseif(in_array($ext, $archive_types)) {
|
132
|
$type = "Compressed Archive";
|
133
|
|
134
|
} elseif(in_array($ext, $document_types)) {
|
135
|
$type = "Type Document";
|
136
|
|
137
|
} elseif(in_array($ext, $font_types)) {
|
138
|
$type = "Type Font";
|
139
|
|
140
|
} else {
|
141
|
$type = "File";
|
142
|
}
|
143
|
|
144
|
return(strtoupper($ext) . " " . $type);
|
145
|
}
|
146
|
|
147
|
|
148
|
|
149
|
// Print the heading stuff
|
150
|
$vpath = ($path != "./")?$path:"";
|
151
|
print "<?xml version='1.0' encoding='utf-8'?>
|
152
|
<!DOCTYPE html PUBLIC '-//W3C//DTD XHTML 1.1//EN' 'http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd'>
|
153
|
<html xmlns='http://www.w3.org/1999/xhtml' xml:lang='en'>
|
154
|
<head>
|
155
|
<title>Index of /" .$vpath. "</title>
|
156
|
<style type='text/css'>
|
157
|
a, a:active {text-decoration: none; color: blue;}
|
158
|
a:visited {color: #48468F;}
|
159
|
a:hover, a:focus {text-decoration: underline; color: red;}
|
160
|
body {background-color: #F5F5F5;}
|
161
|
h2 {margin-bottom: 12px;}
|
162
|
table {margin-left: 12px;}
|
163
|
th, td { font-family: 'Courier New', Courier, monospace; font-size: 10pt; text-align: left;}
|
164
|
th { font-weight: bold; padding-right: 14px; padding-bottom: 3px;}
|
165
|
td {padding-right: 14px;}
|
166
|
td.s, th.s {text-align: right;}
|
167
|
div.list { background-color: white; border-top: 1px solid #646464; border-bottom: 1px solid #646464; padding-top: 10px; padding-bottom: 14px;}
|
168
|
div.foot, div.script_title { font-family: 'Courier New', Courier, monospace; font-size: 10pt; color: #787878; padding-top: 4px;}
|
169
|
div.script_title {float:right;text-align:right;font-size:8pt;color:#999;}
|
170
|
</style>
|
171
|
</head>
|
172
|
<body>
|
173
|
<h2>Index of /" . $vpath ."</h2>
|
174
|
<div class='list'>
|
175
|
<table summary='Directory Listing' cellpadding='0' cellspacing='0'>";
|
176
|
|
177
|
|
178
|
|
179
|
|
180
|
// Get all of the folders and files.
|
181
|
$folderlist = array();
|
182
|
$filelist = array();
|
183
|
if($handle = @opendir($path)) {
|
184
|
while(($item = readdir($handle)) !== false) {
|
185
|
if(is_dir($path.'/'.$item) and $item != '.' and $item != '..') {
|
186
|
if( $show_hidden_files == "false" ) {
|
187
|
if(substr($item, 0, 1) == "." or substr($item, -1) == "~") {
|
188
|
continue;
|
189
|
}
|
190
|
}
|
191
|
$folderlist[] = array(
|
192
|
'name' => $item,
|
193
|
'size' => (($calculate_folder_size)?foldersize($path.'/'.$item):0),
|
194
|
'modtime'=> filemtime($path.'/'.$item),
|
195
|
'file_type' => "Directory"
|
196
|
);
|
197
|
}
|
198
|
|
199
|
elseif(is_file($path.'/'.$item)) {
|
200
|
if( $show_hidden_files == "false" ) {
|
201
|
if(substr($item, 0, 1) == "." or substr($item, -1) == "~") {
|
202
|
continue;
|
203
|
}
|
204
|
}
|
205
|
$filelist[] = array(
|
206
|
'name'=> $item,
|
207
|
'size'=> filesize($path.'/'.$item),
|
208
|
'modtime'=> filemtime($path.'/'.$item),
|
209
|
'file_type' => get_file_type($path.'/'.$item)
|
210
|
);
|
211
|
}
|
212
|
}
|
213
|
fclose($handle);
|
214
|
}
|
215
|
|
216
|
|
217
|
if(!isset($_GET['sort'])) {
|
218
|
$_GET['sort'] = 'name';
|
219
|
}
|
220
|
|
221
|
// Figure out what to sort files by
|
222
|
$file_order_by = array();
|
223
|
foreach ($filelist as $key=>$row) {
|
224
|
$file_order_by[$key] = $row[$_GET['sort']];
|
225
|
}
|
226
|
|
227
|
// Figure out what to sort folders by
|
228
|
$folder_order_by = array();
|
229
|
foreach ($folderlist as $key=>$row) {
|
230
|
$folder_order_by[$key] = $row[$_GET['sort']];
|
231
|
}
|
232
|
|
233
|
// Order the files and folders
|
234
|
if($_GET['order']) {
|
235
|
array_multisort($folder_order_by, SORT_DESC, $folderlist);
|
236
|
array_multisort($file_order_by, SORT_DESC, $filelist);
|
237
|
} else {
|
238
|
array_multisort($folder_order_by, SORT_ASC, $folderlist);
|
239
|
array_multisort($file_order_by, SORT_ASC, $filelist);
|
240
|
$order = "&order=desc";
|
241
|
}
|
242
|
|
243
|
|
244
|
// Show sort methods
|
245
|
print "<thead><tr>";
|
246
|
|
247
|
$sort_methods = array();
|
248
|
$sort_methods['name'] = "Name";
|
249
|
$sort_methods['modtime'] = "Last Modified";
|
250
|
$sort_methods['size'] = "Size";
|
251
|
$sort_methods['file_type'] = "Type";
|
252
|
|
253
|
foreach($sort_methods as $key=>$item) {
|
254
|
if($_GET['sort'] == $key) {
|
255
|
print "<th class='n'><a href='?sort=$key$order'>$item</a></th>";
|
256
|
} else {
|
257
|
print "<th class='n'><a href='?sort=$key'>$item</a></th>";
|
258
|
}
|
259
|
}
|
260
|
print "</tr></thead><tbody>";
|
261
|
|
262
|
|
263
|
|
264
|
// Parent directory link
|
265
|
if($path != "./") {
|
266
|
print "<tr><td class='n'><a href='..'>Parent Directory</a>/</td>";
|
267
|
print "<td class='m'> </td>";
|
268
|
print "<td class='s'> </td>";
|
269
|
print "<td class='t'>Directory</td></tr>";
|
270
|
}
|
271
|
|
272
|
|
273
|
|
274
|
// Print folder information
|
275
|
foreach($folderlist as $folder) {
|
276
|
print "<tr><td class='n'><a href='" . addslashes($folder['name']). "'>" .htmlentities($folder['name']). "</a>/</td>";
|
277
|
print "<td class='m'>" . date('Y-M-d H:m:s', $folder['modtime']) . "</td>";
|
278
|
print "<td class='s'>" . (($calculate_folder_size)?format_bytes($folder['size'], 2):'--') . " </td>";
|
279
|
print "<td class='t'>" . $folder['file_type'] . "</td></tr>";
|
280
|
}
|
281
|
|
282
|
|
283
|
|
284
|
// This simply creates an extra line for file/folder seperation
|
285
|
print "<tr><td colspan='4' style='height:7px;'></td></tr>";
|
286
|
|
287
|
|
288
|
|
289
|
// Print file information
|
290
|
foreach($filelist as $file) {
|
291
|
print "<tr><td class='n'><a href='" . addslashes($file['name']). "'>" .htmlentities($file['name']). "</a></td>";
|
292
|
print "<td class='m'>" . date('Y-M-d H:m:s', $file['modtime']) . "</td>";
|
293
|
print "<td class='s'>" . format_bytes($file['size'],2) . " </td>";
|
294
|
print "<td class='t'>" . $file['file_type'] . "</td></tr>";
|
295
|
}
|
296
|
|
297
|
|
298
|
|
299
|
// Print ending stuff
|
300
|
print "</tbody>
|
301
|
</table>
|
302
|
</div>
|
303
|
<div class='script_title'>Lighttpd Enhanced Directory Listing Script</div>
|
304
|
<div class='foot'>". $_ENV['SERVER_SOFTWARE'] . "</div>
|
305
|
</body>
|
306
|
</html>";
|
307
|
|
308
|
|
309
|
/* -------------------------------------------------------------------------------- *\
|
310
|
I hope you enjoyed my take on the enhanced directory listing script!
|
311
|
If you have any questions, feel free to contact me.
|
312
|
|
313
|
Regards,
|
314
|
Evan Fosmark < me@evanfosmark.com >
|
315
|
\* -------------------------------------------------------------------------------- */
|
316
|
?>
|