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 if(!defined('CHUNK_SIZE')) define('CHUNK_SIZE',16*1024); 18 19 $mimetypes = getMimeTypes(); 20 21 //get input 22 $MEDIA = getID('media',false); // no cleaning - maybe external 23 $CACHE = calc_cache($_REQUEST['cache']); 24 $WIDTH = $_REQUEST['w']; 25 $HEIGHT = $_REQUEST['h']; 26 list($EXT,$MIME) = mimetype($MEDIA); 27 if($EXT === false){ 28 $EXT = 'unknown'; 29 $MIME = 'application/octet-stream'; 30 } 31 32 //media to local file 33 if(preg_match('#^(https?|ftp)://#i',$MEDIA)){ 34 //handle external media 35 $FILE = get_from_URL($MEDIA,$EXT,$CACHE); 36 if(!$FILE){ 37 //download failed - redirect to original URL 38 header('Location: '.$MEDIA); 39 exit; 40 } 41 }else{ 42 $MEDIA = cleanID($MEDIA); 43 if(empty($MEDIA)){ 44 header("HTTP/1.0 400 Bad Request"); 45 print 'Bad request'; 46 exit; 47 } 48 49 //check permissions (namespace only) 50 if(auth_quickaclcheck(getNS($MEDIA).':X') < AUTH_READ){ 51 header("HTTP/1.0 401 Unauthorized"); 52 //fixme add some image for imagefiles 53 print 'Unauthorized'; 54 exit; 55 } 56 $FILE = mediaFN($MEDIA); 57 } 58 59 //check file existance 60 if(!@file_exists($FILE)){ 61 header("HTTP/1.0 404 Not Found"); 62 //FIXME add some default broken image 63 print 'Not Found'; 64 exit; 65 } 66 67 //handle image resizing 68 if((substr($MIME,0,5) == 'image') && $WIDTH){ 69 $FILE = get_resized($FILE,$EXT,$WIDTH,$HEIGHT); 70 } 71 72 // finally send the file to the client 73 sendFile($FILE,$MIME,$CACHE); 74 75/* ------------------------------------------------------------------------ */ 76 77/** 78 * Set headers and send the file to the client 79 * 80 * @author Andreas Gohr <andi@splitbrain.org> 81 * @author Ben Coburn <btcoburn@silicodon.net> 82 */ 83function sendFile($file,$mime,$cache){ 84 global $conf; 85 $fmtime = filemtime($file); 86 // send headers 87 header("Content-Type: $mime"); 88 // smart http caching headers 89 if ($cache==-1) { 90 // cache 91 // cachetime or one hour 92 header('Expires: '.gmdate("D, d M Y H:i:s", time()+max($conf['cachetime'], 3600)).' GMT'); 93 header('Cache-Control: public, proxy-revalidate, no-transform, max-age='.max($conf['cachetime'], 3600)); 94 header('Pragma: public'); 95 } else if ($cache>0) { 96 // recache 97 // remaining cachetime + 10 seconds so the newly recached media is used 98 header('Expires: '.gmdate("D, d M Y H:i:s", $fmtime+$conf['cachetime']+10).' GMT'); 99 header('Cache-Control: public, proxy-revalidate, no-transform, max-age='.max($fmtime-time()+$conf['cachetime']+10, 0)); 100 header('Pragma: public'); 101 } else if ($cache==0) { 102 // nocache 103 header('Cache-Control: must-revalidate, no-transform, post-check=0, pre-check=0'); 104 header('Pragma: public'); 105 } 106 header('Accept-Ranges: bytes'); 107 //send important headers first, script stops here if '304 Not Modified' response 108 http_conditionalRequest($fmtime); 109 list($start,$len) = http_rangeRequest(filesize($file)); 110 111 //application mime type is downloadable 112 if(substr($mime,0,11) == 'application'){ 113 header('Content-Disposition: attachment; filename="'.basename($file).'";'); 114 } 115 116 // send file contents 117 $fp = @fopen($file,"rb"); 118 if($fp){ 119 fseek($fp,$start); //seek to start of range 120 121 $chunk = ($len > CHUNK_SIZE) ? CHUNK_SIZE : $len; 122 while (!feof($fp) && $chunk > 0) { 123 @set_time_limit(); // large files can take a lot of time 124 print fread($fp, $chunk); 125 flush(); 126 $len -= $chunk; 127 $chunk = ($len > CHUNK_SIZE) ? CHUNK_SIZE : $len; 128 } 129 fclose($fp); 130 }else{ 131 header("HTTP/1.0 500 Internal Server Error"); 132 print "Could not read $file - bad permissions?"; 133 } 134} 135 136/** 137 * Checks and sets headers to handle range requets 138 * 139 * @author Andreas Gohr <andi@splitbrain.org> 140 * @returns array The start byte and the amount of bytes to send 141 */ 142function http_rangeRequest($size){ 143 if(!isset($_SERVER['HTTP_RANGE'])){ 144 // no range requested - send the whole file 145 header("Content-Length: $size"); 146 return array(0,$size); 147 } 148 149 $t = explode('=', $_SERVER['HTTP_RANGE']); 150 if (!$t[0]=='bytes') { 151 // we only understand byte ranges - send the whole file 152 header("Content-Length: $size"); 153 return array(0,$size); 154 } 155 156 $r = explode('-', $t[1]); 157 $start = (int)$r[0]; 158 $end = (int)$r[1]; 159 if (!$end) $end = $size - 1; 160 if ($start > $end || $start > $size || $end > $size){ 161 header('HTTP/1.1 416 Requested Range Not Satisfiable'); 162 print 'Bad Range Request!'; 163 exit; 164 } 165 166 $tot = $end - $start + 1; 167 header('HTTP/1.1 206 Partial Content'); 168 header("Content-Range: bytes {$start}-{$end}/{$size}"); 169 header("Content-Length: $tot"); 170 171 return array($start,$tot); 172} 173 174/** 175 * Resizes the given image to the given size 176 * 177 * @author Andreas Gohr <andi@splitbrain.org> 178 */ 179function get_resized($file, $ext, $w, $h=0){ 180 global $conf; 181 182 $info = getimagesize($file); 183 if(!$h) $h = round(($w * $info[1]) / $info[0]); 184 185 186 //cache 187 $local = getCacheName($file,'.media.'.$w.'x'.$h.'.'.$ext); 188 $mtime = @filemtime($local); // 0 if not exists 189 190 if( $mtime > filemtime($file) || 191 resize_imageIM($ext,$file,$info[0],$info[1],$local,$w,$h) || 192 resize_imageGD($ext,$file,$info[0],$info[1],$local,$w,$h) ){ 193 return $local; 194 } 195 //still here? resizing failed 196 return $file; 197} 198 199/** 200 * Returns the wanted cachetime in seconds 201 * 202 * Resolves named constants 203 * 204 * @author Andreas Gohr <andi@splitbrain.org> 205 */ 206function calc_cache($cache){ 207 global $conf; 208 209 if(strtolower($cache) == 'nocache') return 0; //never cache 210 if(strtolower($cache) == 'recache') return $conf['cachetime']; //use standard cache 211 return -1; //cache endless 212} 213 214/** 215 * Download a remote file and return local filename 216 * 217 * returns false if download fails. Uses cached file if available and 218 * wanted 219 * 220 * @author Andreas Gohr <andi@splitbrain.org> 221 * @author Pavel Vitis <Pavel.Vitis@seznam.cz> 222 */ 223function get_from_URL($url,$ext,$cache){ 224 global $conf; 225 226 // if 'nocache' just redirect 227 if ($cache==0) { return false; } 228 229 $local = getCacheName(strtolower($url),".media.$ext"); 230 $mtime = @filemtime($local); // 0 if not exists 231 232 //decide if download needed: 233 if( ($mtime == 0) || // cache does not exist 234 ($cache != -1 && $mtime < time()-$cache) // 'recache' and cache has expired 235 ){ 236 if(io_download($url,$local)){ 237 return $local; 238 }else{ 239 return false; 240 } 241 } 242 243 //if cache exists use it else 244 if($mtime) return $local; 245 246 //else return false 247 return false; 248} 249 250/** 251 * resize images using external ImageMagick convert program 252 * 253 * @author Pavel Vitis <Pavel.Vitis@seznam.cz> 254 * @author Andreas Gohr <andi@splitbrain.org> 255 */ 256function resize_imageIM($ext,$from,$from_w,$from_h,$to,$to_w,$to_h){ 257 global $conf; 258 259 // check if convert is configured 260 if(!$conf['im_convert']) return false; 261 262 // prepare command 263 $cmd = $conf['im_convert']; 264 $cmd .= ' -resize '.$to_w.'x'.$to_h.'!'; 265 if ($ext == 'jpg' || $ext == 'jpeg') { 266 $cmd .= ' -quality '.$conf['jpg_quality']; 267 } 268 $cmd .= " $from $to"; 269 270 @exec($cmd,$out,$retval); 271 if ($retval == 0) return true; 272 273 return false; 274} 275 276/** 277 * resize images using PHP's libGD support 278 * 279 * @author Andreas Gohr <andi@splitbrain.org> 280 */ 281function resize_imageGD($ext,$from,$from_w,$from_h,$to,$to_w,$to_h){ 282 global $conf; 283 284 if($conf['gdlib'] < 1) return false; //no GDlib available or wanted 285 286 // check available memory 287 if(!is_mem_available(($from_w * $from_h * 4) + ($to_w * $to_h * 4))){ 288 return false; 289 } 290 291 // create an image of the given filetype 292 if ($ext == 'jpg' || $ext == 'jpeg'){ 293 if(!function_exists("imagecreatefromjpeg")) return false; 294 $image = @imagecreatefromjpeg($from); 295 }elseif($ext == 'png') { 296 if(!function_exists("imagecreatefrompng")) return false; 297 $image = @imagecreatefrompng($from); 298 299 }elseif($ext == 'gif') { 300 if(!function_exists("imagecreatefromgif")) return false; 301 $image = @imagecreatefromgif($from); 302 } 303 if(!$image) return false; 304 305 if(($conf['gdlib']>1) && function_exists("imagecreatetruecolor")){ 306 $newimg = @imagecreatetruecolor ($to_w, $to_h); 307 } 308 if(!$newimg) $newimg = @imagecreate($to_w, $to_h); 309 if(!$newimg){ 310 imagedestroy($image); 311 return false; 312 } 313 314 //keep png alpha channel if possible 315 if($ext == 'png' && $conf['gdlib']>1 && function_exists('imagesavealpha')){ 316 imagealphablending($newimg, false); 317 imagesavealpha($newimg,true); 318 } 319 320 //try resampling first 321 if(function_exists("imagecopyresampled")){ 322 if(!@imagecopyresampled($newimg, $image, 0, 0, 0, 0, $to_w, $to_h, $from_w, $from_h)) { 323 imagecopyresized($newimg, $image, 0, 0, 0, 0, $to_w, $to_h, $from_w, $from_h); 324 } 325 }else{ 326 imagecopyresized($newimg, $image, 0, 0, 0, 0, $to_w, $to_h, $from_w, $from_h); 327 } 328 329 $okay = false; 330 if ($ext == 'jpg' || $ext == 'jpeg'){ 331 if(!function_exists('imagejpeg')){ 332 $okay = false; 333 }else{ 334 $okay = imagejpeg($newimg, $to, $conf['jpg_quality']); 335 } 336 }elseif($ext == 'png') { 337 if(!function_exists('imagepng')){ 338 $okay = false; 339 }else{ 340 $okay = imagepng($newimg, $to); 341 } 342 }elseif($ext == 'gif') { 343 if(!function_exists('imagegif')){ 344 $okay = false; 345 }else{ 346 $okay = imagegif($newimg, $to); 347 } 348 } 349 350 // destroy GD image ressources 351 if($image) imagedestroy($image); 352 if($newimg) imagedestroy($newimg); 353 354 return $okay; 355} 356 357/** 358 * Checks if the given amount of memory is available 359 * 360 * If the memory_get_usage() function is not available the 361 * function just assumes $used bytes of already allocated memory 362 * 363 * @param int $mem Size of memory you want to allocate in bytes 364 * @param int $used already allocated memory (see above) 365 * @author Filip Oscadal <webmaster@illusionsoftworks.cz> 366 * @author Andreas Gohr <andi@splitbrain.org> 367 */ 368function is_mem_available($mem,$bytes=1048576){ 369 $limit = trim(ini_get('memory_limit')); 370 if(empty($limit)) return true; // no limit set! 371 372 // parse limit to bytes 373 $unit = strtolower(substr($limit,-1)); 374 switch($unit){ 375 case 'g': 376 $limit = substr($limit,0,-1); 377 $limit *= 1024*1024*1024; 378 break; 379 case 'm': 380 $limit = substr($limit,0,-1); 381 $limit *= 1024*1024; 382 break; 383 case 'k': 384 $limit = substr($limit,0,-1); 385 $limit *= 1024; 386 break; 387 } 388 389 // get used memory if possible 390 if(function_exists('memory_get_usage')){ 391 $used = memory_get_usage(); 392 } 393 394 395 if($used+$mem > $limit){ 396 return false; 397 } 398 399 return true; 400} 401 402//Setup VIM: ex: et ts=2 enc=utf-8 : 403?> 404