xref: /plugin/ditaa/action.php (revision 35cad79268be8e2b877be727d6f84846ee2d123e)
138c92790SGerry Weißbach<?php
238c92790SGerry Weißbach/**
338c92790SGerry Weißbach * Siteexport SendFile Plugin
438c92790SGerry Weißbach *
538c92790SGerry Weißbach * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
638c92790SGerry Weißbach * @author     i-net software <tools@inetsoftware.de>
738c92790SGerry Weißbach * @author     Gerry Weissbach <gweissbach@inetsoftware.de>
838c92790SGerry Weißbach */
938c92790SGerry Weißbach
1038c92790SGerry Weißbach// must be run within Dokuwiki
1138c92790SGerry Weißbachif(!defined('DOKU_INC')) die();
1238c92790SGerry Weißbachif(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN', DOKU_INC . 'lib/plugins/');
1338c92790SGerry Weißbach
1438c92790SGerry Weißbachrequire_once(DOKU_PLUGIN . 'action.php');
1538c92790SGerry Weißbach
16*35cad792SAndreas Gohr/**
17*35cad792SAndreas Gohr * Class action_plugin_ditaa
18*35cad792SAndreas Gohr */
1938c92790SGerry Weißbachclass action_plugin_ditaa extends DokuWiki_Action_Plugin {
2038c92790SGerry Weißbach
21*35cad792SAndreas Gohr    /**
22*35cad792SAndreas Gohr     * Registers a callback function for a given event
23*35cad792SAndreas Gohr     *
24*35cad792SAndreas Gohr     * @param Doku_Event_Handler $controller
25*35cad792SAndreas Gohr     */
2638c92790SGerry Weißbach    public function register(Doku_Event_Handler $controller) {
2738c92790SGerry Weißbach        // Download of a file
2838c92790SGerry Weißbach
2938c92790SGerry Weißbach        $controller->register_hook('MEDIA_SENDFILE', 'BEFORE', $this, 'ditaa_sendfile');
3038c92790SGerry Weißbach        $controller->register_hook('FETCH_MEDIA_STATUS', 'BEFORE', $this, 'ditaa_sendfile_not_found');
3138c92790SGerry Weißbach    }
3238c92790SGerry Weißbach
33*35cad792SAndreas Gohr    /**
34*35cad792SAndreas Gohr     * Handle Ditaa file requests
35*35cad792SAndreas Gohr     *
36*35cad792SAndreas Gohr     * @param Doku_Event $event
37*35cad792SAndreas Gohr     * @param $args
3838c92790SGerry Weißbach     */
39*35cad792SAndreas Gohr    public function ditaa_sendfile(Doku_Event $event, $args) {
4038c92790SGerry Weißbach        global $conf;
41*35cad792SAndreas Gohr        global $INPUT;
4238c92790SGerry Weißbach
43*35cad792SAndreas Gohr        if(!$INPUT->str('ditaa')) return;
4438c92790SGerry Weißbach
45*35cad792SAndreas Gohr        /** @var syntax_plugin_ditaa $plugin */
4638c92790SGerry Weißbach        $plugin = plugin_load('syntax', 'ditaa');
47*35cad792SAndreas Gohr
48*35cad792SAndreas Gohr        $event->data['file'] = $plugin->_imgfile($INPUT->str('ditaa'));
4938c92790SGerry Weißbach        $event->data['mime'] = 'image/png';
50*35cad792SAndreas Gohr        $event->data['download'] = false;
5138c92790SGerry Weißbach
5238c92790SGerry Weißbach        if(!$event->data['file']) {
5338c92790SGerry Weißbach            $event->data['file'] = dirname(__FILE__) . '/broken.png';
5438c92790SGerry Weißbach            $event->data['status'] = 404;
5538c92790SGerry Weißbach            $event->data['statusmessage'] = 'Not Found';
5638c92790SGerry Weißbach        }
5738c92790SGerry Weißbach
5838c92790SGerry Weißbach        header('Expires: ' . gmdate("D, d M Y H:i:s", time() + max($conf['cachetime'], 3600)) . ' GMT');
5938c92790SGerry Weißbach        header('Cache-Control: public, proxy-revalidate, no-transform, max-age=' . max($conf['cachetime'], 3600));
6038c92790SGerry Weißbach        header('Pragma: public');
6138c92790SGerry Weißbach    }
6238c92790SGerry Weißbach
63*35cad792SAndreas Gohr    /**
6438c92790SGerry Weißbach     * If a file has not been found yet, we should try to check if this can be solved
6538c92790SGerry Weißbach     * via the ditaa renderer
66*35cad792SAndreas Gohr     *
67*35cad792SAndreas Gohr     * @param Doku_Event $event
68*35cad792SAndreas Gohr     * @param $args
69*35cad792SAndreas Gohr     * @return bool
7038c92790SGerry Weißbach     */
71*35cad792SAndreas Gohr    public function ditaa_sendfile_not_found(Doku_Event $event, $args) {
72*35cad792SAndreas Gohr        global $INPUT;
73*35cad792SAndreas Gohr        if($event->data['status'] >= 500 || !$INPUT->str('ditaa')) {
74*35cad792SAndreas Gohr            return true;
75*35cad792SAndreas Gohr        }
7638c92790SGerry Weißbach        $event->data['status'] = 200;
7738c92790SGerry Weißbach        $event->data['statusmessage'] = 'OK';
7838c92790SGerry Weißbach        return true;
7938c92790SGerry Weißbach    }
8038c92790SGerry Weißbach}
81