1<?php
2
3// Implement similar functionality in PHP 5.2 or 5.3
4// http://php.net/manual/class.recursivecallbackfilteriterator.php#110974
5if (!class_exists('RecursiveCallbackFilterIterator', false)) {
6    class RecursiveCallbackFilterIterator extends RecursiveFilterIterator
7    {
8        private $callback;
9
10        public function __construct(RecursiveIterator $iterator, $callback)
11        {
12            $this->callback = $callback;
13            parent::__construct($iterator);
14        }
15
16        public function accept()
17        {
18            return call_user_func($this->callback, parent::current(), parent::key(), parent::getInnerIterator());
19        }
20
21        public function getChildren()
22        {
23            return new self($this->getInnerIterator()->getChildren(), $this->callback);
24        }
25    }
26}
27
28/**
29 * elFinder driver for local filesystem.
30 *
31 * @author Dmitry (dio) Levashov
32 * @author Troex Nevelin
33 **/
34class elFinderVolumeLocalFileSystem extends elFinderVolumeDriver
35{
36
37    /**
38     * Driver id
39     * Must be started from letter and contains [a-z0-9]
40     * Used as part of volume id
41     *
42     * @var string
43     **/
44    protected $driverId = 'l';
45
46    /**
47     * Required to count total archive files size
48     *
49     * @var int
50     **/
51    protected $archiveSize = 0;
52
53    /**
54     * Is checking stat owner
55     *
56     * @var        boolean
57     */
58    protected $statOwner = false;
59
60    /**
61     * Path to quarantine directory
62     *
63     * @var string
64     */
65    private $quarantine;
66
67    /**
68     * Constructor
69     * Extend options with required fields
70     *
71     * @author Dmitry (dio) Levashov
72     */
73    public function __construct()
74    {
75        $this->options['alias'] = '';              // alias to replace root dir name
76        $this->options['dirMode'] = 0755;            // new dirs mode
77        $this->options['fileMode'] = 0644;            // new files mode
78        $this->options['quarantine'] = '.quarantine'; // quarantine folder name - required to check archive (must be hidden)
79        $this->options['rootCssClass'] = 'elfinder-navbar-root-local';
80        $this->options['followSymLinks'] = true;
81        $this->options['detectDirIcon'] = '';         // file name that is detected as a folder icon e.g. '.diricon.png'
82        $this->options['keepTimestamp'] = array('copy', 'move'); // keep timestamp at inner filesystem allowed 'copy', 'move' and 'upload'
83        $this->options['substituteImg'] = true;       // support substitute image with dim command
84        $this->options['statCorrector'] = null;       // callable to correct stat data `function(&$stat, $path, $statOwner, $volumeDriveInstance){}`
85    }
86
87    /*********************************************************************/
88    /*                        INIT AND CONFIGURE                         */
89    /*********************************************************************/
90
91    /**
92     * Prepare driver before mount volume.
93     * Return true if volume is ready.
94     *
95     * @return bool
96     **/
97    protected function init()
98    {
99        // Normalize directory separator for windows
100        if (DIRECTORY_SEPARATOR !== '/') {
101            foreach (array('path', 'tmbPath', 'tmpPath', 'quarantine') as $key) {
102                if (!empty($this->options[$key])) {
103                    $this->options[$key] = str_replace('/', DIRECTORY_SEPARATOR, $this->options[$key]);
104                }
105            }
106            // PHP >= 7.1 Supports UTF-8 path on Windows
107            if (version_compare(PHP_VERSION, '7.1', '>=')) {
108                $this->options['encoding'] = '';
109                $this->options['locale'] = '';
110            }
111        }
112        if (!$cwd = getcwd()) {
113            return $this->setError('elFinder LocalVolumeDriver requires a result of getcwd().');
114        }
115        // detect systemRoot
116        if (!isset($this->options['systemRoot'])) {
117            if ($cwd[0] === DIRECTORY_SEPARATOR || $this->root[0] === DIRECTORY_SEPARATOR) {
118                $this->systemRoot = DIRECTORY_SEPARATOR;
119            } else if (preg_match('/^([a-zA-Z]:' . preg_quote(DIRECTORY_SEPARATOR, '/') . ')/', $this->root, $m)) {
120                $this->systemRoot = $m[1];
121            } else if (preg_match('/^([a-zA-Z]:' . preg_quote(DIRECTORY_SEPARATOR, '/') . ')/', $cwd, $m)) {
122                $this->systemRoot = $m[1];
123            }
124        }
125        $this->root = $this->getFullPath($this->root, $cwd);
126        if (!empty($this->options['startPath'])) {
127            $this->options['startPath'] = $this->getFullPath($this->options['startPath'], $this->root);
128        }
129
130        if (is_null($this->options['syncChkAsTs'])) {
131            $this->options['syncChkAsTs'] = true;
132        }
133        if (is_null($this->options['syncCheckFunc'])) {
134            $this->options['syncCheckFunc'] = array($this, 'localFileSystemInotify');
135        }
136        // check 'statCorrector'
137        if (empty($this->options['statCorrector']) || !is_callable($this->options['statCorrector'])) {
138            $this->options['statCorrector'] = null;
139        }
140
141        return true;
142    }
143
144    /**
145     * Configure after successfull mount.
146     *
147     * @return void
148     * @throws elFinderAbortException
149     * @author Dmitry (dio) Levashov
150     */
151    protected function configure()
152    {
153        $hiddens = array();
154        $root = $this->stat($this->root);
155
156        // check thumbnails path
157        if (!empty($this->options['tmbPath'])) {
158            if (strpos($this->options['tmbPath'], DIRECTORY_SEPARATOR) === false) {
159                $hiddens['tmb'] = $this->options['tmbPath'];
160                $this->options['tmbPath'] = $this->_abspath($this->options['tmbPath']);
161            } else {
162                $this->options['tmbPath'] = $this->_normpath($this->options['tmbPath']);
163            }
164        }
165        // check temp path
166        if (!empty($this->options['tmpPath'])) {
167            if (strpos($this->options['tmpPath'], DIRECTORY_SEPARATOR) === false) {
168                $hiddens['temp'] = $this->options['tmpPath'];
169                $this->options['tmpPath'] = $this->_abspath($this->options['tmpPath']);
170            } else {
171                $this->options['tmpPath'] = $this->_normpath($this->options['tmpPath']);
172            }
173        }
174        // check quarantine path
175        if (!empty($this->options['quarantine'])) {
176            if (strpos($this->options['quarantine'], DIRECTORY_SEPARATOR) === false) {
177                $hiddens['quarantine'] = $this->options['quarantine'];
178                $this->options['quarantine'] = $this->_abspath($this->options['quarantine']);
179            } else {
180                $this->options['quarantine'] = $this->_normpath($this->options['quarantine']);
181            }
182        }
183
184        parent::configure();
185
186        // check tmbPath
187        if (!$this->tmbPath && isset($hiddens['tmb'])) {
188            unset($hiddens['tmb']);
189        }
190
191        // if no thumbnails url - try detect it
192        if ($root['read'] && !$this->tmbURL && $this->URL) {
193            if (strpos($this->tmbPath, $this->root) === 0) {
194                $this->tmbURL = $this->URL . str_replace(DIRECTORY_SEPARATOR, '/', substr($this->tmbPath, strlen($this->root) + 1));
195                if (preg_match("|[^/?&=]$|", $this->tmbURL)) {
196                    $this->tmbURL .= '/';
197                }
198            }
199        }
200
201        // set $this->tmp by options['tmpPath']
202        $this->tmp = '';
203        if (!empty($this->options['tmpPath'])) {
204            if ((is_dir($this->options['tmpPath']) || mkdir($this->options['tmpPath'], $this->options['dirMode'], true)) && is_writable($this->options['tmpPath'])) {
205                $this->tmp = $this->options['tmpPath'];
206            } else {
207                if (isset($hiddens['temp'])) {
208                    unset($hiddens['temp']);
209                }
210            }
211        }
212        if (!$this->tmp && ($tmp = elFinder::getStaticVar('commonTempPath'))) {
213            $this->tmp = $tmp;
214        }
215
216        // check quarantine dir
217        $this->quarantine = '';
218        if (!empty($this->options['quarantine'])) {
219            if ((is_dir($this->options['quarantine']) || mkdir($this->options['quarantine'], $this->options['dirMode'], true)) && is_writable($this->options['quarantine'])) {
220                $this->quarantine = $this->options['quarantine'];
221            } else {
222                if (isset($hiddens['quarantine'])) {
223                    unset($hiddens['quarantine']);
224                }
225            }
226        }
227
228        if (!$this->quarantine) {
229            if (!$this->tmp) {
230                $this->archivers['extract'] = array();
231                $this->disabled[] = 'extract';
232            } else {
233                $this->quarantine = $this->tmp;
234            }
235        }
236
237        if ($hiddens) {
238            foreach ($hiddens as $hidden) {
239                $this->attributes[] = array(
240                    'pattern' => '~^' . preg_quote(DIRECTORY_SEPARATOR . $hidden, '~') . '$~',
241                    'read' => false,
242                    'write' => false,
243                    'locked' => true,
244                    'hidden' => true
245                );
246            }
247        }
248
249        if (!empty($this->options['keepTimestamp'])) {
250            $this->options['keepTimestamp'] = array_flip($this->options['keepTimestamp']);
251        }
252
253        $this->statOwner = (!empty($this->options['statOwner']));
254    }
255
256    /**
257     * Long pooling sync checker
258     * This function require server command `inotifywait`
259     * If `inotifywait` need full path, Please add `define('ELFINER_INOTIFYWAIT_PATH', '/PATH_TO/inotifywait');` into connector.php
260     *
261     * @param string $path
262     * @param int    $standby
263     * @param number $compare
264     *
265     * @return number|bool
266     * @throws elFinderAbortException
267     */
268    public function localFileSystemInotify($path, $standby, $compare)
269    {
270        if (isset($this->sessionCache['localFileSystemInotify_disable'])) {
271            return false;
272        }
273        $path = realpath($path);
274        $mtime = filemtime($path);
275        if (!$mtime) {
276            return false;
277        }
278        if ($mtime != $compare) {
279            return $mtime;
280        }
281        $inotifywait = defined('ELFINER_INOTIFYWAIT_PATH') ? ELFINER_INOTIFYWAIT_PATH : 'inotifywait';
282        $standby = max(1, intval($standby));
283        $cmd = $inotifywait . ' ' . escapeshellarg($path) . ' -t ' . $standby . ' -e moved_to,moved_from,move,close_write,delete,delete_self';
284        $this->procExec($cmd, $o, $r);
285        if ($r === 0) {
286            // changed
287            clearstatcache();
288            if (file_exists($path)) {
289                $mtime = filemtime($path); // error on busy?
290                return $mtime ? $mtime : time();
291            } else {
292                // target was removed
293                return 0;
294            }
295        } else if ($r === 2) {
296            // not changed (timeout)
297            return $compare;
298        }
299        // error
300        // cache to $_SESSION
301        $this->sessionCache['localFileSystemInotify_disable'] = true;
302        $this->session->set($this->id, $this->sessionCache);
303        return false;
304    }
305
306    /*********************************************************************/
307    /*                               FS API                              */
308    /*********************************************************************/
309
310    /*********************** paths/urls *************************/
311
312    /**
313     * Return parent directory path
314     *
315     * @param  string $path file path
316     *
317     * @return string
318     * @author Dmitry (dio) Levashov
319     **/
320    protected function _dirname($path)
321    {
322        return dirname($path);
323    }
324
325    /**
326     * Return file name
327     *
328     * @param  string $path file path
329     *
330     * @return string
331     * @author Dmitry (dio) Levashov
332     **/
333    protected function _basename($path)
334    {
335        return basename($path);
336    }
337
338    /**
339     * Join dir name and file name and retur full path
340     *
341     * @param  string $dir
342     * @param  string $name
343     *
344     * @return string
345     * @author Dmitry (dio) Levashov
346     **/
347    protected function _joinPath($dir, $name)
348    {
349        return rtrim($dir, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR . $name;
350    }
351
352    /**
353     * Return normalized path, this works the same as os.path.normpath() in Python
354     *
355     * @param  string $path path
356     *
357     * @return string
358     * @author Troex Nevelin
359     **/
360    protected function _normpath($path)
361    {
362        if (empty($path)) {
363            return '.';
364        }
365
366        $changeSep = (DIRECTORY_SEPARATOR !== '/');
367        if ($changeSep) {
368            $drive = '';
369            if (preg_match('/^([a-zA-Z]:)(.*)/', $path, $m)) {
370                $drive = $m[1];
371                $path = $m[2] ? $m[2] : '/';
372            }
373            $path = str_replace(DIRECTORY_SEPARATOR, '/', $path);
374        }
375
376        if (strpos($path, '/') === 0) {
377            $initial_slashes = true;
378        } else {
379            $initial_slashes = false;
380        }
381
382        if (($initial_slashes)
383            && (strpos($path, '//') === 0)
384            && (strpos($path, '///') === false)) {
385            $initial_slashes = 2;
386        }
387
388        $initial_slashes = (int)$initial_slashes;
389
390        $comps = explode('/', $path);
391        $new_comps = array();
392        foreach ($comps as $comp) {
393            if (in_array($comp, array('', '.'))) {
394                continue;
395            }
396
397            if (($comp != '..')
398                || (!$initial_slashes && !$new_comps)
399                || ($new_comps && (end($new_comps) == '..'))) {
400                array_push($new_comps, $comp);
401            } elseif ($new_comps) {
402                array_pop($new_comps);
403            }
404        }
405        $comps = $new_comps;
406        $path = implode('/', $comps);
407        if ($initial_slashes) {
408            $path = str_repeat('/', $initial_slashes) . $path;
409        }
410
411        if ($changeSep) {
412            $path = $drive . str_replace('/', DIRECTORY_SEPARATOR, $path);
413        }
414
415        return $path ? $path : '.';
416    }
417
418    /**
419     * Return file path related to root dir
420     *
421     * @param  string $path file path
422     *
423     * @return string
424     * @author Dmitry (dio) Levashov
425     **/
426    protected function _relpath($path)
427    {
428        if ($path === $this->root) {
429            return '';
430        } else {
431            if (strpos($path, $this->root) === 0) {
432                return ltrim(substr($path, strlen($this->root)), DIRECTORY_SEPARATOR);
433            } else {
434                // for link
435                return $path;
436            }
437        }
438    }
439
440    /**
441     * Convert path related to root dir into real path
442     *
443     * @param  string $path file path
444     *
445     * @return string
446     * @author Dmitry (dio) Levashov
447     **/
448    protected function _abspath($path)
449    {
450        if ($path === DIRECTORY_SEPARATOR) {
451            return $this->root;
452        } else {
453            if (strpos($path, $this->systemRoot) === 0) {
454                return $path;
455            } else if (DIRECTORY_SEPARATOR !== '/' && preg_match('/^[a-zA-Z]:' . preg_quote(DIRECTORY_SEPARATOR, '/') . '/', $path)) {
456                return $path;
457            } else {
458                return $this->_joinPath($this->root, $path);
459            }
460        }
461    }
462
463    /**
464     * Return fake path started from root dir
465     *
466     * @param  string $path file path
467     *
468     * @return string
469     * @author Dmitry (dio) Levashov
470     **/
471    protected function _path($path)
472    {
473        return $this->rootName . ($path == $this->root ? '' : $this->separator . $this->_relpath($path));
474    }
475
476    /**
477     * Return true if $path is children of $parent
478     *
479     * @param  string $path   path to check
480     * @param  string $parent parent path
481     *
482     * @return bool
483     * @author Dmitry (dio) Levashov
484     **/
485    protected function _inpath($path, $parent)
486    {
487        $cwd = getcwd();
488        $real_path = $this->getFullPath($path, $cwd);
489        $real_parent = $this->getFullPath($parent, $cwd);
490        if ($real_path && $real_parent) {
491            return $real_path === $real_parent || strpos($real_path, rtrim($real_parent, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR) === 0;
492        }
493        return false;
494    }
495
496
497
498    /***************** file stat ********************/
499
500    /**
501     * Return stat for given path.
502     * Stat contains following fields:
503     * - (int)    size    file size in b. required
504     * - (int)    ts      file modification time in unix time. required
505     * - (string) mime    mimetype. required for folders, others - optionally
506     * - (bool)   read    read permissions. required
507     * - (bool)   write   write permissions. required
508     * - (bool)   locked  is object locked. optionally
509     * - (bool)   hidden  is object hidden. optionally
510     * - (string) alias   for symlinks - link target path relative to root path. optionally
511     * - (string) target  for symlinks - link target path. optionally
512     * If file does not exists - returns empty array or false.
513     *
514     * @param  string $path file path
515     *
516     * @return array|false
517     * @author Dmitry (dio) Levashov
518     **/
519    protected function _stat($path)
520    {
521        $stat = array();
522
523        if (!file_exists($path) && !is_link($path)) {
524            return $stat;
525        }
526
527        //Verifies the given path is the root or is inside the root. Prevents directory traveral.
528        if (!$this->_inpath($path, $this->root)) {
529            return $stat;
530        }
531
532        $stat['isowner'] = false;
533        $linkreadable = false;
534        if ($path != $this->root && is_link($path)) {
535            if (!$this->options['followSymLinks']) {
536                return array();
537            }
538            if (!($target = $this->readlink($path))
539                || $target == $path) {
540                if (is_null($target)) {
541                    $stat = array();
542                    return $stat;
543                } else {
544                    $stat['mime'] = 'symlink-broken';
545                    $target = readlink($path);
546                    $lstat = lstat($path);
547                    $ostat = $this->getOwnerStat($lstat['uid'], $lstat['gid']);
548                    $linkreadable = !empty($ostat['isowner']);
549                }
550            }
551            $stat['alias'] = $this->_path($target);
552            $stat['target'] = $target;
553        }
554
555        $readable = is_readable($path);
556
557        if ($readable) {
558            $size = sprintf('%u', filesize($path));
559            $stat['ts'] = filemtime($path);
560            if ($this->statOwner) {
561                $fstat = stat($path);
562                $uid = $fstat['uid'];
563                $gid = $fstat['gid'];
564                $stat['perm'] = substr((string)decoct($fstat['mode']), -4);
565                $stat = array_merge($stat, $this->getOwnerStat($uid, $gid));
566            }
567        }
568
569        if (($dir = is_dir($path)) && $this->options['detectDirIcon']) {
570            $favicon = $path . DIRECTORY_SEPARATOR . $this->options['detectDirIcon'];
571            if ($this->URL && file_exists($favicon)) {
572                $stat['icon'] = $this->URL . str_replace(DIRECTORY_SEPARATOR, '/', substr($favicon, strlen($this->root) + 1));
573            }
574        }
575
576        if (!isset($stat['mime'])) {
577            $stat['mime'] = $dir ? 'directory' : $this->mimetype($path);
578        }
579        //logical rights first
580        $stat['read'] = ($linkreadable || $readable) ? null : false;
581        $stat['write'] = is_writable($path) ? null : false;
582
583        if (is_null($stat['read'])) {
584            if ($dir) {
585                $stat['size'] = 0;
586            } else if (isset($size)) {
587                $stat['size'] = $size;
588            }
589        }
590
591        if ($this->options['statCorrector']) {
592            call_user_func_array($this->options['statCorrector'], array(&$stat, $path, $this->statOwner, $this));
593        }
594
595        return $stat;
596    }
597
598    /**
599     * Get stat `owner`, `group` and `isowner` by `uid` and `gid`
600     * Sub-fuction of _stat() and _scandir()
601     *
602     * @param integer $uid
603     * @param integer $gid
604     *
605     * @return array  stat
606     */
607    protected function getOwnerStat($uid, $gid)
608    {
609        static $names = null;
610        static $phpuid = null;
611
612        if (is_null($names)) {
613            $names = array('uid' => array(), 'gid' => array());
614        }
615        if (is_null($phpuid)) {
616            if (is_callable('posix_getuid')) {
617                $phpuid = posix_getuid();
618            } else {
619                $phpuid = 0;
620            }
621        }
622
623        $stat = array();
624
625        if ($uid) {
626            $stat['isowner'] = ($phpuid == $uid);
627            if (isset($names['uid'][$uid])) {
628                $stat['owner'] = $names['uid'][$uid];
629            } else if (is_callable('posix_getpwuid')) {
630                $pwuid = posix_getpwuid($uid);
631                $stat['owner'] = $names['uid'][$uid] = $pwuid['name'];
632            } else {
633                $stat['owner'] = $names['uid'][$uid] = $uid;
634            }
635        }
636        if ($gid) {
637            if (isset($names['gid'][$gid])) {
638                $stat['group'] = $names['gid'][$gid];
639            } else if (is_callable('posix_getgrgid')) {
640                $grgid = posix_getgrgid($gid);
641                $stat['group'] = $names['gid'][$gid] = $grgid['name'];
642            } else {
643                $stat['group'] = $names['gid'][$gid] = $gid;
644            }
645        }
646
647        return $stat;
648    }
649
650    /**
651     * Return true if path is dir and has at least one childs directory
652     *
653     * @param  string $path dir path
654     *
655     * @return bool
656     * @author Dmitry (dio) Levashov
657     **/
658    protected function _subdirs($path)
659    {
660
661        $dirs = false;
662        if (is_dir($path) && is_readable($path)) {
663            if (class_exists('FilesystemIterator', false)) {
664                $dirItr = new ParentIterator(
665                    new RecursiveDirectoryIterator($path,
666                        FilesystemIterator::SKIP_DOTS |
667                        FilesystemIterator::CURRENT_AS_SELF |
668                        (defined('RecursiveDirectoryIterator::FOLLOW_SYMLINKS') ?
669                            RecursiveDirectoryIterator::FOLLOW_SYMLINKS : 0)
670                    )
671                );
672                $dirItr->rewind();
673                if ($dirItr->hasChildren()) {
674                    $dirs = true;
675                    $name = $dirItr->getSubPathName();
676                    while ($dirItr->valid()) {
677                        if (!$this->attr($path . DIRECTORY_SEPARATOR . $name, 'read', null, true)) {
678                            $dirs = false;
679                            $dirItr->next();
680                            $name = $dirItr->getSubPathName();
681                            continue;
682                        }
683                        $dirs = true;
684                        break;
685                    }
686                }
687            } else {
688                $path = strtr($path, array('[' => '\\[', ']' => '\\]', '*' => '\\*', '?' => '\\?'));
689                return (bool)glob(rtrim($path, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR . '*', GLOB_ONLYDIR);
690            }
691        }
692        return $dirs;
693    }
694
695    /**
696     * Return object width and height
697     * Usualy used for images, but can be realize for video etc...
698     *
699     * @param  string $path file path
700     * @param  string $mime file mime type
701     *
702     * @return string
703     * @author Dmitry (dio) Levashov
704     **/
705    protected function _dimensions($path, $mime)
706    {
707        clearstatcache();
708        return strpos($mime, 'image') === 0 && is_readable($path) && filesize($path) && ($s = getimagesize($path)) !== false
709            ? $s[0] . 'x' . $s[1]
710            : false;
711    }
712    /******************** file/dir content *********************/
713
714    /**
715     * Return symlink target file
716     *
717     * @param  string $path link path
718     *
719     * @return string
720     * @author Dmitry (dio) Levashov
721     **/
722    protected function readlink($path)
723    {
724        if (!($target = readlink($path))) {
725            return null;
726        }
727
728        if (strpos($target, $this->systemRoot) !== 0) {
729            $target = $this->_joinPath(dirname($path), $target);
730        }
731
732        if (!file_exists($target)) {
733            return false;
734        }
735
736        return $target;
737    }
738
739    /**
740     * Return files list in directory.
741     *
742     * @param  string $path dir path
743     *
744     * @return array
745     * @throws elFinderAbortException
746     * @author Dmitry (dio) Levashov
747     */
748    protected function _scandir($path)
749    {
750        elFinder::checkAborted();
751        $files = array();
752        $cache = array();
753        $dirWritable = is_writable($path);
754        $dirItr = array();
755        $followSymLinks = $this->options['followSymLinks'];
756        try {
757            $dirItr = new DirectoryIterator($path);
758        } catch (UnexpectedValueException $e) {
759        }
760
761        foreach ($dirItr as $file) {
762            try {
763                if ($file->isDot()) {
764                    continue;
765                }
766
767                $files[] = $fpath = $file->getPathname();
768
769                $br = false;
770                $stat = array();
771
772                $stat['isowner'] = false;
773                $linkreadable = false;
774                if ($file->isLink()) {
775                    if (!$followSymLinks) {
776                        continue;
777                    }
778                    if (!($target = $this->readlink($fpath))
779                        || $target == $fpath) {
780                        if (is_null($target)) {
781                            $stat = array();
782                            $br = true;
783                        } else {
784                            $_path = $fpath;
785                            $stat['mime'] = 'symlink-broken';
786                            $target = readlink($_path);
787                            $lstat = lstat($_path);
788                            $ostat = $this->getOwnerStat($lstat['uid'], $lstat['gid']);
789                            $linkreadable = !empty($ostat['isowner']);
790                            $dir = false;
791                            $stat['alias'] = $this->_path($target);
792                            $stat['target'] = $target;
793                        }
794                    } else {
795                        $dir = is_dir($target);
796                        $stat['alias'] = $this->_path($target);
797                        $stat['target'] = $target;
798                        $stat['mime'] = $dir ? 'directory' : $this->mimetype($stat['alias']);
799                    }
800                } else {
801                    if (($dir = $file->isDir()) && $this->options['detectDirIcon']) {
802                        $path = $file->getPathname();
803                        $favicon = $path . DIRECTORY_SEPARATOR . $this->options['detectDirIcon'];
804                        if ($this->URL && file_exists($favicon)) {
805                            $stat['icon'] = $this->URL . str_replace(DIRECTORY_SEPARATOR, '/', substr($favicon, strlen($this->root) + 1));
806                        }
807                    }
808                    $stat['mime'] = $dir ? 'directory' : $this->mimetype($fpath);
809                }
810                $size = sprintf('%u', $file->getSize());
811                $stat['ts'] = $file->getMTime();
812                if (!$br) {
813                    if ($this->statOwner && !$linkreadable) {
814                        $uid = $file->getOwner();
815                        $gid = $file->getGroup();
816                        $stat['perm'] = substr((string)decoct($file->getPerms()), -4);
817                        $stat = array_merge($stat, $this->getOwnerStat($uid, $gid));
818                    }
819
820                    //logical rights first
821                    $stat['read'] = ($linkreadable || $file->isReadable()) ? null : false;
822                    $stat['write'] = $file->isWritable() ? null : false;
823                    $stat['locked'] = $dirWritable ? null : true;
824
825                    if (is_null($stat['read'])) {
826                        $stat['size'] = $dir ? 0 : $size;
827                    }
828
829                    if ($this->options['statCorrector']) {
830                        call_user_func_array($this->options['statCorrector'], array(&$stat, $fpath, $this->statOwner, $this));
831                    }
832                }
833
834                $cache[] = array($fpath, $stat);
835            } catch (RuntimeException $e) {
836                continue;
837            }
838        }
839
840        if ($cache) {
841            $cache = $this->convEncOut($cache, false);
842            foreach ($cache as $d) {
843                $this->updateCache($d[0], $d[1]);
844            }
845        }
846
847        return $files;
848    }
849
850    /**
851     * Open file and return file pointer
852     *
853     * @param  string $path file path
854     * @param string  $mode
855     *
856     * @return false|resource
857     * @internal param bool $write open file for writing
858     * @author   Dmitry (dio) Levashov
859     */
860    protected function _fopen($path, $mode = 'rb')
861    {
862        return fopen($path, $mode);
863    }
864
865    /**
866     * Close opened file
867     *
868     * @param  resource $fp file pointer
869     * @param string    $path
870     *
871     * @return bool
872     * @author Dmitry (dio) Levashov
873     */
874    protected function _fclose($fp, $path = '')
875    {
876        return (is_resource($fp) && fclose($fp));
877    }
878
879    /********************  file/dir manipulations *************************/
880
881    /**
882     * Create dir and return created dir path or false on failed
883     *
884     * @param  string $path parent dir path
885     * @param string  $name new directory name
886     *
887     * @return string|bool
888     * @author Dmitry (dio) Levashov
889     **/
890    protected function _mkdir($path, $name)
891    {
892        $path = $this->_joinPath($path, $name);
893
894        if (mkdir($path)) {
895            chmod($path, $this->options['dirMode']);
896            return $path;
897        }
898
899        return false;
900    }
901
902    /**
903     * Create file and return it's path or false on failed
904     *
905     * @param  string $path parent dir path
906     * @param string  $name new file name
907     *
908     * @return string|bool
909     * @author Dmitry (dio) Levashov
910     **/
911    protected function _mkfile($path, $name)
912    {
913        $path = $this->_joinPath($path, $name);
914
915        if (($fp = fopen($path, 'w'))) {
916            fclose($fp);
917            chmod($path, $this->options['fileMode']);
918            return $path;
919        }
920        return false;
921    }
922
923    /**
924     * Create symlink
925     *
926     * @param  string $source    file to link to
927     * @param  string $targetDir folder to create link in
928     * @param  string $name      symlink name
929     *
930     * @return bool
931     * @author Dmitry (dio) Levashov
932     **/
933    protected function _symlink($source, $targetDir, $name)
934    {
935        return symlink($source, $this->_joinPath($targetDir, $name));
936    }
937
938    /**
939     * Copy file into another file
940     *
941     * @param  string $source    source file path
942     * @param  string $targetDir target directory path
943     * @param  string $name      new file name
944     *
945     * @return bool
946     * @author Dmitry (dio) Levashov
947     **/
948    protected function _copy($source, $targetDir, $name)
949    {
950        $mtime = filemtime($source);
951        $target = $this->_joinPath($targetDir, $name);
952        if ($ret = copy($source, $target)) {
953            isset($this->options['keepTimestamp']['copy']) && $mtime && touch($target, $mtime);
954        }
955        return $ret;
956    }
957
958    /**
959     * Move file into another parent dir.
960     * Return new file path or false.
961     *
962     * @param  string $source source file path
963     * @param         $targetDir
964     * @param  string $name   file name
965     *
966     * @return bool|string
967     * @internal param string $target target dir path
968     * @author   Dmitry (dio) Levashov
969     */
970    protected function _move($source, $targetDir, $name)
971    {
972        $mtime = filemtime($source);
973        $target = $this->_joinPath($targetDir, $name);
974        if ($ret = rename($source, $target) ? $target : false) {
975            isset($this->options['keepTimestamp']['move']) && $mtime && touch($target, $mtime);
976        }
977        return $ret;
978    }
979
980    /**
981     * Remove file
982     *
983     * @param  string $path file path
984     *
985     * @return bool
986     * @author Dmitry (dio) Levashov
987     **/
988    protected function _unlink($path)
989    {
990        return is_file($path) && unlink($path);
991    }
992
993    /**
994     * Remove dir
995     *
996     * @param  string $path dir path
997     *
998     * @return bool
999     * @author Dmitry (dio) Levashov
1000     **/
1001    protected function _rmdir($path)
1002    {
1003        return rmdir($path);
1004    }
1005
1006    /**
1007     * Create new file and write into it from file pointer.
1008     * Return new file path or false on error.
1009     *
1010     * @param  resource $fp   file pointer
1011     * @param  string   $dir  target dir path
1012     * @param  string   $name file name
1013     * @param  array    $stat file stat (required by some virtual fs)
1014     *
1015     * @return bool|string
1016     * @author Dmitry (dio) Levashov
1017     **/
1018    protected function _save($fp, $dir, $name, $stat)
1019    {
1020        $path = $this->_joinPath($dir, $name);
1021
1022        $meta = stream_get_meta_data($fp);
1023        $uri = isset($meta['uri']) ? $meta['uri'] : '';
1024        if ($uri && !preg_match('#^[a-zA-Z0-9]+://#', $uri) && !is_link($uri)) {
1025            fclose($fp);
1026            $mtime = filemtime($uri);
1027            $isCmdPaste = ($this->ARGS['cmd'] === 'paste');
1028            $isCmdCopy = ($isCmdPaste && empty($this->ARGS['cut']));
1029            if (($isCmdCopy || !rename($uri, $path)) && !copy($uri, $path)) {
1030                return false;
1031            }
1032            // keep timestamp on upload
1033            if ($mtime && $this->ARGS['cmd'] === 'upload') {
1034                touch($path, isset($this->options['keepTimestamp']['upload']) ? $mtime : time());
1035            }
1036        } else {
1037            if (file_put_contents($path, $fp, LOCK_EX) === false) {
1038                return false;
1039            }
1040        }
1041
1042        chmod($path, $this->options['fileMode']);
1043        return $path;
1044    }
1045
1046    /**
1047     * Get file contents
1048     *
1049     * @param  string $path file path
1050     *
1051     * @return string|false
1052     * @author Dmitry (dio) Levashov
1053     **/
1054    protected function _getContents($path)
1055    {
1056        return file_get_contents($path);
1057    }
1058
1059    /**
1060     * Write a string to a file
1061     *
1062     * @param  string $path    file path
1063     * @param  string $content new file content
1064     *
1065     * @return bool
1066     * @author Dmitry (dio) Levashov
1067     **/
1068    protected function _filePutContents($path, $content)
1069    {
1070        return (file_put_contents($path, $content, LOCK_EX) !== false);
1071    }
1072
1073    /**
1074     * Detect available archivers
1075     *
1076     * @return void
1077     * @throws elFinderAbortException
1078     */
1079    protected function _checkArchivers()
1080    {
1081        $this->archivers = $this->getArchivers();
1082        return;
1083    }
1084
1085    /**
1086     * chmod availability
1087     *
1088     * @param string $path
1089     * @param string $mode
1090     *
1091     * @return bool
1092     */
1093    protected function _chmod($path, $mode)
1094    {
1095        $modeOct = is_string($mode) ? octdec($mode) : octdec(sprintf("%04o", $mode));
1096        return chmod($path, $modeOct);
1097    }
1098
1099    /**
1100     * Recursive symlinks search
1101     *
1102     * @param  string $path file/dir path
1103     *
1104     * @return bool
1105     * @throws Exception
1106     * @author Dmitry (dio) Levashov
1107     */
1108    protected function _findSymlinks($path)
1109    {
1110        return self::localFindSymlinks($path);
1111    }
1112
1113    /**
1114     * Extract files from archive
1115     *
1116     * @param  string $path archive path
1117     * @param  array  $arc  archiver command and arguments (same as in $this->archivers)
1118     *
1119     * @return array|string|boolean
1120     * @throws elFinderAbortException
1121     * @author Dmitry (dio) Levashov,
1122     * @author Alexey Sukhotin
1123     */
1124    protected function _extract($path, $arc)
1125    {
1126
1127        if ($this->quarantine) {
1128
1129            $dir = $this->quarantine . DIRECTORY_SEPARATOR . md5(basename($path) . mt_rand());
1130            $archive = (isset($arc['toSpec']) || $arc['cmd'] === 'phpfunction') ? '' : $dir . DIRECTORY_SEPARATOR . basename($path);
1131
1132            if (!mkdir($dir)) {
1133                return false;
1134            }
1135
1136            // insurance unexpected shutdown
1137            register_shutdown_function(array($this, 'rmdirRecursive'), realpath($dir));
1138
1139            chmod($dir, 0777);
1140
1141            // copy in quarantine
1142            if (!is_readable($path) || ($archive && !copy($path, $archive))) {
1143                return false;
1144            }
1145
1146            // extract in quarantine
1147            try {
1148                $this->unpackArchive($path, $arc, $archive ? true : $dir);
1149            } catch(Exception $e) {
1150                return $this->setError($e->getMessage());
1151            }
1152
1153            // get files list
1154            try {
1155                $ls = self::localScandir($dir);
1156            } catch (Exception $e) {
1157                return false;
1158            }
1159
1160            // no files - extract error ?
1161            if (empty($ls)) {
1162                return false;
1163            }
1164
1165            $this->archiveSize = 0;
1166
1167            // find symlinks and check extracted items
1168            $checkRes = $this->checkExtractItems($dir);
1169            if ($checkRes['symlinks']) {
1170                self::localRmdirRecursive($dir);
1171                return $this->setError(array_merge($this->error, array(elFinder::ERROR_ARC_SYMLINKS)));
1172            }
1173            $this->archiveSize = $checkRes['totalSize'];
1174            if ($checkRes['rmNames']) {
1175                foreach ($checkRes['rmNames'] as $name) {
1176                    $this->addError(elFinder::ERROR_SAVE, $name);
1177                }
1178            }
1179
1180            // check max files size
1181            if ($this->options['maxArcFilesSize'] > 0 && $this->options['maxArcFilesSize'] < $this->archiveSize) {
1182                $this->delTree($dir);
1183                return $this->setError(elFinder::ERROR_ARC_MAXSIZE);
1184            }
1185
1186            $extractTo = $this->extractToNewdir; // 'auto', ture or false
1187
1188            // archive contains one item - extract in archive dir
1189            $name = '';
1190            $src = $dir . DIRECTORY_SEPARATOR . $ls[0];
1191            if (($extractTo === 'auto' || !$extractTo) && count($ls) === 1 && is_file($src)) {
1192                $name = $ls[0];
1193            } else if ($extractTo === 'auto' || $extractTo) {
1194                // for several files - create new directory
1195                // create unique name for directory
1196                $src = $dir;
1197                $splits = elFinder::splitFileExtention(basename($path));
1198                $name = $splits[0];
1199                $test = dirname($path) . DIRECTORY_SEPARATOR . $name;
1200                if (file_exists($test) || is_link($test)) {
1201                    $name = $this->uniqueName(dirname($path), $name, '-', false);
1202                }
1203            }
1204
1205            if ($name !== '') {
1206                $result = dirname($path) . DIRECTORY_SEPARATOR . $name;
1207
1208                if (!rename($src, $result)) {
1209                    $this->delTree($dir);
1210                    return false;
1211                }
1212            } else {
1213                $dstDir = dirname($path);
1214                $result = array();
1215                foreach ($ls as $name) {
1216                    $target = $dstDir . DIRECTORY_SEPARATOR . $name;
1217                    if (self::localMoveRecursive($dir . DIRECTORY_SEPARATOR . $name, $target, true, $this->options['copyJoin'])) {
1218                        $result[] = $target;
1219                    }
1220                }
1221                if (!$result) {
1222                    $this->delTree($dir);
1223                    return false;
1224                }
1225            }
1226
1227            is_dir($dir) && $this->delTree($dir);
1228
1229            return (is_array($result) || file_exists($result)) ? $result : false;
1230        }
1231        //TODO: Add return statement here
1232        return false;
1233    }
1234
1235    /**
1236     * Create archive and return its path
1237     *
1238     * @param  string $dir   target dir
1239     * @param  array  $files files names list
1240     * @param  string $name  archive name
1241     * @param  array  $arc   archiver options
1242     *
1243     * @return string|bool
1244     * @throws elFinderAbortException
1245     * @author Dmitry (dio) Levashov,
1246     * @author Alexey Sukhotin
1247     */
1248    protected function _archive($dir, $files, $name, $arc)
1249    {
1250        return $this->makeArchive($dir, $files, $name, $arc);
1251    }
1252
1253    /******************** Over write functions *************************/
1254
1255    /**
1256     * File path of local server side work file path
1257     *
1258     * @param  string $path
1259     *
1260     * @return string
1261     * @author Naoki Sawada
1262     */
1263    protected function getWorkFile($path)
1264    {
1265        return $path;
1266    }
1267
1268    /**
1269     * Delete dirctory trees
1270     *
1271     * @param string $localpath path need convert encoding to server encoding
1272     *
1273     * @return boolean
1274     * @throws elFinderAbortException
1275     * @author Naoki Sawada
1276     */
1277    protected function delTree($localpath)
1278    {
1279        return $this->rmdirRecursive($localpath);
1280    }
1281
1282    /**
1283     * Return fileinfo based on filename
1284     * For item ID based path file system
1285     * Please override if needed on each drivers
1286     *
1287     * @param  string $path file cache
1288     *
1289     * @return array|boolean false
1290     */
1291    protected function isNameExists($path)
1292    {
1293        $exists = file_exists($this->convEncIn($path));
1294        // restore locale
1295        $this->convEncOut();
1296        return $exists ? $this->stat($path) : false;
1297    }
1298
1299    /******************** Over write (Optimized) functions *************************/
1300
1301    /**
1302     * Recursive files search
1303     *
1304     * @param  string $path dir path
1305     * @param  string $q    search string
1306     * @param  array  $mimes
1307     *
1308     * @return array
1309     * @throws elFinderAbortException
1310     * @author Dmitry (dio) Levashov
1311     * @author Naoki Sawada
1312     */
1313    protected function doSearch($path, $q, $mimes)
1314    {
1315        if (!empty($this->doSearchCurrentQuery['matchMethod']) || $this->encoding || !class_exists('FilesystemIterator', false)) {
1316            // has custom match method or non UTF-8, use elFinderVolumeDriver::doSearch()
1317            return parent::doSearch($path, $q, $mimes);
1318        }
1319
1320        $result = array();
1321
1322        $timeout = $this->options['searchTimeout'] ? $this->searchStart + $this->options['searchTimeout'] : 0;
1323        if ($timeout && $timeout < time()) {
1324            $this->setError(elFinder::ERROR_SEARCH_TIMEOUT, $this->path($this->encode($path)));
1325            return $result;
1326        }
1327        elFinder::extendTimeLimit($this->options['searchTimeout'] + 30);
1328
1329        $match = array();
1330        try {
1331            $iterator = new RecursiveIteratorIterator(
1332                new RecursiveCallbackFilterIterator(
1333                    new RecursiveDirectoryIterator($path,
1334                        FilesystemIterator::KEY_AS_PATHNAME |
1335                        FilesystemIterator::SKIP_DOTS |
1336                        ((defined('RecursiveDirectoryIterator::FOLLOW_SYMLINKS') && $this->options['followSymLinks']) ?
1337                            RecursiveDirectoryIterator::FOLLOW_SYMLINKS : 0)
1338                    ),
1339                    array($this, 'localFileSystemSearchIteratorFilter')
1340                ),
1341                RecursiveIteratorIterator::SELF_FIRST,
1342                RecursiveIteratorIterator::CATCH_GET_CHILD
1343            );
1344            foreach ($iterator as $key => $node) {
1345                if ($timeout && ($this->error || $timeout < time())) {
1346                    !$this->error && $this->setError(elFinder::ERROR_SEARCH_TIMEOUT, $this->path($this->encode($node->getPath)));
1347                    break;
1348                }
1349                if ($node->isDir()) {
1350                    if ($this->stripos($node->getFilename(), $q) !== false) {
1351                        $match[] = $key;
1352                    }
1353                } else {
1354                    $match[] = $key;
1355                }
1356            }
1357        } catch (Exception $e) {
1358        }
1359
1360        if ($match) {
1361            foreach ($match as $p) {
1362                if ($timeout && ($this->error || $timeout < time())) {
1363                    !$this->error && $this->setError(elFinder::ERROR_SEARCH_TIMEOUT, $this->path($this->encode(dirname($p))));
1364                    break;
1365                }
1366
1367                $stat = $this->stat($p);
1368
1369                if (!$stat) { // invalid links
1370                    continue;
1371                }
1372
1373                if (!empty($stat['hidden']) || !$this->mimeAccepted($stat['mime'], $mimes)) {
1374                    continue;
1375                }
1376
1377                if ((!$mimes || $stat['mime'] !== 'directory')) {
1378                    $stat['path'] = $this->path($stat['hash']);
1379                    if ($this->URL && !isset($stat['url'])) {
1380                        $_path = str_replace(DIRECTORY_SEPARATOR, '/', substr($p, strlen($this->root) + 1));
1381                        $stat['url'] = $this->URL . str_replace('%2F', '/', rawurlencode($_path));
1382                    }
1383
1384                    $result[] = $stat;
1385                }
1386            }
1387        }
1388
1389        return $result;
1390    }
1391
1392    /******************** Original local functions ************************
1393     *
1394     * @param $file
1395     * @param $key
1396     * @param $iterator
1397     *
1398     * @return bool
1399     */
1400
1401    public function localFileSystemSearchIteratorFilter($file, $key, $iterator)
1402    {
1403        /* @var FilesystemIterator $file */
1404        /* @var RecursiveDirectoryIterator $iterator */
1405        $name = $file->getFilename();
1406        if ($this->doSearchCurrentQuery['excludes']) {
1407            foreach ($this->doSearchCurrentQuery['excludes'] as $exclude) {
1408                if ($this->stripos($name, $exclude) !== false) {
1409                    return false;
1410                }
1411            }
1412        }
1413        if ($iterator->hasChildren()) {
1414            if ($this->options['searchExDirReg'] && preg_match($this->options['searchExDirReg'], $key)) {
1415                return false;
1416            }
1417            return (bool)$this->attr($key, 'read', null, true);
1418        }
1419        return ($this->stripos($name, $this->doSearchCurrentQuery['q']) === false) ? false : true;
1420    }
1421
1422    /**
1423     * Creates a symbolic link
1424     *
1425     * @param      string   $target  The target
1426     * @param      string   $link    The link
1427     *
1428     * @return     boolean  ( result of symlink() )
1429     */
1430    protected function localFileSystemSymlink($target, $link)
1431    {
1432        $res = false;
1433        $errlev = error_reporting();
1434        error_reporting($errlev ^ E_WARNING);
1435        if ($res = symlink(realpath($target), $link)) {
1436            $res = is_readable($link);
1437        }
1438        error_reporting($errlev);
1439        return $res;
1440    }
1441} // END class
1442
1443