3 * Base class for elFinder volume.
5 * 1. Public API (commands)
8 * All abstract methods begin with "_"
10 * @author Dmitry (dio) Levashov
11 * @author Troex Nevelin
12 * @author Alexey Sukhotin
14 abstract class elFinderVolumeDriver
{
18 * Must be started from letter and contains [a-z0-9]
19 * Used as part of volume id
23 protected $driverId = 'a';
26 * Volume id - used as prefix for files hashes
33 * Flag - volume "mounted" and available
37 protected $mounted = false;
47 * Root basename | alias
51 protected $rootName = '';
54 * Default directory to open
58 protected $startPath = '';
72 protected $tmbPath = '';
75 * Is thumbnails dir writable
79 protected $tmbPathWritable = false;
86 protected $tmbURL = '';
89 * Thumbnails size in px
93 protected $tmbSize = 48;
96 * Image manipulation lib name
97 * auto|imagick|mogtify|gd
101 protected $imgLib = 'auto';
104 * Library to crypt files name
108 protected $cryptLib = '';
115 protected $archivers = array(
121 * How many subdirs levels return for tree
125 protected $treeDeep = 1;
128 * Errors from last failed action
132 protected $error = array();
135 * Today 24:00 timestamp
139 protected $today = 0;
142 * Yesterday 24:00 timestamp
146 protected $yesterday = 0;
149 * Object configuration
153 protected $options = array(
155 // root directory path
157 // open this path on initial request instead of root path
159 // how many subdirs levels return per request
161 // root url, not set to disable sending URL to client (replacement for old "fileURL" option)
163 // directory separator. required by client to show paths correctly
164 'separator' => DIRECTORY_SEPARATOR
,
165 // library to crypt/uncrypt files names (not implemented)
167 // how to detect files mimetypes. (auto/internal/finfo/mime_content_type)
168 'mimeDetect' => 'auto',
169 // mime.types file path (for mimeDetect==internal)
171 // directory for thumbnails
173 // mode to create thumbnails dir
174 'tmbPathMode' => 0777,
175 // thumbnails dir URL. Set it if store thumbnails outside root directory
177 // thumbnails size (px)
179 // thumbnails crop (true - crop, false - scale image to fit thumbnail size)
181 // thumbnails background color (hex #rrggbb or 'transparent')
182 'tmbBgColor' => '#ffffff',
183 // image manipulations library
185 // on paste file - if true - old file will be replaced with new one, if false new file get name - original_name-number.ext
186 'copyOverwrite' => true,
187 // if true - join new and old directories content on paste
189 // on upload - if true - old file will be replaced with new one, if false new file get name - original_name-number.ext
190 'uploadOverwrite' => true,
191 // mimetypes allowed to upload
192 'uploadAllow' => array(),
193 // mimetypes not allowed to upload
194 'uploadDeny' => array(),
195 // order to proccess uploadAllow and uploadDeny options
196 'uploadOrder' => array('deny', 'allow'),
197 // maximum upload file size. NOTE - this is size for every uploaded files
198 'uploadMaxSize' => 0,
199 // files dates format
200 'dateFormat' => 'j M Y H:i',
202 'timeFormat' => 'H:i',
203 // if true - every folder will be check for children folders, otherwise all folders will be marked as having subfolders
204 'checkSubfolders' => true,
205 // allow to copy from this volume to other ones?
207 // allow to copy from other volumes to this one?
209 // list of commands disabled on this root
210 'disabled' => array(),
211 // regexp or function name to validate new file name
212 'acceptedName' => '/^\w[\w\s\.\%\-\(\)\[\]]*$/u',
213 // function/class method to control files permissions
214 'accessControl' => null,
215 // some data required by access control
216 'accessControlData' => null,
217 // default permissions. not set hidden/locked here - take no effect
223 'attributes' => array(),
224 // Allowed archive's mimetypes to create. Leave empty for all available types.
225 'archiveMimes' => array(),
226 // Manual config for archivers. See example below. Leave empty for auto detect
227 'archivers' => array(),
228 // required to fix bug on macos
231 'utf8patterns' => array("\u0438\u0306", "\u0435\u0308", "\u0418\u0306", "\u0415\u0308", "\u00d8A", "\u030a"),
232 'utf8replace' => array("\u0439", "\u0451", "\u0419", "\u0401", "\u00d8", "\u00c5")
236 * Defaults permissions
240 protected $defaults = array(
248 * Access control function/class
252 protected $attributes = array();
255 * Access control function/class
259 protected $access = null;
262 * Mime types allowed to upload
266 protected $uploadAllow = array();
269 * Mime types denied to upload
273 protected $uploadDeny = array();
276 * Order to validate uploadAllow and uploadDeny
280 protected $uploadOrder = array();
283 * Maximum allowed upload file size.
284 * Set as number or string with unit - "10M", "500K", "1G"
288 protected $uploadMaxSize = 0;
291 * Mimetype detect method
295 protected $mimeDetect = 'auto';
298 * Flag - mimetypes from externail file was loaded
302 private static $mimetypesLoaded = false;
305 * Finfo object for mimeDetect == 'finfo'
309 protected $finfo = null;
312 * List of disabled client's commands
316 protected $diabled = array();
319 * default extensions/mimetypes for mimeDetect == 'internal'
323 protected static $mimetypes = array(
325 'ai' => 'application/postscript',
326 'eps' => 'application/postscript',
327 'exe' => 'application/x-executable',
328 'doc' => 'application/vnd.ms-word',
329 'xls' => 'application/vnd.ms-excel',
330 'ppt' => 'application/vnd.ms-powerpoint',
331 'pps' => 'application/vnd.ms-powerpoint',
332 'pdf' => 'application/pdf',
333 'xml' => 'application/xml',
334 'odt' => 'application/vnd.oasis.opendocument.text',
335 'swf' => 'application/x-shockwave-flash',
336 'torrent' => 'application/x-bittorrent',
337 'jar' => 'application/x-jar',
339 'gz' => 'application/x-gzip',
340 'tgz' => 'application/x-gzip',
341 'bz' => 'application/x-bzip2',
342 'bz2' => 'application/x-bzip2',
343 'tbz' => 'application/x-bzip2',
344 'zip' => 'application/zip',
345 'rar' => 'application/x-rar',
346 'tar' => 'application/x-tar',
347 '7z' => 'application/x-7z-compressed',
349 'txt' => 'text/plain',
350 'php' => 'text/x-php',
351 'html' => 'text/html',
352 'htm' => 'text/html',
353 'js' => 'text/javascript',
356 'rtfd' => 'text/rtfd',
357 'py' => 'text/x-python',
358 'java' => 'text/x-java-source',
359 'rb' => 'text/x-ruby',
360 'sh' => 'text/x-shellscript',
361 'pl' => 'text/x-perl',
363 'sql' => 'text/x-sql',
364 'c' => 'text/x-csrc',
365 'h' => 'text/x-chdr',
366 'cpp' => 'text/x-c++src',
367 'hh' => 'text/x-c++hdr',
368 'log' => 'text/plain',
369 'csv' => 'text/x-comma-separated-values',
371 'bmp' => 'image/x-ms-bmp',
372 'jpg' => 'image/jpeg',
373 'jpeg' => 'image/jpeg',
374 'gif' => 'image/gif',
375 'png' => 'image/png',
376 'tif' => 'image/tiff',
377 'tiff' => 'image/tiff',
378 'tga' => 'image/x-targa',
379 'psd' => 'image/vnd.adobe.photoshop',
380 'ai' => 'image/vnd.adobe.photoshop',
381 'xbm' => 'image/xbm',
382 'pxm' => 'image/pxm',
384 'mp3' => 'audio/mpeg',
385 'mid' => 'audio/midi',
386 'ogg' => 'audio/ogg',
387 'oga' => 'audio/ogg',
388 'm4a' => 'audio/x-m4a',
389 'wav' => 'audio/wav',
390 'wma' => 'audio/x-ms-wma',
392 'avi' => 'video/x-msvideo',
393 'dv' => 'video/x-dv',
394 'mp4' => 'video/mp4',
395 'mpeg' => 'video/mpeg',
396 'mpg' => 'video/mpeg',
397 'mov' => 'video/quicktime',
398 'wm' => 'video/x-ms-wmv',
399 'flv' => 'video/x-flv',
400 'mkv' => 'video/x-matroska',
401 'webm' => 'video/webm',
402 'ogv' => 'video/ogg',
407 * Directory separator - required by client
411 protected $separator = DIRECTORY_SEPARATOR
;
414 * Mimetypes allowed to display
418 protected $onlyMimes = array();
421 * Store files moved or overwrited files info
425 protected $removed = array();
432 protected $cache = array();
439 protected $dirsCache = array();
441 /*********************************************************************/
443 /*********************************************************************/
446 * Prepare driver before mount volume.
447 * Return true if volume is ready.
450 * @author Dmitry (dio) Levashov
452 protected function init() {
457 * Configure after successfull mount.
458 * By default set thumbnails path and image manipulation library.
461 * @author Dmitry (dio) Levashov
463 protected function configure() {
464 // set thumbnails path
465 $path = $this->options
['tmbPath'];
467 if (!file_exists($path)) {
469 chmod($path, $this->options
['tmbPathMode']);
475 if (is_dir($path) && is_readable($path)) {
476 $this->tmbPath
= $path;
477 $this->tmbPathWritable
= is_writable($path);
481 // set image manipulation library
482 $type = preg_match('/^(imagick|gd|auto)$/i', $this->options
['imgLib'])
483 ?
strtolower($this->options
['imgLib'])
486 if (($type == 'imagick' ||
$type == 'auto') && extension_loaded('imagick')) {
487 $this->imgLib
= 'imagick';
489 $this->imgLib
= function_exists('gd_info') ?
'gd' : '';
495 /*********************************************************************/
497 /*********************************************************************/
500 * Return driver id. Used as a part of volume id.
503 * @author Dmitry (dio) Levashov
505 public function driverId() {
506 return $this->driverId
;
513 * @author Dmitry (dio) Levashov
515 public function id() {
520 * Return debug info for client
523 * @author Dmitry (dio) Levashov
525 public function debug() {
528 'name' => strtolower(substr(get_class($this), strlen('elfinderdriver'))),
529 'mimeDetect' => $this->mimeDetect
,
530 'imgLib' => $this->imgLib
536 * Return true if volume available for read or write,
540 * @author Dmitry (dio) Levashov
541 * @author Alexey Sukhotin
543 public function mount(array $opts) {
544 if (!isset($opts['path']) ||
$opts['path'] === '') {
548 $this->options
= array_merge($this->options
, $opts);
549 $this->id
= $this->driverId
.(!empty($this->options
['id']) ?
$this->options
['id'] : elFinder
::$volumesCnt++
).'_';
550 $this->root
= $this->_normpath($this->options
['path']);
551 $this->separator
= isset($this->options
['separator']) ?
$this->options
['separator'] : DIRECTORY_SEPARATOR
;
553 // default file attribute
554 $this->defaults
= array(
555 'read' => isset($this->options
['defaults']['read']) ?
!!$this->options
['defaults']['read'] : true,
556 'write' => isset($this->options
['defaults']['write']) ?
!!$this->options
['defaults']['write'] : true,
562 $this->attributes
[] = array(
563 'pattern' => '~^'.preg_quote(DIRECTORY_SEPARATOR
).'$~',
567 // set files attributes
568 if (!empty($this->options
['attributes']) && is_array($this->options
['attributes'])) {
570 foreach ($this->options
['attributes'] as $a) {
571 // attributes must contain pattern and at least one rule
572 if (!empty($a['pattern']) ||
count($a) > 1) {
573 $this->attributes
[] = $a;
578 if (!empty($this->options
['accessControl'])) {
579 if (is_string($this->options
['accessControl'])
580 && function_exists($this->options
['accessControl'])) {
581 $this->access
= $this->options
['accessControl'];
582 } elseif (is_array($this->options
['accessControl'])
583 && count($this->options
['accessControl']) > 1
584 && is_object($this->options
['accessControl'][0])
585 && method_exists($this->options
['accessControl'][0], $this->options
['accessControl'][1])) {
586 $this->access
= array($this->options
['accessControl'][0], $this->options
['accessControl'][1]);
590 $this->today
= mktime(0,0,0, date('m'), date('d'), date('Y'));
591 $this->yesterday
= $this->today
-86400;
593 // debug($this->attributes);
594 if (!$this->init()) {
598 // check some options is arrays
599 $this->uploadAllow
= isset($this->options
['uploadAllow']) && is_array($this->options
['uploadAllow'])
600 ?
$this->options
['uploadAllow']
603 $this->uploadDeny
= isset($this->options
['uploadDeny']) && is_array($this->options
['uploadDeny'])
604 ?
$this->options
['uploadDeny']
607 if (is_string($this->options
['uploadOrder'])) { // telephat_mode on, compatibility with 1.x
608 $parts = explode(',', isset($this->options
['uploadOrder']) ?
$this->options
['uploadOrder'] : 'deny,allow');
609 $this->uploadOrder
= array(trim($parts[0]), trim($parts[1]));
610 } else { // telephat_mode off
611 $this->uploadOrder
= $this->options
['uploadOrder'];
614 if (!empty($this->options
['uploadMaxSize'])) {
615 $size = ''.$this->options
['uploadMaxSize'];
616 $unit = strtolower(substr($size, strlen($size) - 1));
628 $this->uploadMaxSize
= intval($size)*$n;
631 $this->disabled
= isset($this->options
['disabled']) && is_array($this->options
['disabled'])
632 ?
$this->options
['disabled']
635 $this->cryptLib
= $this->options
['cryptLib'];
636 $this->mimeDetect
= $this->options
['mimeDetect'];
638 // find available mimetype detect method
639 $type = strtolower($this->options
['mimeDetect']);
640 $type = preg_match('/^(finfo|mime_content_type|internal|auto)$/i', $type) ?
$type : 'auto';
641 $regexp = '/text\/x\-(php|c\+\+)/';
643 if (($type == 'finfo' ||
$type == 'auto')
644 && class_exists('finfo')
645 && preg_match($regexp, array_shift(explode(';', @finfo_file
(finfo_open(FILEINFO_MIME
), __FILE__
))))) {
647 $this->finfo
= finfo_open(FILEINFO_MIME
);
648 } elseif (($type == 'mime_content_type' ||
$type == 'auto')
649 && function_exists('mime_content_type')
650 && preg_match($regexp, array_shift(explode(';', mime_content_type(__FILE__
))))) {
651 $type = 'mime_content_type';
655 $this->mimeDetect
= $type;
657 // load mimes from external file for mimeDetect == 'internal'
658 // based on Alexey Sukhotin idea and patch: http://elrte.org/redmine/issues/163
659 // file must be in file directory or in parent one
660 if ($this->mimeDetect
== 'internal' && !self
::$mimetypesLoaded) {
661 self
::$mimetypesLoaded = true;
662 $this->mimeDetect
= 'internal';
664 if (!empty($this->options
['mimefile']) && file_exists($this->options
['mimefile'])) {
665 $file = $this->options
['mimefile'];
666 } elseif (file_exists(dirname(__FILE__
).DIRECTORY_SEPARATOR
.'mime.types')) {
667 $file = dirname(__FILE__
).DIRECTORY_SEPARATOR
.'mime.types';
668 } elseif (file_exists(dirname(dirname(__FILE__
)).DIRECTORY_SEPARATOR
.'mime.types')) {
669 $file = dirname(dirname(__FILE__
)).DIRECTORY_SEPARATOR
.'mime.types';
672 if ($file && file_exists($file)) {
673 $mimecf = file($file);
675 foreach ($mimecf as $line_num => $line) {
676 if (!preg_match('/^\s*#/', $line)) {
677 $mime = preg_split('/\s+/', $line, -1, PREG_SPLIT_NO_EMPTY
);
678 for ($i = 1, $size = count($mime); $i < $size ; $i++
) {
679 if (!isset(self
::$mimetypes[$mime[$i]])) {
680 self
::$mimetypes[$mime[$i]] = $mime[0];
688 $this->rootName
= empty($this->options
['alias']) ?
$this->_basename($this->root
) : $this->options
['alias'];
689 $root = $this->stat($this->root
);
692 return $this->setError('Root folder does not exists.');
694 if (!$root['read'] && !$root['write']) {
695 return $this->setError('Root folder has not read and write permissions.');
701 // check startPath - path to open by default instead of root
702 if ($this->options
['startPath']) {
703 $start = $this->stat($this->options
['startPath']);
705 && $start['mime'] == 'directory'
707 && empty($start['hidden'])
708 && $this->_inpath($this->options
['startPath'], $this->root
)) {
709 $this->startPath
= $this->options
['startPath'];
710 if (substr($this->startPath
, -1, 1) == $this->options
['separator']) {
711 $this->startPath
= substr($this->startPath
, 0, -1);
716 $this->options
['URL'] = '';
717 $this->options
['tmbURL'] = '';
718 $this->options
['tmbPath'] = '';
720 array_unshift($this->attributes
, array(
725 $this->treeDeep
= $this->options
['treeDeep'] > 0 ?
(int)$this->options
['treeDeep'] : 1;
726 $this->tmbSize
= $this->options
['tmbSize'] > 0 ?
(int)$this->options
['tmbSize'] : 48;
727 $this->URL
= $this->options
['URL'];
728 if ($this->URL
&& preg_match("|[^/?&=]$|", $this->URL
)) {
732 $this->tmbURL
= !empty($this->options
['tmbURL']) ?
$this->options
['tmbURL'] : '';
733 if ($this->tmbURL
&& preg_match("|[^/?&=]$|", $this->tmbURL
)) {
734 $this->tmbURL
.= '/';
737 $this->nameValidator
= is_string($this->options
['acceptedName']) && !empty($this->options
['acceptedName'])
738 ?
$this->options
['acceptedName']
741 $this->_checkArchivers();
742 // manual control archive types to create
743 if (!empty($this->options
['archiveMimes']) && is_array($this->options
['archiveMimes'])) {
744 foreach ($this->archivers
['create'] as $mime => $v) {
745 if (!in_array($mime, $this->options
['archiveMimes'])) {
746 unset($this->archivers
['create'][$mime]);
751 // manualy add archivers
752 if (!empty($this->options
['archivers']['create']) && is_array($this->options
['archivers']['create'])) {
753 foreach ($this->options
['archivers']['create'] as $mime => $conf) {
754 if (strpos($mime, 'application/') === 0
755 && !empty($conf['cmd'])
756 && isset($conf['argc'])
757 && !empty($conf['ext'])
758 && !isset($this->archivers
['create'][$mime])) {
759 $this->archivers
['create'][$mime] = $conf;
764 if (!empty($this->options
['archivers']['extract']) && is_array($this->options
['archivers']['extract'])) {
765 foreach ($this->options
['archivers']['extract'] as $mime => $conf) {
766 if (substr($mime, 'application/') === 0
767 && !empty($cons['cmd'])
768 && isset($conf['argc'])
769 && !empty($conf['ext'])
770 && !isset($this->archivers
['extract'][$mime])) {
771 $this->archivers
['extract'][$mime] = $conf;
777 // echo $this->uploadMaxSize;
778 // echo $this->options['uploadMaxSize'];
779 return $this->mounted
= true;
783 * Some "unmount" stuffs - may be required by virtual fs
786 * @author Dmitry (dio) Levashov
788 public function umount() {
792 * Return error message from last failed action
795 * @author Dmitry (dio) Levashov
797 public function error() {
802 * Set mimetypes allowed to display to client
804 * @param array $mimes
806 * @author Dmitry (dio) Levashov
808 public function setMimesFilter($mimes) {
809 if (is_array($mimes)) {
810 $this->onlyMimes
= $mimes;
815 * Return root folder hash
818 * @author Dmitry (dio) Levashov
820 public function root() {
821 return $this->encode($this->root
);
825 * Return root or startPath hash
828 * @author Dmitry (dio) Levashov
830 public function defaultPath() {
831 return $this->encode($this->startPath ?
$this->startPath
: $this->root
);
835 * Return volume options required by client:
838 * @author Dmitry (dio) Levashov
840 public function options($hash) {
842 'path' => $this->_path($this->decode($hash)),
844 'tmbUrl' => $this->tmbURL
,
845 'disabled' => $this->disabled
,
846 'separator' => $this->separator
,
847 'copyOverwrite' => intval($this->options
['copyOverwrite']),
848 'archivers' => array(
849 'create' => array_keys($this->archivers
['create']),
850 'extract' => array_keys($this->archivers
['extract'])
856 * Return true if command disabled in options
858 * @param string $cmd command name
860 * @author Dmitry (dio) Levashov
862 public function commandDisabled($cmd) {
863 return in_array($cmd, $this->disabled
);
867 * Return true if mime is required mimes list
869 * @param string $mime mime type to check
870 * @param array $mimes allowed mime types list or not set to use client mimes list
871 * @param bool|null $empty what to return on empty list
873 * @author Dmitry (dio) Levashov
874 * @author Troex Nevelin
876 public function mimeAccepted($mime, $mimes = array(), $empty = true) {
877 $mimes = !empty($mimes) ?
$mimes : $this->onlyMimes
;
881 return $mime == 'directory'
882 ||
in_array('all', $mimes)
883 ||
in_array('All', $mimes)
884 ||
in_array($mime, $mimes)
885 ||
in_array(substr($mime, 0, strpos($mime, '/')), $mimes);
889 * Return true if voume is readable.
892 * @author Dmitry (dio) Levashov
894 public function isReadable() {
895 $stat = $this->stat($this->root
);
896 return $stat['read'];
900 * Return true if copy from this volume allowed
903 * @author Dmitry (dio) Levashov
905 public function copyFromAllowed() {
906 return !!$this->options
['copyFrom'];
910 * Return file path related to root
912 * @param string $hash file hash
914 * @author Dmitry (dio) Levashov
916 public function path($hash) {
917 return $this->_path($this->decode($hash));
921 * Return file real path if file exists
923 * @param string $hash file hash
925 * @author Dmitry (dio) Levashov
927 public function realpath($hash) {
928 $path = $this->decode($hash);
929 return $this->stat($path) ?
$path : false;
933 * Return list of moved/overwrited files
936 * @author Dmitry (dio) Levashov
938 public function removed() {
939 return $this->removed
;
943 * Clean removed files list
946 * @author Dmitry (dio) Levashov
948 public function resetRemoved() {
949 $this->removed
= array();
953 * Return file/dir hash or first founded child hash with required attr == $val
955 * @param string $hash file hash
956 * @param string $attr attribute name
957 * @param bool $val attribute value
958 * @return string|false
959 * @author Dmitry (dio) Levashov
961 public function closest($hash, $attr, $val) {
962 return ($path = $this->closestByAttr($this->decode($hash), $attr, $val)) ?
$this->encode($path) : false;
966 * Return file info or false on error
968 * @param string $hash file hash
969 * @param bool $realpath add realpath field to file info
970 * @return array|false
971 * @author Dmitry (dio) Levashov
973 public function file($hash) {
974 $path = $this->decode($hash);
976 return ($file = $this->stat($path)) ?
$file : $this->setError(elFinder
::ERROR_FILE_NOT_FOUND
);
978 if (($file = $this->stat($path)) != false) {
980 $file['realpath'] = $path;
984 return $this->setError(elFinder
::ERROR_FILE_NOT_FOUND
);
990 * @param string $hash folder hash
991 * @param bool $hidden return hidden file info
992 * @return array|false
993 * @author Dmitry (dio) Levashov
995 public function dir($hash, $resolveLink=false) {
996 if (($dir = $this->file($hash)) == false) {
997 return $this->setError(elFinder
::ERROR_DIR_NOT_FOUND
);
1000 if ($resolveLink && !empty($dir['thash'])) {
1001 $dir = $this->file($dir['thash']);
1004 return $dir && $dir['mime'] == 'directory' && empty($dir['hidden'])
1006 : $this->setError(elFinder
::ERROR_NOT_DIR
);
1010 * Return directory content or false on error
1012 * @param string $hash file hash
1013 * @return array|false
1014 * @author Dmitry (dio) Levashov
1016 public function scandir($hash) {
1017 if (($dir = $this->dir($hash)) == false) {
1022 ?
$this->getScandir($this->decode($hash))
1023 : $this->setError(elFinder
::ERROR_PERM_DENIED
);
1027 * Return dir files names list
1029 * @param string $hash file hash
1031 * @author Dmitry (dio) Levashov
1033 public function ls($hash) {
1034 if (($dir = $this->dir($hash)) == false ||
!$dir['read']) {
1039 $path = $this->decode($hash);
1041 foreach ($this->getScandir($path) as $stat) {
1042 if (empty($stat['hidden']) && $this->mimeAccepted($stat['mime'])) {
1043 $list[] = $stat['name'];
1051 * Return subfolders for required folder or false on error
1053 * @param string $hash folder hash or empty string to get tree from root folder
1054 * @param int $deep subdir deep
1055 * @param string $exclude dir hash which subfolders must be exluded from result, required to not get stat twice on cwd subfolders
1056 * @return array|false
1057 * @author Dmitry (dio) Levashov
1059 public function tree($hash='', $deep=0, $exclude='') {
1060 $path = $hash ?
$this->decode($hash) : $this->root
;
1062 if (($dir = $this->stat($path)) == false ||
$dir['mime'] != 'directory') {
1066 $dirs = $this->gettree($path, $deep > 0 ?
$deep -1 : $this->treeDeep
-1, $this->decode($exclude));
1067 array_unshift($dirs, $dir);
1072 * Return part of dirs tree from required dir up to root dir
1074 * @param string $hash directory hash
1076 * @author Dmitry (dio) Levashov
1078 public function parents($hash) {
1079 if (($current = $this->dir($hash)) == false) {
1083 $path = $this->decode($hash);
1086 while ($path && $path != $this->root
) {
1087 $path = $this->_dirname($path);
1088 $stat = $this->stat($path);
1089 if (!empty($stat['hidden']) ||
!$stat['read']) {
1093 array_unshift($tree, $stat);
1094 if ($path != $this->root
) {
1095 foreach ($this->gettree($path, 0) as $dir) {
1096 if (!in_array($dir, $tree)) {
1103 return $tree ?
$tree : array($current);
1107 * Create thumbnail for required file and return its name of false on failed
1109 * @return string|false
1110 * @author Dmitry (dio) Levashov
1112 public function tmb($hash) {
1113 $path = $this->decode($hash);
1114 $stat = $this->stat($path);
1116 if (isset($stat['tmb'])) {
1117 return $stat['tmb'] == "1" ?
$this->createTmb($path, $stat) : $stat['tmb'];
1123 * Return file size / total directory size
1125 * @param string file hash
1127 * @author Dmitry (dio) Levashov
1129 public function size($hash) {
1130 return $this->countSize($this->decode($hash));
1134 * Open file for reading and return file pointer
1136 * @param string file hash
1138 * @author Dmitry (dio) Levashov
1140 public function open($hash) {
1141 if (($file = $this->file($hash)) == false
1142 ||
$file['mime'] == 'directory') {
1146 return $this->_fopen($this->decode($hash), 'rb');
1150 * Close file pointer
1152 * @param Resource $fp file pointer
1153 * @param string $hash file hash
1155 * @author Dmitry (dio) Levashov
1157 public function close($fp, $hash) {
1158 $this->_fclose($fp, $this->decode($hash));
1162 * Create directory and return dir info
1164 * @param string $dst destination directory
1165 * @param string $name directory name
1166 * @return array|false
1167 * @author Dmitry (dio) Levashov
1169 public function mkdir($dst, $name) {
1170 if ($this->commandDisabled('mkdir')) {
1171 return $this->setError(elFinder
::ERROR_PERM_DENIED
);
1174 if (!$this->nameAccepted($name)) {
1175 return $this->setError(elFinder
::ERROR_INVALID_NAME
);
1178 if (($dir = $this->dir($dst)) == false) {
1179 return $this->setError(elFinder
::ERROR_TRGDIR_NOT_FOUND
, '#'.$dst);
1182 if (!$dir['write']) {
1183 return $this->setError(elFinder
::ERROR_PERM_DENIED
);
1186 $path = $this->decode($dst);
1187 $dst = $this->_joinPath($path, $name);
1188 $stat = $this->stat($dst);
1189 if (!empty($stat)) {
1190 return $this->setError(elFinder
::ERROR_EXISTS
, $name);
1192 $this->clearcache();
1193 return ($path = $this->_mkdir($path, $name)) ?
$this->stat($path) : false;
1197 * Create empty file and return its info
1199 * @param string $dst destination directory
1200 * @param string $name file name
1201 * @return array|false
1202 * @author Dmitry (dio) Levashov
1204 public function mkfile($dst, $name) {
1205 if ($this->commandDisabled('mkfile')) {
1206 return $this->setError(elFinder
::ERROR_PERM_DENIED
);
1209 if (!$this->nameAccepted($name)) {
1210 return $this->setError(elFinder
::ERROR_INVALID_NAME
);
1213 if (($dir = $this->dir($dst)) == false) {
1214 return $this->setError(elFinder
::ERROR_TRGDIR_NOT_FOUND
, '#'.$dst);
1217 if (!$dir['write']) {
1218 return $this->setError(elFinder
::ERROR_PERM_DENIED
);
1221 $path = $this->decode($dst);
1223 if ($this->stat($this->_joinPath($path, $name))) {
1224 return $this->setError(elFinder
::ERROR_EXISTS
, $name);
1226 $this->clearcache();
1227 return ($path = $this->_mkfile($path, $name)) ?
$this->stat($path) : false;
1231 * Rename file and return file info
1233 * @param string $hash file hash
1234 * @param string $name new file name
1235 * @return array|false
1236 * @author Dmitry (dio) Levashov
1238 public function rename($hash, $name) {
1239 if ($this->commandDisabled('rename')) {
1240 return $this->setError(elFinder
::ERROR_PERM_DENIED
);
1243 if (!$this->nameAccepted($name)) {
1244 return $this->setError(elFinder
::ERROR_INVALID_NAME
, $name);
1247 if (!($file = $this->file($hash))) {
1248 return $this->setError(elFinder
::ERROR_FILE_NOT_FOUND
);
1251 if ($name == $file['name']) {
1255 if (!empty($file['locked'])) {
1256 return $this->setError(elFinder
::ERROR_LOCKED
, $file['name']);
1259 $path = $this->decode($hash);
1260 $dir = $this->_dirname($path);
1261 $stat = $this->stat($this->_joinPath($dir, $name));
1263 return $this->setError(elFinder
::ERROR_EXISTS
, $name);
1266 if (!$this->_move($path, $dir, $name)) {
1270 if (!empty($stat['tmb']) && $stat['tmb'] != "1") {
1271 $this->rmTmb($stat['tmb']);
1274 $path = $this->_joinPath($dir, $name);
1276 $this->clearcache();
1277 return $this->stat($path);
1281 * Create file copy with suffix "copy number" and return its info
1283 * @param string $hash file hash
1284 * @param string $suffix suffix to add to file name
1285 * @return array|false
1286 * @author Dmitry (dio) Levashov
1288 public function duplicate($hash, $suffix='copy') {
1289 if ($this->commandDisabled('duplicate')) {
1290 return $this->setError(elFinder
::ERROR_COPY
, '#'.$hash, elFinder
::ERROR_PERM_DENIED
);
1293 if (($file = $this->file($hash)) == false) {
1294 return $this->setError(elFinder
::ERROR_COPY
, elFinder
::ERROR_FILE_NOT_FOUND
);
1297 $path = $this->decode($hash);
1298 $dir = $this->_dirname($path);
1300 return ($path = $this->copy($path, $dir, $this->uniqueName($dir, $this->_basename($path), ' '.$suffix.' '))) == false
1302 : $this->stat($path);
1306 * Save uploaded file.
1307 * On success return array with new file stat and with removed file hash (if existed file was replaced)
1309 * @param Resource $fp file pointer
1310 * @param string $dst destination folder hash
1311 * @param string $src file name
1312 * @param string $tmpname file tmp name - required to detect mime type
1313 * @return array|false
1314 * @author Dmitry (dio) Levashov
1316 public function upload($fp, $dst, $name, $tmpname) {
1317 if ($this->commandDisabled('upload')) {
1318 return $this->setError(elFinder
::ERROR_PERM_DENIED
);
1321 if (($dir = $this->dir($dst)) == false) {
1322 return $this->setError(elFinder
::ERROR_TRGDIR_NOT_FOUND
, '#'.$dst);
1325 if (!$dir['write']) {
1326 return $this->setError(elFinder
::ERROR_PERM_DENIED
);
1329 if (!$this->nameAccepted($name)) {
1330 return $this->setError(elFinder
::ERROR_INVALID_NAME
);
1333 $mime = $this->mimetype($this->mimeDetect
== 'internal' ?
$name : $tmpname);
1334 if ($mime == 'unknown' && $this->mimeDetect
== 'internal') {
1335 $mime = elFinderVolumeDriver
::mimetypeInternalDetect($name);
1338 // logic based on http://httpd.apache.org/docs/2.2/mod/mod_authz_host.html#order
1339 $allow = $this->mimeAccepted($mime, $this->uploadAllow
, null);
1340 $deny = $this->mimeAccepted($mime, $this->uploadDeny
, null);
1341 $upload = true; // default to allow
1342 if (strtolower($this->uploadOrder
[0]) == 'allow') { // array('allow', 'deny'), default is to 'deny'
1343 $upload = false; // default is deny
1344 if (!$deny && ($allow === true)) { // match only allow
1346 }// else (both match | no match | match only deny) { deny }
1347 } else { // array('deny', 'allow'), default is to 'allow' - this is the default rule
1348 $upload = true; // default is allow
1349 if (($deny === true) && !$allow) { // match only deny
1351 } // else (both match | no match | match only allow) { allow }
1354 return $this->setError(elFinder
::ERROR_UPLOAD_FILE_MIME
);
1357 if ($this->uploadMaxSize
> 0 && filesize($tmpname) > $this->uploadMaxSize
) {
1358 return $this->setError(elFinder
::ERROR_UPLOAD_FILE_SIZE
);
1361 $dstpath = $this->decode($dst);
1362 $test = $this->_joinPath($dstpath, $name);
1364 $file = $this->stat($test);
1365 $this->clearcache();
1367 if ($file) { // file exists
1368 if ($this->options
['uploadOverwrite']) {
1369 if (!$file['write']) {
1370 return $this->setError(elFinder
::ERROR_PERM_DENIED
);
1371 } elseif ($file['mime'] == 'directory') {
1372 return $this->setError(elFinder
::ERROR_NOT_REPLACE
, $name);
1374 $this->remove($file);
1376 $name = $this->uniqueName($dstpath, $name, '-', false);
1381 if (strpos($mime, 'image') === 0 && ($s = getimagesize($tmpname))) {
1385 // $this->clearcache();
1386 if (($path = $this->_save($fp, $dstpath, $name, $mime, $w, $h)) == false) {
1392 return $this->stat($path);
1398 * @param Object $volume source volume
1399 * @param string $source file hash
1400 * @param string $dst destination dir hash
1401 * @param bool $rmSrc remove source after copy?
1402 * @return array|false
1403 * @author Dmitry (dio) Levashov
1405 public function paste($volume, $src, $dst, $rmSrc = false) {
1406 $err = $rmSrc ? elFinder
::ERROR_MOVE
: elFinder
::ERROR_COPY
;
1408 if ($this->commandDisabled('paste')) {
1409 return $this->setError($err, '#'.$src, elFinder
::ERROR_PERM_DENIED
);
1412 if (($file = $volume->file($src, $rmSrc)) == false) {
1413 return $this->setError($err, '#'.$src, elFinder
::ERROR_FILE_NOT_FOUND
);
1416 $name = $file['name'];
1417 $errpath = $volume->path($src);
1419 if (($dir = $this->dir($dst)) == false) {
1420 return $this->setError($err, $errpath, elFinder
::ERROR_TRGDIR_NOT_FOUND
, '#'.$dst);
1423 if (!$dir['write'] ||
!$file['read']) {
1424 return $this->setError($err, $errpath, elFinder
::ERROR_PERM_DENIED
);
1427 $destination = $this->decode($dst);
1429 if (($test = $volume->closest($src, $rmSrc ?
'locked' : 'read', $rmSrc))) {
1431 ?
$this->setError($err, $errpath, elFinder
::ERROR_LOCKED
, $volume->path($test))
1432 : $this->setError($err, $errpath, elFinder
::ERROR_PERM_DENIED
);
1435 $test = $this->_joinPath($destination, $name);
1436 $stat = $this->stat($test);
1437 $this->clearcache();
1439 if ($this->options
['copyOverwrite']) {
1440 // do not replace file with dir or dir with file
1441 if (!$this->isSameType($file['mime'], $stat['mime'])) {
1442 return $this->setError(elFinder
::ERROR_NOT_REPLACE
, $this->_path($test));
1444 // existed file is not writable
1445 if (!$stat['write']) {
1446 return $this->setError($err, $errpath, elFinder
::ERROR_PERM_DENIED
);
1448 // existed file locked or has locked child
1449 if (($locked = $this->closestByAttr($test, 'locked', true))) {
1450 return $this->setError(elFinder
::ERROR_LOCKED
, $this->_path($locked));
1452 // remove existed file
1453 if (!$this->remove($test)) {
1454 return $this->setError(elFinder
::ERROR_REPLACE
, $this->_path($test));
1457 $name = $this->uniqueName($destination, $name, ' ', false);
1461 // copy/move inside current volume
1462 if ($volume == $this) {
1463 $source = $this->decode($src);
1464 // do not copy into itself
1465 if ($this->_inpath($destination, $source)) {
1466 return $this->setError(elFinder
::ERROR_COPY_INTO_ITSELF
, $path);
1468 $method = $rmSrc ?
'move' : 'copy';
1470 return ($path = $this->$method($source, $destination, $name)) ?
$this->stat($path) : false;
1474 // copy/move from another volume
1475 if (!$this->options
['copyTo'] ||
!$volume->copyFromAllowed()) {
1476 return $this->setError(elFinder
::ERROR_COPY
, $errpath, elFinder
::ERROR_PERM_DENIED
);
1479 if (($path = $this->copyFrom($volume, $src, $destination, $name)) == false) {
1484 if ($volume->rm($src)) {
1485 $this->removed
[] = $file;
1487 return $this->setError(elFinder
::ERROR_MOVE
, $errpath, elFinder
::ERROR_RM_SRC
);
1490 return $this->stat($path);
1494 * Return file contents
1496 * @param string $hash file hash
1497 * @return string|false
1498 * @author Dmitry (dio) Levashov
1500 public function getContents($hash) {
1501 $file = $this->file($hash);
1504 return $this->setError(elFinder
::ERROR_FILE_NOT_FOUND
);
1507 if ($file['mime'] == 'directory') {
1508 return $this->setError(elFinder
::ERROR_NOT_FILE
);
1511 if (!$file['read']) {
1512 return $this->setError(elFinder
::ERROR_PERM_DENIED
);
1515 return $this->_getContents($this->decode($hash));
1519 * Put content in text file and return file info.
1521 * @param string $hash file hash
1522 * @param string $content new file content
1524 * @author Dmitry (dio) Levashov
1526 public function putContents($hash, $content) {
1527 if ($this->commandDisabled('edit')) {
1528 return $this->setError(elFinder
::ERROR_PERM_DENIED
);
1531 $path = $this->decode($hash);
1533 if (!($file = $this->file($hash))) {
1534 return $this->setError(elFinder
::ERROR_FILE_NOT_FOUND
);
1537 if (!$file['write']) {
1538 return $this->setError(elFinder
::ERROR_PERM_DENIED
);
1540 $this->clearcache();
1541 return $this->_filePutContents($path, $content) ?
$this->stat($path) : false;
1545 * Extract files from archive
1547 * @param string $hash archive hash
1548 * @return array|bool
1549 * @author Dmitry (dio) Levashov,
1550 * @author Alexey Sukhotin
1552 public function extract($hash) {
1553 if ($this->commandDisabled('extract')) {
1554 return $this->setError(elFinder
::ERROR_PERM_DENIED
);
1557 if (($file = $this->file($hash)) == false) {
1558 return $this->setError(elFinder
::ERROR_FILE_NOT_FOUND
);
1561 $archiver = isset($this->archivers
['extract'][$file['mime']])
1562 ?
$this->archivers
['extract'][$file['mime']]
1566 return $this->setError(elFinder
::ERROR_NOT_ARCHIVE
);
1569 $path = $this->decode($hash);
1570 $parent = $this->stat($this->_dirname($path));
1572 if (!$file['read'] ||
!$parent['write']) {
1573 return $this->setError(elFinder
::ERROR_PERM_DENIED
);
1575 $this->clearcache();
1576 return ($path = $this->_extract($path, $archiver)) ?
$this->stat($path) : false;
1580 * Add files to archive
1584 public function archive($hashes, $mime) {
1585 if ($this->commandDisabled('archive')) {
1586 return $this->setError(elFinder
::ERROR_PERM_DENIED
);
1589 $archiver = isset($this->archivers
['create'][$mime])
1590 ?
$this->archivers
['create'][$mime]
1594 return $this->setError(elFinder
::ERROR_ARCHIVE_TYPE
);
1599 foreach ($hashes as $hash) {
1600 if (($file = $this->file($hash)) == false) {
1601 return $this->error(elFinder
::ERROR_FILE_NOT_FOUND
, '#'+
$hash);
1603 if (!$file['read']) {
1604 return $this->error(elFinder
::ERROR_PERM_DENIED
);
1606 $path = $this->decode($hash);
1608 $dir = $this->_dirname($path);
1609 $stat = $this->stat($dir);
1610 if (!$stat['write']) {
1611 return $this->error(elFinder
::ERROR_PERM_DENIED
);
1615 $files[] = $this->_basename($path);
1618 $name = (count($files) == 1 ?
$files[0] : 'Archive').'.'.$archiver['ext'];
1619 $name = $this->uniqueName($dir, $name, '');
1620 $this->clearcache();
1621 return ($path = $this->_archive($dir, $files, $name, $archiver)) ?
$this->stat($path) : false;
1627 * @param string $hash image file
1628 * @param int $width new width
1629 * @param int $height new height
1630 * @param int $x X start poistion for crop
1631 * @param int $y Y start poistion for crop
1632 * @param string $mode action how to mainpulate image
1633 * @return array|false
1634 * @author Dmitry (dio) Levashov
1635 * @author Alexey Sukhotin
1637 * @author Troex Nevelin
1639 public function resize($hash, $width, $height, $x, $y, $mode = 'resize', $bg = '', $degree = 0) {
1640 if ($this->commandDisabled('resize')) {
1641 return $this->setError(elFinder
::ERROR_PERM_DENIED
);
1644 if (($file = $this->file($hash)) == false) {
1645 return $this->setError(elFinder
::ERROR_FILE_NOT_FOUND
);
1648 if (!$file['write'] ||
!$file['read']) {
1649 return $this->setError(elFinder
::ERROR_PERM_DENIED
);
1652 $path = $this->decode($hash);
1654 if (!$this->canResize($path, $file)) {
1655 return $this->setError(elFinder
::ERROR_UNSUPPORT_TYPE
);
1661 $result = $this->imgResize($path, $width, $height, true, true);
1665 $result = $this->imgCrop($path, $width, $height, $x, $y);
1669 $result = $this->imgSquareFit($path, $width, $height, 'center', 'middle', ($bg ?
$bg : $this->options
['tmbBgColor']));
1673 $result = $this->imgRotate($path, $degree, ($bg ?
$bg : $this->options
['tmbBgColor']));
1677 $result = $this->imgResize($path, $width, $height, false, true);
1682 if (!empty($file['tmb']) && $file['tmb'] != "1") {
1683 $this->rmTmb($file['tmb']);
1685 $this->clearcache();
1686 return $this->stat($path);
1695 * @param string $hash file hash
1697 * @author Dmitry (dio) Levashov
1699 public function rm($hash) {
1700 return $this->commandDisabled('rm')
1701 ?
array(elFinder
::ERROR_ACCESS_DENIED
)
1702 : $this->remove($this->decode($hash));
1708 * @param string $q search string
1709 * @param array $mimes
1711 * @author Dmitry (dio) Levashov
1713 public function search($q, $mimes) {
1714 return $this->doSearch($this->root
, $q, $mimes);
1718 * Return image dimensions
1720 * @param string $hash file hash
1722 * @author Dmitry (dio) Levashov
1724 public function dimensions($hash) {
1725 if (($file = $this->file($hash)) == false) {
1729 return $this->_dimensions($this->decode($hash), $file['mime']);
1733 * Save error message
1735 * @param array error
1737 * @author Dmitry(dio) Levashov
1739 protected function setError($error) {
1741 $this->error
= array();
1743 foreach (func_get_args() as $err) {
1744 if (is_array($err)) {
1745 $this->error
= array_merge($this->error
, $err);
1747 $this->error
[] = $err;
1751 // $this->error = is_array($error) ? $error : func_get_args();
1755 /*********************************************************************/
1757 /*********************************************************************/
1759 /***************** paths *******************/
1762 * Encode path into hash
1764 * @param string file path
1766 * @author Dmitry (dio) Levashov
1767 * @author Troex Nevelin
1769 protected function encode($path) {
1772 // cut ROOT from $path for security reason, even if hacker decodes the path he will not know the root
1773 $p = $this->_relpath($path);
1774 // if reqesting root dir $path will be empty, then assign '/' as we cannot leave it blank for crypt
1776 $p = DIRECTORY_SEPARATOR
;
1779 // TODO crypt path and return hash
1780 $hash = $this->crypt($p);
1781 // hash is used as id in HTML that means it must contain vaild chars
1782 // make base64 html safe and append prefix in begining
1783 $hash = strtr(base64_encode($hash), '+/=', '-_.');
1784 // remove dots '.' at the end, before it was '=' in base64
1785 $hash = rtrim($hash, '.');
1786 // append volume id to make hash unique
1787 return $this->id
.$hash;
1792 * Decode path from hash
1794 * @param string file hash
1796 * @author Dmitry (dio) Levashov
1797 * @author Troex Nevelin
1799 protected function decode($hash) {
1800 if (strpos($hash, $this->id
) === 0) {
1801 // cut volume id after it was prepended in encode
1802 $h = substr($hash, strlen($this->id
));
1803 // replace HTML safe base64 to normal
1804 $h = base64_decode(strtr($h, '-_.', '+/='));
1805 // TODO uncrypt hash and return path
1806 $path = $this->uncrypt($h);
1807 // append ROOT to path after it was cut in encode
1808 return $this->_abspath($path);//$this->root.($path == DIRECTORY_SEPARATOR ? '' : DIRECTORY_SEPARATOR.$path);
1813 * Return crypted path
1816 * @param string path
1818 * @author Dmitry (dio) Levashov
1820 protected function crypt($path) {
1825 * Return uncrypted path
1830 * @author Dmitry (dio) Levashov
1832 protected function uncrypt($hash) {
1837 * Validate file name based on $this->options['acceptedName'] regexp
1839 * @param string $name file name
1841 * @author Dmitry (dio) Levashov
1843 protected function nameAccepted($name) {
1844 if ($this->nameValidator
) {
1845 if (function_exists($this->nameValidator
)) {
1846 $f = $this->nameValidator
;
1849 return preg_match($this->nameValidator
, $name);
1855 * Return new unique name based on file name and suffix
1857 * @param string $path file path
1858 * @param string $suffix suffix append to name
1860 * @author Dmitry (dio) Levashov
1862 public function uniqueName($dir, $name, $suffix = ' copy', $checkNum=true) {
1865 if (preg_match('/\.((tar\.(gz|bz|bz2|z|lzo))|cpio\.gz|ps\.gz|xcf\.(gz|bz2)|[a-z0-9]{1,4})$/i', $name, $m)) {
1867 $name = substr($name, 0, strlen($name)-strlen($m[0]));
1870 if ($checkNum && preg_match('/('.$suffix.')(\d*)$/i', $name, $m)) {
1872 $name = substr($name, 0, strlen($name)-strlen($m[2]));
1879 while ($i <= $max) {
1880 $n = $name.($i > 0 ?
$i : '').$ext;
1882 if (!$this->stat($this->_joinPath($dir, $n))) {
1883 $this->clearcache();
1888 return $name.md5($dir).$ext;
1891 /*********************** file stat *********************/
1894 * Check file attribute
1896 * @param string $path file path
1897 * @param string $name attribute name (read|write|locked|hidden)
1898 * @param bool $val attribute value returned by file system
1900 * @author Dmitry (dio) Levashov
1902 protected function attr($path, $name, $val=false) {
1903 if (!isset($this->defaults
[$name])) {
1910 if ($this->access
) {
1911 if (is_array($this->access
)) {
1912 $obj = $this->access
[0];
1913 $method = $this->access
[1];
1914 $perm = $obj->{$method}($name, $path, $this->options
['accessControlData'], $this);
1916 $func = $this->access
;
1917 $perm = $func($name, $path, $this->options
['accessControlData'], $this);
1920 if ($perm !== null) {
1925 for ($i = 0, $c = count($this->attributes
); $i < $c; $i++
) {
1926 $attrs = $this->attributes
[$i];
1927 $p = $this->separator
.$this->_relpath($path);
1928 if (isset($attrs[$name]) && isset($attrs['pattern']) && preg_match($attrs['pattern'], $p)) {
1929 $perm = $attrs[$name];
1933 return $perm === null ?
$this->defaults
[$name] : !!$perm;
1939 * @param string $path file cache
1941 * @author Dmitry (dio) Levashov
1943 protected function stat($path) {
1944 return isset($this->cache
[$path])
1945 ?
$this->cache
[$path]
1946 : $this->updateCache($path, $this->_stat($path));
1950 * Put file stat in cache and return it
1952 * @param string $path file path
1953 * @param array $stat file stat
1955 * @author Dmitry (dio) Levashov
1957 protected function updateCache($path, $stat) {
1958 if (empty($stat) ||
!is_array($stat)) {
1959 return $this->cache
[$path] = array();
1962 $stat['hash'] = $this->encode($path);
1964 $root = $path == $this->root
;
1967 $stat['volumeid'] = $this->id
;
1968 if ($this->rootName
) {
1969 $stat['name'] = $this->rootName
;
1972 if (empty($stat['name'])) {
1973 $stat['name'] = $this->_basename($path);
1975 if (empty($stat['phash'])) {
1976 $stat['phash'] = $this->encode($this->_dirname($path));
1980 // fix name if required
1981 if ($this->options
['utf8fix'] && $this->options
['utf8patterns'] && $this->options
['utf8replace']) {
1982 $stat['name'] = json_decode(str_replace($this->options
['utf8patterns'], $this->options
['utf8replace'], json_encode($stat['name'])));
1986 if (empty($stat['mime'])) {
1987 $stat['mime'] = $this->mimetype($stat['name']);
1990 // @todo move dateformat to client
1991 $stat['date'] = isset($stat['ts'])
1992 ?
$this->formatDate($stat['ts'])
1995 if (!isset($stat['size'])) {
1996 $stat['size'] = 'unknown';
1999 $stat['read'] = intval($this->attr($path, 'read', isset($stat['read']) ?
!!$stat['read'] : false));
2000 $stat['write'] = intval($this->attr($path, 'write', isset($stat['write']) ?
!!$stat['write'] : false));
2002 $stat['locked'] = 1;
2003 } elseif ($this->attr($path, 'locked', !empty($stat['locked']))) {
2004 $stat['locked'] = 1;
2006 unset($stat['locked']);
2010 unset($stat['hidden']);
2011 } elseif ($this->attr($path, 'hidden', !empty($stat['hidden']))
2012 ||
!$this->mimeAccepted($stat['mime'])) {
2013 $stat['hidden'] = $root ?
0 : 1;
2015 unset($stat['hidden']);
2018 if ($stat['read'] && empty($stat['hidden'])) {
2020 if ($stat['mime'] == 'directory') {
2021 // for dir - check for subdirs
2023 if ($this->options
['checkSubfolders']) {
2024 if (isset($stat['dirs'])) {
2025 if ($stat['dirs']) {
2028 unset($stat['dirs']);
2030 } elseif (!empty($stat['alias']) && !empty($stat['target'])) {
2031 $stat['dirs'] = isset($this->cache
[$stat['target']])
2032 ?
intval(isset($this->cache
[$stat['target']]['dirs']))
2033 : $this->_subdirs($stat['target']);
2035 } elseif ($this->_subdirs($path)) {
2042 // for files - check for thumbnails
2043 $p = isset($stat['target']) ?
$stat['target'] : $path;
2044 if ($this->tmbURL
&& !isset($stat['tmb']) && $this->canCreateTmb($p, $stat)) {
2045 $tmb = $this->gettmb($p, $stat);
2046 $stat['tmb'] = $tmb ?
$tmb : 1;
2052 if (!empty($stat['alias']) && !empty($stat['target'])) {
2053 $stat['thash'] = $this->encode($stat['target']);
2054 unset($stat['target']);
2057 return $this->cache
[$path] = $stat;
2061 * Get stat for folder content and put in cache
2063 * @param string $path
2065 * @author Dmitry (dio) Levashov
2067 protected function cacheDir($path) {
2068 $this->dirsCache
[$path] = array();
2070 foreach ($this->_scandir($path) as $p) {
2071 if (($stat = $this->stat($p)) && empty($stat['hidden'])) {
2072 $this->dirsCache
[$path][] = $p;
2081 * @author Dmitry (dio) Levashov
2083 protected function clearcache() {
2084 $this->cache
= $this->dirsCache
= array();
2088 * Return file mimetype
2090 * @param string $path file path
2092 * @author Dmitry (dio) Levashov
2094 protected function mimetype($path) {
2097 if ($this->mimeDetect
== 'finfo') {
2098 $type = @finfo_file
($this->finfo
, $path);
2099 } elseif ($type == 'mime_content_type') {
2100 $type = mime_content_type($path);
2102 $type = elFinderVolumeDriver
::mimetypeInternalDetect($path);
2105 $type = explode(';', $type);
2106 $type = trim($type[0]);
2108 if ($type == 'application/x-empty') {
2109 // finfo return this mime for empty files
2110 $type = 'text/plain';
2111 } elseif ($type == 'application/x-zip') {
2112 // http://elrte.org/redmine/issues/163
2113 $type = 'application/zip';
2116 return $type == 'unknown' && $this->mimeDetect
!= 'internal'
2117 ? elFinderVolumeDriver
::mimetypeInternalDetect($path)
2123 * Detect file mimetype using "internal" method
2125 * @param string $path file path
2127 * @author Dmitry (dio) Levashov
2129 static protected function mimetypeInternalDetect($path) {
2130 $pinfo = pathinfo($path);
2131 $ext = isset($pinfo['extension']) ?
strtolower($pinfo['extension']) : '';
2132 return isset(elFinderVolumeDriver
::$mimetypes[$ext]) ? elFinderVolumeDriver
::$mimetypes[$ext] : 'unknown';
2137 * Return file/total directory size
2139 * @param string $path file path
2141 * @author Dmitry (dio) Levashov
2143 protected function countSize($path) {
2144 $stat = $this->stat($path);
2146 if (empty($stat) ||
!$stat['read'] ||
!empty($stat['hidden'])) {
2150 if ($stat['mime'] != 'directory') {
2151 return $stat['size'];
2154 $subdirs = $this->options
['checkSubfolders'];
2155 $this->options
['checkSubfolders'] = true;
2157 foreach ($this->getScandir($path) as $stat) {
2158 $size = $stat['mime'] == 'directory' && $stat['read']
2159 ?
$this->countSize($this->_joinPath($path, $stat['name']))
2165 $this->options
['checkSubfolders'] = $subdirs;
2170 * Return true if all mimes is directory or files
2172 * @param string $mime1 mimetype
2173 * @param string $mime2 mimetype
2175 * @author Dmitry (dio) Levashov
2177 protected function isSameType($mime1, $mime2) {
2178 return ($mime1 == 'directory' && $mime1 == $mime2) ||
($mime1 != 'directory' && $mime2 != 'directory');
2182 * If file has required attr == $val - return file path,
2183 * If dir has child with has required attr == $val - return child path
2185 * @param string $path file path
2186 * @param string $attr attribute name
2187 * @param bool $val attribute value
2188 * @return string|false
2189 * @author Dmitry (dio) Levashov
2191 protected function closestByAttr($path, $attr, $val) {
2192 $stat = $this->stat($path);
2198 $v = isset($stat[$attr]) ?
$stat[$attr] : false;
2204 return $stat['mime'] == 'directory'
2205 ?
$this->childsByAttr($path, $attr, $val)
2210 * Return first found children with required attr == $val
2212 * @param string $path file path
2213 * @param string $attr attribute name
2214 * @param bool $val attribute value
2215 * @return string|false
2216 * @author Dmitry (dio) Levashov
2218 protected function childsByAttr($path, $attr, $val) {
2219 foreach ($this->_scandir($path) as $p) {
2220 if (($_p = $this->closestByAttr($p, $attr, $val)) != false) {
2227 /***************** get content *******************/
2230 * Return required dir's files info.
2231 * If onlyMimes is set - return only dirs and files of required mimes
2233 * @param string $path dir path
2235 * @author Dmitry (dio) Levashov
2237 protected function getScandir($path) {
2240 !isset($this->dirsCache
[$path]) && $this->cacheDir($path);
2242 foreach ($this->dirsCache
[$path] as $p) {
2243 if (($stat = $this->stat($p)) && empty($stat['hidden'])) {
2253 * Return subdirs tree
2255 * @param string $path parent dir path
2256 * @param int $deep tree deep
2258 * @author Dmitry (dio) Levashov
2260 protected function gettree($path, $deep, $exclude='') {
2263 !isset($this->dirsCache
[$path]) && $this->cacheDir($path);
2265 foreach ($this->dirsCache
[$path] as $p) {
2266 $stat = $this->stat($p);
2268 if ($stat && empty($stat['hidden']) && $path != $exclude && $stat['mime'] == 'directory') {
2270 if ($deep > 0 && !empty($stat['dirs'])) {
2271 $dirs = array_merge($dirs, $this->gettree($p, $deep-1));
2280 * Recursive files search
2282 * @param string $path dir path
2283 * @param string $q search string
2284 * @param array $mimes
2286 * @author Dmitry (dio) Levashov
2288 protected function doSearch($path, $q, $mimes) {
2291 foreach($this->_scandir($path) as $p) {
2292 $stat = $this->stat($p);
2294 if (!$stat) { // invalid links
2298 if (!empty($stat['hidden']) ||
!$this->mimeAccepted($stat['mime'])) {
2302 $name = $stat['name'];
2304 if ($this->stripos($name, $q) !== false) {
2305 $stat['path'] = $this->_path($p);
2306 if ($this->URL
&& !isset($stat['url'])) {
2307 $stat['url'] = $this->URL
. str_replace($this->separator
, '/', substr($p, strlen($this->root
) +
1));
2312 if ($stat['mime'] == 'directory' && $stat['read'] && !isset($stat['alias'])) {
2313 $result = array_merge($result, $this->doSearch($p, $q, $mimes));
2320 /********************** manuipulations ******************/
2323 * Copy file/recursive copy dir only in current volume.
2324 * Return new file path or false.
2326 * @param string $src source path
2327 * @param string $dst destination dir path
2328 * @param string $name new file name (optionaly)
2329 * @return string|false
2330 * @author Dmitry (dio) Levashov
2332 protected function copy($src, $dst, $name) {
2333 $srcStat = $this->stat($src);
2334 $this->clearcache();
2336 if (!empty($srcStat['thash'])) {
2337 $target = $this->decode($srcStat['thash']);
2338 $stat = $this->stat($target);
2339 $this->clearcache();
2340 return $stat && $this->_symlink($target, $dst, $name)
2341 ?
$this->_joinPath($dst, $name)
2342 : $this->setError(elFinder
::ERROR_COPY
, $this->_path($src));
2345 if ($srcStat['mime'] == 'directory') {
2346 $test = $this->stat($this->_joinPath($dst, $name));
2348 if (($test && $test['mime'] != 'directory') ||
!$this->_mkdir($dst, $name)) {
2349 return $this->setError(elFinder
::ERROR_COPY
, $this->_path($src));
2352 $dst = $this->_joinPath($dst, $name);
2354 foreach ($this->getScandir($src) as $stat) {
2355 if (empty($stat['hidden'])) {
2356 $name = $stat['name'];
2357 if (!$this->copy($this->_joinPath($src, $name), $dst, $name)) {
2362 $this->clearcache();
2366 return $this->_copy($src, $dst, $name)
2367 ?
$this->_joinPath($dst, $name)
2368 : $this->setError(elFinder
::ERROR_COPY
, $this->_path($src));
2373 * Return new file path or false.
2375 * @param string $src source path
2376 * @param string $dst destination dir path
2377 * @param string $name new file name
2378 * @return string|false
2379 * @author Dmitry (dio) Levashov
2381 protected function move($src, $dst, $name) {
2382 $stat = $this->stat($src);
2383 $stat['realpath'] = $src;
2384 $this->clearcache();
2386 if ($this->_move($src, $dst, $name)) {
2387 $this->removed
[] = $stat;
2388 return $this->_joinPath($dst, $name);
2391 return $this->setError(elFinder
::ERROR_MOVE
, $this->_path($src));
2395 * Copy file from another volume.
2396 * Return new file path or false.
2398 * @param Object $volume source volume
2399 * @param string $src source file hash
2400 * @param string $destination destination dir path
2401 * @param string $name file name
2402 * @return string|false
2403 * @author Dmitry (dio) Levashov
2405 protected function copyFrom($volume, $src, $destination, $name) {
2407 if (($source = $volume->file($src)) == false) {
2408 return $this->setError(elFinder
::ERROR_COPY
, '#'.$src, $volume->error());
2411 $errpath = $volume->path($src);
2413 if (!$this->nameAccepted($source['name'])) {
2414 return $this->setError(elFinder
::ERROR_COPY
, $errpath, elFinder
::ERROR_INVALID_NAME
);
2417 if (!$source['read']) {
2418 return $this->setError(elFinder
::ERROR_COPY
, $errpath, elFinder
::ERROR_PERM_DENIED
);
2421 if ($source['mime'] == 'directory') {
2422 $stat = $this->stat($this->_joinPath($destination, $name));
2423 $this->clearcache();
2424 if ((!$stat ||
$stat['mime'] != 'directory') && !$this->_mkdir($destination, $name)) {
2425 return $this->setError(elFinder
::ERROR_COPY
, $errpath);
2428 $path = $this->_joinPath($destination, $name);
2430 foreach ($volume->scandir($src) as $entr) {
2431 if (!$this->copyFrom($volume, $entr['hash'], $path, $entr['name'])) {
2437 $mime = $source['mime'];
2439 if (strpos($mime, 'image') === 0 && ($dim = $volume->dimensions($src))) {
2440 $s = explode('x', $dim);
2445 if (($fp = $volume->open($src)) == false
2446 ||
($path = $this->_save($fp, $destination, $name, $mime, $w, $h)) == false) {
2447 $fp && $volume->close($fp, $src);
2448 return $this->setError(elFinder
::ERROR_COPY
, $errpath);
2450 $volume->close($fp, $src);
2457 * Remove file/ recursive remove dir
2459 * @param string $path file path
2460 * @param bool $force try to remove even if file locked
2462 * @author Dmitry (dio) Levashov
2464 protected function remove($path, $force = false) {
2465 $stat = $this->stat($path);
2466 $stat['realpath'] = $path;
2467 if (!empty($stat['tmb']) && $stat['tmb'] != "1") {
2468 $this->rmTmb($stat['tmb']);
2470 $this->clearcache();
2473 return $this->setError(elFinder
::ERROR_RM
, $this->_path($path), elFinder
::ERROR_FILE_NOT_FOUND
);
2476 if (!$force && !empty($stat['locked'])) {
2477 return $this->setError(elFinder
::ERROR_LOCKED
, $this->_path($path));
2480 if ($stat['mime'] == 'directory') {
2481 foreach ($this->_scandir($path) as $p) {
2482 $name = $this->_basename($p);
2483 if ($name != '.' && $name != '..' && !$this->remove($p)) {
2487 if (!$this->_rmdir($path)) {
2488 return $this->setError(elFinder
::ERROR_RM
, $this->_path($path));
2492 if (!$this->_unlink($path)) {
2493 return $this->setError(elFinder
::ERROR_RM
, $this->_path($path));
2497 $this->removed
[] = $stat;
2502 /************************* thumbnails **************************/
2505 * Return thumbnail file name for required file
2507 * @param array $stat file stat
2509 * @author Dmitry (dio) Levashov
2511 protected function tmbname($stat) {
2512 return $stat['hash'].$stat['ts'].'.png';
2516 * Return thumnbnail name if exists
2518 * @param string $path file path
2519 * @param array $stat file stat
2520 * @return string|false
2521 * @author Dmitry (dio) Levashov
2523 protected function gettmb($path, $stat) {
2524 if ($this->tmbURL
&& $this->tmbPath
) {
2525 // file itself thumnbnail
2526 if (strpos($path, $this->tmbPath
) === 0) {
2527 return basename($path);
2530 $name = $this->tmbname($stat);
2531 if (file_exists($this->tmbPath
.DIRECTORY_SEPARATOR
.$name)) {
2539 * Return true if thumnbnail for required file can be created
2541 * @param string $path thumnbnail path
2542 * @param array $stat file stat
2543 * @return string|bool
2544 * @author Dmitry (dio) Levashov
2546 protected function canCreateTmb($path, $stat) {
2547 return $this->tmbPathWritable
2548 && strpos($path, $this->tmbPath
) === false // do not create thumnbnail for thumnbnail
2550 && strpos($stat['mime'], 'image') === 0
2551 && ($this->imgLib
== 'gd' ?
$stat['mime'] == 'image/jpeg' ||
$stat['mime'] == 'image/png' ||
$stat['mime'] == 'image/gif' : true);
2555 * Return true if required file can be resized.
2556 * By default - the same as canCreateTmb
2558 * @param string $path thumnbnail path
2559 * @param array $stat file stat
2560 * @return string|bool
2561 * @author Dmitry (dio) Levashov
2563 protected function canResize($path, $stat) {
2564 return $this->canCreateTmb($path, $stat);
2568 * Create thumnbnail and return it's URL on success
2570 * @param string $path file path
2571 * @param string $mime file mime type
2572 * @return string|false
2573 * @author Dmitry (dio) Levashov
2575 protected function createTmb($path, $stat) {
2576 if (!$stat ||
!$this->canCreateTmb($path, $stat)) {
2580 $name = $this->tmbname($stat);
2581 $tmb = $this->tmbPath
.DIRECTORY_SEPARATOR
.$name;
2583 // copy image into tmbPath so some drivers does not store files on local fs
2584 if (($src = $this->_fopen($path, 'rb')) == false) {
2588 if (($trg = fopen($tmb, 'wb')) == false) {
2589 $this->_fclose($src, $path);
2593 while (!feof($src)) {
2594 fwrite($trg, fread($src, 8192));
2597 $this->_fclose($src, $path);
2602 $tmbSize = $this->tmbSize
;
2604 if (($s = getimagesize($tmb)) == false) {
2608 /* If image smaller or equal thumbnail size - just fitting to thumbnail square */
2609 if ($s[0] <= $tmbSize && $s[1] <= $tmbSize) {
2610 $result = $this->imgSquareFit($tmb, $tmbSize, $tmbSize, 'center', 'middle', $this->options
['tmbBgColor'], 'png' );
2614 if ($this->options
['tmbCrop']) {
2616 /* Resize and crop if image bigger than thumbnail */
2617 if (!(($s[0] > $tmbSize && $s[1] <= $tmbSize) ||
($s[0] <= $tmbSize && $s[1] > $tmbSize) ) ||
($s[0] > $tmbSize && $s[1] > $tmbSize)) {
2618 $result = $this->imgResize($tmb, $tmbSize, $tmbSize, true, false, 'png');
2621 if (($s = getimagesize($tmb)) != false) {
2622 $x = $s[0] > $tmbSize ?
intval(($s[0] - $tmbSize)/2) : 0;
2623 $y = $s[1] > $tmbSize ?
intval(($s[1] - $tmbSize)/2) : 0;
2624 $result = $this->imgCrop($tmb, $tmbSize, $tmbSize, $x, $y, 'png');
2628 $result = $this->imgResize($tmb, $tmbSize, $tmbSize, true, true, $this->imgLib
, 'png');
2629 $result = $this->imgSquareFit($tmb, $tmbSize, $tmbSize, 'center', 'middle', $this->options
['tmbBgColor'], 'png' );
2644 * @param string $path image file
2645 * @param int $width new width
2646 * @param int $height new height
2647 * @param bool $keepProportions crop image
2648 * @param bool $resizeByBiggerSide resize image based on bigger side if true
2649 * @param string $destformat image destination format
2650 * @return string|false
2651 * @author Dmitry (dio) Levashov
2652 * @author Alexey Sukhotin
2654 protected function imgResize($path, $width, $height, $keepProportions = false, $resizeByBiggerSide = true, $destformat = null) {
2655 if (($s = @getimagesize
($path)) == false) {
2661 list($size_w, $size_h) = array($width, $height);
2663 if ($keepProportions == true) {
2665 list($orig_w, $orig_h, $new_w, $new_h) = array($s[0], $s[1], $width, $height);
2667 /* Calculating image scale width and height */
2668 $xscale = $orig_w / $new_w;
2669 $yscale = $orig_h / $new_h;
2671 /* Resizing by biggest side */
2673 if ($resizeByBiggerSide) {
2675 if ($orig_w > $orig_h) {
2676 $size_h = $orig_h * $width / $orig_w;
2679 $size_w = $orig_w * $height / $orig_h;
2684 if ($orig_w > $orig_h) {
2685 $size_w = $orig_w * $height / $orig_h;
2688 $size_h = $orig_h * $width / $orig_w;
2694 switch ($this->imgLib
) {
2698 $img = new imagick($path);
2699 } catch (Exception
$e) {
2704 $img->resizeImage($size_w, $size_h, Imagick
::FILTER_LANCZOS
, true);
2706 $result = $img->writeImage($path);
2708 return $result ?
$path : false;
2713 if ($s['mime'] == 'image/jpeg') {
2714 $img = imagecreatefromjpeg($path);
2715 } elseif ($s['mime'] == 'image/png') {
2716 $img = imagecreatefrompng($path);
2717 } elseif ($s['mime'] == 'image/gif') {
2718 $img = imagecreatefromgif($path);
2719 } elseif ($s['mime'] == 'image/xbm') {
2720 $img = imagecreatefromxbm($path);
2723 if ($img && false != ($tmp = imagecreatetruecolor($size_w, $size_h))) {
2724 if (!imagecopyresampled($tmp, $img, 0, 0, 0, 0, $size_w, $size_h, $s[0], $s[1])) {
2728 if ($destformat == 'jpg' ||
($destformat == null && $s['mime'] == 'image/jpeg')) {
2729 $result = imagejpeg($tmp, $path, 100);
2730 } else if ($destformat == 'gif' ||
($destformat == null && $s['mime'] == 'image/gif')) {
2731 $result = imagegif($tmp, $path, 7);
2733 $result = imagepng($tmp, $path, 7);
2739 return $result ?
$path : false;
2751 * @param string $path image file
2752 * @param int $width crop width
2753 * @param int $height crop height
2754 * @param bool $x crop left offset
2755 * @param bool $y crop top offset
2756 * @param string $destformat image destination format
2757 * @return string|false
2758 * @author Dmitry (dio) Levashov
2759 * @author Alexey Sukhotin
2761 protected function imgCrop($path, $width, $height, $x, $y, $destformat = null) {
2762 if (($s = @getimagesize
($path)) == false) {
2768 switch ($this->imgLib
) {
2772 $img = new imagick($path);
2773 } catch (Exception
$e) {
2778 $img->cropImage($width, $height, $x, $y);
2780 $result = $img->writeImage($path);
2782 return $result ?
$path : false;
2787 if ($s['mime'] == 'image/jpeg') {
2788 $img = imagecreatefromjpeg($path);
2789 } elseif ($s['mime'] == 'image/png') {
2790 $img = imagecreatefrompng($path);
2791 } elseif ($s['mime'] == 'image/gif') {
2792 $img = imagecreatefromgif($path);
2793 } elseif ($s['mime'] == 'image/xbm') {
2794 $img = imagecreatefromxbm($path);
2797 if ($img && false != ($tmp = imagecreatetruecolor($width, $height))) {
2799 if (!imagecopy($tmp, $img, 0, 0, $x, $y, $width, $height)) {
2803 if ($destformat == 'jpg' ||
($destformat == null && $s['mime'] == 'image/jpeg')) {
2804 $result = imagejpeg($tmp, $path, 100);
2805 } else if ($destformat == 'gif' ||
($destformat == null && $s['mime'] == 'image/gif')) {
2806 $result = imagegif($tmp, $path, 7);
2808 $result = imagepng($tmp, $path, 7);
2814 return $result ?
$path : false;
2824 * Put image to square
2826 * @param string $path image file
2827 * @param int $width square width
2828 * @param int $height square height
2829 * @param int $align reserved
2830 * @param int $valign reserved
2831 * @param string $bgcolor square background color in #rrggbb format
2832 * @param string $destformat image destination format
2833 * @return string|false
2834 * @author Dmitry (dio) Levashov
2835 * @author Alexey Sukhotin
2837 protected function imgSquareFit($path, $width, $height, $align = 'center', $valign = 'middle', $bgcolor = '#0000ff', $destformat = null) {
2838 if (($s = @getimagesize
($path)) == false) {
2844 /* Coordinates for image over square aligning */
2845 $y = ceil(abs($height - $s[1]) / 2);
2846 $x = ceil(abs($width - $s[0]) / 2);
2848 switch ($this->imgLib
) {
2851 $img = new imagick($path);
2852 } catch (Exception
$e) {
2856 $img1 = new Imagick();
2857 $img1->newImage($width, $height, new ImagickPixel($bgcolor));
2858 $img1->setImageColorspace($img->getImageColorspace());
2859 $img1->setImageFormat($destformat != null ?
$destformat : $img->getFormat());
2860 $img1->compositeImage( $img, imagick
::COMPOSITE_OVER
, $x, $y );
2861 $result = $img1->writeImage($path);
2862 return $result ?
$path : false;
2867 if ($s['mime'] == 'image/jpeg') {
2868 $img = imagecreatefromjpeg($path);
2869 } elseif ($s['mime'] == 'image/png') {
2870 $img = imagecreatefrompng($path);
2871 } elseif ($s['mime'] == 'image/gif') {
2872 $img = imagecreatefromgif($path);
2873 } elseif ($s['mime'] == 'image/xbm') {
2874 $img = imagecreatefromxbm($path);
2877 if ($img && false != ($tmp = imagecreatetruecolor($width, $height))) {
2879 if ($bgcolor == 'transparent') {
2880 list($r, $g, $b) = array(0, 0, 255);
2882 list($r, $g, $b) = sscanf($bgcolor, "#%02x%02x%02x");
2885 $bgcolor1 = imagecolorallocate($tmp, $r, $g, $b);
2887 if ($bgcolor == 'transparent') {
2888 $bgcolor1 = imagecolortransparent($tmp, $bgcolor1);
2891 imagefill($tmp, 0, 0, $bgcolor1);
2893 if (!imagecopy($tmp, $img, $x, $y, 0, 0, $s[0], $s[1])) {
2897 if ($destformat == 'jpg' ||
($destformat == null && $s['mime'] == 'image/jpeg')) {
2898 $result = imagejpeg($tmp, $path, 100);
2899 } else if ($destformat == 'gif' ||
($destformat == null && $s['mime'] == 'image/gif')) {
2900 $result = imagegif($tmp, $path, 7);
2902 $result = imagepng($tmp, $path, 7);
2908 return $result ?
$path : false;
2919 * @param string $path image file
2920 * @param int $degree rotete degrees
2921 * @param string $bgcolor square background color in #rrggbb format
2922 * @param string $destformat image destination format
2923 * @return string|false
2925 * @author Troex Nevelin
2927 protected function imgRotate($path, $degree, $bgcolor = '#ffffff', $destformat = null) {
2928 if (($s = @getimagesize
($path)) == false) {
2934 switch ($this->imgLib
) {
2937 $img = new imagick($path);
2938 } catch (Exception
$e) {
2942 $img->rotateImage(new ImagickPixel($bgcolor), $degree);
2943 $result = $img->writeImage($path);
2944 return $result ?
$path : false;
2949 if ($s['mime'] == 'image/jpeg') {
2950 $img = imagecreatefromjpeg($path);
2951 } elseif ($s['mime'] == 'image/png') {
2952 $img = imagecreatefrompng($path);
2953 } elseif ($s['mime'] == 'image/gif') {
2954 $img = imagecreatefromgif($path);
2955 } elseif ($s['mime'] == 'image/xbm') {
2956 $img = imagecreatefromxbm($path);
2959 $degree = 360 - $degree;
2960 list($r, $g, $b) = sscanf($bgcolor, "#%02x%02x%02x");
2961 $bgcolor = imagecolorallocate($img, $r, $g, $b);
2962 $tmp = imageRotate($img, $degree, (int)$bgcolor);
2964 if ($destformat == 'jpg' ||
($destformat == null && $s['mime'] == 'image/jpeg')) {
2965 $result = imagejpeg($tmp, $path, 100);
2966 } else if ($destformat == 'gif' ||
($destformat == null && $s['mime'] == 'image/gif')) {
2967 $result = imagegif($tmp, $path, 7);
2969 $result = imagepng($tmp, $path, 7);
2975 return $result ?
$path : false;
2984 * Execute shell command
2986 * @param string $command command line
2987 * @param array $output stdout strings
2988 * @param array $return_var process exit code
2989 * @param array $error_output stderr strings
2990 * @return int exit code
2991 * @author Alexey Sukhotin
2993 protected function procExec($command , array &$output = null, &$return_var = -1, array &$error_output = null) {
2995 $descriptorspec = array(
2996 0 => array("pipe", "r"), // stdin
2997 1 => array("pipe", "w"), // stdout
2998 2 => array("pipe", "w") // stderr
3001 $process = proc_open($command, $descriptorspec, $pipes, null, null);
3003 if (is_resource($process)) {
3010 $output = stream_get_contents($pipes[1]);
3011 $error_output = stream_get_contents($pipes[2]);
3015 $return_var = proc_close($process);
3027 * @param string $path file path
3029 * @author Dmitry (dio) Levashov
3031 protected function rmTmb($tmb) {
3032 $tmb = $this->tmbPath
.DIRECTORY_SEPARATOR
.$tmb;
3033 file_exists($tmb) && @unlink
($tmb);
3037 /*********************** misc *************************/
3040 * Return smart formatted date
3042 * @param int $ts file timestamp
3044 * @author Dmitry (dio) Levashov
3046 protected function formatDate($ts) {
3047 if ($ts > $this->today
) {
3048 return 'Today '.date($this->options
['timeFormat'], $ts);
3051 if ($ts > $this->yesterday
) {
3052 return 'Yesterday '.date($this->options
['timeFormat'], $ts);
3055 return date($this->options
['dateFormat'], $ts);
3059 * Find position of first occurrence of string in a string with multibyte support
3061 * @param string $haystack The string being checked.
3062 * @param string $needle The string to find in haystack.
3063 * @param int $offset The search offset. If it is not specified, 0 is used.
3065 * @author Alexey Sukhotin
3067 protected function stripos($haystack , $needle , $offset = 0) {
3068 if (function_exists('mb_stripos')) {
3069 return mb_stripos($haystack , $needle , $offset);
3070 } else if (function_exists('mb_strtolower') && function_exists('mb_strpos')) {
3071 return mb_strpos(mb_strtolower($haystack), mb_strtolower($needle), $offset);
3073 return stripos($haystack , $needle , $offset);
3076 /**==================================* abstract methods *====================================**/
3078 /*********************** paths/urls *************************/
3081 * Return parent directory path
3083 * @param string $path file path
3085 * @author Dmitry (dio) Levashov
3087 abstract protected function _dirname($path);
3092 * @param string $path file path
3094 * @author Dmitry (dio) Levashov
3096 abstract protected function _basename($path);
3099 * Join dir name and file name and return full path.
3100 * Some drivers (db) use int as path - so we give to concat path to driver itself
3102 * @param string $dir dir path
3103 * @param string $name file name
3105 * @author Dmitry (dio) Levashov
3107 abstract protected function _joinPath($dir, $name);
3110 * Return normalized path
3112 * @param string $path file path
3114 * @author Dmitry (dio) Levashov
3116 abstract protected function _normpath($path);
3119 * Return file path related to root dir
3121 * @param string $path file path
3123 * @author Dmitry (dio) Levashov
3125 abstract protected function _relpath($path);
3128 * Convert path related to root dir into real path
3130 * @param string $path rel file path
3132 * @author Dmitry (dio) Levashov
3134 abstract protected function _abspath($path);
3137 * Return fake path started from root dir.
3138 * Required to show path on client side.
3140 * @param string $path file path
3142 * @author Dmitry (dio) Levashov
3144 abstract protected function _path($path);
3147 * Return true if $path is children of $parent
3149 * @param string $path path to check
3150 * @param string $parent parent path
3152 * @author Dmitry (dio) Levashov
3154 abstract protected function _inpath($path, $parent);
3157 * Return stat for given path.
3158 * Stat contains following fields:
3159 * - (int) size file size in b. required
3160 * - (int) ts file modification time in unix time. required
3161 * - (string) mime mimetype. required for folders, others - optionally
3162 * - (bool) read read permissions. required
3163 * - (bool) write write permissions. required
3164 * - (bool) locked is object locked. optionally
3165 * - (bool) hidden is object hidden. optionally
3166 * - (string) alias for symlinks - link target path relative to root path. optionally
3167 * - (string) target for symlinks - link target path. optionally
3169 * If file does not exists - returns empty array or false.
3171 * @param string $path file path
3172 * @return array|false
3173 * @author Dmitry (dio) Levashov
3175 abstract protected function _stat($path);
3178 /***************** file stat ********************/
3182 * Return true if path is dir and has at least one childs directory
3184 * @param string $path dir path
3186 * @author Dmitry (dio) Levashov
3188 abstract protected function _subdirs($path);
3191 * Return object width and height
3192 * Ususaly used for images, but can be realize for video etc...
3194 * @param string $path file path
3195 * @param string $mime file mime type
3197 * @author Dmitry (dio) Levashov
3199 abstract protected function _dimensions($path, $mime);
3201 /******************** file/dir content *********************/
3204 * Return files list in directory
3206 * @param string $path dir path
3208 * @author Dmitry (dio) Levashov
3210 abstract protected function _scandir($path);
3213 * Open file and return file pointer
3215 * @param string $path file path
3216 * @param bool $write open file for writing
3217 * @return resource|false
3218 * @author Dmitry (dio) Levashov
3220 abstract protected function _fopen($path, $mode="rb");
3225 * @param resource $fp file pointer
3226 * @param string $path file path
3228 * @author Dmitry (dio) Levashov
3230 abstract protected function _fclose($fp, $path='');
3232 /******************** file/dir manipulations *************************/
3235 * Create dir and return created dir path or false on failed
3237 * @param string $path parent dir path
3238 * @param string $name new directory name
3239 * @return string|bool
3240 * @author Dmitry (dio) Levashov
3242 abstract protected function _mkdir($path, $name);
3245 * Create file and return it's path or false on failed
3247 * @param string $path parent dir path
3248 * @param string $name new file name
3249 * @return string|bool
3250 * @author Dmitry (dio) Levashov
3252 abstract protected function _mkfile($path, $name);
3257 * @param string $source file to link to
3258 * @param string $targetDir folder to create link in
3259 * @param string $name symlink name
3261 * @author Dmitry (dio) Levashov
3263 abstract protected function _symlink($source, $targetDir, $name);
3266 * Copy file into another file (only inside one volume)
3268 * @param string $source source file path
3269 * @param string $target target dir path
3270 * @param string $name file name
3272 * @author Dmitry (dio) Levashov
3274 abstract protected function _copy($source, $targetDir, $name);
3277 * Move file into another parent dir.
3278 * Return new file path or false.
3280 * @param string $source source file path
3281 * @param string $target target dir path
3282 * @param string $name file name
3283 * @return string|bool
3284 * @author Dmitry (dio) Levashov
3286 abstract protected function _move($source, $targetDir, $name);
3291 * @param string $path file path
3293 * @author Dmitry (dio) Levashov
3295 abstract protected function _unlink($path);
3300 * @param string $path dir path
3302 * @author Dmitry (dio) Levashov
3304 abstract protected function _rmdir($path);
3307 * Create new file and write into it from file pointer.
3308 * Return new file path or false on error.
3310 * @param resource $fp file pointer
3311 * @param string $dir target dir path
3312 * @param string $name file name
3313 * @return bool|string
3314 * @author Dmitry (dio) Levashov
3316 abstract protected function _save($fp, $dir, $name, $mime, $w, $h);
3321 * @param string $path file path
3322 * @return string|false
3323 * @author Dmitry (dio) Levashov
3325 abstract protected function _getContents($path);
3328 * Write a string to a file
3330 * @param string $path file path
3331 * @param string $content new file content
3333 * @author Dmitry (dio) Levashov
3335 abstract protected function _filePutContents($path, $content);
3338 * Extract files from archive
3340 * @param string $path file path
3341 * @param array $arc archiver options
3343 * @author Dmitry (dio) Levashov,
3344 * @author Alexey Sukhotin
3346 abstract protected function _extract($path, $arc);
3349 * Create archive and return its path
3351 * @param string $dir target dir
3352 * @param array $files files names list
3353 * @param string $name archive name
3354 * @param array $arc archiver options
3355 * @return string|bool
3356 * @author Dmitry (dio) Levashov,
3357 * @author Alexey Sukhotin
3359 abstract protected function _archive($dir, $files, $name, $arc);
3362 * Detect available archivers
3365 * @author Dmitry (dio) Levashov,
3366 * @author Alexey Sukhotin
3368 abstract protected function _checkArchivers();