xref: /plugin/siteexport/action/startup.php (revision 992a6e2222fe7d62ac362e6b9b9da54504e6fb46)
17d101cc1SGerry Weißbach<?php
27d101cc1SGerry Weißbach/**
37d101cc1SGerry Weißbach * Site Export Plugin
47d101cc1SGerry Weißbach *
57d101cc1SGerry Weißbach * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
67d101cc1SGerry Weißbach * @author     i-net software <tools@inetsoftware.de>
77d101cc1SGerry Weißbach * @author     Gerry Weissbach <gweissbach@inetsoftware.de>
87d101cc1SGerry Weißbach */
97d101cc1SGerry Weißbach
107d101cc1SGerry Weißbach// must be run within Dokuwiki
11a8c17ab5Si-net /// softwareif (!defined('DOKU_INC')) define('DOKU_INC', /** @scrutinizer ignore-type */ realpath(dirname(__FILE__) . '/../../') . '/');
127d101cc1SGerry Weißbachif (!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN', DOKU_INC . 'lib/plugins/');
137d101cc1SGerry Weißbachrequire_once(DOKU_PLUGIN . 'action.php');
147d101cc1SGerry Weißbach
157d101cc1SGerry Weißbachclass action_plugin_siteexport_startup extends DokuWiki_Action_Plugin {
167d101cc1SGerry Weißbach
177d101cc1SGerry Weißbach    /**
187d101cc1SGerry Weißbach     * Register Plugin in DW
197d101cc1SGerry Weißbach     **/
203f2e6413SGerry Weißbach    public function register(Doku_Event_Handler $controller) {
2116c5261cSGerry Weißbach        $controller->register_hook('INIT_LANG_LOAD', 'BEFORE', $this, 'siteexport_check_template');
227d101cc1SGerry Weißbach        $controller->register_hook('DOKUWIKI_STARTED', 'AFTER', $this, 'siteexport_check_template');
237d101cc1SGerry Weißbach        $controller->register_hook('ACTION_ACT_PREPROCESS', 'BEFORE', $this, 'siteexport_check_export');
2495c3174fSGerry Weißbach        $controller->register_hook('TPL_ACT_UNKNOWN', 'BEFORE',  $this, 'siteexport_addpage');
25b26434b0SGerry Weißbach        $controller->register_hook('TPL_METAHEADER_OUTPUT', 'BEFORE', $this, 'siteexport_metaheaders');
26bbafe1f0SGerry Weißbach        $controller->register_hook('JS_CACHE_USE', 'BEFORE', $this, 'siteexport_check_js_cache');
27*992a6e22Si-net /// software
28c7c6980aSGerry Weißbach        $controller->register_hook('TOOLBAR_DEFINE', 'AFTER', $this, 'siteexport_toolbar_define');
29*992a6e22Si-net /// software
30*992a6e22Si-net /// software        $controller->register_hook('TEMPLATE_PAGETOOLS_DISPLAY', 'BEFORE', $this, 'siteexport_add_page_export');
31*992a6e22Si-net /// software        $controller->register_hook('MENU_ITEMS_ASSEMBLY', 'AFTER', $this, 'siteexport_add_svg_page_export', array());
32c7c6980aSGerry Weißbach    }
33c7c6980aSGerry Weißbach
34c7c6980aSGerry Weißbach    private function hasSiteexportHeaders() {
35c7c6980aSGerry Weißbach        $headers = function_exists('getallheaders') ? getallheaders() : null;
36a8c17ab5Si-net /// software        return is_array($headers) && array_key_exists('X-Site-Exporter', $headers);
377d101cc1SGerry Weißbach    }
387d101cc1SGerry Weißbach
397d101cc1SGerry Weißbach    /**
407d101cc1SGerry Weißbach     * Check for Template changes
417d101cc1SGerry Weißbach     **/
42a8c17ab5Si-net /// software    public function siteexport_check_template()
437d101cc1SGerry Weißbach    {
447d101cc1SGerry Weißbach        global $conf, $INFO;
450cd3f1b4SGerry Weißbach
46c7c6980aSGerry Weißbach        if ( $this->hasSiteexportHeaders() || defined('SITEEXPORT_TPL') ) {
470cd3f1b4SGerry Weißbach            // This is a request via the HTTPProxy of the SiteExporter ... set config to what we need here.
480cd3f1b4SGerry Weißbach            $conf['useslash'] = 1;
490cd3f1b4SGerry Weißbach        }
507d101cc1SGerry Weißbach
517d101cc1SGerry Weißbach        if ( !defined('SITEEXPORT_TPL') ) { return; }
527d101cc1SGerry Weißbach        $conf['template'] = SITEEXPORT_TPL;
537d101cc1SGerry Weißbach    }
547d101cc1SGerry Weißbach
55bbafe1f0SGerry Weißbach    /**
56bbafe1f0SGerry Weißbach     * Check for Template changes in JS
57bbafe1f0SGerry Weißbach     **/
58a8c17ab5Si-net /// software    public function siteexport_check_js_cache(Doku_Event &$event)
59bbafe1f0SGerry Weißbach    {
60bbafe1f0SGerry Weißbach        global $conf, $INFO;
61bbafe1f0SGerry Weißbach
62bbafe1f0SGerry Weißbach        if ( !defined('SITEEXPORT_TPL') ) { return; }
63bbafe1f0SGerry Weißbach        $event->data->key .= SITEEXPORT_TPL;
64bbafe1f0SGerry Weißbach        $event->data->cache = getCacheName($event->data->key,$event->data->ext);
65bbafe1f0SGerry Weißbach    }
66bbafe1f0SGerry Weißbach
67a8c17ab5Si-net /// software    public function siteexport_check_export(Doku_Event &$event)
687d101cc1SGerry Weißbach    {
697d101cc1SGerry Weißbach        global $conf;
70a8c17ab5Si-net /// software        $keys = is_array($event->data) ? array_keys($event->data) : null;
71a8c17ab5Si-net /// software        $command = is_array($keys) ? array_shift($keys) : $event->data;
72fd385364SGerry Weißbach        if ( $command == 'export_siteexport_pdf')
737d101cc1SGerry Weißbach        {
747d101cc1SGerry Weißbach            $event->data = 'show';
757d101cc1SGerry Weißbach            $conf['renderer_xhtml'] = 'siteexport_pdf';
76fd385364SGerry Weißbach        }
77fd385364SGerry Weißbach
78a8c17ab5Si-net /// software        if ( $command == 'siteexport_addpage' && $this->__executeCommand() )
79fd385364SGerry Weißbach        {
8095c3174fSGerry Weißbach            $event->preventDefault();
8195c3174fSGerry Weißbach        }
8295c3174fSGerry Weißbach    }
8395c3174fSGerry Weißbach
84a8c17ab5Si-net /// software    public function siteexport_addpage(Doku_Event &$event)
8595c3174fSGerry Weißbach    {
86a8c17ab5Si-net /// software        if ( $event->data != 'siteexport_addpage' || ! $this->__executeCommand() ) { return; }
8795c3174fSGerry Weißbach        if ( ! $functions=& plugin_load('helper', 'siteexport') ) {
8895c3174fSGerry Weißbach            msg("Can't initialize");
8995c3174fSGerry Weißbach            return false;
9095c3174fSGerry Weißbach        }
9195c3174fSGerry Weißbach
9295c3174fSGerry Weißbach        $functions->__siteexport_addpage();
9395c3174fSGerry Weißbach        $event->preventDefault();
9495c3174fSGerry Weißbach    }
9595c3174fSGerry Weißbach
96a8c17ab5Si-net /// software    public function siteexport_add_page_export(Doku_Event &$event)
9795c3174fSGerry Weißbach    {
9895c3174fSGerry Weißbach        global $ID;
9995c3174fSGerry Weißbach
100a8c17ab5Si-net /// software        if ( $this->__executeCommand() ) {
10195c3174fSGerry Weißbach            $event->data['items'][] = '<li>' . tpl_link(wl($ID, array('do' => 'siteexport_addpage')), '<span>Export Page</span>',
102259f68e8SGerry Weißbach                                                'class="action siteexport_addpage" title="Export Page (Siteexport)"', 1) . '</li>';
103259f68e8SGerry Weißbach
104259f68e8SGerry Weißbach            require_once(DOKU_PLUGIN . 'siteexport/inc/functions.php');
105259f68e8SGerry Weißbach            $functions = new siteexport_functions();
106259f68e8SGerry Weißbach
107259f68e8SGerry Weißbach            $check = array();
10885b8228eSGerry Weißbach            $mapIDs = $functions->getMapID($ID, null, $check);
10985b8228eSGerry Weißbach            $mapID = array_shift($mapIDs);
110259f68e8SGerry Weißbach            if ( !empty($mapID) ) {
111259f68e8SGerry Weißbach                $event->data['items'][] = '<li>' . tpl_link('', '<span>Copy Map-ID: <span class="mapID" data-done="Done.">'.$mapID.'</span></span>',
112259f68e8SGerry Weißbach                                                   'class="action siteexport_mapid" title="Show Map-ID"" data-mapid="'.$mapID.'" onclick="copyMapIDToClipBoard.call(this); return false;"', 1) . '</li>';
113259f68e8SGerry Weißbach            }
1147d101cc1SGerry Weißbach        }
1157d101cc1SGerry Weißbach    }
116b26434b0SGerry Weißbach
117*992a6e22Si-net /// software    public function siteexport_add_svg_page_export(Doku_Event $event) {
118*992a6e22Si-net /// software       /* if this is not a page OR ckgedit/ckgedoku is not  active -> return */
119*992a6e22Si-net /// software      if($event->data['view'] != 'page') return;
120*992a6e22Si-net /// software       array_splice($event->data['items'], -1, 0, [new \dokuwiki\plugin\siteexport\MenuItem()]);
121*992a6e22Si-net /// software    }
122*992a6e22Si-net /// software
123a8c17ab5Si-net /// software    public function siteexport_metaheaders(Doku_Event &$event)
124b26434b0SGerry Weißbach    {
125fc3c50fbSGerry Weißbach        global $conf;
126fc3c50fbSGerry Weißbach        $template = defined('SITEEXPORT_TPL') ? SITEEXPORT_TPL : $conf['template'];
127b26434b0SGerry Weißbach
128b26434b0SGerry Weißbach        $head =& $event->data;
129b26434b0SGerry Weißbach
130b26434b0SGerry Weißbach        foreach( $head['script'] as &$script ) {
131b26434b0SGerry Weißbach            if ( !empty($script['src']) && strstr($script['src'], 'js.php') ) {
132fc3c50fbSGerry Weißbach                $script['src'] .= '&template=' . $template;
133b26434b0SGerry Weißbach            }
134b26434b0SGerry Weißbach        }
135b26434b0SGerry Weißbach
136b26434b0SGerry Weißbach        return true;
137b26434b0SGerry Weißbach    }
138c7c6980aSGerry Weißbach
139a8c17ab5Si-net /// software    public function siteexport_toolbar_define(Doku_Event &$event) {
140c7c6980aSGerry Weißbach
141c7c6980aSGerry Weißbach        if ( $this->hasSiteexportHeaders() ) {
142c7c6980aSGerry Weißbach            // Remove Toolbar
143bf4927b7SGerry Weißbach            // This is pr 5.4 syntax.
144bf4927b7SGerry Weißbach            $event->data = array();
145c7c6980aSGerry Weißbach        }
146c7c6980aSGerry Weißbach    }
147a8c17ab5Si-net /// software
148a8c17ab5Si-net /// software    private function __executeCommand() {
149a8c17ab5Si-net /// software        return ($this->getConf('allowallusers') || auth_isadmin() || auth_ismanager());
150a8c17ab5Si-net /// software    }
1517d101cc1SGerry Weißbach}
152