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' . ucfirst($call);
20 if(method_exists($this, $callfn)) {
23 $evt = new Extension\
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 callQsearch() {
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 callSuggestions() {
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
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() {
125 $ID = cleanID($INPUT->post
->str('id'));
126 if(empty($ID)) return;
135 if(!$INFO['writable']) {
136 $response['errors'][] = 'Permission to write this page has been denied.';
137 echo json_encode($response);
141 if(!checklock($ID)) {
143 $response['lock'] = '1';
146 $draft = new Draft($ID, $INFO['client']);
147 if ($draft->saveDraft()) {
148 $response['draft'] = $draft->getDraftMessage();
150 $response['errors'] = array_merge($response['errors'], $draft->getErrors());
152 echo json_encode($response);
158 * @author Andreas Gohr <andi@splitbrain.org>
160 protected function callDraftdel() {
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');
173 * Return subnamespaces for the Mediamanager
175 * @author Andreas Gohr <andi@splitbrain.org>
177 protected function callMedians() {
182 $ns = cleanID($INPUT->post
->str('ns'));
183 $dir = utf8_encodeFN(str_replace(':', '/', $ns));
185 $lvl = count(explode(':', $ns));
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() {
204 $NS = cleanID($INPUT->post
->str('ns'));
205 $sort = $INPUT->post
->bool('recent') ?
'date' : 'natural';
206 if($INPUT->post
->str('do') == 'media') {
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;
222 require_once(DOKU_INC
. 'lib/exe/mediamanager.php');
225 if($INPUT->has('image')) $image = cleanID($INPUT->str('image'));
226 if(isset($IMG)) $image = $IMG;
227 if(isset($JUMPTO)) $image = $JUMPTO;
229 if(isset($REV) && !$JUMPTO) $rev = $REV;
232 tpl_mediaFileDetails($image, $rev);
236 * Returns image diff representation for mediamanager
238 * @author Kate Arzamastseva <pshns@ukr.net>
240 protected function callMediadiff() {
245 if($INPUT->has('image')) $image = cleanID($INPUT->str('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;
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');
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']);
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);
285 'link' => media_managerURL(array('ns' => $ns, 'image' => $NS . ':' . $id), '&'),
286 'id' => $NS . ':' . $id,
292 foreach($MSG as $msg) {
293 $error .= $msg['msg'];
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() {
316 $ns = cleanID($INPUT->post
->str('idx'));
317 $dir = utf8_encodeFN(str_replace(':', '/', $ns));
319 $lvl = count(explode(':', $ns));
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() {
339 $q = ltrim(trim($INPUT->post
->str('q')), ':');
346 $nsd = utf8_encodeFN(str_replace(':', '/', $ns));
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
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
364 // it is a matching page, add it to the result
373 foreach($dirs as $dir => $junk) {
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);
405 // fixme sort results in a useful way ?
408 echo $lang['nothingfound'];
412 // output the found data
414 foreach($data as $item) {
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'];
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>';