xref: /dokuwiki/lib/exe/fetch.php (revision 28cac1091e0363924084330e38c0a35e1cdc06b6)
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
9if(!defined('DOKU_INC')) define('DOKU_INC', dirname(__FILE__).'/../../');
10if (!defined('DOKU_DISABLE_GZIP_OUTPUT')) define('DOKU_DISABLE_GZIP_OUTPUT', 1);
11require_once(DOKU_INC.'inc/init.php');
12session_write_close(); //close session
13
14require_once(DOKU_INC.'inc/fetch.functions.php');
15
16if (defined('SIMPLE_TEST')) {
17    $INPUT = new Input();
18}
19
20// BEGIN main
21    $mimetypes = getMimeTypes();
22
23    //get input
24    $MEDIA  = stripctl(getID('media', false)); // no cleaning except control chars - maybe external
25    $CACHE  = calc_cache($INPUT->str('cache'));
26    $WIDTH  = $INPUT->int('w');
27    $HEIGHT = $INPUT->int('h');
28    $REV    = & $INPUT->ref('rev');
29    //sanitize revision
30    $REV = preg_replace('/[^0-9]/', '', $REV);
31
32    list($EXT, $MIME, $DL) = mimetype($MEDIA, false);
33    if($EXT === false) {
34        $EXT  = 'unknown';
35        $MIME = 'application/octet-stream';
36        $DL   = true;
37    }
38
39    // check for permissions, preconditions and cache external files
40    list($STATUS, $STATUSMESSAGE) = checkFileStatus($MEDIA, $FILE, $REV, $WIDTH, $HEIGHT);
41
42    // prepare data for plugin events
43    $data = array(
44        'media'         => $MEDIA,
45        'file'          => $FILE,
46        'orig'          => $FILE,
47        'mime'          => $MIME,
48        'download'      => $DL,
49        'cache'         => $CACHE,
50        'ext'           => $EXT,
51        'width'         => $WIDTH,
52        'height'        => $HEIGHT,
53        'status'        => $STATUS,
54        'statusmessage' => $STATUSMESSAGE,
55        'ispublic'      => media_ispublic($MEDIA),
56    );
57
58    // handle the file status
59    $evt = new Doku_Event('FETCH_MEDIA_STATUS', $data);
60    if($evt->advise_before()) {
61        // redirects
62        if($data['status'] > 300 && $data['status'] <= 304) {
63            send_redirect($data['statusmessage']);
64        }
65        // send any non 200 status
66        if($data['status'] != 200) {
67            http_status($data['status'], $data['statusmessage']);
68        }
69        // die on errors
70        if($data['status'] > 203) {
71            print $data['statusmessage'];
72            if (defined('SIMPLE_TEST')) return;
73            exit;
74        }
75    }
76    $evt->advise_after();
77    unset($evt);
78
79    //handle image resizing/cropping
80    if((substr($MIME, 0, 5) == 'image') && $WIDTH) {
81        if($HEIGHT) {
82            $data['file'] = $FILE = media_crop_image($data['file'], $EXT, $WIDTH, $HEIGHT);
83        } else {
84            $data['file'] = $FILE = media_resize_image($data['file'], $EXT, $WIDTH, $HEIGHT);
85        }
86    }
87
88    // finally send the file to the client
89    $evt = new Doku_Event('MEDIA_SENDFILE', $data);
90    if($evt->advise_before()) {
91        sendFile($data['file'], $data['mime'], $data['download'], $data['cache'], $data['ispublic']);
92    }
93    // Do something after the download finished.
94    $evt->advise_after();  // will not be emitted on 304 or x-sendfile
95
96// END DO main
97
98//Setup VIM: ex: et ts=2 :
99