1<?php
2/**
3 * AJAX Backend for tindexmenu
4 *
5 * @license     GPL 2 (http://www.gnu.org/licenses/gpl.html)
6 * @author     Samuele Tognini <samuele@netsons.org>
7 * @author     Rene Hadler <rene.hadler@iteas.at>
8 */
9
10//fix for Opera XMLHttpRequests
11if(!count($_POST) && @$HTTP_RAW_POST_DATA){
12	parse_str($HTTP_RAW_POST_DATA, $_POST);
13}
14
15if(!defined('DOKU_INC')) define('DOKU_INC',realpath(dirname(__FILE__).'/../../../').'/');
16if(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
17require_once(DOKU_INC.'inc/init.php');
18require_once(DOKU_INC.'inc/auth.php');
19if(!defined('INDEXMENU_IMG_ABSDIR')) define('INDEXMENU_IMG_ABSDIR',DOKU_PLUGIN."tindexmenu/images");
20//close session
21session_write_close();
22
23$ajax_indexmenu=new ajax_indexmenu_plugin;
24$ajax_indexmenu->render();
25
26class ajax_indexmenu_plugin {
27	/**
28	 * Output
29	 *
30	 * @author Samuele Tognini <samuele@netsons.org>
31	 */
32
33	function render () {
34		$req=$_REQUEST['req'];
35		$succ=false;
36		//send the zip
37		if ($req == 'send' and isset($_REQUEST['t'])) {
38			include(DOKU_PLUGIN.'tindexmenu/inc/repo.class.php');
39			$repo=new repo_indexmenu_plugin;
40			$succ=$repo->send_theme($_REQUEST['t']);
41		}
42		if ($succ) return true;
43
44		header('Content-Type: text/html; charset=utf-8');
45		header('Cache-Control: public, max-age=3600');
46		header('Pragma: public');
47		switch ($req) {
48			case 'local':
49				//list themes
50				print $this->local_themes();
51				break;
52			case 'toc':
53				//print toc preview
54				if (isset($_REQUEST['id'])) print $this->print_toc($_REQUEST['id']);
55				break;
56			case 'index':
57				//print index
58				if (isset($_REQUEST['idx'])) print $this->print_index($_REQUEST['idx']);
59				break;
60		}
61	}
62
63	/**
64	 * Print a list of local themes
65	 *
66	 * @author Samuele Tognini <samuele@netsons.org>
67	 */
68
69	function local_themes() {
70		$list='indexmenu,'.DOKU_URL.",lib/plugins/tindexmenu/images,";
71		$data=array();
72		$handle=@opendir(INDEXMENU_IMG_ABSDIR);
73		while (false !== ($file = readdir($handle))) {
74			if ( is_dir (INDEXMENU_IMG_ABSDIR.'/'.$file)
75					&& $file != "."
76					&& $file != ".."
77					&& $file != "repository"
78					&& $file != "tmp"
79			) {
80				$data[]=$file;
81			}
82		}
83		closedir($handle);
84		sort($data);
85		$list .= implode(",",$data);
86		return $list;
87	}
88
89	/**
90	 * Print a toc preview
91	 *
92	 * @author Samuele Tognini <samuele@netsons.org>
93	 * @author Andreas Gohr <andi@splitbrain.org>
94	 */
95	function print_toc($id) {
96		require_once(DOKU_INC.'inc/parser/xhtml.php');
97		$id=cleanID($id);
98		if (auth_quickaclcheck($id) < AUTH_READ) return;
99		$meta = p_get_metadata($id);
100		$toc = $meta['description']['tableofcontents'];
101		$out .= '<div class="tocheader toctoggle">'.DOKU_LF;
102		if (count($toc)>1) {
103			$out .= $this->render_toc($toc);
104		} else {
105			$out .= '<a href="'.wl($id).'">';
106			$out .= ($meta['title']) ? htmlspecialchars($meta['title']) :  htmlspecialchars(noNS($id));
107			$out .= '</a>'.DOKU_LF;
108			if ($meta['description']['abstract']) {
109				$out .= '</div>'.DOKU_LF;
110				$out .= '<div class="indexmenu_toc_inside">'.DOKU_LF;
111				$out .= p_render('xhtml',p_get_instructions($meta['description']['abstract']),$info);
112				$out .= '</div>'.DOKU_LF;
113			}
114		}
115		$out .= '</div>'.DOKU_LF;
116		return $out;
117	}
118
119	/**
120	 * Return the TOC rendered to XHTML
121	 *
122	 * @author Andreas Gohr <andi@splitbrain.org>
123	 */
124	function render_toc($toc){
125		global $lang;
126		$r = new Doku_Renderer_xhtml;
127		$r->toc = $toc;
128		$out .= $lang['toc'];
129		$out .= '</div>'.DOKU_LF;
130		$out .= '<div class="indexmenu_toc_inside">'.DOKU_LF;
131		$out .= html_buildlist($r->toc,'toc',array($this,'_tocitem'));
132		$out .= '</div>'.DOKU_LF;
133		return $out;
134	}
135
136	/**
137	 * Callback for html_buildlist
138	 */
139	function _tocitem($item){
140		$id=cleanID($_POST['id']);
141		return '<span class="li"><a href="'.wl($id,'#'.$item['hid'],false,'').'" class="toc">'.
142				htmlspecialchars($item['title']).'</a></span>';
143	}
144
145	/**
146	 * Print index nodes
147	 *
148	 * @author Samuele Tognini <samuele@netsons.org>
149	 * @author Andreas Gohr <andi@splitbrain.org>
150	 */
151	function print_index($ns) {
152		require_once(DOKU_PLUGIN.'tindexmenu/syntax/indexmenu.php');
153		global $conf;
154		$idxm=new syntax_plugin_indexmenu_indexmenu();
155		$ns=$idxm->_parse_ns(rawurldecode($ns));
156		$level=-1;
157		$max=0;
158		$data = array();
159		if ($_REQUEST['max'] > 0) {
160			$max=$_REQUEST['max'];
161			$level=$max;
162		}
163		$nss= ($_REQUEST['nss']) ? cleanID($_REQUEST['nss']) : '';
164		$idxm->sort=$_REQUEST['sort'];
165		$idxm->msort=$_REQUEST['msort'];
166		$idxm->rsort=$_REQUEST['rsort'];
167		$idxm->nsort=$_REQUEST['nsort'];
168		$fsdir="/".utf8_encodeFN(str_replace(':','/',$ns));
169		$opts= array('level' => $level,
170				'nons' => $_REQUEST['nons'],
171				'nss' => array(array($nss,1)),
172				'max' => $max,
173				'js' => false,
174				'nopg' => $_REQUEST['nopg'],
175				'skip_index' => $idxm->getConf('skip_index'),
176				'skip_file' => $idxm->getConf('skip_file'),
177				'headpage' => $idxm->getConf('headpage'),
178				'hide_headpage' => $idxm->getConf('hide_headpage')
179		);
180		if ($idxm->sort||$idxm->msort||$idxm->rsort) {
181			$idxm->_search($data,$conf['datadir'],array($idxm,'_search_index'),$opts,$fsdir);
182		} else {
183			search($data,$conf['datadir'],array($idxm,'_search_index'),$opts,$fsdir);
184		}
185		if ($_REQUEST['nojs']) {
186			require_once(DOKU_INC.'inc/html.php');
187			$out_tmp=html_buildlist($data,'idx',array($idxm,"_html_list_index"),"html_li_index");
188			$out.=preg_replace('/<ul class="idx">(.*)<\/ul>/s',"$1",$out_tmp);
189		} else {
190			$nodes=$idxm->_jsnodes($data,'',0);
191			$out="ajxnodes = [";
192			$out.=rtrim($nodes[0],",");
193			$out.="];";
194		}
195		return $out;
196	}
197}
198