1<?php 2/** 3 * DokuWiki media passthrough file 4 * 5 * @license GPL 2 (http://www.gnu.org/licenses/gpl.html) 6 * @author Andreas Gohr <andi@splitbrain.org> 7 */ 8 9 if(!defined('DOKU_INC')) define('DOKU_INC',realpath(dirname(__FILE__).'/../../').'/'); 10 require_once(DOKU_INC.'inc/init.php'); 11 require_once(DOKU_INC.'inc/common.php'); 12 require_once(DOKU_INC.'inc/pageutils.php'); 13 require_once(DOKU_INC.'inc/confutils.php'); 14 require_once(DOKU_INC.'inc/auth.php'); 15 //close sesseion 16 session_write_close(); 17 18 $mimetypes = getMimeTypes(); 19 20 //get input 21 $MEDIA = getID('media',false); // no cleaning - maybe external 22 $CACHE = calc_cache($_REQUEST['cache']); 23 $WIDTH = $_REQUEST['w']; 24 $HEIGHT = $_REQUEST['h']; 25 list($EXT,$MIME) = mimetype($MEDIA); 26 if($EXT === false){ 27 $EXT = 'unknown'; 28 $MIME = 'application/octet-stream'; 29 } 30 31 //media to local file 32 if(preg_match('#^(https?|ftp)://#i',$MEDIA)){ 33 //handle external media 34 $FILE = get_from_URL($MEDIA,$EXT,$CACHE); 35 if(!$FILE){ 36 //download failed - redirect to original URL 37 header('Location: '.$MEDIA); 38 exit; 39 } 40 }else{ 41 $MEDIA = cleanID($MEDIA); 42 if(empty($MEDIA)){ 43 header("HTTP/1.0 400 Bad Request"); 44 print 'Bad request'; 45 exit; 46 } 47 48 //check permissions (namespace only) 49 if(auth_quickaclcheck(getNS($MEDIA).':X') < AUTH_READ){ 50 header("HTTP/1.0 401 Unauthorized"); 51 //fixme add some image for imagefiles 52 print 'Unauthorized'; 53 exit; 54 } 55 $FILE = mediaFN($MEDIA); 56 } 57 58 //check file existance 59 if(!@file_exists($FILE)){ 60 header("HTTP/1.0 404 Not Found"); 61 //FIXME add some default broken image 62 print 'Not Found'; 63 exit; 64 } 65 66 //handle image resizing 67 if((substr($MIME,0,5) == 'image') && $WIDTH){ 68 $FILE = get_resized($FILE,$EXT,$WIDTH,$HEIGHT); 69 } 70 71 72 //FIXME set sane cachecontrol headers 73 //FIXME handle conditional and partial requests 74 75 //send file 76 header("Content-Type: $MIME"); 77 header('Last-Modified: '.date('r',filemtime($FILE))); 78 header('Content-Length: '.filesize($FILE)); 79 header('Cache-Control: private, must-revalidate, post-check=0, pre-check=0'); 80 81 //application mime type is downloadable 82 if(substr($MIME,0,11) == 'application'){ 83 header('Content-Disposition: attachment; filename="'.basename($FILE).'"'); 84 } 85 86 $fp = @fopen($FILE,"rb"); 87 if($fp){ 88 while (!feof($fp)) { 89 @set_time_limit(); // large files can take a lot of time 90 print fread($fp, 16*1024); 91 flush(); 92 } 93 fclose($fp); 94 }else{ 95 header("HTTP/1.0 500 Internal Server Error"); 96 print "Could not read $FILE - bad permissions?"; 97 } 98 99/* ------------------------------------------------------------------------ */ 100 101/** 102 * Resizes the given image to the given size 103 * 104 * @author Andreas Gohr <andi@splitbrain.org> 105 */ 106function get_resized($file, $ext, $w, $h=0){ 107 global $conf; 108 109 $info = getimagesize($file); 110 if(!$h) $h = round(($w * $info[1]) / $info[0]); 111 112 113 //cache 114 $local = getCacheName($file,'.media.'.$w.'x'.$h.'.'.$ext); 115 $mtime = @filemtime($local); // 0 if not exists 116 117 if( $mtime > filemtime($file) || 118 resize_imageIM($ext,$file,$info[0],$info[1],$local,$w,$h) || 119 resize_imageGD($ext,$file,$info[0],$info[1],$local,$w,$h) ){ 120 return $local; 121 } 122 //still here? resizing failed 123 return $file; 124} 125 126/** 127 * Returns the wanted cachetime in seconds 128 * 129 * Resolves named constants 130 * 131 * @author Andreas Gohr <andi@splitbrain.org> 132 */ 133function calc_cache($cache){ 134 global $conf; 135 136 if(strtolower($cache) == 'nocache') return 0; //never cache 137 if(strtolower($cache) == 'recache') return $conf['cachetime']; //use standard cache 138 return -1; //cache endless 139} 140 141/** 142 * Download a remote file and return local filename 143 * 144 * returns false if download fails. Uses cached file if available and 145 * wanted 146 * 147 * @author Andreas Gohr <andi@splitbrain.org> 148 * @author Pavel Vitis <Pavel.Vitis@seznam.cz> 149 */ 150function get_from_URL($url,$ext,$cache){ 151 global $conf; 152 153 $local = getCacheName(strtolower($url),".media.$ext"); 154 $mtime = @filemtime($local); // 0 if not exists 155 156 //decide if download needed: 157 if( $cache == 0 || // never cache 158 ($mtime != 0 && $cache != -1) || // exists but no endless cache 159 ($mtime == 0) || // not exists 160 ($cache != -1 && $mtime < time()-$cache) // expired 161 ){ 162 if(io_download($url,$local)){ 163 return $local; 164 }else{ 165 return false; 166 } 167 } 168 169 //if cache exists use it else 170 if($mtime) return $local; 171 172 //else return false 173 return false; 174} 175 176/** 177 * resize images using external ImageMagick convert program 178 * 179 * @author Pavel Vitis <Pavel.Vitis@seznam.cz> 180 * @author Andreas Gohr <andi@splitbrain.org> 181 */ 182function resize_imageIM($ext,$from,$from_w,$from_h,$to,$to_w,$to_h){ 183 global $conf; 184 185 // check if convert is configured 186 if(!$conf['im_convert']) return false; 187 188 // prepare command 189 $cmd = $conf['im_convert']; 190 $cmd .= ' -resize '.$to_w.'x'.$to_h.'!'; 191 $cmd .= " $from $to"; 192 193 @exec($cmd,$out,$retval); 194 if ($retval == 0) return true; 195 196 return false; 197} 198 199/** 200 * resize images using PHP's libGD support 201 * 202 * @author Andreas Gohr <andi@splitbrain.org> 203 */ 204function resize_imageGD($ext,$from,$from_w,$from_h,$to,$to_w,$to_h){ 205 global $conf; 206 207 if($conf['gdlib'] < 1) return false; //no GDlib available or wanted 208 209 // check available memory 210 if(!is_mem_available(($from_w * $from_h * 4) + ($to_w * $to_h * 4))){ 211 return false; 212 } 213 214 // create an image of the given filetype 215 if ($ext == 'jpg' || $ext == 'jpeg'){ 216 if(!function_exists("imagecreatefromjpeg")) return false; 217 $image = @imagecreatefromjpeg($from); 218 }elseif($ext == 'png') { 219 if(!function_exists("imagecreatefrompng")) return false; 220 $image = @imagecreatefrompng($from); 221 222 }elseif($ext == 'gif') { 223 if(!function_exists("imagecreatefromgif")) return false; 224 $image = @imagecreatefromgif($from); 225 } 226 if(!$image) return false; 227 228 if(($conf['gdlib']>1) && function_exists("imagecreatetruecolor")){ 229 $newimg = @imagecreatetruecolor ($to_w, $to_h); 230 } 231 if(!$newimg) $newimg = @imagecreate($to_w, $to_h); 232 if(!$newimg){ 233 imagedestroy($image); 234 return false; 235 } 236 237 //keep png alpha channel if possible 238 if($ext == 'png' && $conf['gdlib']>1 && function_exists('imagesavealpha')){ 239 imagealphablending($newimg, false); 240 imagesavealpha($newimg,true); 241 } 242 243 //try resampling first 244 if(function_exists("imagecopyresampled")){ 245 if(!@imagecopyresampled($newimg, $image, 0, 0, 0, 0, $to_w, $to_h, $from_w, $from_h)) { 246 imagecopyresized($newimg, $image, 0, 0, 0, 0, $to_w, $to_h, $from_w, $from_h); 247 } 248 }else{ 249 imagecopyresized($newimg, $image, 0, 0, 0, 0, $to_w, $to_h, $from_w, $from_h); 250 } 251 252 $okay = false; 253 if ($ext == 'jpg' || $ext == 'jpeg'){ 254 if(!function_exists('imagejpeg')){ 255 $okay = false; 256 }else{ 257 $okay = imagejpeg($newimg, $to, 70); 258 } 259 }elseif($ext == 'png') { 260 if(!function_exists('imagepng')){ 261 $okay = false; 262 }else{ 263 $okay = imagepng($newimg, $to); 264 } 265 }elseif($ext == 'gif') { 266 if(!function_exists('imagegif')){ 267 $okay = false; 268 }else{ 269 $okay = imagegif($newimg, $to); 270 } 271 } 272 273 // destroy GD image ressources 274 if($image) imagedestroy($image); 275 if($newimg) imagedestroy($newimg); 276 277 return $okay; 278} 279 280/** 281 * Checks if the given amount of memory is available 282 * 283 * If the memory_get_usage() function is not available the 284 * function just assumes $used bytes of already allocated memory 285 * 286 * @param int $mem Size of memory you want to allocate in bytes 287 * @param int $used already allocated memory (see above) 288 * @author Filip Oscadal <webmaster@illusionsoftworks.cz> 289 * @author Andreas Gohr <andi@splitbrain.org> 290 */ 291function is_mem_available($mem,$bytes=1048576){ 292 $limit = trim(ini_get('memory_limit')); 293 if(empty($limit)) return true; // no limit set! 294 295 // parse limit to bytes 296 $unit = strtolower(substr($limit,-1)); 297 switch($unit){ 298 case 'g': 299 $limit = substr($limit,0,-1); 300 $limit *= 1024*1024*1024; 301 break; 302 case 'm': 303 $limit = substr($limit,0,-1); 304 $limit *= 1024*1024; 305 break; 306 case 'k': 307 $limit = substr($limit,0,-1); 308 $limit *= 1024; 309 break; 310 } 311 312 // get used memory if possible 313 if(function_exists('memory_get_usage')){ 314 $used = memory_get_usage(); 315 } 316 317 318 if($used+$mem > $limit){ 319 return false; 320 } 321 322 return true; 323} 324 325//Setup VIM: ex: et ts=2 enc=utf-8 : 326?> 327