rlm@3: options = array_merge(array( rlm@3: 'directory' => '../../Gallery', rlm@3: 'assetBasePath' => '../Assets', rlm@3: 'dateFormat' => 'j M Y - H:i', rlm@3: 'maxUploadSize' => 1024*1024*3, rlm@3: 'upload' => false, rlm@3: 'destroy' => false, rlm@3: 'safe' => true, rlm@3: 'filter' => null, rlm@3: ), $options); rlm@3: rlm@3: $this->basedir = realpath($this->options['directory']); rlm@3: $this->basename = pathinfo($this->basedir, PATHINFO_BASENAME).'/'; rlm@3: $this->path = realpath($this->options['directory'].'/../'); rlm@3: $this->length = strlen($this->path); rlm@3: rlm@3: header('Expires: Fri, 01 Jan 1990 00:00:00 GMT'); rlm@3: header('Cache-Control: no-cache, no-store, max-age=0, must-revalidate'); rlm@3: rlm@3: $this->get = $_GET; rlm@3: $this->post = $_POST; rlm@3: } rlm@3: rlm@3: public function fireEvent($event){ rlm@3: $event = $event ? 'on'.ucfirst($event) : null; rlm@3: if(!$event || !method_exists($this, $event)) $event = 'onView'; rlm@3: rlm@3: $this->{$event}(); rlm@3: } rlm@3: rlm@3: protected function onView(){ rlm@3: $dir = $this->getDir(!empty($this->post['directory']) ? $this->post['directory'] : null); rlm@3: $files = ($files = glob($dir.'/*')) ? $files : array(); rlm@3: rlm@3: if($dir!=$this->basedir) array_unshift($files, $dir.'/..'); rlm@3: natcasesort($files); rlm@3: foreach($files as $file){ rlm@3: $mime = $this->getMimeType($file); rlm@3: if($this->options['filter'] && $mime!='text/directory' && !FileManagerUtility::startsWith($mime, $this->options['filter'])) rlm@3: continue; rlm@3: rlm@3: $out[is_dir($file) ? 0 : 1][] = array( rlm@3: 'name' => pathinfo($file, PATHINFO_BASENAME), rlm@3: 'date' => date($this->options['dateFormat'], filemtime($file)), rlm@3: 'mime' => $this->getMimeType($file), rlm@3: 'icon' => $this->getIcon($this->normalize($file)), rlm@3: 'size' => filesize($file), rlm@3: ); rlm@3: } rlm@3: rlm@3: echo json_encode(array( rlm@3: 'path' => $this->getPath($dir), rlm@3: 'dir' => array( rlm@3: 'name' => pathinfo($dir, PATHINFO_BASENAME), rlm@3: 'date' => date($this->options['dateFormat'], filemtime($dir)), rlm@3: 'mime' => 'text/directory', rlm@3: 'icon' => 'dir', rlm@3: ), rlm@3: 'files' => array_merge(!empty($out[0]) ? $out[0] : array(), !empty($out[1]) ? $out[1] : array()), rlm@3: )); rlm@3: } rlm@3: rlm@3: protected function onDetail(){ rlm@3: if(empty($this->post['directory']) || empty($this->post['file'])) return; rlm@3: rlm@3: $file = realpath($this->path.'/'.$this->post['directory'].'/'.$this->post['file']); rlm@3: if(!$this->checkFile($file)) return; rlm@3: rlm@3: require_once(FileManagerUtility::getPath().'/Assets/getid3/getid3.php'); rlm@3: rlm@3: $url = $this->normalize(substr($file, strlen($this->path)+1)); rlm@3: $mime = $this->getMimeType($file); rlm@3: $content = null; rlm@3: if(FileManagerUtility::startsWith($mime, 'image/')){ rlm@3: $size = getimagesize($file); rlm@3: $content = ' rlm@3:

${more}

rlm@3:
rlm@3:
${width}
'.$size[0].'px
rlm@3:
${height}
'.$size[1].'px
rlm@3:
'; rlm@3: }elseif(FileManagerUtility::startsWith($mime, 'text/') || $mime=='application/x-javascript'){ rlm@3: $filecontent = file_get_contents($file, null, null, 0, 300); rlm@3: if(!FileManagerUtility::isBinary($filecontent)) $content = '
'.nl2br(str_replace(array('$', "\t"), array('$', '  '), htmlentities($filecontent))).'
'; rlm@3: }elseif($mime=='application/zip'){ rlm@3: $out = array(array(), array()); rlm@3: $getid3 = new getID3(); rlm@3: $getid3->Analyze($file); rlm@3: foreach($getid3->info['zip']['files'] as $name => $size){ rlm@3: $icon = is_array($size) ? 'dir' : $this->getIcon($name); rlm@3: $out[$icon=='dir' ? 0 : 1][$name] = '
  • '.$name.'
  • '; rlm@3: } rlm@3: natcasesort($out[0]); rlm@3: natcasesort($out[1]); rlm@3: $content = ''; rlm@3: }elseif(FileManagerUtility::startsWith($mime, 'audio/')){ rlm@3: $getid3 = new getID3(); rlm@3: $getid3->Analyze($file); rlm@3: rlm@3: $content = '
    rlm@3: rlm@3: rlm@3: rlm@3:
    rlm@3:

    ${more}

    rlm@3:
    rlm@3:
    ${title}
    '.$getid3->info['comments']['title'][0].'
    rlm@3:
    ${artist}
    '.$getid3->info['comments']['artist'][0].'
    rlm@3:
    ${album}
    '.$getid3->info['comments']['album'][0].'
    rlm@3:
    ${length}
    '.$getid3->info['playtime_string'].'
    rlm@3:
    ${bitrate}
    '.round($getid3->info['bitrate']/1000).'kbps
    rlm@3:
    '; rlm@3: } rlm@3: rlm@3: echo json_encode(array( rlm@3: 'content' => $content ? $content : '
    rlm@3: ${nopreview}
    rlm@3:
    ', rlm@3: )); rlm@3: } rlm@3: rlm@3: protected function onDestroy(){ rlm@3: if(!$this->options['destroy'] || empty($this->post['directory']) || empty($this->post['file'])) return; rlm@3: rlm@3: $file = realpath($this->path.'/'.$this->post['directory'].'/'.$this->post['file']); rlm@3: if(!$this->checkFile($file)) return; rlm@3: rlm@3: $this->unlink($file); rlm@3: rlm@3: echo json_encode(array( rlm@3: 'content' => 'destroyed', rlm@3: )); rlm@3: } rlm@3: rlm@3: protected function onCreate(){ rlm@3: if(empty($this->post['directory']) || empty($this->post['file'])) return; rlm@3: rlm@3: $file = $this->getName($this->post['file'], $this->getDir($this->post['directory'])); rlm@3: if(!$file) return; rlm@3: rlm@3: mkdir($file); rlm@3: rlm@3: $this->onView(); rlm@3: } rlm@3: rlm@3: protected function onUpload(){ rlm@3: try{ rlm@3: if(!$this->options['upload']) rlm@3: throw new FileManagerException('disabled'); rlm@3: if(empty($this->get['directory']) || (function_exists('UploadIsAuthenticated') && !UploadIsAuthenticated($this->get))) rlm@3: throw new FileManagerException('authenticated'); rlm@3: rlm@3: $dir = $this->getDir($this->get['directory']); rlm@3: $name = pathinfo((Upload::exists('Filedata')) ? $this->getName($_FILES['Filedata']['name'], $dir) : null, PATHINFO_FILENAME); rlm@3: $file = Upload::move('Filedata', $dir.'/', array( rlm@3: 'name' => $name, rlm@3: 'extension' => $this->options['safe'] && $name && in_array(strtolower(pathinfo($_FILES['Filedata']['name'], PATHINFO_EXTENSION)), array('exe', 'dll', 'php', 'php3', 'php4', 'php5', 'phps')) ? 'txt' : null, rlm@3: 'size' => $this->options['maxUploadSize'], rlm@3: 'mimes' => $this->getAllowedMimeTypes(), rlm@3: )); rlm@3: rlm@3: if(FileManagerUtility::startsWith(Upload::mime($file), 'image/') && !empty($this->get['resize'])){ rlm@3: $img = new Image($file); rlm@3: $size = $img->getSize(); rlm@3: if($size['width']>800) $img->resize(800)->save(); rlm@3: elseif($size['height']>600) $img->resize(null, 600)->save(); rlm@3: } rlm@3: rlm@3: echo json_encode(array( rlm@3: 'status' => 1, rlm@3: 'name' => pathinfo($file, PATHINFO_BASENAME), rlm@3: )); rlm@3: }catch(UploadException $e){ rlm@3: echo json_encode(array( rlm@3: 'status' => 0, rlm@3: 'error' => class_exists('ValidatorException') ? $e->getMessage() : '${upload.'.$e->getMessage().'}', // This is for Styx :) rlm@3: )); rlm@3: }catch(FileManagerException $e){ rlm@3: echo json_encode(array( rlm@3: 'status' => 0, rlm@3: 'error' => '${upload.'.$e->getMessage().'}', rlm@3: )); rlm@3: } rlm@3: } rlm@3: rlm@3: /* This method is used by both move and rename */ rlm@3: protected function onMove(){ rlm@3: if(empty($this->post['directory']) || empty($this->post['file'])) return; rlm@3: rlm@3: $rename = empty($this->post['newDirectory']) && !empty($this->post['name']); rlm@3: $dir = $this->getDir($this->post['directory']); rlm@3: $file = realpath($dir.'/'.$this->post['file']); rlm@3: rlm@3: $is_dir = is_dir($file); rlm@3: if(!$this->checkFile($file) || (!$rename && $is_dir)) rlm@3: return; rlm@3: rlm@3: if($rename || $is_dir){ rlm@3: if(empty($this->post['name'])) return; rlm@3: $newname = $this->getName($this->post['name'], $dir); rlm@3: $fn = 'rename'; rlm@3: }else{ rlm@3: $newname = $this->getName(pathinfo($file, PATHINFO_FILENAME), $this->getDir($this->post['newDirectory'])); rlm@3: $fn = !empty($this->post['copy']) ? 'copy' : 'rename'; rlm@3: } rlm@3: rlm@3: if(!$newname) return; rlm@3: rlm@3: $ext = pathinfo($file, PATHINFO_EXTENSION); rlm@3: if($ext) $newname .= '.'.$ext; rlm@3: $fn($file, $newname); rlm@3: rlm@3: echo json_encode(array( rlm@3: 'name' => pathinfo($this->normalize($newname), PATHINFO_BASENAME), rlm@3: )); rlm@3: } rlm@3: rlm@3: protected function unlink($file){ rlm@3: $file = realpath($file); rlm@3: if($this->basedir==$file || strlen($this->basedir)>=strlen($file)) rlm@3: return; rlm@3: rlm@3: if(is_dir($file)){ rlm@3: $files = glob($file.'/*'); rlm@3: if(is_array($files)) rlm@3: foreach($files as $f) rlm@3: $this->unlink($f); rlm@3: rlm@3: rmdir($file); rlm@3: }else{ rlm@3: try{ if($this->checkFile($file)) unlink($file); }catch(Exception $e){} rlm@3: } rlm@3: } rlm@3: rlm@3: protected function getName($file, $dir){ rlm@3: $files = array(); rlm@3: foreach((array)glob($dir.'/*') as $f) rlm@3: $files[] = pathinfo($f, PATHINFO_FILENAME); rlm@3: rlm@3: $pathinfo = pathinfo($file); rlm@3: $file = $dir.'/'.FileManagerUtility::pagetitle($pathinfo['filename'], $files).(!empty($pathinfo['extension']) ? '.'.$pathinfo['extension'] : null); rlm@3: rlm@3: return !$file || !FileManagerUtility::startsWith($file, $this->basedir) || file_exists($file) ? null : $file; rlm@3: } rlm@3: rlm@3: protected function getIcon($file){ rlm@3: if(FileManagerUtility::endsWith($file, '/..')) return 'dir_up'; rlm@3: else if(is_dir($file)) return 'dir'; rlm@3: rlm@3: $ext = pathinfo($file, PATHINFO_EXTENSION); rlm@3: return ($ext && file_exists(realpath($this->options['assetBasePath'].'/Icons/'.$ext.'.png'))) ? $ext : 'default'; rlm@3: } rlm@3: rlm@3: protected function getMimeType($file){ rlm@3: return is_dir($file) ? 'text/directory' : Upload::mime($file); rlm@3: } rlm@3: rlm@3: protected function getDir($dir){ rlm@3: $dir = realpath($this->path.'/'.(FileManagerUtility::startsWith($dir, $this->basename) ? $dir : $this->basename)); rlm@3: return $this->checkFile($dir) ? $dir : $this->basedir; rlm@3: } rlm@3: rlm@3: protected function getPath($file){ rlm@3: $file = $this->normalize(substr($file, $this->length)); rlm@3: return substr($file, FileManagerUtility::startsWith($file, '/') ? 1 : 0); rlm@3: } rlm@3: rlm@3: protected function checkFile($file){ rlm@3: $mimes = $this->getAllowedMimeTypes(); rlm@3: $hasFilter = $this->options['filter'] && count($mimes); rlm@3: if($hasFilter) array_push($mimes, 'text/directory'); rlm@3: return !(!$file || !FileManagerUtility::startsWith($file, $this->basedir) || !file_exists($file) || ($hasFilter && !in_array($this->getMimeType($file), $mimes))); rlm@3: } rlm@3: rlm@3: protected function normalize($file){ rlm@3: return preg_replace('/\\\|\/{2,}/', '/', $file); rlm@3: } rlm@3: rlm@3: protected function getAllowedMimeTypes(){ rlm@3: $filter = $this->options['filter']; rlm@3: rlm@3: if(!$filter) return null; rlm@3: if(!FileManagerUtility::endsWith($filter, '/')) return array($filter); rlm@3: rlm@3: static $mimes; rlm@3: if(!$mimes) $mimes = parse_ini_file(FileManagerUtility::getPath().'/MimeTypes.ini'); rlm@3: rlm@3: foreach($mimes as $mime) rlm@3: if(FileManagerUtility::startsWith($mime, $filter)) rlm@3: $mimeTypes[] = strtolower($mime); rlm@3: rlm@3: return $mimeTypes; rlm@3: } rlm@3: rlm@3: } rlm@3: rlm@3: class FileManagerException extends Exception {} rlm@3: rlm@3: /* Stripped-down version of some Styx PHP Framework-Functionality bundled with this FileBrowser. Styx is located at: http://styx.og5.net */ rlm@3: class FileManagerUtility { rlm@3: rlm@3: public static function endsWith($string, $look){ rlm@3: return strrpos($string, $look)===strlen($string)-strlen($look); rlm@3: } rlm@3: rlm@3: public static function startsWith($string, $look){ rlm@3: return strpos($string, $look)===0; rlm@3: } rlm@3: rlm@3: public static function pagetitle($data, $options = array()){ rlm@3: static $regex; rlm@3: if(!$regex){ rlm@3: $regex = array( rlm@3: explode(' ', 'Æ æ Œ œ ß Ü ü Ö ö Ä ä À Á Â Ã Ä Å Ą Ă Ç Ć Č Ď Đ Ð È É Ê Ë Ę Ě Ğ Ì Í Î Ï İ Ł Ľ Ĺ Ñ Ń Ň Ò Ó Ô Õ Ö Ø Ő Ŕ Ř Š Ś Ş Ť Ţ Ù Ú Û Ü Ů Ű Ý Ž Ź Ż à á â ã ä å ą ă ç ć č ď đ è é ê ë ę ě ğ ì í î ï ı ł ľ ĺ ñ ń ň ð ò ó ô õ ö ø ő ŕ ř ś š ş ť ţ ù ú û ü ů ű ý ÿ ž ź ż'), rlm@3: explode(' ', 'Ae ae Oe oe ss Ue ue Oe oe Ae ae A A A A A A A A C C C D D D E E E E E E G I I I I I L L L N N N O O O O O O O R R S S S T T U U U U U U Y Z Z Z a a a a a a a a c c c d d e e e e e e g i i i i i l l l n n n o o o o o o o o r r s s s t t u u u u u u y y z z z'), rlm@3: ); rlm@3: rlm@3: $regex[0][] = '"'; rlm@3: $regex[0][] = "'"; rlm@3: } rlm@3: rlm@3: $data = trim(substr(preg_replace('/(?:[^A-z0-9]|_|\^)+/i', '_', str_replace($regex[0], $regex[1], $data)), 0, 64), '_'); rlm@3: return !empty($options) ? self::checkTitle($data, $options) : $data; rlm@3: } rlm@3: rlm@3: protected static function checkTitle($data, $options = array(), $i = 0){ rlm@3: if(!is_array($options)) return $data; rlm@3: rlm@3: foreach($options as $content) rlm@3: if($content && strtolower($content)==strtolower($data.($i ? '_'.$i : ''))) rlm@3: return self::checkTitle($data, $options, ++$i); rlm@3: rlm@3: return $data.($i ? '_'.$i : ''); rlm@3: } rlm@3: rlm@3: public static function isBinary($str){ rlm@3: $array = array(0, 255); rlm@3: for($i = 0; $i < strlen($str); $i++) rlm@3: if(in_array(ord($str[$i]), $array)) return true; rlm@3: rlm@3: return false; rlm@3: } rlm@3: rlm@3: public static function getPath(){ rlm@3: static $path; rlm@3: return $path ? $path : $path = pathinfo(__FILE__, PATHINFO_DIRNAME); rlm@3: } rlm@3: rlm@3: }