Merge branch 'fixes' into main/rendor-staging
[ryzomcore.git] / web / public_php / ams / misc / elfinder-connector / elFinderVolumeLocalFileSystem.class.php
blob1c1af8ec59b5da1e5fbcc350a48f0eccffe0ade0
1 <?php
3 /**
4 * elFinder driver for local filesystem.
6 * @author Dmitry (dio) Levashov
7 * @author Troex Nevelin
8 **/
9 class elFinderVolumeLocalFileSystem extends elFinderVolumeDriver {
11 /**
12 * Driver id
13 * Must be started from letter and contains [a-z0-9]
14 * Used as part of volume id
16 * @var string
17 **/
18 protected $driverId = 'l';
20 /**
21 * Required to count total archive files size
23 * @var int
24 **/
25 protected $archiveSize = 0;
27 /**
28 * Constructor
29 * Extend options with required fields
31 * @return void
32 * @author Dmitry (dio) Levashov
33 **/
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 /*********************************************************************/
46 /**
47 * Configure after successfull mount.
49 * @return void
50 * @author Dmitry (dio) Levashov
51 **/
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']).'$~',
59 'read' => false,
60 'write' => false,
61 'locked' => true,
62 'hidden' => true
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']);
75 parent::configure();
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)) {
82 $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';
94 } else {
95 $this->archivers['extract'] = array();
96 $this->disabled[] = 'extract';
101 /*********************************************************************/
102 /* FS API */
103 /*********************************************************************/
105 /*********************** paths/urls *************************/
108 * Return parent directory path
110 * @param string $path file path
111 * @return string
112 * @author Dmitry (dio) Levashov
114 protected function _dirname($path) {
115 return dirname($path);
119 * Return file name
121 * @param string $path file path
122 * @return string
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
132 * @param string $dir
133 * @param string $name
134 * @return string
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
145 * @return string
146 * @author Troex Nevelin
148 protected function _normpath($path) {
149 if (empty($path)) {
150 return '.';
153 if (strpos($path, '/') === 0) {
154 $initial_slashes = true;
155 } else {
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('', '.'))) {
171 continue;
174 if (($comp != '..')
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);
182 $comps = $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
195 * @return string
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
206 * @return string
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
217 * @return string
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
229 * @return bool
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) {
260 $stat = array();
262 if (!file_exists($path)) {
263 return $stat;
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;
272 $stat['size'] = 0;
273 return $stat;
275 $stat['alias'] = $this->_path($target);
276 $stat['target'] = $target;
277 $path = $target;
278 $lstat = lstat($path);
279 $size = $lstat['size'];
280 } else {
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);
290 if ($stat['read']) {
291 $stat['size'] = $dir ? 0 : $size;
294 return $stat;
299 * Return true if path is dir and has at least one childs directory
301 * @param string $path dir path
302 * @return bool
303 * @author Dmitry (dio) Levashov
305 protected function _subdirs($path) {
307 if (($dir = dir($path))) {
308 $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')) {
312 $dir->close();
313 return true;
316 $dir->close();
318 return false;
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
327 * @return string
328 * @author Dmitry (dio) Levashov
330 protected function _dimensions($path, $mime) {
331 clearstatcache();
332 return strpos($mime, 'image') === 0 && ($s = @getimagesize($path)) !== false
333 ? $s[0].'x'.$s[1]
334 : false;
336 /******************** file/dir content *********************/
339 * Return symlink target file
341 * @param string $path link path
342 * @return string
343 * @author Dmitry (dio) Levashov
345 protected function readlink($path) {
346 if (!($target = @readlink($path))) {
347 return false;
350 if (substr($target, 0, 1) != DIRECTORY_SEPARATOR) {
351 $target = dirname($path).DIRECTORY_SEPARATOR.$target;
354 $atarget = realpath($target);
356 if (!$atarget) {
357 return false;
360 $root = $this->root;
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));
367 return false;
371 * Return files list in directory.
373 * @param string $path dir path
374 * @return array
375 * @author Dmitry (dio) Levashov
377 protected function _scandir($path) {
378 $files = array();
380 foreach (scandir($path) as $name) {
381 if ($name != '.' && $name != '..') {
382 $files[] = $path.DIRECTORY_SEPARATOR.$name;
385 return $files;
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');
401 * Close opened file
403 * @param resource $fp file pointer
404 * @return bool
405 * @author Dmitry (dio) Levashov
407 protected function _fclose($fp, $path='') {
408 return @fclose($fp);
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;
424 if (@mkdir($path)) {
425 @chmod($path, $this->options['dirMode']);
426 return $path;
429 return false;
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'))) {
444 @fclose($fp);
445 @chmod($path, $this->options['fileMode']);
446 return $path;
448 return false;
452 * Create symlink
454 * @param string $source file to link to
455 * @param string $targetDir folder to create link in
456 * @param string $name symlink name
457 * @return bool
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
470 * @return bool
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;
493 * Remove file
495 * @param string $path file path
496 * @return bool
497 * @author Dmitry (dio) Levashov
499 protected function _unlink($path) {
500 return @unlink($path);
504 * Remove dir
506 * @param string $path dir path
507 * @return bool
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'))) {
528 return false;
531 while (!feof($fp)) {
532 fwrite($target, fread($fp, 8192));
534 fclose($target);
535 @chmod($path, $this->options['fileMode']);
536 clearstatcache();
537 return $path;
541 * Get file contents
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
556 * @return bool
557 * @author Dmitry (dio) Levashov
559 protected function _filePutContents($path, $content) {
560 if (@file_put_contents($path, $content, LOCK_EX) !== false) {
561 clearstatcache();
562 return true;
564 return false;
568 * Detect available archivers
570 * @return void
572 protected function _checkArchivers() {
573 if (!function_exists('exec')) {
574 $this->options['archivers'] = $this->options['archive'] = array();
575 return;
577 $arcs = array(
578 'create' => array(),
579 'extract' => array()
582 //exec('tar --version', $o, $ctar);
583 $this->procExec('tar --version', $o, $ctar);
585 if ($ctar == 0) {
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);
589 unset($o);
590 $test = $this->procExec('gzip --version', $o, $c);
592 if ($c == 0) {
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');
596 unset($o);
597 //$test = exec('bzip2 --version', $o, $c);
598 $test = $this->procExec('bzip2 --version', $o, $c);
599 if ($c == 0) {
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');
604 unset($o);
605 //exec('zip --version', $o, $c);
606 $this->procExec('zip -v', $o, $c);
607 if ($c == 0) {
608 $arcs['create']['application/zip'] = array('cmd' => 'zip', 'argc' => '-r9', 'ext' => 'zip');
610 unset($o);
611 $this->procExec('unzip --help', $o, $c);
612 if ($c == 0) {
613 $arcs['extract']['application/zip'] = array('cmd' => 'unzip', 'argc' => '', 'ext' => 'zip');
615 unset($o);
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');
621 } else {
622 unset($o);
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');
629 unset($o);
630 //exec('7za --help', $o, $c);
631 $this->procExec('7za --help', $o, $c);
632 if ($c == 0) {
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;
666 * Unpack archive
668 * @param string $path archive path
669 * @param array $arc archiver command and arguments (same as in $this->archivers)
670 * @return void
671 * @author Dmitry (dio) Levashov
672 * @author Alexey Sukhotin
674 protected function _unpack($path, $arc) {
675 $cwd = getcwd();
676 $dir = $this->_dirname($path);
677 chdir($dir);
678 $cmd = $arc['cmd'].' '.$arc['argc'].' '.escapeshellarg($this->_basename($path));
679 $this->procExec($cmd, $o, $c);
680 chdir($cwd);
684 * Recursive symlinks search
686 * @param string $path file/dir path
687 * @return bool
688 * @author Dmitry (dio) Levashov
690 protected function _findSymlinks($path) {
691 if (is_link($path)) {
692 return true;
695 if (is_dir($path)) {
696 foreach (scandir($path) as $name) {
697 if ($name != '.' && $name != '..') {
698 $p = $path.DIRECTORY_SEPARATOR.$name;
699 if (is_link($p)) {
700 return true;
702 if (is_dir($p) && $this->_findSymlinks($p)) {
703 return true;
704 } elseif (is_file($p)) {
705 $this->archiveSize += filesize($p);
709 } else {
710 $this->archiveSize += filesize($path);
713 return false;
717 * Extract files from archive
719 * @param string $path archive path
720 * @param array $arc archiver command and arguments (same as in $this->archivers)
721 * @return true
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);
731 if (!@mkdir($dir)) {
732 return false;
735 chmod($dir, 0777);
737 // copy in quarantine
738 if (!copy($path, $archive)) {
739 return false;
742 // extract in quarantine
743 $this->_unpack($archive, $arc);
744 @unlink($archive);
746 // get files list
747 $ls = array();
748 foreach (scandir($dir) as $i => $name) {
749 if ($name != '.' && $name != '..') {
750 $ls[] = $name;
754 // no files - extract error ?
755 if (empty($ls)) {
756 return false;
759 $this->archiveSize = 0;
761 // find symlinks
762 $symlinks = $this->_findSymlinks($dir);
763 // remove arc copy
764 $this->remove($dir);
766 if ($symlinks) {
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];
783 } else {
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)) {
799 return false;
802 $this->_unpack($archive, $arc);
803 @unlink($archive);
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) {
822 $cwd = getcwd();
823 chdir($dir);
825 $files = array_map('escapeshellarg', $files);
827 $cmd = $arc['cmd'].' '.$arc['argc'].' '.escapeshellarg($name).' '.implode(' ', $files);
828 $this->procExec($cmd, $o, $c);
829 chdir($cwd);
831 $path = $dir.DIRECTORY_SEPARATOR.$name;
832 return file_exists($path) ? $path : false;
835 } // END class