1<?php
2/**
3 * tIndexmenu Action Plugin:   Indexmenu Component.
4 *
5 * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
6 * @author     Samuele Tognini <samuele@netsons.org> mod. by Rene Hadler <rene.hadler@iteas.at>
7 * @author     Rene Hadler <rene.hadler@iteas.at>
8 */
9
10if(!defined('DOKU_INC')) die();
11if(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
12require_once(DOKU_PLUGIN.'action.php');
13
14class action_plugin_indexmenu extends DokuWiki_Action_Plugin {
15
16	/**
17	 * return some info
18	 */
19	function getInfo(){
20		return array(
21				'author' => 'Samuele Tognini mod. by Rene Hadler',
22				'email'  => 'samuele@netsons.org, rene.hadler@iteas.at',
23				'date'   => rtrim(io_readFile(DOKU_PLUGIN.'tindexmenu/VERSION.txt')),
24				'name'   => 'tIndexmenu (action plugin component)',
25				'desc'   => 'tIndexmenu action functions.',
26				'url'    => 'https://bitbucket.org/iteas/tindexmenu',
27		);
28	}
29
30	/*
31	 * plugin should use this method to register its handlers with the dokuwiki's event controller
32	*/
33	function register(&$controller) {
34		if ($this->getConf('only_admins')) $controller->register_hook('IO_WIKIPAGE_WRITE', 'BEFORE',  $this, '_checkperm');
35		if ($this->getConf('page_index') != '') $controller->register_hook('TPL_ACT_RENDER', 'BEFORE', $this, '_loadindex');
36		$controller->register_hook('TPL_METAHEADER_OUTPUT', 'BEFORE', $this, '_hookjs');
37		$controller->register_hook('PARSER_CACHE_USE', 'BEFORE',  $this, '_purgecache');
38		if ($this->getConf('show_sort')) $controller->register_hook('TPL_CONTENT_DISPLAY', 'BEFORE', $this, '_showsort');
39	}
40
41	/**
42	 * Check if user has permission to insert indexmenu
43	 *
44	 * @author Samuele Tognini <samuele@netsons.org>
45	 */
46	function _checkperm(&$event, $param) {
47		if ($this->_notadmin()) {
48			$event->data[0][1]= preg_replace("/{{indexmenu(|_n)>.+?}}/","",$event->data[0][1]);
49		}
50	}
51
52	/**
53	 * Hook js script into page headers.
54	 *
55	 * @author Samuele Tognini <samuele@netsons.org>
56	 */
57	function _hookjs(&$event, $param) {
58		global $ID;
59		global $INFO;
60		$jsmenu=DOKU_PLUGIN."tindexmenu/jsmenu/";
61
62		if ($INFO['userinfo']['grps']) {
63			$jsmenu .= ($this->_notadmin()) ? "usrmenu.js" : "admmenu.js";
64		} else {
65			$jsmenu .= "menu.js";
66		}
67
68		$event->data["script"][] = array (  "type" => "text/javascript",
69				"charset" => "utf-8",
70				"_data" => "",
71				"src" =>  $jsmenu
72		);
73
74		$event->data["script"][] = array (	"type" => "text/javascript",
75				"charset" => "utf-8",
76				"_data" => "",
77				"src" => DOKU_BASE."lib/plugins/tindexmenu/indexmenu-full.js"
78		);
79
80		$event->data["script"][] = array (	"type" => "text/javascript",
81				"charset" => "utf-8",
82				"_data" => "var indexmenu_ID='".idfilter($ID)."'"
83		);
84	}
85
86	/**
87	 * Check for pages changes and eventually purge cache.
88	 *
89	 * @author Samuele Tognini <samuele@netsons.org>
90	 */
91	function _purgecache(&$event, $param) {
92		global $ID;
93		global $conf;
94		$cache = &$event->data;
95
96		if (!isset($cache->page)) return;
97		//purge only xhtml cache
98		if ($cache->mode != "xhtml") return;
99		//Check if it is an indexmenu page
100		if (!p_get_metadata($ID,'indexmenu')) return;
101		$aclcache=$this->getConf('aclcache');
102		if ($conf['useacl']) {
103			$newkey=false;
104			if ($aclcache == 'user') {
105				//Cache per user
106				if ($_SERVER['REMOTE_USER']) $newkey=$_SERVER['REMOTE_USER'];
107			} else if ($aclcache == 'groups') {
108				//Cache per groups
109				global $INFO;
110				if ($INFO['userinfo']['grps']) $newkey=implode('#',$INFO['userinfo']['grps']);
111			}
112			if ($newkey) {
113				$cache->key .= "#".$newkey;
114				$cache->cache = getCacheName($cache->key, $cache->ext);
115			}
116		}
117		//Check if a page is more recent than purgefile.
118		if (@filemtime($cache->cache) < @filemtime($conf['cachedir'].'/purgefile')) {
119			$event->preventDefault();
120			$event->stopPropagation();
121			$event->result = false;
122		}
123	}
124
125	/**
126	 * Render a defined page as index.
127	 *
128	 * @author Samuele Tognini <samuele@netsons.org>
129	 */
130	function _loadindex(&$event, $param) {
131		if ('index' != $event->data) return;
132		if (!file_exists(wikiFN($this->getConf('page_index')))) return;
133		global $lang;
134		print '<h1><a id="index" name="index">'.$lang['btn_index']."</a></h1>\n";
135		print p_wiki_xhtml($this->getConf('page_index'));
136		$event->preventDefault();
137		$event->stopPropagation();
138
139	}
140
141	/**
142	 * Display the indexmenu sort number.
143	 *
144	 * @author Samuele Tognini <samuele@netsons.org>
145	 */
146	function _showsort(&$event, $param) {
147		global $ID,$ACT;
148		if ($ACT != 'show' || $this->_notadmin()) return;
149		if ($n=p_get_metadata($ID,'indexmenu_n')) {
150			ptln('<div class="info">');
151			ptln($this->getLang('showsort').$n);
152			ptln('</div>');
153		}
154	}
155
156	/**
157	 * Check if user is administrator..
158	 *
159	 * @author Samuele Tognini <samuele@netsons.org>
160	 */
161	function _notadmin() {
162		global $conf;
163		global $INFO;
164
165		if ($conf['useacl'] && $INFO['perm'] < AUTH_ADMIN) {
166			return true;
167		}
168		return false;
169	}
170}
171