6 * Manage all builtin AJAX calls
8 * @todo The calls should be refactored out to their own proper classes
14 * Execute the given call
16 * @param string $call name of the ajax call
18 public function __construct($call) {
19 $callfn = 'call_' . $call;
20 if(method_exists($this, $callfn)) {
23 $evt = new \
Doku_Event('AJAX_CALL_UNKNOWN', $call);
24 if($evt->advise_before()) {
25 print "AJAX call '" . hsc($call) . "' unknown!\n";
34 * Searches for matching pagenames
36 * @author Andreas Gohr <andi@splitbrain.org>
38 protected function call_qsearch() {
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>';
57 foreach($data as $id => $title) {
58 if(useHeading('navigation')) {
63 $name = noNS($id) . ' (' . $ns . ')';
68 echo '<li>' . html_wikilink(':' . $id, $name) . '</li>';
71 if($counter > $maxnumbersuggestions) {
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 call_suggestions() {
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);
103 /* now construct a json */
104 $suggestions = array(
105 $query, // the original query
106 $data, // some suggestions
107 array(), // no description
112 header('Content-Type: application/x-suggestions+json');
113 print $json->encode($suggestions);
117 * Refresh a page lock and save draft
119 * Andreas Gohr <andi@splitbrain.org>
121 protected function call_lock() {
126 $ID = cleanID($INPUT->post
->str('id'));
127 if(empty($ID)) return;
136 if(!$INFO['writable']) {
137 $response['errors'][] = 'Permission to write this page has been denied.';
138 echo json_encode($response);
142 if(!checklock($ID)) {
144 $response['lock'] = '1';
147 $draft = new Draft($ID, $INFO['client']);
148 if ($draft->saveDraft()) {
149 $response['draft'] = $draft->getDraftMessage();
151 $response['errors'] = array_merge($response['errors'], $draft->getErrors());
153 echo json_encode($response);
159 * @author Andreas Gohr <andi@splitbrain.org>
161 protected function call_draftdel() {
163 $id = cleanID($INPUT->str('id'));
164 if(empty($id)) return;
166 $client = $_SERVER['REMOTE_USER'];
167 if(!$client) $client = clientIP(true);
169 $cname = getCacheName($client . $id, '.draft');
174 * Return subnamespaces for the Mediamanager
176 * @author Andreas Gohr <andi@splitbrain.org>
178 protected function call_medians() {
183 $ns = cleanID($INPUT->post
->str('ns'));
184 $dir = utf8_encodeFN(str_replace(':', '/', $ns));
186 $lvl = count(explode(':', $ns));
189 search($data, $conf['mediadir'], 'search_index', array('nofiles' => true), $dir);
190 foreach(array_keys($data) as $item) {
191 $data[$item]['level'] = $lvl +
1;
193 echo html_buildlist($data, 'idx', 'media_nstree_item', 'media_nstree_li');
197 * Return list of files for the Mediamanager
199 * @author Andreas Gohr <andi@splitbrain.org>
201 protected function call_medialist() {
205 $NS = cleanID($INPUT->post
->str('ns'));
206 $sort = $INPUT->post
->bool('recent') ?
'date' : 'natural';
207 if($INPUT->post
->str('do') == 'media') {
210 tpl_mediaContent(true, $sort);
215 * Return the content of the right column
216 * (image details) for the Mediamanager
218 * @author Kate Arzamastseva <pshns@ukr.net>
220 protected function call_mediadetails() {
221 global $IMG, $JUMPTO, $REV, $fullscreen, $INPUT;
223 require_once(DOKU_INC
. 'lib/exe/mediamanager.php');
226 if($INPUT->has('image')) $image = cleanID($INPUT->str('image'));
227 if(isset($IMG)) $image = $IMG;
228 if(isset($JUMPTO)) $image = $JUMPTO;
230 if(isset($REV) && !$JUMPTO) $rev = $REV;
233 tpl_mediaFileDetails($image, $rev);
237 * Returns image diff representation for mediamanager
239 * @author Kate Arzamastseva <pshns@ukr.net>
241 protected function call_mediadiff() {
246 if($INPUT->has('image')) $image = cleanID($INPUT->str('image'));
248 $auth = auth_quickaclcheck("$NS:*");
249 media_diff($image, $NS, $auth, true);
253 * Manages file uploads
255 * @author Kate Arzamastseva <pshns@ukr.net>
257 protected function call_mediaupload() {
258 global $NS, $MSG, $INPUT;
261 if($_FILES['qqfile']['tmp_name']) {
262 $id = $INPUT->post
->str('mediaid', $_FILES['qqfile']['name']);
263 } elseif($INPUT->get
->has('qqfile')) {
264 $id = $INPUT->get
->str('qqfile');
269 $NS = $INPUT->str('ns');
270 $ns = $NS . ':' . getNS($id);
272 $AUTH = auth_quickaclcheck("$ns:*");
273 if($AUTH >= AUTH_UPLOAD
) {
274 io_createNamespace("$ns:xxx", 'media');
277 if($_FILES['qqfile']['error']) unset($_FILES['qqfile']);
280 if($_FILES['qqfile']['tmp_name']) $res = media_upload($NS, $AUTH, $_FILES['qqfile']);
281 if($INPUT->get
->has('qqfile')) $res = media_upload_xhr($NS, $AUTH);
286 'link' => media_managerURL(array('ns' => $ns, 'image' => $NS . ':' . $id), '&'),
287 'id' => $NS . ':' . $id,
293 foreach($MSG as $msg) {
294 $error .= $msg['msg'];
303 header('Content-Type: application/json');
304 echo $json->encode($result);
308 * Return sub index for index view
310 * @author Andreas Gohr <andi@splitbrain.org>
312 protected function call_index() {
317 $ns = cleanID($INPUT->post
->str('idx'));
318 $dir = utf8_encodeFN(str_replace(':', '/', $ns));
320 $lvl = count(explode(':', $ns));
323 search($data, $conf['datadir'], 'search_index', array('ns' => $ns), $dir);
324 foreach(array_keys($data) as $item) {
325 $data[$item]['level'] = $lvl +
1;
327 echo html_buildlist($data, 'idx', 'html_list_index', 'html_li_index');
331 * List matching namespaces and pages for the link wizard
333 * @author Andreas Gohr <gohr@cosmocode.de>
335 protected function call_linkwiz() {
340 $q = ltrim(trim($INPUT->post
->str('q')), ':');
347 $nsd = utf8_encodeFN(str_replace(':', '/', $ns));
352 // use index to lookup matching pages
353 $pages = ft_pageLookup($id, true);
355 // result contains matches in pages and namespaces
356 // we now extract the matching namespaces to show
360 foreach($pages as $pid => $title) {
361 if(strpos(noNS($pid), $id) === false) {
362 // match was in the namespace
363 $dirs[getNS($pid)] = 1; // assoc array avoids dupes
365 // it is a matching page, add it to the result
374 foreach($dirs as $dir => $junk) {
389 'sneakyacl' => $conf['sneaky_index'],
391 if($id) $opts['filematch'] = '^.*\/' . $id;
392 if($id) $opts['dirmatch'] = '^.*\/' . $id;
393 search($data, $conf['datadir'], 'search_universal', $opts, $nsd);
406 // fixme sort results in a useful way ?
409 echo $lang['nothingfound'];
413 // output the found data
415 foreach($data as $item) {
418 if(($item['type'] == 'd' ||
$item['type'] == 'u') && $item['id']) $item['id'] .= ':';
419 $link = wl($item['id']);
421 echo '<div class="' . (($even > 0) ?
'even' : 'odd') . ' type_' . $item['type'] . '">';
423 if($item['type'] == 'u') {
424 $name = $lang['upperns'];
426 $name = hsc($item['id']);
429 echo '<a href="' . $link . '" title="' . hsc($item['id']) . '" class="wikilink1">' . $name . '</a>';
431 if(!blank($item['title'])) {
432 echo '<span>' . hsc($item['title']) . '</span>';