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