xref: /dokuwiki/lib/exe/fetch.php (revision cce551cd93cf21893db3a351353f646de5facb86)
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    $evt = new Event('MEDIA_RESIZE', $data);
84    if($evt->advise_before()) {
85        if((substr($MIME, 0, 5) == 'image') && ($WIDTH || $HEIGHT)) {
86            if($HEIGHT && $WIDTH) {
87                $data['file'] = $FILE = media_crop_image($data['file'], $EXT, $WIDTH, $HEIGHT);
88            } else {
89                $data['file'] = $FILE = media_resize_image($data['file'], $EXT, $WIDTH, $HEIGHT);
90            }
91        }
92    }
93    $evt->advise_after();
94    unset($evt);
95
96    // finally send the file to the client
97    $evt = new Event('MEDIA_SENDFILE', $data);
98    if($evt->advise_before()) {
99        sendFile($data['file'], $data['mime'], $data['download'], $data['cache'], $data['ispublic'], $data['orig']);
100    }
101    // Do something after the download finished.
102    $evt->advise_after();  // will not be emitted on 304 or x-sendfile
103
104// END DO main
105
106//Setup VIM: ex: et ts=2 :
107