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 $mimetypes = getMimeTypes(); 16 17 //get input 18 $MEDIA = $_REQUEST['media']; 19 $CACHE = calc_cache($_REQUEST['cache']); 20 $WIDTH = $_REQUEST['w']; 21 $HEIGHT = $_REQUEST['h']; 22 list($EXT,$MIME) = mimetype($MEDIA); 23 if($EXT === false){ 24 $EXT = 'unknown'; 25 $MIME = 'application/octet-stream'; 26 } 27 28 //media to local file 29 if(preg_match('#^(https?|ftp)://#i',$MEDIA)){ 30 //handle external media 31 $FILE = get_from_URL($MEDIA,$EXT,$CACHE); 32 if(!$FILE){ 33 //download failed - redirect to original URL 34 header('Location: '.$MEDIA); 35 exit; 36 } 37 }else{ 38 $MEDIA = cleanID($MEDIA); 39 if(empty($MEDIA)){ 40 header("HTTP/1.0 400 Bad Request"); 41 print 'Bad request'; 42 exit; 43 } 44 45 //check permissions (namespace only) 46 if(auth_quickaclcheck(getNS($MEDIA).':X') < AUTH_READ){ 47 header("HTTP/1.0 401 Unauthorized"); 48 //fixme add some image for imagefiles 49 print 'Unauthorized'; 50 exit; 51 } 52 $FILE = mediaFN($MEDIA); 53 } 54 55 //check file existance 56 if(!@file_exists($FILE)){ 57 header("HTTP/1.0 404 Not Found"); 58 //FIXME add some default broken image 59 print 'Not Found'; 60 exit; 61 } 62 63 //handle image resizing 64 if((substr($MIME,0,5) == 'image') && $WIDTH){ 65 $FILE = get_resized($FILE,$EXT,$WIDTH,$HEIGHT); 66 } 67 68 69 //FIXME set sane cachecontrol headers 70 //FIXME handle conditional and partial requests 71 72 //send file 73 header("Content-Type: $MIME"); 74 header('Last-Modified: '.date('r',filemtime($FILE))); 75 header('Content-Length: '.filesize($FILE)); 76 77 //application mime type is downloadable 78 if(substr($MIME,0,11) == 'application'){ 79 header('Content-Disposition: attachment; filename="'.basename($FILE).'"'); 80 } 81 82 $fp = @fopen($FILE,"rb"); 83 if($fp){ 84 fpassthru($fp); //does a close itself 85 }else{ 86 header("HTTP/1.0 500 Internal Server Error"); 87 print "Could not read $FILE - bad permissions?"; 88 } 89 90/* ------------------------------------------------------------------------ */ 91 92/** 93 * Resizes the given image to the given size 94 * 95 * @author Andreas Gohr <andi@splitbrain.org> 96 */ 97function get_resized($file, $ext, $w, $h=0){ 98 global $conf; 99 100 $md5 = md5($file); 101 $info = getimagesize($file); 102 if(!$h) $h = round(($w * $info[1]) / $info[0]); 103 104 105 //cache 106 $local = $conf['mediadir'].'/_cache/'.$md5.'.'.$w.'x'.$h.'.'.$ext; 107 $mtime = @filemtime($local); // 0 if not exists 108 109 if( $mtime > filemtime($file) || resize_image($ext,$file,$info[0],$info[1],$local,$w,$h) ){ 110 return $local; 111 } 112 //still here? resizing failed 113 return $file; 114} 115 116/** 117 * Returns the wanted cachetime in seconds 118 * 119 * Resolves named constants 120 * 121 * @author Andreas Gohr <andi@splitbrain.org> 122 */ 123function calc_cache($cache){ 124 global $conf; 125 126 if(strtolower($cache) == 'nocache') return 0; //never cache 127 if(strtolower($cache) == 'recache') return $conf['cachetime']; //use standard cache 128 return -1; //cache endless 129} 130 131/** 132 * Download a remote file and return local filename 133 * 134 * returns false if download fails. Uses cached file if available and 135 * wanted 136 * 137 * @author Andreas Gohr <andi@splitbrain.org> 138 */ 139function get_from_URL($url,$ext,$cache){ 140 global $conf; 141 142 $url = strtolower($url); 143 $md5 = md5($url); 144 145 $local = $conf['mediadir']."/_cache/$md5.$ext"; 146 $mtime = @filemtime($local); // 0 if not exists 147 148 //decide if download needed: 149 150 // never cache exists but no endless cache not exists or expired 151 if( $cache == 0 || ($mtime != 0 && $cache != -1) || $mtime < time()-$cache ){ 152 if(io_download($url,$local)){ 153 return $local; 154 }else{ 155 return false; 156 } 157 } 158 159 //if cache exists use it else 160 if($mtime) return $local; 161 162 //else return false 163 return false; 164} 165 166/** 167 * resize images 168 * 169 * @author Andreas Gohr <andi@splitbrain.org> 170 */ 171function resize_image($ext,$from,$from_w,$from_h,$to,$to_w,$to_h){ 172 global $conf; 173 174 if($conf['gdlib'] < 1) return false; //no GDlib available or wanted 175 176 // create an image of the given filetype 177 if ($ext == 'jpg' || $ext == 'jpeg'){ 178 if(!function_exists("imagecreatefromjpeg")) return false; 179 $image = @imagecreatefromjpeg($from); 180 }elseif($ext == 'png') { 181 if(!function_exists("imagecreatefrompng")) return false; 182 $image = @imagecreatefrompng($from); 183 184 }elseif($ext == 'gif') { 185 if(!function_exists("imagecreatefromgif")) return false; 186 $image = @imagecreatefromgif($from); 187 } 188 if(!$image) return false; 189 190 if(($conf['gdlib']>1) && function_exists("imagecreatetruecolor")){ 191 $newimg = @imagecreatetruecolor ($to_w, $to_h); 192 } 193 if(!$newimg) $newimg = @imagecreate($to_w, $to_h); 194 if(!$newimg) return false; 195 196 //keep png alpha channel if possible 197 if($ext == 'png' && $conf['gdlib']>1 && function_exists('imagesavealpha')){ 198 imagealphablending($newimg, false); 199 imagesavealpha($newimg,true); 200 } 201 202 // create cachedir 203 io_makeFileDir($to); 204 205 //try resampling first 206 if(function_exists("imagecopyresampled")){ 207 if(!@imagecopyresampled($newimg, $image, 0, 0, 0, 0, $to_w, $to_h, $from_w, $from_h)) { 208 imagecopyresized($newimg, $image, 0, 0, 0, 0, $to_w, $to_h, $from_w, $from_h); 209 } 210 }else{ 211 imagecopyresized($newimg, $image, 0, 0, 0, 0, $to_w, $to_h, $from_w, $from_h); 212 } 213 214 if ($ext == 'jpg' || $ext == 'jpeg'){ 215 if(!function_exists("imagejpeg")) return false; 216 return imagejpeg($newimg, $to, 70); 217 }elseif($ext == 'png') { 218 if(!function_exists("imagepng")) return false; 219 return imagepng($newimg, $to); 220 }elseif($ext == 'gif') { 221 if(!function_exists("imagegif")) return false; 222 return imagegif($newimg, $to); 223 } 224 225 return false; 226} 227 228 229//Setup VIM: ex: et ts=2 enc=utf-8 : 230?> 231