xref: /plugin/ditaa/action.php (revision 1682b6d1459cafef32578df8f416046b55e225c1)
138c92790SGerry Weißbach<?php
238c92790SGerry Weißbach/**
3*1682b6d1SGerry Weißbach * Ditaa Plugin - Action Component
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
1435cad792SAndreas Gohr/**
1535cad792SAndreas Gohr * Class action_plugin_ditaa
1635cad792SAndreas Gohr */
1738c92790SGerry Weißbachclass action_plugin_ditaa extends DokuWiki_Action_Plugin {
1838c92790SGerry Weißbach
1935cad792SAndreas Gohr    /**
2035cad792SAndreas Gohr     * Registers a callback function for a given event
2135cad792SAndreas Gohr     *
2235cad792SAndreas Gohr     * @param Doku_Event_Handler $controller
2335cad792SAndreas Gohr     */
2438c92790SGerry Weißbach    public function register(Doku_Event_Handler $controller) {
2538c92790SGerry Weißbach        // Download of a file
2638c92790SGerry Weißbach
2738c92790SGerry Weißbach        $controller->register_hook('MEDIA_SENDFILE', 'BEFORE', $this, 'ditaa_sendfile');
2838c92790SGerry Weißbach        $controller->register_hook('FETCH_MEDIA_STATUS', 'BEFORE', $this, 'ditaa_sendfile_not_found');
2938c92790SGerry Weißbach    }
3038c92790SGerry Weißbach
3135cad792SAndreas Gohr    /**
3235cad792SAndreas Gohr     * Handle Ditaa file requests
3335cad792SAndreas Gohr     *
3435cad792SAndreas Gohr     * @param Doku_Event $event
3535cad792SAndreas Gohr     * @param $args
3638c92790SGerry Weißbach     */
3735cad792SAndreas Gohr    public function ditaa_sendfile(Doku_Event $event, $args) {
3838c92790SGerry Weißbach        global $conf;
3935cad792SAndreas Gohr        global $INPUT;
4038c92790SGerry Weißbach
4135cad792SAndreas Gohr        if(!$INPUT->str('ditaa')) return;
4238c92790SGerry Weißbach
4335cad792SAndreas Gohr        /** @var syntax_plugin_ditaa $plugin */
4438c92790SGerry Weißbach        $plugin = plugin_load('syntax', 'ditaa');
4535cad792SAndreas Gohr
4635cad792SAndreas Gohr        $event->data['file'] = $plugin->_imgfile($INPUT->str('ditaa'));
4738c92790SGerry Weißbach        $event->data['mime'] = 'image/png';
4835cad792SAndreas Gohr        $event->data['download'] = false;
4938c92790SGerry Weißbach
5038c92790SGerry Weißbach        if(!$event->data['file']) {
5138c92790SGerry Weißbach            $event->data['file'] = dirname(__FILE__) . '/broken.png';
5238c92790SGerry Weißbach            $event->data['status'] = 404;
5338c92790SGerry Weißbach            $event->data['statusmessage'] = 'Not Found';
5438c92790SGerry Weißbach        }
5538c92790SGerry Weißbach
5638c92790SGerry Weißbach        header('Expires: ' . gmdate("D, d M Y H:i:s", time() + max($conf['cachetime'], 3600)) . ' GMT');
5738c92790SGerry Weißbach        header('Cache-Control: public, proxy-revalidate, no-transform, max-age=' . max($conf['cachetime'], 3600));
5838c92790SGerry Weißbach        header('Pragma: public');
5938c92790SGerry Weißbach    }
6038c92790SGerry Weißbach
6135cad792SAndreas Gohr    /**
6238c92790SGerry Weißbach     * If a file has not been found yet, we should try to check if this can be solved
6338c92790SGerry Weißbach     * via the ditaa renderer
6435cad792SAndreas Gohr     *
6535cad792SAndreas Gohr     * @param Doku_Event $event
6635cad792SAndreas Gohr     * @param $args
6735cad792SAndreas Gohr     * @return bool
6838c92790SGerry Weißbach     */
6935cad792SAndreas Gohr    public function ditaa_sendfile_not_found(Doku_Event $event, $args) {
7035cad792SAndreas Gohr        global $INPUT;
7135cad792SAndreas Gohr        if($event->data['status'] >= 500 || !$INPUT->str('ditaa')) {
7235cad792SAndreas Gohr            return true;
7335cad792SAndreas Gohr        }
7438c92790SGerry Weißbach        $event->data['status'] = 200;
7538c92790SGerry Weißbach        $event->data['statusmessage'] = 'OK';
7638c92790SGerry Weißbach        return true;
7738c92790SGerry Weißbach    }
7838c92790SGerry Weißbach}
79