xref: /dokuwiki/lib/exe/ajax.php (revision 40eaacde1105b473321e61d63421d059aaffce1b)
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    print html_wikilink(':'.$id);
70    print '</li>';
71  }
72  print '</ul>';
73}
74
75/**
76 * Support OpenSearch suggestions
77 *
78 * @link   http://www.opensearch.org/Specifications/OpenSearch/Extensions/Suggestions/1.0
79 * @author Mike Frysinger <vapier@gentoo.org>
80 */
81function ajax_suggestions() {
82  global $conf;
83  global $lang;
84
85  $query = cleanID($_POST['q']);
86  if(empty($query)) $query = cleanID($_GET['q']);
87  if(empty($query)) return;
88
89  require_once(DOKU_INC.'inc/html.php');
90  require_once(DOKU_INC.'inc/fulltext.php');
91  require_once(DOKU_INC.'inc/JSON.php');
92
93  $data = array();
94  $data = ft_pageLookup($query);
95  if(!count($data)) return;
96
97  // limit results to 15 hits
98  $data = array_slice($data, 0, 15);
99  $data = array_map('trim',$data);
100  $data = array_map('noNS',$data);
101  $data = array_unique($data);
102  sort($data);
103
104  /* now construct a json */
105  $suggestions = array(
106    $query,  // the original query
107    $data,   // some suggestions
108    array(), // no description
109    array()  // no urls
110  );
111  $json = new JSON();
112
113  header('Content-Type: application/x-suggestions+json');
114  print $json->encode($suggestions);
115}
116
117/**
118 * Refresh a page lock and save draft
119 *
120 * Andreas Gohr <andi@splitbrain.org>
121 */
122function ajax_lock(){
123  global $conf;
124  global $lang;
125  $id = cleanID($_POST['id']);
126  if(empty($id)) return;
127
128  if(!checklock($id)){
129    lock($id);
130    echo 1;
131  }
132
133  if($conf['usedraft'] && $_POST['wikitext']){
134    $client = $_SERVER['REMOTE_USER'];
135    if(!$client) $client = clientIP(true);
136
137    $draft = array('id'     => $id,
138                   'prefix' => $_POST['prefix'],
139                   'text'   => $_POST['wikitext'],
140                   'suffix' => $_POST['suffix'],
141                   'date'   => $_POST['date'],
142                   'client' => $client,
143                  );
144    $cname = getCacheName($draft['client'].$id,'.draft');
145    if(io_saveFile($cname,serialize($draft))){
146      echo $lang['draftdate'].' '.strftime($conf['dformat']);
147    }
148  }
149
150}
151
152/**
153 * Delete a draft
154 *
155 * @author Andreas Gohr <andi@splitbrain.org>
156 */
157function ajax_draftdel(){
158  $id = cleanID($_POST['id']);
159  if(empty($id)) return;
160
161  $client = $_SERVER['REMOTE_USER'];
162  if(!$client) $client = clientIP(true);
163
164  $cname = getCacheName($client.$id,'.draft');
165  @unlink($cname);
166}
167
168/**
169 * Return subnamespaces for the Mediamanager
170 *
171 * @author Andreas Gohr <andi@splitbrain.org>
172 */
173function ajax_medians(){
174  global $conf;
175  require_once(DOKU_INC.'inc/search.php');
176  require_once(DOKU_INC.'inc/media.php');
177
178  // wanted namespace
179  $ns  = cleanID($_POST['ns']);
180  $dir  = utf8_encodeFN(str_replace(':','/',$ns));
181
182  $lvl = count(explode(':',$ns));
183
184  $data = array();
185  search($data,$conf['mediadir'],'search_index',array('nofiles' => true),$dir);
186  foreach($data as $item){
187    $item['level'] = $lvl+1;
188    echo media_nstree_li($item);
189    echo media_nstree_item($item);
190    echo '</li>';
191  }
192}
193
194/**
195 * Return list of files for the Mediamanager
196 *
197 * @author Andreas Gohr <andi@splitbrain.org>
198 */
199function ajax_medialist(){
200  global $conf;
201  require_once(DOKU_INC.'inc/media.php');
202
203  media_filelist($_POST['ns']);
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  require_once(DOKU_INC.'inc/search.php');
214  require_once(DOKU_INC.'inc/html.php');
215
216  // wanted namespace
217  $ns  = cleanID($_POST['idx']);
218  $dir  = utf8_encodeFN(str_replace(':','/',$ns));
219
220  $lvl = count(explode(':',$ns));
221
222  $data = array();
223  search($data,$conf['datadir'],'search_index',array('ns' => $ns),$dir);
224  foreach($data as $item){
225    $item['level'] = $lvl+1;
226    echo html_li_index($item);
227    echo '<div class="li">';
228    echo html_list_index($item);
229    echo '</div>';
230    echo '</li>';
231  }
232}
233
234//Setup VIM: ex: et ts=2 enc=utf-8 :
235?>
236