1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Default elFinder connector |
5
|
|
|
* |
6
|
|
|
* @author Dmitry (dio) Levashov |
7
|
|
|
**/ |
8
|
|
|
class elFinderConnector { |
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* elFinder instance |
11
|
|
|
* |
12
|
|
|
* @var elFinder |
13
|
|
|
**/ |
14
|
|
|
protected $elFinder; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Options |
18
|
|
|
* |
19
|
|
|
* @var aray |
20
|
|
|
**/ |
21
|
|
|
protected $options = array(); |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* undocumented class variable |
25
|
|
|
* |
26
|
|
|
* @var string |
27
|
|
|
**/ |
28
|
|
|
protected $header = 'Content-Type: application/json'; |
29
|
|
|
|
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Constructor |
33
|
|
|
* |
34
|
|
|
* @return void |
|
|
|
|
35
|
|
|
* @author Dmitry (dio) Levashov |
36
|
|
|
**/ |
37
|
|
|
public function __construct($elFinder, $debug=false) { |
38
|
|
|
|
39
|
|
|
$this->elFinder = $elFinder; |
40
|
|
|
if ($debug) { |
41
|
|
|
$this->header = 'Content-Type: text/html; charset=utf-8'; |
42
|
|
|
} |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Execute elFinder command and output result |
47
|
|
|
* |
48
|
|
|
* @return void |
49
|
|
|
* @author Dmitry (dio) Levashov |
50
|
|
|
**/ |
51
|
|
|
public function run() { |
|
|
|
|
52
|
|
|
$isPost = $_SERVER["REQUEST_METHOD"] == 'POST'; |
53
|
|
|
$src = $_SERVER["REQUEST_METHOD"] == 'POST' ? $_POST : $_GET; |
54
|
|
|
if ($isPost && !$src && $rawPostData = @file_get_contents('php://input')) { |
55
|
|
|
// for support IE XDomainRequest() |
56
|
|
|
$parts = explode('&', $rawPostData); |
57
|
|
|
foreach($parts as $part) { |
58
|
|
|
list($key, $value) = array_pad(explode('=', $part), 2, ''); |
59
|
|
|
$key = rawurldecode($key); |
60
|
|
|
if (substr($key, -2) === '[]') { |
61
|
|
|
$key = substr($key, 0, strlen($key) - 2); |
62
|
|
|
if (!isset($src[$key])) { |
63
|
|
|
$src[$key] = array(); |
64
|
|
|
} |
65
|
|
|
$src[$key][] = rawurldecode($value); |
66
|
|
|
} else { |
67
|
|
|
$src[$key] = rawurldecode($value); |
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
$_POST = $this->input_filter($src); |
71
|
|
|
$_REQUEST = $this->input_filter(array_merge_recursive($src, $_REQUEST)); |
72
|
|
|
} |
73
|
|
|
$cmd = isset($src['cmd']) ? $src['cmd'] : ''; |
74
|
|
|
$args = array(); |
75
|
|
|
|
76
|
|
|
if (!function_exists('json_encode')) { |
77
|
|
|
$error = $this->elFinder->error(elFinder::ERROR_CONF, elFinder::ERROR_CONF_NO_JSON); |
78
|
|
|
$this->output(array('error' => '{"error":["'.implode('","', $error).'"]}', 'raw' => true)); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
if (!$this->elFinder->loaded()) { |
82
|
|
|
$this->output(array('error' => $this->elFinder->error(elFinder::ERROR_CONF, elFinder::ERROR_CONF_NO_VOL), 'debug' => $this->elFinder->mountErrors)); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
// telepat_mode: on |
86
|
|
|
if (!$cmd && $isPost) { |
87
|
|
|
$this->output(array('error' => $this->elFinder->error(elFinder::ERROR_UPLOAD, elFinder::ERROR_UPLOAD_TOTAL_SIZE), 'header' => 'Content-Type: text/html')); |
88
|
|
|
} |
89
|
|
|
// telepat_mode: off |
90
|
|
|
|
91
|
|
|
if (!$this->elFinder->commandExists($cmd)) { |
92
|
|
|
$this->output(array('error' => $this->elFinder->error(elFinder::ERROR_UNKNOWN_CMD))); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
// collect required arguments to exec command |
96
|
|
|
foreach ($this->elFinder->commandArgsList($cmd) as $name => $req) { |
97
|
|
|
$arg = $name == 'FILES' |
98
|
|
|
? $_FILES |
99
|
|
|
: (isset($src[$name]) ? $src[$name] : ''); |
100
|
|
|
|
101
|
|
|
if (!is_array($arg)) { |
102
|
|
|
$arg = trim($arg); |
103
|
|
|
} |
104
|
|
|
if ($req && (!isset($arg) || $arg === '')) { |
105
|
|
|
$this->output(array('error' => $this->elFinder->error(elFinder::ERROR_INV_PARAMS, $cmd))); |
106
|
|
|
} |
107
|
|
|
$args[$name] = $arg; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
$args['debug'] = isset($src['debug']) ? !!$src['debug'] : false; |
111
|
|
|
|
112
|
|
|
$this->output($this->elFinder->exec($cmd, $this->input_filter($args))); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* Output json |
117
|
|
|
* |
118
|
|
|
* @param array data to output |
119
|
|
|
* @return void |
120
|
|
|
* @author Dmitry (dio) Levashov |
121
|
|
|
**/ |
122
|
|
|
protected function output(array $data) { |
|
|
|
|
123
|
|
|
// clear output buffer |
124
|
|
|
while(@ob_get_level()){ @ob_end_clean(); } |
|
|
|
|
125
|
|
|
|
126
|
|
|
$header = isset($data['header']) ? $data['header'] : $this->header; |
127
|
|
|
unset($data['header']); |
128
|
|
|
if ($header) { |
129
|
|
|
if (is_array($header)) { |
130
|
|
|
foreach ($header as $h) { |
131
|
|
|
header($h); |
132
|
|
|
} |
133
|
|
|
} else { |
134
|
|
|
header($header); |
135
|
|
|
} |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
if (isset($data['pointer'])) { |
139
|
|
|
$toEnd = true; |
140
|
|
|
$fp = $data['pointer']; |
141
|
|
|
if (elFinder::isSeekableStream($fp)) { |
142
|
|
|
header('Accept-Ranges: bytes'); |
143
|
|
|
$psize = null; |
144
|
|
|
if (!empty($_SERVER['HTTP_RANGE'])) { |
145
|
|
|
$size = $data['info']['size']; |
146
|
|
|
$start = 0; |
|
|
|
|
147
|
|
|
$end = $size - 1; |
148
|
|
|
if (preg_match('/bytes=(\d*)-(\d*)(,?)/i', $_SERVER['HTTP_RANGE'], $matches)) { |
149
|
|
|
if (empty($matches[3])) { |
150
|
|
|
if (empty($matches[1]) && $matches[1] !== '0') { |
151
|
|
|
$start = $size - $matches[2]; |
152
|
|
|
} else { |
153
|
|
|
$start = intval($matches[1]); |
154
|
|
|
if (!empty($matches[2])) { |
155
|
|
|
$end = intval($matches[2]); |
156
|
|
|
if ($end >= $size) { |
157
|
|
|
$end = $size - 1; |
158
|
|
|
} |
159
|
|
|
$toEnd = ($end == ($size - 1)); |
160
|
|
|
} |
161
|
|
|
} |
162
|
|
|
$psize = $end - $start + 1; |
163
|
|
|
|
164
|
|
|
header('HTTP/1.1 206 Partial Content'); |
165
|
|
|
header('Content-Length: ' . $psize); |
166
|
|
|
header('Content-Range: bytes ' . $start . '-' . $end . '/' . $size); |
167
|
|
|
|
168
|
|
|
fseek($fp, $start); |
169
|
|
|
} |
170
|
|
|
} |
171
|
|
|
} |
172
|
|
|
if (is_null($psize)){ |
173
|
|
|
rewind($fp); |
174
|
|
|
} |
175
|
|
|
} else { |
176
|
|
|
header('Accept-Ranges: none'); |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
// unlock session data for multiple access |
180
|
|
|
session_id() && session_write_close(); |
181
|
|
|
// client disconnect should abort |
182
|
|
|
ignore_user_abort(false); |
183
|
|
|
|
184
|
|
|
if ($toEnd) { |
185
|
|
|
fpassthru($fp); |
186
|
|
|
} else { |
187
|
|
|
$out = fopen('php://output', 'wb'); |
188
|
|
|
stream_copy_to_stream($fp, $out, $psize); |
|
|
|
|
189
|
|
|
fclose($out); |
190
|
|
|
} |
191
|
|
|
if (!empty($data['volume'])) { |
192
|
|
|
$data['volume']->close($data['pointer'], $data['info']['hash']); |
193
|
|
|
} |
194
|
|
|
exit(); |
|
|
|
|
195
|
|
|
} else { |
196
|
|
|
if (!empty($data['raw']) && !empty($data['error'])) { |
197
|
|
|
exit($data['error']); |
|
|
|
|
198
|
|
|
} else { |
199
|
|
|
exit(json_encode($data)); |
|
|
|
|
200
|
|
|
} |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
/** |
206
|
|
|
* Remove null & stripslashes applies on "magic_quotes_gpc" |
207
|
|
|
* |
208
|
|
|
* @param mixed $args |
209
|
|
|
* @return mixed |
210
|
|
|
* @author Naoki Sawada |
211
|
|
|
*/ |
212
|
|
|
protected function input_filter($args) { |
213
|
|
|
static $magic_quotes_gpc = NULL; |
214
|
|
|
|
215
|
|
|
if ($magic_quotes_gpc === NULL) |
216
|
|
|
$magic_quotes_gpc = (version_compare(PHP_VERSION, '5.4', '<') && get_magic_quotes_gpc()); |
217
|
|
|
|
218
|
|
|
if (is_array($args)) { |
219
|
|
|
return array_map(array(& $this, 'input_filter'), $args); |
220
|
|
|
} |
221
|
|
|
$res = str_replace("\0", '', $args); |
222
|
|
|
$magic_quotes_gpc && ($res = stripslashes($res)); |
223
|
|
|
return $res; |
224
|
|
|
} |
225
|
|
|
}// END class |
226
|
|
|
|
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.