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