xref: /plugin/siteexport/action/startup.php (revision 259f68e8ec1d9a938f5110ac378af63a6c859318)
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
44        if ( $this->hasSiteexportHeaders() || defined('SITEEXPORT_TPL') ) {
45            // This is a request via the HTTPProxy of the SiteExporter ... set config to what we need here.
46            $conf['useslash'] = 1;
47        }
48
49        if ( !defined('SITEEXPORT_TPL') ) { return; }
50        $conf['template'] = SITEEXPORT_TPL;
51    }
52
53    /**
54     * Check for Template changes in JS
55     **/
56    function siteexport_check_js_cache(&$event)
57    {
58        global $conf, $INFO;
59
60        if ( !defined('SITEEXPORT_TPL') ) { return; }
61        $event->data->key .= SITEEXPORT_TPL;
62        $event->data->cache = getCacheName($event->data->key,$event->data->ext);
63    }
64
65    function siteexport_check_export(&$event)
66    {
67        global $conf;
68        $command = is_array($event->data) ? array_shift(array_keys($event->data)) : $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->getConf('allowallusers') || auth_isadmin() || auth_ismanager() ) )
76        {
77            $event->preventDefault();
78        }
79    }
80
81    function siteexport_addpage(&$event)
82    {
83        if ( $event->data != 'siteexport_addpage' || ! ($this->getConf('allowallusers') || auth_isadmin() || auth_ismanager()) ) { 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    function siteexport_add_page_export(&$event)
94    {
95        global $ID;
96
97        if ( ($this->getConf('allowallusers') || auth_isadmin() || auth_ismanager()) ) {
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            $mapID = array_shift($functions->getMapID($ID, null, $check));
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