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