xref: /dokuwiki/lib/exe/fetch.php (revision 3b6f95e62fc7049712b96aacd245be507f83d5ee)
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,
50add8678fSAndreas 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()) {
851c7d84beSAndreas Gohr        sendFile($data['file'], $data['mime'], $data['download'], $data['cache'], $data['ispublic']);
86b80bedd6SAndreas Gohr    }
87cd98d9c3SGerry Weißbach    // Do something after the download finished.
88add8678fSAndreas Gohr    $evt->advise_after();  // will not be emitted on 304 or x-sendfile
89f62ea8a1Sandi
9036625b96SAndreas Gohr}// END DO main
9136625b96SAndreas Gohr
92e935fb4aSAndreas Gohr/* ------------------------------------------------------------------------ */
93f62ea8a1Sandi
94e935fb4aSAndreas Gohr/**
95e935fb4aSAndreas Gohr * Set headers and send the file to the client
96e935fb4aSAndreas Gohr *
974a516840SAndreas Gohr * The $cache parameter influences how long files may be kept in caches, the $public parameter
984a516840SAndreas Gohr * influences if this caching may happen in public proxis or in the browser cache only FS#2734
99add8678fSAndreas Gohr *
100add8678fSAndreas Gohr * This function will abort the current script when a 304 is sent or file sending is handled
101add8678fSAndreas Gohr * through x-sendfile
102add8678fSAndreas Gohr *
103e935fb4aSAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org>
10483730152SBen Coburn * @author Ben Coburn <btcoburn@silicodon.net>
105add8678fSAndreas Gohr * @param string $file   local file to send
106add8678fSAndreas Gohr * @param string $mime   mime type of the file
107add8678fSAndreas Gohr * @param bool   $dl     set to true to force a browser download
1084a516840SAndreas Gohr * @param int    $cache  remaining cache time in seconds (-1 for $conf['cache'], 0 for no-cache)
1094a516840SAndreas Gohr * @param bool   $public is this a public ressource or a private one?
110e935fb4aSAndreas Gohr */
1114a516840SAndreas Gohrfunction sendFile($file, $mime, $dl, $cache, $public = false) {
11283730152SBen Coburn    global $conf;
1134a516840SAndreas Gohr    // send mime headers
114e935fb4aSAndreas Gohr    header("Content-Type: $mime");
1154a516840SAndreas Gohr
1164a516840SAndreas Gohr    // calculate cache times
11783730152SBen Coburn    if($cache == -1) {
1184a516840SAndreas Gohr        $maxage  = max($conf['cachetime'], 3600); // cachetime or one hour
1194a516840SAndreas Gohr        $expires = time() + $maxage;
12083730152SBen Coburn    } else if($cache > 0) {
1214a516840SAndreas Gohr        $maxage  = $cache; // given time
1224a516840SAndreas Gohr        $expires = time() + $maxage;
1234a516840SAndreas Gohr    } else { // $cache == 0
1244a516840SAndreas Gohr        $maxage  = 0;
1254a516840SAndreas Gohr        $expires = 0; // 1970-01-01
1264a516840SAndreas Gohr    }
1274a516840SAndreas Gohr
1284a516840SAndreas Gohr    // smart http caching headers
1294a516840SAndreas Gohr    if($maxage) {
1304a516840SAndreas Gohr        if($public) {
1314a516840SAndreas Gohr            // cache publically
1324a516840SAndreas Gohr            header('Expires: '.gmdate("D, d M Y H:i:s", $expires).' GMT');
1334a516840SAndreas Gohr            header('Cache-Control: public, proxy-revalidate, no-transform, max-age='.$maxage);
13483730152SBen Coburn            header('Pragma: public');
1354a516840SAndreas Gohr        } else {
1364a516840SAndreas Gohr            // cache in browser
1374a516840SAndreas Gohr            header('Expires: '.gmdate("D, d M Y H:i:s", $expires).' GMT');
1384a516840SAndreas Gohr            header('Cache-Control: private, no-transform, max-age='.$maxage);
1394a516840SAndreas Gohr            header('Pragma: private');
1404a516840SAndreas Gohr        }
1414a516840SAndreas Gohr    } else {
1424a516840SAndreas Gohr        // no cache at all
143add8678fSAndreas Gohr        header('Expires: Thu, 01 Jan 1970 00:00:00 GMT');
144*3b6f95e6SAndreas Gohr        header('Cache-Control: no-cache, no-transform, max-age=0');
145*3b6f95e6SAndreas Gohr        header('Pragma: no-cache');
14683730152SBen Coburn    }
1474a516840SAndreas Gohr
148ff4f5ee7SBen Coburn    //send important headers first, script stops here if '304 Not Modified' response
1494a516840SAndreas Gohr    $fmtime = @filemtime($file);
15083730152SBen Coburn    http_conditionalRequest($fmtime);
1519a87c72aSAndreas Gohr
152ecebf3a8SAndreas Gohr    //download or display?
153ecebf3a8SAndreas Gohr    if($dl) {
1543009a773SAndreas Gohr        header('Content-Disposition: attachment; filename="'.utf8_basename($file).'";');
155ecebf3a8SAndreas Gohr    } else {
1563009a773SAndreas Gohr        header('Content-Disposition: inline; filename="'.utf8_basename($file).'";');
157f62ea8a1Sandi    }
158f62ea8a1Sandi
1599a87c72aSAndreas Gohr    //use x-sendfile header to pass the delivery to compatible webservers
1606106ad89SChris Smith    if(http_sendfile($file)) exit;
1619a87c72aSAndreas Gohr
162e935fb4aSAndreas Gohr    // send file contents
163e935fb4aSAndreas Gohr    $fp = @fopen($file, "rb");
164f62ea8a1Sandi    if($fp) {
165758447cfSAndreas Gohr        http_rangeRequest($fp, filesize($file), $mime);
166f62ea8a1Sandi    } else {
1679d2e1be6SAndreas Gohr        http_status(500);
168e935fb4aSAndreas Gohr        print "Could not read $file - bad permissions?";
169e935fb4aSAndreas Gohr    }
170f62ea8a1Sandi}
171f62ea8a1Sandi
17203293305SAndreas Gohr/**
17303293305SAndreas Gohr * Check for media for preconditions and return correct status code
174cd98d9c3SGerry Weißbach *
175cd98d9c3SGerry Weißbach * READ: MEDIA, MIME, EXT, CACHE
176cd98d9c3SGerry Weißbach * WRITE: MEDIA, FILE, array( STATUS, STATUSMESSAGE )
177cd98d9c3SGerry Weißbach *
178cd98d9c3SGerry Weißbach * @author Gerry Weissbach <gerry.w@gammaproduction.de>
179cd98d9c3SGerry Weißbach * @param $media reference to the media id
180cd98d9c3SGerry Weißbach * @param $file  reference to the file variable
181cd98d9c3SGerry Weißbach * @returns array(STATUS, STATUSMESSAGE)
182cd98d9c3SGerry Weißbach */
183fc4aefb9SKate Arzamastsevafunction checkFileStatus(&$media, &$file, $rev = '') {
1845373d847SHakan Sandell    global $MIME, $EXT, $CACHE, $INPUT;
185cd98d9c3SGerry Weißbach
186cd98d9c3SGerry Weißbach    //media to local file
187cd98d9c3SGerry Weißbach    if(preg_match('#^(https?)://#i', $media)) {
188cd98d9c3SGerry Weißbach        //check hash
18958789954SAndreas Gohr        if(substr(md5(auth_cookiesalt().$media), 0, 6) !== $INPUT->str('hash')) {
190cd98d9c3SGerry Weißbach            return array(412, 'Precondition Failed');
191cd98d9c3SGerry Weißbach        }
192cd98d9c3SGerry Weißbach        //handle external images
193cd98d9c3SGerry Weißbach        if(strncmp($MIME, 'image/', 6) == 0) $file = media_get_from_URL($media, $EXT, $CACHE);
194cd98d9c3SGerry Weißbach        if(!$file) {
195cd98d9c3SGerry Weißbach            //download failed - redirect to original URL
19603293305SAndreas Gohr            return array(302, $media);
197cd98d9c3SGerry Weißbach        }
198cd98d9c3SGerry Weißbach    } else {
199cd98d9c3SGerry Weißbach        $media = cleanID($media);
200cd98d9c3SGerry Weißbach        if(empty($media)) {
201cd98d9c3SGerry Weißbach            return array(400, 'Bad request');
202cd98d9c3SGerry Weißbach        }
203cd98d9c3SGerry Weißbach
204cd98d9c3SGerry Weißbach        //check permissions (namespace only)
205cd98d9c3SGerry Weißbach        if(auth_quickaclcheck(getNS($media).':X') < AUTH_READ) {
206c6f610efSAndreas Gohr            return array(403, 'Forbidden');
207cd98d9c3SGerry Weißbach        }
208fc4aefb9SKate Arzamastseva        $file = mediaFN($media, $rev);
209cd98d9c3SGerry Weißbach    }
210cd98d9c3SGerry Weißbach
211cd98d9c3SGerry Weißbach    //check file existance
212cd98d9c3SGerry Weißbach    if(!@file_exists($file)) {
213cd98d9c3SGerry Weißbach        return array(404, 'Not Found');
214cd98d9c3SGerry Weißbach    }
215cd98d9c3SGerry Weißbach
21603293305SAndreas Gohr    return array(200, null);
217cd98d9c3SGerry Weißbach}
218cd98d9c3SGerry Weißbach
219e935fb4aSAndreas Gohr/**
220f62ea8a1Sandi * Returns the wanted cachetime in seconds
221f62ea8a1Sandi *
222f62ea8a1Sandi * Resolves named constants
223f62ea8a1Sandi *
224f62ea8a1Sandi * @author  Andreas Gohr <andi@splitbrain.org>
225f62ea8a1Sandi */
226f62ea8a1Sandifunction calc_cache($cache) {
227f62ea8a1Sandi    global $conf;
228f62ea8a1Sandi
229f62ea8a1Sandi    if(strtolower($cache) == 'nocache') return 0; //never cache
230f62ea8a1Sandi    if(strtolower($cache) == 'recache') return $conf['cachetime']; //use standard cache
231f62ea8a1Sandi    return -1; //cache endless
232f62ea8a1Sandi}
233f62ea8a1Sandi
234e3776c06SMichael Hamann//Setup VIM: ex: et ts=2 :
235