1<?php 2 3namespace dokuwiki; 4 5use dokuwiki\Ui; 6use dokuwiki\Utf8\Sort; 7 8/** 9 * Manage all builtin AJAX calls 10 * 11 * @todo The calls should be refactored out to their own proper classes 12 * @package dokuwiki 13 */ 14class Ajax { 15 16 /** 17 * Execute the given call 18 * 19 * @param string $call name of the ajax call 20 */ 21 public function __construct($call) { 22 $callfn = 'call' . ucfirst($call); 23 if(method_exists($this, $callfn)) { 24 $this->$callfn(); 25 } else { 26 $evt = new Extension\Event('AJAX_CALL_UNKNOWN', $call); 27 if($evt->advise_before()) { 28 print "AJAX call '" . hsc($call) . "' unknown!\n"; 29 } else { 30 $evt->advise_after(); 31 unset($evt); 32 } 33 } 34 } 35 36 /** 37 * Searches for matching pagenames 38 * 39 * @author Andreas Gohr <andi@splitbrain.org> 40 */ 41 protected function callQsearch() { 42 global $lang; 43 global $INPUT; 44 45 $maxnumbersuggestions = 50; 46 47 $query = $INPUT->post->str('q'); 48 if(empty($query)) $query = $INPUT->get->str('q'); 49 if(empty($query)) return; 50 51 $query = urldecode($query); 52 53 $data = ft_pageLookup($query, true, useHeading('navigation')); 54 55 if(!count($data)) return; 56 57 print '<strong>' . $lang['quickhits'] . '</strong>'; 58 print '<ul>'; 59 $counter = 0; 60 foreach($data as $id => $title) { 61 if(useHeading('navigation')) { 62 $name = $title; 63 } else { 64 $ns = getNS($id); 65 if($ns) { 66 $name = noNS($id) . ' (' . $ns . ')'; 67 } else { 68 $name = $id; 69 } 70 } 71 echo '<li>' . html_wikilink(':' . $id, $name) . '</li>'; 72 73 $counter++; 74 if($counter > $maxnumbersuggestions) { 75 echo '<li>...</li>'; 76 break; 77 } 78 } 79 print '</ul>'; 80 } 81 82 /** 83 * Support OpenSearch suggestions 84 * 85 * @link http://www.opensearch.org/Specifications/OpenSearch/Extensions/Suggestions/1.0 86 * @author Mike Frysinger <vapier@gentoo.org> 87 */ 88 protected function callSuggestions() { 89 global $INPUT; 90 91 $query = cleanID($INPUT->post->str('q')); 92 if(empty($query)) $query = cleanID($INPUT->get->str('q')); 93 if(empty($query)) return; 94 95 $data = ft_pageLookup($query); 96 if(!count($data)) return; 97 $data = array_keys($data); 98 99 // limit results to 15 hits 100 $data = array_slice($data, 0, 15); 101 $data = array_map('trim', $data); 102 $data = array_map('noNS', $data); 103 $data = array_unique($data); 104 Sort::sort($data); 105 106 /* now construct a json */ 107 $suggestions = array( 108 $query, // the original query 109 $data, // some suggestions 110 array(), // no description 111 array() // no urls 112 ); 113 114 header('Content-Type: application/x-suggestions+json'); 115 print json_encode($suggestions); 116 } 117 118 /** 119 * Refresh a page lock and save draft 120 * 121 * Andreas Gohr <andi@splitbrain.org> 122 */ 123 protected function callLock() { 124 global $ID; 125 global $INFO; 126 global $INPUT; 127 128 $ID = cleanID($INPUT->post->str('id')); 129 if(empty($ID)) return; 130 131 $INFO = pageinfo(); 132 133 $response = [ 134 'errors' => [], 135 'lock' => '0', 136 'draft' => '', 137 ]; 138 if(!$INFO['writable']) { 139 $response['errors'][] = 'Permission to write this page has been denied.'; 140 echo json_encode($response); 141 return; 142 } 143 144 if(!checklock($ID)) { 145 lock($ID); 146 $response['lock'] = '1'; 147 } 148 149 $draft = new Draft($ID, $INFO['client']); 150 if ($draft->saveDraft()) { 151 $response['draft'] = $draft->getDraftMessage(); 152 } else { 153 $response['errors'] = array_merge($response['errors'], $draft->getErrors()); 154 } 155 echo json_encode($response); 156 } 157 158 /** 159 * Delete a draft 160 * 161 * @author Andreas Gohr <andi@splitbrain.org> 162 */ 163 protected function callDraftdel() { 164 global $INPUT; 165 $id = cleanID($INPUT->str('id')); 166 if(empty($id)) return; 167 168 $client = $_SERVER['REMOTE_USER']; 169 if(!$client) $client = clientIP(true); 170 171 $cname = getCacheName($client . $id, '.draft'); 172 @unlink($cname); 173 } 174 175 /** 176 * Return subnamespaces for the Mediamanager 177 * 178 * @author Andreas Gohr <andi@splitbrain.org> 179 */ 180 protected function callMedians() { 181 global $conf; 182 global $INPUT; 183 184 // wanted namespace 185 $ns = cleanID($INPUT->post->str('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(array_keys($data) as $item) { 193 $data[$item]['level'] = $lvl + 1; 194 } 195 echo html_buildlist($data, 'idx', 'media_nstree_item', 'media_nstree_li'); 196 } 197 198 /** 199 * Return list of files for the Mediamanager 200 * 201 * @author Andreas Gohr <andi@splitbrain.org> 202 */ 203 protected function callMedialist() { 204 global $NS; 205 global $INPUT; 206 207 $NS = cleanID($INPUT->post->str('ns')); 208 $sort = $INPUT->post->bool('recent') ? 'date' : 'natural'; 209 if($INPUT->post->str('do') == 'media') { 210 tpl_mediaFileList(); 211 } else { 212 tpl_mediaContent(true, $sort); 213 } 214 } 215 216 /** 217 * Return the content of the right column 218 * (image details) for the Mediamanager 219 * 220 * @author Kate Arzamastseva <pshns@ukr.net> 221 */ 222 protected function callMediadetails() { 223 global $IMG, $JUMPTO, $REV, $fullscreen, $INPUT; 224 $fullscreen = true; 225 require_once(DOKU_INC . 'lib/exe/mediamanager.php'); 226 227 $image = ''; 228 if($INPUT->has('image')) $image = cleanID($INPUT->str('image')); 229 if(isset($IMG)) $image = $IMG; 230 if(isset($JUMPTO)) $image = $JUMPTO; 231 $rev = false; 232 if(isset($REV) && !$JUMPTO) $rev = $REV; 233 234 html_msgarea(); 235 tpl_mediaFileDetails($image, $rev); 236 } 237 238 /** 239 * Returns image diff representation for mediamanager 240 * 241 * @author Kate Arzamastseva <pshns@ukr.net> 242 */ 243 protected function callMediadiff() { 244 global $INPUT; 245 246 $image = ''; 247 if($INPUT->has('image')) $image = cleanID($INPUT->str('image')); 248 (new Ui\MediaDiff($image))->preference('fromAjax', true)->show(); 249 } 250 251 /** 252 * Manages file uploads 253 * 254 * @author Kate Arzamastseva <pshns@ukr.net> 255 */ 256 protected function callMediaupload() { 257 global $NS, $MSG, $INPUT; 258 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'); 264 } 265 266 $id = cleanID($id); 267 268 $NS = $INPUT->str('ns'); 269 $ns = $NS . ':' . getNS($id); 270 271 $AUTH = auth_quickaclcheck("$ns:*"); 272 if($AUTH >= AUTH_UPLOAD) { 273 io_createNamespace("$ns:xxx", 'media'); 274 } 275 276 if(isset($_FILES['qqfile']['error']) && $_FILES['qqfile']['error']) unset($_FILES['qqfile']); 277 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); 281 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 288 ); 289 } else { 290 $error = ''; 291 if(isset($MSG)) { 292 foreach($MSG as $msg) { 293 $error .= $msg['msg']; 294 } 295 } 296 $result = array( 297 'error' => $error, 298 'ns' => $NS 299 ); 300 } 301 302 header('Content-Type: application/json'); 303 echo json_encode($result); 304 } 305 306 /** 307 * Return sub index for index view 308 * 309 * @author Andreas Gohr <andi@splitbrain.org> 310 */ 311 protected function callIndex() { 312 global $conf; 313 global $INPUT; 314 315 // wanted namespace 316 $ns = cleanID($INPUT->post->str('idx')); 317 $dir = utf8_encodeFN(str_replace(':', '/', $ns)); 318 319 $lvl = count(explode(':', $ns)); 320 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; 325 } 326 $idx = new Ui\Index; 327 echo html_buildlist($data, 'idx', [$idx,'formatListItem'], [$idx,'tagListItem']); 328 } 329 330 /** 331 * List matching namespaces and pages for the link wizard 332 * 333 * @author Andreas Gohr <gohr@cosmocode.de> 334 */ 335 protected function callLinkwiz() { 336 global $conf; 337 global $lang; 338 global $INPUT; 339 340 $q = ltrim(trim($INPUT->post->str('q')), ':'); 341 $id = noNS($q); 342 $ns = getNS($q); 343 344 $ns = cleanID($ns); 345 $id = cleanID($id); 346 347 $nsd = utf8_encodeFN(str_replace(':', '/', $ns)); 348 349 $data = array(); 350 if($q !== '' && $ns === '') { 351 352 // use index to lookup matching pages 353 $pages = ft_pageLookup($id, true); 354 355 // result contains matches in pages and namespaces 356 // we now extract the matching namespaces to show 357 // them seperately 358 $dirs = array(); 359 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 364 } else { 365 // it is a matching page, add it to the result 366 $data[] = array( 367 'id' => $pid, 368 'title' => $title, 369 'type' => 'f', 370 ); 371 } 372 unset($pages[$pid]); 373 } 374 foreach($dirs as $dir => $junk) { 375 $data[] = array( 376 'id' => $dir, 377 'type' => 'd', 378 ); 379 } 380 381 } else { 382 383 $opts = array( 384 'depth' => 1, 385 'listfiles' => true, 386 'listdirs' => true, 387 'pagesonly' => true, 388 'firsthead' => true, 389 'sneakyacl' => $conf['sneaky_index'], 390 ); 391 if($id) $opts['filematch'] = '^.*\/' . $id; 392 if($id) $opts['dirmatch'] = '^.*\/' . $id; 393 search($data, $conf['datadir'], 'search_universal', $opts, $nsd); 394 395 // add back to upper 396 if($ns) { 397 array_unshift( 398 $data, array( 399 'id' => getNS($ns), 400 'type' => 'u', 401 ) 402 ); 403 } 404 } 405 406 // fixme sort results in a useful way ? 407 408 if(!count($data)) { 409 echo $lang['nothingfound']; 410 exit; 411 } 412 413 // output the found data 414 $even = 1; 415 foreach($data as $item) { 416 $even *= -1; //zebra 417 418 if(($item['type'] == 'd' || $item['type'] == 'u') && $item['id'] !== '') $item['id'] .= ':'; 419 $link = wl($item['id']); 420 421 echo '<div class="' . (($even > 0) ? 'even' : 'odd') . ' type_' . $item['type'] . '">'; 422 423 if($item['type'] == 'u') { 424 $name = $lang['upperns']; 425 } else { 426 $name = hsc($item['id']); 427 } 428 429 echo '<a href="' . $link . '" title="' . hsc($item['id']) . '" class="wikilink1">' . $name . '</a>'; 430 431 if(!blank($item['title'])) { 432 echo '<span>' . hsc($item['title']) . '</span>'; 433 } 434 echo '</div>'; 435 } 436 437 } 438 439} 440