xref: /dokuwiki/inc/fetch.functions.php (revision 2fd1f790dae5f89a60537f9b80b9c5a10312f595)
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; filename="'.utf8_basename($orig).'";');
76    } else {
77        header('Content-Disposition: inline; 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 * Check for media for preconditions and return correct status code
95 *
96 * READ: MEDIA, MIME, EXT, CACHE
97 * WRITE: MEDIA, FILE, array( STATUS, STATUSMESSAGE )
98 *
99 * @author Gerry Weissbach <gerry.w@gammaproduction.de>
100 *
101 * @param string $media  reference to the media id
102 * @param string $file   reference to the file variable
103 * @param string $rev
104 * @param int    $width
105 * @param int    $height
106 * @return array as array(STATUS, STATUSMESSAGE)
107 */
108function checkFileStatus(&$media, &$file, $rev = '', $width=0, $height=0) {
109    global $MIME, $EXT, $CACHE, $INPUT;
110
111    //media to local file
112    if(media_isexternal($media)) {
113        //check token for external image and additional for resized and cached images
114        if(media_get_token($media, $width, $height) !== $INPUT->str('tok')) {
115            return array(412, 'Precondition Failed');
116        }
117        //handle external images
118        if(strncmp($MIME, 'image/', 6) == 0) $file = media_get_from_URL($media, $EXT, $CACHE);
119        if(!$file) {
120            //download failed - redirect to original URL
121            return array(302, $media);
122        }
123    } else {
124        $media = cleanID($media);
125        if(empty($media)) {
126            return array(400, 'Bad request');
127        }
128        // check token for resized images
129        if (($width || $height) && media_get_token($media, $width, $height) !== $INPUT->str('tok')) {
130            return array(412, 'Precondition Failed');
131        }
132
133        //check permissions (namespace only)
134        if(auth_quickaclcheck(getNS($media).':X') < AUTH_READ) {
135            return array(403, 'Forbidden');
136        }
137        $file = mediaFN($media, $rev);
138    }
139
140    //check file existance
141    if(!@file_exists($file)) {
142        return array(404, 'Not Found');
143    }
144
145    return array(200, null);
146}
147
148/**
149 * Returns the wanted cachetime in seconds
150 *
151 * Resolves named constants
152 *
153 * @author  Andreas Gohr <andi@splitbrain.org>
154 *
155 * @param string $cache
156 * @return int cachetime in seconds
157 */
158function calc_cache($cache) {
159    global $conf;
160
161    if(strtolower($cache) == 'nocache') return 0; //never cache
162    if(strtolower($cache) == 'recache') return $conf['cachetime']; //use standard cache
163    return -1; //cache endless
164}
165