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 $draft = new Draft($id, $client); 172 if ($draft->isDraftAvailable() && checkSecurityToken()) { 173 $draft->deleteDraft(); 174 } 175 } 176 177 /** 178 * Return subnamespaces for the Mediamanager 179 * 180 * @author Andreas Gohr <andi@splitbrain.org> 181 */ 182 protected function callMedians() { 183 global $conf; 184 global $INPUT; 185 186 // wanted namespace 187 $ns = cleanID($INPUT->post->str('ns')); 188 $dir = utf8_encodeFN(str_replace(':', '/', $ns)); 189 190 $lvl = count(explode(':', $ns)); 191 192 $data = array(); 193 search($data, $conf['mediadir'], 'search_index', array('nofiles' => true), $dir); 194 foreach(array_keys($data) as $item) { 195 $data[$item]['level'] = $lvl + 1; 196 } 197 echo html_buildlist($data, 'idx', 'media_nstree_item', 'media_nstree_li'); 198 } 199 200 /** 201 * Return list of files for the Mediamanager 202 * 203 * @author Andreas Gohr <andi@splitbrain.org> 204 */ 205 protected function callMedialist() { 206 global $NS; 207 global $INPUT; 208 209 $NS = cleanID($INPUT->post->str('ns')); 210 $sort = $INPUT->post->bool('recent') ? 'date' : 'natural'; 211 if($INPUT->post->str('do') == 'media') { 212 tpl_mediaFileList(); 213 } else { 214 tpl_mediaContent(true, $sort); 215 } 216 } 217 218 /** 219 * Return the content of the right column 220 * (image details) for the Mediamanager 221 * 222 * @author Kate Arzamastseva <pshns@ukr.net> 223 */ 224 protected function callMediadetails() { 225 global $IMG, $JUMPTO, $REV, $fullscreen, $INPUT; 226 $fullscreen = true; 227 require_once(DOKU_INC . 'lib/exe/mediamanager.php'); 228 229 $image = ''; 230 if($INPUT->has('image')) $image = cleanID($INPUT->str('image')); 231 if(isset($IMG)) $image = $IMG; 232 if(isset($JUMPTO)) $image = $JUMPTO; 233 $rev = false; 234 if(isset($REV) && !$JUMPTO) $rev = $REV; 235 236 html_msgarea(); 237 tpl_mediaFileDetails($image, $rev); 238 } 239 240 /** 241 * Returns image diff representation for mediamanager 242 * 243 * @author Kate Arzamastseva <pshns@ukr.net> 244 */ 245 protected function callMediadiff() { 246 global $NS; 247 global $INPUT; 248 249 $image = ''; 250 if($INPUT->has('image')) $image = cleanID($INPUT->str('image')); 251 $NS = getNS($image); 252 $auth = auth_quickaclcheck("$NS:*"); 253 media_diff($image, $NS, $auth, true); 254 } 255 256 /** 257 * Manages file uploads 258 * 259 * @author Kate Arzamastseva <pshns@ukr.net> 260 */ 261 protected function callMediaupload() { 262 global $NS, $MSG, $INPUT; 263 264 $id = ''; 265 if(isset($_FILES['qqfile']['tmp_name'])) { 266 $id = $INPUT->post->str('mediaid', $_FILES['qqfile']['name']); 267 } elseif($INPUT->get->has('qqfile')) { 268 $id = $INPUT->get->str('qqfile'); 269 } 270 271 $id = cleanID($id); 272 273 $NS = $INPUT->str('ns'); 274 $ns = $NS . ':' . getNS($id); 275 276 $AUTH = auth_quickaclcheck("$ns:*"); 277 if($AUTH >= AUTH_UPLOAD) { 278 io_createNamespace("$ns:xxx", 'media'); 279 } 280 281 if(isset($_FILES['qqfile']['error']) && $_FILES['qqfile']['error']) unset($_FILES['qqfile']); 282 283 $res = false; 284 if(isset($_FILES['qqfile']['tmp_name'])) $res = media_upload($NS, $AUTH, $_FILES['qqfile']); 285 if($INPUT->get->has('qqfile')) $res = media_upload_xhr($NS, $AUTH); 286 287 if($res) { 288 $result = array( 289 'success' => true, 290 'link' => media_managerURL(array('ns' => $ns, 'image' => $NS . ':' . $id), '&'), 291 'id' => $NS . ':' . $id, 292 'ns' => $NS 293 ); 294 } else { 295 $error = ''; 296 if(isset($MSG)) { 297 foreach($MSG as $msg) { 298 $error .= $msg['msg']; 299 } 300 } 301 $result = array( 302 'error' => $error, 303 'ns' => $NS 304 ); 305 } 306 307 header('Content-Type: application/json'); 308 echo json_encode($result); 309 } 310 311 /** 312 * Return sub index for index view 313 * 314 * @author Andreas Gohr <andi@splitbrain.org> 315 */ 316 protected function callIndex() { 317 global $conf; 318 global $INPUT; 319 320 // wanted namespace 321 $ns = cleanID($INPUT->post->str('idx')); 322 $dir = utf8_encodeFN(str_replace(':', '/', $ns)); 323 324 $lvl = count(explode(':', $ns)); 325 326 $data = array(); 327 search($data, $conf['datadir'], 'search_index', array('ns' => $ns), $dir); 328 foreach (array_keys($data) as $item) { 329 $data[$item]['level'] = $lvl + 1; 330 } 331 $idx = new Ui\Index; 332 echo html_buildlist($data, 'idx', [$idx,'formatListItem'], [$idx,'tagListItem']); 333 } 334 335 /** 336 * List matching namespaces and pages for the link wizard 337 * 338 * @author Andreas Gohr <gohr@cosmocode.de> 339 */ 340 protected function callLinkwiz() { 341 global $conf; 342 global $lang; 343 global $INPUT; 344 345 $q = ltrim(trim($INPUT->post->str('q')), ':'); 346 $id = noNS($q); 347 $ns = getNS($q); 348 349 $ns = cleanID($ns); 350 $id = cleanID($id); 351 352 $nsd = utf8_encodeFN(str_replace(':', '/', $ns)); 353 354 $data = array(); 355 if($q !== '' && $ns === '') { 356 357 // use index to lookup matching pages 358 $pages = ft_pageLookup($id, true); 359 360 // result contains matches in pages and namespaces 361 // we now extract the matching namespaces to show 362 // them seperately 363 $dirs = array(); 364 365 foreach($pages as $pid => $title) { 366 if(strpos(noNS($pid), $id) === false) { 367 // match was in the namespace 368 $dirs[getNS($pid)] = 1; // assoc array avoids dupes 369 } else { 370 // it is a matching page, add it to the result 371 $data[] = array( 372 'id' => $pid, 373 'title' => $title, 374 'type' => 'f', 375 ); 376 } 377 unset($pages[$pid]); 378 } 379 foreach($dirs as $dir => $junk) { 380 $data[] = array( 381 'id' => $dir, 382 'type' => 'd', 383 ); 384 } 385 386 } else { 387 388 $opts = array( 389 'depth' => 1, 390 'listfiles' => true, 391 'listdirs' => true, 392 'pagesonly' => true, 393 'firsthead' => true, 394 'sneakyacl' => $conf['sneaky_index'], 395 ); 396 if($id) $opts['filematch'] = '^.*\/' . $id; 397 if($id) $opts['dirmatch'] = '^.*\/' . $id; 398 search($data, $conf['datadir'], 'search_universal', $opts, $nsd); 399 400 // add back to upper 401 if($ns) { 402 array_unshift( 403 $data, array( 404 'id' => getNS($ns), 405 'type' => 'u', 406 ) 407 ); 408 } 409 } 410 411 // fixme sort results in a useful way ? 412 413 if(!count($data)) { 414 echo $lang['nothingfound']; 415 exit; 416 } 417 418 // output the found data 419 $even = 1; 420 foreach($data as $item) { 421 $even *= -1; //zebra 422 423 if(($item['type'] == 'd' || $item['type'] == 'u') && $item['id'] !== '') $item['id'] .= ':'; 424 $link = wl($item['id']); 425 426 echo '<div class="' . (($even > 0) ? 'even' : 'odd') . ' type_' . $item['type'] . '">'; 427 428 if($item['type'] == 'u') { 429 $name = $lang['upperns']; 430 } else { 431 $name = hsc($item['id']); 432 } 433 434 echo '<a href="' . $link . '" title="' . hsc($item['id']) . '" class="wikilink1">' . $name . '</a>'; 435 436 if(!blank($item['title'])) { 437 echo '<span>' . hsc($item['title']) . '</span>'; 438 } 439 echo '</div>'; 440 } 441 442 } 443 444} 445