xref: /dokuwiki/lib/exe/ajax.php (revision 286bea67acbd887f06b3f8a613e7ebdf55c3298b)
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');
16require_once(DOKU_INC.'inc/common.php');
17require_once(DOKU_INC.'inc/pageutils.php');
18require_once(DOKU_INC.'inc/auth.php');
19//close sesseion
20session_write_close();
21
22header('Content-Type: text/html; charset=utf-8');
23
24
25//call the requested function
26if(isset($_POST['call']))
27  $call = 'ajax_'.$_POST['call'];
28else if(isset($_GET['call']))
29  $call = 'ajax_'.$_GET['call'];
30else
31  exit;
32if(function_exists($call)){
33  $call();
34}else{
35  $call = $_POST['call'];
36  $evt = new Doku_Event('AJAX_CALL_UNKNOWN', $call);
37  if ($evt->advise_before()) {
38    print "AJAX call '".htmlspecialchars($_POST['call'])."' unknown!\n";
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  require_once(DOKU_INC.'inc/html.php');
58  require_once(DOKU_INC.'inc/fulltext.php');
59
60  $data = array();
61  $data = ft_pageLookup($query);
62
63  if(!count($data)) return;
64
65  print '<strong>'.$lang['quickhits'].'</strong>';
66  print '<ul>';
67  foreach($data as $id){
68    print '<li>';
69    $ns = getNS($id);
70    if($ns){
71      $name = shorten(noNS($id), ' ('.$ns.')',30);
72    }else{
73      $name = $id;
74    }
75    print html_wikilink(':'.$id,$name);
76    print '</li>';
77  }
78  print '</ul>';
79}
80
81/**
82 * Support OpenSearch suggestions
83 *
84 * @link   http://www.opensearch.org/Specifications/OpenSearch/Extensions/Suggestions/1.0
85 * @author Mike Frysinger <vapier@gentoo.org>
86 */
87function ajax_suggestions() {
88  global $conf;
89  global $lang;
90
91  $query = cleanID($_POST['q']);
92  if(empty($query)) $query = cleanID($_GET['q']);
93  if(empty($query)) return;
94
95  require_once(DOKU_INC.'inc/html.php');
96  require_once(DOKU_INC.'inc/fulltext.php');
97  require_once(DOKU_INC.'inc/JSON.php');
98
99  $data = array();
100  $data = ft_pageLookup($query);
101  if(!count($data)) return;
102
103  // limit results to 15 hits
104  $data = array_slice($data, 0, 15);
105  $data = array_map('trim',$data);
106  $data = array_map('noNS',$data);
107  $data = array_unique($data);
108  sort($data);
109
110  /* now construct a json */
111  $suggestions = array(
112    $query,  // the original query
113    $data,   // some suggestions
114    array(), // no description
115    array()  // no urls
116  );
117  $json = new JSON();
118
119  header('Content-Type: application/x-suggestions+json');
120  print $json->encode($suggestions);
121}
122
123/**
124 * Refresh a page lock and save draft
125 *
126 * Andreas Gohr <andi@splitbrain.org>
127 */
128function ajax_lock(){
129  global $conf;
130  global $lang;
131  $id = cleanID($_POST['id']);
132  if(empty($id)) return;
133
134  if(!checklock($id)){
135    lock($id);
136    echo 1;
137  }
138
139  if($conf['usedraft'] && $_POST['wikitext']){
140    $client = $_SERVER['REMOTE_USER'];
141    if(!$client) $client = clientIP(true);
142
143    $draft = array('id'     => $id,
144                   'prefix' => $_POST['prefix'],
145                   'text'   => $_POST['wikitext'],
146                   'suffix' => $_POST['suffix'],
147                   'date'   => $_POST['date'],
148                   'client' => $client,
149                  );
150    $cname = getCacheName($draft['client'].$id,'.draft');
151    if(io_saveFile($cname,serialize($draft))){
152      echo $lang['draftdate'].' '.strftime($conf['dformat']);
153    }
154  }
155
156}
157
158/**
159 * Delete a draft
160 *
161 * @author Andreas Gohr <andi@splitbrain.org>
162 */
163function ajax_draftdel(){
164  $id = cleanID($_POST['id']);
165  if(empty($id)) return;
166
167  $client = $_SERVER['REMOTE_USER'];
168  if(!$client) $client = clientIP(true);
169
170  $cname = getCacheName($client.$id,'.draft');
171  @unlink($cname);
172}
173
174/**
175 * Return subnamespaces for the Mediamanager
176 *
177 * @author Andreas Gohr <andi@splitbrain.org>
178 */
179function ajax_medians(){
180  global $conf;
181  require_once(DOKU_INC.'inc/search.php');
182  require_once(DOKU_INC.'inc/media.php');
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($data as $item){
193    $item['level'] = $lvl+1;
194    echo media_nstree_li($item);
195    echo media_nstree_item($item);
196    echo '</li>';
197  }
198}
199
200/**
201 * Return list of files for the Mediamanager
202 *
203 * @author Andreas Gohr <andi@splitbrain.org>
204 */
205function ajax_medialist(){
206  global $conf;
207  require_once(DOKU_INC.'inc/media.php');
208
209  media_filelist($_POST['ns']);
210}
211
212/**
213 * Return sub index for index view
214 *
215 * @author Andreas Gohr <andi@splitbrain.org>
216 */
217function ajax_index(){
218  global $conf;
219  require_once(DOKU_INC.'inc/search.php');
220  require_once(DOKU_INC.'inc/html.php');
221
222  // wanted namespace
223  $ns  = cleanID($_POST['idx']);
224  $dir  = utf8_encodeFN(str_replace(':','/',$ns));
225
226  $lvl = count(explode(':',$ns));
227
228  $data = array();
229  search($data,$conf['datadir'],'search_index',array('ns' => $ns),$dir);
230  foreach($data as $item){
231    $item['level'] = $lvl+1;
232    echo html_li_index($item);
233    echo '<div class="li">';
234    echo html_list_index($item);
235    echo '</div>';
236    echo '</li>';
237  }
238}
239
240//Setup VIM: ex: et ts=2 enc=utf-8 :
241?>
242