xref: /dokuwiki/lib/exe/fetch.php (revision a71ffec876bcdc3474b5b832ebf041a940eedf2a)
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            if (defined('SIMPLE_TEST')) return; //TestResponse doesn't recognize redirects
64            send_redirect($data['statusmessage']);
65        }
66        // send any non 200 status
67        if($data['status'] != 200) {
68            http_status($data['status'], $data['statusmessage']);
69        }
70        // die on errors
71        if($data['status'] > 203) {
72            print $data['statusmessage'];
73            if (defined('SIMPLE_TEST')) return;
74            exit;
75        }
76    }
77    $evt->advise_after();
78    unset($evt);
79
80    //handle image resizing/cropping
81    if((substr($MIME, 0, 5) == 'image') && $WIDTH) {
82        if($HEIGHT) {
83            $data['file'] = $FILE = media_crop_image($data['file'], $EXT, $WIDTH, $HEIGHT);
84        } else {
85            $data['file'] = $FILE = media_resize_image($data['file'], $EXT, $WIDTH, $HEIGHT);
86        }
87    }
88
89    // finally send the file to the client
90    $evt = new Doku_Event('MEDIA_SENDFILE', $data);
91    if($evt->advise_before()) {
92        sendFile($data['file'], $data['mime'], $data['download'], $data['cache'], $data['ispublic']);
93    }
94    // Do something after the download finished.
95    $evt->advise_after();  // will not be emitted on 304 or x-sendfile
96
97// END DO main
98
99//Setup VIM: ex: et ts=2 :
100