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