xref: /plugin/siteexport/action/startup.php (revision c7c6980aaf4892babd5bf9ddf9e27f98645b6927)
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="Add page"', 1) . '</li>';
100		}
101	}
102
103	function siteexport_metaheaders(&$event)
104	{
105		global $conf;
106		$template = defined('SITEEXPORT_TPL') ? SITEEXPORT_TPL : $conf['template'];
107
108		$head =& $event->data;
109
110		foreach( $head['script'] as &$script ) {
111			if ( !empty($script['src']) && strstr($script['src'], 'js.php') ) {
112				$script['src'] .= '&template=' . $template;
113			}
114		}
115
116		return true;
117	}
118
119	function siteexport_toolbar_define(&$event) {
120
121    	if ( $this->hasSiteexportHeaders() ) {
122        	// Remove Toolbar
123        	$event->data = [];
124    	}
125	}
126}
127