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