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