-
Notifications
You must be signed in to change notification settings - Fork 30
/
index.php
375 lines (318 loc) · 11.1 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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
<?php
// enable debug
ini_set('display_errors', 'On');
error_reporting(E_ALL | E_STRICT);
class Fm {
private $base_path = 'content';
/**
* Prints the page.
*/
public function index() {
echo file_get_contents('tpl.html');
}
/**
* Sends a success message back in json.
*/
private function success($msg) {
echo json_encode(array('status' => 'ok', 'msg' => $msg));
}
/**
* Sends a fail message back in json and aborts the execution.
*/
private function error($msg, $exit=true) {
echo json_encode(array('status' => 'fail', 'msg' => $msg));
if ($exit)
exit();
}
/**
* Checks path for '..' and replace spaces
*/
private function check_path(&$path) {
if (strpos($path, '..') === true)
$this->error('no .. allowed in path');
else
$path = str_replace(' ', '-', $path);
}
/**
* Takes a given path and prints the content in json format.
*/
public function browse() {
// check path
$this->check_path($_POST['path']);
// concat
$path = $this->base_path.$_POST['path'];
$files = array();
$folders = array();
foreach (scandir($path) as $f) {
if ($f == '.' || $f == '..')
continue;
$e = $this->get_file_info("$path/$f", array('name', 'size', 'date', 'fileperms'));
if (is_dir("$path/$f"))
$folders[] = array(
'name' => $e['name'],
'size' => '---',
'date' => date('Y-m-d H:i:s', $e['date']),
'perm' => $this->unix_perm_string($e['fileperms'])
);
else
$files[] = array(
'name' => $e['name'],
'size' => $this->human_filesize($e['size']),
'date' => date('Y-m-d H:i:s', $e['date']),
'perm' => $this->unix_perm_string($e['fileperms'])
);
}
echo json_encode(array('status' => 'ok', 'folders' => $folders, 'files' => $files));
}
/**
* Creates a file / folder according to type.
*/
public function create() {
// check path
$this->check_path($_POST['target']);
// concat
$target = $this->base_path.$_POST['target'];
// check if target already exists
if (file_exists($target))
$this->error('target already exists');
// check if dir is writeable
if (!is_writable(pathinfo($target, PATHINFO_DIRNAME)))
$this->error('target directory not writeable');
if ($_POST['type'] == 'folder') {
// create folder
if (mkdir($target))
$this->success('directory created');
else
$this->error('mkdir failed');
} elseif ($_POST['type'] == 'file') {
// create empty file
if (file_put_contents($target, '') !== false)
$this->success('file created');
else
$this->error('file has not been created');
} else {
$this->error('unknown type given');
}
}
/**
* Move file / folder from source to destination.
*/
public function move() {
// check paths
$this->check_path($_POST['source']);
$this->check_path($_POST['destination']);
// concat
$src = $this->base_path.$_POST['source'];
$dst = $this->base_path.$_POST['destination'];
// check if source exists
if (!file_exists($src))
$this->error('source file / folder does not exist');
// check if destination exists
if (file_exists($dst))
$this->error('destination file / folder already exists');
// check if destination path exists
if (!file_exists(pathinfo($dst, PATHINFO_DIRNAME)))
$this->error('destination path does not exist');
// check if source is writable
if (!is_writable($src))
$this->error('source file / folder is not writable');
// check if destination path is writable
if (!is_writable(pathinfo($dst, PATHINFO_DIRNAME)))
$this->error('destination path is not writable');
// move source to destination
if (@rename($src, $dst))
$this->success('moved file / folder');
else
$this->error('file / folder was not moved');
}
/**
* Removes target.
*/
public function remove() {
// check path
$this->check_path($_POST['target']);
// concat
$target = $this->base_path.$_POST['target'];
// check if target exists
if (!file_exists($target))
$this->error('target does not exist');
// check if target is writable
if (!is_writable($target))
$this->error('target is not writable');
// remove target
if (is_dir($target))
$result = @rmdir($target);
else
$result = @unlink($target);
// check result
if ($result)
$this->success('target has been removed');
else
$this->error('target has not been removed');
}
/**
* Receive uploaded files and save them at target.
*/
public function upload() {
// check path
$this->check_path($_POST['path']);
// concat
$path = $this->base_path.$_POST['path'];
// check if files are set
if (!isset($_FILES['files']['name'][0]))
$this->error('no files uploaded');
// restructure
$files = array();
foreach ($_FILES['files']['name'] as $n => $v) {
$files[$n] = array(
'name' => $_FILES['files']['name'][$n],
'type' => $_FILES['files']['type'][$n],
'tmp_name' => $_FILES['files']['tmp_name'][$n],
'error' => $_FILES['files']['error'][$n],
'size' => $_FILES['files']['size'][$n]
);
}
// check upload state
foreach ($files as $f)
if ($f['error'] > 0)
$this->error($f['name'].' was not uploaded successfully');
// replace spaces in filename
foreach ($files as $n => $f)
$files[$n]['name'] = str_replace(' ', '-', $f['name']);
// check if files already exists
foreach ($files as $f)
if (file_exists($path.$f['name']))
$this->error('a file named '.$f['name'].' already exists');
// check if path is writable
if (!is_writable($path))
$this->error('target path is not writable');
// move files from tmp
foreach ($files as $f)
if (!move_uploaded_file($f['tmp_name'], $path.$f['name']))
$this->error('file '.$f['name'].' was not moved from tmp to destination');
// success
$this->success(count($files).' files have been uploaded');
}
/**
* Show edit page of target.
*/
public function edit() {
// check path
$this->check_path($_POST['target']);
// concat
$target = $this->base_path.$_POST['target'];
// print editor ^^
echo '<textarea>'.htmlentities(file_get_contents($target)).'</textarea>';
}
/**
* Takes a file size in bytes and process a human readable filesize.
*/
private function human_filesize($bytes, $decimals = 2) {
$sz = 'BKMGTP';
$factor = floor((strlen($bytes) - 1) / 3);
return sprintf("%.{$decimals}f", $bytes / pow(1024, $factor)) . @$sz[$factor];
}
/**
* Taken from php.net, this function takes an permission value and builds a
* human_readable unix style permission string.
*/
private function unix_perm_string($perms) {
if (($perms & 0xC000) == 0xC000) {
// Socket
$info = 's';
} elseif (($perms & 0xA000) == 0xA000) {
// Symbolic Link
$info = 'l';
} elseif (($perms & 0x8000) == 0x8000) {
// Regular
$info = '-';
} elseif (($perms & 0x6000) == 0x6000) {
// Block special
$info = 'b';
} elseif (($perms & 0x4000) == 0x4000) {
// Directory
$info = 'd';
} elseif (($perms & 0x2000) == 0x2000) {
// Character special
$info = 'c';
} elseif (($perms & 0x1000) == 0x1000) {
// FIFO pipe
$info = 'p';
} else {
// Unknown
$info = 'u';
}
// Owner
$info .= (($perms & 0x0100) ? 'r' : '-');
$info .= (($perms & 0x0080) ? 'w' : '-');
$info .= (($perms & 0x0040) ?
(($perms & 0x0800) ? 's' : 'x' ) :
(($perms & 0x0800) ? 'S' : '-'));
// Group
$info .= (($perms & 0x0020) ? 'r' : '-');
$info .= (($perms & 0x0010) ? 'w' : '-');
$info .= (($perms & 0x0008) ?
(($perms & 0x0400) ? 's' : 'x' ) :
(($perms & 0x0400) ? 'S' : '-'));
// World
$info .= (($perms & 0x0004) ? 'r' : '-');
$info .= (($perms & 0x0002) ? 'w' : '-');
$info .= (($perms & 0x0001) ?
(($perms & 0x0200) ? 't' : 'x' ) :
(($perms & 0x0200) ? 'T' : '-'));
return $info;
}
/**
* Taken from code igniter. This function returns information about a given
* file.
*/
private function get_file_info($file, $returned_values = array('name', 'server_path', 'size', 'date')) {
if ( ! file_exists($file))
{
return FALSE;
}
if (is_string($returned_values))
{
$returned_values = explode(',', $returned_values);
}
foreach ($returned_values as $key)
{
switch ($key)
{
case 'name':
$fileinfo['name'] = substr(strrchr($file, DIRECTORY_SEPARATOR), 1);
break;
case 'server_path':
$fileinfo['server_path'] = $file;
break;
case 'size':
$fileinfo['size'] = filesize($file);
break;
case 'date':
$fileinfo['date'] = filemtime($file);
break;
case 'readable':
$fileinfo['readable'] = is_readable($file);
break;
case 'writable':
// There are known problems using is_weritable on IIS. It may not be reliable - consider fileperms()
$fileinfo['writable'] = is_writable($file);
break;
case 'executable':
$fileinfo['executable'] = is_executable($file);
break;
case 'fileperms':
$fileinfo['fileperms'] = fileperms($file);
break;
}
}
return $fileinfo;
}
}
$fm = new Fm();
// routing
if (isset($_POST['fun']))
$fm->$_POST['fun']();
else
$fm->index();
?>