xref: /plugin/siteexport/action/startup.php (revision 5cdb2201c941d03f9c7ea10714cc15d573dae534)
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	}
29
30	/**
31	* Check for Template changes
32	**/
33	function siteexport_check_template()
34	{
35		global $conf, $INFO;
36
37        if ( function_exists('getallheaders') ) {
38            $headers = getallheaders();
39        }
40
41        if ( is_array($headers) && array_key_exists('X-Site-Exporter', $headers) && $headers['X-Site-Exporter'] = getSecurityToken() || defined('SITEEXPORT_TPL') ) {
42            // This is a request via the HTTPProxy of the SiteExporter ... set config to what we need here.
43    		$conf['useslash'] = 1;
44        }
45
46		if ( !defined('SITEEXPORT_TPL') ) { return; }
47		$conf['template'] = SITEEXPORT_TPL;
48	}
49
50	/**
51	* Check for Template changes in JS
52	**/
53	function siteexport_check_js_cache(&$event)
54	{
55		global $conf, $INFO;
56
57		if ( !defined('SITEEXPORT_TPL') ) { return; }
58		$event->data->key .= SITEEXPORT_TPL;
59		$event->data->cache = getCacheName($event->data->key,$event->data->ext);
60	}
61
62	function siteexport_check_export(&$event)
63	{
64	    global $conf;
65	    $command = is_array($event->data) ? array_shift(array_keys($event->data)) : $event->data;
66	    if ( $command == 'export_siteexport_pdf')
67	    {
68	        $event->data = 'show';
69	        $conf['renderer_xhtml'] = 'siteexport_pdf';
70	    }
71
72	    if ( $command == 'siteexport_addpage' && ($this->getConf('allowallusers') || auth_isadmin() || auth_ismanager() ) )
73	    {
74			$event->preventDefault();
75	    }
76	}
77
78	function siteexport_addpage(&$event)
79	{
80		if ( $event->data != 'siteexport_addpage' || ! ($this->getConf('allowallusers') || auth_isadmin() || auth_ismanager()) ) { return; }
81        if ( ! $functions=& plugin_load('helper', 'siteexport') ) {
82            msg("Can't initialize");
83            return false;
84        }
85
86        $functions->__siteexport_addpage();
87	    $event->preventDefault();
88	}
89
90	function siteexport_add_page_export(&$event)
91	{
92		global $ID;
93
94		if ( ($this->getConf('allowallusers') || auth_isadmin() || auth_ismanager()) ) {
95			$event->data['items'][] = '<li>' . tpl_link(wl($ID, array('do' => 'siteexport_addpage')), '<span>Export Page</span>',
96												'class="action siteexport_addpage" title="Add page"', 1) . '</li>';
97		}
98	}
99
100	function siteexport_metaheaders(&$event)
101	{
102		global $conf;
103		$template = defined('SITEEXPORT_TPL') ? SITEEXPORT_TPL : $conf['template'];
104
105		$head =& $event->data;
106
107		foreach( $head['script'] as &$script ) {
108			if ( !empty($script['src']) && strstr($script['src'], 'js.php') ) {
109				$script['src'] .= '&template=' . $template;
110			}
111		}
112
113		return true;
114	}
115}
116