Merge pull request #3154 from dregad/typo
[dokuwiki.git] / inc / Ajax.php
blob386d65397eae258328fbb65700ba04dc12a9e712
1 <?php
3 namespace dokuwiki;
5 /**
6 * Manage all builtin AJAX calls
8 * @todo The calls should be refactored out to their own proper classes
9 * @package dokuwiki
11 class Ajax {
13 /**
14 * Execute the given call
16 * @param string $call name of the ajax call
18 public function __construct($call) {
19 $callfn = 'call' . ucfirst($call);
20 if(method_exists($this, $callfn)) {
21 $this->$callfn();
22 } else {
23 $evt = new Extension\Event('AJAX_CALL_UNKNOWN', $call);
24 if($evt->advise_before()) {
25 print "AJAX call '" . hsc($call) . "' unknown!\n";
26 } else {
27 $evt->advise_after();
28 unset($evt);
33 /**
34 * Searches for matching pagenames
36 * @author Andreas Gohr <andi@splitbrain.org>
38 protected function callQsearch() {
39 global $lang;
40 global $INPUT;
42 $maxnumbersuggestions = 50;
44 $query = $INPUT->post->str('q');
45 if(empty($query)) $query = $INPUT->get->str('q');
46 if(empty($query)) return;
48 $query = urldecode($query);
50 $data = ft_pageLookup($query, true, useHeading('navigation'));
52 if(!count($data)) return;
54 print '<strong>' . $lang['quickhits'] . '</strong>';
55 print '<ul>';
56 $counter = 0;
57 foreach($data as $id => $title) {
58 if(useHeading('navigation')) {
59 $name = $title;
60 } else {
61 $ns = getNS($id);
62 if($ns) {
63 $name = noNS($id) . ' (' . $ns . ')';
64 } else {
65 $name = $id;
68 echo '<li>' . html_wikilink(':' . $id, $name) . '</li>';
70 $counter++;
71 if($counter > $maxnumbersuggestions) {
72 echo '<li>...</li>';
73 break;
76 print '</ul>';
79 /**
80 * Support OpenSearch suggestions
82 * @link http://www.opensearch.org/Specifications/OpenSearch/Extensions/Suggestions/1.0
83 * @author Mike Frysinger <vapier@gentoo.org>
85 protected function callSuggestions() {
86 global $INPUT;
88 $query = cleanID($INPUT->post->str('q'));
89 if(empty($query)) $query = cleanID($INPUT->get->str('q'));
90 if(empty($query)) return;
92 $data = ft_pageLookup($query);
93 if(!count($data)) return;
94 $data = array_keys($data);
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);
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
111 header('Content-Type: application/x-suggestions+json');
112 print json_encode($suggestions);
116 * Refresh a page lock and save draft
118 * Andreas Gohr <andi@splitbrain.org>
120 protected function callLock() {
121 global $ID;
122 global $INFO;
123 global $INPUT;
125 $ID = cleanID($INPUT->post->str('id'));
126 if(empty($ID)) return;
128 $INFO = pageinfo();
130 $response = [
131 'errors' => [],
132 'lock' => '0',
133 'draft' => '',
135 if(!$INFO['writable']) {
136 $response['errors'][] = 'Permission to write this page has been denied.';
137 echo json_encode($response);
138 return;
141 if(!checklock($ID)) {
142 lock($ID);
143 $response['lock'] = '1';
146 $draft = new Draft($ID, $INFO['client']);
147 if ($draft->saveDraft()) {
148 $response['draft'] = $draft->getDraftMessage();
149 } else {
150 $response['errors'] = array_merge($response['errors'], $draft->getErrors());
152 echo json_encode($response);
156 * Delete a draft
158 * @author Andreas Gohr <andi@splitbrain.org>
160 protected function callDraftdel() {
161 global $INPUT;
162 $id = cleanID($INPUT->str('id'));
163 if(empty($id)) return;
165 $client = $_SERVER['REMOTE_USER'];
166 if(!$client) $client = clientIP(true);
168 $cname = getCacheName($client . $id, '.draft');
169 @unlink($cname);
173 * Return subnamespaces for the Mediamanager
175 * @author Andreas Gohr <andi@splitbrain.org>
177 protected function callMedians() {
178 global $conf;
179 global $INPUT;
181 // wanted namespace
182 $ns = cleanID($INPUT->post->str('ns'));
183 $dir = utf8_encodeFN(str_replace(':', '/', $ns));
185 $lvl = count(explode(':', $ns));
187 $data = array();
188 search($data, $conf['mediadir'], 'search_index', array('nofiles' => true), $dir);
189 foreach(array_keys($data) as $item) {
190 $data[$item]['level'] = $lvl + 1;
192 echo html_buildlist($data, 'idx', 'media_nstree_item', 'media_nstree_li');
196 * Return list of files for the Mediamanager
198 * @author Andreas Gohr <andi@splitbrain.org>
200 protected function callMedialist() {
201 global $NS;
202 global $INPUT;
204 $NS = cleanID($INPUT->post->str('ns'));
205 $sort = $INPUT->post->bool('recent') ? 'date' : 'natural';
206 if($INPUT->post->str('do') == 'media') {
207 tpl_mediaFileList();
208 } else {
209 tpl_mediaContent(true, $sort);
214 * Return the content of the right column
215 * (image details) for the Mediamanager
217 * @author Kate Arzamastseva <pshns@ukr.net>
219 protected function callMediadetails() {
220 global $IMG, $JUMPTO, $REV, $fullscreen, $INPUT;
221 $fullscreen = true;
222 require_once(DOKU_INC . 'lib/exe/mediamanager.php');
224 $image = '';
225 if($INPUT->has('image')) $image = cleanID($INPUT->str('image'));
226 if(isset($IMG)) $image = $IMG;
227 if(isset($JUMPTO)) $image = $JUMPTO;
228 $rev = false;
229 if(isset($REV) && !$JUMPTO) $rev = $REV;
231 html_msgarea();
232 tpl_mediaFileDetails($image, $rev);
236 * Returns image diff representation for mediamanager
238 * @author Kate Arzamastseva <pshns@ukr.net>
240 protected function callMediadiff() {
241 global $NS;
242 global $INPUT;
244 $image = '';
245 if($INPUT->has('image')) $image = cleanID($INPUT->str('image'));
246 $NS = getNS($image);
247 $auth = auth_quickaclcheck("$NS:*");
248 media_diff($image, $NS, $auth, true);
252 * Manages file uploads
254 * @author Kate Arzamastseva <pshns@ukr.net>
256 protected function callMediaupload() {
257 global $NS, $MSG, $INPUT;
259 $id = '';
260 if(isset($_FILES['qqfile']['tmp_name'])) {
261 $id = $INPUT->post->str('mediaid', $_FILES['qqfile']['name']);
262 } elseif($INPUT->get->has('qqfile')) {
263 $id = $INPUT->get->str('qqfile');
266 $id = cleanID($id);
268 $NS = $INPUT->str('ns');
269 $ns = $NS . ':' . getNS($id);
271 $AUTH = auth_quickaclcheck("$ns:*");
272 if($AUTH >= AUTH_UPLOAD) {
273 io_createNamespace("$ns:xxx", 'media');
276 if(isset($_FILES['qqfile']['error']) && $_FILES['qqfile']['error']) unset($_FILES['qqfile']);
278 $res = false;
279 if(isset($_FILES['qqfile']['tmp_name'])) $res = media_upload($NS, $AUTH, $_FILES['qqfile']);
280 if($INPUT->get->has('qqfile')) $res = media_upload_xhr($NS, $AUTH);
282 if($res) {
283 $result = array(
284 'success' => true,
285 'link' => media_managerURL(array('ns' => $ns, 'image' => $NS . ':' . $id), '&'),
286 'id' => $NS . ':' . $id,
287 'ns' => $NS
289 } else {
290 $error = '';
291 if(isset($MSG)) {
292 foreach($MSG as $msg) {
293 $error .= $msg['msg'];
296 $result = array(
297 'error' => $error,
298 'ns' => $NS
302 header('Content-Type: application/json');
303 echo json_encode($result);
307 * Return sub index for index view
309 * @author Andreas Gohr <andi@splitbrain.org>
311 protected function callIndex() {
312 global $conf;
313 global $INPUT;
315 // wanted namespace
316 $ns = cleanID($INPUT->post->str('idx'));
317 $dir = utf8_encodeFN(str_replace(':', '/', $ns));
319 $lvl = count(explode(':', $ns));
321 $data = array();
322 search($data, $conf['datadir'], 'search_index', array('ns' => $ns), $dir);
323 foreach(array_keys($data) as $item) {
324 $data[$item]['level'] = $lvl + 1;
326 echo html_buildlist($data, 'idx', 'html_list_index', 'html_li_index');
330 * List matching namespaces and pages for the link wizard
332 * @author Andreas Gohr <gohr@cosmocode.de>
334 protected function callLinkwiz() {
335 global $conf;
336 global $lang;
337 global $INPUT;
339 $q = ltrim(trim($INPUT->post->str('q')), ':');
340 $id = noNS($q);
341 $ns = getNS($q);
343 $ns = cleanID($ns);
344 $id = cleanID($id);
346 $nsd = utf8_encodeFN(str_replace(':', '/', $ns));
348 $data = array();
349 if($q !== '' && $ns === '') {
351 // use index to lookup matching pages
352 $pages = ft_pageLookup($id, true);
354 // result contains matches in pages and namespaces
355 // we now extract the matching namespaces to show
356 // them seperately
357 $dirs = array();
359 foreach($pages as $pid => $title) {
360 if(strpos(noNS($pid), $id) === false) {
361 // match was in the namespace
362 $dirs[getNS($pid)] = 1; // assoc array avoids dupes
363 } else {
364 // it is a matching page, add it to the result
365 $data[] = array(
366 'id' => $pid,
367 'title' => $title,
368 'type' => 'f',
371 unset($pages[$pid]);
373 foreach($dirs as $dir => $junk) {
374 $data[] = array(
375 'id' => $dir,
376 'type' => 'd',
380 } else {
382 $opts = array(
383 'depth' => 1,
384 'listfiles' => true,
385 'listdirs' => true,
386 'pagesonly' => true,
387 'firsthead' => true,
388 'sneakyacl' => $conf['sneaky_index'],
390 if($id) $opts['filematch'] = '^.*\/' . $id;
391 if($id) $opts['dirmatch'] = '^.*\/' . $id;
392 search($data, $conf['datadir'], 'search_universal', $opts, $nsd);
394 // add back to upper
395 if($ns) {
396 array_unshift(
397 $data, array(
398 'id' => getNS($ns),
399 'type' => 'u',
405 // fixme sort results in a useful way ?
407 if(!count($data)) {
408 echo $lang['nothingfound'];
409 exit;
412 // output the found data
413 $even = 1;
414 foreach($data as $item) {
415 $even *= -1; //zebra
417 if(($item['type'] == 'd' || $item['type'] == 'u') && $item['id'] !== '') $item['id'] .= ':';
418 $link = wl($item['id']);
420 echo '<div class="' . (($even > 0) ? 'even' : 'odd') . ' type_' . $item['type'] . '">';
422 if($item['type'] == 'u') {
423 $name = $lang['upperns'];
424 } else {
425 $name = hsc($item['id']);
428 echo '<a href="' . $link . '" title="' . hsc($item['id']) . '" class="wikilink1">' . $name . '</a>';
430 if(!blank($item['title'])) {
431 echo '<span>' . hsc($item['title']) . '</span>';
433 echo '</div>';