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