xref: /dokuwiki/lib/exe/ajax.php (revision d91a4ef556635b5298c47df2856851f0694b2885)
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
9if(!defined('DOKU_INC')) define('DOKU_INC',dirname(__FILE__).'/../../');
10require_once(DOKU_INC.'inc/init.php');
11//close session
12session_write_close();
13
14header('Content-Type: text/html; charset=utf-8');
15
16//call the requested function
17if($INPUT->post->has('call')){
18    $call = $INPUT->post->str('call');
19}else if($INPUT->get->has('call')){
20    $call = $INPUT->get->str('call');
21}else{
22    exit;
23}
24$callfn = 'ajax_'.$call;
25
26if(function_exists($callfn)){
27    $callfn();
28}else{
29    $evt = new Doku_Event('AJAX_CALL_UNKNOWN', $call);
30    if ($evt->advise_before()) {
31        print "AJAX call '".htmlspecialchars($call)."' unknown!\n";
32        exit;
33    }
34    $evt->advise_after();
35    unset($evt);
36}
37
38/**
39 * Searches for matching pagenames
40 *
41 * @author Andreas Gohr <andi@splitbrain.org>
42 */
43function ajax_qsearch(){
44    global $conf;
45    global $lang;
46    global $INPUT;
47
48    $query = $INPUT->post->str('q');
49    if(empty($query)) $query = $INPUT->get->str('q');
50    if(empty($query)) return;
51
52    $query = urldecode($query);
53
54    $data = ft_pageLookup($query, true, useHeading('navigation'));
55
56    if(!count($data)) return;
57
58    print '<strong>'.$lang['quickhits'].'</strong>';
59    print '<ul>';
60    $counter = 0;
61    foreach($data as $id => $title){
62        if (useHeading('navigation')) {
63            $name = $title;
64        } else {
65            $ns = getNS($id);
66            if($ns){
67                $name = noNS($id).' ('.$ns.')';
68            }else{
69                $name = $id;
70            }
71        }
72        echo '<li>' . html_wikilink(':'.$id,$name) . '</li>';
73
74        $counter ++;
75        if($counter > 50) {
76            echo '<li>...</li>';
77            break;
78        }
79    }
80    print '</ul>';
81}
82
83/**
84 * Support OpenSearch suggestions
85 *
86 * @link   http://www.opensearch.org/Specifications/OpenSearch/Extensions/Suggestions/1.0
87 * @author Mike Frysinger <vapier@gentoo.org>
88 */
89function ajax_suggestions() {
90    global $conf;
91    global $lang;
92    global $INPUT;
93
94    $query = cleanID($INPUT->post->str('q'));
95    if(empty($query)) $query = cleanID($INPUT->get->str('q'));
96    if(empty($query)) return;
97
98    $data = array();
99    $data = ft_pageLookup($query);
100    if(!count($data)) return;
101    $data = array_keys($data);
102
103    // limit results to 15 hits
104    $data = array_slice($data, 0, 15);
105    $data = array_map('trim',$data);
106    $data = array_map('noNS',$data);
107    $data = array_unique($data);
108    sort($data);
109
110    /* now construct a json */
111    $suggestions = array(
112                        $query,  // the original query
113                        $data,   // some suggestions
114                        array(), // no description
115                        array()  // no urls
116                   );
117    $json = new JSON();
118
119    header('Content-Type: application/x-suggestions+json');
120    print $json->encode($suggestions);
121}
122
123/**
124 * Refresh a page lock and save draft
125 *
126 * Andreas Gohr <andi@splitbrain.org>
127 */
128function ajax_lock(){
129    global $conf;
130    global $lang;
131    global $ID;
132    global $INFO;
133    global $INPUT;
134
135    $ID = cleanID($INPUT->post->str('id'));
136    if(empty($ID)) return;
137
138    $INFO = pageinfo();
139
140    if (!$INFO['writable']) {
141        echo 'Permission denied';
142        return;
143    }
144
145    if(!checklock($ID)){
146        lock($ID);
147        echo 1;
148    }
149
150    if($conf['usedraft'] && $INPUT->post->str('wikitext')){
151        $client = $_SERVER['REMOTE_USER'];
152        if(!$client) $client = clientIP(true);
153
154        $draft = array('id'     => $ID,
155                'prefix' => substr($INPUT->post->str('prefix'), 0, -1),
156                'text'   => $INPUT->post->str('wikitext'),
157                'suffix' => $INPUT->post->str('suffix'),
158                'date'   => $INPUT->post->int('date'),
159                'client' => $client,
160                );
161        $cname = getCacheName($draft['client'].$ID,'.draft');
162        if(io_saveFile($cname,serialize($draft))){
163            echo $lang['draftdate'].' '.dformat();
164        }
165    }
166
167}
168
169/**
170 * Delete a draft
171 *
172 * @author Andreas Gohr <andi@splitbrain.org>
173 */
174function ajax_draftdel(){
175    global $INPUT;
176    $id = cleanID($INPUT->str('id'));
177    if(empty($id)) return;
178
179    $client = $_SERVER['REMOTE_USER'];
180    if(!$client) $client = clientIP(true);
181
182    $cname = getCacheName($client.$id,'.draft');
183    @unlink($cname);
184}
185
186/**
187 * Return subnamespaces for the Mediamanager
188 *
189 * @author Andreas Gohr <andi@splitbrain.org>
190 */
191function ajax_medians(){
192    global $conf;
193    global $INPUT;
194
195    // wanted namespace
196    $ns  = cleanID($INPUT->post->str('ns'));
197    $dir  = utf8_encodeFN(str_replace(':','/',$ns));
198
199    $lvl = count(explode(':',$ns));
200
201    $data = array();
202    search($data,$conf['mediadir'],'search_index',array('nofiles' => true),$dir);
203    foreach(array_keys($data) as $item){
204        $data[$item]['level'] = $lvl+1;
205    }
206    echo html_buildlist($data, 'idx', 'media_nstree_item', 'media_nstree_li');
207}
208
209/**
210 * Return list of files for the Mediamanager
211 *
212 * @author Andreas Gohr <andi@splitbrain.org>
213 */
214function ajax_medialist(){
215    global $conf;
216    global $NS;
217    global $INPUT;
218
219    $NS = cleanID($INPUT->post->str('ns'));
220    if ($INPUT->post->str('do') == 'media') {
221        tpl_mediaFileList();
222    } else {
223        tpl_mediaContent(true);
224    }
225}
226
227/**
228 * Return the content of the right column
229 * (image details) for the Mediamanager
230 *
231 * @author Kate Arzamastseva <pshns@ukr.net>
232 */
233function ajax_mediadetails(){
234    global $DEL, $NS, $IMG, $AUTH, $JUMPTO, $REV, $lang, $fullscreen, $conf, $INPUT;
235    $fullscreen = true;
236    require_once(DOKU_INC.'lib/exe/mediamanager.php');
237
238    if ($INPUT->has('image')) $image = cleanID($INPUT->str('image'));
239    if (isset($IMG)) $image = $IMG;
240    if (isset($JUMPTO)) $image = $JUMPTO;
241    if (isset($REV) && !$JUMPTO) $rev = $REV;
242
243    html_msgarea();
244    tpl_mediaFileDetails($image, $rev);
245}
246
247/**
248 * Returns image diff representation for mediamanager
249 * @author Kate Arzamastseva <pshns@ukr.net>
250 */
251function ajax_mediadiff(){
252    global $NS;
253    global $INPUT;
254
255    if ($INPUT->has('image')) $image = cleanID($INPUT->str('image'));
256    $NS = $INPUT->post->str('ns');
257    $auth = auth_quickaclcheck("$NS:*");
258    media_diff($image, $NS, $auth, true);
259}
260
261function ajax_mediaupload(){
262    global $NS, $MSG, $INPUT;
263
264    if ($_FILES['qqfile']['tmp_name']) {
265        $id = $INPUT->post->str('mediaid', $_FILES['qqfile']['name']);
266    } elseif ($INPUT->get->has('qqfile')) {
267        $id = $INPUT->get->str('qqfile');
268    }
269
270    $id = cleanID($id);
271
272    $NS = $INPUT->str('ns');
273    $ns = $NS.':'.getNS($id);
274
275    $AUTH = auth_quickaclcheck("$ns:*");
276    if($AUTH >= AUTH_UPLOAD) { io_createNamespace("$ns:xxx", 'media'); }
277
278    if ($_FILES['qqfile']['error']) unset($_FILES['qqfile']);
279
280    if ($_FILES['qqfile']['tmp_name']) $res = media_upload($NS, $AUTH, $_FILES['qqfile']);
281    if ($INPUT->get->has('qqfile')) $res = media_upload_xhr($NS, $AUTH);
282
283    if ($res) $result = array('success' => true,
284        'link' => media_managerURL(array('ns' => $ns, 'image' => $NS.':'.$id), '&'),
285        'id' => $NS.':'.$id, 'ns' => $NS);
286
287    if (!$result) {
288        $error = '';
289        if (isset($MSG)) {
290            foreach($MSG as $msg) $error .= $msg['msg'];
291        }
292        $result = array('error' => $msg['msg'], 'ns' => $NS);
293    }
294    $json = new JSON;
295    echo htmlspecialchars($json->encode($result), ENT_NOQUOTES);
296}
297
298function dir_delete($path) {
299    if (!is_string($path) || $path == "") return false;
300
301    if (is_dir($path) && !is_link($path)) {
302        if (!$dh = @opendir($path)) return false;
303
304        while ($f = readdir($dh)) {
305            if ($f == '..' || $f == '.') continue;
306            dir_delete("$path/$f");
307        }
308
309        closedir($dh);
310        return @rmdir($path);
311    } else {
312        return @unlink($path);
313    }
314
315    return false;
316}
317
318/**
319 * Return sub index for index view
320 *
321 * @author Andreas Gohr <andi@splitbrain.org>
322 */
323function ajax_index(){
324    global $conf;
325    global $INPUT;
326
327    // wanted namespace
328    $ns  = cleanID($INPUT->post->str('idx'));
329    $dir  = utf8_encodeFN(str_replace(':','/',$ns));
330
331    $lvl = count(explode(':',$ns));
332
333    $data = array();
334    search($data,$conf['datadir'],'search_index',array('ns' => $ns),$dir);
335    foreach(array_keys($data) as $item){
336        $data[$item]['level'] = $lvl+1;
337    }
338    echo html_buildlist($data, 'idx', 'html_list_index', 'html_li_index');
339}
340
341/**
342 * List matching namespaces and pages for the link wizard
343 *
344 * @author Andreas Gohr <gohr@cosmocode.de>
345 */
346function ajax_linkwiz(){
347    global $conf;
348    global $lang;
349    global $INPUT;
350
351    $q  = ltrim(trim($INPUT->post->str('q')),':');
352    $id = noNS($q);
353    $ns = getNS($q);
354
355    $ns = cleanID($ns);
356    $id = cleanID($id);
357
358    $nsd  = utf8_encodeFN(str_replace(':','/',$ns));
359    $idd  = utf8_encodeFN(str_replace(':','/',$id));
360
361    $data = array();
362    if($q && !$ns){
363
364        // use index to lookup matching pages
365        $pages = array();
366        $pages = ft_pageLookup($id,true);
367
368        // result contains matches in pages and namespaces
369        // we now extract the matching namespaces to show
370        // them seperately
371        $dirs  = array();
372
373        foreach($pages as $pid => $title){
374            if(strpos(noNS($pid),$id) === false){
375                // match was in the namespace
376                $dirs[getNS($pid)] = 1; // assoc array avoids dupes
377            }else{
378                // it is a matching page, add it to the result
379                $data[] = array(
380                        'id'    => $pid,
381                        'title' => $title,
382                        'type'  => 'f',
383                        );
384            }
385            unset($pages[$pid]);
386        }
387        foreach($dirs as $dir => $junk){
388            $data[] = array(
389                    'id'   => $dir,
390                    'type' => 'd',
391                    );
392        }
393
394    }else{
395
396        $opts = array(
397                'depth' => 1,
398                'listfiles' => true,
399                'listdirs'  => true,
400                'pagesonly' => true,
401                'firsthead' => true,
402                'sneakyacl' => $conf['sneaky_index'],
403                );
404        if($id) $opts['filematch'] = '^.*\/'.$id;
405        if($id) $opts['dirmatch']  = '^.*\/'.$id;
406        search($data,$conf['datadir'],'search_universal',$opts,$nsd);
407
408        // add back to upper
409        if($ns){
410            array_unshift($data,array(
411                        'id'   => getNS($ns),
412                        'type' => 'u',
413                        ));
414        }
415    }
416
417    // fixme sort results in a useful way ?
418
419    if(!count($data)){
420        echo $lang['nothingfound'];
421        exit;
422    }
423
424    // output the found data
425    $even = 1;
426    foreach($data as $item){
427        $even *= -1; //zebra
428
429        if(($item['type'] == 'd' || $item['type'] == 'u') && $item['id']) $item['id'] .= ':';
430        $link = wl($item['id']);
431
432        echo '<div class="'.(($even > 0)?'even':'odd').' type_'.$item['type'].'">';
433
434        if($item['type'] == 'u'){
435            $name = $lang['upperns'];
436        }else{
437            $name = htmlspecialchars($item['id']);
438        }
439
440        echo '<a href="'.$link.'" title="'.htmlspecialchars($item['id']).'" class="wikilink1">'.$name.'</a>';
441
442        if($item['title']){
443            echo '<span>'.htmlspecialchars($item['title']).'</span>';
444        }
445        echo '</div>';
446    }
447
448}
449
450//Setup VIM: ex: et ts=2 :
451