diff e2gallerypro/e2upload/Backend/FileManager.php @ 3:3f6b44aa6b35 judyates

[svn r4] added ability to buy stuff, from a Prints page, but it doesn't work well with the css, and it also has not been fitted into the perl make system.
author rlm
date Mon, 22 Feb 2010 08:02:39 -0500
parents
children
line wrap: on
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/e2gallerypro/e2upload/Backend/FileManager.php	Mon Feb 22 08:02:39 2010 -0500
     1.3 @@ -0,0 +1,391 @@
     1.4 +<?php
     1.5 +/*
     1.6 +Script: FileManager.php
     1.7 +	MooTools FileManager - Backend for the FileManager Script
     1.8 +
     1.9 +License:
    1.10 +	MIT-style license.
    1.11 +
    1.12 +Copyright:
    1.13 +	Copyright (c) 2009 [Christoph Pojer](http://og5.net/christoph).
    1.14 +
    1.15 +Dependencies:
    1.16 +	- Upload.php
    1.17 +	- Image.php
    1.18 +	- getId3 Library
    1.19 +
    1.20 +Options:
    1.21 +	- directory: (string) The base directory to be used for the FileManger
    1.22 +	- assetBasePath: (string) The path to all images and swf files
    1.23 +	- dateFormat: (string, defaults to *j M Y - H:i*) The format in which dates should be displayed
    1.24 +	- upload: (boolean, defaults to *false*) Whether to allow uploads or not
    1.25 +	- destroy: (boolean, defaults to *false*) Whether to allow deletion of files or not
    1.26 +	- maxUploadSize: (integeter, defaults to *3145728* bytes) The maximum file size for upload in bytes
    1.27 +	- safe: (string, defaults to *true*) If true, disallows 
    1.28 +	- filter: (string) If specified, the mimetypes to be allowed (for display and upload).
    1.29 +		Example: image/ allows all Image Mimetypes
    1.30 +*/
    1.31 +
    1.32 +require_once(FileManagerUtility::getPath().'/Upload.php');
    1.33 +require_once(FileManagerUtility::getPath().'/Image.php');
    1.34 +
    1.35 +class FileManager {
    1.36 +	
    1.37 +	private $path = null,
    1.38 +		$length = null,
    1.39 +		$basedir = null,
    1.40 +		$basename = null,
    1.41 +		$options,
    1.42 +		$post,
    1.43 +		$get;
    1.44 +	
    1.45 +	public function __construct($options){
    1.46 +		$this->options = array_merge(array(
    1.47 +			'directory' => '../../Gallery',
    1.48 +			'assetBasePath' => '../Assets',
    1.49 +			'dateFormat' => 'j M Y - H:i',
    1.50 +			'maxUploadSize' => 1024*1024*3,
    1.51 +			'upload' => false,
    1.52 +			'destroy' => false,
    1.53 +			'safe' => true,
    1.54 +			'filter' => null,
    1.55 +		), $options);
    1.56 +		
    1.57 +		$this->basedir = realpath($this->options['directory']);
    1.58 +		$this->basename = pathinfo($this->basedir, PATHINFO_BASENAME).'/';
    1.59 +		$this->path = realpath($this->options['directory'].'/../');
    1.60 +		$this->length = strlen($this->path);
    1.61 +		
    1.62 +		header('Expires: Fri, 01 Jan 1990 00:00:00 GMT');
    1.63 +		header('Cache-Control: no-cache, no-store, max-age=0, must-revalidate');
    1.64 +		
    1.65 +		$this->get = $_GET;
    1.66 +		$this->post = $_POST;
    1.67 +	}
    1.68 +	
    1.69 +	public function fireEvent($event){
    1.70 +		$event = $event ? 'on'.ucfirst($event) : null;
    1.71 +		if(!$event || !method_exists($this, $event)) $event = 'onView';
    1.72 +		
    1.73 +		$this->{$event}();
    1.74 +	}
    1.75 +	
    1.76 +	protected function onView(){
    1.77 +		$dir = $this->getDir(!empty($this->post['directory']) ? $this->post['directory'] : null);
    1.78 +		$files = ($files = glob($dir.'/*')) ? $files : array();
    1.79 +		
    1.80 +		if($dir!=$this->basedir) array_unshift($files, $dir.'/..');
    1.81 +		natcasesort($files);
    1.82 +		foreach($files as $file){
    1.83 +			$mime = $this->getMimeType($file);
    1.84 +			if($this->options['filter'] && $mime!='text/directory' && !FileManagerUtility::startsWith($mime, $this->options['filter']))
    1.85 +				continue;
    1.86 +			
    1.87 +			$out[is_dir($file) ? 0 : 1][] = array(
    1.88 +				'name' => pathinfo($file, PATHINFO_BASENAME),
    1.89 +				'date' => date($this->options['dateFormat'], filemtime($file)),
    1.90 +				'mime' => $this->getMimeType($file),
    1.91 +				'icon' => $this->getIcon($this->normalize($file)),
    1.92 +				'size' => filesize($file),
    1.93 +			);
    1.94 +		}
    1.95 +		
    1.96 +		echo json_encode(array(
    1.97 +			'path' => $this->getPath($dir),
    1.98 +			'dir' => array(
    1.99 +				'name' => pathinfo($dir, PATHINFO_BASENAME),
   1.100 +				'date' => date($this->options['dateFormat'], filemtime($dir)),
   1.101 +				'mime' => 'text/directory',
   1.102 +				'icon' => 'dir',
   1.103 +			),
   1.104 +			'files' => array_merge(!empty($out[0]) ? $out[0] : array(), !empty($out[1]) ? $out[1] : array()),
   1.105 +		));
   1.106 +	}
   1.107 +	
   1.108 +	protected function onDetail(){
   1.109 +		if(empty($this->post['directory']) || empty($this->post['file'])) return;
   1.110 +		
   1.111 +		$file = realpath($this->path.'/'.$this->post['directory'].'/'.$this->post['file']);
   1.112 +		if(!$this->checkFile($file)) return;
   1.113 +		
   1.114 +		require_once(FileManagerUtility::getPath().'/Assets/getid3/getid3.php');
   1.115 +		
   1.116 +		$url = $this->normalize(substr($file, strlen($this->path)+1));
   1.117 +		$mime = $this->getMimeType($file);
   1.118 +		$content = null;
   1.119 +		if(FileManagerUtility::startsWith($mime, 'image/')){
   1.120 +			$size = getimagesize($file);
   1.121 +			$content = '<img src="'.$url.'" class="preview" alt="" />
   1.122 +				<h2>${more}</h2>
   1.123 +				<dl>
   1.124 +					<dt>${width}</dt><dd>'.$size[0].'px</dd>
   1.125 +					<dt>${height}</dt><dd>'.$size[1].'px</dd>
   1.126 +				</dl>';
   1.127 +		}elseif(FileManagerUtility::startsWith($mime, 'text/') || $mime=='application/x-javascript'){
   1.128 +			$filecontent = file_get_contents($file, null, null, 0, 300);
   1.129 +			if(!FileManagerUtility::isBinary($filecontent)) $content = '<div class="textpreview">'.nl2br(str_replace(array('$', "\t"), array('&#36;', '&nbsp;&nbsp;'), htmlentities($filecontent))).'</div>';
   1.130 +		}elseif($mime=='application/zip'){
   1.131 +			$out = array(array(), array());
   1.132 +			$getid3 = new getID3();
   1.133 +			$getid3->Analyze($file);
   1.134 +			foreach($getid3->info['zip']['files'] as $name => $size){
   1.135 +				$icon = is_array($size) ? 'dir' : $this->getIcon($name);
   1.136 +				$out[$icon=='dir' ? 0 : 1][$name] = '<li><a><img src="'.$this->options['assetBasePath'].'/Icons/'.$icon.'.png" alt="" /> '.$name.'</a></li>';
   1.137 +			}
   1.138 +			natcasesort($out[0]);
   1.139 +			natcasesort($out[1]);
   1.140 +			$content = '<ul>'.implode(array_merge($out[0], $out[1])).'</ul>';
   1.141 +		}elseif(FileManagerUtility::startsWith($mime, 'audio/')){
   1.142 +			$getid3 = new getID3();
   1.143 +			$getid3->Analyze($file);
   1.144 +			
   1.145 +			$content = '<div class="object">
   1.146 +					<object type="application/x-shockwave-flash" data="'.$this->options['assetBasePath'].'/dewplayer.swf?mp3='.rawurlencode($url).'&volume=30" width="200" height="20">
   1.147 +						<param name="movie" value="'.$this->options['assetBasePath'].'/dewplayer.swf?mp3='.rawurlencode($url).'&volume=30" />
   1.148 +					</object>
   1.149 +				</div>
   1.150 +				<h2>${more}</h2>
   1.151 +				<dl>
   1.152 +					<dt>${title}</dt><dd>'.$getid3->info['comments']['title'][0].'</dd>
   1.153 +					<dt>${artist}</dt><dd>'.$getid3->info['comments']['artist'][0].'</dd>
   1.154 +					<dt>${album}</dt><dd>'.$getid3->info['comments']['album'][0].'</dd>
   1.155 +					<dt>${length}</dt><dd>'.$getid3->info['playtime_string'].'</dd>
   1.156 +					<dt>${bitrate}</dt><dd>'.round($getid3->info['bitrate']/1000).'kbps</dd>
   1.157 +				</dl>';
   1.158 +		}
   1.159 +		
   1.160 +		echo json_encode(array(
   1.161 +			'content' => $content ? $content : '<div class="margin">
   1.162 +					${nopreview}<br/><button value="'.$url.'">${download}</button>
   1.163 +				</div>',
   1.164 +		));
   1.165 +	}
   1.166 +	
   1.167 +	protected function onDestroy(){
   1.168 +		if(!$this->options['destroy'] || empty($this->post['directory']) || empty($this->post['file'])) return;
   1.169 +		
   1.170 +		$file = realpath($this->path.'/'.$this->post['directory'].'/'.$this->post['file']);
   1.171 +		if(!$this->checkFile($file)) return;
   1.172 +		
   1.173 +		$this->unlink($file);
   1.174 +		
   1.175 +		echo json_encode(array(
   1.176 +			'content' => 'destroyed',
   1.177 +		));
   1.178 +	}
   1.179 +	
   1.180 +	protected function onCreate(){
   1.181 +		if(empty($this->post['directory']) || empty($this->post['file'])) return;
   1.182 +		
   1.183 +		$file = $this->getName($this->post['file'], $this->getDir($this->post['directory']));
   1.184 +		if(!$file) return;
   1.185 +		
   1.186 +		mkdir($file);
   1.187 +		
   1.188 +		$this->onView();
   1.189 +	}
   1.190 +	
   1.191 +	protected function onUpload(){
   1.192 +		try{
   1.193 +			if(!$this->options['upload'])
   1.194 +				throw new FileManagerException('disabled');
   1.195 +			if(empty($this->get['directory']) || (function_exists('UploadIsAuthenticated') && !UploadIsAuthenticated($this->get)))
   1.196 +				throw new FileManagerException('authenticated');
   1.197 +			
   1.198 +			$dir = $this->getDir($this->get['directory']);
   1.199 +			$name = pathinfo((Upload::exists('Filedata')) ? $this->getName($_FILES['Filedata']['name'], $dir) : null, PATHINFO_FILENAME);
   1.200 +			$file = Upload::move('Filedata', $dir.'/', array(
   1.201 +				'name' => $name,
   1.202 +				'extension' => $this->options['safe'] && $name && in_array(strtolower(pathinfo($_FILES['Filedata']['name'], PATHINFO_EXTENSION)), array('exe', 'dll', 'php', 'php3', 'php4', 'php5', 'phps')) ? 'txt' : null,
   1.203 +				'size' => $this->options['maxUploadSize'],
   1.204 +				'mimes' => $this->getAllowedMimeTypes(),
   1.205 +			));
   1.206 +			
   1.207 +			if(FileManagerUtility::startsWith(Upload::mime($file), 'image/') && !empty($this->get['resize'])){
   1.208 +				$img = new Image($file);
   1.209 +				$size = $img->getSize();
   1.210 +				if($size['width']>800) $img->resize(800)->save();
   1.211 +				elseif($size['height']>600) $img->resize(null, 600)->save();
   1.212 +			}
   1.213 +			
   1.214 +			echo json_encode(array(
   1.215 +				'status' => 1,
   1.216 +				'name' => pathinfo($file, PATHINFO_BASENAME),
   1.217 +			));
   1.218 +		}catch(UploadException $e){
   1.219 +			echo json_encode(array(
   1.220 +				'status' => 0,
   1.221 +				'error' => class_exists('ValidatorException') ? $e->getMessage() : '${upload.'.$e->getMessage().'}', // This is for Styx :)
   1.222 +			));
   1.223 +		}catch(FileManagerException $e){
   1.224 +			echo json_encode(array(
   1.225 +				'status' => 0,
   1.226 +				'error' => '${upload.'.$e->getMessage().'}',
   1.227 +			));
   1.228 +		}
   1.229 +	}
   1.230 +	
   1.231 +	/* This method is used by both move and rename */
   1.232 +	protected function onMove(){
   1.233 +		if(empty($this->post['directory']) || empty($this->post['file'])) return;
   1.234 +		
   1.235 +		$rename = empty($this->post['newDirectory']) && !empty($this->post['name']);
   1.236 +		$dir = $this->getDir($this->post['directory']);
   1.237 +		$file = realpath($dir.'/'.$this->post['file']);
   1.238 +		
   1.239 +		$is_dir = is_dir($file);
   1.240 +		if(!$this->checkFile($file) || (!$rename && $is_dir))
   1.241 +			return;
   1.242 +		
   1.243 +		if($rename || $is_dir){
   1.244 +			if(empty($this->post['name'])) return;
   1.245 +			$newname = $this->getName($this->post['name'], $dir);
   1.246 +			$fn = 'rename';
   1.247 +		}else{
   1.248 +			$newname = $this->getName(pathinfo($file, PATHINFO_FILENAME), $this->getDir($this->post['newDirectory']));
   1.249 +			$fn = !empty($this->post['copy']) ? 'copy' : 'rename';
   1.250 +		}
   1.251 +		
   1.252 +		if(!$newname) return;
   1.253 +		
   1.254 +		$ext = pathinfo($file, PATHINFO_EXTENSION);
   1.255 +		if($ext) $newname .= '.'.$ext;
   1.256 +		$fn($file, $newname);
   1.257 +		
   1.258 +		echo json_encode(array(
   1.259 +			'name' => pathinfo($this->normalize($newname), PATHINFO_BASENAME),
   1.260 +		));
   1.261 +	}
   1.262 +	
   1.263 +	protected function unlink($file){
   1.264 +		$file = realpath($file);
   1.265 +		if($this->basedir==$file || strlen($this->basedir)>=strlen($file))
   1.266 +			return;
   1.267 +		
   1.268 +		if(is_dir($file)){
   1.269 +			$files = glob($file.'/*');
   1.270 +			if(is_array($files))
   1.271 +				foreach($files as $f)
   1.272 +					$this->unlink($f);
   1.273 +				
   1.274 +			rmdir($file);
   1.275 +		}else{
   1.276 +			try{ if($this->checkFile($file)) unlink($file); }catch(Exception $e){}
   1.277 +		}
   1.278 +	}
   1.279 +	
   1.280 +	protected function getName($file, $dir){
   1.281 +		$files = array();
   1.282 +		foreach((array)glob($dir.'/*') as $f)
   1.283 +			$files[] = pathinfo($f, PATHINFO_FILENAME);
   1.284 +		
   1.285 +		$pathinfo = pathinfo($file);
   1.286 +		$file = $dir.'/'.FileManagerUtility::pagetitle($pathinfo['filename'], $files).(!empty($pathinfo['extension']) ? '.'.$pathinfo['extension'] : null);
   1.287 +		
   1.288 +		return !$file || !FileManagerUtility::startsWith($file, $this->basedir) || file_exists($file) ? null : $file;
   1.289 +	}
   1.290 +	
   1.291 +	protected function getIcon($file){
   1.292 +		if(FileManagerUtility::endsWith($file, '/..')) return 'dir_up';
   1.293 +		else if(is_dir($file)) return 'dir';
   1.294 +		
   1.295 +		$ext = pathinfo($file, PATHINFO_EXTENSION);
   1.296 +		return ($ext && file_exists(realpath($this->options['assetBasePath'].'/Icons/'.$ext.'.png'))) ? $ext : 'default';
   1.297 +	}
   1.298 +
   1.299 +	protected function getMimeType($file){
   1.300 +		return is_dir($file) ? 'text/directory' : Upload::mime($file);
   1.301 +	}
   1.302 +	
   1.303 +	protected function getDir($dir){
   1.304 +		$dir = realpath($this->path.'/'.(FileManagerUtility::startsWith($dir, $this->basename) ? $dir : $this->basename));
   1.305 +		return $this->checkFile($dir) ? $dir : $this->basedir;
   1.306 +	}
   1.307 +	
   1.308 +	protected function getPath($file){
   1.309 +		$file = $this->normalize(substr($file, $this->length));
   1.310 +		return substr($file, FileManagerUtility::startsWith($file, '/') ? 1 : 0);
   1.311 +	}
   1.312 +	
   1.313 +	protected function checkFile($file){
   1.314 +		$mimes = $this->getAllowedMimeTypes();
   1.315 +		$hasFilter = $this->options['filter'] && count($mimes);
   1.316 +		if($hasFilter) array_push($mimes, 'text/directory');
   1.317 +		return !(!$file || !FileManagerUtility::startsWith($file, $this->basedir) || !file_exists($file) || ($hasFilter && !in_array($this->getMimeType($file), $mimes)));
   1.318 +	}
   1.319 +	
   1.320 +	protected function normalize($file){
   1.321 +		return preg_replace('/\\\|\/{2,}/', '/', $file);
   1.322 +	}
   1.323 +	
   1.324 +	protected function getAllowedMimeTypes(){
   1.325 +		$filter = $this->options['filter'];
   1.326 +		
   1.327 +		if(!$filter) return null;
   1.328 +		if(!FileManagerUtility::endsWith($filter, '/')) return array($filter);
   1.329 +		
   1.330 +		static $mimes;
   1.331 +		if(!$mimes) $mimes = parse_ini_file(FileManagerUtility::getPath().'/MimeTypes.ini');
   1.332 +		
   1.333 +		foreach($mimes as $mime)
   1.334 +			if(FileManagerUtility::startsWith($mime, $filter))
   1.335 +				$mimeTypes[] = strtolower($mime);
   1.336 +		
   1.337 +		return $mimeTypes;
   1.338 +	}
   1.339 +
   1.340 +}
   1.341 +
   1.342 +class FileManagerException extends Exception {}
   1.343 +
   1.344 +/* Stripped-down version of some Styx PHP Framework-Functionality bundled with this FileBrowser. Styx is located at: http://styx.og5.net */
   1.345 +class FileManagerUtility {
   1.346 +	
   1.347 +	public static function endsWith($string, $look){
   1.348 +		return strrpos($string, $look)===strlen($string)-strlen($look);
   1.349 +	}
   1.350 +	
   1.351 +	public static function startsWith($string, $look){
   1.352 +		return strpos($string, $look)===0;
   1.353 +	}
   1.354 +	
   1.355 +	public static function pagetitle($data, $options = array()){
   1.356 +		static $regex;
   1.357 +		if(!$regex){
   1.358 +			$regex = array(
   1.359 +				explode(' ', 'Æ æ Œ œ ß Ü ü Ö ö Ä ä À Á Â Ã Ä Å &#260; &#258; Ç &#262; &#268; &#270; &#272; Ð È É Ê Ë &#280; &#282; &#286; Ì Í Î Ï &#304; &#321; &#317; &#313; Ñ &#323; &#327; Ò Ó Ô Õ Ö Ø &#336; &#340; &#344; Š &#346; &#350; &#356; &#354; Ù Ú Û Ü &#366; &#368; Ý Ž &#377; &#379; à á â ã ä å &#261; &#259; ç &#263; &#269; &#271; &#273; è é ê ë &#281; &#283; &#287; ì í î ï &#305; &#322; &#318; &#314; ñ &#324; &#328; ð ò ó ô õ ö ø &#337; &#341; &#345; &#347; š &#351; &#357; &#355; ù ú û ü &#367; &#369; ý ÿ ž &#378; &#380;'),
   1.360 +				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'),
   1.361 +			);
   1.362 +			
   1.363 +			$regex[0][] = '"';
   1.364 +			$regex[0][] = "'";
   1.365 +		}
   1.366 +		
   1.367 +		$data = trim(substr(preg_replace('/(?:[^A-z0-9]|_|\^)+/i', '_', str_replace($regex[0], $regex[1], $data)), 0, 64), '_');
   1.368 +		return !empty($options) ? self::checkTitle($data, $options) : $data;
   1.369 +	}
   1.370 +	
   1.371 +	protected static function checkTitle($data, $options = array(), $i = 0){
   1.372 +		if(!is_array($options)) return $data;
   1.373 +		
   1.374 +		foreach($options as $content)
   1.375 +			if($content && strtolower($content)==strtolower($data.($i ? '_'.$i : '')))
   1.376 +				return self::checkTitle($data, $options, ++$i);
   1.377 +		
   1.378 +		return $data.($i ? '_'.$i : '');
   1.379 +	}
   1.380 +	
   1.381 +	public static function isBinary($str){
   1.382 +		$array = array(0, 255);
   1.383 +		for($i = 0; $i < strlen($str); $i++)
   1.384 +			if(in_array(ord($str[$i]), $array)) return true;
   1.385 +		
   1.386 +		return false;
   1.387 +	}
   1.388 +	
   1.389 +	public static function getPath(){
   1.390 +		static $path;
   1.391 +		return $path ? $path : $path = pathinfo(__FILE__, PATHINFO_DIRNAME);
   1.392 +	}
   1.393 +	
   1.394 +}
   1.395 \ No newline at end of file