xref: /dokuwiki/lib/exe/ajax.php (revision 24b3cb1ac0784ffa2acabf021fdd06c6d49cc7b1)
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');
16*24b3cb1aSAndreas Gohr//close session
178746e727Sandisession_write_close();
18f62ea8a1Sandi
1995657bc6Sandiheader('Content-Type: text/html; charset=utf-8');
2095657bc6Sandi
2195657bc6Sandi
22f62ea8a1Sandi//call the requested function
23da96af35SAndreas Gohrif(isset($_POST['call']))
24850c2e7cSAndreas Gohr  $call = $_POST['call'];
25da96af35SAndreas Gohrelse if(isset($_GET['call']))
26850c2e7cSAndreas Gohr  $call = $_GET['call'];
27da96af35SAndreas Gohrelse
28a06884abSAndreas Gohr  exit;
29850c2e7cSAndreas Gohr
30850c2e7cSAndreas Gohr$callfn = 'ajax_'.$call;
31850c2e7cSAndreas Gohr
32850c2e7cSAndreas Gohrif(function_exists($callfn)){
33850c2e7cSAndreas Gohr  $callfn();
34f62ea8a1Sandi}else{
353cb4b39fSBen Coburn  $evt = new Doku_Event('AJAX_CALL_UNKNOWN', $call);
363cb4b39fSBen Coburn  if ($evt->advise_before()) {
37850c2e7cSAndreas Gohr    print "AJAX call '".htmlspecialchars($call)."' unknown!\n";
38850c2e7cSAndreas Gohr    exit;
393cb4b39fSBen Coburn  }
403cb4b39fSBen Coburn  $evt->advise_after();
413cb4b39fSBen Coburn  unset($evt);
42f62ea8a1Sandi}
43f62ea8a1Sandi
44f62ea8a1Sandi/**
45f62ea8a1Sandi * Searches for matching pagenames
46f62ea8a1Sandi *
47f62ea8a1Sandi * @author Andreas Gohr <andi@splitbrain.org>
48f62ea8a1Sandi */
49f62ea8a1Sandifunction ajax_qsearch(){
50f62ea8a1Sandi  global $conf;
51f62ea8a1Sandi  global $lang;
52f62ea8a1Sandi
53f62ea8a1Sandi  $query = cleanID($_POST['q']);
54da96af35SAndreas Gohr  if(empty($query)) $query = cleanID($_GET['q']);
55f62ea8a1Sandi  if(empty($query)) return;
56f62ea8a1Sandi
57f62ea8a1Sandi  $data = array();
58506fa893SAndreas Gohr  $data = ft_pageLookup($query);
59f62ea8a1Sandi
60f62ea8a1Sandi  if(!count($data)) return;
61f62ea8a1Sandi
6296331712SAnika Henke  print '<strong>'.$lang['quickhits'].'</strong>';
63506fa893SAndreas Gohr  print '<ul>';
64506fa893SAndreas Gohr  foreach($data as $id){
65506fa893SAndreas Gohr    print '<li>';
66bd2f6c2fSAndreas Gohr    $ns = getNS($id);
67bd2f6c2fSAndreas Gohr    if($ns){
68bd2f6c2fSAndreas Gohr      $name = shorten(noNS($id), ' ('.$ns.')',30);
69bd2f6c2fSAndreas Gohr    }else{
70bd2f6c2fSAndreas Gohr      $name = $id;
71bd2f6c2fSAndreas Gohr    }
72bd2f6c2fSAndreas Gohr    print html_wikilink(':'.$id,$name);
73506fa893SAndreas Gohr    print '</li>';
74506fa893SAndreas Gohr  }
75506fa893SAndreas Gohr  print '</ul>';
76f62ea8a1Sandi}
77f62ea8a1Sandi
786035eb33SAndreas Gohr/**
79da96af35SAndreas Gohr * Support OpenSearch suggestions
80da96af35SAndreas Gohr *
81da96af35SAndreas Gohr * @link   http://www.opensearch.org/Specifications/OpenSearch/Extensions/Suggestions/1.0
82da96af35SAndreas Gohr * @author Mike Frysinger <vapier@gentoo.org>
83da96af35SAndreas Gohr */
84da96af35SAndreas Gohrfunction ajax_suggestions() {
85da96af35SAndreas Gohr  global $conf;
86da96af35SAndreas Gohr  global $lang;
87da96af35SAndreas Gohr
88da96af35SAndreas Gohr  $query = cleanID($_POST['q']);
89da96af35SAndreas Gohr  if(empty($query)) $query = cleanID($_GET['q']);
90da96af35SAndreas Gohr  if(empty($query)) return;
91da96af35SAndreas Gohr
92da96af35SAndreas Gohr  $data = array();
93da96af35SAndreas Gohr  $data = ft_pageLookup($query);
94da96af35SAndreas Gohr  if(!count($data)) return;
95da96af35SAndreas Gohr
96da96af35SAndreas Gohr  // limit results to 15 hits
97da96af35SAndreas Gohr  $data = array_slice($data, 0, 15);
98da96af35SAndreas Gohr  $data = array_map('trim',$data);
99da96af35SAndreas Gohr  $data = array_map('noNS',$data);
100da96af35SAndreas Gohr  $data = array_unique($data);
101da96af35SAndreas Gohr  sort($data);
102da96af35SAndreas Gohr
103da96af35SAndreas Gohr  /* now construct a json */
104da96af35SAndreas Gohr  $suggestions = array(
105da96af35SAndreas Gohr    $query,  // the original query
106da96af35SAndreas Gohr    $data,   // some suggestions
107da96af35SAndreas Gohr    array(), // no description
108da96af35SAndreas Gohr    array()  // no urls
109da96af35SAndreas Gohr  );
110da96af35SAndreas Gohr  $json = new JSON();
111da96af35SAndreas Gohr
112da96af35SAndreas Gohr  header('Content-Type: application/x-suggestions+json');
113da96af35SAndreas Gohr  print $json->encode($suggestions);
114da96af35SAndreas Gohr}
115da96af35SAndreas Gohr
116da96af35SAndreas Gohr/**
117ee4c4a1bSAndreas Gohr * Refresh a page lock and save draft
1186035eb33SAndreas Gohr *
1196035eb33SAndreas Gohr * Andreas Gohr <andi@splitbrain.org>
1206035eb33SAndreas Gohr */
1216035eb33SAndreas Gohrfunction ajax_lock(){
122ee4c4a1bSAndreas Gohr  global $conf;
123ee4c4a1bSAndreas Gohr  global $lang;
1246035eb33SAndreas Gohr  $id = cleanID($_POST['id']);
1256035eb33SAndreas Gohr  if(empty($id)) return;
1266035eb33SAndreas Gohr
1276035eb33SAndreas Gohr  if(!checklock($id)){
1286035eb33SAndreas Gohr    lock($id);
129ee4c4a1bSAndreas Gohr    echo 1;
1306035eb33SAndreas Gohr  }
131ee4c4a1bSAndreas Gohr
132ee4c4a1bSAndreas Gohr  if($conf['usedraft'] && $_POST['wikitext']){
133ee4c4a1bSAndreas Gohr    $client = $_SERVER['REMOTE_USER'];
134ee4c4a1bSAndreas Gohr    if(!$client) $client = clientIP(true);
135ee4c4a1bSAndreas Gohr
136d98d4540SBen Coburn    $draft = array('id'     => $id,
137ee4c4a1bSAndreas Gohr                   'prefix' => $_POST['prefix'],
138ee4c4a1bSAndreas Gohr                   'text'   => $_POST['wikitext'],
139ee4c4a1bSAndreas Gohr                   'suffix' => $_POST['suffix'],
140ee4c4a1bSAndreas Gohr                   'date'   => $_POST['date'],
141ee4c4a1bSAndreas Gohr                   'client' => $client,
142ee4c4a1bSAndreas Gohr                  );
143ee4c4a1bSAndreas Gohr    $cname = getCacheName($draft['client'].$id,'.draft');
144ee4c4a1bSAndreas Gohr    if(io_saveFile($cname,serialize($draft))){
145f2263577SAndreas Gohr      echo $lang['draftdate'].' '.dformat();
146ee4c4a1bSAndreas Gohr    }
147ee4c4a1bSAndreas Gohr  }
148ee4c4a1bSAndreas Gohr
149ee4c4a1bSAndreas Gohr}
150ee4c4a1bSAndreas Gohr
151ee4c4a1bSAndreas Gohr/**
152ee4c4a1bSAndreas Gohr * Delete a draft
1533df72098SAndreas Gohr *
1543df72098SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org>
155ee4c4a1bSAndreas Gohr */
156ee4c4a1bSAndreas Gohrfunction ajax_draftdel(){
157ee4c4a1bSAndreas Gohr  $id = cleanID($_POST['id']);
158ee4c4a1bSAndreas Gohr  if(empty($id)) return;
159ee4c4a1bSAndreas Gohr
160ee4c4a1bSAndreas Gohr  $client = $_SERVER['REMOTE_USER'];
161ee4c4a1bSAndreas Gohr  if(!$client) $client = clientIP(true);
162ee4c4a1bSAndreas Gohr
163ee4c4a1bSAndreas Gohr  $cname = getCacheName($client.$id,'.draft');
164ee4c4a1bSAndreas Gohr  @unlink($cname);
1656035eb33SAndreas Gohr}
1666035eb33SAndreas Gohr
1673df72098SAndreas Gohr/**
1683df72098SAndreas Gohr * Return subnamespaces for the Mediamanager
169a06884abSAndreas Gohr *
170a06884abSAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org>
1713df72098SAndreas Gohr */
1723df72098SAndreas Gohrfunction ajax_medians(){
1733df72098SAndreas Gohr  global $conf;
1743df72098SAndreas Gohr
1753df72098SAndreas Gohr  // wanted namespace
1763df72098SAndreas Gohr  $ns  = cleanID($_POST['ns']);
1773df72098SAndreas Gohr  $dir  = utf8_encodeFN(str_replace(':','/',$ns));
1783df72098SAndreas Gohr
1793df72098SAndreas Gohr  $lvl = count(explode(':',$ns));
1803df72098SAndreas Gohr
1813df72098SAndreas Gohr  $data = array();
182ee7b5a62SAndreas Gohr  search($data,$conf['mediadir'],'search_index',array('nofiles' => true),$dir);
1833df72098SAndreas Gohr  foreach($data as $item){
1843df72098SAndreas Gohr    $item['level'] = $lvl+1;
1853df72098SAndreas Gohr    echo media_nstree_li($item);
1863df72098SAndreas Gohr    echo media_nstree_item($item);
1879f420521SAndreas Gohr    echo '</li>';
1883df72098SAndreas Gohr  }
1893df72098SAndreas Gohr}
1903df72098SAndreas Gohr
1913df72098SAndreas Gohr/**
192a06884abSAndreas Gohr * Return list of files for the Mediamanager
193a06884abSAndreas Gohr *
194a06884abSAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org>
1953df72098SAndreas Gohr */
1963df72098SAndreas Gohrfunction ajax_medialist(){
1973df72098SAndreas Gohr  global $conf;
198c182313eSAndreas Gohr  global $NS;
1993df72098SAndreas Gohr
200c182313eSAndreas Gohr  $NS = $_POST['ns'];
201c182313eSAndreas Gohr  tpl_mediaContent(true);
2023df72098SAndreas Gohr}
2033df72098SAndreas Gohr
204a06884abSAndreas Gohr/**
205bf1f3ac4Ssarnowski * Return list of search result for the Mediamanager
206bf1f3ac4Ssarnowski *
207bf1f3ac4Ssarnowski * @author Tobias Sarnowski <sarnowski@cosmocode.de>
208bf1f3ac4Ssarnowski */
209bf1f3ac4Ssarnowskifunction ajax_mediasearchlist(){
210bf1f3ac4Ssarnowski  global $conf;
211bf1f3ac4Ssarnowski
212bf1f3ac4Ssarnowski  media_searchlist($_POST['ns']);
213bf1f3ac4Ssarnowski}
214bf1f3ac4Ssarnowski
215bf1f3ac4Ssarnowski/**
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
223a06884abSAndreas Gohr  // wanted namespace
224a06884abSAndreas Gohr  $ns  = cleanID($_POST['idx']);
225a06884abSAndreas Gohr  $dir  = utf8_encodeFN(str_replace(':','/',$ns));
226a06884abSAndreas Gohr
227a06884abSAndreas Gohr  $lvl = count(explode(':',$ns));
228a06884abSAndreas Gohr
229a06884abSAndreas Gohr  $data = array();
230a06884abSAndreas Gohr  search($data,$conf['datadir'],'search_index',array('ns' => $ns),$dir);
231a06884abSAndreas Gohr  foreach($data as $item){
232a06884abSAndreas Gohr    $item['level'] = $lvl+1;
233a06884abSAndreas Gohr    echo html_li_index($item);
234a06884abSAndreas Gohr    echo '<div class="li">';
235a06884abSAndreas Gohr    echo html_list_index($item);
236a06884abSAndreas Gohr    echo '</div>';
237a06884abSAndreas Gohr    echo '</li>';
238a06884abSAndreas Gohr  }
239a06884abSAndreas Gohr}
240a06884abSAndreas Gohr
24156dfcc12SAndreas Gohr/**
24256dfcc12SAndreas Gohr * List matching namespaces and pages for the link wizard
2433e23cbfdSAndreas Gohr *
2443e23cbfdSAndreas Gohr * @author Andreas Gohr <gohr@cosmocode.de>
24556dfcc12SAndreas Gohr */
24656dfcc12SAndreas Gohrfunction ajax_linkwiz(){
24756dfcc12SAndreas Gohr  global $conf;
24856dfcc12SAndreas Gohr  global $lang;
24956dfcc12SAndreas Gohr
25056dfcc12SAndreas Gohr  $q  = ltrim($_POST['q'],':');
25156dfcc12SAndreas Gohr  $id = noNS($q);
25256dfcc12SAndreas Gohr  $ns = getNS($q);
25356dfcc12SAndreas Gohr
25456dfcc12SAndreas Gohr  $ns = cleanID($ns);
25556dfcc12SAndreas Gohr  $id = cleanID($id);
25656dfcc12SAndreas Gohr
25756dfcc12SAndreas Gohr  $nsd  = utf8_encodeFN(str_replace(':','/',$ns));
25856dfcc12SAndreas Gohr  $idd  = utf8_encodeFN(str_replace(':','/',$id));
25956dfcc12SAndreas Gohr
26056dfcc12SAndreas Gohr  $data = array();
26156dfcc12SAndreas Gohr  if($q && !$ns){
26256dfcc12SAndreas Gohr
26356dfcc12SAndreas Gohr    // use index to lookup matching pages
26456dfcc12SAndreas Gohr    $pages = array();
26556dfcc12SAndreas Gohr    $pages = ft_pageLookup($id,false);
26656dfcc12SAndreas Gohr
26756dfcc12SAndreas Gohr    // result contains matches in pages and namespaces
26856dfcc12SAndreas Gohr    // we now extract the matching namespaces to show
26956dfcc12SAndreas Gohr    // them seperately
27056dfcc12SAndreas Gohr    $dirs  = array();
27156dfcc12SAndreas Gohr    $count = count($pages);
27256dfcc12SAndreas Gohr    for($i=0; $i<$count; $i++){
27356dfcc12SAndreas Gohr      if(strpos(noNS($pages[$i]),$id) === false){
27456dfcc12SAndreas Gohr        // match was in the namespace
27556dfcc12SAndreas Gohr        $dirs[getNS($pages[$i])] = 1; // assoc array avoids dupes
27656dfcc12SAndreas Gohr      }else{
27756dfcc12SAndreas Gohr        // it is a matching page, add it to the result
27856dfcc12SAndreas Gohr        $data[] = array(
27956dfcc12SAndreas Gohr          'id'    => $pages[$i],
28056dfcc12SAndreas Gohr          'title' => p_get_first_heading($pages[$i],false),
28156dfcc12SAndreas Gohr          'type'  => 'f',
28256dfcc12SAndreas Gohr        );
28356dfcc12SAndreas Gohr      }
28456dfcc12SAndreas Gohr      unset($pages[$i]);
28556dfcc12SAndreas Gohr    }
28656dfcc12SAndreas Gohr    foreach($dirs as $dir => $junk){
28756dfcc12SAndreas Gohr      $data[] = array(
28856dfcc12SAndreas Gohr        'id'   => $dir,
28956dfcc12SAndreas Gohr        'type' => 'd',
29056dfcc12SAndreas Gohr      );
29156dfcc12SAndreas Gohr    }
29256dfcc12SAndreas Gohr
29356dfcc12SAndreas Gohr  }else{
29456dfcc12SAndreas Gohr
29556dfcc12SAndreas Gohr    $opts = array(
29656dfcc12SAndreas Gohr      'depth' => 1,
29756dfcc12SAndreas Gohr      'listfiles' => true,
29856dfcc12SAndreas Gohr      'listdirs'  => true,
29956dfcc12SAndreas Gohr      'pagesonly' => true,
30056dfcc12SAndreas Gohr      'firsthead' => true,
30156dfcc12SAndreas Gohr    );
30256dfcc12SAndreas Gohr    if($id) $opts['filematch'] = '^.*\/'.$id;
30356dfcc12SAndreas Gohr    if($id) $opts['dirmatch']  = '^.*\/'.$id;
30456dfcc12SAndreas Gohr    search($data,$conf['datadir'],'search_universal',$opts,$nsd);
30556dfcc12SAndreas Gohr
30656dfcc12SAndreas Gohr    // add back to upper
30756dfcc12SAndreas Gohr    if($ns){
30856dfcc12SAndreas Gohr        array_unshift($data,array(
30956dfcc12SAndreas Gohr            'id'   => getNS($ns),
31056dfcc12SAndreas Gohr            'type' => 'u',
31156dfcc12SAndreas Gohr        ));
31256dfcc12SAndreas Gohr    }
31356dfcc12SAndreas Gohr  }
31456dfcc12SAndreas Gohr
31556dfcc12SAndreas Gohr  // fixme sort results in a useful way ?
31656dfcc12SAndreas Gohr
31756dfcc12SAndreas Gohr  if(!count($data)){
31856dfcc12SAndreas Gohr    echo $lang['nothingfound'];
31956dfcc12SAndreas Gohr    exit;
32056dfcc12SAndreas Gohr  }
32156dfcc12SAndreas Gohr
32256dfcc12SAndreas Gohr  // output the found data
32356dfcc12SAndreas Gohr  $even = 1;
32456dfcc12SAndreas Gohr  foreach($data as $item){
32556dfcc12SAndreas Gohr    $even *= -1; //zebra
32656dfcc12SAndreas Gohr
32756dfcc12SAndreas Gohr    if(($item['type'] == 'd' || $item['type'] == 'u') && $item['id']) $item['id'] .= ':';
32856dfcc12SAndreas Gohr    $link = wl($item['id']);
32956dfcc12SAndreas Gohr
33056dfcc12SAndreas Gohr    echo '<div class="'.(($even > 0)?'even':'odd').' type_'.$item['type'].'">';
33156dfcc12SAndreas Gohr
33256dfcc12SAndreas Gohr
33356dfcc12SAndreas Gohr    if($item['type'] == 'u'){
3343e23cbfdSAndreas Gohr        $name = $lang['upperns'];
33556dfcc12SAndreas Gohr    }else{
33656dfcc12SAndreas Gohr        $name = htmlspecialchars($item['id']);
33756dfcc12SAndreas Gohr    }
33856dfcc12SAndreas Gohr
33956dfcc12SAndreas Gohr    echo '<a href="'.$link.'" title="'.htmlspecialchars($item['id']).'" class="wikilink1">'.$name.'</a>';
34056dfcc12SAndreas Gohr
34156dfcc12SAndreas Gohr    if($item['title']){
34256dfcc12SAndreas Gohr      echo '<span>'.htmlspecialchars($item['title']).'</span>';
34356dfcc12SAndreas Gohr    }
34456dfcc12SAndreas Gohr    echo '</div>';
34556dfcc12SAndreas Gohr  }
34656dfcc12SAndreas Gohr
34756dfcc12SAndreas Gohr}
34856dfcc12SAndreas Gohr
349dc57ef04Sandi//Setup VIM: ex: et ts=2 enc=utf-8 :
350