xref: /dokuwiki/lib/exe/fetch.php (revision ccc4c71ca88c25bcefb7f42eb01f0c040487e3a9)
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
9d0a27cb0SAndreas Gohrif(!defined('DOKU_INC')) define('DOKU_INC', dirname(__FILE__).'/../../');
107fb7960fSChristopher Smithif (!defined('DOKU_DISABLE_GZIP_OUTPUT')) define('DOKU_DISABLE_GZIP_OUTPUT', 1);
11f62ea8a1Sandirequire_once(DOKU_INC.'inc/init.php');
1236625b96SAndreas Gohrsession_write_close(); //close session
138746e727Sandi
147fb7960fSChristopher Smithrequire_once(DOKU_INC.'inc/fetch.functions.php');
157fb7960fSChristopher Smith
167fb7960fSChristopher Smithif (defined('SIMPLE_TEST')) {
17*ccc4c71cSAndreas Gohr    $INPUT = new \dokuwiki\Input\Input();
187fb7960fSChristopher Smith}
197fb7960fSChristopher Smith
207fb7960fSChristopher Smith// BEGIN main
21f62ea8a1Sandi    $mimetypes = getMimeTypes();
22f62ea8a1Sandi
23f62ea8a1Sandi    //get input
2402b0b681SAndreas Gohr    $MEDIA  = stripctl(getID('media', false)); // no cleaning except control chars - maybe external
25bfd0f597STom N Harris    $CACHE  = calc_cache($INPUT->str('cache'));
26bfd0f597STom N Harris    $WIDTH  = $INPUT->int('w');
27bfd0f597STom N Harris    $HEIGHT = $INPUT->int('h');
28bfd0f597STom N Harris    $REV    = & $INPUT->ref('rev');
29fc4aefb9SKate Arzamastseva    //sanitize revision
30fc4aefb9SKate Arzamastseva    $REV = preg_replace('/[^0-9]/', '', $REV);
31fc4aefb9SKate Arzamastseva
3227bf7924STom N Harris    list($EXT, $MIME, $DL) = mimetype($MEDIA, false);
33f62ea8a1Sandi    if($EXT === false) {
34f62ea8a1Sandi        $EXT  = 'unknown';
35f62ea8a1Sandi        $MIME = 'application/octet-stream';
36ecebf3a8SAndreas Gohr        $DL   = true;
37f62ea8a1Sandi    }
38f62ea8a1Sandi
3903293305SAndreas Gohr    // check for permissions, preconditions and cache external files
400f4e0092SChristopher Smith    list($STATUS, $STATUSMESSAGE) = checkFileStatus($MEDIA, $FILE, $REV, $WIDTH, $HEIGHT);
4103293305SAndreas Gohr
4203293305SAndreas Gohr    // prepare data for plugin events
433b399a1bSAndreas Gohr    $data = array(
443b399a1bSAndreas Gohr        'media'         => $MEDIA,
45cd98d9c3SGerry Weißbach        'file'          => $FILE,
46cd98d9c3SGerry Weißbach        'orig'          => $FILE,
47cd98d9c3SGerry Weißbach        'mime'          => $MIME,
48cd98d9c3SGerry Weißbach        'download'      => $DL,
49cd98d9c3SGerry Weißbach        'cache'         => $CACHE,
50cd98d9c3SGerry Weißbach        'ext'           => $EXT,
51cd98d9c3SGerry Weißbach        'width'         => $WIDTH,
52cd98d9c3SGerry Weißbach        'height'        => $HEIGHT,
53cd98d9c3SGerry Weißbach        'status'        => $STATUS,
54cd98d9c3SGerry Weißbach        'statusmessage' => $STATUSMESSAGE,
55add8678fSAndreas Gohr        'ispublic'      => media_ispublic($MEDIA),
56cd98d9c3SGerry Weißbach    );
57f62ea8a1Sandi
5803293305SAndreas Gohr    // handle the file status
5903293305SAndreas Gohr    $evt = new Doku_Event('FETCH_MEDIA_STATUS', $data);
60cd98d9c3SGerry Weißbach    if($evt->advise_before()) {
6103293305SAndreas Gohr        // redirects
6203293305SAndreas Gohr        if($data['status'] > 300 && $data['status'] <= 304) {
63d572baf8SKlap-in            if (defined('SIMPLE_TEST')) return; //TestResponse doesn't recognize redirects
6403293305SAndreas Gohr            send_redirect($data['statusmessage']);
6503293305SAndreas Gohr        }
6603293305SAndreas Gohr        // send any non 200 status
6703293305SAndreas Gohr        if($data['status'] != 200) {
689d2e1be6SAndreas Gohr            http_status($data['status'], $data['statusmessage']);
6903293305SAndreas Gohr        }
7003293305SAndreas Gohr        // die on errors
7103293305SAndreas Gohr        if($data['status'] > 203) {
72cd98d9c3SGerry Weißbach            print $data['statusmessage'];
737fb7960fSChristopher Smith            if (defined('SIMPLE_TEST')) return;
74f62ea8a1Sandi            exit;
75f62ea8a1Sandi        }
76f62ea8a1Sandi    }
7703293305SAndreas Gohr    $evt->advise_after();
7803293305SAndreas Gohr    unset($evt);
79f62ea8a1Sandi
8020bc86cfSAndreas Gohr    //handle image resizing/cropping
8177450f40Slisps    if((substr($MIME, 0, 5) == 'image') && ($WIDTH || $HEIGHT)) {
82793c31f2SChristopher Smith        if($HEIGHT && $WIDTH) {
83cd98d9c3SGerry Weißbach            $data['file'] = $FILE = media_crop_image($data['file'], $EXT, $WIDTH, $HEIGHT);
8420bc86cfSAndreas Gohr        } else {
85cd98d9c3SGerry Weißbach            $data['file'] = $FILE = media_resize_image($data['file'], $EXT, $WIDTH, $HEIGHT);
86f62ea8a1Sandi        }
8720bc86cfSAndreas Gohr    }
88f62ea8a1Sandi
89e935fb4aSAndreas Gohr    // finally send the file to the client
90b80bedd6SAndreas Gohr    $evt = new Doku_Event('MEDIA_SENDFILE', $data);
91b80bedd6SAndreas Gohr    if($evt->advise_before()) {
92a0e46368SGerry Weißbach        sendFile($data['file'], $data['mime'], $data['download'], $data['cache'], $data['ispublic'], $data['orig']);
93b80bedd6SAndreas Gohr    }
94cd98d9c3SGerry Weißbach    // Do something after the download finished.
95add8678fSAndreas Gohr    $evt->advise_after();  // will not be emitted on 304 or x-sendfile
96f62ea8a1Sandi
977fb7960fSChristopher Smith// END DO main
98f62ea8a1Sandi
99e3776c06SMichael Hamann//Setup VIM: ex: et ts=2 :
100