xref: /dokuwiki/inc/fetch.functions.php (revision a0e46368e9ad87d959bd921504ced42bc3d5b4bc)
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 $orig   original file to send - the file name will be used for the Content-Disposition
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 */
26function sendFile($file, $orig, $mime, $dl, $cache, $public = false) {
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    //download or display?
68    if($dl) {
69        header('Content-Disposition: attachment; filename="'.utf8_basename($orig).'";');
70    } else {
71        header('Content-Disposition: inline; filename="'.utf8_basename($orig).'";');
72    }
73
74    //use x-sendfile header to pass the delivery to compatible webservers
75    if(http_sendfile($file)) exit;
76
77    // send file contents
78    $fp = @fopen($file, "rb");
79    if($fp) {
80        http_rangeRequest($fp, filesize($file), $mime);
81    } else {
82        http_status(500);
83        print "Could not read $file - bad permissions?";
84    }
85}
86
87/**
88 * Check for media for preconditions and return correct status code
89 *
90 * READ: MEDIA, MIME, EXT, CACHE
91 * WRITE: MEDIA, FILE, array( STATUS, STATUSMESSAGE )
92 *
93 * @author Gerry Weissbach <gerry.w@gammaproduction.de>
94 * @param string $media  reference to the media id
95 * @param string $file   reference to the file variable
96 * @param string $rev
97 * @param int    $width
98 * @param int    $height
99 * @return array(STATUS, STATUSMESSAGE)
100 */
101function checkFileStatus(&$media, &$file, $rev = '', $width=0, $height=0) {
102    global $MIME, $EXT, $CACHE, $INPUT;
103
104    //media to local file
105    if(media_isexternal($media)) {
106        //check token for external image and additional for resized and cached images
107        if(media_get_token($media, $width, $height) !== $INPUT->str('tok')) {
108            return array(412, 'Precondition Failed');
109        }
110        //handle external images
111        if(strncmp($MIME, 'image/', 6) == 0) $file = media_get_from_URL($media, $EXT, $CACHE);
112        if(!$file) {
113            //download failed - redirect to original URL
114            return array(302, $media);
115        }
116    } else {
117        $media = cleanID($media);
118        if(empty($media)) {
119            return array(400, 'Bad request');
120        }
121        // check token for resized images
122        if (($width || $height) && media_get_token($media, $width, $height) !== $INPUT->str('tok')) {
123            return array(412, 'Precondition Failed');
124        }
125
126        //check permissions (namespace only)
127        if(auth_quickaclcheck(getNS($media).':X') < AUTH_READ) {
128            return array(403, 'Forbidden');
129        }
130        $file = mediaFN($media, $rev);
131    }
132
133    //check file existance
134    if(!@file_exists($file)) {
135        return array(404, 'Not Found');
136    }
137
138    return array(200, null);
139}
140
141/**
142 * Returns the wanted cachetime in seconds
143 *
144 * Resolves named constants
145 *
146 * @author  Andreas Gohr <andi@splitbrain.org>
147 */
148function calc_cache($cache) {
149    global $conf;
150
151    if(strtolower($cache) == 'nocache') return 0; //never cache
152    if(strtolower($cache) == 'recache') return $conf['cachetime']; //use standard cache
153    return -1; //cache endless
154}
155