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