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 9if(!defined('DOKU_INC')) define('DOKU_INC', dirname(__FILE__).'/../../'); 10define('DOKU_DISABLE_GZIP_OUTPUT', 1); 11require_once(DOKU_INC.'inc/init.php'); 12session_write_close(); //close session 13 14// BEGIN main (if not testing) 15if(!defined('SIMPLE_TEST')) { 16 $mimetypes = getMimeTypes(); 17 18 //get input 19 $MEDIA = stripctl(getID('media', false)); // no cleaning except control chars - maybe external 20 $CACHE = calc_cache($INPUT->str('cache')); 21 $WIDTH = $INPUT->int('w'); 22 $HEIGHT = $INPUT->int('h'); 23 $REV = & $INPUT->ref('rev'); 24 //sanitize revision 25 $REV = preg_replace('/[^0-9]/', '', $REV); 26 27 list($EXT, $MIME, $DL) = mimetype($MEDIA, false); 28 if($EXT === false) { 29 $EXT = 'unknown'; 30 $MIME = 'application/octet-stream'; 31 $DL = true; 32 } 33 34 // check for permissions, preconditions and cache external files 35 list($STATUS, $STATUSMESSAGE) = checkFileStatus($MEDIA, $FILE, $REV); 36 37 // prepare data for plugin events 38 $data = array( 39 'media' => $MEDIA, 40 'file' => $FILE, 41 'orig' => $FILE, 42 'mime' => $MIME, 43 'download' => $DL, 44 'cache' => $CACHE, 45 'ext' => $EXT, 46 'width' => $WIDTH, 47 'height' => $HEIGHT, 48 'status' => $STATUS, 49 'statusmessage' => $STATUSMESSAGE, 50 'ispublic' => media_ispublic($MEDIA), 51 ); 52 53 // handle the file status 54 $evt = new Doku_Event('FETCH_MEDIA_STATUS', $data); 55 if($evt->advise_before()) { 56 // redirects 57 if($data['status'] > 300 && $data['status'] <= 304) { 58 send_redirect($data['statusmessage']); 59 } 60 // send any non 200 status 61 if($data['status'] != 200) { 62 http_status($data['status'], $data['statusmessage']); 63 } 64 // die on errors 65 if($data['status'] > 203) { 66 print $data['statusmessage']; 67 exit; 68 } 69 } 70 $evt->advise_after(); 71 unset($evt); 72 73 //handle image resizing/cropping 74 if((substr($MIME, 0, 5) == 'image') && $WIDTH) { 75 if($HEIGHT) { 76 $data['file'] = $FILE = media_crop_image($data['file'], $EXT, $WIDTH, $HEIGHT); 77 } else { 78 $data['file'] = $FILE = media_resize_image($data['file'], $EXT, $WIDTH, $HEIGHT); 79 } 80 } 81 82 // finally send the file to the client 83 $evt = new Doku_Event('MEDIA_SENDFILE', $data); 84 if($evt->advise_before()) { 85 sendFile($data['file'], $data['mime'], $data['download'], $data['cache'], $data['ispublic']); 86 } 87 // Do something after the download finished. 88 $evt->advise_after(); // will not be emitted on 304 or x-sendfile 89 90}// END DO main 91 92/* ------------------------------------------------------------------------ */ 93 94/** 95 * Set headers and send the file to the client 96 * 97 * The $cache parameter influences how long files may be kept in caches, the $public parameter 98 * influences if this caching may happen in public proxis or in the browser cache only FS#2734 99 * 100 * This function will abort the current script when a 304 is sent or file sending is handled 101 * through x-sendfile 102 * 103 * @author Andreas Gohr <andi@splitbrain.org> 104 * @author Ben Coburn <btcoburn@silicodon.net> 105 * @param string $file local file to send 106 * @param string $mime mime type of the file 107 * @param bool $dl set to true to force a browser download 108 * @param int $cache remaining cache time in seconds (-1 for $conf['cache'], 0 for no-cache) 109 * @param bool $public is this a public ressource or a private one? 110 */ 111function sendFile($file, $mime, $dl, $cache, $public = false) { 112 global $conf; 113 // send mime headers 114 header("Content-Type: $mime"); 115 116 // calculate cache times 117 if($cache == -1) { 118 $maxage = max($conf['cachetime'], 3600); // cachetime or one hour 119 $expires = time() + $maxage; 120 } else if($cache > 0) { 121 $maxage = $cache; // given time 122 $expires = time() + $maxage; 123 } else { // $cache == 0 124 $maxage = 0; 125 $expires = 0; // 1970-01-01 126 } 127 128 // smart http caching headers 129 if($maxage) { 130 if($public) { 131 // cache publically 132 header('Expires: '.gmdate("D, d M Y H:i:s", $expires).' GMT'); 133 header('Cache-Control: public, proxy-revalidate, no-transform, max-age='.$maxage); 134 header('Pragma: public'); 135 } else { 136 // cache in browser 137 header('Expires: '.gmdate("D, d M Y H:i:s", $expires).' GMT'); 138 header('Cache-Control: private, no-transform, max-age='.$maxage); 139 header('Pragma: no-cache'); 140 } 141 } else { 142 // no cache at all 143 header('Expires: Thu, 01 Jan 1970 00:00:00 GMT'); 144 header('Cache-Control: no-cache, no-transform'); 145 header('Pragma: no-cache'); 146 } 147 148 //send important headers first, script stops here if '304 Not Modified' response 149 $fmtime = @filemtime($file); 150 http_conditionalRequest($fmtime); 151 152 //download or display? 153 if($dl) { 154 header('Content-Disposition: attachment; filename="'.utf8_basename($file).'";'); 155 } else { 156 header('Content-Disposition: inline; filename="'.utf8_basename($file).'";'); 157 } 158 159 //use x-sendfile header to pass the delivery to compatible webservers 160 if(http_sendfile($file)) exit; 161 162 // send file contents 163 $fp = @fopen($file, "rb"); 164 if($fp) { 165 http_rangeRequest($fp, filesize($file), $mime); 166 } else { 167 http_status(500); 168 print "Could not read $file - bad permissions?"; 169 } 170} 171 172/** 173 * Check for media for preconditions and return correct status code 174 * 175 * READ: MEDIA, MIME, EXT, CACHE 176 * WRITE: MEDIA, FILE, array( STATUS, STATUSMESSAGE ) 177 * 178 * @author Gerry Weissbach <gerry.w@gammaproduction.de> 179 * @param $media reference to the media id 180 * @param $file reference to the file variable 181 * @returns array(STATUS, STATUSMESSAGE) 182 */ 183function checkFileStatus(&$media, &$file, $rev = '') { 184 global $MIME, $EXT, $CACHE, $INPUT; 185 186 //media to local file 187 if(preg_match('#^(https?)://#i', $media)) { 188 //check hash 189 if(substr(md5(auth_cookiesalt().$media), 0, 6) !== $INPUT->str('hash')) { 190 return array(412, 'Precondition Failed'); 191 } 192 //handle external images 193 if(strncmp($MIME, 'image/', 6) == 0) $file = media_get_from_URL($media, $EXT, $CACHE); 194 if(!$file) { 195 //download failed - redirect to original URL 196 return array(302, $media); 197 } 198 } else { 199 $media = cleanID($media); 200 if(empty($media)) { 201 return array(400, 'Bad request'); 202 } 203 204 //check permissions (namespace only) 205 if(auth_quickaclcheck(getNS($media).':X') < AUTH_READ) { 206 return array(403, 'Forbidden'); 207 } 208 $file = mediaFN($media, $rev); 209 } 210 211 //check file existance 212 if(!@file_exists($file)) { 213 return array(404, 'Not Found'); 214 } 215 216 return array(200, null); 217} 218 219/** 220 * Returns the wanted cachetime in seconds 221 * 222 * Resolves named constants 223 * 224 * @author Andreas Gohr <andi@splitbrain.org> 225 */ 226function calc_cache($cache) { 227 global $conf; 228 229 if(strtolower($cache) == 'nocache') return 0; //never cache 230 if(strtolower($cache) == 'recache') return $conf['cachetime']; //use standard cache 231 return -1; //cache endless 232} 233 234//Setup VIM: ex: et ts=2 : 235