1<?php
2/**
3 * Siteexport SendFile Plugin
4 *
5 * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
6 * @author     i-net software <tools@inetsoftware.de>
7 * @author     Gerry Weissbach <gweissbach@inetsoftware.de>
8 */
9
10// must be run within Dokuwiki
11if(!defined('DOKU_INC')) die();
12if(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN', DOKU_INC . 'lib/plugins/');
13
14require_once(DOKU_PLUGIN . 'action.php');
15
16/**
17 * Class action_plugin_ditaa
18 */
19class action_plugin_ditaa extends DokuWiki_Action_Plugin {
20
21    /**
22     * Registers a callback function for a given event
23     *
24     * @param Doku_Event_Handler $controller
25     */
26    public function register(Doku_Event_Handler $controller) {
27        // Download of a file
28
29        $controller->register_hook('MEDIA_SENDFILE', 'BEFORE', $this, 'ditaa_sendfile');
30        $controller->register_hook('FETCH_MEDIA_STATUS', 'BEFORE', $this, 'ditaa_sendfile_not_found');
31    }
32
33    /**
34     * Handle Ditaa file requests
35     *
36     * @param Doku_Event $event
37     * @param $args
38     */
39    public function ditaa_sendfile(Doku_Event $event, $args) {
40        global $conf;
41        global $INPUT;
42
43        if(!$INPUT->str('ditaa')) return;
44
45        /** @var syntax_plugin_ditaa $plugin */
46        $plugin = plugin_load('syntax', 'ditaa');
47
48        $event->data['file'] = $plugin->_imgfile($INPUT->str('ditaa'));
49        $event->data['mime'] = 'image/png';
50        $event->data['download'] = false;
51
52        if(!$event->data['file']) {
53            $event->data['file'] = dirname(__FILE__) . '/broken.png';
54            $event->data['status'] = 404;
55            $event->data['statusmessage'] = 'Not Found';
56        }
57
58        header('Expires: ' . gmdate("D, d M Y H:i:s", time() + max($conf['cachetime'], 3600)) . ' GMT');
59        header('Cache-Control: public, proxy-revalidate, no-transform, max-age=' . max($conf['cachetime'], 3600));
60        header('Pragma: public');
61    }
62
63    /**
64     * If a file has not been found yet, we should try to check if this can be solved
65     * via the ditaa renderer
66     *
67     * @param Doku_Event $event
68     * @param $args
69     * @return bool
70     */
71    public function ditaa_sendfile_not_found(Doku_Event $event, $args) {
72        global $INPUT;
73        if($event->data['status'] >= 500 || !$INPUT->str('ditaa')) {
74            return true;
75        }
76        $event->data['status'] = 200;
77        $event->data['statusmessage'] = 'OK';
78        return true;
79    }
80}
81