xref: /dokuwiki/lib/exe/fetch.php (revision 5a5ec053461c2401bdae52ca4bc4bec1245d79d9)
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 */
8*5a5ec053SGerrit Uitslag
9e3c3abf1SAndreas Gohruse dokuwiki\Input\Input;
10e1d9dcc8SAndreas Gohruse dokuwiki\Extension\Event;
11e1d9dcc8SAndreas Gohr
12e3c3abf1SAndreas Gohrif (!defined('DOKU_INC')) define('DOKU_INC', __DIR__ . '/../../');
137fb7960fSChristopher Smithif (!defined('DOKU_DISABLE_GZIP_OUTPUT')) define('DOKU_DISABLE_GZIP_OUTPUT', 1);
14f62ea8a1Sandirequire_once(DOKU_INC . 'inc/init.php');
1536625b96SAndreas Gohrsession_write_close(); //close session
168746e727Sandi
177fb7960fSChristopher Smithrequire_once(DOKU_INC . 'inc/fetch.functions.php');
187fb7960fSChristopher Smith
197fb7960fSChristopher Smithif (defined('SIMPLE_TEST')) {
20e3c3abf1SAndreas Gohr    $INPUT = new Input();
217fb7960fSChristopher Smith}
227fb7960fSChristopher Smith
237fb7960fSChristopher Smith// BEGIN main
24f62ea8a1Sandi$mimetypes = getMimeTypes();
25f62ea8a1Sandi
26f62ea8a1Sandi//get input
2702b0b681SAndreas Gohr$MEDIA = stripctl(getID('media', false)); // no cleaning except control chars - maybe external
28bfd0f597STom N Harris$CACHE = calc_cache($INPUT->str('cache'));
29bfd0f597STom N Harris$WIDTH = $INPUT->int('w');
30bfd0f597STom N Harris$HEIGHT = $INPUT->int('h');
31bfd0f597STom N Harris$REV = &$INPUT->ref('rev');
32fc4aefb9SKate Arzamastseva//sanitize revision
33fc4aefb9SKate Arzamastseva$REV = preg_replace('/[^0-9]/', '', $REV);
34fc4aefb9SKate Arzamastseva
35e3c3abf1SAndreas Gohr[$EXT, $MIME, $DL] = mimetype($MEDIA, false);
36f62ea8a1Sandiif ($EXT === false) {
37f62ea8a1Sandi    $EXT = 'unknown';
38f62ea8a1Sandi    $MIME = 'application/octet-stream';
39ecebf3a8SAndreas Gohr    $DL = true;
40f62ea8a1Sandi}
41f62ea8a1Sandi
4203293305SAndreas Gohr// check for permissions, preconditions and cache external files
43e3c3abf1SAndreas Gohr[$STATUS, $STATUSMESSAGE] = checkFileStatus($MEDIA, $FILE, $REV, $WIDTH, $HEIGHT);
4403293305SAndreas Gohr
4503293305SAndreas Gohr// prepare data for plugin events
46e3c3abf1SAndreas Gohr$data = [
473b399a1bSAndreas Gohr    'media' => $MEDIA,
48cd98d9c3SGerry Weißbach    'file' => $FILE,
49cd98d9c3SGerry Weißbach    'orig' => $FILE,
50cd98d9c3SGerry Weißbach    'mime' => $MIME,
51cd98d9c3SGerry Weißbach    'download' => $DL,
52cd98d9c3SGerry Weißbach    'cache' => $CACHE,
53cd98d9c3SGerry Weißbach    'ext' => $EXT,
54cd98d9c3SGerry Weißbach    'width' => $WIDTH,
55cd98d9c3SGerry Weißbach    'height' => $HEIGHT,
56cd98d9c3SGerry Weißbach    'status' => $STATUS,
57cd98d9c3SGerry Weißbach    'statusmessage' => $STATUSMESSAGE,
58add8678fSAndreas Gohr    'ispublic' => media_ispublic($MEDIA),
596cda96e3SAndreas Gohr    'csp' => [
606cda96e3SAndreas Gohr        'default-src' => "'none'",
616cda96e3SAndreas Gohr        'style-src' => "'unsafe-inline'",
626cda96e3SAndreas Gohr        'media-src' => "'self'",
636cda96e3SAndreas Gohr        'object-src' => "'self'",
6436300e60SAndreas Gohr        'font-src' => "'self' data:",
656cda96e3SAndreas Gohr        'form-action' => "'none'",
6601648efdSAndreas Gohr        'frame-ancestors' => "'self'",
67e3c3abf1SAndreas Gohr    ]
68e3c3abf1SAndreas Gohr];
69f62ea8a1Sandi
7003293305SAndreas Gohr// handle the file status
71e1d9dcc8SAndreas Gohr$evt = new Event('FETCH_MEDIA_STATUS', $data);
72cd98d9c3SGerry Weißbachif ($evt->advise_before()) {
7303293305SAndreas Gohr    // redirects
7403293305SAndreas Gohr    if ($data['status'] > 300 && $data['status'] <= 304) {
75d572baf8SKlap-in        if (defined('SIMPLE_TEST')) return; //TestResponse doesn't recognize redirects
7603293305SAndreas Gohr        send_redirect($data['statusmessage']);
7703293305SAndreas Gohr    }
7803293305SAndreas Gohr    // send any non 200 status
7903293305SAndreas Gohr    if ($data['status'] != 200) {
809d2e1be6SAndreas Gohr        http_status($data['status'], $data['statusmessage']);
8103293305SAndreas Gohr    }
8203293305SAndreas Gohr    // die on errors
8303293305SAndreas Gohr    if ($data['status'] > 203) {
84cd98d9c3SGerry Weißbach        print $data['statusmessage'];
857fb7960fSChristopher Smith        if (defined('SIMPLE_TEST')) return;
86f62ea8a1Sandi        exit;
87f62ea8a1Sandi    }
88f62ea8a1Sandi}
8903293305SAndreas Gohr$evt->advise_after();
9003293305SAndreas Gohrunset($evt);
91f62ea8a1Sandi
9220bc86cfSAndreas Gohr//handle image resizing/cropping
93bfca0246SSam$evt = new Event('MEDIA_RESIZE', $data);
94bfca0246SSamif ($evt->advise_before()) {
958e9d8d55SAndreas Gohr    if (
968e9d8d55SAndreas Gohr        $MIME != 'image/svg+xml' &&
978e9d8d55SAndreas Gohr        (substr($MIME, 0, 5) == 'image') &&
988e9d8d55SAndreas Gohr        ($WIDTH || $HEIGHT)
998e9d8d55SAndreas Gohr    ) {
100793c31f2SChristopher Smith        if ($HEIGHT && $WIDTH) {
101cd98d9c3SGerry Weißbach            $data['file'] = $FILE = media_crop_image($data['file'], $EXT, $WIDTH, $HEIGHT);
10220bc86cfSAndreas Gohr        } else {
103cd98d9c3SGerry Weißbach            $data['file'] = $FILE = media_resize_image($data['file'], $EXT, $WIDTH, $HEIGHT);
104f62ea8a1Sandi        }
10520bc86cfSAndreas Gohr    }
106bfca0246SSam}
107bfca0246SSam$evt->advise_after();
108bfca0246SSamunset($evt);
109f62ea8a1Sandi
110e935fb4aSAndreas Gohr// finally send the file to the client
111e1d9dcc8SAndreas Gohr$evt = new Event('MEDIA_SENDFILE', $data);
112b80bedd6SAndreas Gohrif ($evt->advise_before()) {
1136cda96e3SAndreas Gohr    sendFile(
1146cda96e3SAndreas Gohr        $data['file'],
1156cda96e3SAndreas Gohr        $data['mime'],
1166cda96e3SAndreas Gohr        $data['download'],
1176cda96e3SAndreas Gohr        $data['cache'],
1186cda96e3SAndreas Gohr        $data['ispublic'],
1196cda96e3SAndreas Gohr        $data['orig'],
1206cda96e3SAndreas Gohr        $data['csp']
1216cda96e3SAndreas Gohr    );
122b80bedd6SAndreas Gohr}
123cd98d9c3SGerry Weißbach// Do something after the download finished.
124add8678fSAndreas Gohr$evt->advise_after();  // will not be emitted on 304 or x-sendfile
125f62ea8a1Sandi
1267fb7960fSChristopher Smith// END DO main
127