xref: /dokuwiki/lib/exe/ajax.php (revision 7216538165621816dc3f751adc0746bf66805421)
1<?php
2/**
3 * DokuWiki AJAX call handler
4 *
5 * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
6 * @author     Andreas Gohr <andi@splitbrain.org>
7 */
8
9//fix for Opera XMLHttpRequests
10if(!count($_POST) && !empty($HTTP_RAW_POST_DATA)){
11    parse_str($HTTP_RAW_POST_DATA, $_POST);
12}
13
14if(!defined('DOKU_INC')) define('DOKU_INC',dirname(__FILE__).'/../../');
15require_once(DOKU_INC.'inc/init.php');
16//close session
17session_write_close();
18
19header('Content-Type: text/html; charset=utf-8');
20
21
22//call the requested function
23if(isset($_POST['call'])){
24    $call = $_POST['call'];
25}else if(isset($_GET['call'])){
26    $call = $_GET['call'];
27}else{
28    exit;
29}
30$callfn = 'ajax_'.$call;
31
32if(function_exists($callfn)){
33    $callfn();
34}else{
35    $evt = new Doku_Event('AJAX_CALL_UNKNOWN', $call);
36    if ($evt->advise_before()) {
37        print "AJAX call '".htmlspecialchars($call)."' unknown!\n";
38        exit;
39    }
40    $evt->advise_after();
41    unset($evt);
42}
43
44/**
45 * Searches for matching pagenames
46 *
47 * @author Andreas Gohr <andi@splitbrain.org>
48 */
49function ajax_qsearch(){
50    global $conf;
51    global $lang;
52
53    $query = $_POST['q'];
54    if(empty($query)) $query = $_GET['q'];
55    if(empty($query)) return;
56
57    $data = ft_pageLookup($query, true, useHeading('navigation'));
58
59    if(!count($data)) return;
60
61    print '<strong>'.$lang['quickhits'].'</strong>';
62    print '<ul>';
63    foreach($data as $id => $title){
64        if (useHeading('navigation')) {
65            $name = $title;
66        } else {
67            $ns = getNS($id);
68            if($ns){
69                $name = noNS($id).' ('.$ns.')';
70            }else{
71                $name = $id;
72            }
73        }
74        echo '<li>' . html_wikilink(':'.$id,$name) . '</li>';
75    }
76    print '</ul>';
77}
78
79/**
80 * Support OpenSearch suggestions
81 *
82 * @link   http://www.opensearch.org/Specifications/OpenSearch/Extensions/Suggestions/1.0
83 * @author Mike Frysinger <vapier@gentoo.org>
84 */
85function ajax_suggestions() {
86    global $conf;
87    global $lang;
88
89    $query = cleanID($_POST['q']);
90    if(empty($query)) $query = cleanID($_GET['q']);
91    if(empty($query)) return;
92
93    $data = array();
94    $data = ft_pageLookup($query);
95    if(!count($data)) return;
96    $data = array_keys($data);
97
98    // limit results to 15 hits
99    $data = array_slice($data, 0, 15);
100    $data = array_map('trim',$data);
101    $data = array_map('noNS',$data);
102    $data = array_unique($data);
103    sort($data);
104
105    /* now construct a json */
106    $suggestions = array(
107                        $query,  // the original query
108                        $data,   // some suggestions
109                        array(), // no description
110                        array()  // no urls
111                   );
112    $json = new JSON();
113
114    header('Content-Type: application/x-suggestions+json');
115    print $json->encode($suggestions);
116}
117
118/**
119 * Refresh a page lock and save draft
120 *
121 * Andreas Gohr <andi@splitbrain.org>
122 */
123function ajax_lock(){
124    global $conf;
125    global $lang;
126    $id = cleanID($_POST['id']);
127    if(empty($id)) return;
128
129    if(!checklock($id)){
130        lock($id);
131        echo 1;
132    }
133
134    if($conf['usedraft'] && $_POST['wikitext']){
135        $client = $_SERVER['REMOTE_USER'];
136        if(!$client) $client = clientIP(true);
137
138        $draft = array('id'     => $id,
139                'prefix' => substr($_POST['prefix'], 0, -1),
140                'text'   => $_POST['wikitext'],
141                'suffix' => $_POST['suffix'],
142                'date'   => (int) $_POST['date'],
143                'client' => $client,
144                );
145        $cname = getCacheName($draft['client'].$id,'.draft');
146        if(io_saveFile($cname,serialize($draft))){
147            echo $lang['draftdate'].' '.dformat();
148        }
149    }
150
151}
152
153/**
154 * Delete a draft
155 *
156 * @author Andreas Gohr <andi@splitbrain.org>
157 */
158function ajax_draftdel(){
159    $id = cleanID($_REQUEST['id']);
160    if(empty($id)) return;
161
162    $client = $_SERVER['REMOTE_USER'];
163    if(!$client) $client = clientIP(true);
164
165    $cname = getCacheName($client.$id,'.draft');
166    @unlink($cname);
167}
168
169/**
170 * Return subnamespaces for the Mediamanager
171 *
172 * @author Andreas Gohr <andi@splitbrain.org>
173 */
174function ajax_medians(){
175    global $conf;
176
177    // wanted namespace
178    $ns  = cleanID($_POST['ns']);
179    $dir  = utf8_encodeFN(str_replace(':','/',$ns));
180
181    $lvl = count(explode(':',$ns));
182
183    $data = array();
184    search($data,$conf['mediadir'],'search_index',array('nofiles' => true),$dir);
185    foreach($data as $item){
186        $item['level'] = $lvl+1;
187        echo media_nstree_li($item);
188        echo media_nstree_item($item);
189        echo '</li>';
190    }
191}
192
193/**
194 * Return list of files for the Mediamanager
195 *
196 * @author Andreas Gohr <andi@splitbrain.org>
197 */
198function ajax_medialist(){
199    global $conf;
200    global $NS;
201
202    $NS = $_POST['ns'];
203    tpl_mediaContent(true);
204}
205
206/**
207 * Return sub index for index view
208 *
209 * @author Andreas Gohr <andi@splitbrain.org>
210 */
211function ajax_index(){
212    global $conf;
213
214    // wanted namespace
215    $ns  = cleanID($_POST['idx']);
216    $dir  = utf8_encodeFN(str_replace(':','/',$ns));
217
218    $lvl = count(explode(':',$ns));
219
220    $data = array();
221    search($data,$conf['datadir'],'search_index',array('ns' => $ns),$dir);
222    foreach($data as $item){
223        $item['level'] = $lvl+1;
224        echo html_li_index($item);
225        echo '<div class="li">';
226        echo html_list_index($item);
227        echo '</div>';
228        echo '</li>';
229    }
230}
231
232/**
233 * List matching namespaces and pages for the link wizard
234 *
235 * @author Andreas Gohr <gohr@cosmocode.de>
236 */
237function ajax_linkwiz(){
238    global $conf;
239    global $lang;
240
241    $q  = ltrim($_POST['q'],':');
242    $id = noNS($q);
243    $ns = getNS($q);
244
245    $ns = cleanID($ns);
246    $id = cleanID($id);
247
248    $nsd  = utf8_encodeFN(str_replace(':','/',$ns));
249    $idd  = utf8_encodeFN(str_replace(':','/',$id));
250
251    $data = array();
252    if($q && !$ns){
253
254        // use index to lookup matching pages
255        $pages = array();
256        $pages = ft_pageLookup($id,true);
257
258        // result contains matches in pages and namespaces
259        // we now extract the matching namespaces to show
260        // them seperately
261        $dirs  = array();
262
263        foreach($pages as $pid => $title){
264            if(strpos(noNS($pid),$id) === false){
265                // match was in the namespace
266                $dirs[getNS($pid)] = 1; // assoc array avoids dupes
267            }else{
268                // it is a matching page, add it to the result
269                $data[] = array(
270                        'id'    => $pid,
271                        'title' => $title,
272                        'type'  => 'f',
273                        );
274            }
275            unset($pages[$pid]);
276        }
277        foreach($dirs as $dir => $junk){
278            $data[] = array(
279                    'id'   => $dir,
280                    'type' => 'd',
281                    );
282        }
283
284    }else{
285
286        $opts = array(
287                'depth' => 1,
288                'listfiles' => true,
289                'listdirs'  => true,
290                'pagesonly' => true,
291                'firsthead' => true,
292                'sneakyacl' => $conf['sneaky_index'],
293                );
294        if($id) $opts['filematch'] = '^.*\/'.$id;
295        if($id) $opts['dirmatch']  = '^.*\/'.$id;
296        search($data,$conf['datadir'],'search_universal',$opts,$nsd);
297
298        // add back to upper
299        if($ns){
300            array_unshift($data,array(
301                        'id'   => getNS($ns),
302                        'type' => 'u',
303                        ));
304        }
305    }
306
307    // fixme sort results in a useful way ?
308
309    if(!count($data)){
310        echo $lang['nothingfound'];
311        exit;
312    }
313
314    // output the found data
315    $even = 1;
316    foreach($data as $item){
317        $even *= -1; //zebra
318
319        if(($item['type'] == 'd' || $item['type'] == 'u') && $item['id']) $item['id'] .= ':';
320        $link = wl($item['id']);
321
322        echo '<div class="'.(($even > 0)?'even':'odd').' type_'.$item['type'].'">';
323
324        if($item['type'] == 'u'){
325            $name = $lang['upperns'];
326        }else{
327            $name = htmlspecialchars($item['id']);
328        }
329
330        echo '<a href="'.$link.'" title="'.htmlspecialchars($item['id']).'" class="wikilink1">'.$name.'</a>';
331
332        if($item['title']){
333            echo '<span>'.htmlspecialchars($item['title']).'</span>';
334        }
335        echo '</div>';
336    }
337
338}
339
340//Setup VIM: ex: et ts=2 :
341