xref: /dokuwiki/lib/exe/fetch.php (revision add8678f233ad74892a96444e3013e0465616200)
1f62ea8a1Sandi<?php
2f62ea8a1Sandi/**
3f62ea8a1Sandi * DokuWiki media passthrough file
4f62ea8a1Sandi *
5f62ea8a1Sandi * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
6f62ea8a1Sandi * @author     Andreas Gohr <andi@splitbrain.org>
7f62ea8a1Sandi */
8f62ea8a1Sandi
9d0a27cb0SAndreas Gohrif(!defined('DOKU_INC')) define('DOKU_INC', dirname(__FILE__).'/../../');
103138b5c7SAndreas Gohrdefine('DOKU_DISABLE_GZIP_OUTPUT', 1);
11f62ea8a1Sandirequire_once(DOKU_INC.'inc/init.php');
1236625b96SAndreas Gohrsession_write_close(); //close session
138746e727Sandi
1436625b96SAndreas Gohr// BEGIN main (if not testing)
1536625b96SAndreas Gohrif(!defined('SIMPLE_TEST')) {
16f62ea8a1Sandi    $mimetypes = getMimeTypes();
17f62ea8a1Sandi
18f62ea8a1Sandi    //get input
1902b0b681SAndreas Gohr    $MEDIA  = stripctl(getID('media', false)); // no cleaning except control chars - maybe external
20bfd0f597STom N Harris    $CACHE  = calc_cache($INPUT->str('cache'));
21bfd0f597STom N Harris    $WIDTH  = $INPUT->int('w');
22bfd0f597STom N Harris    $HEIGHT = $INPUT->int('h');
23bfd0f597STom N Harris    $REV    = & $INPUT->ref('rev');
24fc4aefb9SKate Arzamastseva    //sanitize revision
25fc4aefb9SKate Arzamastseva    $REV = preg_replace('/[^0-9]/', '', $REV);
26fc4aefb9SKate Arzamastseva
2727bf7924STom N Harris    list($EXT, $MIME, $DL) = mimetype($MEDIA, false);
28f62ea8a1Sandi    if($EXT === false) {
29f62ea8a1Sandi        $EXT  = 'unknown';
30f62ea8a1Sandi        $MIME = 'application/octet-stream';
31ecebf3a8SAndreas Gohr        $DL   = true;
32f62ea8a1Sandi    }
33f62ea8a1Sandi
3403293305SAndreas Gohr    // check for permissions, preconditions and cache external files
35fc4aefb9SKate Arzamastseva    list($STATUS, $STATUSMESSAGE) = checkFileStatus($MEDIA, $FILE, $REV);
3603293305SAndreas Gohr
3703293305SAndreas Gohr    // prepare data for plugin events
383b399a1bSAndreas Gohr    $data = array(
393b399a1bSAndreas Gohr        'media'         => $MEDIA,
40cd98d9c3SGerry Weißbach        'file'          => $FILE,
41cd98d9c3SGerry Weißbach        'orig'          => $FILE,
42cd98d9c3SGerry Weißbach        'mime'          => $MIME,
43cd98d9c3SGerry Weißbach        'download'      => $DL,
44cd98d9c3SGerry Weißbach        'cache'         => $CACHE,
45cd98d9c3SGerry Weißbach        'ext'           => $EXT,
46cd98d9c3SGerry Weißbach        'width'         => $WIDTH,
47cd98d9c3SGerry Weißbach        'height'        => $HEIGHT,
48cd98d9c3SGerry Weißbach        'status'        => $STATUS,
49cd98d9c3SGerry Weißbach        'statusmessage' => $STATUSMESSAGE,
50*add8678fSAndreas Gohr        'ispublic'      => media_ispublic($MEDIA),
51cd98d9c3SGerry Weißbach    );
52f62ea8a1Sandi
5303293305SAndreas Gohr    // handle the file status
5403293305SAndreas Gohr    $evt = new Doku_Event('FETCH_MEDIA_STATUS', $data);
55cd98d9c3SGerry Weißbach    if($evt->advise_before()) {
5603293305SAndreas Gohr        // redirects
5703293305SAndreas Gohr        if($data['status'] > 300 && $data['status'] <= 304) {
5803293305SAndreas Gohr            send_redirect($data['statusmessage']);
5903293305SAndreas Gohr        }
6003293305SAndreas Gohr        // send any non 200 status
6103293305SAndreas Gohr        if($data['status'] != 200) {
629d2e1be6SAndreas Gohr            http_status($data['status'], $data['statusmessage']);
6303293305SAndreas Gohr        }
6403293305SAndreas Gohr        // die on errors
6503293305SAndreas Gohr        if($data['status'] > 203) {
66cd98d9c3SGerry Weißbach            print $data['statusmessage'];
67f62ea8a1Sandi            exit;
68f62ea8a1Sandi        }
69f62ea8a1Sandi    }
7003293305SAndreas Gohr    $evt->advise_after();
7103293305SAndreas Gohr    unset($evt);
72f62ea8a1Sandi
7320bc86cfSAndreas Gohr    //handle image resizing/cropping
74f62ea8a1Sandi    if((substr($MIME, 0, 5) == 'image') && $WIDTH) {
75d52a56e1SAndreas Gohr        if($HEIGHT) {
76cd98d9c3SGerry Weißbach            $data['file'] = $FILE = media_crop_image($data['file'], $EXT, $WIDTH, $HEIGHT);
7720bc86cfSAndreas Gohr        } else {
78cd98d9c3SGerry Weißbach            $data['file'] = $FILE = media_resize_image($data['file'], $EXT, $WIDTH, $HEIGHT);
79f62ea8a1Sandi        }
8020bc86cfSAndreas Gohr    }
81f62ea8a1Sandi
82e935fb4aSAndreas Gohr    // finally send the file to the client
83b80bedd6SAndreas Gohr    $evt = new Doku_Event('MEDIA_SENDFILE', $data);
84b80bedd6SAndreas Gohr    if($evt->advise_before()) {
85*add8678fSAndreas Gohr        $cache = $data['cache'];
86*add8678fSAndreas Gohr        if($cache != 0 && !$data['ispublic']) $cache = 0; // no cache headers for private files FS#2734
87*add8678fSAndreas Gohr
88*add8678fSAndreas Gohr        sendFile($data['file'], $data['mime'], $data['download'], $cache);
89b80bedd6SAndreas Gohr    }
90cd98d9c3SGerry Weißbach    // Do something after the download finished.
91*add8678fSAndreas Gohr    $evt->advise_after();  // will not be emitted on 304 or x-sendfile
92f62ea8a1Sandi
9336625b96SAndreas Gohr}// END DO main
9436625b96SAndreas Gohr
95e935fb4aSAndreas Gohr/* ------------------------------------------------------------------------ */
96f62ea8a1Sandi
97e935fb4aSAndreas Gohr/**
98e935fb4aSAndreas Gohr * Set headers and send the file to the client
99e935fb4aSAndreas Gohr *
100*add8678fSAndreas Gohr * Unless $cache is set to 0, the data may end up in intermediate proxy servers. Therefor,
101*add8678fSAndreas Gohr * if you're sending (ACL protected) private files, $cache should be 0.
102*add8678fSAndreas Gohr *
103*add8678fSAndreas Gohr * This function will abort the current script when a 304 is sent or file sending is handled
104*add8678fSAndreas Gohr * through x-sendfile
105*add8678fSAndreas Gohr *
106e935fb4aSAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org>
10783730152SBen Coburn * @author Ben Coburn <btcoburn@silicodon.net>
108*add8678fSAndreas Gohr * @param string $file  local file to send
109*add8678fSAndreas Gohr * @param string $mime  mime type of the file
110*add8678fSAndreas Gohr * @param bool   $dl    set to true to force a browser download
111*add8678fSAndreas Gohr * @param int    $cache remaining cache time in seconds (-1 for $conf['cache'], 0 for off)
112e935fb4aSAndreas Gohr */
113ecebf3a8SAndreas Gohrfunction sendFile($file, $mime, $dl, $cache) {
11483730152SBen Coburn    global $conf;
11509a5b61fSgweissbach    $fmtime = @filemtime($file);
116e935fb4aSAndreas Gohr    // send headers
117e935fb4aSAndreas Gohr    header("Content-Type: $mime");
11883730152SBen Coburn    // smart http caching headers
11983730152SBen Coburn    if($cache == -1) {
12083730152SBen Coburn        // cache
12183730152SBen Coburn        // cachetime or one hour
12283730152SBen Coburn        header('Expires: '.gmdate("D, d M Y H:i:s", time() + max($conf['cachetime'], 3600)).' GMT');
12383730152SBen Coburn        header('Cache-Control: public, proxy-revalidate, no-transform, max-age='.max($conf['cachetime'], 3600));
124e935fb4aSAndreas Gohr        header('Pragma: public');
12583730152SBen Coburn    } else if($cache > 0) {
12683730152SBen Coburn        // recache
12783730152SBen Coburn        // remaining cachetime + 10 seconds so the newly recached media is used
12883730152SBen Coburn        header('Expires: '.gmdate("D, d M Y H:i:s", $fmtime + $conf['cachetime'] + 10).' GMT');
12983730152SBen Coburn        header('Cache-Control: public, proxy-revalidate, no-transform, max-age='.max($fmtime - time() + $conf['cachetime'] + 10, 0));
13083730152SBen Coburn        header('Pragma: public');
13183730152SBen Coburn    } else if($cache == 0) {
132*add8678fSAndreas Gohr        // nocache, avoid resending files from intermediate caches without revalidation FS#2734
133*add8678fSAndreas Gohr        header('Expires: Thu, 01 Jan 1970 00:00:00 GMT');
134*add8678fSAndreas Gohr        header('Cache-Control: private, no-transform, max-age=0');
135*add8678fSAndreas Gohr        header('Pragma: no-cache');
13683730152SBen Coburn    }
137ff4f5ee7SBen Coburn    //send important headers first, script stops here if '304 Not Modified' response
13883730152SBen Coburn    http_conditionalRequest($fmtime);
1399a87c72aSAndreas Gohr
140ecebf3a8SAndreas Gohr    //download or display?
141ecebf3a8SAndreas Gohr    if($dl) {
1423009a773SAndreas Gohr        header('Content-Disposition: attachment; filename="'.utf8_basename($file).'";');
143ecebf3a8SAndreas Gohr    } else {
1443009a773SAndreas Gohr        header('Content-Disposition: inline; filename="'.utf8_basename($file).'";');
145f62ea8a1Sandi    }
146f62ea8a1Sandi
1479a87c72aSAndreas Gohr    //use x-sendfile header to pass the delivery to compatible webservers
1486106ad89SChris Smith    if(http_sendfile($file)) exit;
1499a87c72aSAndreas Gohr
150e935fb4aSAndreas Gohr    // send file contents
151e935fb4aSAndreas Gohr    $fp = @fopen($file, "rb");
152f62ea8a1Sandi    if($fp) {
153758447cfSAndreas Gohr        http_rangeRequest($fp, filesize($file), $mime);
154f62ea8a1Sandi    } else {
1559d2e1be6SAndreas Gohr        http_status(500);
156e935fb4aSAndreas Gohr        print "Could not read $file - bad permissions?";
157e935fb4aSAndreas Gohr    }
158f62ea8a1Sandi}
159f62ea8a1Sandi
16003293305SAndreas Gohr/**
16103293305SAndreas Gohr * Check for media for preconditions and return correct status code
162cd98d9c3SGerry Weißbach *
163cd98d9c3SGerry Weißbach * READ: MEDIA, MIME, EXT, CACHE
164cd98d9c3SGerry Weißbach * WRITE: MEDIA, FILE, array( STATUS, STATUSMESSAGE )
165cd98d9c3SGerry Weißbach *
166cd98d9c3SGerry Weißbach * @author Gerry Weissbach <gerry.w@gammaproduction.de>
167cd98d9c3SGerry Weißbach * @param $media reference to the media id
168cd98d9c3SGerry Weißbach * @param $file  reference to the file variable
169cd98d9c3SGerry Weißbach * @returns array(STATUS, STATUSMESSAGE)
170cd98d9c3SGerry Weißbach */
171fc4aefb9SKate Arzamastsevafunction checkFileStatus(&$media, &$file, $rev = '') {
1725373d847SHakan Sandell    global $MIME, $EXT, $CACHE, $INPUT;
173cd98d9c3SGerry Weißbach
174cd98d9c3SGerry Weißbach    //media to local file
175cd98d9c3SGerry Weißbach    if(preg_match('#^(https?)://#i', $media)) {
176cd98d9c3SGerry Weißbach        //check hash
17758789954SAndreas Gohr        if(substr(md5(auth_cookiesalt().$media), 0, 6) !== $INPUT->str('hash')) {
178cd98d9c3SGerry Weißbach            return array(412, 'Precondition Failed');
179cd98d9c3SGerry Weißbach        }
180cd98d9c3SGerry Weißbach        //handle external images
181cd98d9c3SGerry Weißbach        if(strncmp($MIME, 'image/', 6) == 0) $file = media_get_from_URL($media, $EXT, $CACHE);
182cd98d9c3SGerry Weißbach        if(!$file) {
183cd98d9c3SGerry Weißbach            //download failed - redirect to original URL
18403293305SAndreas Gohr            return array(302, $media);
185cd98d9c3SGerry Weißbach        }
186cd98d9c3SGerry Weißbach    } else {
187cd98d9c3SGerry Weißbach        $media = cleanID($media);
188cd98d9c3SGerry Weißbach        if(empty($media)) {
189cd98d9c3SGerry Weißbach            return array(400, 'Bad request');
190cd98d9c3SGerry Weißbach        }
191cd98d9c3SGerry Weißbach
192cd98d9c3SGerry Weißbach        //check permissions (namespace only)
193cd98d9c3SGerry Weißbach        if(auth_quickaclcheck(getNS($media).':X') < AUTH_READ) {
194c6f610efSAndreas Gohr            return array(403, 'Forbidden');
195cd98d9c3SGerry Weißbach        }
196fc4aefb9SKate Arzamastseva        $file = mediaFN($media, $rev);
197cd98d9c3SGerry Weißbach    }
198cd98d9c3SGerry Weißbach
199cd98d9c3SGerry Weißbach    //check file existance
200cd98d9c3SGerry Weißbach    if(!@file_exists($file)) {
201cd98d9c3SGerry Weißbach        return array(404, 'Not Found');
202cd98d9c3SGerry Weißbach    }
203cd98d9c3SGerry Weißbach
20403293305SAndreas Gohr    return array(200, null);
205cd98d9c3SGerry Weißbach}
206cd98d9c3SGerry Weißbach
207e935fb4aSAndreas Gohr/**
208f62ea8a1Sandi * Returns the wanted cachetime in seconds
209f62ea8a1Sandi *
210f62ea8a1Sandi * Resolves named constants
211f62ea8a1Sandi *
212f62ea8a1Sandi * @author  Andreas Gohr <andi@splitbrain.org>
213f62ea8a1Sandi */
214f62ea8a1Sandifunction calc_cache($cache) {
215f62ea8a1Sandi    global $conf;
216f62ea8a1Sandi
217f62ea8a1Sandi    if(strtolower($cache) == 'nocache') return 0; //never cache
218f62ea8a1Sandi    if(strtolower($cache) == 'recache') return $conf['cachetime']; //use standard cache
219f62ea8a1Sandi    return -1; //cache endless
220f62ea8a1Sandi}
221f62ea8a1Sandi
222e3776c06SMichael Hamann//Setup VIM: ex: et ts=2 :
223