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 9d0a27cb0SAndreas Gohrif(!defined('DOKU_INC')) define('DOKU_INC', dirname(__FILE__).'/../../'); 103138b5c7SAndreas Gohrdefine('DOKU_DISABLE_GZIP_OUTPUT', 1); 11f62ea8a1Sandirequire_once(DOKU_INC.'inc/init.php'); 12*36625b96SAndreas Gohrsession_write_close(); //close session 138746e727Sandi 14*36625b96SAndreas Gohr// BEGIN main (if not testing) 15*36625b96SAndreas Gohrif(!defined('SIMPLE_TEST')) { 16f62ea8a1Sandi $mimetypes = getMimeTypes(); 17f62ea8a1Sandi 18f62ea8a1Sandi //get input 1902b0b681SAndreas Gohr $MEDIA = stripctl(getID('media', false)); // no cleaning except control chars - maybe external 20bfd0f597STom N Harris $CACHE = calc_cache($INPUT->str('cache')); 21bfd0f597STom N Harris $WIDTH = $INPUT->int('w'); 22bfd0f597STom N Harris $HEIGHT = $INPUT->int('h'); 23bfd0f597STom N Harris $REV = & $INPUT->ref('rev'); 24fc4aefb9SKate Arzamastseva //sanitize revision 25fc4aefb9SKate Arzamastseva $REV = preg_replace('/[^0-9]/', '', $REV); 26fc4aefb9SKate Arzamastseva 2727bf7924STom N Harris list($EXT, $MIME, $DL) = mimetype($MEDIA, false); 28f62ea8a1Sandi if($EXT === false) { 29f62ea8a1Sandi $EXT = 'unknown'; 30f62ea8a1Sandi $MIME = 'application/octet-stream'; 31ecebf3a8SAndreas Gohr $DL = true; 32f62ea8a1Sandi } 33f62ea8a1Sandi 3403293305SAndreas Gohr // check for permissions, preconditions and cache external files 35fc4aefb9SKate Arzamastseva list($STATUS, $STATUSMESSAGE) = checkFileStatus($MEDIA, $FILE, $REV); 3603293305SAndreas Gohr 3703293305SAndreas Gohr // prepare data for plugin events 383b399a1bSAndreas Gohr $data = array( 393b399a1bSAndreas Gohr 'media' => $MEDIA, 40cd98d9c3SGerry Weißbach 'file' => $FILE, 41cd98d9c3SGerry Weißbach 'orig' => $FILE, 42cd98d9c3SGerry Weißbach 'mime' => $MIME, 43cd98d9c3SGerry Weißbach 'download' => $DL, 44cd98d9c3SGerry Weißbach 'cache' => $CACHE, 45cd98d9c3SGerry Weißbach 'ext' => $EXT, 46cd98d9c3SGerry Weißbach 'width' => $WIDTH, 47cd98d9c3SGerry Weißbach 'height' => $HEIGHT, 48cd98d9c3SGerry Weißbach 'status' => $STATUS, 49cd98d9c3SGerry Weißbach 'statusmessage' => $STATUSMESSAGE, 50cd98d9c3SGerry Weißbach ); 51f62ea8a1Sandi 5203293305SAndreas Gohr // handle the file status 5303293305SAndreas Gohr $evt = new Doku_Event('FETCH_MEDIA_STATUS', $data); 54cd98d9c3SGerry Weißbach if($evt->advise_before()) { 5503293305SAndreas Gohr // redirects 5603293305SAndreas Gohr if($data['status'] > 300 && $data['status'] <= 304) { 5703293305SAndreas Gohr send_redirect($data['statusmessage']); 5803293305SAndreas Gohr } 5903293305SAndreas Gohr // send any non 200 status 6003293305SAndreas Gohr if($data['status'] != 200) { 61cd98d9c3SGerry Weißbach header('HTTP/1.0 '.$data['status'].' '.$data['statusmessage']); 6203293305SAndreas Gohr } 6303293305SAndreas Gohr // die on errors 6403293305SAndreas Gohr if($data['status'] > 203) { 65cd98d9c3SGerry Weißbach print $data['statusmessage']; 66f62ea8a1Sandi exit; 67f62ea8a1Sandi } 68f62ea8a1Sandi } 6903293305SAndreas Gohr $evt->advise_after(); 7003293305SAndreas Gohr unset($evt); 71f62ea8a1Sandi 7220bc86cfSAndreas Gohr //handle image resizing/cropping 73f62ea8a1Sandi if((substr($MIME, 0, 5) == 'image') && $WIDTH) { 74d52a56e1SAndreas Gohr if($HEIGHT) { 75cd98d9c3SGerry Weißbach $data['file'] = $FILE = media_crop_image($data['file'], $EXT, $WIDTH, $HEIGHT); 7620bc86cfSAndreas Gohr } else { 77cd98d9c3SGerry Weißbach $data['file'] = $FILE = media_resize_image($data['file'], $EXT, $WIDTH, $HEIGHT); 78f62ea8a1Sandi } 7920bc86cfSAndreas Gohr } 80f62ea8a1Sandi 81e935fb4aSAndreas Gohr // finally send the file to the client 82b80bedd6SAndreas Gohr $evt = new Doku_Event('MEDIA_SENDFILE', $data); 83b80bedd6SAndreas Gohr if($evt->advise_before()) { 84ecebf3a8SAndreas Gohr sendFile($data['file'], $data['mime'], $data['download'], $data['cache']); 85b80bedd6SAndreas Gohr } 86cd98d9c3SGerry Weißbach // Do something after the download finished. 87cd98d9c3SGerry Weißbach $evt->advise_after(); 88f62ea8a1Sandi 89*36625b96SAndreas Gohr}// END DO main 90*36625b96SAndreas Gohr 91e935fb4aSAndreas Gohr/* ------------------------------------------------------------------------ */ 92f62ea8a1Sandi 93e935fb4aSAndreas Gohr/** 94e935fb4aSAndreas Gohr * Set headers and send the file to the client 95e935fb4aSAndreas Gohr * 96e935fb4aSAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 9783730152SBen Coburn * @author Ben Coburn <btcoburn@silicodon.net> 98e935fb4aSAndreas Gohr */ 99ecebf3a8SAndreas Gohrfunction sendFile($file, $mime, $dl, $cache) { 10083730152SBen Coburn global $conf; 10109a5b61fSgweissbach $fmtime = @filemtime($file); 102e935fb4aSAndreas Gohr // send headers 103e935fb4aSAndreas Gohr header("Content-Type: $mime"); 10483730152SBen Coburn // smart http caching headers 10583730152SBen Coburn if($cache == -1) { 10683730152SBen Coburn // cache 10783730152SBen Coburn // cachetime or one hour 10883730152SBen Coburn header('Expires: '.gmdate("D, d M Y H:i:s", time() + max($conf['cachetime'], 3600)).' GMT'); 10983730152SBen Coburn header('Cache-Control: public, proxy-revalidate, no-transform, max-age='.max($conf['cachetime'], 3600)); 110e935fb4aSAndreas Gohr header('Pragma: public'); 11183730152SBen Coburn } else if($cache > 0) { 11283730152SBen Coburn // recache 11383730152SBen Coburn // remaining cachetime + 10 seconds so the newly recached media is used 11483730152SBen Coburn header('Expires: '.gmdate("D, d M Y H:i:s", $fmtime + $conf['cachetime'] + 10).' GMT'); 11583730152SBen Coburn header('Cache-Control: public, proxy-revalidate, no-transform, max-age='.max($fmtime - time() + $conf['cachetime'] + 10, 0)); 11683730152SBen Coburn header('Pragma: public'); 11783730152SBen Coburn } else if($cache == 0) { 11883730152SBen Coburn // nocache 11983730152SBen Coburn header('Cache-Control: must-revalidate, no-transform, post-check=0, pre-check=0'); 12083730152SBen Coburn header('Pragma: public'); 12183730152SBen Coburn } 122ff4f5ee7SBen Coburn //send important headers first, script stops here if '304 Not Modified' response 12383730152SBen Coburn http_conditionalRequest($fmtime); 1249a87c72aSAndreas Gohr 125ecebf3a8SAndreas Gohr //download or display? 126ecebf3a8SAndreas Gohr if($dl) { 1273009a773SAndreas Gohr header('Content-Disposition: attachment; filename="'.utf8_basename($file).'";'); 128ecebf3a8SAndreas Gohr } else { 1293009a773SAndreas Gohr header('Content-Disposition: inline; filename="'.utf8_basename($file).'";'); 130f62ea8a1Sandi } 131f62ea8a1Sandi 1329a87c72aSAndreas Gohr //use x-sendfile header to pass the delivery to compatible webservers 1336106ad89SChris Smith if(http_sendfile($file)) exit; 1349a87c72aSAndreas Gohr 135e935fb4aSAndreas Gohr // send file contents 136e935fb4aSAndreas Gohr $fp = @fopen($file, "rb"); 137f62ea8a1Sandi if($fp) { 138758447cfSAndreas Gohr http_rangeRequest($fp, filesize($file), $mime); 139f62ea8a1Sandi } else { 140f62ea8a1Sandi header("HTTP/1.0 500 Internal Server Error"); 141e935fb4aSAndreas Gohr print "Could not read $file - bad permissions?"; 142e935fb4aSAndreas Gohr } 143f62ea8a1Sandi} 144f62ea8a1Sandi 14503293305SAndreas Gohr/** 14603293305SAndreas Gohr * Check for media for preconditions and return correct status code 147cd98d9c3SGerry Weißbach * 148cd98d9c3SGerry Weißbach * READ: MEDIA, MIME, EXT, CACHE 149cd98d9c3SGerry Weißbach * WRITE: MEDIA, FILE, array( STATUS, STATUSMESSAGE ) 150cd98d9c3SGerry Weißbach * 151cd98d9c3SGerry Weißbach * @author Gerry Weissbach <gerry.w@gammaproduction.de> 152cd98d9c3SGerry Weißbach * @param $media reference to the media id 153cd98d9c3SGerry Weißbach * @param $file reference to the file variable 154cd98d9c3SGerry Weißbach * @returns array(STATUS, STATUSMESSAGE) 155cd98d9c3SGerry Weißbach */ 156fc4aefb9SKate Arzamastsevafunction checkFileStatus(&$media, &$file, $rev = '') { 1575373d847SHakan Sandell global $MIME, $EXT, $CACHE, $INPUT; 158cd98d9c3SGerry Weißbach 159cd98d9c3SGerry Weißbach //media to local file 160cd98d9c3SGerry Weißbach if(preg_match('#^(https?)://#i', $media)) { 161cd98d9c3SGerry Weißbach //check hash 16258789954SAndreas Gohr if(substr(md5(auth_cookiesalt().$media), 0, 6) !== $INPUT->str('hash')) { 163cd98d9c3SGerry Weißbach return array(412, 'Precondition Failed'); 164cd98d9c3SGerry Weißbach } 165cd98d9c3SGerry Weißbach //handle external images 166cd98d9c3SGerry Weißbach if(strncmp($MIME, 'image/', 6) == 0) $file = media_get_from_URL($media, $EXT, $CACHE); 167cd98d9c3SGerry Weißbach if(!$file) { 168cd98d9c3SGerry Weißbach //download failed - redirect to original URL 16903293305SAndreas Gohr return array(302, $media); 170cd98d9c3SGerry Weißbach } 171cd98d9c3SGerry Weißbach } else { 172cd98d9c3SGerry Weißbach $media = cleanID($media); 173cd98d9c3SGerry Weißbach if(empty($media)) { 174cd98d9c3SGerry Weißbach return array(400, 'Bad request'); 175cd98d9c3SGerry Weißbach } 176cd98d9c3SGerry Weißbach 177cd98d9c3SGerry Weißbach //check permissions (namespace only) 178cd98d9c3SGerry Weißbach if(auth_quickaclcheck(getNS($media).':X') < AUTH_READ) { 179c6f610efSAndreas Gohr return array(403, 'Forbidden'); 180cd98d9c3SGerry Weißbach } 181fc4aefb9SKate Arzamastseva $file = mediaFN($media, $rev); 182cd98d9c3SGerry Weißbach } 183cd98d9c3SGerry Weißbach 184cd98d9c3SGerry Weißbach //check file existance 185cd98d9c3SGerry Weißbach if(!@file_exists($file)) { 186cd98d9c3SGerry Weißbach return array(404, 'Not Found'); 187cd98d9c3SGerry Weißbach } 188cd98d9c3SGerry Weißbach 18903293305SAndreas Gohr return array(200, null); 190cd98d9c3SGerry Weißbach} 191cd98d9c3SGerry Weißbach 192e935fb4aSAndreas Gohr/** 193f62ea8a1Sandi * Returns the wanted cachetime in seconds 194f62ea8a1Sandi * 195f62ea8a1Sandi * Resolves named constants 196f62ea8a1Sandi * 197f62ea8a1Sandi * @author Andreas Gohr <andi@splitbrain.org> 198f62ea8a1Sandi */ 199f62ea8a1Sandifunction calc_cache($cache) { 200f62ea8a1Sandi global $conf; 201f62ea8a1Sandi 202f62ea8a1Sandi if(strtolower($cache) == 'nocache') return 0; //never cache 203f62ea8a1Sandi if(strtolower($cache) == 'recache') return $conf['cachetime']; //use standard cache 204f62ea8a1Sandi return -1; //cache endless 205f62ea8a1Sandi} 206f62ea8a1Sandi 207e3776c06SMichael Hamann//Setup VIM: ex: et ts=2 : 208