xref: /dokuwiki/lib/exe/fetch.php (revision 83734cddc910b198a1a199d89dad99d7831c1590)
1<?php
2/**
3 * DokuWiki media passthrough file
4 *
5 * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
6 * @author     Andreas Gohr <andi@splitbrain.org>
7 */
8
9use dokuwiki\Extension\Event;
10
11if(!defined('DOKU_INC')) define('DOKU_INC', dirname(__FILE__).'/../../');
12if (!defined('DOKU_DISABLE_GZIP_OUTPUT')) define('DOKU_DISABLE_GZIP_OUTPUT', 1);
13require_once(DOKU_INC.'inc/init.php');
14session_write_close(); //close session
15
16require_once(DOKU_INC.'inc/fetch.functions.php');
17
18if (defined('SIMPLE_TEST')) {
19    $INPUT = new \dokuwiki\Input\Input();
20}
21
22// BEGIN main
23    $mimetypes = getMimeTypes();
24
25    //get input
26    $MEDIA  = stripctl(getID('media', false)); // no cleaning except control chars - maybe external
27    $CACHE  = calc_cache($INPUT->str('cache'));
28    $WIDTH  = $INPUT->int('w');
29    $HEIGHT = $INPUT->int('h');
30    $REV    = & $INPUT->ref('rev');
31    //sanitize revision
32    $REV = preg_replace('/[^0-9]/', '', $REV);
33
34    list($EXT, $MIME, $DL) = mimetype($MEDIA, false);
35    if($EXT === false) {
36        $EXT  = 'unknown';
37        $MIME = 'application/octet-stream';
38        $DL   = true;
39    }
40
41    // check for permissions, preconditions and cache external files
42    list($STATUS, $STATUSMESSAGE) = checkFileStatus($MEDIA, $FILE, $REV, $WIDTH, $HEIGHT);
43
44    // prepare data for plugin events
45    $data = array(
46        'media'         => $MEDIA,
47        'file'          => $FILE,
48        'orig'          => $FILE,
49        'mime'          => $MIME,
50        'download'      => $DL,
51        'cache'         => $CACHE,
52        'ext'           => $EXT,
53        'width'         => $WIDTH,
54        'height'        => $HEIGHT,
55        'status'        => $STATUS,
56        'statusmessage' => $STATUSMESSAGE,
57        'ispublic'      => media_ispublic($MEDIA),
58    );
59
60    // handle the file status
61    $evt = new Event('FETCH_MEDIA_STATUS', $data);
62    if($evt->advise_before()) {
63        // redirects
64        if($data['status'] > 300 && $data['status'] <= 304) {
65            if (defined('SIMPLE_TEST')) return; //TestResponse doesn't recognize redirects
66            send_redirect($data['statusmessage']);
67        }
68        // send any non 200 status
69        if($data['status'] != 200) {
70            http_status($data['status'], $data['statusmessage']);
71        }
72        // die on errors
73        if($data['status'] > 203) {
74            print $data['statusmessage'];
75            if (defined('SIMPLE_TEST')) return;
76            exit;
77        }
78    }
79    $evt->advise_after();
80    unset($evt);
81
82    //handle image resizing/cropping
83    if((substr($MIME, 0, 5) == 'image') && ($WIDTH || $HEIGHT)) {
84        if($HEIGHT && $WIDTH) {
85            $data['file'] = $FILE = media_crop_image($data['file'], $EXT, $WIDTH, $HEIGHT);
86        } else {
87            $data['file'] = $FILE = media_resize_image($data['file'], $EXT, $WIDTH, $HEIGHT);
88        }
89    }
90
91    // finally send the file to the client
92    $evt = new Event('MEDIA_SENDFILE', $data);
93    if($evt->advise_before()) {
94        sendFile($data['file'], $data['mime'], $data['download'], $data['cache'], $data['ispublic'], $data['orig']);
95    }
96    // Do something after the download finished.
97    $evt->advise_after();  // will not be emitted on 304 or x-sendfile
98
99// END DO main
100
101//Setup VIM: ex: et ts=2 :
102