xref: /dokuwiki/lib/exe/fetch.php (revision e1d9dcc8b460b6f029ac9c8d5d3b8d23b6e73402)
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
9*e1d9dcc8SAndreas Gohruse dokuwiki\Extension\Event;
10*e1d9dcc8SAndreas Gohr
11d0a27cb0SAndreas Gohrif(!defined('DOKU_INC')) define('DOKU_INC', dirname(__FILE__).'/../../');
127fb7960fSChristopher Smithif (!defined('DOKU_DISABLE_GZIP_OUTPUT')) define('DOKU_DISABLE_GZIP_OUTPUT', 1);
13f62ea8a1Sandirequire_once(DOKU_INC.'inc/init.php');
1436625b96SAndreas Gohrsession_write_close(); //close session
158746e727Sandi
167fb7960fSChristopher Smithrequire_once(DOKU_INC.'inc/fetch.functions.php');
177fb7960fSChristopher Smith
187fb7960fSChristopher Smithif (defined('SIMPLE_TEST')) {
19ccc4c71cSAndreas Gohr    $INPUT = new \dokuwiki\Input\Input();
207fb7960fSChristopher Smith}
217fb7960fSChristopher Smith
227fb7960fSChristopher Smith// BEGIN main
23f62ea8a1Sandi    $mimetypes = getMimeTypes();
24f62ea8a1Sandi
25f62ea8a1Sandi    //get input
2602b0b681SAndreas Gohr    $MEDIA  = stripctl(getID('media', false)); // no cleaning except control chars - maybe external
27bfd0f597STom N Harris    $CACHE  = calc_cache($INPUT->str('cache'));
28bfd0f597STom N Harris    $WIDTH  = $INPUT->int('w');
29bfd0f597STom N Harris    $HEIGHT = $INPUT->int('h');
30bfd0f597STom N Harris    $REV    = & $INPUT->ref('rev');
31fc4aefb9SKate Arzamastseva    //sanitize revision
32fc4aefb9SKate Arzamastseva    $REV = preg_replace('/[^0-9]/', '', $REV);
33fc4aefb9SKate Arzamastseva
3427bf7924STom N Harris    list($EXT, $MIME, $DL) = mimetype($MEDIA, false);
35f62ea8a1Sandi    if($EXT === false) {
36f62ea8a1Sandi        $EXT  = 'unknown';
37f62ea8a1Sandi        $MIME = 'application/octet-stream';
38ecebf3a8SAndreas Gohr        $DL   = true;
39f62ea8a1Sandi    }
40f62ea8a1Sandi
4103293305SAndreas Gohr    // check for permissions, preconditions and cache external files
420f4e0092SChristopher Smith    list($STATUS, $STATUSMESSAGE) = checkFileStatus($MEDIA, $FILE, $REV, $WIDTH, $HEIGHT);
4303293305SAndreas Gohr
4403293305SAndreas Gohr    // prepare data for plugin events
453b399a1bSAndreas Gohr    $data = array(
463b399a1bSAndreas Gohr        'media'         => $MEDIA,
47cd98d9c3SGerry Weißbach        'file'          => $FILE,
48cd98d9c3SGerry Weißbach        'orig'          => $FILE,
49cd98d9c3SGerry Weißbach        'mime'          => $MIME,
50cd98d9c3SGerry Weißbach        'download'      => $DL,
51cd98d9c3SGerry Weißbach        'cache'         => $CACHE,
52cd98d9c3SGerry Weißbach        'ext'           => $EXT,
53cd98d9c3SGerry Weißbach        'width'         => $WIDTH,
54cd98d9c3SGerry Weißbach        'height'        => $HEIGHT,
55cd98d9c3SGerry Weißbach        'status'        => $STATUS,
56cd98d9c3SGerry Weißbach        'statusmessage' => $STATUSMESSAGE,
57add8678fSAndreas Gohr        'ispublic'      => media_ispublic($MEDIA),
58cd98d9c3SGerry Weißbach    );
59f62ea8a1Sandi
6003293305SAndreas Gohr    // handle the file status
61*e1d9dcc8SAndreas Gohr    $evt = new Event('FETCH_MEDIA_STATUS', $data);
62cd98d9c3SGerry Weißbach    if($evt->advise_before()) {
6303293305SAndreas Gohr        // redirects
6403293305SAndreas Gohr        if($data['status'] > 300 && $data['status'] <= 304) {
65d572baf8SKlap-in            if (defined('SIMPLE_TEST')) return; //TestResponse doesn't recognize redirects
6603293305SAndreas Gohr            send_redirect($data['statusmessage']);
6703293305SAndreas Gohr        }
6803293305SAndreas Gohr        // send any non 200 status
6903293305SAndreas Gohr        if($data['status'] != 200) {
709d2e1be6SAndreas Gohr            http_status($data['status'], $data['statusmessage']);
7103293305SAndreas Gohr        }
7203293305SAndreas Gohr        // die on errors
7303293305SAndreas Gohr        if($data['status'] > 203) {
74cd98d9c3SGerry Weißbach            print $data['statusmessage'];
757fb7960fSChristopher Smith            if (defined('SIMPLE_TEST')) return;
76f62ea8a1Sandi            exit;
77f62ea8a1Sandi        }
78f62ea8a1Sandi    }
7903293305SAndreas Gohr    $evt->advise_after();
8003293305SAndreas Gohr    unset($evt);
81f62ea8a1Sandi
8220bc86cfSAndreas Gohr    //handle image resizing/cropping
8377450f40Slisps    if((substr($MIME, 0, 5) == 'image') && ($WIDTH || $HEIGHT)) {
84793c31f2SChristopher Smith        if($HEIGHT && $WIDTH) {
85cd98d9c3SGerry Weißbach            $data['file'] = $FILE = media_crop_image($data['file'], $EXT, $WIDTH, $HEIGHT);
8620bc86cfSAndreas Gohr        } else {
87cd98d9c3SGerry Weißbach            $data['file'] = $FILE = media_resize_image($data['file'], $EXT, $WIDTH, $HEIGHT);
88f62ea8a1Sandi        }
8920bc86cfSAndreas Gohr    }
90f62ea8a1Sandi
91e935fb4aSAndreas Gohr    // finally send the file to the client
92*e1d9dcc8SAndreas Gohr    $evt = new Event('MEDIA_SENDFILE', $data);
93b80bedd6SAndreas Gohr    if($evt->advise_before()) {
94a0e46368SGerry Weißbach        sendFile($data['file'], $data['mime'], $data['download'], $data['cache'], $data['ispublic'], $data['orig']);
95b80bedd6SAndreas Gohr    }
96cd98d9c3SGerry Weißbach    // Do something after the download finished.
97add8678fSAndreas Gohr    $evt->advise_after();  // will not be emitted on 304 or x-sendfile
98f62ea8a1Sandi
997fb7960fSChristopher Smith// END DO main
100f62ea8a1Sandi
101e3776c06SMichael Hamann//Setup VIM: ex: et ts=2 :
102