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        'csp' => [
59            'sandbox' => '',
60            'default-src' => "'none'",
61            'style-src' => "'unsafe-inline'",
62            'media-src' => "'self'",
63            'object-src' => "'self'",
64            'form-action' => "'none'",
65            'frame-ancestors' => "'self'",
66        ],
67    );
68
69    // handle the file status
70    $evt = new Event('FETCH_MEDIA_STATUS', $data);
71    if($evt->advise_before()) {
72        // redirects
73        if($data['status'] > 300 && $data['status'] <= 304) {
74            if (defined('SIMPLE_TEST')) return; //TestResponse doesn't recognize redirects
75            send_redirect($data['statusmessage']);
76        }
77        // send any non 200 status
78        if($data['status'] != 200) {
79            http_status($data['status'], $data['statusmessage']);
80        }
81        // die on errors
82        if($data['status'] > 203) {
83            print $data['statusmessage'];
84            if (defined('SIMPLE_TEST')) return;
85            exit;
86        }
87    }
88    $evt->advise_after();
89    unset($evt);
90
91    //handle image resizing/cropping
92    $evt = new Event('MEDIA_RESIZE', $data);
93    if($evt->advise_before()) {
94        if((substr($MIME, 0, 5) == 'image') && ($WIDTH || $HEIGHT)) {
95            if($HEIGHT && $WIDTH) {
96                $data['file'] = $FILE = media_crop_image($data['file'], $EXT, $WIDTH, $HEIGHT);
97            } else {
98                $data['file'] = $FILE = media_resize_image($data['file'], $EXT, $WIDTH, $HEIGHT);
99            }
100        }
101    }
102    $evt->advise_after();
103    unset($evt);
104
105    // finally send the file to the client
106    $evt = new Event('MEDIA_SENDFILE', $data);
107    if($evt->advise_before()) {
108        sendFile(
109            $data['file'],
110            $data['mime'],
111            $data['download'],
112            $data['cache'],
113            $data['ispublic'],
114            $data['orig'],
115            $data['csp']
116        );
117    }
118    // Do something after the download finished.
119    $evt->advise_after();  // will not be emitted on 304 or x-sendfile
120
121// END DO main
122
123//Setup VIM: ex: et ts=2 :
124