Detect file mimetype using "internal" method or Loading mime.types with $path = ''
protected static mimetypeInternalDetect ( string $path = '' ) : string | ||
$path | string | file path |
return | string |
/** * Return file mimetype * * @param string $path file path * @return string * @author Dmitry (dio) Levashov **/ protected function mimetype($path) { $type = ''; if ($this->mimeDetect == 'finfo') { $type = @finfo_file($this->finfo, $path); } elseif ($type == 'mime_content_type') { $type = mime_content_type($path); } else { $type = elFinderVolumeDriver::mimetypeInternalDetect($path); } $type = explode(';', $type); $type = trim($type[0]); if ($type == 'application/x-empty') { // finfo return this mime for empty files $type = 'text/plain'; } elseif ($type == 'application/x-zip') { // http://elrte.org/redmine/issues/163 $type = 'application/zip'; } return $type == 'unknown' && $this->mimeDetect != 'internal' ? elFinderVolumeDriver::mimetypeInternalDetect($path) : $type; }
/** * Return file mimetype * * @param string $path file path * @return string * @author Dmitry (dio) Levashov **/ protected function mimetype($path, $name = '') { $type = ''; if ($name === '') { $name = $path; } $ext = false === ($pos = strrpos($name, '.')) ? '' : substr($name, $pos + 1); if ($this->mimeDetect == 'finfo') { if ($type = @finfo_file($this->finfo, $path)) { if ($ext && preg_match('~^application/(?:octet-stream|(?:x-)?zip)~', $type)) { if (isset(elFinderVolumeDriver::$mimetypes[$ext])) { $type = elFinderVolumeDriver::$mimetypes[$ext]; } } else { if ($ext === 'js' && preg_match('~^text/~', $type)) { $type = 'text/javascript'; } } } else { $type = 'unknown'; } } elseif ($this->mimeDetect == 'mime_content_type') { $type = mime_content_type($path); } else { $type = elFinderVolumeDriver::mimetypeInternalDetect($path); } $type = explode(';', $type); $type = trim($type[0]); if (in_array($type, array('application/x-empty', 'inode/x-empty'))) { // finfo return this mime for empty files $type = 'text/plain'; } elseif ($type == 'application/x-zip') { // http://elrte.org/redmine/issues/163 $type = 'application/zip'; } // mime type normalization $_checkKey = strtolower($ext . ':' . $type); if (isset($this->options['mimeMap'][$_checkKey])) { $type = $this->options['mimeMap'][$_checkKey]; } return $type == 'unknown' && $this->mimeDetect != 'internal' ? elFinderVolumeDriver::mimetypeInternalDetect($path) : $type; }
/** * Recursive symlinks search * * @param string $path file/dir path * @return bool * @author Dmitry (dio) Levashov **/ protected function _findSymlinks($path) { if (is_link($path)) { return true; } if (is_dir($path)) { foreach (self::localScandir($path) as $name) { $p = $path . DIRECTORY_SEPARATOR . $name; if (is_link($p) || !$this->nameAccepted($name) || ($mimeByName = elFinderVolumeDriver::mimetypeInternalDetect($name)) && $mimeByName !== 'unknown' && !$this->allowPutMime($mimeByName)) { $this->setError(elFinder::ERROR_SAVE, $name); return true; } if (is_dir($p) && $this->_findSymlinks($p)) { return true; } elseif (is_file($p)) { $this->archiveSize += sprintf('%u', filesize($p)); } } } else { $this->archiveSize += sprintf('%u', filesize($path)); } return false; }
/** * Save uploaded file. * On success return array with new file stat and with removed file hash (if existed file was replaced) * * @param Resource $fp file pointer * @param string $dst destination folder hash * @param string $src file name * @param string $tmpname file tmp name - required to detect mime type * @return array|false * @author Dmitry (dio) Levashov * */ public function upload($fp, $dst, $name, $tmpname) { if ($this->commandDisabled('upload')) { return $this->setError(elFinder::ERROR_PERM_DENIED); } if (($dir = $this->dir($dst)) == false) { return $this->setError(elFinder::ERROR_TRGDIR_NOT_FOUND, '#' . $dst); } if (!$dir['write']) { return $this->setError(elFinder::ERROR_PERM_DENIED); } if (!$this->nameAccepted($name)) { return $this->setError(elFinder::ERROR_INVALID_NAME); } $mime = $this->mimetype($this->mimeDetect == 'internal' ? $name : $tmpname); if ($mime == 'unknown' && $this->mimeDetect == 'internal') { $mime = elFinderVolumeDriver::mimetypeInternalDetect($name); } // logic based on http://httpd.apache.org/docs/2.2/mod/mod_authz_host.html#order $allow = $this->mimeAccepted($mime, $this->uploadAllow, null); $deny = $this->mimeAccepted($mime, $this->uploadDeny, null); $upload = true; // default to allow if (strtolower($this->uploadOrder[0]) == 'allow') { // array('allow', 'deny'), default is to 'deny' $upload = false; // default is deny if (!$deny && $allow === true) { // match only allow $upload = true; } // else (both match | no match | match only deny) { deny } } else { // array('deny', 'allow'), default is to 'allow' - this is the default rule $upload = true; // default is allow if ($deny === true && !$allow) { // match only deny $upload = false; } // else (both match | no match | match only allow) { allow } } if (!$upload) { return $this->setError(elFinder::ERROR_UPLOAD_FILE_MIME); } if ($this->uploadMaxSize > 0 && filesize($tmpname) > $this->uploadMaxSize) { return $this->setError(elFinder::ERROR_UPLOAD_FILE_SIZE); } $dstpath = $this->decode($dst); $test = $this->_joinPath($dstpath, $name); $file = $this->stat($test); $this->clearcache(); if ($file) { // file exists if ($this->options['uploadOverwrite']) { if (!$file['write']) { return $this->setError(elFinder::ERROR_PERM_DENIED); } elseif ($file['mime'] == 'directory') { return $this->setError(elFinder::ERROR_NOT_REPLACE, $name); } $this->remove($file); } else { $name = $this->uniqueName($dstpath, $name, '-', false); } } $w = $h = 0; if (strpos($mime, 'image') === 0 && ($s = getimagesize($tmpname))) { $w = $s[0]; $h = $s[1]; } // $this->clearcache(); if (($path = $this->_save($fp, $dstpath, $name, $mime, $w, $h)) == false) { return false; } return $this->stat($path); }
/** * Copy file from another volume. * Return new file path or false. * * @param Object $volume source volume * @param string $src source file hash * @param string $destination destination dir path * @param string $name file name * @return string|false * @author Dmitry (dio) Levashov **/ protected function copyFrom($volume, $src, $destination, $name) { if (($source = $volume->file($src)) == false) { return $this->setError(elFinder::ERROR_COPY, '#' . $src, $volume->error()); } $errpath = $volume->path($source['hash']); if (!$this->nameAccepted($source['name'])) { return $this->setError(elFinder::ERROR_COPY, $errpath, elFinder::ERROR_INVALID_NAME); } if (!$source['read']) { return $this->setError(elFinder::ERROR_COPY, $errpath, elFinder::ERROR_PERM_DENIED); } if ($source['mime'] == 'directory') { $test = $this->stat($this->joinPathCE($destination, $name)); $this->clearcache(); if ($test && $test['mime'] != 'directory' || !($test = $this->mkdir($this->encode($destination), $name))) { //if ((!$stat || $stat['mime'] != 'directory') && $this->convEncOut(!$this->_mkdir($this->convEncIn($destination), $this->convEncIn($name)))) { return $this->setError(elFinder::ERROR_COPY, $errpath); } $path = $this->joinPathCE($destination, $name); $path = $this->decode($test['hash']); foreach ($volume->scandir($src) as $entr) { if (!$this->copyFrom($volume, $entr['hash'], $path, $entr['name'])) { $this->remove($path, true); // fall back return $this->setError($this->error, elFinder::ERROR_COPY, $errpath); } } } else { // $mime = $source['mime']; // $w = $h = 0; if ($dim = $volume->dimensions($src)) { $s = explode('x', $dim); $source['width'] = $s[0]; $source['height'] = $s[1]; } if (($fp = $volume->open($src)) == false || ($path = $this->saveCE($fp, $destination, $name, $source)) == false) { $fp && $volume->close($fp, $src); return $this->setError(elFinder::ERROR_COPY, $errpath); } $volume->close($fp, $src); // MIME check $stat = $this->stat($path); $mimeByName = elFinderVolumeDriver::mimetypeInternalDetect($stat['name']); if ($stat['mime'] === $mimeByName) { $mimeByName = ''; } if (!$this->allowPutMime($stat['mime']) || $mimeByName && $mimeByName !== 'unknown' && !$this->allowPutMime($mimeByName)) { $this->remove($path, true); return $this->setError(elFinder::ERROR_UPLOAD_FILE_MIME, $errpath); } } return $path; }
/** * Recursive symlinks search * * @param string $path file/dir path * @return bool * @author Dmitry (dio) Levashov **/ protected function _findSymlinks($path) { if (is_link($path)) { return true; } if (is_dir($path)) { foreach (scandir($path) as $name) { if ($name != '.' && $name != '..') { $p = $path . DIRECTORY_SEPARATOR . $name; if (is_link($p) || !$this->nameAccepted($name) || ($mimeByName = elFinderVolumeDriver::mimetypeInternalDetect($name)) && $mimeByName !== 'unknown' && !$this->allowPutMime($mimeByName)) { return true; } if (is_dir($p) && $this->_findSymlinks($p)) { return true; } elseif (is_file($p)) { $this->archiveSize += filesize($p); } } } } else { $this->archiveSize += filesize($path); } return false; }