4 * elFinder driver for local filesystem.
6 * @author Dmitry (dio) Levashov
7 * @author Troex Nevelin
9 class elFinderVolumeLocalFileSystem
extends elFinderVolumeDriver
{
13 * Must be started from letter and contains [a-z0-9]
14 * Used as part of volume id
18 protected $driverId = 'l';
21 * Required to count total archive files size
25 protected $archiveSize = 0;
29 * Extend options with required fields
32 * @author Dmitry (dio) Levashov
34 public function __construct() {
35 $this->options
['alias'] = ''; // alias to replace root dir name
36 $this->options
['dirMode'] = 0755; // new dirs mode
37 $this->options
['fileMode'] = 0644; // new files mode
38 $this->options
['quarantine'] = '.quarantine'; // quarantine folder name - required to check archive (must be hidden)
39 $this->options
['maxArcFilesSize'] = 0; // max allowed archive files size (0 - no limit)
42 /*********************************************************************/
43 /* INIT AND CONFIGURE */
44 /*********************************************************************/
47 * Configure after successfull mount.
50 * @author Dmitry (dio) Levashov
52 protected function configure() {
53 $this->aroot
= realpath($this->root
);
54 $root = $this->stat($this->root
);
56 if ($this->options
['quarantine']) {
57 $this->attributes
[] = array(
58 'pattern' => '~^'.preg_quote(DIRECTORY_SEPARATOR
.$this->options
['quarantine']).'$~',
66 // chek thumbnails path
67 if ($this->options
['tmbPath']) {
68 $this->options
['tmbPath'] = strpos($this->options
['tmbPath'], DIRECTORY_SEPARATOR
) === false
69 // tmb path set as dirname under root dir
70 ?
$this->root
.DIRECTORY_SEPARATOR
.$this->options
['tmbPath']
71 // tmb path as full path
72 : $this->_normpath($this->options
['tmbPath']);
77 // if no thumbnails url - try detect it
78 if ($root['read'] && !$this->tmbURL
&& $this->URL
) {
79 if (strpos($this->tmbPath
, $this->root
) === 0) {
80 $this->tmbURL
= $this->URL
.str_replace(DIRECTORY_SEPARATOR
, '/', substr($this->tmbPath
, strlen($this->root
)+
1));
81 if (preg_match("|[^/?&=]$|", $this->tmbURL
)) {
87 // check quarantine dir
88 if (!empty($this->options
['quarantine'])) {
89 $this->quarantine
= $this->root
.DIRECTORY_SEPARATOR
.$this->options
['quarantine'];
90 if ((!is_dir($this->quarantine
) && !$this->_mkdir($this->root
, $this->options
['quarantine'])) ||
!is_writable($this->quarantine
)) {
91 $this->archivers
['extract'] = array();
92 $this->disabled
[] = 'extract';
95 $this->archivers
['extract'] = array();
96 $this->disabled
[] = 'extract';
101 /*********************************************************************/
103 /*********************************************************************/
105 /*********************** paths/urls *************************/
108 * Return parent directory path
110 * @param string $path file path
112 * @author Dmitry (dio) Levashov
114 protected function _dirname($path) {
115 return dirname($path);
121 * @param string $path file path
123 * @author Dmitry (dio) Levashov
125 protected function _basename($path) {
126 return basename($path);
130 * Join dir name and file name and retur full path
133 * @param string $name
135 * @author Dmitry (dio) Levashov
137 protected function _joinPath($dir, $name) {
138 return $dir.DIRECTORY_SEPARATOR
.$name;
142 * Return normalized path, this works the same as os.path.normpath() in Python
144 * @param string $path path
146 * @author Troex Nevelin
148 protected function _normpath($path) {
153 if (strpos($path, '/') === 0) {
154 $initial_slashes = true;
156 $initial_slashes = false;
159 if (($initial_slashes)
160 && (strpos($path, '//') === 0)
161 && (strpos($path, '///') === false)) {
162 $initial_slashes = 2;
165 $initial_slashes = (int) $initial_slashes;
167 $comps = explode('/', $path);
168 $new_comps = array();
169 foreach ($comps as $comp) {
170 if (in_array($comp, array('', '.'))) {
175 ||
(!$initial_slashes && !$new_comps)
176 ||
($new_comps && (end($new_comps) == '..'))) {
177 array_push($new_comps, $comp);
178 } elseif ($new_comps) {
179 array_pop($new_comps);
183 $path = implode('/', $comps);
184 if ($initial_slashes) {
185 $path = str_repeat('/', $initial_slashes) . $path;
188 return $path ?
$path : '.';
192 * Return file path related to root dir
194 * @param string $path file path
196 * @author Dmitry (dio) Levashov
198 protected function _relpath($path) {
199 return $path == $this->root ?
'' : substr($path, strlen($this->root
)+
1);
203 * Convert path related to root dir into real path
205 * @param string $path file path
207 * @author Dmitry (dio) Levashov
209 protected function _abspath($path) {
210 return $path == DIRECTORY_SEPARATOR ?
$this->root
: $this->root
.DIRECTORY_SEPARATOR
.$path;
214 * Return fake path started from root dir
216 * @param string $path file path
218 * @author Dmitry (dio) Levashov
220 protected function _path($path) {
221 return $this->rootName
.($path == $this->root ?
'' : $this->separator
.$this->_relpath($path));
225 * Return true if $path is children of $parent
227 * @param string $path path to check
228 * @param string $parent parent path
230 * @author Dmitry (dio) Levashov
232 protected function _inpath($path, $parent) {
233 return $path == $parent ||
strpos($path, $parent.DIRECTORY_SEPARATOR
) === 0;
238 /***************** file stat ********************/
241 * Return stat for given path.
242 * Stat contains following fields:
243 * - (int) size file size in b. required
244 * - (int) ts file modification time in unix time. required
245 * - (string) mime mimetype. required for folders, others - optionally
246 * - (bool) read read permissions. required
247 * - (bool) write write permissions. required
248 * - (bool) locked is object locked. optionally
249 * - (bool) hidden is object hidden. optionally
250 * - (string) alias for symlinks - link target path relative to root path. optionally
251 * - (string) target for symlinks - link target path. optionally
253 * If file does not exists - returns empty array or false.
255 * @param string $path file path
256 * @return array|false
257 * @author Dmitry (dio) Levashov
259 protected function _stat($path) {
262 if (!file_exists($path)) {
266 if ($path != $this->root
&& is_link($path)) {
267 if (($target = $this->readlink($path)) == false
268 ||
$target == $path) {
269 $stat['mime'] = 'symlink-broken';
270 $stat['read'] = false;
271 $stat['write'] = false;
275 $stat['alias'] = $this->_path($target);
276 $stat['target'] = $target;
278 $lstat = lstat($path);
279 $size = $lstat['size'];
281 $size = @filesize
($path);
284 $dir = is_dir($path);
286 $stat['mime'] = $dir ?
'directory' : $this->mimetype($path);
287 $stat['ts'] = filemtime($path);
288 $stat['read'] = is_readable($path);
289 $stat['write'] = is_writable($path);
291 $stat['size'] = $dir ?
0 : $size;
299 * Return true if path is dir and has at least one childs directory
301 * @param string $path dir path
303 * @author Dmitry (dio) Levashov
305 protected function _subdirs($path) {
307 if (($dir = dir($path))) {
309 while (($entry = $dir->read()) !== false) {
310 $p = $dir->path
.DIRECTORY_SEPARATOR
.$entry;
311 if ($entry != '.' && $entry != '..' && is_dir($p) && !$this->attr($p, 'hidden')) {
322 * Return object width and height
323 * Ususaly used for images, but can be realize for video etc...
325 * @param string $path file path
326 * @param string $mime file mime type
328 * @author Dmitry (dio) Levashov
330 protected function _dimensions($path, $mime) {
332 return strpos($mime, 'image') === 0 && ($s = @getimagesize
($path)) !== false
336 /******************** file/dir content *********************/
339 * Return symlink target file
341 * @param string $path link path
343 * @author Dmitry (dio) Levashov
345 protected function readlink($path) {
346 if (!($target = @readlink
($path))) {
350 if (substr($target, 0, 1) != DIRECTORY_SEPARATOR
) {
351 $target = dirname($path).DIRECTORY_SEPARATOR
.$target;
354 $atarget = realpath($target);
361 $aroot = $this->aroot
;
363 if ($this->_inpath($atarget, $this->aroot
)) {
364 return $this->_normpath($this->root
.DIRECTORY_SEPARATOR
.substr($atarget, strlen($this->aroot
)+
1));
371 * Return files list in directory.
373 * @param string $path dir path
375 * @author Dmitry (dio) Levashov
377 protected function _scandir($path) {
380 foreach (scandir($path) as $name) {
381 if ($name != '.' && $name != '..') {
382 $files[] = $path.DIRECTORY_SEPARATOR
.$name;
389 * Open file and return file pointer
391 * @param string $path file path
392 * @param bool $write open file for writing
393 * @return resource|false
394 * @author Dmitry (dio) Levashov
396 protected function _fopen($path, $mode='rb') {
397 return @fopen
($path, 'r');
403 * @param resource $fp file pointer
405 * @author Dmitry (dio) Levashov
407 protected function _fclose($fp, $path='') {
411 /******************** file/dir manipulations *************************/
414 * Create dir and return created dir path or false on failed
416 * @param string $path parent dir path
417 * @param string $name new directory name
418 * @return string|bool
419 * @author Dmitry (dio) Levashov
421 protected function _mkdir($path, $name) {
422 $path = $path.DIRECTORY_SEPARATOR
.$name;
425 @chmod
($path, $this->options
['dirMode']);
433 * Create file and return it's path or false on failed
435 * @param string $path parent dir path
436 * @param string $name new file name
437 * @return string|bool
438 * @author Dmitry (dio) Levashov
440 protected function _mkfile($path, $name) {
441 $path = $path.DIRECTORY_SEPARATOR
.$name;
443 if (($fp = @fopen
($path, 'w'))) {
445 @chmod
($path, $this->options
['fileMode']);
454 * @param string $source file to link to
455 * @param string $targetDir folder to create link in
456 * @param string $name symlink name
458 * @author Dmitry (dio) Levashov
460 protected function _symlink($source, $targetDir, $name) {
461 return @symlink
($source, $targetDir.DIRECTORY_SEPARATOR
.$name);
465 * Copy file into another file
467 * @param string $source source file path
468 * @param string $targetDir target directory path
469 * @param string $name new file name
471 * @author Dmitry (dio) Levashov
473 protected function _copy($source, $targetDir, $name) {
474 return copy($source, $targetDir.DIRECTORY_SEPARATOR
.$name);
478 * Move file into another parent dir.
479 * Return new file path or false.
481 * @param string $source source file path
482 * @param string $target target dir path
483 * @param string $name file name
484 * @return string|bool
485 * @author Dmitry (dio) Levashov
487 protected function _move($source, $targetDir, $name) {
488 $target = $targetDir.DIRECTORY_SEPARATOR
.$name;
489 return @rename
($source, $target) ?
$target : false;
495 * @param string $path file path
497 * @author Dmitry (dio) Levashov
499 protected function _unlink($path) {
500 return @unlink
($path);
506 * @param string $path dir path
508 * @author Dmitry (dio) Levashov
510 protected function _rmdir($path) {
511 return @rmdir
($path);
515 * Create new file and write into it from file pointer.
516 * Return new file path or false on error.
518 * @param resource $fp file pointer
519 * @param string $dir target dir path
520 * @param string $name file name
521 * @return bool|string
522 * @author Dmitry (dio) Levashov
524 protected function _save($fp, $dir, $name, $mime, $w, $h) {
525 $path = $dir.DIRECTORY_SEPARATOR
.$name;
527 if (!($target = @fopen
($path, 'wb'))) {
532 fwrite($target, fread($fp, 8192));
535 @chmod
($path, $this->options
['fileMode']);
543 * @param string $path file path
544 * @return string|false
545 * @author Dmitry (dio) Levashov
547 protected function _getContents($path) {
548 return file_get_contents($path);
552 * Write a string to a file
554 * @param string $path file path
555 * @param string $content new file content
557 * @author Dmitry (dio) Levashov
559 protected function _filePutContents($path, $content) {
560 if (@file_put_contents
($path, $content, LOCK_EX
) !== false) {
568 * Detect available archivers
572 protected function _checkArchivers() {
573 if (!function_exists('exec')) {
574 $this->options
['archivers'] = $this->options
['archive'] = array();
582 //exec('tar --version', $o, $ctar);
583 $this->procExec('tar --version', $o, $ctar);
586 $arcs['create']['application/x-tar'] = array('cmd' => 'tar', 'argc' => '-cf', 'ext' => 'tar');
587 $arcs['extract']['application/x-tar'] = array('cmd' => 'tar', 'argc' => '-xf', 'ext' => 'tar');
588 //$test = exec('gzip --version', $o, $c);
590 $test = $this->procExec('gzip --version', $o, $c);
593 $arcs['create']['application/x-gzip'] = array('cmd' => 'tar', 'argc' => '-czf', 'ext' => 'tgz');
594 $arcs['extract']['application/x-gzip'] = array('cmd' => 'tar', 'argc' => '-xzf', 'ext' => 'tgz');
597 //$test = exec('bzip2 --version', $o, $c);
598 $test = $this->procExec('bzip2 --version', $o, $c);
600 $arcs['create']['application/x-bzip2'] = array('cmd' => 'tar', 'argc' => '-cjf', 'ext' => 'tbz');
601 $arcs['extract']['application/x-bzip2'] = array('cmd' => 'tar', 'argc' => '-xjf', 'ext' => 'tbz');
605 //exec('zip --version', $o, $c);
606 $this->procExec('zip -v', $o, $c);
608 $arcs['create']['application/zip'] = array('cmd' => 'zip', 'argc' => '-r9', 'ext' => 'zip');
611 $this->procExec('unzip --help', $o, $c);
613 $arcs['extract']['application/zip'] = array('cmd' => 'unzip', 'argc' => '', 'ext' => 'zip');
616 //exec('rar --version', $o, $c);
617 $this->procExec('rar --version', $o, $c);
618 if ($c == 0 ||
$c == 7) {
619 $arcs['create']['application/x-rar'] = array('cmd' => 'rar', 'argc' => 'a -inul', 'ext' => 'rar');
620 $arcs['extract']['application/x-rar'] = array('cmd' => 'rar', 'argc' => 'x -y', 'ext' => 'rar');
623 //$test = exec('unrar', $o, $c);
624 $test = $this->procExec('unrar', $o, $c);
625 if ($c==0 ||
$c == 7) {
626 $arcs['extract']['application/x-rar'] = array('cmd' => 'unrar', 'argc' => 'x -y', 'ext' => 'rar');
630 //exec('7za --help', $o, $c);
631 $this->procExec('7za --help', $o, $c);
633 $arcs['create']['application/x-7z-compressed'] = array('cmd' => '7za', 'argc' => 'a', 'ext' => '7z');
634 $arcs['extract']['application/x-7z-compressed'] = array('cmd' => '7za', 'argc' => 'e -y', 'ext' => '7z');
636 if (empty($arcs['create']['application/x-gzip'])) {
637 $arcs['create']['application/x-gzip'] = array('cmd' => '7za', 'argc' => 'a -tgzip', 'ext' => 'tar.gz');
639 if (empty($arcs['extract']['application/x-gzip'])) {
640 $arcs['extract']['application/x-gzip'] = array('cmd' => '7za', 'argc' => 'e -tgzip -y', 'ext' => 'tar.gz');
642 if (empty($arcs['create']['application/x-bzip2'])) {
643 $arcs['create']['application/x-bzip2'] = array('cmd' => '7za', 'argc' => 'a -tbzip2', 'ext' => 'tar.bz');
645 if (empty($arcs['extract']['application/x-bzip2'])) {
646 $arcs['extract']['application/x-bzip2'] = array('cmd' => '7za', 'argc' => 'a -tbzip2 -y', 'ext' => 'tar.bz');
648 if (empty($arcs['create']['application/zip'])) {
649 $arcs['create']['application/zip'] = array('cmd' => '7za', 'argc' => 'a -tzip -l', 'ext' => 'zip');
651 if (empty($arcs['extract']['application/zip'])) {
652 $arcs['extract']['application/zip'] = array('cmd' => '7za', 'argc' => 'e -tzip -y', 'ext' => 'zip');
654 if (empty($arcs['create']['application/x-tar'])) {
655 $arcs['create']['application/x-tar'] = array('cmd' => '7za', 'argc' => 'a -ttar -l', 'ext' => 'tar');
657 if (empty($arcs['extract']['application/x-tar'])) {
658 $arcs['extract']['application/x-tar'] = array('cmd' => '7za', 'argc' => 'e -ttar -y', 'ext' => 'tar');
662 $this->archivers
= $arcs;
668 * @param string $path archive path
669 * @param array $arc archiver command and arguments (same as in $this->archivers)
671 * @author Dmitry (dio) Levashov
672 * @author Alexey Sukhotin
674 protected function _unpack($path, $arc) {
676 $dir = $this->_dirname($path);
678 $cmd = $arc['cmd'].' '.$arc['argc'].' '.escapeshellarg($this->_basename($path));
679 $this->procExec($cmd, $o, $c);
684 * Recursive symlinks search
686 * @param string $path file/dir path
688 * @author Dmitry (dio) Levashov
690 protected function _findSymlinks($path) {
691 if (is_link($path)) {
696 foreach (scandir($path) as $name) {
697 if ($name != '.' && $name != '..') {
698 $p = $path.DIRECTORY_SEPARATOR
.$name;
702 if (is_dir($p) && $this->_findSymlinks($p)) {
704 } elseif (is_file($p)) {
705 $this->archiveSize +
= filesize($p);
710 $this->archiveSize +
= filesize($path);
717 * Extract files from archive
719 * @param string $path archive path
720 * @param array $arc archiver command and arguments (same as in $this->archivers)
722 * @author Dmitry (dio) Levashov,
723 * @author Alexey Sukhotin
725 protected function _extract($path, $arc) {
727 if ($this->quarantine
) {
728 $dir = $this->quarantine
.DIRECTORY_SEPARATOR
.str_replace(' ', '_', microtime()).basename($path);
729 $archive = $dir.DIRECTORY_SEPARATOR
.basename($path);
737 // copy in quarantine
738 if (!copy($path, $archive)) {
742 // extract in quarantine
743 $this->_unpack($archive, $arc);
748 foreach (scandir($dir) as $i => $name) {
749 if ($name != '.' && $name != '..') {
754 // no files - extract error ?
759 $this->archiveSize
= 0;
762 $symlinks = $this->_findSymlinks($dir);
767 return $this->setError(elFinder
::ERROR_ARC_SYMLINKS
);
770 // check max files size
771 if ($this->options
['maxArcFilesSize'] > 0 && $this->options
['maxArcFilesSize'] < $this->archiveSize
) {
772 return $this->setError(elFinder
::ERROR_ARC_MAXSIZE
);
777 // archive contains one item - extract in archive dir
778 if (count($ls) == 1) {
779 $this->_unpack($path, $arc);
780 $result = dirname($path).DIRECTORY_SEPARATOR
.$ls[0];
784 // for several files - create new directory
785 // create unique name for directory
786 $name = basename($path);
787 if (preg_match('/\.((tar\.(gz|bz|bz2|z|lzo))|cpio\.gz|ps\.gz|xcf\.(gz|bz2)|[a-z0-9]{1,4})$/i', $name, $m)) {
788 $name = substr($name, 0, strlen($name)-strlen($m[0]));
790 $test = dirname($path).DIRECTORY_SEPARATOR
.$name;
791 if (file_exists($test) ||
is_link($test)) {
792 $name = $this->uniqueName(dirname($path), $name, '-', false);
795 $result = dirname($path).DIRECTORY_SEPARATOR
.$name;
796 $archive = $result.DIRECTORY_SEPARATOR
.basename($path);
798 if (!$this->_mkdir(dirname($path), $name) ||
!copy($path, $archive)) {
802 $this->_unpack($archive, $arc);
806 return file_exists($result) ?
$result : false;
811 * Create archive and return its path
813 * @param string $dir target dir
814 * @param array $files files names list
815 * @param string $name archive name
816 * @param array $arc archiver options
817 * @return string|bool
818 * @author Dmitry (dio) Levashov,
819 * @author Alexey Sukhotin
821 protected function _archive($dir, $files, $name, $arc) {
825 $files = array_map('escapeshellarg', $files);
827 $cmd = $arc['cmd'].' '.$arc['argc'].' '.escapeshellarg($name).' '.implode(' ', $files);
828 $this->procExec($cmd, $o, $c);
831 $path = $dir.DIRECTORY_SEPARATOR
.$name;
832 return file_exists($path) ?
$path : false;