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