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( 108 '/[\x00-\x20*\'%()<>@,;:\\\\"\/[\]?=\x80-\xFF]/', 109 function ($match) { 110 return rawurlencode($match[0]); 111 }, 112 $value 113 ); 114 if ( $value != $internal ) { 115 return ' '.$name.'*='.$charset."'".$lang."'".$internal; 116 } else { 117 return ' '.$name.'="'.$value.'"'; 118 } 119} 120 121/** 122 * Check for media for preconditions and return correct status code 123 * 124 * READ: MEDIA, MIME, EXT, CACHE 125 * WRITE: MEDIA, FILE, array( STATUS, STATUSMESSAGE ) 126 * 127 * @author Gerry Weissbach <gerry.w@gammaproduction.de> 128 * 129 * @param string $media reference to the media id 130 * @param string $file reference to the file variable 131 * @param string $rev 132 * @param int $width 133 * @param int $height 134 * @return array as array(STATUS, STATUSMESSAGE) 135 */ 136function checkFileStatus(&$media, &$file, $rev = '', $width=0, $height=0) { 137 global $MIME, $EXT, $CACHE, $INPUT; 138 139 //media to local file 140 if(media_isexternal($media)) { 141 //check token for external image and additional for resized and cached images 142 if(media_get_token($media, $width, $height) !== $INPUT->str('tok')) { 143 return array(412, 'Precondition Failed'); 144 } 145 //handle external images 146 if(strncmp($MIME, 'image/', 6) == 0) $file = media_get_from_URL($media, $EXT, $CACHE); 147 if(!$file) { 148 //download failed - redirect to original URL 149 return array(302, $media); 150 } 151 } else { 152 $media = cleanID($media); 153 if(empty($media)) { 154 return array(400, 'Bad request'); 155 } 156 // check token for resized images 157 if (($width || $height) && media_get_token($media, $width, $height) !== $INPUT->str('tok')) { 158 return array(412, 'Precondition Failed'); 159 } 160 161 //check permissions (namespace only) 162 if(auth_quickaclcheck(getNS($media).':X') < AUTH_READ) { 163 return array(403, 'Forbidden'); 164 } 165 $file = mediaFN($media, $rev); 166 } 167 168 //check file existance 169 if(!file_exists($file)) { 170 return array(404, 'Not Found'); 171 } 172 173 return array(200, null); 174} 175 176/** 177 * Returns the wanted cachetime in seconds 178 * 179 * Resolves named constants 180 * 181 * @author Andreas Gohr <andi@splitbrain.org> 182 * 183 * @param string $cache 184 * @return int cachetime in seconds 185 */ 186function calc_cache($cache) { 187 global $conf; 188 189 if(strtolower($cache) == 'nocache') return 0; //never cache 190 if(strtolower($cache) == 'recache') return $conf['cachetime']; //use standard cache 191 return -1; //cache endless 192} 193