Merge pull request #3115 from moisesbr-dw/sort-with-collator
[dokuwiki.git] / inc / fetch.functions.php
blob63672629d2cd5f51fddc39c423ee30dd7c4fd08a
1 <?php
2 /**
3 * Functions used by lib/exe/fetch.php
4 * (not included by other parts of dokuwiki)
5 */
7 /**
8 * Set headers and send the file to the client
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
13 * This function will abort the current script when a 304 is sent or file sending is handled
14 * through x-sendfile
16 * @author Andreas Gohr <andi@splitbrain.org>
17 * @author Ben Coburn <btcoburn@silicodon.net>
18 * @author Gerry Weissbach <dokuwiki@gammaproduction.de>
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
27 function sendFile($file, $mime, $dl, $cache, $public = false, $orig = null) {
28 global $conf;
29 // send mime headers
30 header("Content-Type: $mime");
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
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);
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');
61 //send important headers first, script stops here if '304 Not Modified' response
62 $fmtime = @filemtime($file);
63 http_conditionalRequest($fmtime);
65 // Use the current $file if is $orig is not set.
66 if ( $orig == null ) {
67 $orig = $file;
70 //download or display?
71 if ($dl) {
72 header('Content-Disposition: attachment;' . rfc2231_encode(
73 'filename', \dokuwiki\Utf8\PhpString::basename($orig)) . ';'
75 } else {
76 header('Content-Disposition: inline;' . rfc2231_encode(
77 'filename', \dokuwiki\Utf8\PhpString::basename($orig)) . ';'
81 //use x-sendfile header to pass the delivery to compatible webservers
82 http_sendfile($file);
84 // send file contents
85 $fp = @fopen($file, "rb");
86 if($fp) {
87 http_rangeRequest($fp, filesize($file), $mime);
88 } else {
89 http_status(500);
90 print "Could not read $file - bad permissions?";
94 /**
95 * Try an rfc2231 compatible encoding. This ensures correct
96 * interpretation of filenames outside of the ASCII set.
97 * This seems to be needed for file names with e.g. umlauts that
98 * would otherwise decode wrongly in IE.
100 * There is no additional checking, just the encoding and setting the key=value for usage in headers
102 * @author Gerry Weissbach <gerry.w@gammaproduction.de>
103 * @param string $name name of the field to be set in the header() call
104 * @param string $value value of the field to be set in the header() call
105 * @param string $charset used charset for the encoding of value
106 * @param string $lang language used.
107 * @return string in the format " name=value" for values WITHOUT special characters
108 * @return string in the format " name*=charset'lang'value" for values WITH special characters
110 function rfc2231_encode($name, $value, $charset='utf-8', $lang='en') {
111 $internal = preg_replace_callback(
112 '/[\x00-\x20*\'%()<>@,;:\\\\"\/[\]?=\x80-\xFF]/',
113 function ($match) {
114 return rawurlencode($match[0]);
116 $value
118 if ( $value != $internal ) {
119 return ' '.$name.'*='.$charset."'".$lang."'".$internal;
120 } else {
121 return ' '.$name.'="'.$value.'"';
126 * Check for media for preconditions and return correct status code
128 * READ: MEDIA, MIME, EXT, CACHE
129 * WRITE: MEDIA, FILE, array( STATUS, STATUSMESSAGE )
131 * @author Gerry Weissbach <gerry.w@gammaproduction.de>
133 * @param string $media reference to the media id
134 * @param string $file reference to the file variable
135 * @param string $rev
136 * @param int $width
137 * @param int $height
138 * @return array as array(STATUS, STATUSMESSAGE)
140 function checkFileStatus(&$media, &$file, $rev = '', $width=0, $height=0) {
141 global $MIME, $EXT, $CACHE, $INPUT;
143 //media to local file
144 if(media_isexternal($media)) {
145 //check token for external image and additional for resized and cached images
146 if(media_get_token($media, $width, $height) !== $INPUT->str('tok')) {
147 return array(412, 'Precondition Failed');
149 //handle external images
150 if(strncmp($MIME, 'image/', 6) == 0) $file = media_get_from_URL($media, $EXT, $CACHE);
151 if(!$file) {
152 //download failed - redirect to original URL
153 return array(302, $media);
155 } else {
156 $media = cleanID($media);
157 if(empty($media)) {
158 return array(400, 'Bad request');
160 // check token for resized images
161 if (($width || $height) && media_get_token($media, $width, $height) !== $INPUT->str('tok')) {
162 return array(412, 'Precondition Failed');
165 //check permissions (namespace only)
166 if(auth_quickaclcheck(getNS($media).':X') < AUTH_READ) {
167 return array(403, 'Forbidden');
169 $file = mediaFN($media, $rev);
172 //check file existance
173 if(!file_exists($file)) {
174 return array(404, 'Not Found');
177 return array(200, null);
181 * Returns the wanted cachetime in seconds
183 * Resolves named constants
185 * @author Andreas Gohr <andi@splitbrain.org>
187 * @param string $cache
188 * @return int cachetime in seconds
190 function calc_cache($cache) {
191 global $conf;
193 if(strtolower($cache) == 'nocache') return 0; //never cache
194 if(strtolower($cache) == 'recache') return $conf['cachetime']; //use standard cache
195 return -1; //cache endless