· 9 years ago · Nov 17, 2016, 01:58 PM
1'auto';
2$charset = 'ISO-8859-1';
3$homedir = './';
4$editcols = 80;
5$editrows = 25;
6$htaccess = '.htaccess';
7$htpasswd = '.htpasswd';
8if (get_magic_quotes_gpc()) {
9 array_walk($_GET, 'strip');
10 array_walk($_POST, 'strip');
11 array_walk($_REQUEST, 'strip');
12}
13if (array_key_exists('image', $_GET)) {
14 header('Content-Type: image/gif');
15 die(getimage($_GET['image']));
16}
17$delim = DIRECTORY_SEPARATOR;
18if (function_exists('php_uname')) {
19 $win = (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') ? true : false;
20} else {
21 $win = ($delim == '\\') ? true : false;
22}
23if (!empty($_SERVER['PATH_TRANSLATED'])) {
24 $scriptdir = dirname($_SERVER['PATH_TRANSLATED']);
25} elseif (!empty($_SERVER['SCRIPT_FILENAME'])) {
26 $scriptdir = dirname($_SERVER['SCRIPT_FILENAME']);
27} elseif (function_exists('getcwd')) {
28 $scriptdir = getcwd();
29} else {
30 $scriptdir = '.';
31}
32$homedir = relative2absolute($homedir, $scriptdir);
33
34$dir = (array_key_exists('dir', $_REQUEST)) ? $_REQUEST['dir'] : $homedir;
35
36if (array_key_exists('olddir', $_POST) && !path_is_relative($_POST['olddir'])) {
37 $dir = relative2absolute($dir, $_POST['olddir']);
38}
39
40$directory = simplify_path(addslash($dir));
41
42$files = array();
43$action = '';
44if (!empty($_POST['submit_all'])) {
45 $action = $_POST['action_all'];
46 for ($i = 0; $i < $_POST['num']; $i++) {
47 if (array_key_exists("checked$i", $_POST) && $_POST["checked$i"] == 'true') {
48 $files[] = $_POST["file$i"];
49 }
50 }
51} elseif (!empty($_REQUEST['action'])) {
52 $action = $_REQUEST['action'];
53 $files[] = relative2absolute($_REQUEST['file'], $directory);
54} elseif (!empty($_POST['submit_upload']) && !empty($_FILES['upload']['name'])) {
55 $files[] = $_FILES['upload'];
56 $action = 'upload';
57} elseif (array_key_exists('num', $_POST)) {
58 for ($i = 0; $i < $_POST['num']; $i++) {
59 if (array_key_exists("submit$i", $_POST)) break;
60 }
61 if ($i < $_POST['num']) {
62 $action = $_POST["action$i"];
63 $files[] = $_POST["file$i"];
64 }
65}
66if (empty($action) && (!empty($_POST['submit_create']) || (array_key_exists('focus', $_POST) && $_POST['focus'] == 'create')) && !empty($_POST['create_name'])) {
67 $files[] = relative2absolute($_POST['create_name'], $directory);
68 switch ($_POST['create_type']) {
69 case 'directory':
70 $action = 'create_directory';
71 break;
72 case 'file':
73 $action = 'create_file';
74 }
75}
76if (sizeof($files) == 0) $action = ''; else $file = reset($files);
77
78if ($lang == 'auto') {
79 if (array_key_exists('HTTP_ACCEPT_LANGUAGE', $_SERVER) && strlen($_SERVER['HTTP_ACCEPT_LANGUAGE']) >= 2) {
80 $lang = substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2);
81 } else {
82 $lang = 'en';
83 }
84}
85$words = getwords($lang);
86$cols = ($win) ? 4 : 7;
87if (!isset($dirpermission)) {
88 $dirpermission = (function_exists('umask')) ? (0777 & ~umask()) : 0755;
89}
90if (!isset($filepermission)) {
91 $filepermission = (function_exists('umask')) ? (0666 & ~umask()) : 0644;
92}
93if (!empty($_SERVER['SCRIPT_NAME'])) {
94 $self = html(basename($_SERVER['SCRIPT_NAME']));
95} elseif (!empty($_SERVER['PHP_SELF'])) {
96 $self = html(basename($_SERVER['PHP_SELF']));
97} else {
98 $self = '';
99}
100if (!empty($_SERVER['SERVER_SOFTWARE'])) {
101 if (strtolower(substr($_SERVER['SERVER_SOFTWARE'], 0, 6)) == 'apache') {
102 $apache = true;
103 } else {
104 $apache = false;
105 }
106} else {
107 $apache = true;
108}
109switch ($action) {
110case 'view':
111 if (is_script($file)) {
112 ob_start();
113 highlight_file($file);
114 $src = ereg_replace('<font color="([^"]*)">', '<span style="color: \1">', ob_get_contents());
115 $src = str_replace(array('</font>', "\r", "\n"), array('</span>', '', ''), $src);
116 ob_end_clean();
117 html_header();
118 echo '<h2 style="text-align: left; margin-bottom: 0">' . html($file) . '</h2>
119<hr />
120<table>
121<tr>
122<td style="text-align: right; vertical-align: top; color: gray; padding-right: 3pt; border-right: 1px solid gray">
123<pre style="margin-top: 0"><code>';
124 for ($i = 1; $i <= sizeof(file($file)); $i++) echo "$i\n";
125 echo '</code></pre>
126</td>
127<td style="text-align: left; vertical-align: top; padding-left: 3pt">
128<pre style="margin-top: 0">' . $src . '</pre>
129</td>
130</tr>
131</table>
132';
133 html_footer();
134 } else {
135 header('Content-Type: ' . getmimetype($file));
136 header('Content-Disposition: filename=' . basename($file));
137 readfile($file);
138 }
139 break;
140case 'download':
141 header('Pragma: public');
142 header('Expires: 0');
143 header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
144 header('Content-Type: ' . getmimetype($file));
145 header('Content-Disposition: attachment; filename=' . basename($file) . ';');
146 header('Content-Length: ' . filesize($file));
147 readfile($file);
148 break;
149case 'upload':
150 $dest = relative2absolute($file['name'], $directory);
151 if (@file_exists($dest)) {
152 listing_page(error('already_exists', $dest));
153 } elseif (@move_uploaded_file($file['tmp_name'], $dest)) {
154 listing_page(notice('uploaded', $file['name']));
155 } else {
156 listing_page(error('not_uploaded', $file['name']));
157 }
158 break;
159case 'create_directory':
160 if (@file_exists($file)) {
161 listing_page(error('already_exists', $file));
162 } else {
163 $old = @umask(0777 & ~$dirpermission);
164 if (@mkdir($file, $dirpermission)) {
165 listing_page(notice('created', $file));
166 } else {
167 listing_page(error('not_created', $file));
168 }
169 @umask($old);
170 }
171 break;
172case 'create_file':
173 if (@file_exists($file)) {
174 listing_page(error('already_exists', $file));
175 } else {
176 $old = @umask(0777 & ~$filepermission);
177 if (@touch($file)) {
178 edit($file);
179 } else {
180 listing_page(error('not_created', $file));
181 }
182 @umask($old);
183 }
184 break;
185case 'execute':
186 chdir(dirname($file));
187 $output = array();
188 $retval = 0;
189 exec('echo "./' . basename($file) . '" | /bin/sh', $output, $retval);
190 $error = ($retval == 0) ? false : true;
191 if (sizeof($output) == 0) $output = array('<' . $words['no_output'] . '>');
192 if ($error) {
193 listing_page(error('not_executed', $file, implode("\n", $output)));
194 } else {
195 listing_page(notice('executed', $file, implode("\n", $output)));
196 }
197 break;
198case 'delete':
199 if (!empty($_POST['no'])) {
200 listing_page();
201 } elseif (!empty($_POST['yes'])) {
202 $failure = array();
203 $success = array();
204 foreach ($files as $file) {
205 if (del($file)) {
206 $success[] = $file;
207 } else {
208 $failure[] = $file;
209 }
210 }
211 $message = '';
212 if (sizeof($failure) > 0) {
213 $message = error('not_deleted', implode("\n", $failure));
214 }
215 if (sizeof($success) > 0) {
216 $message .= notice('deleted', implode("\n", $success));
217 }
218 listing_page($message);
219 } else {
220 html_header();
221 echo '<form action="' . $self . '" method="post">
222<table class="dialog">
223<tr>
224<td class="dialog">
225';
226 request_dump();
227 echo "\t<b>" . word('really_delete') . '</b>
228 <p>
229';
230 foreach ($files as $file) {
231 echo "\t" . html($file) . "<br />\n";
232 }
233 echo ' </p>
234 <hr />
235 <input type="submit" name="no" value="' . word('no') . '" id="red_button" />
236 <input type="submit" name="yes" value="' . word('yes') . '" id="green_button" style="margin-left: 50px" />
237</td>
238</tr>
239</table>
240</form>
241';
242 html_footer();
243 }
244 break;
245case 'rename':
246 if (!empty($_POST['destination'])) {
247 $dest = relative2absolute($_POST['destination'], $directory);
248 if (!@file_exists($dest) && @rename($file, $dest)) {
249 listing_page(notice('renamed', $file, $dest));
250 } else {
251 listing_page(error('not_renamed', $file, $dest));
252 }
253 } else {
254 $name = basename($file);
255 html_header();
256 echo '<form action="' . $self . '" method="post">
257<table class="dialog">
258<tr>
259<td class="dialog">
260 <input type="hidden" name="action" value="rename" />
261 <input type="hidden" name="file" value="' . html($file) . '" />
262 <input type="hidden" name="dir" value="' . html($directory) . '" />
263 <b>' . word('rename_file') . '</b>
264 <p>' . html($file) . '</p>
265 <b>' . substr($file, 0, strlen($file) - strlen($name)) . '</b>
266 <input type="text" name="destination" size="' . textfieldsize($name) . '" value="' . html($name) . '" />
267 <hr />
268 <input type="submit" value="' . word('rename') . '" />
269</td>
270</tr>
271</table>
272<p><a href="' . $self . '?dir=' . urlencode($directory) . '">[ ' . word('back') . ' ]</a></p>
273</form>
274';
275 html_footer();
276 }
277 break;
278case 'move':
279 if (!empty($_POST['destination'])) {
280 $dest = relative2absolute($_POST['destination'], $directory);
281 $failure = array();
282 $success = array();
283 foreach ($files as $file) {
284 $filename = substr($file, strlen($directory));
285 $d = $dest . $filename;
286 if (!@file_exists($d) && @rename($file, $d)) {
287 $success[] = $file;
288 } else {
289 $failure[] = $file;
290 }
291 }
292 $message = '';
293 if (sizeof($failure) > 0) {
294 $message = error('not_moved', implode("\n", $failure), $dest);
295 }
296 if (sizeof($success) > 0) {
297 $message .= notice('moved', implode("\n", $success), $dest);
298 }
299 listing_page($message);
300 } else {
301 html_header();
302 echo '<form action="' . $self . '" method="post">
303<table class="dialog">
304<tr>
305<td class="dialog">
306';
307 request_dump();
308 echo "\t<b>" . word('move_files') . '</b>
309 <p>
310';
311 foreach ($files as $file) {
312 echo "\t" . html($file) . "<br />\n";
313 }
314 echo ' </p>
315 <hr />
316 ' . word('destination') . ':
317 <input type="text" name="destination" size="' . textfieldsize($directory) . '" value="' . html($directory) . '" />
318 <input type="submit" value="' . word('move') . '" />
319</td>
320</tr>
321</table>
322<p><a href="' . $self . '?dir=' . urlencode($directory) . '">[ ' . word('back') . ' ]</a></p>
323</form>
324';
325 html_footer();
326 }
327 break;
328case 'copy':
329 if (!empty($_POST['destination'])) {
330 $dest = relative2absolute($_POST['destination'], $directory);
331 if (@is_dir($dest)) {
332 $failure = array();
333 $success = array();
334 foreach ($files as $file) {
335 $filename = substr($file, strlen($directory));
336 $d = addslash($dest) . $filename;
337 if (!@is_dir($file) && !@file_exists($d) && @copy($file, $d)) {
338 $success[] = $file;
339 } else {
340 $failure[] = $file;
341 }
342 }
343 $message = '';
344 if (sizeof($failure) > 0) {
345 $message = error('not_copied', implode("\n", $failure), $dest);
346 }
347 if (sizeof($success) > 0) {
348 $message .= notice('copied', implode("\n", $success), $dest);
349 }
350 listing_page($message);
351 } else {
352 if (!@file_exists($dest) && @copy($file, $dest)) {
353 listing_page(notice('copied', $file, $dest));
354 } else {
355 listing_page(error('not_copied', $file, $dest));
356 }
357 }
358 } else {
359 html_header();
360 echo '<form action="' . $self . '" method="post">
361<table class="dialog">
362<tr>
363<td class="dialog">
364';
365 request_dump();
366 echo "\n<b>" . word('copy_files') . '</b>
367 <p>
368';
369 foreach ($files as $file) {
370 echo "\t" . html($file) . "<br />\n";
371 }
372 echo ' </p>
373 <hr />
374 ' . word('destination') . ':
375 <input type="text" name="destination" size="' . textfieldsize($directory) . '" value="' . html($directory) . '" />
376 <input type="submit" value="' . word('copy') . '" />
377</td>
378</tr>
379</table>
380<p><a href="' . $self . '?dir=' . urlencode($directory) . '">[ ' . word('back') . ' ]</a></p>
381</form>
382';
383 html_footer();
384 }
385 break;
386case 'create_symlink':
387 if (!empty($_POST['destination'])) {
388 $dest = relative2absolute($_POST['destination'], $directory);
389 if (substr($dest, -1, 1) == $delim) $dest .= basename($file);
390 if (!empty($_POST['relative'])) $file = absolute2relative(addslash(dirname($dest)), $file);
391 if (!@file_exists($dest) && @symlink($file, $dest)) {
392 listing_page(notice('symlinked', $file, $dest));
393 } else {
394 listing_page(error('not_symlinked', $file, $dest));
395 }
396 } else {
397 html_header();
398 echo '<form action="' . $self . '" method="post">
399<table class="dialog" id="symlink">
400<tr>
401 <td style="vertical-align: top">' . word('destination') . ': </td>
402 <td>
403 <b>' . html($file) . '</b><br />
404 <input type="checkbox" name="relative" value="yes" id="checkbox_relative" checked="checked" style="margin-top: 1ex" />
405 <label for="checkbox_relative">' . word('relative') . '</label>
406 <input type="hidden" name="action" value="create_symlink" />
407 <input type="hidden" name="file" value="' . html($file) . '" />
408 <input type="hidden" name="dir" value="' . html($directory) . '" />
409 </td>
410</tr>
411<tr>
412 <td>' . word('symlink') . ': </td>
413 <td>
414 <input type="text" name="destination" size="' . textfieldsize($directory) . '" value="' . html($directory) . '" />
415 <input type="submit" value="' . word('create_symlink') . '" />
416 </td>
417</tr>
418</table>
419<p><a href="' . $self . '?dir=' . urlencode($directory) . '">[ ' . word('back') . ' ]</a></p>
420</form>
421';
422 html_footer();
423 }
424 break;
425case 'edit':
426 if (!empty($_POST['save'])) {
427 $content = str_replace("\r\n", "\n", $_POST['content']);
428 if (($f = @fopen($file, 'w')) && @fwrite($f, $content) !== false && @fclose($f)) {
429 listing_page(notice('saved', $file));
430 } else {
431 listing_page(error('not_saved', $file));
432 }
433 } else {
434 if (@is_readable($file) && @is_writable($file)) {
435 edit($file);
436 } else {
437 listing_page(error('not_edited', $file));
438 }
439 }
440 break;
441case 'permission':
442 if (!empty($_POST['set'])) {
443 $mode = 0;
444 if (!empty($_POST['ur'])) $mode |= 0400; if (!empty($_POST['uw'])) $mode |= 0200; if (!empty($_POST['ux'])) $mode |= 0100;
445 if (!empty($_POST['gr'])) $mode |= 0040; if (!empty($_POST['gw'])) $mode |= 0020; if (!empty($_POST['gx'])) $mode |= 0010;
446 if (!empty($_POST['or'])) $mode |= 0004; if (!empty($_POST['ow'])) $mode |= 0002; if (!empty($_POST['ox'])) $mode |= 0001;
447
448 if (@chmod($file, $mode)) {
449 listing_page(notice('permission_set', $file, decoct($mode)));
450 } else {
451 listing_page(error('permission_not_set', $file, decoct($mode)));
452 }
453 } else {
454 html_header();
455 $mode = fileperms($file);
456 echo '<form action="' . $self . '" method="post">
457<table class="dialog">
458<tr>
459<td class="dialog">
460 <p style="margin: 0">' . phrase('permission_for', $file) . '</p>
461 <hr />
462 <table id="permission">
463 <tr>
464 <td></td>
465 <td style="border-right: 1px solid black">' . word('owner') . '</td>
466 <td style="border-right: 1px solid black">' . word('group') . '</td>
467 <td>' . word('other') . '</td>
468 </tr>
469 <tr>
470 <td style="text-align: right">' . word('read') . ':</td>
471 <td><input type="checkbox" name="ur" value="1"'; if ($mode & 00400) echo ' checked="checked"'; echo ' /></td>
472 <td><input type="checkbox" name="gr" value="1"'; if ($mode & 00040) echo ' checked="checked"'; echo ' /></td>
473 <td><input type="checkbox" name="or" value="1"'; if ($mode & 00004) echo ' checked="checked"'; echo ' /></td>
474 </tr>
475 <tr>
476 <td style="text-align: right">' . word('write') . ':</td>
477 <td><input type="checkbox" name="uw" value="1"'; if ($mode & 00200) echo ' checked="checked"'; echo ' /></td>
478 <td><input type="checkbox" name="gw" value="1"'; if ($mode & 00020) echo ' checked="checked"'; echo ' /></td>
479 <td><input type="checkbox" name="ow" value="1"'; if ($mode & 00002) echo ' checked="checked"'; echo ' /></td>
480 </tr>
481 <tr>
482 <td style="text-align: right">' . word('execute') . ':</td>
483 <td><input type="checkbox" name="ux" value="1"'; if ($mode & 00100) echo ' checked="checked"'; echo ' /></td>
484 <td><input type="checkbox" name="gx" value="1"'; if ($mode & 00010) echo ' checked="checked"'; echo ' /></td>
485 <td><input type="checkbox" name="ox" value="1"'; if ($mode & 00001) echo ' checked="checked"'; echo ' /></td>
486 </tr>
487 </table>
488 <hr />
489 <input type="submit" name="set" value="' . word('set') . '" />
490 <input type="hidden" name="action" value="permission" />
491 <input type="hidden" name="file" value="' . html($file) . '" />
492 <input type="hidden" name="dir" value="' . html($directory) . '" />
493</td>
494</tr>
495</table>
496<p><a href="' . $self . '?dir=' . urlencode($directory) . '">[ ' . word('back') . ' ]</a></p>
497</form>
498';
499 html_footer();
500 }
501 break;
502default:
503 listing_page();
504}
505function getlist ($directory) {
506 global $delim, $win;
507 if ($d = @opendir($directory)) {
508 while (($filename = @readdir($d)) !== false) {
509 $path = $directory . $filename;
510 if ($stat = @lstat($path)) {
511 $file = array(
512 'filename' => $filename,
513 'path' => $path,
514 'is_file' => @is_file($path),
515 'is_dir' => @is_dir($path),
516 'is_link' => @is_link($path),
517 'is_readable' => @is_readable($path),
518 'is_writable' => @is_writable($path),
519 'size' => $stat['size'],
520 'permission' => $stat['mode'],
521 'owner' => $stat['uid'],
522 'group' => $stat['gid'],
523 'mtime' => @filemtime($path),
524 'atime' => @fileatime($path),
525 'ctime' => @filectime($path)
526 );
527 if ($file['is_dir']) {
528 $file['is_executable'] = @file_exists($path . $delim . '.');
529 } else {
530 if (!$win) {
531 $file['is_executable'] = @is_executable($path);
532 } else {
533 $file['is_executable'] = true;
534 }
535 }
536 if ($file['is_link']) $file['target'] = @readlink($path);
537 if (function_exists('posix_getpwuid')) $file['owner_name'] = @reset(posix_getpwuid($file['owner']));
538 if (function_exists('posix_getgrgid')) $file['group_name'] = @reset(posix_getgrgid($file['group']));
539 $files[] = $file;
540 }
541 }
542 return $files;
543 } else {
544 return false;
545 }
546}
547function sortlist (&$list, $key, $reverse) {
548 quicksort($list, 0, sizeof($list) - 1, $key);
549 if ($reverse) $list = array_reverse($list);
550}
551function quicksort (&$array, $first, $last, $key) {
552 if ($first < $last) {
553 $cmp = $array[floor(($first + $last) / 2)][$key];
554 $l = $first;
555 $r = $last;
556 while ($l <= $r) {
557 while ($array[$l][$key] < $cmp) $l++;
558 while ($array[$r][$key] > $cmp) $r--;
559 if ($l <= $r) {
560 $tmp = $array[$l];
561 $array[$l] = $array[$r];
562 $array[$r] = $tmp;
563 $l++;
564 $r--;
565 }
566 }
567 quicksort($array, $first, $r, $key);
568 quicksort($array, $l, $last, $key);
569 }
570}
571function permission_octal2string ($mode) {
572 if (($mode & 0xC000) === 0xC000) {
573 $type = 's';
574 } elseif (($mode & 0xA000) === 0xA000) {
575 $type = 'l';
576 } elseif (($mode & 0x8000) === 0x8000) {
577 $type = '-';
578 } elseif (($mode & 0x6000) === 0x6000) {
579 $type = 'b';
580 } elseif (($mode & 0x4000) === 0x4000) {
581 $type = 'd';
582 } elseif (($mode & 0x2000) === 0x2000) {
583 $type = 'c';
584 } elseif (($mode & 0x1000) === 0x1000) {
585 $type = 'p';
586 } else {
587 $type = '?';
588 }
589 $owner = ($mode & 00400) ? 'r' : '-';
590 $owner .= ($mode & 00200) ? 'w' : '-';
591 if ($mode & 0x800) {
592 $owner .= ($mode & 00100) ? 's' : 'S';
593 } else {
594 $owner .= ($mode & 00100) ? 'x' : '-';
595 }
596 $group = ($mode & 00040) ? 'r' : '-';
597 $group .= ($mode & 00020) ? 'w' : '-';
598 if ($mode & 0x400) {
599 $group .= ($mode & 00010) ? 's' : 'S';
600 } else {
601 $group .= ($mode & 00010) ? 'x' : '-';
602 }
603 $other = ($mode & 00004) ? 'r' : '-';
604 $other .= ($mode & 00002) ? 'w' : '-';
605 if ($mode & 0x200) {
606 $other .= ($mode & 00001) ? 't' : 'T';
607 } else {
608 $other .= ($mode & 00001) ? 'x' : '-';
609 }
610 return $type . $owner . $group . $other;
611}
612function is_script ($filename) {
613 return ereg('\.php$|\.php3$|\.php4$|\.php5$', $filename);
614}
615function getmimetype ($filename) {
616 static $mimes = array(
617 '\.jpg$|\.jpeg$' => 'image/jpeg',
618 '\.gif$' => 'image/gif',
619 '\.png$' => 'image/png',
620 '\.html$|\.html$' => 'text/html',
621 '\.txt$|\.asc$' => 'text/plain',
622 '\.xml$|\.xsl$' => 'application/xml',
623 '\.pdf$' => 'application/pdf'
624 );
625 foreach ($mimes as $regex => $mime) {
626 if (eregi($regex, $filename)) return $mime;
627 }
628 return 'text/plain';
629}
630function del ($file) {
631 global $delim;
632 if (!@is_link($file) && !file_exists($file)) return false;
633 if (!@is_link($file) && @is_dir($file)) {
634 if ($dir = @opendir($file)) {
635 $error = false;
636 while (($f = readdir($dir)) !== false) {
637 if ($f != '.' && $f != '..' && !del($file . $delim . $f)) {
638 $error = true;
639 }
640 }
641 closedir($dir);
642 if (!$error) return @rmdir($file);
643 return !$error;
644 } else {
645 return false;
646 }
647 } else {
648 return @unlink($file);
649 }
650}
651function addslash ($directory) {
652 global $delim;
653 if (substr($directory, -1, 1) != $delim) {
654 return $directory . $delim;
655 } else {
656 return $directory;
657 }
658}
659function relative2absolute ($string, $directory) {
660 if (path_is_relative($string)) {
661 return simplify_path(addslash($directory) . $string);
662 } else {
663 return simplify_path($string);
664 }
665}
666function path_is_relative ($path) {
667 global $win;
668 if ($win) {
669 return (substr($path, 1, 1) != ':');
670 } else {
671 return (substr($path, 0, 1) != '/');
672 }
673}
674function absolute2relative ($directory, $target) {
675 global $delim;
676 $path = '';
677 while ($directory != $target) {
678 if ($directory == substr($target, 0, strlen($directory))) {
679 $path .= substr($target, strlen($directory));
680 break;
681 } else {
682 $path .= '..' . $delim;
683 $directory = substr($directory, 0, strrpos(substr($directory, 0, -1), $delim) + 1);
684 }
685 }
686 if ($path == '') $path = '.';
687 return $path;
688}
689function simplify_path ($path) {
690 global $delim;
691 if (@file_exists($path) && function_exists('realpath') && @realpath($path) != '') {
692 $path = realpath($path);
693 if (@is_dir($path)) {
694 return addslash($path);
695 } else {
696 return $path;
697 }
698 }
699 $pattern = $delim . '.' . $delim;
700 if (@is_dir($path)) {
701 $path = addslash($path);
702 }
703 while (strpos($path, $pattern) !== false) {
704 $path = str_replace($pattern, $delim, $path);
705 }
706 $e = addslashes($delim);
707 $regex = $e . '((\.[^\.' . $e . '][^' . $e . ']*)|(\.\.[^' . $e . ']+)|([^\.][^' . $e . ']*))' . $e . '\.\.' . $e;
708 while (ereg($regex, $path)) {
709 $path = ereg_replace($regex, $delim, $path);
710 }
711 return $path;
712}
713function human_filesize ($filesize) {
714 $suffices = 'kMGTPE';
715 $n = 0;
716 while ($filesize >= 1000) {
717 $filesize /= 1024;
718 $n++;
719 }
720 $filesize = round($filesize, 3 - strpos($filesize, '.'));
721 if (strpos($filesize, '.') !== false) {
722 while (in_array(substr($filesize, -1, 1), array('0', '.'))) {
723 $filesize = substr($filesize, 0, strlen($filesize) - 1);
724 }
725 }
726 $suffix = (($n == 0) ? '' : substr($suffices, $n - 1, 1));
727 return $filesize . " {$suffix}B";
728}
729function strip (&$str) {
730 $str = stripslashes($str);
731}
732function listing_page ($message = null) {
733 global $self, $directory, $sort, $reverse;
734 html_header();
735 $list = getlist($directory);
736 if (array_key_exists('sort', $_GET)) $sort = $_GET['sort']; else $sort = 'filename';
737 if (array_key_exists('reverse', $_GET) && $_GET['reverse'] == 'true') $reverse = true; else $reverse = false;
738 sortlist($list, $sort, $reverse);
739 echo '<h1 style="margin-bottom: 0">Nice</h1>
740<form enctype="multipart/form-data" action="' . $self . '" method="post">
741<table id="main">
742';
743 directory_choice();
744 if (!empty($message)) {
745 spacer();
746 echo $message;
747 }
748 if (@is_writable($directory)) {
749 upload_box();
750 create_box();
751 } else {
752 spacer();
753 }
754 if ($list) {
755 listing($list);
756 } else {
757 echo error('not_readable', $directory);
758 }
759 echo '</table>
760</form>
761';
762 html_footer();
763}
764function listing ($list) {
765 global $directory, $homedir, $sort, $reverse, $win, $cols, $date_format, $self;
766 echo '<tr class="listing">
767 <th style="text-align: center; vertical-align: middle"><img src="' . $self . '?image=smiley" alt="smiley" /></th>
768';
769 $d = 'dir=' . urlencode($directory) . '&';
770 if (!$reverse && $sort == 'filename') $r = '&reverse=true'; else $r = '';
771 echo "\t<th class=\"filename\"><a href=\"$self?{$d}sort=filename$r\">" . word('filename') . "</a></th>\n";
772 if (!$reverse && $sort == 'size') $r = '&reverse=true'; else $r = '';
773 echo "\t<th class=\"size\"><a href=\"$self?{$d}sort=size$r\">" . word('size') . "</a></th>\n";
774 if (!$win) {
775 if (!$reverse && $sort == 'permission') $r = '&reverse=true'; else $r = '';
776 echo "\t<th class=\"permission_header\"><a href=\"$self?{$d}sort=permission$r\">" . word('permission') . "</a></th>\n";
777 if (!$reverse && $sort == 'owner') $r = '&reverse=true'; else $r = '';
778 echo "\t<th class=\"owner\"><a href=\"$self?{$d}sort=owner$r\">" . word('owner') . "</a></th>\n";
779 if (!$reverse && $sort == 'group') $r = '&reverse=true'; else $r = '';
780 echo "\t<th class=\"group\"><a href=\"$self?{$d}sort=group$r\">" . word('group') . "</a></th>\n";
781 }
782 echo ' <th class="functions">' . word('functions') . '</th>
783</tr>
784';
785 for ($i = 0; $i < sizeof($list); $i++) {
786 $file = $list[$i];
787 $timestamps = 'mtime: ' . date($date_format, $file['mtime']) . ', ';
788 $timestamps .= 'atime: ' . date($date_format, $file['atime']) . ', ';
789 $timestamps .= 'ctime: ' . date($date_format, $file['ctime']);
790 echo '<tr class="listing">
791 <td class="checkbox"><input type="checkbox" name="checked' . $i . '" value="true" onfocus="activate(\'other\')" /></td>
792 <td class="filename" title="' . html($timestamps) . '">';
793 if ($file['is_link']) {
794 echo '<img src="' . $self . '?image=link" alt="link" /> ';
795 echo html($file['filename']) . ' → ';
796 $real_file = relative2absolute($file['target'], $directory);
797 if (@is_readable($real_file)) {
798 if (@is_dir($real_file)) {
799 echo '[ <a href="' . $self . '?dir=' . urlencode($real_file) . '">' . html($file['target']) . '</a> ]';
800 } else {
801 echo '<a href="' . $self . '?action=view&file=' . urlencode($real_file) . '">' . html($file['target']) . '</a>';
802 }
803 } else {
804 echo html($file['target']);
805 }
806 } elseif ($file['is_dir']) {
807 echo '<img src="' . $self . '?image=folder" alt="folder" /> [ ';
808 if ($win || $file['is_executable']) {
809 echo '<a href="' . $self . '?dir=' . urlencode($file['path']) . '">' . html($file['filename']) . '</a>';
810 } else {
811 echo html($file['filename']);
812 }
813 echo ' ]';
814 } else {
815 if (substr($file['filename'], 0, 1) == '.') {
816 echo '<img src="' . $self . '?image=hidden_file" alt="hidden file" /> ';
817 } else {
818 echo '<img src="' . $self . '?image=file" alt="file" /> ';
819 }
820 if ($file['is_file'] && $file['is_readable']) {
821 echo '<a href="' . $self . '?action=view&file=' . urlencode($file['path']) . '">' . html($file['filename']) . '</a>';
822 } else {
823 echo html($file['filename']);
824 }
825 }
826 if ($file['size'] >= 1000) {
827 $human = ' title="' . human_filesize($file['size']) . '"';
828 } else {
829 $human = '';
830 }
831 echo "\t<td class=\"size\"$human>{$file['size']} B</td>\n";
832 if (!$win) {
833 echo "\t<td class=\"permission\" title=\"" . decoct($file['permission']) . '">';
834 $l = !$file['is_link'] && (!function_exists('posix_getuid') || $file['owner'] == posix_getuid());
835 if ($l) echo '<a href="' . $self . '?action=permission&file=' . urlencode($file['path']) . '&dir=' . urlencode($directory) . '">';
836 echo html(permission_octal2string($file['permission']));
837 if ($l) echo '</a>';
838 echo "</td>\n";
839 if (array_key_exists('owner_name', $file)) {
840 echo "\t<td class=\"owner\" title=\"uid: {$file['owner']}\">{$file['owner_name']}</td>\n";
841 } else {
842 echo "\t<td class=\"owner\">{$file['owner']}</td>\n";
843 }
844 if (array_key_exists('group_name', $file)) {
845 echo "\t<td class=\"group\" title=\"gid: {$file['group']}\">{$file['group_name']}</td>\n";
846 } else {
847 echo "\t<td class=\"group\">{$file['group']}</td>\n";
848 }
849 }
850 echo ' <td class="functions">
851 <input type="hidden" name="file' . $i . '" value="' . html($file['path']) . '" />
852';
853 $actions = array();
854 if (function_exists('symlink')) {
855 $actions[] = 'create_symlink';
856 }
857 if (@is_writable(dirname($file['path']))) {
858 $actions[] = 'delete';
859 $actions[] = 'rename';
860 $actions[] = 'move';
861 }
862 if ($file['is_file'] && $file['is_readable']) {
863 $actions[] = 'copy';
864 $actions[] = 'download';
865 if ($file['is_writable']) $actions[] = 'edit';
866 }
867 if (!$win && function_exists('exec') && $file['is_file'] && $file['is_executable'] && file_exists('/bin/sh')) {
868 $actions[] = 'execute';
869 }
870 if (sizeof($actions) > 0) {
871 echo ' <select class="small" name="action' . $i . '" size="1">
872 <option value="">' . str_repeat(' ', 30) . '</option>
873';
874 foreach ($actions as $action) {
875 echo "\t\t<option value=\"$action\">" . word($action) . "</option>\n";
876 }
877 echo ' </select>
878 <input class="small" type="submit" name="submit' . $i . '" value=" > " onfocus="activate(\'other\')" />
879';
880 }
881 echo ' </td>
882</tr>
883';
884 }
885 echo '<tr class="listing_footer">
886 <td style="text-align: right; vertical-align: top"><img src="' . $self . '?image=arrow" alt=">" /></td>
887 <td colspan="' . ($cols - 1) . '">
888 <input type="hidden" name="num" value="' . sizeof($list) . '" />
889 <input type="hidden" name="focus" value="" />
890 <input type="hidden" name="olddir" value="' . html($directory) . '" />
891';
892 $actions = array();
893 if (@is_writable(dirname($file['path']))) {
894 $actions[] = 'delete';
895 $actions[] = 'move';
896 }
897 $actions[] = 'copy';
898 echo ' <select class="small" name="action_all" size="1">
899 <option value="">' . str_repeat(' ', 30) . '</option>
900';
901 foreach ($actions as $action) {
902 echo "\t\t<option value=\"$action\">" . word($action) . "</option>\n";
903 }
904 echo ' </select>
905 <input class="small" type="submit" name="submit_all" value=" > " onfocus="activate(\'other\')" />
906 </td>
907</tr>
908';
909}
910function directory_choice () {
911 global $directory, $homedir, $cols, $self;
912 echo '<tr>
913 <td colspan="' . $cols . '" id="directory">
914 <a href="' . $self . '?dir=' . urlencode($homedir) . '">' . word('directory') . '</a>:
915 <input type="text" name="dir" size="' . textfieldsize($directory) . '" value="' . html($directory) . '" onfocus="activate(\'directory\')" />
916 <input type="submit" name="changedir" value="' . word('change') . '" onfocus="activate(\'directory\')" />
917 </td>
918</tr>
919';
920}
921function upload_box () {
922 global $cols;
923 echo '<tr>
924 <td colspan="' . $cols . '" id="upload">
925 ' . word('file') . ':
926 <input type="file" name="upload" onfocus="activate(\'other\')" />
927 <input type="submit" name="submit_upload" value="' . word('upload') . '" onfocus="activate(\'other\')" />
928 </td>
929</tr>
930';
931}
932function create_box () {
933 global $cols;
934 echo '<tr>
935 <td colspan="' . $cols . '" id="create">
936 <select name="create_type" size="1" onfocus="activate(\'create\')">
937 <option value="file">' . word('file') . '</option>
938 <option value="directory">' . word('directory') . '</option>
939 </select>
940 <input type="text" name="create_name" onfocus="activate(\'create\')" />
941 <input type="submit" name="submit_create" value="' . word('create') . '" onfocus="activate(\'create\')" />
942 </td>
943</tr>
944';
945}
946function edit ($file) {
947 global $self, $directory, $editcols, $editrows, $apache, $htpasswd, $htaccess;
948 html_header();
949 echo '<h2 style="margin-bottom: 3pt">' . html($file) . '</h2>
950<form action="' . $self . '" method="post">
951<table class="dialog">
952<tr>
953<td class="dialog">
954 <textarea name="content" cols="' . $editcols . '" rows="' . $editrows . '" WRAP="off">';
955 if (array_key_exists('content', $_POST)) {
956 echo $_POST['content'];
957 } else {
958 $f = fopen($file, 'r');
959 while (!feof($f)) {
960 echo html(fread($f, 8192));
961 }
962 fclose($f);
963 }
964 if (!empty($_POST['user'])) {
965 echo "\n" . $_POST['user'] . ':' . crypt($_POST['password']);
966 }
967 if (!empty($_POST['basic_auth'])) {
968 if ($win) {
969 $authfile = str_replace('\\', '/', $directory) . $htpasswd;
970 } else {
971 $authfile = $directory . $htpasswd;
972 }
973 echo "\nAuthType Basic\nAuthName "Restricted Directory"\n";
974 echo 'AuthUserFile "' . html($authfile) . ""\n";
975 echo 'Require valid-user';
976 }
977 echo '</textarea>
978 <hr />
979';
980 if ($apache && basename($file) == $htpasswd) {
981 echo '
982 ' . word('user') . ': <input type="text" name="user" />
983 ' . word('password') . ': <input type="password" name="password" />
984 <input type="submit" value="' . word('add') . '" />
985 <hr />
986';
987 }
988 if ($apache && basename($file) == $htaccess) {
989 echo '
990 <input type="submit" name="basic_auth" value="' . word('add_basic_auth') . '" />
991 <hr />
992';
993 }
994 echo '
995 <input type="hidden" name="action" value="edit" />
996 <input type="hidden" name="file" value="' . html($file) . '" />
997 <input type="hidden" name="dir" value="' . html($directory) . '" />
998 <input type="reset" value="' . word('reset') . '" id="red_button" />
999 <input type="submit" name="save" value="' . word('save') . '" id="green_button" style="margin-left: 50px" />
1000</td>
1001</tr>
1002</table>
1003<p><a href="' . $self . '?dir=' . urlencode($directory) . '">[ ' . word('back') . ' ]</a></p>
1004</form>
1005';
1006 html_footer();
1007}
1008function spacer () {
1009 global $cols;
1010 echo '<tr>
1011 <td colspan="' . $cols . '" style="height: 1em"></td>
1012</tr>
1013';
1014}
1015function textfieldsize ($content) {
1016 $size = strlen($content) + 5;
1017 if ($size < 30) $size = 30;
1018 return $size;
1019}
1020function request_dump () {
1021 foreach ($_REQUEST as $key => $value) {
1022 echo "\t<input type=\"hidden\" name=\"" . html($key) . '" value="' . html($value) . "\" />\n";
1023 }
1024}
1025function html ($string) {
1026 global $charset;
1027 return htmlentities($string, ENT_COMPAT, $charset);
1028}
1029function word ($word) {
1030 global $words, $word_charset;
1031 return htmlentities($words[$word], ENT_COMPAT, $word_charset);
1032}
1033function phrase ($phrase, $arguments) {
1034 global $words;
1035 static $search;
1036 if (!is_array($search)) for ($i = 1; $i <= 8; $i++) $search[] = "%$i";
1037 for ($i = 0; $i < sizeof($arguments); $i++) {
1038 $arguments[$i] = nl2br(html($arguments[$i]));
1039 }
1040 $replace = array('{' => '<pre>', '}' =>'</pre>', '[' => '<b>', ']' => '</b>');
1041 return str_replace($search, $arguments, str_replace(array_keys($replace), $replace, nl2br(html($words[$phrase]))));
1042}
1043function getwords ($lang) {
1044 global $word_charset, $date_format;
1045 switch ($lang) {
1046 case 'en':
1047 default:
1048 $date_format = 'n/j/y H:i:s';
1049 $word_charset = 'ISO-8859-1';
1050 return array(
1051'directory' => 'Directory',
1052'file' => 'File',
1053'filename' => 'Filename',
1054'size' => 'Size',
1055'permission' => 'Permission',
1056'owner' => 'Owner',
1057'group' => 'Group',
1058'other' => 'Others',
1059'functions' => 'Functions',
1060'read' => 'read',
1061'write' => 'write',
1062'execute' => 'execute',
1063'create_symlink' => 'create symlink',
1064'delete' => 'delete',
1065'rename' => 'rename',
1066'move' => 'move',
1067'copy' => 'copy',
1068'edit' => 'edit',
1069'download' => 'download',
1070'upload' => 'upload',
1071'create' => 'create',
1072'change' => 'change',
1073'save' => 'save',
1074'set' => 'set',
1075'reset' => 'reset',
1076'relative' => 'Relative path to target',
1077'yes' => 'Yes',
1078'no' => 'No',
1079'back' => 'back',
1080'destination' => 'Destination',
1081'symlink' => 'Symlink',
1082'no_output' => 'no output',
1083'user' => 'User',
1084'password' => 'Password',
1085'add' => 'add',
1086'add_basic_auth' => 'add basic-authentification',
1087'uploaded' => '"[%1]" has been uploaded.',
1088'not_uploaded' => '"[%1]" could not be uploaded.',
1089'already_exists' => '"[%1]" already exists.',
1090'created' => '"[%1]" has been created.',
1091'not_created' => '"[%1]" could not be created.',
1092'really_delete' => 'Delete these files?',
1093'deleted' => "These files have been deleted:\n[%1]",
1094'not_deleted' => "These files could not be deleted:\n[%1]",
1095'rename_file' => 'Rename file:',
1096'renamed' => '"[%1]" has been renamed to "[%2]".',
1097'not_renamed' => '"[%1] could not be renamed to "[%2]".',
1098'move_files' => 'Move these files:',
1099'moved' => "These files have been moved to \"[%2]\":\n[%1]",
1100'not_moved' => "These files could not be moved to \"[%2]\":\n[%1]",
1101'copy_files' => 'Copy these files:',
1102'copied' => "These files have been copied to \"[%2]\":\n[%1]",
1103'not_copied' => "These files could not be copied to \"[%2]\":\n[%1]",
1104'not_edited' => '"[%1]" can not be edited.',
1105'executed' => "\"[%1]\" has been executed successfully:\n{%2}",
1106'not_executed' => "\"[%1]\" could not be executed successfully:\n{%2}",
1107'saved' => '"[%1]" has been saved.',
1108'not_saved' => '"[%1]" could not be saved.',
1109'symlinked' => 'Symlink from "[%2]" to "[%1]" has been created.',
1110'not_symlinked' => 'Symlink from "[%2]" to "[%1]" could not be created.',
1111'permission_for' => 'Permission of "[%1]":',
1112'permission_set' => 'Permission of "[%1]" was set to [%2].',
1113'permission_not_set' => 'Permission of "[%1]" could not be set to [%2].',
1114'not_readable' => '"[%1]" can not be read.'
1115 );
1116 }
1117}
1118function getimage ($image) {
1119 switch ($image) {
1120 case 'file':
1121 return base64_decode('R0lGODlhEQANAJEDAJmZmf///wAAAP///yH5BAHoAwMALAAAAAARAA0AAAItnIGJxg0B42rsiSvCA/REmXQWhmnih3LUSGaqg35vFbSXucbSabunjnMohq8CADsA');
1122 case 'folder':
1123 return base64_decode('R0lGODlhEQANAJEDAJmZmf///8zMzP///yH5BAHoAwMALAAAAAARAA0AAAIqnI+ZwKwbYgTPtIudlbwLOgCBQJYmCYrn+m3smY5vGc+0a7dhjh7ZbygAADsA');
1124 case 'hidden_file':
1125 return base64_decode('R0lGODlhEQANAJEDAMwAAP///5mZmf///yH5BAHoAwMALAAAAAARAA0AAAItnIGJxg0B42rsiSvCA/REmXQWhmnih3LUSGaqg35vFbSXucbSabunjnMohq8CADsA');
1126 case 'link':
1127 return base64_decode('R0lGODlhEQANAKIEAJmZmf///wAAAMwAAP///wAAAAAAAAAAACH5BAHoAwQALAAAAAARAA0AAAM5SArcrDCCQOuLcIotwgTYUllNOA0DxXkmhY4shM5zsMUKTY8gNgUvW6cnAaZgxMyIM2zBLCaHlJgAADsA');
1128 case 'smiley':
1129 return base64_decode('R0lGODlhEQANAJECAAAAAP//AP///wAAACH5BAHoAwIALAAAAAARAA0AAAIslI+pAu2wDAiz0jWD3hqmBzZf1VCleJQch0rkdnppB3dKZuIygrMRE/oJDwUAOwA=');
1130 case 'arrow':
1131 return base64_decode('R0lGODlhEQANAIABAAAAAP///yH5BAEKAAEALAAAAAARAA0AAAIdjA9wy6gNQ4pwUmav0yvn+hhJiI3mCJ6otrIkxxQAOw==');
1132 }
1133}
1134function html_header () {
1135 global $charset;
1136 echo <<<END
1137<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
1138 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
1139<html xmlns="http://www.w3.org/1999/xhtml">
1140<head>
1141<meta http-equiv="Content-Type" content="text/html; charset=$charset" />
1142<title>webadmin.php</title>
1143<style type="text/css">
1144body { font: small sans-serif; text-align: center }
1145img { width: 17px; height: 13px }
1146a, a:visited { text-decoration: none; color: navy }
1147hr { border-style: none; height: 1px; background-color: silver; color: silver }
1148#main { margin-top: 6pt; margin-left: auto; margin-right: auto; border-spacing: 1px }
1149#main th { background: #eee; padding: 3pt 3pt 0pt 3pt }
1150.listing th, .listing td { padding: 1px 3pt 0 3pt }
1151.listing th { border: 1px solid silver }
1152.listing td { border: 1px solid #ddd; background: white }
1153.listing .checkbox { text-align: center }
1154.listing .filename { text-align: left }
1155.listing .size { text-align: right }
1156.listing .permission_header { text-align: left }
1157.listing .permission { font-family: monospace }
1158.listing .owner { text-align: left }
1159.listing .group { text-align: left }
1160.listing .functions { text-align: left }
1161.listing_footer td { background: #eee; border: 1px solid silver }
1162#directory, #upload, #create, .listing_footer td, #error td, #notice td { text-align: left; padding: 3pt }
1163#directory { background: #eee; border: 1px solid silver }
1164#upload { padding-top: 1em }
1165#create { padding-bottom: 1em }
1166.small, .small option { font-size: x-small }
1167textarea { border: none; background: white }
1168table.dialog { margin-left: auto; margin-right: auto }
1169td.dialog { background: #eee; padding: 1ex; border: 1px solid silver; text-align: center }
1170#permission { margin-left: auto; margin-right: auto }
1171#permission td { padding-left: 3pt; padding-right: 3pt; text-align: center }
1172td.permission_action { text-align: right }
1173#symlink { background: #eee; border: 1px solid silver }
1174#symlink td { text-align: left; padding: 3pt }
1175#red_button { width: 120px; color: #400 }
1176#green_button { width: 120px; color: #040 }
1177#error td { background: maroon; color: white; border: 1px solid silver }
1178#notice td { background: green; color: white; border: 1px solid silver }
1179#notice pre, #error pre { background: silver; color: black; padding: 1ex; margin-left: 1ex; margin-right: 1ex }
1180code { font-size: 12pt }
1181td { white-space: nowrap }
1182</style>
1183<script type="text/javascript">
1184<!--
1185function activate (name) {
1186 if (document && document.forms[0] && document.forms[0].elements['focus']) {
1187 document.forms[0].elements['focus'].value = name;
1188 }
1189}
1190//-->
1191</script>
1192</head>
1193<body>
1194END;
1195}
1196function html_footer () {
1197 echo <<<END
1198</body>
1199</html>
1200END;
1201}
1202function notice ($phrase) {
1203 global $cols;
1204 $args = func_get_args();
1205 array_shift($args);
1206 return '<tr id="notice">
1207 <td colspan="' . $cols . '">' . phrase($phrase, $args) . '</td>
1208</tr>
1209';
1210}
1211function error ($phrase) {
1212 global $cols;
1213 $args = func_get_args();
1214 array_shift($args);
1215 return '<tr id="error">
1216 <td colspan="' . $cols . '">' . phrase($phrase, $args) . '</td>
1217</tr>
1218';
1219}
1220?>