xref: /dokuwiki/lib/exe/ajax.php (revision 003a37b419108048f084be9c59598a00a7eb8724)
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'];
25else if(isset($_GET['call']))
26  $call = $_GET['call'];
27else
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 = shorten(noNS($id), ' ('.$ns.')',30);
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' => $_POST['prefix'],
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
264    foreach($pages as $pid => $title){
265      if(strpos(noNS($pid),$id) === false){
266        // match was in the namespace
267        $dirs[getNS($pid)] = 1; // assoc array avoids dupes
268      }else{
269        // it is a matching page, add it to the result
270        $data[] = array(
271          'id'    => $pid,
272          'title' => $title,
273          'type'  => 'f',
274        );
275      }
276      unset($pages[$pid]);
277    }
278    foreach($dirs as $dir => $junk){
279      $data[] = array(
280        'id'   => $dir,
281        'type' => 'd',
282      );
283    }
284
285  }else{
286
287    $opts = array(
288      'depth' => 1,
289      'listfiles' => true,
290      'listdirs'  => true,
291      'pagesonly' => true,
292      'firsthead' => true,
293      'sneakyacl' => $conf['sneaky_index'],
294    );
295    if($id) $opts['filematch'] = '^.*\/'.$id;
296    if($id) $opts['dirmatch']  = '^.*\/'.$id;
297    search($data,$conf['datadir'],'search_universal',$opts,$nsd);
298
299    // add back to upper
300    if($ns){
301        array_unshift($data,array(
302            'id'   => getNS($ns),
303            'type' => 'u',
304        ));
305    }
306  }
307
308  // fixme sort results in a useful way ?
309
310  if(!count($data)){
311    echo $lang['nothingfound'];
312    exit;
313  }
314
315  // output the found data
316  $even = 1;
317  foreach($data as $item){
318    $even *= -1; //zebra
319
320    if(($item['type'] == 'd' || $item['type'] == 'u') && $item['id']) $item['id'] .= ':';
321    $link = wl($item['id']);
322
323    echo '<div class="'.(($even > 0)?'even':'odd').' type_'.$item['type'].'">';
324
325
326    if($item['type'] == 'u'){
327        $name = $lang['upperns'];
328    }else{
329        $name = htmlspecialchars($item['id']);
330    }
331
332    echo '<a href="'.$link.'" title="'.htmlspecialchars($item['id']).'" class="wikilink1">'.$name.'</a>';
333
334    if($item['title']){
335      echo '<span>'.htmlspecialchars($item['title']).'</span>';
336    }
337    echo '</div>';
338  }
339
340}
341
342//Setup VIM: ex: et ts=2 enc=utf-8 :
343