xref: /dokuwiki/lib/exe/fetch.php (revision 7fb7960f92047a9bcadf9d497ae79615979e9a6d)
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__).'/../../');
10*7fb7960fSChristopher 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
14*7fb7960fSChristopher Smithrequire_once(DOKU_INC.'inc/fetch.functions.php');
15*7fb7960fSChristopher Smith
16*7fb7960fSChristopher Smithif (defined('SIMPLE_TEST')) {
17*7fb7960fSChristopher Smith    $INPUT = new Input();
18*7fb7960fSChristopher Smith}
19*7fb7960fSChristopher Smith
20*7fb7960fSChristopher 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) {
6303293305SAndreas Gohr            send_redirect($data['statusmessage']);
6403293305SAndreas Gohr        }
6503293305SAndreas Gohr        // send any non 200 status
6603293305SAndreas Gohr        if($data['status'] != 200) {
679d2e1be6SAndreas Gohr            http_status($data['status'], $data['statusmessage']);
6803293305SAndreas Gohr        }
6903293305SAndreas Gohr        // die on errors
7003293305SAndreas Gohr        if($data['status'] > 203) {
71cd98d9c3SGerry Weißbach            print $data['statusmessage'];
72*7fb7960fSChristopher Smith            if (defined('SIMPLE_TEST')) return;
73f62ea8a1Sandi            exit;
74f62ea8a1Sandi        }
75f62ea8a1Sandi    }
7603293305SAndreas Gohr    $evt->advise_after();
7703293305SAndreas Gohr    unset($evt);
78f62ea8a1Sandi
7920bc86cfSAndreas Gohr    //handle image resizing/cropping
80f62ea8a1Sandi    if((substr($MIME, 0, 5) == 'image') && $WIDTH) {
81d52a56e1SAndreas Gohr        if($HEIGHT) {
82cd98d9c3SGerry Weißbach            $data['file'] = $FILE = media_crop_image($data['file'], $EXT, $WIDTH, $HEIGHT);
8320bc86cfSAndreas Gohr        } else {
84cd98d9c3SGerry Weißbach            $data['file'] = $FILE = media_resize_image($data['file'], $EXT, $WIDTH, $HEIGHT);
85f62ea8a1Sandi        }
8620bc86cfSAndreas Gohr    }
87f62ea8a1Sandi
88e935fb4aSAndreas Gohr    // finally send the file to the client
89b80bedd6SAndreas Gohr    $evt = new Doku_Event('MEDIA_SENDFILE', $data);
90b80bedd6SAndreas Gohr    if($evt->advise_before()) {
911c7d84beSAndreas Gohr        sendFile($data['file'], $data['mime'], $data['download'], $data['cache'], $data['ispublic']);
92b80bedd6SAndreas Gohr    }
93cd98d9c3SGerry Weißbach    // Do something after the download finished.
94add8678fSAndreas Gohr    $evt->advise_after();  // will not be emitted on 304 or x-sendfile
95f62ea8a1Sandi
96*7fb7960fSChristopher Smith// END DO main
97f62ea8a1Sandi
98e3776c06SMichael Hamann//Setup VIM: ex: et ts=2 :
99