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