1<?php
2/**
3 * AJAX Backend for indexmenu
4 *
5 * @author Samuele Tognini <samuele@samuele.netsons.org>
6 * @license     GPL 2 (http://www.gnu.org/licenses/gpl.html)
7 */
8
9//fix for Opera XMLHttpRequests
10if(!count($_POST) && @$HTTP_RAW_POST_DATA) {
11    parse_str($HTTP_RAW_POST_DATA, $_POST);
12}
13
14if(!defined('DOKU_INC')) define('DOKU_INC', realpath(dirname(__FILE__).'/../../../').'/');
15if(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN', DOKU_INC.'lib/plugins/');
16require_once(DOKU_INC.'inc/init.php');
17require_once(DOKU_INC.'inc/auth.php');
18if(!defined('INDEXMENU_IMG_ABSDIR')) define('INDEXMENU_IMG_ABSDIR', DOKU_PLUGIN."indexmenu/images");
19//close session
20session_write_close();
21
22$ajax_indexmenu = new ajax_indexmenu_plugin;
23$ajax_indexmenu->render();
24
25class ajax_indexmenu_plugin {
26    /**
27     * Output
28     *
29     * @author Samuele Tognini <samuele@samuele.netsons.org>
30     */
31
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']);
40        }
41        if($succ) return true;
42
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':  //required for admin.php
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;   */
59        }
60    }
61
62    /**
63     * Print a list of local themes
64     *
65     * @author Samuele Tognini <samuele@samuele.netsons.org>
66     */
67
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                && $file != ".svn"
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@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
129        $out = $lang['toc'];
130        $out .= '</div>'.DOKU_LF;
131        $out .= '<div class="indexmenu_toc_inside">'.DOKU_LF;
132        $out .= html_buildlist($r->toc, 'toc', array($this, '_tocitem'));
133        $out .= '</div>'.DOKU_LF;
134        return $out;
135    }
136
137    /**
138     * Callback for html_buildlist
139     */
140    function _tocitem($item) {
141        $id = cleanID($_POST['id']);
142        return '<span class="li"><a href="'.wl($id, '#'.$item['hid'], false, '').'" class="toc">'.
143            htmlspecialchars($item['title']).'</a></span>';
144    }
145
146    /**
147     * Print index nodes
148     *
149     * @author Samuele Tognini <samuele@samuele.netsons.org>
150     * @author Andreas Gohr <andi@splitbrain.org>
151     * @author Rene Hadler <rene.hadler@iteas.at>
152     */
153    function print_index($ns) {
154        require_once(DOKU_PLUGIN.'indexmenu/syntax/indexmenu.php');
155        global $conf;
156        $idxm  = new syntax_plugin_indexmenu_indexmenu();
157		$ns=$idxm->_parse_ns(rawurldecode($ns));
158        $level = -1;
159        $max   = 0;
160        $data  = array();
161        $out   = '';
162        if($_REQUEST['max'] > 0) {
163            $max   = $_REQUEST['max'];
164            $level = $max;
165        }
166        $nss         = ($_REQUEST['nss']) ? cleanID($_REQUEST['nss']) : '';
167        $idxm->sort  = $_REQUEST['sort'];
168        $idxm->msort = $_REQUEST['msort'];
169        $idxm->rsort = $_REQUEST['rsort'];
170        $idxm->nsort = $_REQUEST['nsort'];
171        $idxm->hsort = $_REQUEST['hsort'];
172        $fsdir       = "/".utf8_encodeFN(str_replace(':', '/', $ns));
173        $opts        = array(
174            'level'         => $level,
175            'nons'          => $_REQUEST['nons'],
176            'nss'           => array(array($nss, 1)),
177            'max'           => $max,
178            'js'            => false,
179            'nopg'          => $_REQUEST['nopg'],
180            'skip_index'    => $idxm->getConf('skip_index'),
181            'skip_file'     => $idxm->getConf('skip_file'),
182            'headpage'      => $idxm->getConf('headpage'),
183            'hide_headpage' => $idxm->getConf('hide_headpage')
184        );
185        if($idxm->sort || $idxm->msort || $idxm->rsort || $idxm->hsort) {
186            $idxm->_search($data, $conf['datadir'], array($idxm, '_search_index'), $opts, $fsdir);
187        } else {
188            search($data, $conf['datadir'], array($idxm, '_search_index'), $opts, $fsdir);
189        }
190        if($_REQUEST['nojs']) {
191            require_once(DOKU_INC.'inc/html.php');
192            $out_tmp = html_buildlist($data, 'idx', array($idxm, "_html_list_index"), "html_li_index");
193            $out .= preg_replace('/<ul class="idx">(.*)<\/ul>/s', "$1", $out_tmp);
194        } else {
195            $nodes = $idxm->_jsnodes($data, '', 0);
196            $out   = "ajxnodes = [";
197            $out .= rtrim($nodes[0], ",");
198            $out .= "];";
199        }
200        return $out;
201    }
202}
203