1*7fb7960fSChristopher Smith<?php 2*7fb7960fSChristopher Smith/** 3*7fb7960fSChristopher Smith * Functions used by lib/exe/fetch.php 4*7fb7960fSChristopher Smith * (not included by other parts of dokuwiki) 5*7fb7960fSChristopher Smith */ 6*7fb7960fSChristopher Smith 7*7fb7960fSChristopher Smith/** 8*7fb7960fSChristopher Smith * Set headers and send the file to the client 9*7fb7960fSChristopher Smith * 10*7fb7960fSChristopher Smith * The $cache parameter influences how long files may be kept in caches, the $public parameter 11*7fb7960fSChristopher Smith * influences if this caching may happen in public proxis or in the browser cache only FS#2734 12*7fb7960fSChristopher Smith * 13*7fb7960fSChristopher Smith * This function will abort the current script when a 304 is sent or file sending is handled 14*7fb7960fSChristopher Smith * through x-sendfile 15*7fb7960fSChristopher Smith * 16*7fb7960fSChristopher Smith * @author Andreas Gohr <andi@splitbrain.org> 17*7fb7960fSChristopher Smith * @author Ben Coburn <btcoburn@silicodon.net> 18*7fb7960fSChristopher Smith * @param string $file local file to send 19*7fb7960fSChristopher Smith * @param string $mime mime type of the file 20*7fb7960fSChristopher Smith * @param bool $dl set to true to force a browser download 21*7fb7960fSChristopher Smith * @param int $cache remaining cache time in seconds (-1 for $conf['cache'], 0 for no-cache) 22*7fb7960fSChristopher Smith * @param bool $public is this a public ressource or a private one? 23*7fb7960fSChristopher Smith */ 24*7fb7960fSChristopher Smithfunction sendFile($file, $mime, $dl, $cache, $public = false) { 25*7fb7960fSChristopher Smith global $conf; 26*7fb7960fSChristopher Smith // send mime headers 27*7fb7960fSChristopher Smith header("Content-Type: $mime"); 28*7fb7960fSChristopher Smith 29*7fb7960fSChristopher Smith // calculate cache times 30*7fb7960fSChristopher Smith if($cache == -1) { 31*7fb7960fSChristopher Smith $maxage = max($conf['cachetime'], 3600); // cachetime or one hour 32*7fb7960fSChristopher Smith $expires = time() + $maxage; 33*7fb7960fSChristopher Smith } else if($cache > 0) { 34*7fb7960fSChristopher Smith $maxage = $cache; // given time 35*7fb7960fSChristopher Smith $expires = time() + $maxage; 36*7fb7960fSChristopher Smith } else { // $cache == 0 37*7fb7960fSChristopher Smith $maxage = 0; 38*7fb7960fSChristopher Smith $expires = 0; // 1970-01-01 39*7fb7960fSChristopher Smith } 40*7fb7960fSChristopher Smith 41*7fb7960fSChristopher Smith // smart http caching headers 42*7fb7960fSChristopher Smith if($maxage) { 43*7fb7960fSChristopher Smith if($public) { 44*7fb7960fSChristopher Smith // cache publically 45*7fb7960fSChristopher Smith header('Expires: '.gmdate("D, d M Y H:i:s", $expires).' GMT'); 46*7fb7960fSChristopher Smith header('Cache-Control: public, proxy-revalidate, no-transform, max-age='.$maxage); 47*7fb7960fSChristopher Smith header('Pragma: public'); 48*7fb7960fSChristopher Smith } else { 49*7fb7960fSChristopher Smith // cache in browser 50*7fb7960fSChristopher Smith header('Expires: '.gmdate("D, d M Y H:i:s", $expires).' GMT'); 51*7fb7960fSChristopher Smith header('Cache-Control: private, no-transform, max-age='.$maxage); 52*7fb7960fSChristopher Smith header('Pragma: no-cache'); 53*7fb7960fSChristopher Smith } 54*7fb7960fSChristopher Smith } else { 55*7fb7960fSChristopher Smith // no cache at all 56*7fb7960fSChristopher Smith header('Expires: Thu, 01 Jan 1970 00:00:00 GMT'); 57*7fb7960fSChristopher Smith header('Cache-Control: no-cache, no-transform'); 58*7fb7960fSChristopher Smith header('Pragma: no-cache'); 59*7fb7960fSChristopher Smith } 60*7fb7960fSChristopher Smith 61*7fb7960fSChristopher Smith //send important headers first, script stops here if '304 Not Modified' response 62*7fb7960fSChristopher Smith $fmtime = @filemtime($file); 63*7fb7960fSChristopher Smith http_conditionalRequest($fmtime); 64*7fb7960fSChristopher Smith 65*7fb7960fSChristopher Smith //download or display? 66*7fb7960fSChristopher Smith if($dl) { 67*7fb7960fSChristopher Smith header('Content-Disposition: attachment; filename="'.utf8_basename($file).'";'); 68*7fb7960fSChristopher Smith } else { 69*7fb7960fSChristopher Smith header('Content-Disposition: inline; filename="'.utf8_basename($file).'";'); 70*7fb7960fSChristopher Smith } 71*7fb7960fSChristopher Smith 72*7fb7960fSChristopher Smith //use x-sendfile header to pass the delivery to compatible webservers 73*7fb7960fSChristopher Smith if(http_sendfile($file)) exit; 74*7fb7960fSChristopher Smith 75*7fb7960fSChristopher Smith // send file contents 76*7fb7960fSChristopher Smith $fp = @fopen($file, "rb"); 77*7fb7960fSChristopher Smith if($fp) { 78*7fb7960fSChristopher Smith http_rangeRequest($fp, filesize($file), $mime); 79*7fb7960fSChristopher Smith } else { 80*7fb7960fSChristopher Smith http_status(500); 81*7fb7960fSChristopher Smith print "Could not read $file - bad permissions?"; 82*7fb7960fSChristopher Smith } 83*7fb7960fSChristopher Smith} 84*7fb7960fSChristopher Smith 85*7fb7960fSChristopher Smith/** 86*7fb7960fSChristopher Smith * Check for media for preconditions and return correct status code 87*7fb7960fSChristopher Smith * 88*7fb7960fSChristopher Smith * READ: MEDIA, MIME, EXT, CACHE 89*7fb7960fSChristopher Smith * WRITE: MEDIA, FILE, array( STATUS, STATUSMESSAGE ) 90*7fb7960fSChristopher Smith * 91*7fb7960fSChristopher Smith * @author Gerry Weissbach <gerry.w@gammaproduction.de> 92*7fb7960fSChristopher Smith * @param $media reference to the media id 93*7fb7960fSChristopher Smith * @param $file reference to the file variable 94*7fb7960fSChristopher Smith * @returns array(STATUS, STATUSMESSAGE) 95*7fb7960fSChristopher Smith */ 96*7fb7960fSChristopher Smithfunction checkFileStatus(&$media, &$file, $rev = '', $width=0, $height=0) { 97*7fb7960fSChristopher Smith global $MIME, $EXT, $CACHE, $INPUT; 98*7fb7960fSChristopher Smith 99*7fb7960fSChristopher Smith //media to local file 100*7fb7960fSChristopher Smith if(preg_match('#^(https?)://#i', $media)) { 101*7fb7960fSChristopher Smith //check hash 102*7fb7960fSChristopher Smith if(substr(md5(auth_cookiesalt().$media), 0, 6) !== $INPUT->str('hash')) { 103*7fb7960fSChristopher Smith return array(412, 'Precondition Failed'); 104*7fb7960fSChristopher Smith } 105*7fb7960fSChristopher Smith //handle external images 106*7fb7960fSChristopher Smith if(strncmp($MIME, 'image/', 6) == 0) $file = media_get_from_URL($media, $EXT, $CACHE); 107*7fb7960fSChristopher Smith if(!$file) { 108*7fb7960fSChristopher Smith //download failed - redirect to original URL 109*7fb7960fSChristopher Smith return array(302, $media); 110*7fb7960fSChristopher Smith } 111*7fb7960fSChristopher Smith } else { 112*7fb7960fSChristopher Smith $media = cleanID($media); 113*7fb7960fSChristopher Smith if(empty($media)) { 114*7fb7960fSChristopher Smith return array(400, 'Bad request'); 115*7fb7960fSChristopher Smith } 116*7fb7960fSChristopher Smith // check token for resized images 117*7fb7960fSChristopher Smith if (($width || $height) && media_get_token($media, $width, $height) !== $INPUT->str('tok')) { 118*7fb7960fSChristopher Smith return array(412, 'Precondition Failed'); 119*7fb7960fSChristopher Smith } 120*7fb7960fSChristopher Smith 121*7fb7960fSChristopher Smith //check permissions (namespace only) 122*7fb7960fSChristopher Smith if(auth_quickaclcheck(getNS($media).':X') < AUTH_READ) { 123*7fb7960fSChristopher Smith return array(403, 'Forbidden'); 124*7fb7960fSChristopher Smith } 125*7fb7960fSChristopher Smith $file = mediaFN($media, $rev); 126*7fb7960fSChristopher Smith } 127*7fb7960fSChristopher Smith 128*7fb7960fSChristopher Smith //check file existance 129*7fb7960fSChristopher Smith if(!@file_exists($file)) { 130*7fb7960fSChristopher Smith return array(404, 'Not Found'); 131*7fb7960fSChristopher Smith } 132*7fb7960fSChristopher Smith 133*7fb7960fSChristopher Smith return array(200, null); 134*7fb7960fSChristopher Smith} 135*7fb7960fSChristopher Smith 136*7fb7960fSChristopher Smith/** 137*7fb7960fSChristopher Smith * Returns the wanted cachetime in seconds 138*7fb7960fSChristopher Smith * 139*7fb7960fSChristopher Smith * Resolves named constants 140*7fb7960fSChristopher Smith * 141*7fb7960fSChristopher Smith * @author Andreas Gohr <andi@splitbrain.org> 142*7fb7960fSChristopher Smith */ 143*7fb7960fSChristopher Smithfunction calc_cache($cache) { 144*7fb7960fSChristopher Smith global $conf; 145*7fb7960fSChristopher Smith 146*7fb7960fSChristopher Smith if(strtolower($cache) == 'nocache') return 0; //never cache 147*7fb7960fSChristopher Smith if(strtolower($cache) == 'recache') return $conf['cachetime']; //use standard cache 148*7fb7960fSChristopher Smith return -1; //cache endless 149*7fb7960fSChristopher Smith} 150