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