xref: /plugin/siteexport/action/startup.php (revision 85b8228e8219872f0277d89ec68b54b18ab07832)
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', 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) /*&& $headers['X-Site-Exporter'] = getSecurityToken()*/;
34    }
35
36    /**
37     * Check for Template changes
38     **/
39    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    function siteexport_check_js_cache(&$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    function siteexport_check_export(&$event)
65    {
66        global $conf;
67        $command = is_array($event->data) ? array_shift(array_keys($event->data)) : $event->data;
68        if ( $command == 'export_siteexport_pdf')
69        {
70            $event->data = 'show';
71            $conf['renderer_xhtml'] = 'siteexport_pdf';
72        }
73
74        if ( $command == 'siteexport_addpage' && ($this->getConf('allowallusers') || auth_isadmin() || auth_ismanager() ) )
75        {
76            $event->preventDefault();
77        }
78    }
79
80    function siteexport_addpage(&$event)
81    {
82        if ( $event->data != 'siteexport_addpage' || ! ($this->getConf('allowallusers') || auth_isadmin() || auth_ismanager()) ) { return; }
83        if ( ! $functions=& plugin_load('helper', 'siteexport') ) {
84            msg("Can't initialize");
85            return false;
86        }
87
88        $functions->__siteexport_addpage();
89        $event->preventDefault();
90    }
91
92    function siteexport_add_page_export(&$event)
93    {
94        global $ID;
95
96        if ( ($this->getConf('allowallusers') || auth_isadmin() || auth_ismanager()) ) {
97            $event->data['items'][] = '<li>' . tpl_link(wl($ID, array('do' => 'siteexport_addpage')), '<span>Export Page</span>',
98                                                'class="action siteexport_addpage" title="Export Page (Siteexport)"', 1) . '</li>';
99
100            require_once(DOKU_PLUGIN . 'siteexport/inc/functions.php');
101            $functions = new siteexport_functions();
102
103            $check = array();
104            $mapIDs = $functions->getMapID($ID, null, $check);
105            $mapID = array_shift($mapIDs);
106            if ( !empty($mapID) ) {
107                $event->data['items'][] = '<li>' . tpl_link('', '<span>Copy Map-ID: <span class="mapID" data-done="Done.">'.$mapID.'</span></span>',
108                                                   'class="action siteexport_mapid" title="Show Map-ID"" data-mapid="'.$mapID.'" onclick="copyMapIDToClipBoard.call(this); return false;"', 1) . '</li>';
109            }
110        }
111    }
112
113    function siteexport_metaheaders(&$event)
114    {
115        global $conf;
116        $template = defined('SITEEXPORT_TPL') ? SITEEXPORT_TPL : $conf['template'];
117
118        $head =& $event->data;
119
120        foreach( $head['script'] as &$script ) {
121            if ( !empty($script['src']) && strstr($script['src'], 'js.php') ) {
122                $script['src'] .= '&template=' . $template;
123            }
124        }
125
126        return true;
127    }
128
129    function siteexport_toolbar_define(&$event) {
130
131        if ( $this->hasSiteexportHeaders() ) {
132            // Remove Toolbar
133            // This is pr 5.4 syntax.
134            $event->data = array();
135        }
136    }
137}
138