xref: /plugin/siteexport/action/startup.php (revision a8c17ab5b37308343f86651acb8c4a1b3f36f0ae)
1<?php
2/**
3 * Site Export 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')) define('DOKU_INC', /** @scrutinizer ignore-type */ realpath(dirname(__FILE__) . '/../../') . '/');
12if (!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN', DOKU_INC . 'lib/plugins/');
13require_once(DOKU_PLUGIN . 'action.php');
14
15class action_plugin_siteexport_startup extends DokuWiki_Action_Plugin {
16
17    /**
18     * Register Plugin in DW
19     **/
20    public function register(Doku_Event_Handler $controller) {
21        $controller->register_hook('INIT_LANG_LOAD', 'BEFORE', $this, 'siteexport_check_template');
22        $controller->register_hook('DOKUWIKI_STARTED', 'AFTER', $this, 'siteexport_check_template');
23        $controller->register_hook('ACTION_ACT_PREPROCESS', 'BEFORE', $this, 'siteexport_check_export');
24        $controller->register_hook('TEMPLATE_PAGETOOLS_DISPLAY', 'BEFORE', $this, 'siteexport_add_page_export');
25        $controller->register_hook('TPL_ACT_UNKNOWN', 'BEFORE',  $this, 'siteexport_addpage');
26        $controller->register_hook('TPL_METAHEADER_OUTPUT', 'BEFORE', $this, 'siteexport_metaheaders');
27        $controller->register_hook('JS_CACHE_USE', 'BEFORE', $this, 'siteexport_check_js_cache');
28        $controller->register_hook('TOOLBAR_DEFINE', 'AFTER', $this, 'siteexport_toolbar_define');
29    }
30
31    private function hasSiteexportHeaders() {
32        $headers = function_exists('getallheaders') ? getallheaders() : null;
33        return is_array($headers) && array_key_exists('X-Site-Exporter', $headers);
34    }
35
36    /**
37     * Check for Template changes
38     **/
39    public function siteexport_check_template()
40    {
41        global $conf, $INFO;
42
43        if ( $this->hasSiteexportHeaders() || defined('SITEEXPORT_TPL') ) {
44            // This is a request via the HTTPProxy of the SiteExporter ... set config to what we need here.
45            $conf['useslash'] = 1;
46        }
47
48        if ( !defined('SITEEXPORT_TPL') ) { return; }
49        $conf['template'] = SITEEXPORT_TPL;
50    }
51
52    /**
53     * Check for Template changes in JS
54     **/
55    public function siteexport_check_js_cache(Doku_Event &$event)
56    {
57        global $conf, $INFO;
58
59        if ( !defined('SITEEXPORT_TPL') ) { return; }
60        $event->data->key .= SITEEXPORT_TPL;
61        $event->data->cache = getCacheName($event->data->key,$event->data->ext);
62    }
63
64    public function siteexport_check_export(Doku_Event &$event)
65    {
66        global $conf;
67        $keys = is_array($event->data) ? array_keys($event->data) : null;
68        $command = is_array($keys) ? array_shift($keys) : $event->data;
69        if ( $command == 'export_siteexport_pdf')
70        {
71            $event->data = 'show';
72            $conf['renderer_xhtml'] = 'siteexport_pdf';
73        }
74
75        if ( $command == 'siteexport_addpage' && $this->__executeCommand() )
76        {
77            $event->preventDefault();
78        }
79    }
80
81    public function siteexport_addpage(Doku_Event &$event)
82    {
83        if ( $event->data != 'siteexport_addpage' || ! $this->__executeCommand() ) { return; }
84        if ( ! $functions=& plugin_load('helper', 'siteexport') ) {
85            msg("Can't initialize");
86            return false;
87        }
88
89        $functions->__siteexport_addpage();
90        $event->preventDefault();
91    }
92
93    public function siteexport_add_page_export(Doku_Event &$event)
94    {
95        global $ID;
96
97        if ( $this->__executeCommand() ) {
98            $event->data['items'][] = '<li>' . tpl_link(wl($ID, array('do' => 'siteexport_addpage')), '<span>Export Page</span>',
99                                                'class="action siteexport_addpage" title="Export Page (Siteexport)"', 1) . '</li>';
100
101            require_once(DOKU_PLUGIN . 'siteexport/inc/functions.php');
102            $functions = new siteexport_functions();
103
104            $check = array();
105            $mapIDs = $functions->getMapID($ID, null, $check);
106            $mapID = array_shift($mapIDs);
107            if ( !empty($mapID) ) {
108                $event->data['items'][] = '<li>' . tpl_link('', '<span>Copy Map-ID: <span class="mapID" data-done="Done.">'.$mapID.'</span></span>',
109                                                   'class="action siteexport_mapid" title="Show Map-ID"" data-mapid="'.$mapID.'" onclick="copyMapIDToClipBoard.call(this); return false;"', 1) . '</li>';
110            }
111        }
112    }
113
114    public function siteexport_metaheaders(Doku_Event &$event)
115    {
116        global $conf;
117        $template = defined('SITEEXPORT_TPL') ? SITEEXPORT_TPL : $conf['template'];
118
119        $head =& $event->data;
120
121        foreach( $head['script'] as &$script ) {
122            if ( !empty($script['src']) && strstr($script['src'], 'js.php') ) {
123                $script['src'] .= '&template=' . $template;
124            }
125        }
126
127        return true;
128    }
129
130    public function siteexport_toolbar_define(Doku_Event &$event) {
131
132        if ( $this->hasSiteexportHeaders() ) {
133            // Remove Toolbar
134            // This is pr 5.4 syntax.
135            $event->data = array();
136        }
137    }
138
139    private function __executeCommand() {
140        return ($this->getConf('allowallusers') || auth_isadmin() || auth_ismanager());
141    }
142}
143