xref: /plugin/ditaa/action.php (revision 38c9279020ea3c3abc0a18e7d236f87252b3f01b)
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
16class action_plugin_ditaa extends DokuWiki_Action_Plugin {
17
18    public function register(Doku_Event_Handler $controller) {
19        // Download of a file
20
21        $controller->register_hook('MEDIA_SENDFILE', 'BEFORE', $this, 'ditaa_sendfile');
22        $controller->register_hook('FETCH_MEDIA_STATUS', 'BEFORE', $this, 'ditaa_sendfile_not_found');
23    }
24
25    /*
26     * Redirect File to real File
27     */
28    function ditaa_sendfile(&$event, $args) {
29        global $conf;
30
31        if ( empty( $_REQUEST['ditaa'] ) ) {
32            return;
33        }
34
35        $plugin = plugin_load( 'syntax', 'ditaa' );
36        $data = p_get_metadata( $event->data['media'], 'ditaa' );
37        $event->data['file'] = $plugin->_imgfile($event->data['media'], $data[$_REQUEST['ditaa']]);
38        $event->data['mime'] = 'image/png';
39
40        if( !$event->data['file'] ) {
41            $event->data['file'] = dirname(__FILE__) . '/broken.png';
42            $event->data['status'] = 404;
43            $event->data['statusmessage'] = 'Not Found';
44        }
45
46        header('Expires: '.gmdate("D, d M Y H:i:s", time()+max($conf['cachetime'], 3600)).' GMT');
47        header('Cache-Control: public, proxy-revalidate, no-transform, max-age='.max($conf['cachetime'], 3600));
48        header('Pragma: public');
49    }
50
51    /*
52     * If a file has not been found yet, we should try to check if this can be solved
53     * via the ditaa renderer
54     */
55    function ditaa_sendfile_not_found(&$event, $args)
56    {
57        if ( $event->data['status'] >= 500 || empty( $_REQUEST['ditaa'] ) ) { return true; }
58        $event->data['status'] = 200;
59        $event->data['statusmessage'] = 'OK';
60        return true;
61    }
62}