-
Notifications
You must be signed in to change notification settings - Fork 20
/
fileManager.php
255 lines (225 loc) · 8.67 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
<?php
/*
* File Manager
*
* Copyright (c) 2010 Tom Kay - oridan82@gmail.com
*
* 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/>.*
*
*/
/*
* Uncomment the line below to enable basic operation. By default this
* will manage a folder called 'uploads' relative to this script's path.
*
* You can change this folder to anything you like. The script will not
* allow managing of files outside of this path.
*
* Advanced users may wish to write a separate php script to control the
* ajax features of the File Manager. Including callbacks for deleting
* or renaming files.
*/
jqFileManager::ProcessAjax( dirname(__FILE__).'/uploads' );
class jqFileManager {
private static $docroot = NULL;
public static function SetDocRoot($path) {
self::$docroot = $path;
}
private static $relroot = '/';
public static function SetRelRoot($path) {
self::$relroot = $path;
}
private static $data = array();
static function GetRelativePath($path) {
$docroot = self::$docroot ? self::$docroot : $_SERVER['DOCUMENT_ROOT'];
$path = realpath($path);
$path = str_replace($docroot,self::$relroot,$path);
$path = str_replace(DIRECTORY_SEPARATOR,'/',$path);
return $path;
}
static function GetPathFolder() {
return self::GetRelativePath(dirname(__FILE__)).DIRECTORY_SEPARATOR;
}
static function GetPathSelf() {
return self::GetRelativePath(__FILE__);
}
static function GetPathJS() {
return self::GetPathFolder().'jquery.fileManager.js';
}
static function GetPathCSS() {
return self::GetPathFolder().'jquery.fileManager.css';
}
static function ResolvePath($path) {
$newpath = preg_replace('/[^\/]+\/\.\.\/?/', '', $path);
if ($newpath != $path) $newpath = self::ResolvePath($newpath);
return $newpath;
}
static function AddIcon($path, $title='',$folder=false,$icon='') {
self::$data[] = array('path'=>$path,'title'=>$title,'type'=>$folder,'icon'=>$icon);
}
static function ProcessAjax($rootPath,$deleteCallback=null,$renameCallback=null,$iconCallback=null) {
$pMod = array_key_exists('path',$_POST) ? $_POST['path'] : '';
$path = $rootPath.'/'.trim($pMod,'/');
$path = self::ResolvePath($path);
$path = rtrim($path,'/');
if (strpos($path,$rootPath)===FALSE) $path = $rootPath;
if (!file_exists($path)) mkdir($path,octdec('0777'),true);
if (isset($_FILES['file'])) return self::ProcessUpload($path);
if (isset($_POST['delete'])) {
$from = self::ResolvePath($path.'/'.$_POST['delete']);
if (strpos($from,$rootPath)===FALSE) {
echo 'alert("Can only perform operations within the root path");';
return false;
}
if (!file_exists($from)) {
echo 'alert("File or Folder no longer exists");';
return false;
}
try {
if (is_dir($from)) rmdir($from);
else unlink($from);
} catch (Exception $e) {
echo 'alert("Cannot Delete. Folder may not be empty.");';
return false;
}
return true;
}
if (array_key_exists('mFrom',$_POST) && array_key_exists('mTo',$_POST)) {
$from = self::ResolvePath($path.'/'.$_POST['mFrom']);
$to = self::ResolvePath($path.'/'.$_POST['mTo']);
if (strpos($from,$rootPath)===FALSE || strpos($to,$rootPath)===FALSE) {
echo 'alert("Can only perform operations within the root path");';
return false;
}
if (file_exists($to)) {
echo 'alert("Destination already exists");';
return false;
}
try {
rename($from,$to);
} catch(Exception $e) {
echo $e->getMessage();
echo 'alert("Cannot move or rename.");';
return false;
}
if (is_callable($renameCallback)) call_user_func($renameCallback,$from,$to);
return true;
}
$glob = glob($path.'/{,.}*',GLOB_BRACE);
natcasesort($glob);
$files = array_merge(array_filter($glob, 'is_dir'),array_filter($glob, 'is_file'));
foreach ($files as $file) {
$filename = basename($file);
if ($filename === '..' || $filename === '.') continue;
if (!is_dir($file) && array_key_exists('filter',$_POST) && !preg_match('/'.$_POST['filter'].'/i',$filename)) continue;
$icon = (is_callable($iconCallback)) ? call_user_func($iconCallback,$file) : '';
self::AddIcon($filename,$filename,is_dir($file)?1:0,$icon);
}
// uPath is full path less rootpath less filename
$uPath = substr(self::GetRelativePath($path),strlen(self::GetRelativePath($rootPath)));
if (!$uPath) $uPath = '';
header('Content-Type: application/json');
echo json_encode(array('rootPath'=>self::GetRelativePath($rootPath),'path'=>$uPath,'files'=>self::$data));
}
public static function ProcessUpload($path) {
if (ob_get_level()) ob_end_clean();
$destination = realpath($path);
// HTTP headers for no cache etc
header('Content-type: application/json; charset=UTF-8');
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
header("Cache-Control: no-store, no-cache, must-revalidate");
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache");
// Settings
$targetDir = $destination;// ini_get("upload_tmp_dir") . DIRECTORY_SEPARATOR . "plupload";
$cleanupTargetDir = false; // Remove old files
$maxFileAge = 60 * 60; // Temp file age in seconds
// 5 minutes execution time
//set_time_limit(0);
// usleep(5000);
// Get parameters
$chunk = array_key_exists('chunk',$_REQUEST) ? $_REQUEST["chunk"] : 0;
$chunks = array_key_exists('chunks',$_REQUEST) ? $_REQUEST["chunks"] : 0;
$fileName = array_key_exists('name',$_REQUEST) ? $_REQUEST["name"] : '';
// Clean the fileName for security reasons
$fileName = preg_replace('/[^\w\._]+/', '', $fileName);
if (is_dir($targetDir . DIRECTORY_SEPARATOR . $fileName)) {
echo '{"jsonrpc" : "2.0", "error" : {"code": 102, "message": "Failed to open output stream."}, "id" : "id"}';
}
// Create target dir
if (!file_exists($targetDir)) {
mkdir($targetDir);
chmod($targetDir,0777);
}
// Remove old temp files
if (is_dir($targetDir) && ($dir = opendir($targetDir))) {
while (($file = readdir($dir)) !== false) {
$filePath = $targetDir . DIRECTORY_SEPARATOR . $file;
// Remove temp files if they are older than the max age
if (preg_match('/\\.tmp$/', $file) && (filemtime($filePath) < time() - $maxFileAge))
unlink($filePath);
}
closedir($dir);
} else {
echo '{"jsonrpc" : "2.0", "error" : {"code": 100, "message": "Failed to open temp directory."}, "id" : "id"}';
}
$contentType = '';
// Look for the content type header
if (isset($_SERVER["HTTP_CONTENT_TYPE"]))
$contentType = $_SERVER["HTTP_CONTENT_TYPE"];
if (isset($_SERVER["CONTENT_TYPE"]))
$contentType = $_SERVER["CONTENT_TYPE"];
if (strpos($contentType, "multipart") !== false) {
if (isset($_FILES['file']['tmp_name']) && is_uploaded_file($_FILES['file']['tmp_name'])) {
// Open temp file
$out = fopen($targetDir . DIRECTORY_SEPARATOR . $fileName, $chunk == 0 ? "wb" : "ab");
if ($out) {
// Read binary input stream and append it to temp file
$in = fopen($_FILES['file']['tmp_name'], "rb");
if ($in) {
while ($buff = fread($in, 4096))
fwrite($out, $buff);
} else {
echo '{"jsonrpc" : "2.0", "error" : {"code": 101, "message": "Failed to open input stream."}, "id" : "id"}';
}
fclose($out);
unlink($_FILES['file']['tmp_name']);
} else {
echo '{"jsonrpc" : "2.0", "error" : {"code": 102, "message": "Failed to open output stream."}, "id" : "id"}';
}
} else {
echo '{"jsonrpc" : "2.0", "error" : {"code": 103, "message": "Failed to move uploaded file."}, "id" : "id"}';
}
} else {
// Open temp file
$out = fopen($targetDir . DIRECTORY_SEPARATOR . $fileName, $chunk == 0 ? "wb" : "ab");
if ($out) {
// Read binary input stream and append it to temp file
$in = fopen("php://input", "rb");
if ($in) {
while ($buff = fread($in, 4096))
fwrite($out, $buff);
} else {
echo '{"jsonrpc" : "2.0", "error" : {"code": 101, "message": "Failed to open input stream."}, "id" : "id"}';
}
fclose($out);
} else {
echo '{"jsonrpc" : "2.0", "error" : {"code": 102, "message": "Failed to open output stream."}, "id" : "id"}';
}
}
// Return JSON-RPC response
echo '{"jsonrpc" : "2.0", "result" : null, "id" : "id"}';
}
}
?>