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 'nocache' just redirect 228 if ($cache==0) { return false; } 229 230 $local = getCacheName(strtolower($url),".media.$ext"); 231 $mtime = @filemtime($local); // 0 if not exists 232 233 //decide if download needed: 234 if( ($mtime == 0) || // cache does not exist 235 ($cache != -1 && $mtime < time()-$cache) // 'recache' and cache has expired 236 ){ 237 if(io_download($url,$local)){ 238 return $local; 239 }else{ 240 return false; 241 } 242 } 243 244 //if cache exists use it else 245 if($mtime) return $local; 246 247 //else return false 248 return false; 249} 250 251/** 252 * resize images using external ImageMagick convert program 253 * 254 * @author Pavel Vitis <Pavel.Vitis@seznam.cz> 255 * @author Andreas Gohr <andi@splitbrain.org> 256 */ 257function resize_imageIM($ext,$from,$from_w,$from_h,$to,$to_w,$to_h){ 258 global $conf; 259 260 // check if convert is configured 261 if(!$conf['im_convert']) return false; 262 263 // prepare command 264 $cmd = $conf['im_convert']; 265 $cmd .= ' -resize '.$to_w.'x'.$to_h.'!'; 266 if ($ext == 'jpg' || $ext == 'jpeg') { 267 $cmd .= ' -quality '.$conf['jpg_quality']; 268 } 269 $cmd .= " $from $to"; 270 271 @exec($cmd,$out,$retval); 272 if ($retval == 0) return true; 273 274 return false; 275} 276 277/** 278 * resize images using PHP's libGD support 279 * 280 * @author Andreas Gohr <andi@splitbrain.org> 281 */ 282function resize_imageGD($ext,$from,$from_w,$from_h,$to,$to_w,$to_h){ 283 global $conf; 284 285 if($conf['gdlib'] < 1) return false; //no GDlib available or wanted 286 287 // check available memory 288 if(!is_mem_available(($from_w * $from_h * 4) + ($to_w * $to_h * 4))){ 289 return false; 290 } 291 292 // create an image of the given filetype 293 if ($ext == 'jpg' || $ext == 'jpeg'){ 294 if(!function_exists("imagecreatefromjpeg")) return false; 295 $image = @imagecreatefromjpeg($from); 296 }elseif($ext == 'png') { 297 if(!function_exists("imagecreatefrompng")) return false; 298 $image = @imagecreatefrompng($from); 299 300 }elseif($ext == 'gif') { 301 if(!function_exists("imagecreatefromgif")) return false; 302 $image = @imagecreatefromgif($from); 303 } 304 if(!$image) return false; 305 306 if(($conf['gdlib']>1) && function_exists("imagecreatetruecolor")){ 307 $newimg = @imagecreatetruecolor ($to_w, $to_h); 308 } 309 if(!$newimg) $newimg = @imagecreate($to_w, $to_h); 310 if(!$newimg){ 311 imagedestroy($image); 312 return false; 313 } 314 315 //keep png alpha channel if possible 316 if($ext == 'png' && $conf['gdlib']>1 && function_exists('imagesavealpha')){ 317 imagealphablending($newimg, false); 318 imagesavealpha($newimg,true); 319 } 320 321 //try resampling first 322 if(function_exists("imagecopyresampled")){ 323 if(!@imagecopyresampled($newimg, $image, 0, 0, 0, 0, $to_w, $to_h, $from_w, $from_h)) { 324 imagecopyresized($newimg, $image, 0, 0, 0, 0, $to_w, $to_h, $from_w, $from_h); 325 } 326 }else{ 327 imagecopyresized($newimg, $image, 0, 0, 0, 0, $to_w, $to_h, $from_w, $from_h); 328 } 329 330 $okay = false; 331 if ($ext == 'jpg' || $ext == 'jpeg'){ 332 if(!function_exists('imagejpeg')){ 333 $okay = false; 334 }else{ 335 $okay = imagejpeg($newimg, $to, $conf['jpg_quality']); 336 } 337 }elseif($ext == 'png') { 338 if(!function_exists('imagepng')){ 339 $okay = false; 340 }else{ 341 $okay = imagepng($newimg, $to); 342 } 343 }elseif($ext == 'gif') { 344 if(!function_exists('imagegif')){ 345 $okay = false; 346 }else{ 347 $okay = imagegif($newimg, $to); 348 } 349 } 350 351 // destroy GD image ressources 352 if($image) imagedestroy($image); 353 if($newimg) imagedestroy($newimg); 354 355 return $okay; 356} 357 358/** 359 * Checks if the given amount of memory is available 360 * 361 * If the memory_get_usage() function is not available the 362 * function just assumes $used bytes of already allocated memory 363 * 364 * @param int $mem Size of memory you want to allocate in bytes 365 * @param int $used already allocated memory (see above) 366 * @author Filip Oscadal <webmaster@illusionsoftworks.cz> 367 * @author Andreas Gohr <andi@splitbrain.org> 368 */ 369function is_mem_available($mem,$bytes=1048576){ 370 $limit = trim(ini_get('memory_limit')); 371 if(empty($limit)) return true; // no limit set! 372 373 // parse limit to bytes 374 $unit = strtolower(substr($limit,-1)); 375 switch($unit){ 376 case 'g': 377 $limit = substr($limit,0,-1); 378 $limit *= 1024*1024*1024; 379 break; 380 case 'm': 381 $limit = substr($limit,0,-1); 382 $limit *= 1024*1024; 383 break; 384 case 'k': 385 $limit = substr($limit,0,-1); 386 $limit *= 1024; 387 break; 388 } 389 390 // get used memory if possible 391 if(function_exists('memory_get_usage')){ 392 $used = memory_get_usage(); 393 } 394 395 396 if($used+$mem > $limit){ 397 return false; 398 } 399 400 return true; 401} 402 403//Setup VIM: ex: et ts=2 enc=utf-8 : 404?> 405