xref: /dokuwiki/lib/exe/ajax.php (revision 56dfcc12d4d4b326fc393a8271da0cf8374d3a11)
1f62ea8a1Sandi<?php
2f62ea8a1Sandi/**
3f62ea8a1Sandi * DokuWiki AJAX call handler
4f62ea8a1Sandi *
5f62ea8a1Sandi * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
6f62ea8a1Sandi * @author     Andreas Gohr <andi@splitbrain.org>
7f62ea8a1Sandi */
8f62ea8a1Sandi
9f62ea8a1Sandi//fix for Opera XMLHttpRequests
10f62ea8a1Sandiif(!count($_POST) && $HTTP_RAW_POST_DATA){
11f62ea8a1Sandi  parse_str($HTTP_RAW_POST_DATA, $_POST);
12f62ea8a1Sandi}
13f62ea8a1Sandi
14d0a27cb0SAndreas Gohrif(!defined('DOKU_INC')) define('DOKU_INC',dirname(__FILE__).'/../../');
15f62ea8a1Sandirequire_once(DOKU_INC.'inc/init.php');
16f62ea8a1Sandirequire_once(DOKU_INC.'inc/common.php');
17f62ea8a1Sandirequire_once(DOKU_INC.'inc/pageutils.php');
18f62ea8a1Sandirequire_once(DOKU_INC.'inc/auth.php');
198746e727Sandi//close sesseion
208746e727Sandisession_write_close();
21f62ea8a1Sandi
2295657bc6Sandiheader('Content-Type: text/html; charset=utf-8');
2395657bc6Sandi
2495657bc6Sandi
25f62ea8a1Sandi//call the requested function
26da96af35SAndreas Gohrif(isset($_POST['call']))
27850c2e7cSAndreas Gohr  $call = $_POST['call'];
28da96af35SAndreas Gohrelse if(isset($_GET['call']))
29850c2e7cSAndreas Gohr  $call = $_GET['call'];
30da96af35SAndreas Gohrelse
31a06884abSAndreas Gohr  exit;
32850c2e7cSAndreas Gohr
33850c2e7cSAndreas Gohr$callfn = 'ajax_'.$call;
34850c2e7cSAndreas Gohr
35850c2e7cSAndreas Gohrif(function_exists($callfn)){
36850c2e7cSAndreas Gohr  $callfn();
37f62ea8a1Sandi}else{
383cb4b39fSBen Coburn  $evt = new Doku_Event('AJAX_CALL_UNKNOWN', $call);
393cb4b39fSBen Coburn  if ($evt->advise_before()) {
40850c2e7cSAndreas Gohr    print "AJAX call '".htmlspecialchars($call)."' unknown!\n";
41850c2e7cSAndreas Gohr    exit;
423cb4b39fSBen Coburn  }
433cb4b39fSBen Coburn  $evt->advise_after();
443cb4b39fSBen Coburn  unset($evt);
45f62ea8a1Sandi}
46f62ea8a1Sandi
47f62ea8a1Sandi/**
48f62ea8a1Sandi * Searches for matching pagenames
49f62ea8a1Sandi *
50f62ea8a1Sandi * @author Andreas Gohr <andi@splitbrain.org>
51f62ea8a1Sandi */
52f62ea8a1Sandifunction ajax_qsearch(){
53f62ea8a1Sandi  global $conf;
54f62ea8a1Sandi  global $lang;
55f62ea8a1Sandi
56f62ea8a1Sandi  $query = cleanID($_POST['q']);
57da96af35SAndreas Gohr  if(empty($query)) $query = cleanID($_GET['q']);
58f62ea8a1Sandi  if(empty($query)) return;
59f62ea8a1Sandi
60f62ea8a1Sandi  require_once(DOKU_INC.'inc/html.php');
61506fa893SAndreas Gohr  require_once(DOKU_INC.'inc/fulltext.php');
62f62ea8a1Sandi
63f62ea8a1Sandi  $data = array();
64506fa893SAndreas Gohr  $data = ft_pageLookup($query);
65f62ea8a1Sandi
66f62ea8a1Sandi  if(!count($data)) return;
67f62ea8a1Sandi
6896331712SAnika Henke  print '<strong>'.$lang['quickhits'].'</strong>';
69506fa893SAndreas Gohr  print '<ul>';
70506fa893SAndreas Gohr  foreach($data as $id){
71506fa893SAndreas Gohr    print '<li>';
72bd2f6c2fSAndreas Gohr    $ns = getNS($id);
73bd2f6c2fSAndreas Gohr    if($ns){
74bd2f6c2fSAndreas Gohr      $name = shorten(noNS($id), ' ('.$ns.')',30);
75bd2f6c2fSAndreas Gohr    }else{
76bd2f6c2fSAndreas Gohr      $name = $id;
77bd2f6c2fSAndreas Gohr    }
78bd2f6c2fSAndreas Gohr    print html_wikilink(':'.$id,$name);
79506fa893SAndreas Gohr    print '</li>';
80506fa893SAndreas Gohr  }
81506fa893SAndreas Gohr  print '</ul>';
82f62ea8a1Sandi}
83f62ea8a1Sandi
846035eb33SAndreas Gohr/**
85da96af35SAndreas Gohr * Support OpenSearch suggestions
86da96af35SAndreas Gohr *
87da96af35SAndreas Gohr * @link   http://www.opensearch.org/Specifications/OpenSearch/Extensions/Suggestions/1.0
88da96af35SAndreas Gohr * @author Mike Frysinger <vapier@gentoo.org>
89da96af35SAndreas Gohr */
90da96af35SAndreas Gohrfunction ajax_suggestions() {
91da96af35SAndreas Gohr  global $conf;
92da96af35SAndreas Gohr  global $lang;
93da96af35SAndreas Gohr
94da96af35SAndreas Gohr  $query = cleanID($_POST['q']);
95da96af35SAndreas Gohr  if(empty($query)) $query = cleanID($_GET['q']);
96da96af35SAndreas Gohr  if(empty($query)) return;
97da96af35SAndreas Gohr
98da96af35SAndreas Gohr  require_once(DOKU_INC.'inc/html.php');
99da96af35SAndreas Gohr  require_once(DOKU_INC.'inc/fulltext.php');
100da96af35SAndreas Gohr  require_once(DOKU_INC.'inc/JSON.php');
101da96af35SAndreas Gohr
102da96af35SAndreas Gohr  $data = array();
103da96af35SAndreas Gohr  $data = ft_pageLookup($query);
104da96af35SAndreas Gohr  if(!count($data)) return;
105da96af35SAndreas Gohr
106da96af35SAndreas Gohr  // limit results to 15 hits
107da96af35SAndreas Gohr  $data = array_slice($data, 0, 15);
108da96af35SAndreas Gohr  $data = array_map('trim',$data);
109da96af35SAndreas Gohr  $data = array_map('noNS',$data);
110da96af35SAndreas Gohr  $data = array_unique($data);
111da96af35SAndreas Gohr  sort($data);
112da96af35SAndreas Gohr
113da96af35SAndreas Gohr  /* now construct a json */
114da96af35SAndreas Gohr  $suggestions = array(
115da96af35SAndreas Gohr    $query,  // the original query
116da96af35SAndreas Gohr    $data,   // some suggestions
117da96af35SAndreas Gohr    array(), // no description
118da96af35SAndreas Gohr    array()  // no urls
119da96af35SAndreas Gohr  );
120da96af35SAndreas Gohr  $json = new JSON();
121da96af35SAndreas Gohr
122da96af35SAndreas Gohr  header('Content-Type: application/x-suggestions+json');
123da96af35SAndreas Gohr  print $json->encode($suggestions);
124da96af35SAndreas Gohr}
125da96af35SAndreas Gohr
126da96af35SAndreas Gohr/**
127ee4c4a1bSAndreas Gohr * Refresh a page lock and save draft
1286035eb33SAndreas Gohr *
1296035eb33SAndreas Gohr * Andreas Gohr <andi@splitbrain.org>
1306035eb33SAndreas Gohr */
1316035eb33SAndreas Gohrfunction ajax_lock(){
132ee4c4a1bSAndreas Gohr  global $conf;
133ee4c4a1bSAndreas Gohr  global $lang;
1346035eb33SAndreas Gohr  $id = cleanID($_POST['id']);
1356035eb33SAndreas Gohr  if(empty($id)) return;
1366035eb33SAndreas Gohr
1376035eb33SAndreas Gohr  if(!checklock($id)){
1386035eb33SAndreas Gohr    lock($id);
139ee4c4a1bSAndreas Gohr    echo 1;
1406035eb33SAndreas Gohr  }
141ee4c4a1bSAndreas Gohr
142ee4c4a1bSAndreas Gohr  if($conf['usedraft'] && $_POST['wikitext']){
143ee4c4a1bSAndreas Gohr    $client = $_SERVER['REMOTE_USER'];
144ee4c4a1bSAndreas Gohr    if(!$client) $client = clientIP(true);
145ee4c4a1bSAndreas Gohr
146d98d4540SBen Coburn    $draft = array('id'     => $id,
147ee4c4a1bSAndreas Gohr                   'prefix' => $_POST['prefix'],
148ee4c4a1bSAndreas Gohr                   'text'   => $_POST['wikitext'],
149ee4c4a1bSAndreas Gohr                   'suffix' => $_POST['suffix'],
150ee4c4a1bSAndreas Gohr                   'date'   => $_POST['date'],
151ee4c4a1bSAndreas Gohr                   'client' => $client,
152ee4c4a1bSAndreas Gohr                  );
153ee4c4a1bSAndreas Gohr    $cname = getCacheName($draft['client'].$id,'.draft');
154ee4c4a1bSAndreas Gohr    if(io_saveFile($cname,serialize($draft))){
155e656dcd4SAndreas Gohr      echo $lang['draftdate'].' '.strftime($conf['dformat']);
156ee4c4a1bSAndreas Gohr    }
157ee4c4a1bSAndreas Gohr  }
158ee4c4a1bSAndreas Gohr
159ee4c4a1bSAndreas Gohr}
160ee4c4a1bSAndreas Gohr
161ee4c4a1bSAndreas Gohr/**
162ee4c4a1bSAndreas Gohr * Delete a draft
1633df72098SAndreas Gohr *
1643df72098SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org>
165ee4c4a1bSAndreas Gohr */
166ee4c4a1bSAndreas Gohrfunction ajax_draftdel(){
167ee4c4a1bSAndreas Gohr  $id = cleanID($_POST['id']);
168ee4c4a1bSAndreas Gohr  if(empty($id)) return;
169ee4c4a1bSAndreas Gohr
170ee4c4a1bSAndreas Gohr  $client = $_SERVER['REMOTE_USER'];
171ee4c4a1bSAndreas Gohr  if(!$client) $client = clientIP(true);
172ee4c4a1bSAndreas Gohr
173ee4c4a1bSAndreas Gohr  $cname = getCacheName($client.$id,'.draft');
174ee4c4a1bSAndreas Gohr  @unlink($cname);
1756035eb33SAndreas Gohr}
1766035eb33SAndreas Gohr
1773df72098SAndreas Gohr/**
1783df72098SAndreas Gohr * Return subnamespaces for the Mediamanager
179a06884abSAndreas Gohr *
180a06884abSAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org>
1813df72098SAndreas Gohr */
1823df72098SAndreas Gohrfunction ajax_medians(){
1833df72098SAndreas Gohr  global $conf;
1843df72098SAndreas Gohr  require_once(DOKU_INC.'inc/search.php');
1853df72098SAndreas Gohr  require_once(DOKU_INC.'inc/media.php');
1863df72098SAndreas Gohr
1873df72098SAndreas Gohr  // wanted namespace
1883df72098SAndreas Gohr  $ns  = cleanID($_POST['ns']);
1893df72098SAndreas Gohr  $dir  = utf8_encodeFN(str_replace(':','/',$ns));
1903df72098SAndreas Gohr
1913df72098SAndreas Gohr  $lvl = count(explode(':',$ns));
1923df72098SAndreas Gohr
1933df72098SAndreas Gohr  $data = array();
194ee7b5a62SAndreas Gohr  search($data,$conf['mediadir'],'search_index',array('nofiles' => true),$dir);
1953df72098SAndreas Gohr  foreach($data as $item){
1963df72098SAndreas Gohr    $item['level'] = $lvl+1;
1973df72098SAndreas Gohr    echo media_nstree_li($item);
1983df72098SAndreas Gohr    echo media_nstree_item($item);
1999f420521SAndreas Gohr    echo '</li>';
2003df72098SAndreas Gohr  }
2013df72098SAndreas Gohr}
2023df72098SAndreas Gohr
2033df72098SAndreas Gohr/**
204a06884abSAndreas Gohr * Return list of files for the Mediamanager
205a06884abSAndreas Gohr *
206a06884abSAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org>
2073df72098SAndreas Gohr */
2083df72098SAndreas Gohrfunction ajax_medialist(){
2093df72098SAndreas Gohr  global $conf;
2103df72098SAndreas Gohr  require_once(DOKU_INC.'inc/media.php');
2113df72098SAndreas Gohr
2123df72098SAndreas Gohr  media_filelist($_POST['ns']);
2133df72098SAndreas Gohr}
2143df72098SAndreas Gohr
215a06884abSAndreas Gohr/**
216a06884abSAndreas Gohr * Return sub index for index view
217a06884abSAndreas Gohr *
218a06884abSAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org>
219a06884abSAndreas Gohr */
220a06884abSAndreas Gohrfunction ajax_index(){
221a06884abSAndreas Gohr  global $conf;
222a06884abSAndreas Gohr  require_once(DOKU_INC.'inc/search.php');
223a06884abSAndreas Gohr  require_once(DOKU_INC.'inc/html.php');
224a06884abSAndreas Gohr
225a06884abSAndreas Gohr  // wanted namespace
226a06884abSAndreas Gohr  $ns  = cleanID($_POST['idx']);
227a06884abSAndreas Gohr  $dir  = utf8_encodeFN(str_replace(':','/',$ns));
228a06884abSAndreas Gohr
229a06884abSAndreas Gohr  $lvl = count(explode(':',$ns));
230a06884abSAndreas Gohr
231a06884abSAndreas Gohr  $data = array();
232a06884abSAndreas Gohr  search($data,$conf['datadir'],'search_index',array('ns' => $ns),$dir);
233a06884abSAndreas Gohr  foreach($data as $item){
234a06884abSAndreas Gohr    $item['level'] = $lvl+1;
235a06884abSAndreas Gohr    echo html_li_index($item);
236a06884abSAndreas Gohr    echo '<div class="li">';
237a06884abSAndreas Gohr    echo html_list_index($item);
238a06884abSAndreas Gohr    echo '</div>';
239a06884abSAndreas Gohr    echo '</li>';
240a06884abSAndreas Gohr  }
241a06884abSAndreas Gohr}
242a06884abSAndreas Gohr
243*56dfcc12SAndreas Gohr/**
244*56dfcc12SAndreas Gohr * List matching namespaces and pages for the link wizard
245*56dfcc12SAndreas Gohr */
246*56dfcc12SAndreas Gohrfunction ajax_linkwiz(){
247*56dfcc12SAndreas Gohr  global $conf;
248*56dfcc12SAndreas Gohr  global $lang;
249*56dfcc12SAndreas Gohr  require_once(DOKU_INC.'inc/html.php');
250*56dfcc12SAndreas Gohr
251*56dfcc12SAndreas Gohr  $q  = ltrim($_POST['q'],':');
252*56dfcc12SAndreas Gohr  $id = noNS($q);
253*56dfcc12SAndreas Gohr  $ns = getNS($q);
254*56dfcc12SAndreas Gohr
255*56dfcc12SAndreas Gohr  $ns = cleanID($ns);
256*56dfcc12SAndreas Gohr  $id = cleanID($id);
257*56dfcc12SAndreas Gohr
258*56dfcc12SAndreas Gohr  $nsd  = utf8_encodeFN(str_replace(':','/',$ns));
259*56dfcc12SAndreas Gohr  $idd  = utf8_encodeFN(str_replace(':','/',$id));
260*56dfcc12SAndreas Gohr
261*56dfcc12SAndreas Gohr  $data = array();
262*56dfcc12SAndreas Gohr  if($q && !$ns){
263*56dfcc12SAndreas Gohr
264*56dfcc12SAndreas Gohr    // use index to lookup matching pages
265*56dfcc12SAndreas Gohr    require_once(DOKU_INC.'inc/fulltext.php');
266*56dfcc12SAndreas Gohr    require_once(DOKU_INC.'inc/parserutils.php');
267*56dfcc12SAndreas Gohr    $pages = array();
268*56dfcc12SAndreas Gohr    $pages = ft_pageLookup($id,false);
269*56dfcc12SAndreas Gohr
270*56dfcc12SAndreas Gohr    // result contains matches in pages and namespaces
271*56dfcc12SAndreas Gohr    // we now extract the matching namespaces to show
272*56dfcc12SAndreas Gohr    // them seperately
273*56dfcc12SAndreas Gohr    $dirs  = array();
274*56dfcc12SAndreas Gohr    $count = count($pages);
275*56dfcc12SAndreas Gohr    for($i=0; $i<$count; $i++){
276*56dfcc12SAndreas Gohr      if(strpos(noNS($pages[$i]),$id) === false){
277*56dfcc12SAndreas Gohr        // match was in the namespace
278*56dfcc12SAndreas Gohr        $dirs[getNS($pages[$i])] = 1; // assoc array avoids dupes
279*56dfcc12SAndreas Gohr      }else{
280*56dfcc12SAndreas Gohr        // it is a matching page, add it to the result
281*56dfcc12SAndreas Gohr        $data[] = array(
282*56dfcc12SAndreas Gohr          'id'    => $pages[$i],
283*56dfcc12SAndreas Gohr          'title' => p_get_first_heading($pages[$i],false),
284*56dfcc12SAndreas Gohr          'type'  => 'f',
285*56dfcc12SAndreas Gohr        );
286*56dfcc12SAndreas Gohr      }
287*56dfcc12SAndreas Gohr      unset($pages[$i]);
288*56dfcc12SAndreas Gohr    }
289*56dfcc12SAndreas Gohr    foreach($dirs as $dir => $junk){
290*56dfcc12SAndreas Gohr      $data[] = array(
291*56dfcc12SAndreas Gohr        'id'   => $dir,
292*56dfcc12SAndreas Gohr        'type' => 'd',
293*56dfcc12SAndreas Gohr      );
294*56dfcc12SAndreas Gohr    }
295*56dfcc12SAndreas Gohr
296*56dfcc12SAndreas Gohr  }else{
297*56dfcc12SAndreas Gohr
298*56dfcc12SAndreas Gohr    require_once(DOKU_INC.'inc/search.php');
299*56dfcc12SAndreas Gohr    $opts = array(
300*56dfcc12SAndreas Gohr      'depth' => 1,
301*56dfcc12SAndreas Gohr      'listfiles' => true,
302*56dfcc12SAndreas Gohr      'listdirs'  => true,
303*56dfcc12SAndreas Gohr      'pagesonly' => true,
304*56dfcc12SAndreas Gohr      'firsthead' => true,
305*56dfcc12SAndreas Gohr    );
306*56dfcc12SAndreas Gohr    if($id) $opts['filematch'] = '^.*\/'.$id;
307*56dfcc12SAndreas Gohr    if($id) $opts['dirmatch']  = '^.*\/'.$id;
308*56dfcc12SAndreas Gohr    search($data,$conf['datadir'],'search_universal',$opts,$nsd);
309*56dfcc12SAndreas Gohr
310*56dfcc12SAndreas Gohr    // add back to upper
311*56dfcc12SAndreas Gohr    if($ns){
312*56dfcc12SAndreas Gohr        array_unshift($data,array(
313*56dfcc12SAndreas Gohr            'id'   => getNS($ns),
314*56dfcc12SAndreas Gohr            'type' => 'u',
315*56dfcc12SAndreas Gohr        ));
316*56dfcc12SAndreas Gohr    }
317*56dfcc12SAndreas Gohr  }
318*56dfcc12SAndreas Gohr
319*56dfcc12SAndreas Gohr  // fixme sort results in a useful way ?
320*56dfcc12SAndreas Gohr
321*56dfcc12SAndreas Gohr  if(!count($data)){
322*56dfcc12SAndreas Gohr    echo $lang['nothingfound'];
323*56dfcc12SAndreas Gohr    exit;
324*56dfcc12SAndreas Gohr  }
325*56dfcc12SAndreas Gohr
326*56dfcc12SAndreas Gohr  // output the found data
327*56dfcc12SAndreas Gohr  $even = 1;
328*56dfcc12SAndreas Gohr  foreach($data as $item){
329*56dfcc12SAndreas Gohr    $even *= -1; //zebra
330*56dfcc12SAndreas Gohr
331*56dfcc12SAndreas Gohr    if(($item['type'] == 'd' || $item['type'] == 'u') && $item['id']) $item['id'] .= ':';
332*56dfcc12SAndreas Gohr    $link = wl($item['id']);
333*56dfcc12SAndreas Gohr
334*56dfcc12SAndreas Gohr    echo '<div class="'.(($even > 0)?'even':'odd').' type_'.$item['type'].'">';
335*56dfcc12SAndreas Gohr
336*56dfcc12SAndreas Gohr
337*56dfcc12SAndreas Gohr    if($item['type'] == 'u'){
338*56dfcc12SAndreas Gohr        $name = 'back to upper';
339*56dfcc12SAndreas Gohr    }else{
340*56dfcc12SAndreas Gohr        $name = htmlspecialchars($item['id']);
341*56dfcc12SAndreas Gohr    }
342*56dfcc12SAndreas Gohr
343*56dfcc12SAndreas Gohr    echo '<a href="'.$link.'" title="'.htmlspecialchars($item['id']).'" class="wikilink1">'.$name.'</a>';
344*56dfcc12SAndreas Gohr
345*56dfcc12SAndreas Gohr    if($item['title']){
346*56dfcc12SAndreas Gohr      echo '<span>'.htmlspecialchars($item['title']).'</span>';
347*56dfcc12SAndreas Gohr    }
348*56dfcc12SAndreas Gohr    echo '</div>';
349*56dfcc12SAndreas Gohr  }
350*56dfcc12SAndreas Gohr
351*56dfcc12SAndreas Gohr}
352*56dfcc12SAndreas Gohr
353dc57ef04Sandi//Setup VIM: ex: et ts=2 enc=utf-8 :
354