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