-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.php
211 lines (191 loc) · 5.41 KB
/
index.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
<?php
/**
* nixList :: Directory Listing
*
* A script to list and browse files in directories
*
* @author tsjost <[email protected]>
* @copyright 2009 tsjost
* @version 0.0.1
*/
/** CONFIG **/
$CONFIG['username'] = 'Guest'; // Leave blank to use the webserver's user
$CONFIG['hostname'] = ''; // Leave blank to use the domain name
$CONFIG['numColumns'] = 5;
$CONFIG['showLegend'] = false; // Display the legend for different colors
$CONFIG['showSelf'] = false; // Display this file in the directory listing
$CONFIG['showBackup'] = false; // Display backup files (~)
/** DO NOT EDIT BELOW IF YOU DO NOT KNOW EXACTLY WHAT YOU ARE DOING **/
function getLs($path, $file) {
global $CONFIG;
if ($CONFIG['showSelf'] == false and $path == './' and $file == basename(__FILE__)) {
return false;
}
if ($CONFIG['showBackup'] == false and substr($file,-1) == '~') {
return false;
}
if ($path == './' and $file == '..') {
return false;
}
$linktitle = '';
$class = '';
if ( ! is_link($path . $file) or (is_link($path . $file) and file_exists($path . readlink($path . $file)))) {
$permissions = substr(decoct(fileperms($path.$file)), -3);
$permArr = str_split($permissions);
}
if (is_dir($path.$file)) {
$class = 'dir';
if ($file == '.') {
$urlpath = substr($path, 2);
if (empty($urlpath)) {
$link = ROOTPATH;
} else {
$link = ROOTPATH .'?dir='. $urlpath;
}
} else if ($file == '..') {
$dirs = explode('/', substr($path, 2, -1));
if (count($dirs) == 1) {
$link = ROOTPATH;
} else {
unset($dirs[count($dirs) - 1]);
$link = ROOTPATH .'?dir='. implode('/', $dirs) .'/';
}
} else
$link = '?dir=' . substr($path, 2) . $file . '/';
} else if (is_link($path . $file)) {
$link = $linktitle = readlink($path . $file);
$link = $path . $link;
if (file_exists($link)) {
$class = 'symlink';
} else {
$class = 'symlinkbroken';
}
} else if (preg_match('/\.(jpg|jpeg|png|bmp|tif|gif)$/i', $file)) {
$class = 'image';
$link = $path . $file;
} else if (preg_match('/\.(iso|tar|bz2|gz|[s]?7z|arj|cab|rar|tgz|zip)$/i', $file)) {
$class = 'archive';
$link = $path . $file;
} else {
foreach($permArr as $permission) {
if ($permission & 1) {
$class = 'executable';
break;
}
}
$link = $path . $file;
}
$return = '<a href="'. $link .'" title="'. $linktitle .'"><span class="'. $class .'">'. $file .'</span></a><br>';
return $return;
}
$rootpath = 'http://'. $_SERVER['HTTP_HOST'] . substr($_SERVER['PHP_SELF'], 0, strrpos($_SERVER['PHP_SELF'], '/')) .'/';
define('ROOTPATH', $rootpath);
define('VERSION', '0.0.1');
define('CURRENTPATH', ROOTPATH . empty($_SERVER['QUERY_STRING']) ? '' : '?'. $_SERVER['QUERY_STRING']);
$username = empty($CONFIG['username']) ? trim(`whoami`) : $CONFIG['username'];
$hostname = empty($CONFIG['hostname']) ? $_SERVER['HTTP_HOST'] : $CONFIG['hostname'];
$path = '';
if ( ! empty($_GET['dir'])) {
$path = $_GET['dir'];
$path = str_replace('..', '', $path);
$path = str_replace('./', '', $path);
if (substr($path, 0, 1) == '.') {
$path = substr($path, 1);
}
if (substr($path, 0, 1) == '/') {
$path = substr($path, 1);
}
do {
$path = str_replace('//','',$path,$count);
} while($count);
if (substr($path, -1) != '/') {
$path = $path . '/';
}
if ($path == '/') {
unset($path);
}
if ($path != $_GET['dir']) {
header('HTTP/1.1 301 Moved Permanently');
if (empty($path)) {
header('Location: '. ROOTPATH);
} else {
header('Location: '. ROOTPATH .'?dir='. $path);
}
die();
}
}
$path = './'. $path;
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>nixList v<?=VERSION?></title>
<style>
body { background-color:#000; color:#FFF; font:11px Monospace; }
#dirlist { float:left; margin:10px; }
a { color:#FFF; }
#dirlist a { text-decoration:none; }
#dirlist span { padding:0 2px; }
.dir { color:#729fcf; font-weight:bold; }
.symlink { color:#34e2e2; font-weight:bold; }
.symlinkbroken { color:#ef2929; font-weight:bold; background-color:#2e3436; }
.image { color:#ad7fa8; font-weight:bold; }
.archive { color:#ef2929; font-weight:bold; }
.executable { color:#8ae234; font-weight:bold; }
</style>
</head>
<body>
<p>
<?=$username?>@<?=$hostname?>:<?=substr($path,1)?>$ ./about<br>
/**<br>
* nixList :: Directory Listing<br>
*<br>
* @author tsjost <<a href="https://tsjo.st">tsjo.st</a>><br>
* @version <?=VERSION?><br>
* @link <a href="https://github.com/tsjost/nixlist">https://github.com/tsjost/nixlist</a><br>
*/
</p>
<p>
<?=$username?>@<?=$hostname?>:<?=substr($path, 1)?>$ ls
</p>
<div id="dirlist">
<?php
if (is_dir($path)) {
$files = scandir($path);
if ($CONFIG['numColumns'] > 0) {
$perColumn = ceil( count($files) / $CONFIG['numColumns'] );
} else {
$perColumn = count($files);
}
$i = 0;
foreach($files as $file) {
if ($i >= $perColumn) {
$i = 0;
echo '</div><div id="dirlist">'."\n";
}
$list = getLs($path, $file);
if ($list != false) {
echo $list."\n";
$i++;
}
}
} else {
echo 'No such file or directory.';
}
?>
</div>
<?php if ($CONFIG['showLegend'] == true): ?>
<p style="clear:left;">
<?=$username?>@<?=$hostname?>:<?=substr($path,1)?>$ ./legend<br>
Normal file<br>
<span class="dir">Directory</span><br>
<span class="symlink">Symlink</span><br>
<span class="symlinkbroken">Broken symlink</span><br>
<span class="image">Image</span><br>
<span class="archive">Archive</span><br>
<span class="executable">Executable</span><br>
</p>
<?php endif ?>
</body>
</html>