Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like elFinder often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use elFinder, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
12 | class elFinder { |
||
|
|||
13 | |||
14 | /** |
||
15 | * API version number |
||
16 | * |
||
17 | * @var string |
||
18 | **/ |
||
19 | protected $version = '2.0'; |
||
20 | |||
21 | /** |
||
22 | * Storages (root dirs) |
||
23 | * |
||
24 | * @var array |
||
25 | **/ |
||
26 | protected $volumes = array(); |
||
27 | |||
28 | /** |
||
29 | * Network mount drivers |
||
30 | * |
||
31 | * @var array |
||
32 | */ |
||
33 | public static $netDrivers = array(); |
||
34 | |||
35 | /** |
||
36 | * elFinder global locale |
||
37 | * |
||
38 | * @var string |
||
39 | */ |
||
40 | public static $locale = ''; |
||
41 | |||
42 | /** |
||
43 | * elFinder global sessionCacheKey |
||
44 | * |
||
45 | * @var string |
||
46 | */ |
||
47 | public static $sessionCacheKey = ''; |
||
48 | |||
49 | /** |
||
50 | * elFinder base64encodeSessionData |
||
51 | * elFinder save session data as `UTF-8` |
||
52 | * If the session storage mechanism of the system does not allow `UTF-8` |
||
53 | * And it must be `true` option 'base64encodeSessionData' of elFinder |
||
54 | * |
||
55 | * @var bool |
||
56 | */ |
||
57 | protected static $base64encodeSessionData = false; |
||
58 | |||
59 | /** |
||
60 | * Session key of net mount volumes |
||
61 | * @var string |
||
62 | */ |
||
63 | protected $netVolumesSessionKey = ''; |
||
64 | |||
65 | /** |
||
66 | * Mounted volumes count |
||
67 | * Required to create unique volume id |
||
68 | * |
||
69 | * @var int |
||
70 | **/ |
||
71 | public static $volumesCnt = 1; |
||
72 | |||
73 | /** |
||
74 | * Default root (storage) |
||
75 | * |
||
76 | * @var elFinderStorageDriver |
||
77 | **/ |
||
78 | protected $default = null; |
||
79 | |||
80 | /** |
||
81 | * Commands and required arguments list |
||
82 | * |
||
83 | * @var array |
||
84 | **/ |
||
85 | protected $commands = array( |
||
86 | 'open' => array('target' => false, 'tree' => false, 'init' => false, 'mimes' => false, 'compare' => false), |
||
87 | 'ls' => array('target' => true, 'mimes' => false), |
||
88 | 'tree' => array('target' => true), |
||
89 | 'parents' => array('target' => true), |
||
90 | 'tmb' => array('targets' => true), |
||
91 | 'file' => array('target' => true, 'download' => false), |
||
92 | 'size' => array('targets' => true), |
||
93 | 'mkdir' => array('target' => true, 'name' => true), |
||
94 | 'mkfile' => array('target' => true, 'name' => true, 'mimes' => false), |
||
95 | 'rm' => array('targets' => true), |
||
96 | 'rename' => array('target' => true, 'name' => true, 'mimes' => false), |
||
97 | 'duplicate' => array('targets' => true, 'suffix' => false), |
||
98 | 'paste' => array('dst' => true, 'targets' => true, 'cut' => false, 'mimes' => false, 'renames' => false, 'suffix' => false), |
||
99 | 'upload' => array('target' => true, 'FILES' => true, 'mimes' => false, 'html' => false, 'upload' => false, 'name' => false, 'upload_path' => false, 'chunk' => false, 'cid' => false, 'node' => false, 'renames' => false, 'suffix' => false), |
||
100 | 'get' => array('target' => true, 'conv' => false), |
||
101 | 'put' => array('target' => true, 'content' => '', 'mimes' => false), |
||
102 | 'archive' => array('targets' => true, 'type' => true, 'mimes' => false, 'name' => false), |
||
103 | 'extract' => array('target' => true, 'mimes' => false, 'makedir' => false), |
||
104 | 'search' => array('q' => true, 'mimes' => false, 'target' => false), |
||
105 | 'info' => array('targets' => true, 'compare' => false), |
||
106 | 'dim' => array('target' => true), |
||
107 | 'resize' => array('target' => true, 'width' => true, 'height' => true, 'mode' => false, 'x' => false, 'y' => false, 'degree' => false, 'quality' => false), |
||
108 | 'netmount' => array('protocol' => true, 'host' => true, 'path' => false, 'port' => false, 'user' => false, 'pass' => false, 'alias' => false, 'options' => false), |
||
109 | 'url' => array('target' => true, 'options' => false), |
||
110 | 'callback' => array('node' => true, 'json' => false, 'bind' => false, 'done' => false), |
||
111 | 'chmod' => array('targets' => true, 'mode' => true) |
||
112 | ); |
||
113 | |||
114 | /** |
||
115 | * Plugins instance |
||
116 | * |
||
117 | * @var array |
||
118 | **/ |
||
119 | protected $plugins = array(); |
||
120 | |||
121 | /** |
||
122 | * Commands listeners |
||
123 | * |
||
124 | * @var array |
||
125 | **/ |
||
126 | protected $listeners = array(); |
||
127 | |||
128 | /** |
||
129 | * script work time for debug |
||
130 | * |
||
131 | * @var string |
||
132 | **/ |
||
133 | protected $time = 0; |
||
134 | /** |
||
135 | * Is elFinder init correctly? |
||
136 | * |
||
137 | * @var bool |
||
138 | **/ |
||
139 | protected $loaded = false; |
||
140 | /** |
||
141 | * Send debug to client? |
||
142 | * |
||
143 | * @var string |
||
144 | **/ |
||
145 | protected $debug = false; |
||
146 | |||
147 | /** |
||
148 | * Call `session_write_close()` before exec command? |
||
149 | * |
||
150 | * @var bool |
||
151 | */ |
||
152 | protected $sessionCloseEarlier = true; |
||
153 | |||
154 | /** |
||
155 | * SESSION use commands @see __construct() |
||
156 | * |
||
157 | * @var array |
||
158 | */ |
||
159 | protected $sessionUseCmds = array(); |
||
160 | |||
161 | /** |
||
162 | * session expires timeout |
||
163 | * |
||
164 | * @var int |
||
165 | **/ |
||
166 | protected $timeout = 0; |
||
167 | |||
168 | /** |
||
169 | * Temp dir path for Upload |
||
170 | * |
||
171 | * @var string |
||
172 | */ |
||
173 | protected $uploadTempPath = ''; |
||
174 | |||
175 | /** |
||
176 | * undocumented class variable |
||
177 | * |
||
178 | * @var string |
||
179 | **/ |
||
180 | protected $uploadDebug = ''; |
||
181 | |||
182 | /** |
||
183 | * Errors from not mounted volumes |
||
184 | * |
||
185 | * @var array |
||
186 | **/ |
||
187 | public $mountErrors = array(); |
||
188 | |||
189 | /** |
||
190 | * URL for callback output window for CORS |
||
191 | * redirect to this URL when callback output |
||
192 | * |
||
193 | * @var string URL |
||
194 | */ |
||
195 | protected $callbackWindowURL = ''; |
||
196 | |||
197 | // Errors messages |
||
198 | const ERROR_UNKNOWN = 'errUnknown'; |
||
199 | const ERROR_UNKNOWN_CMD = 'errUnknownCmd'; |
||
200 | const ERROR_CONF = 'errConf'; |
||
201 | const ERROR_CONF_NO_JSON = 'errJSON'; |
||
202 | const ERROR_CONF_NO_VOL = 'errNoVolumes'; |
||
203 | const ERROR_INV_PARAMS = 'errCmdParams'; |
||
204 | const ERROR_OPEN = 'errOpen'; |
||
205 | const ERROR_DIR_NOT_FOUND = 'errFolderNotFound'; |
||
206 | const ERROR_FILE_NOT_FOUND = 'errFileNotFound'; // 'File not found.' |
||
207 | const ERROR_TRGDIR_NOT_FOUND = 'errTrgFolderNotFound'; // 'Target folder "$1" not found.' |
||
208 | const ERROR_NOT_DIR = 'errNotFolder'; |
||
209 | const ERROR_NOT_FILE = 'errNotFile'; |
||
210 | const ERROR_PERM_DENIED = 'errPerm'; |
||
211 | const ERROR_LOCKED = 'errLocked'; // '"$1" is locked and can not be renamed, moved or removed.' |
||
212 | const ERROR_EXISTS = 'errExists'; // 'File named "$1" already exists.' |
||
213 | const ERROR_INVALID_NAME = 'errInvName'; // 'Invalid file name.' |
||
214 | const ERROR_MKDIR = 'errMkdir'; |
||
215 | const ERROR_MKFILE = 'errMkfile'; |
||
216 | const ERROR_RENAME = 'errRename'; |
||
217 | const ERROR_COPY = 'errCopy'; |
||
218 | const ERROR_MOVE = 'errMove'; |
||
219 | const ERROR_COPY_FROM = 'errCopyFrom'; |
||
220 | const ERROR_COPY_TO = 'errCopyTo'; |
||
221 | const ERROR_COPY_ITSELF = 'errCopyInItself'; |
||
222 | const ERROR_REPLACE = 'errReplace'; // 'Unable to replace "$1".' |
||
223 | const ERROR_RM = 'errRm'; // 'Unable to remove "$1".' |
||
224 | const ERROR_RM_SRC = 'errRmSrc'; // 'Unable remove source file(s)' |
||
225 | const ERROR_MKOUTLINK = 'errMkOutLink'; // 'Unable to create a link to outside the volume root.' |
||
226 | const ERROR_UPLOAD = 'errUpload'; // 'Upload error.' |
||
227 | const ERROR_UPLOAD_FILE = 'errUploadFile'; // 'Unable to upload "$1".' |
||
228 | const ERROR_UPLOAD_NO_FILES = 'errUploadNoFiles'; // 'No files found for upload.' |
||
229 | const ERROR_UPLOAD_TOTAL_SIZE = 'errUploadTotalSize'; // 'Data exceeds the maximum allowed size.' |
||
230 | const ERROR_UPLOAD_FILE_SIZE = 'errUploadFileSize'; // 'File exceeds maximum allowed size.' |
||
231 | const ERROR_UPLOAD_FILE_MIME = 'errUploadMime'; // 'File type not allowed.' |
||
232 | const ERROR_UPLOAD_TRANSFER = 'errUploadTransfer'; // '"$1" transfer error.' |
||
233 | const ERROR_UPLOAD_TEMP = 'errUploadTemp'; // 'Unable to make temporary file for upload.' |
||
234 | // const ERROR_ACCESS_DENIED = 'errAccess'; |
||
235 | const ERROR_NOT_REPLACE = 'errNotReplace'; // Object "$1" already exists at this location and can not be replaced with object of another type. |
||
236 | const ERROR_SAVE = 'errSave'; |
||
237 | const ERROR_EXTRACT = 'errExtract'; |
||
238 | const ERROR_ARCHIVE = 'errArchive'; |
||
239 | const ERROR_NOT_ARCHIVE = 'errNoArchive'; |
||
240 | const ERROR_ARCHIVE_TYPE = 'errArcType'; |
||
241 | const ERROR_ARC_SYMLINKS = 'errArcSymlinks'; |
||
242 | const ERROR_ARC_MAXSIZE = 'errArcMaxSize'; |
||
243 | const ERROR_RESIZE = 'errResize'; |
||
244 | const ERROR_UNSUPPORT_TYPE = 'errUsupportType'; |
||
245 | const ERROR_CONV_UTF8 = 'errConvUTF8'; |
||
246 | const ERROR_NOT_UTF8_CONTENT = 'errNotUTF8Content'; |
||
247 | const ERROR_NETMOUNT = 'errNetMount'; |
||
248 | const ERROR_NETUNMOUNT = 'errNetUnMount'; |
||
249 | const ERROR_NETMOUNT_NO_DRIVER = 'errNetMountNoDriver'; |
||
250 | const ERROR_NETMOUNT_FAILED = 'errNetMountFailed'; |
||
251 | |||
252 | const ERROR_SESSION_EXPIRES = 'errSessionExpires'; |
||
253 | |||
254 | const ERROR_CREATING_TEMP_DIR = 'errCreatingTempDir'; |
||
255 | const ERROR_FTP_DOWNLOAD_FILE = 'errFtpDownloadFile'; |
||
256 | const ERROR_FTP_UPLOAD_FILE = 'errFtpUploadFile'; |
||
257 | const ERROR_FTP_MKDIR = 'errFtpMkdir'; |
||
258 | const ERROR_ARCHIVE_EXEC = 'errArchiveExec'; |
||
259 | const ERROR_EXTRACT_EXEC = 'errExtractExec'; |
||
260 | const ERROR_SEARCH_TIMEOUT = 'errSearchTimeout'; // 'Timed out while searching "$1". Search result is partial.' |
||
261 | |||
262 | /** |
||
263 | * Constructor |
||
264 | * |
||
265 | * @param array elFinder and roots configurations |
||
266 | * @return void |
||
267 | * @author Dmitry (dio) Levashov |
||
268 | **/ |
||
269 | public function __construct($opts) { |
||
270 | // try session start | restart |
||
271 | @session_start(); |
||
272 | |||
273 | $sessionUseCmds = array(); |
||
274 | if (isset($opts['sessionUseCmds']) && is_array($opts['sessionUseCmds'])) { |
||
275 | $sessionUseCmds = $opts['sessionUseCmds']; |
||
276 | } |
||
277 | |||
278 | // set self::$volumesCnt by HTTP header "X-elFinder-VolumesCntStart" |
||
279 | if (isset($_SERVER['HTTP_X_ELFINDER_VOLUMESCNTSTART']) && ($volumesCntStart = intval($_SERVER['HTTP_X_ELFINDER_VOLUMESCNTSTART']))) { |
||
280 | self::$volumesCnt = $volumesCntStart; |
||
281 | } |
||
282 | |||
283 | $this->time = $this->utime(); |
||
284 | $this->debug = (isset($opts['debug']) && $opts['debug'] ? true : false); |
||
285 | $this->sessionCloseEarlier = isset($opts['sessionCloseEarlier'])? (bool)$opts['sessionCloseEarlier'] : true; |
||
286 | $this->sessionUseCmds = array_flip($sessionUseCmds); |
||
287 | $this->timeout = (isset($opts['timeout']) ? $opts['timeout'] : 0); |
||
288 | $this->uploadTempPath = (isset($opts['uploadTempPath']) ? $opts['uploadTempPath'] : ''); |
||
289 | $this->netVolumesSessionKey = !empty($opts['netVolumesSessionKey'])? $opts['netVolumesSessionKey'] : 'elFinderNetVolumes'; |
||
290 | $this->callbackWindowURL = (isset($opts['callbackWindowURL']) ? $opts['callbackWindowURL'] : ''); |
||
291 | self::$sessionCacheKey = !empty($opts['sessionCacheKey']) ? $opts['sessionCacheKey'] : 'elFinderCaches'; |
||
292 | |||
293 | // check session cache |
||
294 | $_optsMD5 = md5(json_encode($opts['roots'])); |
||
295 | if (! isset($_SESSION[self::$sessionCacheKey]) || $_SESSION[self::$sessionCacheKey]['_optsMD5'] !== $_optsMD5) { |
||
296 | $_SESSION[self::$sessionCacheKey] = array( |
||
297 | '_optsMD5' => $_optsMD5 |
||
298 | ); |
||
299 | } |
||
300 | self::$base64encodeSessionData = !empty($opts['base64encodeSessionData']); |
||
301 | |||
302 | // setlocale and global locale regists to elFinder::locale |
||
303 | self::$locale = !empty($opts['locale']) ? $opts['locale'] : 'en_US.UTF-8'; |
||
304 | if (false === @setlocale(LC_ALL, self::$locale)) { |
||
305 | self::$locale = setlocale(LC_ALL, ''); |
||
306 | } |
||
307 | |||
308 | // bind events listeners |
||
309 | if (!empty($opts['bind']) && is_array($opts['bind'])) { |
||
310 | $_req = $_SERVER["REQUEST_METHOD"] == 'POST' ? $_POST : $_GET; |
||
311 | $_reqCmd = isset($_req['cmd']) ? $_req['cmd'] : ''; |
||
312 | foreach ($opts['bind'] as $cmd => $handlers) { |
||
313 | $doRegist = (strpos($cmd, '*') !== false); |
||
314 | if (! $doRegist) { |
||
315 | $_getcmd = create_function('$cmd', 'list($ret) = explode(\'.\', $cmd);return trim($ret);'); |
||
316 | $doRegist = ($_reqCmd && in_array($_reqCmd, array_map($_getcmd, explode(' ', $cmd)))); |
||
317 | } |
||
318 | if ($doRegist) { |
||
319 | if (! is_array($handlers) || is_object($handlers[0])) { |
||
320 | $handlers = array($handlers); |
||
321 | } |
||
322 | foreach($handlers as $handler) { |
||
323 | if ($handler) { |
||
324 | if (is_string($handler) && strpos($handler, '.')) { |
||
325 | list($_domain, $_name, $_method) = array_pad(explode('.', $handler), 3, ''); |
||
326 | if (strcasecmp($_domain, 'plugin') === 0) { |
||
327 | if ($plugin = $this->getPluginInstance($_name, isset($opts['plugin'][$_name])? $opts['plugin'][$_name] : array()) |
||
328 | and method_exists($plugin, $_method)) { |
||
329 | $this->bind($cmd, array($plugin, $_method)); |
||
330 | } |
||
331 | } |
||
332 | } else { |
||
333 | $this->bind($cmd, $handler); |
||
334 | } |
||
335 | } |
||
336 | } |
||
337 | } |
||
338 | } |
||
339 | } |
||
340 | |||
341 | if (!isset($opts['roots']) || !is_array($opts['roots'])) { |
||
342 | $opts['roots'] = array(); |
||
343 | } |
||
344 | |||
345 | // check for net volumes stored in session |
||
346 | foreach ($this->getNetVolumes() as $key => $root) { |
||
347 | $opts['roots'][$key] = $root; |
||
348 | } |
||
349 | |||
350 | // "mount" volumes |
||
351 | foreach ($opts['roots'] as $i => $o) { |
||
352 | $class = 'elFinderVolume'.(isset($o['driver']) ? $o['driver'] : ''); |
||
353 | |||
354 | if (class_exists($class)) { |
||
355 | $volume = new $class(); |
||
356 | |||
357 | try { |
||
358 | if ($volume->mount($o)) { |
||
359 | // unique volume id (ends on "_") - used as prefix to files hash |
||
360 | $id = $volume->id(); |
||
361 | |||
362 | $this->volumes[$id] = $volume; |
||
363 | if ((!$this->default || $volume->root() !== $volume->defaultPath()) && $volume->isReadable()) { |
||
364 | $this->default = $this->volumes[$id]; |
||
365 | } |
||
366 | } else { |
||
367 | $this->removeNetVolume($i); |
||
368 | $this->mountErrors[] = 'Driver "'.$class.'" : '.implode(' ', $volume->error()); |
||
369 | } |
||
370 | } catch (Exception $e) { |
||
371 | $this->removeNetVolume($i); |
||
372 | $this->mountErrors[] = 'Driver "'.$class.'" : '.$e->getMessage(); |
||
373 | } |
||
374 | } else { |
||
375 | $this->mountErrors[] = 'Driver "'.$class.'" does not exists'; |
||
376 | } |
||
377 | } |
||
378 | |||
379 | // if at least one readable volume - ii desu >_< |
||
380 | $this->loaded = !empty($this->default); |
||
381 | } |
||
382 | |||
383 | /** |
||
384 | * Return true if fm init correctly |
||
385 | * |
||
386 | * @return bool |
||
387 | * @author Dmitry (dio) Levashov |
||
388 | **/ |
||
389 | public function loaded() { |
||
390 | return $this->loaded; |
||
391 | } |
||
392 | |||
393 | /** |
||
394 | * Return version (api) number |
||
395 | * |
||
396 | * @return string |
||
397 | * @author Dmitry (dio) Levashov |
||
398 | **/ |
||
399 | public function version() { |
||
400 | return $this->version; |
||
401 | } |
||
402 | |||
403 | /** |
||
404 | * Add handler to elFinder command |
||
405 | * |
||
406 | * @param string command name |
||
407 | * @param string|array callback name or array(object, method) |
||
408 | * @return elFinder |
||
409 | * @author Dmitry (dio) Levashov |
||
410 | **/ |
||
411 | public function bind($cmd, $handler) { |
||
412 | $allCmds = array_keys($this->commands); |
||
413 | $cmds = array(); |
||
414 | foreach(explode(' ', $cmd) as $_cmd) { |
||
415 | if ($_cmd !== '') { |
||
416 | if ($all = strpos($_cmd, '*') !== false) { |
||
417 | list(, $sub) = array_pad(explode('.', $_cmd), 2, ''); |
||
418 | if ($sub) { |
||
419 | $sub = str_replace('\'', '\\\'', $sub); |
||
420 | $addSub = create_function('$cmd', 'return $cmd . \'.\' . trim(\'' . $sub . '\');'); |
||
421 | $cmds = array_merge($cmds, array_map($addSub, $allCmds)); |
||
422 | } else { |
||
423 | $cmds = array_merge($cmds, $allCmds); |
||
424 | } |
||
425 | } else { |
||
426 | $cmds[] = $_cmd; |
||
427 | } |
||
428 | } |
||
429 | } |
||
430 | $cmds = array_unique($cmds); |
||
431 | |||
432 | foreach ($cmds as $cmd) { |
||
433 | if (!isset($this->listeners[$cmd])) { |
||
434 | $this->listeners[$cmd] = array(); |
||
435 | } |
||
436 | |||
437 | if (is_callable($handler)) { |
||
438 | $this->listeners[$cmd][] = $handler; |
||
439 | } |
||
440 | } |
||
441 | |||
442 | return $this; |
||
443 | } |
||
444 | |||
445 | /** |
||
446 | * Remove event (command exec) handler |
||
447 | * |
||
448 | * @param string command name |
||
449 | * @param string|array callback name or array(object, method) |
||
450 | * @return elFinder |
||
451 | * @author Dmitry (dio) Levashov |
||
452 | **/ |
||
453 | public function unbind($cmd, $handler) { |
||
454 | if (!empty($this->listeners[$cmd])) { |
||
455 | foreach ($this->listeners[$cmd] as $i => $h) { |
||
456 | if ($h === $handler) { |
||
457 | unset($this->listeners[$cmd][$i]); |
||
458 | return $this; |
||
459 | } |
||
460 | } |
||
461 | } |
||
462 | return $this; |
||
463 | } |
||
464 | |||
465 | /** |
||
466 | * Return true if command exists |
||
467 | * |
||
468 | * @param string command name |
||
469 | * @return bool |
||
470 | * @author Dmitry (dio) Levashov |
||
471 | **/ |
||
472 | public function commandExists($cmd) { |
||
473 | return $this->loaded && isset($this->commands[$cmd]) && method_exists($this, $cmd); |
||
474 | } |
||
475 | |||
476 | /** |
||
477 | * Return root - file's owner (public func of volume()) |
||
478 | * |
||
479 | * @param string file hash |
||
480 | * @return elFinderStorageDriver |
||
481 | * @author Naoki Sawada |
||
482 | */ |
||
483 | public function getVolume($hash) { |
||
484 | return $this->volume($hash); |
||
485 | } |
||
486 | |||
487 | /** |
||
488 | * Return command required arguments info |
||
489 | * |
||
490 | * @param string command name |
||
491 | * @return array |
||
492 | * @author Dmitry (dio) Levashov |
||
493 | **/ |
||
494 | public function commandArgsList($cmd) { |
||
495 | return $this->commandExists($cmd) ? $this->commands[$cmd] : array(); |
||
496 | } |
||
497 | |||
498 | private function session_expires() { |
||
499 | |||
500 | if (!isset($_SESSION[self::$sessionCacheKey . ':LAST_ACTIVITY'])) { |
||
501 | $_SESSION[self::$sessionCacheKey . ':LAST_ACTIVITY'] = time(); |
||
502 | return false; |
||
503 | } |
||
504 | |||
505 | if ( ($this->timeout > 0) && (time() - $_SESSION[self::$sessionCacheKey . ':LAST_ACTIVITY'] > $this->timeout) ) { |
||
506 | return true; |
||
507 | } |
||
508 | |||
509 | $_SESSION[self::$sessionCacheKey . ':LAST_ACTIVITY'] = time(); |
||
510 | return false; |
||
511 | } |
||
512 | |||
513 | /** |
||
514 | * Exec command and return result |
||
515 | * |
||
516 | * @param string $cmd command name |
||
517 | * @param array $args command arguments |
||
518 | * @return array |
||
519 | * @author Dmitry (dio) Levashov |
||
520 | **/ |
||
521 | public function exec($cmd, $args) { |
||
522 | |||
523 | if (!$this->loaded) { |
||
524 | return array('error' => $this->error(self::ERROR_CONF, self::ERROR_CONF_NO_VOL)); |
||
525 | } |
||
526 | |||
527 | if ($this->session_expires()) { |
||
528 | return array('error' => $this->error(self::ERROR_SESSION_EXPIRES)); |
||
529 | } |
||
530 | |||
531 | if (!$this->commandExists($cmd)) { |
||
532 | return array('error' => $this->error(self::ERROR_UNKNOWN_CMD)); |
||
533 | } |
||
534 | |||
535 | if (!empty($args['mimes']) && is_array($args['mimes'])) { |
||
536 | foreach ($this->volumes as $id => $v) { |
||
537 | $this->volumes[$id]->setMimesFilter($args['mimes']); |
||
538 | } |
||
539 | } |
||
540 | |||
541 | // call pre handlers for this command |
||
542 | $args['sessionCloseEarlier'] = isset($this->sessionUseCmds[$cmd])? false : $this->sessionCloseEarlier; |
||
543 | if (!empty($this->listeners[$cmd.'.pre'])) { |
||
544 | $volume = isset($args['target'])? $this->volume($args['target']) : false; |
||
545 | View Code Duplication | foreach ($this->listeners[$cmd.'.pre'] as $handler) { |
|
546 | call_user_func_array($handler, array($cmd, &$args, $this, $volume)); |
||
547 | } |
||
548 | } |
||
549 | |||
550 | // unlock session data for multiple access |
||
551 | $this->sessionCloseEarlier && $args['sessionCloseEarlier'] && session_id() && session_write_close(); |
||
552 | |||
553 | if (substr(PHP_OS,0,3) === 'WIN') { |
||
554 | // set time out |
||
555 | if (($_max_execution_time = ini_get('max_execution_time')) && $_max_execution_time < 300) { |
||
556 | @set_time_limit(300); |
||
557 | } |
||
558 | } |
||
559 | |||
560 | $result = $this->$cmd($args); |
||
561 | |||
562 | if (isset($result['removed'])) { |
||
563 | foreach ($this->volumes as $volume) { |
||
564 | $result['removed'] = array_merge($result['removed'], $volume->removed()); |
||
565 | $volume->resetRemoved(); |
||
566 | } |
||
567 | } |
||
568 | |||
569 | // call handlers for this command |
||
570 | if (!empty($this->listeners[$cmd])) { |
||
571 | foreach ($this->listeners[$cmd] as $handler) { |
||
572 | if (call_user_func_array($handler,array($cmd,&$result,$args,$this))) { |
||
573 | // handler return true to force sync client after command completed |
||
574 | $result['sync'] = true; |
||
575 | } |
||
576 | } |
||
577 | } |
||
578 | |||
579 | // replace removed files info with removed files hashes |
||
580 | if (!empty($result['removed'])) { |
||
581 | $removed = array(); |
||
582 | foreach ($result['removed'] as $file) { |
||
583 | $removed[] = $file['hash']; |
||
584 | } |
||
585 | $result['removed'] = array_unique($removed); |
||
586 | } |
||
587 | // remove hidden files and filter files by mimetypes |
||
588 | if (!empty($result['added'])) { |
||
589 | $result['added'] = $this->filter($result['added']); |
||
590 | } |
||
591 | // remove hidden files and filter files by mimetypes |
||
592 | if (!empty($result['changed'])) { |
||
593 | $result['changed'] = $this->filter($result['changed']); |
||
594 | } |
||
595 | |||
596 | if ($this->debug || !empty($args['debug'])) { |
||
597 | $result['debug'] = array( |
||
598 | 'connector' => 'php', |
||
599 | 'phpver' => PHP_VERSION, |
||
600 | 'time' => $this->utime() - $this->time, |
||
601 | 'memory' => (function_exists('memory_get_peak_usage') ? ceil(memory_get_peak_usage()/1024).'Kb / ' : '').ceil(memory_get_usage()/1024).'Kb / '.ini_get('memory_limit'), |
||
602 | 'upload' => $this->uploadDebug, |
||
603 | 'volumes' => array(), |
||
604 | 'mountErrors' => $this->mountErrors |
||
605 | ); |
||
606 | |||
607 | foreach ($this->volumes as $id => $volume) { |
||
608 | $result['debug']['volumes'][] = $volume->debug(); |
||
609 | } |
||
610 | } |
||
611 | |||
612 | foreach ($this->volumes as $volume) { |
||
613 | $volume->umount(); |
||
614 | } |
||
615 | |||
616 | if (!empty($result['callback'])) { |
||
617 | $result['callback']['json'] = json_encode($result); |
||
618 | $this->callback($result['callback']); |
||
619 | } else { |
||
620 | return $result; |
||
621 | } |
||
622 | } |
||
623 | |||
624 | /** |
||
625 | * Return file real path |
||
626 | * |
||
627 | * @param string $hash file hash |
||
628 | * @return string |
||
629 | * @author Dmitry (dio) Levashov |
||
630 | **/ |
||
631 | public function realpath($hash) { |
||
637 | |||
638 | /** |
||
639 | * Return network volumes config. |
||
640 | * |
||
641 | * @return array |
||
642 | * @author Dmitry (dio) Levashov |
||
643 | */ |
||
644 | protected function getNetVolumes() { |
||
652 | |||
653 | /** |
||
654 | * Save network volumes config. |
||
655 | * |
||
656 | * @param array $volumes volumes config |
||
657 | * @return void |
||
658 | * @author Dmitry (dio) Levashov |
||
659 | */ |
||
660 | protected function saveNetVolumes($volumes) { |
||
663 | |||
664 | /** |
||
665 | * Remove netmount volume |
||
666 | * |
||
667 | * @param string $key netvolume key |
||
668 | */ |
||
669 | protected function removeNetVolume($key) { |
||
676 | |||
677 | /** |
||
678 | * Get plugin instance & set to $this->plugins |
||
679 | * |
||
680 | * @param string $name Plugin name (dirctory name) |
||
681 | * @param array $opts Plugin options (optional) |
||
682 | * @return object | bool Plugin object instance Or false |
||
683 | * @author Naoki Sawada |
||
684 | */ |
||
685 | protected function getPluginInstance($name, $opts = array()) { |
||
699 | |||
700 | /***************************************************************************/ |
||
701 | /* commands */ |
||
702 | /***************************************************************************/ |
||
703 | |||
704 | /** |
||
705 | * Normalize error messages |
||
706 | * |
||
707 | * @return array |
||
708 | * @author Dmitry (dio) Levashov |
||
709 | **/ |
||
710 | public function error() { |
||
723 | |||
724 | protected function netmount($args) { |
||
725 | // try session restart |
||
726 | @session_start(); |
||
727 | |||
728 | $options = array(); |
||
729 | $protocol = $args['protocol']; |
||
730 | |||
731 | if ($protocol === 'netunmount') { |
||
732 | $key = $args['host']; |
||
733 | $netVolumes = $this->getNetVolumes(); |
||
802 | |||
803 | /** |
||
804 | * "Open" directory |
||
805 | * Return array with following elements |
||
806 | * - cwd - opened dir info |
||
807 | * - files - opened dir content [and dirs tree if $args[tree]] |
||
808 | * - api - api version (if $args[init]) |
||
809 | * - uplMaxSize - if $args[init] |
||
810 | * - error - on failed |
||
811 | * |
||
812 | * @param array command arguments |
||
813 | * @return array |
||
814 | * @author Dmitry (dio) Levashov |
||
815 | **/ |
||
816 | protected function open($args) { |
||
914 | |||
915 | /** |
||
916 | * Return dir files names list |
||
917 | * |
||
918 | * @param array command arguments |
||
919 | * @return array |
||
920 | * @author Dmitry (dio) Levashov |
||
921 | **/ |
||
922 | View Code Duplication | protected function ls($args) { |
|
931 | |||
932 | /** |
||
933 | * Return subdirs for required directory |
||
934 | * |
||
935 | * @param array command arguments |
||
936 | * @return array |
||
937 | * @author Dmitry (dio) Levashov |
||
938 | **/ |
||
939 | View Code Duplication | protected function tree($args) { |
|
949 | |||
950 | /** |
||
951 | * Return parents dir for required directory |
||
952 | * |
||
953 | * @param array command arguments |
||
954 | * @return array |
||
955 | * @author Dmitry (dio) Levashov |
||
956 | **/ |
||
957 | View Code Duplication | protected function parents($args) { |
|
967 | |||
968 | /** |
||
969 | * Return new created thumbnails list |
||
970 | * |
||
971 | * @param array command arguments |
||
972 | * @return array |
||
973 | * @author Dmitry (dio) Levashov |
||
974 | **/ |
||
975 | protected function tmb($args) { |
||
988 | |||
989 | /** |
||
990 | * Required to output file in browser when volume URL is not set |
||
991 | * Return array contains opened file pointer, root itself and required headers |
||
992 | * |
||
993 | * @param array command arguments |
||
994 | * @return array |
||
995 | * @author Dmitry (dio) Levashov |
||
996 | **/ |
||
997 | protected function file($args) { |
||
1071 | |||
1072 | /** |
||
1073 | * Count total files size |
||
1074 | * |
||
1075 | * @param array command arguments |
||
1076 | * @return array |
||
1077 | * @author Dmitry (dio) Levashov |
||
1078 | **/ |
||
1079 | protected function size($args) { |
||
1093 | |||
1094 | /** |
||
1095 | * Create directory |
||
1096 | * |
||
1097 | * @param array command arguments |
||
1098 | * @return array |
||
1099 | * @author Dmitry (dio) Levashov |
||
1100 | **/ |
||
1101 | View Code Duplication | protected function mkdir($args) { |
|
1113 | |||
1114 | /** |
||
1115 | * Create empty file |
||
1116 | * |
||
1117 | * @param array command arguments |
||
1118 | * @return array |
||
1119 | * @author Dmitry (dio) Levashov |
||
1120 | **/ |
||
1121 | View Code Duplication | protected function mkfile($args) { |
|
1133 | |||
1134 | /** |
||
1135 | * Rename file |
||
1136 | * |
||
1137 | * @param array $args |
||
1138 | * @return array |
||
1139 | * @author Dmitry (dio) Levashov |
||
1140 | **/ |
||
1141 | protected function rename($args) { |
||
1155 | |||
1156 | /** |
||
1157 | * Duplicate file - create copy with "copy %d" suffix |
||
1158 | * |
||
1159 | * @param array $args command arguments |
||
1160 | * @return array |
||
1161 | * @author Dmitry (dio) Levashov |
||
1162 | **/ |
||
1163 | protected function duplicate($args) { |
||
1185 | |||
1186 | /** |
||
1187 | * Remove dirs/files |
||
1188 | * |
||
1189 | * @param array command arguments |
||
1190 | * @return array |
||
1191 | * @author Dmitry (dio) Levashov |
||
1192 | **/ |
||
1193 | protected function rm($args) { |
||
1210 | |||
1211 | /** |
||
1212 | * Get remote contents |
||
1213 | * |
||
1214 | * @param string $url target url |
||
1215 | * @param int $timeout timeout (sec) |
||
1216 | * @param int $redirect_max redirect max count |
||
1217 | * @param string $ua |
||
1218 | * @param resource $fp |
||
1219 | * @return string or bool(false) |
||
1220 | * @retval string contents |
||
1221 | * @retval false error |
||
1222 | * @author Naoki Sawada |
||
1223 | **/ |
||
1224 | protected function get_remote_contents( &$url, $timeout = 30, $redirect_max = 5, $ua = 'Mozilla/5.0', $fp = null ) { |
||
1228 | |||
1229 | /** |
||
1230 | * Get remote contents with cURL |
||
1231 | * |
||
1232 | * @param string $url target url |
||
1233 | * @param int $timeout timeout (sec) |
||
1234 | * @param int $redirect_max redirect max count |
||
1235 | * @param string $ua |
||
1236 | * @param resource $outfp |
||
1237 | * @return string or bool(false) |
||
1238 | * @retval string contents |
||
1239 | * @retval false error |
||
1240 | * @author Naoki Sawada |
||
1241 | **/ |
||
1242 | protected function curl_get_contents( &$url, $timeout, $redirect_max, $ua, $outfp ){ |
||
1263 | |||
1264 | /** |
||
1265 | * Get remote contents with fsockopen() |
||
1266 | * |
||
1267 | * @param string $url url |
||
1268 | * @param int $timeout timeout (sec) |
||
1269 | * @param int $redirect_max redirect max count |
||
1270 | * @param string $ua |
||
1271 | * @param resource $outfp |
||
1272 | * @return string or bool(false) |
||
1273 | * @retval string contents |
||
1274 | * @retval false error |
||
1275 | * @author Naoki Sawada |
||
1276 | */ |
||
1277 | protected function fsock_get_contents( &$url, $timeout, $redirect_max, $ua, $outfp ) { |
||
1401 | |||
1402 | /** |
||
1403 | * Parse Data URI scheme |
||
1404 | * |
||
1405 | * @param string $str |
||
1406 | * @param array $extTable |
||
1407 | * @return array |
||
1408 | * @author Naoki Sawada |
||
1409 | */ |
||
1410 | protected function parse_data_scheme( $str, $extTable ) { |
||
1422 | |||
1423 | /** |
||
1424 | * Detect file type extension by local path |
||
1425 | * |
||
1426 | * @param string $path Local path |
||
1427 | * @return string file type extension with dot |
||
1428 | * @author Naoki Sawada |
||
1429 | */ |
||
1430 | protected function detectFileExtension($path) { |
||
1482 | |||
1483 | /** |
||
1484 | * Get temporary dirctroy path |
||
1485 | * |
||
1486 | * @param string $volumeTempPath |
||
1487 | * @return string |
||
1488 | * @author Naoki Sawada |
||
1489 | */ |
||
1490 | private function getTempDir($volumeTempPath = null) { |
||
1517 | |||
1518 | /** |
||
1519 | * chmod |
||
1520 | * |
||
1521 | * @param array command arguments |
||
1522 | * @return array |
||
1523 | * @author David Bartle |
||
1524 | **/ |
||
1525 | protected function chmod($args) { |
||
1562 | |||
1563 | /** |
||
1564 | * Check chunked upload files |
||
1565 | * |
||
1566 | * @param string $tmpname uploaded temporary file path |
||
1567 | * @param string $chunk uploaded chunk file name |
||
1568 | * @param string $cid uploaded chunked file id |
||
1569 | * @param string $tempDir temporary dirctroy path |
||
1570 | * @return array (string JoinedTemporaryFilePath, string FileName) or (empty, empty) |
||
1571 | * @author Naoki Sawada |
||
1572 | */ |
||
1573 | private function checkChunkedFile($tmpname, $chunk, $cid, $tempDir, $volume = null) { |
||
1696 | |||
1697 | /** |
||
1698 | * Save uploaded files |
||
1699 | * |
||
1700 | * @param array |
||
1701 | * @return array |
||
1702 | * @author Dmitry (dio) Levashov |
||
1703 | **/ |
||
1704 | protected function upload($args) { |
||
1962 | |||
1963 | /** |
||
1964 | * Copy/move files into new destination |
||
1965 | * |
||
1966 | * @param array command arguments |
||
1967 | * @return array |
||
1968 | * @author Dmitry (dio) Levashov |
||
1969 | **/ |
||
1970 | protected function paste($args) { |
||
2022 | |||
2023 | /** |
||
2024 | * Return file content |
||
2025 | * |
||
2026 | * @param array $args command arguments |
||
2027 | * @return array |
||
2028 | * @author Dmitry (dio) Levashov |
||
2029 | **/ |
||
2030 | protected function get($args) { |
||
2065 | |||
2066 | /** |
||
2067 | * Save content into text file |
||
2068 | * |
||
2069 | * @return array |
||
2070 | * @author Dmitry (dio) Levashov |
||
2071 | **/ |
||
2072 | protected function put($args) { |
||
2086 | |||
2087 | /** |
||
2088 | * Extract files from archive |
||
2089 | * |
||
2090 | * @param array $args command arguments |
||
2091 | * @return array |
||
2092 | * @author Dmitry (dio) Levashov, |
||
2093 | * @author Alexey Sukhotin |
||
2094 | **/ |
||
2095 | protected function extract($args) { |
||
2110 | |||
2111 | /** |
||
2112 | * Create archive |
||
2113 | * |
||
2114 | * @param array $args command arguments |
||
2115 | * @return array |
||
2116 | * @author Dmitry (dio) Levashov, |
||
2117 | * @author Alexey Sukhotin |
||
2118 | **/ |
||
2119 | protected function archive($args) { |
||
2132 | |||
2133 | /** |
||
2134 | * Search files |
||
2135 | * |
||
2136 | * @param array $args command arguments |
||
2137 | * @return array |
||
2138 | * @author Dmitry Levashov |
||
2139 | **/ |
||
2140 | protected function search($args) { |
||
2165 | |||
2166 | /** |
||
2167 | * Return file info (used by client "places" ui) |
||
2168 | * |
||
2169 | * @param array $args command arguments |
||
2170 | * @return array |
||
2171 | * @author Dmitry Levashov |
||
2172 | **/ |
||
2173 | protected function info($args) { |
||
2226 | |||
2227 | /** |
||
2228 | * Return image dimmensions |
||
2229 | * |
||
2230 | * @param array $args command arguments |
||
2231 | * @return array |
||
2232 | * @author Dmitry (dio) Levashov |
||
2233 | **/ |
||
2234 | protected function dim($args) { |
||
2243 | |||
2244 | /** |
||
2245 | * Resize image |
||
2246 | * |
||
2247 | * @param array command arguments |
||
2248 | * @return array |
||
2249 | * @author Dmitry (dio) Levashov |
||
2250 | * @author Alexey Sukhotin |
||
2251 | **/ |
||
2252 | protected function resize($args) { |
||
2272 | |||
2273 | /** |
||
2274 | * Return content URL |
||
2275 | * |
||
2276 | * @param array $args command arguments |
||
2277 | * @return array |
||
2278 | * @author Naoki Sawada |
||
2279 | **/ |
||
2280 | protected function url($args) { |
||
2289 | |||
2290 | /** |
||
2291 | * Output callback result with JavaScript that control elFinder |
||
2292 | * or HTTP redirect to callbackWindowURL |
||
2293 | * |
||
2294 | * @param array command arguments |
||
2295 | * @author Naoki Sawada |
||
2296 | */ |
||
2297 | protected function callback($args) { |
||
2363 | |||
2364 | /***************************************************************************/ |
||
2365 | /* utils */ |
||
2366 | /***************************************************************************/ |
||
2367 | |||
2368 | /** |
||
2369 | * Return root - file's owner |
||
2370 | * |
||
2371 | * @param string file hash |
||
2372 | * @return elFinderStorageDriver |
||
2373 | * @author Dmitry (dio) Levashov |
||
2374 | **/ |
||
2375 | protected function volume($hash) { |
||
2383 | |||
2384 | /** |
||
2385 | * Return files info array |
||
2386 | * |
||
2387 | * @param array $data one file info or files info |
||
2388 | * @return array |
||
2389 | * @author Dmitry (dio) Levashov |
||
2390 | **/ |
||
2391 | protected function toArray($data) { |
||
2394 | |||
2395 | /** |
||
2396 | * Return fils hashes list |
||
2397 | * |
||
2398 | * @param array $files files info |
||
2399 | * @return array |
||
2400 | * @author Dmitry (dio) Levashov |
||
2401 | **/ |
||
2402 | protected function hashes($files) { |
||
2409 | |||
2410 | /** |
||
2411 | * Remove from files list hidden files and files with required mime types |
||
2412 | * |
||
2413 | * @param array $files files info |
||
2414 | * @return array |
||
2415 | * @author Dmitry (dio) Levashov |
||
2416 | **/ |
||
2417 | protected function filter($files) { |
||
2425 | |||
2426 | protected function utime() { |
||
2430 | |||
2431 | |||
2432 | /***************************************************************************/ |
||
2433 | /* static utils */ |
||
2434 | /***************************************************************************/ |
||
2435 | |||
2436 | /** |
||
2437 | * Return Is Animation Gif |
||
2438 | * |
||
2439 | * @param string $path server local path of target image |
||
2440 | * @return bool |
||
2441 | */ |
||
2442 | public static function isAnimationGif($path) { |
||
2484 | |||
2485 | /** |
||
2486 | * Return Is seekable stream resource |
||
2487 | * |
||
2488 | * @param resource $resource |
||
2489 | * @return bool |
||
2490 | */ |
||
2491 | public static function isSeekableStream($resource) { |
||
2495 | |||
2496 | /** |
||
2497 | * serialize and base64_encode of session data (If needed) |
||
2498 | * |
||
2499 | * @param mixed $var target variable |
||
2500 | * @author Naoki Sawada |
||
2501 | */ |
||
2502 | public static function sessionDataEncode($var) { |
||
2508 | |||
2509 | /** |
||
2510 | * base64_decode and unserialize of session data (If needed) |
||
2511 | * |
||
2512 | * @param mixed $var target variable |
||
2513 | * @param bool $checkIs data type for check (array|string|object|int) |
||
2514 | * @author Naoki Sawada |
||
2515 | */ |
||
2516 | public static function sessionDataDecode(&$var, $checkIs = null) { |
||
2545 | } // END class |
||
2546 |
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.