annotate e2gallerypro/e2upload/Backend/FileManager.php @ 12:2f433df9b961 judyates

[svn r13] Modified esgallery source to enable multiple paypal buttons; got the photo gallery operational and tested.
author rlm
date Mon, 12 Apr 2010 03:24:08 -0400
parents 3f6b44aa6b35
children
rev   line source
rlm@3 1 <?php
rlm@3 2 /*
rlm@3 3 Script: FileManager.php
rlm@3 4 MooTools FileManager - Backend for the FileManager Script
rlm@3 5
rlm@3 6 License:
rlm@3 7 MIT-style license.
rlm@3 8
rlm@3 9 Copyright:
rlm@3 10 Copyright (c) 2009 [Christoph Pojer](http://og5.net/christoph).
rlm@3 11
rlm@3 12 Dependencies:
rlm@3 13 - Upload.php
rlm@3 14 - Image.php
rlm@3 15 - getId3 Library
rlm@3 16
rlm@3 17 Options:
rlm@3 18 - directory: (string) The base directory to be used for the FileManger
rlm@3 19 - assetBasePath: (string) The path to all images and swf files
rlm@3 20 - dateFormat: (string, defaults to *j M Y - H:i*) The format in which dates should be displayed
rlm@3 21 - upload: (boolean, defaults to *false*) Whether to allow uploads or not
rlm@3 22 - destroy: (boolean, defaults to *false*) Whether to allow deletion of files or not
rlm@3 23 - maxUploadSize: (integeter, defaults to *3145728* bytes) The maximum file size for upload in bytes
rlm@3 24 - safe: (string, defaults to *true*) If true, disallows
rlm@3 25 - filter: (string) If specified, the mimetypes to be allowed (for display and upload).
rlm@3 26 Example: image/ allows all Image Mimetypes
rlm@3 27 */
rlm@3 28
rlm@3 29 require_once(FileManagerUtility::getPath().'/Upload.php');
rlm@3 30 require_once(FileManagerUtility::getPath().'/Image.php');
rlm@3 31
rlm@3 32 class FileManager {
rlm@3 33
rlm@3 34 private $path = null,
rlm@3 35 $length = null,
rlm@3 36 $basedir = null,
rlm@3 37 $basename = null,
rlm@3 38 $options,
rlm@3 39 $post,
rlm@3 40 $get;
rlm@3 41
rlm@3 42 public function __construct($options){
rlm@3 43 $this->options = array_merge(array(
rlm@3 44 'directory' => '../../Gallery',
rlm@3 45 'assetBasePath' => '../Assets',
rlm@3 46 'dateFormat' => 'j M Y - H:i',
rlm@3 47 'maxUploadSize' => 1024*1024*3,
rlm@3 48 'upload' => false,
rlm@3 49 'destroy' => false,
rlm@3 50 'safe' => true,
rlm@3 51 'filter' => null,
rlm@3 52 ), $options);
rlm@3 53
rlm@3 54 $this->basedir = realpath($this->options['directory']);
rlm@3 55 $this->basename = pathinfo($this->basedir, PATHINFO_BASENAME).'/';
rlm@3 56 $this->path = realpath($this->options['directory'].'/../');
rlm@3 57 $this->length = strlen($this->path);
rlm@3 58
rlm@3 59 header('Expires: Fri, 01 Jan 1990 00:00:00 GMT');
rlm@3 60 header('Cache-Control: no-cache, no-store, max-age=0, must-revalidate');
rlm@3 61
rlm@3 62 $this->get = $_GET;
rlm@3 63 $this->post = $_POST;
rlm@3 64 }
rlm@3 65
rlm@3 66 public function fireEvent($event){
rlm@3 67 $event = $event ? 'on'.ucfirst($event) : null;
rlm@3 68 if(!$event || !method_exists($this, $event)) $event = 'onView';
rlm@3 69
rlm@3 70 $this->{$event}();
rlm@3 71 }
rlm@3 72
rlm@3 73 protected function onView(){
rlm@3 74 $dir = $this->getDir(!empty($this->post['directory']) ? $this->post['directory'] : null);
rlm@3 75 $files = ($files = glob($dir.'/*')) ? $files : array();
rlm@3 76
rlm@3 77 if($dir!=$this->basedir) array_unshift($files, $dir.'/..');
rlm@3 78 natcasesort($files);
rlm@3 79 foreach($files as $file){
rlm@3 80 $mime = $this->getMimeType($file);
rlm@3 81 if($this->options['filter'] && $mime!='text/directory' && !FileManagerUtility::startsWith($mime, $this->options['filter']))
rlm@3 82 continue;
rlm@3 83
rlm@3 84 $out[is_dir($file) ? 0 : 1][] = array(
rlm@3 85 'name' => pathinfo($file, PATHINFO_BASENAME),
rlm@3 86 'date' => date($this->options['dateFormat'], filemtime($file)),
rlm@3 87 'mime' => $this->getMimeType($file),
rlm@3 88 'icon' => $this->getIcon($this->normalize($file)),
rlm@3 89 'size' => filesize($file),
rlm@3 90 );
rlm@3 91 }
rlm@3 92
rlm@3 93 echo json_encode(array(
rlm@3 94 'path' => $this->getPath($dir),
rlm@3 95 'dir' => array(
rlm@3 96 'name' => pathinfo($dir, PATHINFO_BASENAME),
rlm@3 97 'date' => date($this->options['dateFormat'], filemtime($dir)),
rlm@3 98 'mime' => 'text/directory',
rlm@3 99 'icon' => 'dir',
rlm@3 100 ),
rlm@3 101 'files' => array_merge(!empty($out[0]) ? $out[0] : array(), !empty($out[1]) ? $out[1] : array()),
rlm@3 102 ));
rlm@3 103 }
rlm@3 104
rlm@3 105 protected function onDetail(){
rlm@3 106 if(empty($this->post['directory']) || empty($this->post['file'])) return;
rlm@3 107
rlm@3 108 $file = realpath($this->path.'/'.$this->post['directory'].'/'.$this->post['file']);
rlm@3 109 if(!$this->checkFile($file)) return;
rlm@3 110
rlm@3 111 require_once(FileManagerUtility::getPath().'/Assets/getid3/getid3.php');
rlm@3 112
rlm@3 113 $url = $this->normalize(substr($file, strlen($this->path)+1));
rlm@3 114 $mime = $this->getMimeType($file);
rlm@3 115 $content = null;
rlm@3 116 if(FileManagerUtility::startsWith($mime, 'image/')){
rlm@3 117 $size = getimagesize($file);
rlm@3 118 $content = '<img src="'.$url.'" class="preview" alt="" />
rlm@3 119 <h2>${more}</h2>
rlm@3 120 <dl>
rlm@3 121 <dt>${width}</dt><dd>'.$size[0].'px</dd>
rlm@3 122 <dt>${height}</dt><dd>'.$size[1].'px</dd>
rlm@3 123 </dl>';
rlm@3 124 }elseif(FileManagerUtility::startsWith($mime, 'text/') || $mime=='application/x-javascript'){
rlm@3 125 $filecontent = file_get_contents($file, null, null, 0, 300);
rlm@3 126 if(!FileManagerUtility::isBinary($filecontent)) $content = '<div class="textpreview">'.nl2br(str_replace(array('$', "\t"), array('&#36;', '&nbsp;&nbsp;'), htmlentities($filecontent))).'</div>';
rlm@3 127 }elseif($mime=='application/zip'){
rlm@3 128 $out = array(array(), array());
rlm@3 129 $getid3 = new getID3();
rlm@3 130 $getid3->Analyze($file);
rlm@3 131 foreach($getid3->info['zip']['files'] as $name => $size){
rlm@3 132 $icon = is_array($size) ? 'dir' : $this->getIcon($name);
rlm@3 133 $out[$icon=='dir' ? 0 : 1][$name] = '<li><a><img src="'.$this->options['assetBasePath'].'/Icons/'.$icon.'.png" alt="" /> '.$name.'</a></li>';
rlm@3 134 }
rlm@3 135 natcasesort($out[0]);
rlm@3 136 natcasesort($out[1]);
rlm@3 137 $content = '<ul>'.implode(array_merge($out[0], $out[1])).'</ul>';
rlm@3 138 }elseif(FileManagerUtility::startsWith($mime, 'audio/')){
rlm@3 139 $getid3 = new getID3();
rlm@3 140 $getid3->Analyze($file);
rlm@3 141
rlm@3 142 $content = '<div class="object">
rlm@3 143 <object type="application/x-shockwave-flash" data="'.$this->options['assetBasePath'].'/dewplayer.swf?mp3='.rawurlencode($url).'&volume=30" width="200" height="20">
rlm@3 144 <param name="movie" value="'.$this->options['assetBasePath'].'/dewplayer.swf?mp3='.rawurlencode($url).'&volume=30" />
rlm@3 145 </object>
rlm@3 146 </div>
rlm@3 147 <h2>${more}</h2>
rlm@3 148 <dl>
rlm@3 149 <dt>${title}</dt><dd>'.$getid3->info['comments']['title'][0].'</dd>
rlm@3 150 <dt>${artist}</dt><dd>'.$getid3->info['comments']['artist'][0].'</dd>
rlm@3 151 <dt>${album}</dt><dd>'.$getid3->info['comments']['album'][0].'</dd>
rlm@3 152 <dt>${length}</dt><dd>'.$getid3->info['playtime_string'].'</dd>
rlm@3 153 <dt>${bitrate}</dt><dd>'.round($getid3->info['bitrate']/1000).'kbps</dd>
rlm@3 154 </dl>';
rlm@3 155 }
rlm@3 156
rlm@3 157 echo json_encode(array(
rlm@3 158 'content' => $content ? $content : '<div class="margin">
rlm@3 159 ${nopreview}<br/><button value="'.$url.'">${download}</button>
rlm@3 160 </div>',
rlm@3 161 ));
rlm@3 162 }
rlm@3 163
rlm@3 164 protected function onDestroy(){
rlm@3 165 if(!$this->options['destroy'] || empty($this->post['directory']) || empty($this->post['file'])) return;
rlm@3 166
rlm@3 167 $file = realpath($this->path.'/'.$this->post['directory'].'/'.$this->post['file']);
rlm@3 168 if(!$this->checkFile($file)) return;
rlm@3 169
rlm@3 170 $this->unlink($file);
rlm@3 171
rlm@3 172 echo json_encode(array(
rlm@3 173 'content' => 'destroyed',
rlm@3 174 ));
rlm@3 175 }
rlm@3 176
rlm@3 177 protected function onCreate(){
rlm@3 178 if(empty($this->post['directory']) || empty($this->post['file'])) return;
rlm@3 179
rlm@3 180 $file = $this->getName($this->post['file'], $this->getDir($this->post['directory']));
rlm@3 181 if(!$file) return;
rlm@3 182
rlm@3 183 mkdir($file);
rlm@3 184
rlm@3 185 $this->onView();
rlm@3 186 }
rlm@3 187
rlm@3 188 protected function onUpload(){
rlm@3 189 try{
rlm@3 190 if(!$this->options['upload'])
rlm@3 191 throw new FileManagerException('disabled');
rlm@3 192 if(empty($this->get['directory']) || (function_exists('UploadIsAuthenticated') && !UploadIsAuthenticated($this->get)))
rlm@3 193 throw new FileManagerException('authenticated');
rlm@3 194
rlm@3 195 $dir = $this->getDir($this->get['directory']);
rlm@3 196 $name = pathinfo((Upload::exists('Filedata')) ? $this->getName($_FILES['Filedata']['name'], $dir) : null, PATHINFO_FILENAME);
rlm@3 197 $file = Upload::move('Filedata', $dir.'/', array(
rlm@3 198 'name' => $name,
rlm@3 199 '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 200 'size' => $this->options['maxUploadSize'],
rlm@3 201 'mimes' => $this->getAllowedMimeTypes(),
rlm@3 202 ));
rlm@3 203
rlm@3 204 if(FileManagerUtility::startsWith(Upload::mime($file), 'image/') && !empty($this->get['resize'])){
rlm@3 205 $img = new Image($file);
rlm@3 206 $size = $img->getSize();
rlm@3 207 if($size['width']>800) $img->resize(800)->save();
rlm@3 208 elseif($size['height']>600) $img->resize(null, 600)->save();
rlm@3 209 }
rlm@3 210
rlm@3 211 echo json_encode(array(
rlm@3 212 'status' => 1,
rlm@3 213 'name' => pathinfo($file, PATHINFO_BASENAME),
rlm@3 214 ));
rlm@3 215 }catch(UploadException $e){
rlm@3 216 echo json_encode(array(
rlm@3 217 'status' => 0,
rlm@3 218 'error' => class_exists('ValidatorException') ? $e->getMessage() : '${upload.'.$e->getMessage().'}', // This is for Styx :)
rlm@3 219 ));
rlm@3 220 }catch(FileManagerException $e){
rlm@3 221 echo json_encode(array(
rlm@3 222 'status' => 0,
rlm@3 223 'error' => '${upload.'.$e->getMessage().'}',
rlm@3 224 ));
rlm@3 225 }
rlm@3 226 }
rlm@3 227
rlm@3 228 /* This method is used by both move and rename */
rlm@3 229 protected function onMove(){
rlm@3 230 if(empty($this->post['directory']) || empty($this->post['file'])) return;
rlm@3 231
rlm@3 232 $rename = empty($this->post['newDirectory']) && !empty($this->post['name']);
rlm@3 233 $dir = $this->getDir($this->post['directory']);
rlm@3 234 $file = realpath($dir.'/'.$this->post['file']);
rlm@3 235
rlm@3 236 $is_dir = is_dir($file);
rlm@3 237 if(!$this->checkFile($file) || (!$rename && $is_dir))
rlm@3 238 return;
rlm@3 239
rlm@3 240 if($rename || $is_dir){
rlm@3 241 if(empty($this->post['name'])) return;
rlm@3 242 $newname = $this->getName($this->post['name'], $dir);
rlm@3 243 $fn = 'rename';
rlm@3 244 }else{
rlm@3 245 $newname = $this->getName(pathinfo($file, PATHINFO_FILENAME), $this->getDir($this->post['newDirectory']));
rlm@3 246 $fn = !empty($this->post['copy']) ? 'copy' : 'rename';
rlm@3 247 }
rlm@3 248
rlm@3 249 if(!$newname) return;
rlm@3 250
rlm@3 251 $ext = pathinfo($file, PATHINFO_EXTENSION);
rlm@3 252 if($ext) $newname .= '.'.$ext;
rlm@3 253 $fn($file, $newname);
rlm@3 254
rlm@3 255 echo json_encode(array(
rlm@3 256 'name' => pathinfo($this->normalize($newname), PATHINFO_BASENAME),
rlm@3 257 ));
rlm@3 258 }
rlm@3 259
rlm@3 260 protected function unlink($file){
rlm@3 261 $file = realpath($file);
rlm@3 262 if($this->basedir==$file || strlen($this->basedir)>=strlen($file))
rlm@3 263 return;
rlm@3 264
rlm@3 265 if(is_dir($file)){
rlm@3 266 $files = glob($file.'/*');
rlm@3 267 if(is_array($files))
rlm@3 268 foreach($files as $f)
rlm@3 269 $this->unlink($f);
rlm@3 270
rlm@3 271 rmdir($file);
rlm@3 272 }else{
rlm@3 273 try{ if($this->checkFile($file)) unlink($file); }catch(Exception $e){}
rlm@3 274 }
rlm@3 275 }
rlm@3 276
rlm@3 277 protected function getName($file, $dir){
rlm@3 278 $files = array();
rlm@3 279 foreach((array)glob($dir.'/*') as $f)
rlm@3 280 $files[] = pathinfo($f, PATHINFO_FILENAME);
rlm@3 281
rlm@3 282 $pathinfo = pathinfo($file);
rlm@3 283 $file = $dir.'/'.FileManagerUtility::pagetitle($pathinfo['filename'], $files).(!empty($pathinfo['extension']) ? '.'.$pathinfo['extension'] : null);
rlm@3 284
rlm@3 285 return !$file || !FileManagerUtility::startsWith($file, $this->basedir) || file_exists($file) ? null : $file;
rlm@3 286 }
rlm@3 287
rlm@3 288 protected function getIcon($file){
rlm@3 289 if(FileManagerUtility::endsWith($file, '/..')) return 'dir_up';
rlm@3 290 else if(is_dir($file)) return 'dir';
rlm@3 291
rlm@3 292 $ext = pathinfo($file, PATHINFO_EXTENSION);
rlm@3 293 return ($ext && file_exists(realpath($this->options['assetBasePath'].'/Icons/'.$ext.'.png'))) ? $ext : 'default';
rlm@3 294 }
rlm@3 295
rlm@3 296 protected function getMimeType($file){
rlm@3 297 return is_dir($file) ? 'text/directory' : Upload::mime($file);
rlm@3 298 }
rlm@3 299
rlm@3 300 protected function getDir($dir){
rlm@3 301 $dir = realpath($this->path.'/'.(FileManagerUtility::startsWith($dir, $this->basename) ? $dir : $this->basename));
rlm@3 302 return $this->checkFile($dir) ? $dir : $this->basedir;
rlm@3 303 }
rlm@3 304
rlm@3 305 protected function getPath($file){
rlm@3 306 $file = $this->normalize(substr($file, $this->length));
rlm@3 307 return substr($file, FileManagerUtility::startsWith($file, '/') ? 1 : 0);
rlm@3 308 }
rlm@3 309
rlm@3 310 protected function checkFile($file){
rlm@3 311 $mimes = $this->getAllowedMimeTypes();
rlm@3 312 $hasFilter = $this->options['filter'] && count($mimes);
rlm@3 313 if($hasFilter) array_push($mimes, 'text/directory');
rlm@3 314 return !(!$file || !FileManagerUtility::startsWith($file, $this->basedir) || !file_exists($file) || ($hasFilter && !in_array($this->getMimeType($file), $mimes)));
rlm@3 315 }
rlm@3 316
rlm@3 317 protected function normalize($file){
rlm@3 318 return preg_replace('/\\\|\/{2,}/', '/', $file);
rlm@3 319 }
rlm@3 320
rlm@3 321 protected function getAllowedMimeTypes(){
rlm@3 322 $filter = $this->options['filter'];
rlm@3 323
rlm@3 324 if(!$filter) return null;
rlm@3 325 if(!FileManagerUtility::endsWith($filter, '/')) return array($filter);
rlm@3 326
rlm@3 327 static $mimes;
rlm@3 328 if(!$mimes) $mimes = parse_ini_file(FileManagerUtility::getPath().'/MimeTypes.ini');
rlm@3 329
rlm@3 330 foreach($mimes as $mime)
rlm@3 331 if(FileManagerUtility::startsWith($mime, $filter))
rlm@3 332 $mimeTypes[] = strtolower($mime);
rlm@3 333
rlm@3 334 return $mimeTypes;
rlm@3 335 }
rlm@3 336
rlm@3 337 }
rlm@3 338
rlm@3 339 class FileManagerException extends Exception {}
rlm@3 340
rlm@3 341 /* Stripped-down version of some Styx PHP Framework-Functionality bundled with this FileBrowser. Styx is located at: http://styx.og5.net */
rlm@3 342 class FileManagerUtility {
rlm@3 343
rlm@3 344 public static function endsWith($string, $look){
rlm@3 345 return strrpos($string, $look)===strlen($string)-strlen($look);
rlm@3 346 }
rlm@3 347
rlm@3 348 public static function startsWith($string, $look){
rlm@3 349 return strpos($string, $look)===0;
rlm@3 350 }
rlm@3 351
rlm@3 352 public static function pagetitle($data, $options = array()){
rlm@3 353 static $regex;
rlm@3 354 if(!$regex){
rlm@3 355 $regex = array(
rlm@3 356 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;'),
rlm@3 357 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 358 );
rlm@3 359
rlm@3 360 $regex[0][] = '"';
rlm@3 361 $regex[0][] = "'";
rlm@3 362 }
rlm@3 363
rlm@3 364 $data = trim(substr(preg_replace('/(?:[^A-z0-9]|_|\^)+/i', '_', str_replace($regex[0], $regex[1], $data)), 0, 64), '_');
rlm@3 365 return !empty($options) ? self::checkTitle($data, $options) : $data;
rlm@3 366 }
rlm@3 367
rlm@3 368 protected static function checkTitle($data, $options = array(), $i = 0){
rlm@3 369 if(!is_array($options)) return $data;
rlm@3 370
rlm@3 371 foreach($options as $content)
rlm@3 372 if($content && strtolower($content)==strtolower($data.($i ? '_'.$i : '')))
rlm@3 373 return self::checkTitle($data, $options, ++$i);
rlm@3 374
rlm@3 375 return $data.($i ? '_'.$i : '');
rlm@3 376 }
rlm@3 377
rlm@3 378 public static function isBinary($str){
rlm@3 379 $array = array(0, 255);
rlm@3 380 for($i = 0; $i < strlen($str); $i++)
rlm@3 381 if(in_array(ord($str[$i]), $array)) return true;
rlm@3 382
rlm@3 383 return false;
rlm@3 384 }
rlm@3 385
rlm@3 386 public static function getPath(){
rlm@3 387 static $path;
rlm@3 388 return $path ? $path : $path = pathinfo(__FILE__, PATHINFO_DIRNAME);
rlm@3 389 }
rlm@3 390
rlm@3 391 }