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 * @author Gerry Weissbach <dokuwiki@gammaproduction.de> 19 * @param string $file local file to send 20 * @param string $mime mime type of the file 21 * @param bool $dl set to true to force a browser download 22 * @param int $cache remaining cache time in seconds (-1 for $conf['cache'], 0 for no-cache) 23 * @param bool $public is this a public ressource or a private one? 24 * @param string $orig original file to send - the file name will be used for the Content-Disposition 25 */ 26function sendFile($file, $mime, $dl, $cache, $public = false, $orig = null) { 27 global $conf; 28 // send mime headers 29 header("Content-Type: $mime"); 30 31 // calculate cache times 32 if($cache == -1) { 33 $maxage = max($conf['cachetime'], 3600); // cachetime or one hour 34 $expires = time() + $maxage; 35 } else if($cache > 0) { 36 $maxage = $cache; // given time 37 $expires = time() + $maxage; 38 } else { // $cache == 0 39 $maxage = 0; 40 $expires = 0; // 1970-01-01 41 } 42 43 // smart http caching headers 44 if($maxage) { 45 if($public) { 46 // cache publically 47 header('Expires: '.gmdate("D, d M Y H:i:s", $expires).' GMT'); 48 header('Cache-Control: public, proxy-revalidate, no-transform, max-age='.$maxage); 49 header('Pragma: public'); 50 } else { 51 // cache in browser 52 header('Expires: '.gmdate("D, d M Y H:i:s", $expires).' GMT'); 53 header('Cache-Control: private, no-transform, max-age='.$maxage); 54 header('Pragma: no-cache'); 55 } 56 } else { 57 // no cache at all 58 header('Expires: Thu, 01 Jan 1970 00:00:00 GMT'); 59 header('Cache-Control: no-cache, no-transform'); 60 header('Pragma: no-cache'); 61 } 62 63 //send important headers first, script stops here if '304 Not Modified' response 64 $fmtime = @filemtime($file); 65 http_conditionalRequest($fmtime); 66 67 // Use the current $file if is $orig is not set. 68 if ( $orig == null ) { 69 $orig = $file; 70 } 71 72 //download or display? 73 if($dl) { 74 header('Content-Disposition: attachment;'.rfc2231_encode('filename', utf8_basename($orig)).';'); 75 } else { 76 header('Content-Disposition: inline;'.rfc2231_encode('filename', utf8_basename($orig)).';'); 77 } 78 79 //use x-sendfile header to pass the delivery to compatible webservers 80 http_sendfile($file); 81 82 // send file contents 83 $fp = @fopen($file, "rb"); 84 if($fp) { 85 http_rangeRequest($fp, filesize($file), $mime); 86 } else { 87 http_status(500); 88 print "Could not read $file - bad permissions?"; 89 } 90} 91 92/** 93 * Try an rfc2231 compatible encoding. This ensures correct 94 * interpretation of filenames outside of the ASCII set. 95 * This seems to be needed for file names with e.g. umlauts that 96 * would otherwise decode wrongly in IE. 97 * 98 * There is no additional checking, just the encoding and setting the key=value for usage in headers 99 * 100 * @author Gerry Weissbach <gerry.w@gammaproduction.de> 101 * @param string $name name of the field to be set in the header() call 102 * @param string $value value of the field to be set in the header() call 103 * @param string $charset used charset for the encoding of value 104 * @param string $lang language used. 105 * @return string in the format " name=value" for values WITHOUT special characters 106 * @return string in the format " name*=charset'lang'value" for values WITH special characters 107 */ 108function rfc2231_encode($name, $value, $charset='utf-8', $lang='en') { 109 $internal = preg_replace_callback('/[\x00-\x20*\'%()<>@,;:\\\\"\/[\]?=\x80-\xFF]/', function($match) { return rawurlencode($match[0]); }, $value); 110 if ( $value != $internal ) { 111 return ' '.$name.'*='.$charset."'".$lang."'".$internal; 112 } else { 113 return ' '.$name.'="'.$value.'"'; 114 } 115} 116 117/** 118 * Check for media for preconditions and return correct status code 119 * 120 * READ: MEDIA, MIME, EXT, CACHE 121 * WRITE: MEDIA, FILE, array( STATUS, STATUSMESSAGE ) 122 * 123 * @author Gerry Weissbach <gerry.w@gammaproduction.de> 124 * @param string $media reference to the media id 125 * @param string $file reference to the file variable 126 * @param string $rev 127 * @param int $width 128 * @param int $height 129 * @return array(STATUS, STATUSMESSAGE) 130 */ 131function checkFileStatus(&$media, &$file, $rev = '', $width=0, $height=0) { 132 global $MIME, $EXT, $CACHE, $INPUT; 133 134 //media to local file 135 if(media_isexternal($media)) { 136 //check token for external image and additional for resized and cached images 137 if(media_get_token($media, $width, $height) !== $INPUT->str('tok')) { 138 return array(412, 'Precondition Failed'); 139 } 140 //handle external images 141 if(strncmp($MIME, 'image/', 6) == 0) $file = media_get_from_URL($media, $EXT, $CACHE); 142 if(!$file) { 143 //download failed - redirect to original URL 144 return array(302, $media); 145 } 146 } else { 147 $media = cleanID($media); 148 if(empty($media)) { 149 return array(400, 'Bad request'); 150 } 151 // check token for resized images 152 if (($width || $height) && media_get_token($media, $width, $height) !== $INPUT->str('tok')) { 153 return array(412, 'Precondition Failed'); 154 } 155 156 //check permissions (namespace only) 157 if(auth_quickaclcheck(getNS($media).':X') < AUTH_READ) { 158 return array(403, 'Forbidden'); 159 } 160 $file = mediaFN($media, $rev); 161 } 162 163 //check file existance 164 if(!@file_exists($file)) { 165 return array(404, 'Not Found'); 166 } 167 168 return array(200, null); 169} 170 171/** 172 * Returns the wanted cachetime in seconds 173 * 174 * Resolves named constants 175 * 176 * @author Andreas Gohr <andi@splitbrain.org> 177 */ 178function calc_cache($cache) { 179 global $conf; 180 181 if(strtolower($cache) == 'nocache') return 0; //never cache 182 if(strtolower($cache) == 'recache') return $conf['cachetime']; //use standard cache 183 return -1; //cache endless 184} 185