1f62ea8a1Sandi<?php 2f62ea8a1Sandi/** 3f62ea8a1Sandi * DokuWiki media passthrough file 4f62ea8a1Sandi * 5f62ea8a1Sandi * @license GPL 2 (http://www.gnu.org/licenses/gpl.html) 6f62ea8a1Sandi * @author Andreas Gohr <andi@splitbrain.org> 7f62ea8a1Sandi */ 8f62ea8a1Sandi 9b1a72e3dSmatthiasgrimm if(!defined('DOKU_INC')) define('DOKU_INC',realpath(dirname(__FILE__).'/../../').'/'); 10f62ea8a1Sandi require_once(DOKU_INC.'inc/init.php'); 11f62ea8a1Sandi require_once(DOKU_INC.'inc/common.php'); 12f62ea8a1Sandi require_once(DOKU_INC.'inc/pageutils.php'); 13f62ea8a1Sandi require_once(DOKU_INC.'inc/confutils.php'); 14f62ea8a1Sandi require_once(DOKU_INC.'inc/auth.php'); 158746e727Sandi //close sesseion 168746e727Sandi session_write_close(); 17e935fb4aSAndreas Gohr if(!defined('CHUNK_SIZE')) define('CHUNK_SIZE',16*1024); 188746e727Sandi 19f62ea8a1Sandi $mimetypes = getMimeTypes(); 20f62ea8a1Sandi 21f62ea8a1Sandi //get input 2242905504SAndreas Gohr $MEDIA = getID('media',false); // no cleaning - maybe external 23f62ea8a1Sandi $CACHE = calc_cache($_REQUEST['cache']); 24f62ea8a1Sandi $WIDTH = $_REQUEST['w']; 25f62ea8a1Sandi $HEIGHT = $_REQUEST['h']; 26f62ea8a1Sandi list($EXT,$MIME) = mimetype($MEDIA); 27f62ea8a1Sandi if($EXT === false){ 28f62ea8a1Sandi $EXT = 'unknown'; 29f62ea8a1Sandi $MIME = 'application/octet-stream'; 30f62ea8a1Sandi } 31f62ea8a1Sandi 32f62ea8a1Sandi //media to local file 33f62ea8a1Sandi if(preg_match('#^(https?|ftp)://#i',$MEDIA)){ 34f62ea8a1Sandi //handle external media 35f62ea8a1Sandi $FILE = get_from_URL($MEDIA,$EXT,$CACHE); 36f62ea8a1Sandi if(!$FILE){ 37f62ea8a1Sandi //download failed - redirect to original URL 38f62ea8a1Sandi header('Location: '.$MEDIA); 39f62ea8a1Sandi exit; 40f62ea8a1Sandi } 41f62ea8a1Sandi }else{ 42f62ea8a1Sandi $MEDIA = cleanID($MEDIA); 43f62ea8a1Sandi if(empty($MEDIA)){ 44f62ea8a1Sandi header("HTTP/1.0 400 Bad Request"); 45f62ea8a1Sandi print 'Bad request'; 46f62ea8a1Sandi exit; 47f62ea8a1Sandi } 48f62ea8a1Sandi 49f62ea8a1Sandi //check permissions (namespace only) 50f62ea8a1Sandi if(auth_quickaclcheck(getNS($MEDIA).':X') < AUTH_READ){ 51f62ea8a1Sandi header("HTTP/1.0 401 Unauthorized"); 52f62ea8a1Sandi //fixme add some image for imagefiles 53f62ea8a1Sandi print 'Unauthorized'; 54f62ea8a1Sandi exit; 55f62ea8a1Sandi } 56f62ea8a1Sandi $FILE = mediaFN($MEDIA); 57f62ea8a1Sandi } 58f62ea8a1Sandi 59f62ea8a1Sandi //check file existance 60f62ea8a1Sandi if(!@file_exists($FILE)){ 61f62ea8a1Sandi header("HTTP/1.0 404 Not Found"); 62f62ea8a1Sandi //FIXME add some default broken image 63f62ea8a1Sandi print 'Not Found'; 64f62ea8a1Sandi exit; 65f62ea8a1Sandi } 66f62ea8a1Sandi 67f62ea8a1Sandi //handle image resizing 68f62ea8a1Sandi if((substr($MIME,0,5) == 'image') && $WIDTH){ 69f62ea8a1Sandi $FILE = get_resized($FILE,$EXT,$WIDTH,$HEIGHT); 70f62ea8a1Sandi } 71f62ea8a1Sandi 72e935fb4aSAndreas Gohr // finally send the file to the client 73e935fb4aSAndreas Gohr sendFile($FILE,$MIME); 74f62ea8a1Sandi 75e935fb4aSAndreas Gohr/* ------------------------------------------------------------------------ */ 76f62ea8a1Sandi 77e935fb4aSAndreas Gohr/** 78e935fb4aSAndreas Gohr * Set headers and send the file to the client 79e935fb4aSAndreas Gohr * 80e935fb4aSAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 81e935fb4aSAndreas Gohr */ 82e935fb4aSAndreas Gohrfunction sendFile($file,$mime){ 83e935fb4aSAndreas Gohr // send headers 84e935fb4aSAndreas Gohr header("Content-Type: $mime"); 85e935fb4aSAndreas Gohr header('Cache-Control: must-revalidate, post-check=0, pre-check=0'); 86e935fb4aSAndreas Gohr header('Pragma: public'); 87e935fb4aSAndreas Gohr header('Accept-Ranges: bytes'); 88ff4f5ee7SBen Coburn //send important headers first, script stops here if '304 Not Modified' response 89ff4f5ee7SBen Coburn http_conditionalRequest(filemtime($file)); 90ff4f5ee7SBen Coburn list($start,$len) = http_rangeRequest(filesize($file)); 91f62ea8a1Sandi 92f62ea8a1Sandi //application mime type is downloadable 93e935fb4aSAndreas Gohr if(substr($mime,0,11) == 'application'){ 94e935fb4aSAndreas Gohr header('Content-Disposition: attachment; filename="'.basename($file).'";'); 95f62ea8a1Sandi } 96f62ea8a1Sandi 97e935fb4aSAndreas Gohr // send file contents 98e935fb4aSAndreas Gohr $fp = @fopen($file,"rb"); 99f62ea8a1Sandi if($fp){ 100e935fb4aSAndreas Gohr fseek($fp,$start); //seek to start of range 101e935fb4aSAndreas Gohr 102e935fb4aSAndreas Gohr $chunk = ($len > CHUNK_SIZE) ? CHUNK_SIZE : $len; 103e935fb4aSAndreas Gohr while (!feof($fp) && $chunk > 0) { 104615a21edSBrian Cowan @set_time_limit(); // large files can take a lot of time 105e935fb4aSAndreas Gohr print fread($fp, $chunk); 106615a21edSBrian Cowan flush(); 107e935fb4aSAndreas Gohr $len -= $chunk; 108e935fb4aSAndreas Gohr $chunk = ($len > CHUNK_SIZE) ? CHUNK_SIZE : $len; 109615a21edSBrian Cowan } 110615a21edSBrian Cowan fclose($fp); 111f62ea8a1Sandi }else{ 112f62ea8a1Sandi header("HTTP/1.0 500 Internal Server Error"); 113e935fb4aSAndreas Gohr print "Could not read $file - bad permissions?"; 114e935fb4aSAndreas Gohr } 115f62ea8a1Sandi} 116f62ea8a1Sandi 117e935fb4aSAndreas Gohr/** 118e935fb4aSAndreas Gohr * Checks and sets headers to handle range requets 119e935fb4aSAndreas Gohr * 120e935fb4aSAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 121e935fb4aSAndreas Gohr * @returns array The start byte and the amount of bytes to send 122e935fb4aSAndreas Gohr */ 123e935fb4aSAndreas Gohrfunction http_rangeRequest($size){ 124e935fb4aSAndreas Gohr if(!isset($_SERVER['HTTP_RANGE'])){ 125e935fb4aSAndreas Gohr // no range requested - send the whole file 126e935fb4aSAndreas Gohr header("Content-Length: $size"); 127e935fb4aSAndreas Gohr return array(0,$size); 128e935fb4aSAndreas Gohr } 129e935fb4aSAndreas Gohr 130e935fb4aSAndreas Gohr $t = explode('=', $_SERVER['HTTP_RANGE']); 131e935fb4aSAndreas Gohr if (!$t[0]=='bytes') { 132e935fb4aSAndreas Gohr // we only understand byte ranges - send the whole file 133e935fb4aSAndreas Gohr header("Content-Length: $size"); 134e935fb4aSAndreas Gohr return array(0,$size); 135e935fb4aSAndreas Gohr } 136e935fb4aSAndreas Gohr 137e935fb4aSAndreas Gohr $r = explode('-', $t[1]); 138e935fb4aSAndreas Gohr $start = (int)$r[0]; 139e935fb4aSAndreas Gohr $end = (int)$r[1]; 140e935fb4aSAndreas Gohr if (!$end) $end = $size - 1; 141e935fb4aSAndreas Gohr if ($start > $end || $start > $size || $end > $size){ 142e935fb4aSAndreas Gohr header('HTTP/1.1 416 Requested Range Not Satisfiable'); 143e935fb4aSAndreas Gohr print 'Bad Range Request!'; 144e935fb4aSAndreas Gohr exit; 145e935fb4aSAndreas Gohr } 146e935fb4aSAndreas Gohr 147e935fb4aSAndreas Gohr $tot = $end - $start + 1; 148e935fb4aSAndreas Gohr header('HTTP/1.1 206 Partial Content'); 149e935fb4aSAndreas Gohr header("Content-Range: bytes {$start}-{$end}/{$size}"); 150e935fb4aSAndreas Gohr header("Content-Length: $tot"); 151e935fb4aSAndreas Gohr 152e935fb4aSAndreas Gohr return array($start,$tot); 153e935fb4aSAndreas Gohr} 154e935fb4aSAndreas Gohr 155e935fb4aSAndreas Gohr/** 156f62ea8a1Sandi * Resizes the given image to the given size 157f62ea8a1Sandi * 158f62ea8a1Sandi * @author Andreas Gohr <andi@splitbrain.org> 159f62ea8a1Sandi */ 160f62ea8a1Sandifunction get_resized($file, $ext, $w, $h=0){ 161f62ea8a1Sandi global $conf; 162f62ea8a1Sandi 163f62ea8a1Sandi $info = getimagesize($file); 164f62ea8a1Sandi if(!$h) $h = round(($w * $info[1]) / $info[0]); 165f62ea8a1Sandi 166f62ea8a1Sandi 167f62ea8a1Sandi //cache 16898407a7aSandi $local = getCacheName($file,'.media.'.$w.'x'.$h.'.'.$ext); 169f62ea8a1Sandi $mtime = @filemtime($local); // 0 if not exists 170f62ea8a1Sandi 17168375754SPavel Vitis if( $mtime > filemtime($file) || 17268375754SPavel Vitis resize_imageIM($ext,$file,$info[0],$info[1],$local,$w,$h) || 17368375754SPavel Vitis resize_imageGD($ext,$file,$info[0],$info[1],$local,$w,$h) ){ 174f62ea8a1Sandi return $local; 175f62ea8a1Sandi } 176f62ea8a1Sandi //still here? resizing failed 177f62ea8a1Sandi return $file; 178f62ea8a1Sandi} 179f62ea8a1Sandi 180f62ea8a1Sandi/** 181f62ea8a1Sandi * Returns the wanted cachetime in seconds 182f62ea8a1Sandi * 183f62ea8a1Sandi * Resolves named constants 184f62ea8a1Sandi * 185f62ea8a1Sandi * @author Andreas Gohr <andi@splitbrain.org> 186f62ea8a1Sandi */ 187f62ea8a1Sandifunction calc_cache($cache){ 188f62ea8a1Sandi global $conf; 189f62ea8a1Sandi 190f62ea8a1Sandi if(strtolower($cache) == 'nocache') return 0; //never cache 191f62ea8a1Sandi if(strtolower($cache) == 'recache') return $conf['cachetime']; //use standard cache 192f62ea8a1Sandi return -1; //cache endless 193f62ea8a1Sandi} 194f62ea8a1Sandi 195f62ea8a1Sandi/** 196f62ea8a1Sandi * Download a remote file and return local filename 197f62ea8a1Sandi * 198f62ea8a1Sandi * returns false if download fails. Uses cached file if available and 199f62ea8a1Sandi * wanted 200f62ea8a1Sandi * 201f62ea8a1Sandi * @author Andreas Gohr <andi@splitbrain.org> 20268375754SPavel Vitis * @author Pavel Vitis <Pavel.Vitis@seznam.cz> 203f62ea8a1Sandi */ 204f62ea8a1Sandifunction get_from_URL($url,$ext,$cache){ 205f62ea8a1Sandi global $conf; 206f62ea8a1Sandi 207*4f3c4962SBen Coburn // if 'nocache' just redirect 208*4f3c4962SBen Coburn if ($cache==0) { return false; } 209*4f3c4962SBen Coburn 21068375754SPavel Vitis $local = getCacheName(strtolower($url),".media.$ext"); 211f62ea8a1Sandi $mtime = @filemtime($local); // 0 if not exists 212f62ea8a1Sandi 213f62ea8a1Sandi //decide if download needed: 214*4f3c4962SBen Coburn if( ($mtime == 0) || // cache does not exist 215*4f3c4962SBen Coburn ($cache != -1 && $mtime < time()-$cache) // 'recache' and cache has expired 21668375754SPavel Vitis ){ 217f62ea8a1Sandi if(io_download($url,$local)){ 218f62ea8a1Sandi return $local; 219f62ea8a1Sandi }else{ 220f62ea8a1Sandi return false; 221f62ea8a1Sandi } 222f62ea8a1Sandi } 223f62ea8a1Sandi 224f62ea8a1Sandi //if cache exists use it else 225f62ea8a1Sandi if($mtime) return $local; 226f62ea8a1Sandi 227f62ea8a1Sandi //else return false 228f62ea8a1Sandi return false; 229f62ea8a1Sandi} 230f62ea8a1Sandi 231f62ea8a1Sandi/** 23268375754SPavel Vitis * resize images using external ImageMagick convert program 23368375754SPavel Vitis * 23468375754SPavel Vitis * @author Pavel Vitis <Pavel.Vitis@seznam.cz> 23568375754SPavel Vitis * @author Andreas Gohr <andi@splitbrain.org> 23668375754SPavel Vitis */ 23768375754SPavel Vitisfunction resize_imageIM($ext,$from,$from_w,$from_h,$to,$to_w,$to_h){ 23868375754SPavel Vitis global $conf; 23968375754SPavel Vitis 2407bc7a78eSAndreas Gohr // check if convert is configured 2417bc7a78eSAndreas Gohr if(!$conf['im_convert']) return false; 24268375754SPavel Vitis 24368375754SPavel Vitis // prepare command 24468375754SPavel Vitis $cmd = $conf['im_convert']; 24568375754SPavel Vitis $cmd .= ' -resize '.$to_w.'x'.$to_h.'!'; 24668375754SPavel Vitis $cmd .= " $from $to"; 24768375754SPavel Vitis 24868375754SPavel Vitis @exec($cmd,$out,$retval); 24968375754SPavel Vitis if ($retval == 0) return true; 25068375754SPavel Vitis 25168375754SPavel Vitis return false; 25268375754SPavel Vitis} 25368375754SPavel Vitis 25468375754SPavel Vitis/** 25568375754SPavel Vitis * resize images using PHP's libGD support 256f62ea8a1Sandi * 257f62ea8a1Sandi * @author Andreas Gohr <andi@splitbrain.org> 258f62ea8a1Sandi */ 25968375754SPavel Vitisfunction resize_imageGD($ext,$from,$from_w,$from_h,$to,$to_w,$to_h){ 260f62ea8a1Sandi global $conf; 261f62ea8a1Sandi 262f62ea8a1Sandi if($conf['gdlib'] < 1) return false; //no GDlib available or wanted 263f62ea8a1Sandi 2644e406776SAndreas Gohr // check available memory 2654e406776SAndreas Gohr if(!is_mem_available(($from_w * $from_h * 4) + ($to_w * $to_h * 4))){ 2664e406776SAndreas Gohr return false; 2674e406776SAndreas Gohr } 2684e406776SAndreas Gohr 269f62ea8a1Sandi // create an image of the given filetype 270f62ea8a1Sandi if ($ext == 'jpg' || $ext == 'jpeg'){ 271f62ea8a1Sandi if(!function_exists("imagecreatefromjpeg")) return false; 272f62ea8a1Sandi $image = @imagecreatefromjpeg($from); 273f62ea8a1Sandi }elseif($ext == 'png') { 274f62ea8a1Sandi if(!function_exists("imagecreatefrompng")) return false; 275f62ea8a1Sandi $image = @imagecreatefrompng($from); 276f62ea8a1Sandi 277f62ea8a1Sandi }elseif($ext == 'gif') { 278f62ea8a1Sandi if(!function_exists("imagecreatefromgif")) return false; 279f62ea8a1Sandi $image = @imagecreatefromgif($from); 280f62ea8a1Sandi } 281f62ea8a1Sandi if(!$image) return false; 282f62ea8a1Sandi 283f62ea8a1Sandi if(($conf['gdlib']>1) && function_exists("imagecreatetruecolor")){ 284f62ea8a1Sandi $newimg = @imagecreatetruecolor ($to_w, $to_h); 285f62ea8a1Sandi } 286f62ea8a1Sandi if(!$newimg) $newimg = @imagecreate($to_w, $to_h); 287dd7bbbf4SAndreas Gohr if(!$newimg){ 288dd7bbbf4SAndreas Gohr imagedestroy($image); 289dd7bbbf4SAndreas Gohr return false; 290dd7bbbf4SAndreas Gohr } 291f62ea8a1Sandi 292f62ea8a1Sandi //keep png alpha channel if possible 293f62ea8a1Sandi if($ext == 'png' && $conf['gdlib']>1 && function_exists('imagesavealpha')){ 294f62ea8a1Sandi imagealphablending($newimg, false); 295f62ea8a1Sandi imagesavealpha($newimg,true); 296f62ea8a1Sandi } 297f62ea8a1Sandi 298f62ea8a1Sandi //try resampling first 299f62ea8a1Sandi if(function_exists("imagecopyresampled")){ 300f62ea8a1Sandi if(!@imagecopyresampled($newimg, $image, 0, 0, 0, 0, $to_w, $to_h, $from_w, $from_h)) { 301f62ea8a1Sandi imagecopyresized($newimg, $image, 0, 0, 0, 0, $to_w, $to_h, $from_w, $from_h); 302f62ea8a1Sandi } 303f62ea8a1Sandi }else{ 304f62ea8a1Sandi imagecopyresized($newimg, $image, 0, 0, 0, 0, $to_w, $to_h, $from_w, $from_h); 305f62ea8a1Sandi } 306f62ea8a1Sandi 307dd7bbbf4SAndreas Gohr $okay = false; 308f62ea8a1Sandi if ($ext == 'jpg' || $ext == 'jpeg'){ 309dd7bbbf4SAndreas Gohr if(!function_exists('imagejpeg')){ 310dd7bbbf4SAndreas Gohr $okay = false; 311dd7bbbf4SAndreas Gohr }else{ 312dd7bbbf4SAndreas Gohr $okay = imagejpeg($newimg, $to, 70); 313dd7bbbf4SAndreas Gohr } 314f62ea8a1Sandi }elseif($ext == 'png') { 315dd7bbbf4SAndreas Gohr if(!function_exists('imagepng')){ 316dd7bbbf4SAndreas Gohr $okay = false; 317dd7bbbf4SAndreas Gohr }else{ 318dd7bbbf4SAndreas Gohr $okay = imagepng($newimg, $to); 319dd7bbbf4SAndreas Gohr } 320f62ea8a1Sandi }elseif($ext == 'gif') { 321dd7bbbf4SAndreas Gohr if(!function_exists('imagegif')){ 322dd7bbbf4SAndreas Gohr $okay = false; 323dd7bbbf4SAndreas Gohr }else{ 324dd7bbbf4SAndreas Gohr $okay = imagegif($newimg, $to); 325dd7bbbf4SAndreas Gohr } 326f62ea8a1Sandi } 327f62ea8a1Sandi 328dd7bbbf4SAndreas Gohr // destroy GD image ressources 329dd7bbbf4SAndreas Gohr if($image) imagedestroy($image); 330dd7bbbf4SAndreas Gohr if($newimg) imagedestroy($newimg); 331dd7bbbf4SAndreas Gohr 332dd7bbbf4SAndreas Gohr return $okay; 333f62ea8a1Sandi} 334f62ea8a1Sandi 3354e406776SAndreas Gohr/** 3364e406776SAndreas Gohr * Checks if the given amount of memory is available 3374e406776SAndreas Gohr * 3384e406776SAndreas Gohr * If the memory_get_usage() function is not available the 3394e406776SAndreas Gohr * function just assumes $used bytes of already allocated memory 3404e406776SAndreas Gohr * 3414e406776SAndreas Gohr * @param int $mem Size of memory you want to allocate in bytes 3424e406776SAndreas Gohr * @param int $used already allocated memory (see above) 3434e406776SAndreas Gohr * @author Filip Oscadal <webmaster@illusionsoftworks.cz> 3444e406776SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 3454e406776SAndreas Gohr */ 3464e406776SAndreas Gohrfunction is_mem_available($mem,$bytes=1048576){ 3474e406776SAndreas Gohr $limit = trim(ini_get('memory_limit')); 3484e406776SAndreas Gohr if(empty($limit)) return true; // no limit set! 3494e406776SAndreas Gohr 3504e406776SAndreas Gohr // parse limit to bytes 3514e406776SAndreas Gohr $unit = strtolower(substr($limit,-1)); 3524e406776SAndreas Gohr switch($unit){ 3534e406776SAndreas Gohr case 'g': 3544e406776SAndreas Gohr $limit = substr($limit,0,-1); 3554e406776SAndreas Gohr $limit *= 1024*1024*1024; 3564e406776SAndreas Gohr break; 3574e406776SAndreas Gohr case 'm': 3584e406776SAndreas Gohr $limit = substr($limit,0,-1); 3594e406776SAndreas Gohr $limit *= 1024*1024; 3604e406776SAndreas Gohr break; 3614e406776SAndreas Gohr case 'k': 3624e406776SAndreas Gohr $limit = substr($limit,0,-1); 3634e406776SAndreas Gohr $limit *= 1024; 3644e406776SAndreas Gohr break; 3654e406776SAndreas Gohr } 3664e406776SAndreas Gohr 3674e406776SAndreas Gohr // get used memory if possible 3684e406776SAndreas Gohr if(function_exists('memory_get_usage')){ 3694e406776SAndreas Gohr $used = memory_get_usage(); 3704e406776SAndreas Gohr } 3714e406776SAndreas Gohr 3724e406776SAndreas Gohr 3734e406776SAndreas Gohr if($used+$mem > $limit){ 3744e406776SAndreas Gohr return false; 3754e406776SAndreas Gohr } 3764e406776SAndreas Gohr 3774e406776SAndreas Gohr return true; 3784e406776SAndreas Gohr} 379f62ea8a1Sandi 380f62ea8a1Sandi//Setup VIM: ex: et ts=2 enc=utf-8 : 381f62ea8a1Sandi?> 382