forked from auracms/AuraCMS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
filemanager.php
executable file
·500 lines (450 loc) · 17.7 KB
/
filemanager.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
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
<?php
/*************************************************************************
Program: File Manager for TinyMCE 3.
Version: 0.13
Author: Mad182
E-mail: [email protected]
WEB: http://sourceforge.net/projects/tinyfilemanager/
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
TO DO:
* image manipulations - crop/resize, filters, etc. - partly done;
* fix bugs in IE and Opera
*************************************************************************/
//user authentification (add your own, if you need one)
//if (NO AUTENTIFICATION) { die('Unauthorized!'); }
session_name("Login");
session_start();
if (isset($_SESSION['level']) && $_SESSION['level'] == 'administrator'){
//config (if your TinyMCE location is different from example, you should also check paths at line ~360)
$file_root = 'files'; //where to store files, must be created and writable
$root_path = '/auracmsv3'; //jika di upload di server maka cukup di tulis '' saja.
$thmb_size = 100; //max size of preview thumbnail
$no_script = false; //true/false - turns scripts into text files
$lang = 'en'; //language (look in /plugin/mfm_013/mfm/lang/ for available)
error_reporting(0); //'E_ALL' for debugging, '0' for use
//array of known file types (used for icons)
$file_class = array(
'swf',
'txt',
'htm',
'html',
'zip',
'gz',
'rar',
'cab',
'tar',
'7z',
'deb',
'rpm',
'php',
'mp3',
'ogg',
'mid',
'avi',
'mpg',
'flv',
'mpeg',
'pdf',
'ttf',
'exe'
);
//upload class (see file for credits)
require('plugin/mfm_013/mfm/class.upload.php');
//lang
$lng = array();
require('plugin/mfm_013/mfm/lang/lang_' . strtolower($lang) . '.php');
header("Content-type: text/html; charset=utf-8");
//stand alone or tynimce?
$mode = 'mce';
if(isset($_GET['mode'])) { $mode = $_GET['mode']; }
//replaces special characters for latvian and russian lang., and removes all other
function format_filename($filename) {
$bads = array(' ','a','c','e','g','i','k','l','n','r','š','u','ž','A','C','E','G','I','K','L','N','R','Š','U','Ž','$','&','?','?','?','?','?','?','?','?','?','?','?','?','?','?','?','?','?','?','?','?','?','?','?','?','?','?','?','?','??','?','?','?','?','?','?','?','?','?','?','?','?','?','?','?','?','?','?','?','?','?','?','?','?','?','?','?','?','??','?','?','?','?','?','?');
$good = array('-','a','c','e','g','i','k','l','n','r','s','u','z','A','C','E','G','I','K','L','N','R','S','U','Z','s','and','A','B','V','G','D','E','J','Z','Z','I','J','K','L','M','N','O','P','R','S','T','U','F','H','C','C','S','S','T','T','E','Ju','Ja','a','b','v','g','d','e','e','z','z','i','j','k','l','m','n','o','p','r','s','t','u','f','h','c','c','s','t','t','y','z','e','ju','ja');
$filename = str_replace($bads,$good,trim($filename));
$allowed = "/[^a-z0-9\\.\\-\\_\\\\]/i";
$filename = preg_replace($allowed,'',$filename);
return $filename;
}
//convert file size to human readable format
function byte_convert($bytes) {
$symbol = array('B', 'KiB', 'MiB', 'GiB', 'TiB', 'PiB', 'EiB', 'ZiB', 'YiB');
$exp = 0;
$converted_value = 0;
if( $bytes > 0 ) {
$exp = floor( log($bytes)/log(1024) );
$converted_value = ( $bytes/pow(1024,floor($exp)) );
}
return sprintf( '%.2f '.$symbol[$exp], $converted_value );
}
//show recursive directory tree
function print_tree($dir = '.') {
global $root_path;
echo '<ul class="dirlist">';
$d = opendir($dir);
while($f = readdir($d)) {
if(strpos($f, '.') === 0) continue;
$ff = $dir . '/' . $f;
if(is_dir($ff)) {
echo '<li><a href="' . $root_path . '/' . $ff . '/" onclick="load(\'filemanager.php?viewdir=' . $ff . '\',\'view-files\'); return false;">' . $f . '</a>';
print_tree($ff);
echo '</li>';
}
}
echo '</ul>';
}
//show file list of given directory
function print_files($c = '.') {
global $root_path, $mode, $thmb_size, $file_class, $lng;
echo('<table id="file-list">');
$d = opendir($c);
$i = 0;
while($f = readdir($d)) {
if(strpos($f, '.') === 0) continue;
$ff = $c . '/' . $f;
$ext = strtolower(substr(strrchr($f, '.'), 1));
if(!is_dir($ff)) {
echo '<tr' . ($i%2 ? ' class="light"' : ' class="dark"') .'>';
//show preview and different icon, if file is image
$imageinfo = @getimagesize($ff);
if($imageinfo && $imageinfo[2] > 0 && $imageinfo[2]< 4) {
$resize = '';
if($imageinfo[0] > $thmb_size or $imageinfo[1] > $thmb_size) {
if($imageinfo[0] > $imageinfo[1]) {
$resize = ' style="width: ' . $thmb_size . 'px;"';
} else {
$resize = ' style="height: ' . $thmb_size . 'px;"';
}
}
if ($imageinfo[2] == 1) {
$imagetype = "image_gif";
} elseif ($imageinfo[2] == 2) {
$imagetype = "image_jpg";
} elseif ($imageinfo[2] == 3) {
$imagetype = "image_jpg";
} else {
$imagetype = "image";
}
echo '<td><a class="file thumbnail ' . $imagetype . '" href="#" onclick="submit_url(\'' . $root_path . '/' . $ff . '\');">' . $f . '<span><img' . $resize . ' src="' . $root_path . '/' . $ff . '" /></span></a>'; echo '</td>';
//known file types
} elseif(in_array($ext,$file_class)) {
echo '<td><a class="file file_' . $ext . '" href="#" onclick="submit_url(\'' . $root_path . '/' . $ff . '\');">' . $f . '</a>'; echo '</td>';
//all other files
} else {
echo '<td><a class="file unknown" href="#" onclick="submit_url(\'' . $root_path . '/' . $ff . '\');">' . $f . '</a>'; echo '</td>';
}
echo '<td>' . byte_convert(filesize($ff)) . '</td>';
echo '<td class="delete"><a href="#" title="' . $lng['delete_title'] . '" onclick="delete_file(\'' . $c . '\',\'' . $f . '\');">' . $lng['delete'] . '</a></td>';
echo '</tr>';
$i++;
}
}
echo('</table>');
}
function delete_directory($dirname) {
if (is_dir($dirname))
$dir_handle = opendir($dirname);
if (!$dir_handle)
return false;
while($file = readdir($dir_handle)) {
if ($file != "." && $file != "..") {
if (!is_dir($dirname."/".$file))
unlink($dirname."/".$file);
else
delete_directory($dirname.'/'.$file);
}
}
closedir($dir_handle);
rmdir($dirname);
return true;
}
$uploadstatus = 0;
if(isset($_GET['status'])) { $uploadstatus = $_GET['status']; }
//handles file uploads
if(isset($_FILES['new_file']) && isset($_POST['return'])) {
if(is_dir($_POST['return'])) {
$handle = new upload($_FILES['new_file']);
if ($handle->uploaded) {
$handle->file_new_name_body = format_filename(substr($_FILES['new_file']['name'],0,-4));
//resize image. more options coming soon.
if(isset($_POST['new_resize']) && $_POST['new_resize'] > 0) {
$handle->image_resize = true;
$handle->image_x = (int)$_POST['new_resize'];
$handle->image_ratio_y = true;
}
if(isset($_POST['new_greyscale']) && $_POST['new_greyscale']) {
$handle->image_greyscale = true;
}
if(isset($_POST['new_rotate']) && $_POST['new_rotate'] == 90 or $_POST['new_rotate'] == 180 or $_POST['new_rotate'] == 270) {
$handle->image_rotate = $_POST['new_rotate'];
}
$handle->mime_check = $no_script;
$handle->no_script = $no_script;
$handle->process($_POST['return'] . '/');
if ($handle->processed) {
$handle->clean();
$uploadstatus = 1;
} else {
//uncomment for upload debugging
//echo 'error : ' . $handle->error;
$uploadstatus = 2;
}
}
} else {
$uploadstatus = 3;
}
}
//remove unnecessary folder
if(isset($_GET['deletefolder'])) {
$deldir = $_GET['deletefolder'];
//fix by Arpad SZUCS Jul 6, 2011
$block = false;
if (strstr($_SERVER['SERVER_SOFTWARE'], "Win32")) {
// windows is case insesitive:
$block = ! strcasecmp($deldir, $file_root);
} else {
$block = ! strcmp($deldir, $file_root);
}
if(is_dir($_GET['deletefolder'])) {
if(!$block && delete_directory($deldir)) {
header('Location: filemanager.php?status=4');
} else {
$uploadstatus = 5;
}
} else {
$uploadstatus = 6;
}
}
//display only directory tree for dynamic AHAH requests
if(isset($_GET['viewtree'])) {
?>
<ul class="dirlist">
<li><a href="<?php echo $root_path . '/' . $file_root; ?>/" onclick="load('filemanager.php?viewdir=<?php echo $file_root; ?>','view-files'); return false;"><?php echo $file_root; ?></a> <a href="#" onclick="load('filemanager.php?viewtree=true','view-tree'); return false;" id="refresh-tree"><?php echo $lng['refresh']; ?></a>
<?php print_tree($file_root); ?>
</li>
</ul>
<?php
exit;
}
//display file list for dynamic requests
if(isset($_GET['viewdir'])) {
?>
<ul id="browser-toolbar">
<li class="file-new"><a href="#" title="<?php echo $lng['new_file_title']; ?>" onclick="toggle_visibility('load-file'); return false;"><?php echo $lng['new_file']; ?></a></li>
<li class="folder-new"><a href="#" title="<?php echo $lng['new_folder_title']; ?>" onclick="create_folder('<?php echo $_GET['viewdir']; ?>'); return false;"><?php echo $lng['new_folder']; ?></a></li>
<li class="folder-delete"><a href="#" title="<?php echo $lng['delete_folder_title']; ?>" onclick="delete_folder('<?php echo $_GET['viewdir']; ?>');"><?php echo $lng['delete_folder']; ?></a></li>
<li class="file-refresh"><a href="#" title="<?php echo $lng['refresh_files_title']; ?>" onclick="load('filemanager.php?viewdir=<?php echo $_GET['viewdir']; ?>','view-files'); return false;"><?php echo $lng['refresh']; ?></a></li>
</ul>
<div id="current-loction">
<?php echo htmlspecialchars($root_path . '/' . $_GET['viewdir'] . '/'); ?>
</div>
<form style="display: none;" id="load-file" action="" class="load-file" method="post" enctype="multipart/form-data">
<fieldset>
<legend><?php echo $lng['new_file_title']; ?></legend>
<input type="hidden" value="<?php echo $_GET['viewdir']; ?>" name="return" />
<label><?php echo $lng['form_file']; ?><input type="file" name="new_file" /></label>
</fieldset>
<fieldset>
<legend><?php echo $lng['new_file_manipulations']; ?></legend>
<table>
<tr>
<td><label for="new_resize"><?php echo $lng['form_width']; ?></label></td>
<td><input type="text" class="number" maxlength="4" id="new_resize" name="new_resize" value="" /> px</td>
</tr>
<tr>
<td><label for="new_rotate"><?php echo $lng['form_rotate']; ?></label></td>
<td>
<select id="new_rotate" name="new_rotate">
<option value="0"></option>
<option value="90">90</option>
<option value="180">180</option>
<option value="270">270</option>
</select>
</td>
</tr>
<tr>
<td></td>
<td><input type="checkbox" class="checkbox" id="new_greyscale" name="new_greyscale" /><label for="new_greyscale"><?php echo $lng['form_greyscale']; ?></label></td>
</tr>
</table>
</fieldset>
<input type="submit" id="insert" value="<?php echo $lng['form_submit']; ?>" />
</form>
<?php
//create directory and show results
if(isset($_GET['newdir'])) {
$new_title = format_filename($_GET['newdir']);
if(!is_dir($_GET['viewdir'] . '/' . $new_title)) {
if(mkdir($_GET['viewdir'] . '/' . $new_title, 0777)) {
echo '<p class="successful">"' . $new_title . '"' . $lng['message_created_folder'] . '</p>';
} else {
echo '<p class="failed">' . $lng['message_cannot_create'] . '"' . $new_title . '"!<br />' . $lng['message_cannot_write'] . '</p>';
}
} else {
echo '<p class="failed">' . $lng['message_cannot_create'] . '"' . $new_title . '"!<br />' . $lng['message_exists'] . '</p>';
}
}
//remove unnecessary files
if(isset($_GET['deletefile'])) {
if(!file_exists($_GET['viewdir'] . '/' . $_GET['deletefile'])) {
echo '<p class="failed">' . $lng['message_cannot_delete_nonexist'] . '</p>';
} else {
if(unlink($_GET['viewdir'] . '/' . $_GET['deletefile'])) {
echo '<p class="successful">' . $lng['message_deleted'] . '</p>';
} else {
echo '<p class="failed">' . $lng['message_cannot_delete'] . '</p>';
}
}
}
//show status messages by code
if(isset($_GET['status'])) {
//upload file
if($_GET['status'] == 1) {
echo '<p class="successful">' . $lng['message_uploaded'] . '</p>';
} elseif($_GET['status'] == 2) {
echo '<p class="failed">' . $lng['message_upload_failed'] . '</p>';
} elseif($_GET['status'] == 3) {
echo '<p class="failed">' . $lng['message_wrong_dir'] . '</p>';
//remove directory
} elseif($_GET['status'] == 4) {
echo '<p class="successful">' . $lng['message_folder_deleted'] . '</p>';
} elseif($_GET['status'] == 5) {
echo '<p class="failed">' . $lng['message_cant_delete_folder'] . '</p>';
} elseif($_GET['status'] == 6) {
echo '<p class="failed">' . $lng['message_folder_not_exist'] . '</p>';
}
}
//finally show file list
print_files($_GET['viewdir']);
exit;
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8" />
<meta http-equiv="content-language" content="en" />
<title><?php echo $lng['window_title']; ?></title>
<link rel="stylesheet" href="plugin/mfm_013/mfm/style.css" type="text/css" />
<link rel="stylesheet" href="<?php echo $root_path; ?>/js/tinymce/jscripts/tiny_mce/themes/advanced/skins/default/dialog.css" type="text/css" />
<script type="text/javascript" src="<?php echo $root_path; ?>/js/tinymce/jscripts/tiny_mce/tiny_mce_popup.js"></script>
<script type="text/javascript">
//<![CDATA[
//load content using AHAH (asynchronous HTML and HTTP)
function ahah(url, target, callback) {
document.getElementById(target).innerHTML = '<img src="plugin/mfm_013/mfm/loading.gif" alt="" /> <?php echo $lng['loading']; ?>';
if (window.XMLHttpRequest) {
req = new XMLHttpRequest();
} else if (window.ActiveXObject) {
req = new ActiveXObject("Microsoft.XMLHTTP");
}
if (req != undefined) {
req.onreadystatechange = function() {ahahDone(url, target, callback);};
var url = url + ((url.indexOf("?") == -1) ? "?" : "&") + escape(new Date().toString()); //prevent caching
req.open("GET", url, true);
req.send("");
}
}
function ahahDone(url, target, callback) {
if (req.readyState == 4) {
if (req.status == 200) {
document.getElementById(target).innerHTML = req.responseText;
if (callback) callback();
} else {
document.getElementById(target).innerHTML=" AHAH Error:\n"+ req.status + "\n" +req.statusText;
}
}
}
function load(name, div, callback) {
ahah(name,div,callback);
return false;
}
//ask for folder title and request it's creation
function create_folder(viewdir) {
var name=prompt("<?php echo $lng['ask_folder_title']; ?>","<?php echo $lng['default_folder']; ?>");
if (name!=null && name!=""){
load('filemanager.php?viewdir=' + viewdir + '&newdir=' + name + '','view-files', create_folder_done);
}
}
//reload directory tree after folder creation
function create_folder_done() {
load( '?mfm=1&viewtree=true', 'view-tree' );
}
<?php
//first one for inserting file name into given field, second for working as tinyMCE plugin
if ($mode == 'standalone' && isset($_GET['field'])) {
?>
function submit_url(URL) {
window.opener.document.getElementById('<?php echo $_GET['field']; ?>').value = URL;
self.close();
}
<?php
} else {
?>
function submit_url(URL) {
var win = tinyMCEPopup.getWindowArg("window");
win.document.getElementById(tinyMCEPopup.getWindowArg("input")).value = URL;
if (win.ImageDialog.getImageData) win.ImageDialog.getImageData();
if (win.ImageDialog.showPreviewImage) win.ImageDialog.showPreviewImage(URL);
tinyMCEPopup.close();
}
<?php } ?>
//confirm and delete file
function delete_file(dir,file) {
var answer = confirm("<?php echo $lng['ask_really_delete']; ?>");
if (answer){
load('filemanager.php?viewdir=' + dir + '&deletefile=' + file,'view-files');
}
}
//confirm and delete folder
function delete_folder(dir) {
var answer = confirm("<?php echo $lng['ask_really_delete']; ?>");
if (answer){
location.href = 'filemanager.php?deletefolder=' + dir;
}
}
//show/hide element (for file upload form)
function toggle_visibility(id) {
var e = document.getElementById(id);
if(e != null) {
if(e.style.display == 'none') {
e.style.display = 'block';
} else {
e.style.display = 'none';
}
}
}
//]]>
</script>
</head>
<?php
$return = $file_root;
if(isset($_REQUEST['return'])) {$return = $_REQUEST['return'];}
?>
<body onload="load('filemanager.php?status=<?php echo $uploadstatus; ?>&viewdir=<?php echo $return; ?>','view-files');">
<div id="browser-wrapper">
<div id="view-tree">
<ul class="dirlist">
<li><a href="<?php echo $root_path . '/' . $file_root; ?>/" onclick="load('filemanager.php?viewdir=<?php echo $file_root; ?>','view-files'); return false;"><?php echo $file_root; ?></a> <a href="#" title="<?php echo $lng['refresh_tree_title']; ?>" onclick="load('filemanager.php?viewtree=true','view-tree'); return false;" id="refresh-tree"><?php echo $lng['refresh']; ?></a>
<?php print_tree($file_root); ?>
</li>
</ul>
</div>
<div id="view-files"></div>
</div>
</body>
</html>
<?php
}else{
exit;
}
?>