-
Notifications
You must be signed in to change notification settings - Fork 0
/
fileManager.php
212 lines (187 loc) · 7.61 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
<?php
/*
* File Manager
*
* Copyright (c) 2010 Tom Kay - [email protected]
*
* 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/>.*
*
*/
class jqFileManager {
private static $data = array();
static function GetRelativePath($path) {
//$path = realpath($path); // dont use realpath as it doesnt work with files that dont exist.
$path = preg_replace('/[^\/]+\/\.\.\/?/', '', $path);
$pos = strpos($path,$_SERVER['DOCUMENT_ROOT']);
return '/'.ltrim(substr($path,$pos+strlen($_SERVER['DOCUMENT_ROOT'])),DIRECTORY_SEPARATOR);
}
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 AddIcon($path, $title='',$folder=false) {
self::$data[] = array('path'=>$path,'title'=>$title,'type'=>$folder);
}
static function ProcessAjax($rootPath,$deleteCallback=null,$renameCallback=null) {
$pMod = array_key_exists('path',$_GET) ? $_GET['path'] : '';
$path = $rootPath.'/'.trim($pMod,'/');
// translate path
$path = preg_replace('/[^\/]+\/\.\.\/?/', '', $path);
$path = '/'.trim($path,'/');
if (!file_exists($path)) mkdir($path,octdec('0777'),true);
// chmod($path,0777);
if (array_key_exists('delete',$_GET)) {
$from = $path.'/'.$_GET['delete'];
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',$_GET) && array_key_exists('mTo',$_GET)) {
$from = $path.'/'.$_GET['mFrom'];
$to = $path.'/'.$_GET['mTo'];
$to = preg_replace('/\w+\/\.\.\//', '', $to);
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;
}
$from = $path.'/'.$_GET['mFrom'];
$to = $path.'/'.$_GET['mTo'];
if (is_callable($renameCallback)) call_user_func($renameCallback,$from,$to);
return true;
}
$glob = glob($path.'/{,.}*',GLOB_BRACE);
$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',$_GET) && !preg_match('/'.$_GET['filter'].'/i',$filename)) continue;
// $fldr = is_dir($file) ? 'cmsMediaFolder cmsDrop cmsDrag' : 'cmsDrag';
self::AddIcon($filename,$filename,is_dir($file)?1:0);
//echo "<div class=\"cmsMediaIcon$fldr\" title=\"$filename\">$filename</div>";
}
// uPath is full path less rootpath less filename
$uPath = substr(self::GetRelativePath($path),strlen(self::GetRelativePath($rootPath)));
if (!$uPath) $uPath = '';
die(json_encode(array('rootPath'=>self::GetRelativePath($rootPath),'path'=>$uPath,'files'=>self::$data)));
}
public static function ProcessUpload($rootPath) {
if (ob_get_level()) ob_end_clean();
$pMod = array_key_exists('path',$_GET) ? $_GET['path'] : '';
if (!file_exists($rootPath)) mkdir($rootPath);
$destination = realpath(rtrim($rootPath,'/').DIRECTORY_SEPARATOR.trim($pMod,'/'));
// HTTP headers for no cache etc
header('Content-type: text/plain; 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)) die('{"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
die('{"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
die('{"jsonrpc" : "2.0", "error" : {"code": 101, "message": "Failed to open input stream."}, "id" : "id"}');
fclose($out);
unlink($_FILES['file']['tmp_name']);
} else
die('{"jsonrpc" : "2.0", "error" : {"code": 102, "message": "Failed to open output stream."}, "id" : "id"}');
} else
die('{"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
die('{"jsonrpc" : "2.0", "error" : {"code": 101, "message": "Failed to open input stream."}, "id" : "id"}');
fclose($out);
} else
die('{"jsonrpc" : "2.0", "error" : {"code": 102, "message": "Failed to open output stream."}, "id" : "id"}');
}
// Return JSON-RPC response
die('{"jsonrpc" : "2.0", "result" : null, "id" : "id"}');
}
}
?>