xref: /dokuwiki/inc/fetch.functions.php (revision 618191d008b98cb421694c541145c863d7b300ce)
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 * @param string $file   local file to send
20 * @param string $mime   mime type of the file
21 * @param bool   $dl     set to true to force a browser download
22 * @param int    $cache  remaining cache time in seconds (-1 for $conf['cache'], 0 for no-cache)
23 * @param bool   $public is this a public ressource or a private one?
24 * @param string $orig   original file to send - the file name will be used for the Content-Disposition
25 */
26function sendFile($file, $mime, $dl, $cache, $public = false, $orig = null) {
27    global $conf;
28    // send mime headers
29    header("Content-Type: $mime");
30
31    // calculate cache times
32    if($cache == -1) {
33        $maxage  = max($conf['cachetime'], 3600); // cachetime or one hour
34        $expires = time() + $maxage;
35    } else if($cache > 0) {
36        $maxage  = $cache; // given time
37        $expires = time() + $maxage;
38    } else { // $cache == 0
39        $maxage  = 0;
40        $expires = 0; // 1970-01-01
41    }
42
43    // smart http caching headers
44    if($maxage) {
45        if($public) {
46            // cache publically
47            header('Expires: '.gmdate("D, d M Y H:i:s", $expires).' GMT');
48            header('Cache-Control: public, proxy-revalidate, no-transform, max-age='.$maxage);
49            header('Pragma: public');
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            header('Pragma: no-cache');
55        }
56    } else {
57        // no cache at all
58        header('Expires: Thu, 01 Jan 1970 00:00:00 GMT');
59        header('Cache-Control: no-cache, no-transform');
60        header('Pragma: no-cache');
61    }
62
63    //send important headers first, script stops here if '304 Not Modified' response
64    $fmtime = @filemtime($file);
65    http_conditionalRequest($fmtime);
66
67    // Use the current $file if is $orig is not set.
68    if ( $orig == null ) {
69        $orig = $file;
70    }
71
72    //download or display?
73    if($dl) {
74        header('Content-Disposition: attachment; filename="'.utf8_basename($orig).'";');
75    } else {
76        header('Content-Disposition: inline; filename="'.utf8_basename($orig).'";');
77    }
78
79    //use x-sendfile header to pass the delivery to compatible webservers
80    if(http_sendfile($file)) exit;
81
82    // send file contents
83    $fp = @fopen($file, "rb");
84    if($fp) {
85        http_rangeRequest($fp, filesize($file), $mime);
86    } else {
87        http_status(500);
88        print "Could not read $file - bad permissions?";
89    }
90}
91
92/**
93 * Check for media for preconditions and return correct status code
94 *
95 * READ: MEDIA, MIME, EXT, CACHE
96 * WRITE: MEDIA, FILE, array( STATUS, STATUSMESSAGE )
97 *
98 * @author Gerry Weissbach <gerry.w@gammaproduction.de>
99 * @param string $media  reference to the media id
100 * @param string $file   reference to the file variable
101 * @param string $rev
102 * @param int    $width
103 * @param int    $height
104 * @return array(STATUS, STATUSMESSAGE)
105 */
106function checkFileStatus(&$media, &$file, $rev = '', $width=0, $height=0) {
107    global $MIME, $EXT, $CACHE, $INPUT;
108
109    //media to local file
110    if(media_isexternal($media)) {
111        //check token for external image and additional for resized and cached images
112        if(media_get_token($media, $width, $height) !== $INPUT->str('tok')) {
113            return array(412, 'Precondition Failed');
114        }
115        //handle external images
116        if(strncmp($MIME, 'image/', 6) == 0) $file = media_get_from_URL($media, $EXT, $CACHE);
117        if(!$file) {
118            //download failed - redirect to original URL
119            return array(302, $media);
120        }
121    } else {
122        $media = cleanID($media);
123        if(empty($media)) {
124            return array(400, 'Bad request');
125        }
126        // check token for resized images
127        if (($width || $height) && media_get_token($media, $width, $height) !== $INPUT->str('tok')) {
128            return array(412, 'Precondition Failed');
129        }
130
131        //check permissions (namespace only)
132        if(auth_quickaclcheck(getNS($media).':X') < AUTH_READ) {
133            return array(403, 'Forbidden');
134        }
135        $file = mediaFN($media, $rev);
136    }
137
138    //check file existance
139    if(!@file_exists($file)) {
140        return array(404, 'Not Found');
141    }
142
143    return array(200, null);
144}
145
146/**
147 * Returns the wanted cachetime in seconds
148 *
149 * Resolves named constants
150 *
151 * @author  Andreas Gohr <andi@splitbrain.org>
152 */
153function calc_cache($cache) {
154    global $conf;
155
156    if(strtolower($cache) == 'nocache') return 0; //never cache
157    if(strtolower($cache) == 'recache') return $conf['cachetime']; //use standard cache
158    return -1; //cache endless
159}
160