1<?php 2/** 3 * DokuWiki AJAX call handler 4 * 5 * @license GPL 2 (http://www.gnu.org/licenses/gpl.html) 6 * @author Andreas Gohr <andi@splitbrain.org> 7 */ 8 9if(!defined('DOKU_INC')) define('DOKU_INC',dirname(__FILE__).'/../../'); 10require_once(DOKU_INC.'inc/init.php'); 11//close session 12session_write_close(); 13 14header('Content-Type: text/html; charset=utf-8'); 15 16//call the requested function 17if($INPUT->post->has('call')){ 18 $call = $INPUT->post->str('call'); 19}else if($INPUT->get->has('call')){ 20 $call = $INPUT->get->str('call'); 21}else{ 22 exit; 23} 24$callfn = 'ajax_'.$call; 25 26if(function_exists($callfn)){ 27 $callfn(); 28}else{ 29 $evt = new Doku_Event('AJAX_CALL_UNKNOWN', $call); 30 if ($evt->advise_before()) { 31 print "AJAX call '".htmlspecialchars($call)."' unknown!\n"; 32 exit; 33 } 34 $evt->advise_after(); 35 unset($evt); 36} 37 38/** 39 * Searches for matching pagenames 40 * 41 * @author Andreas Gohr <andi@splitbrain.org> 42 */ 43function ajax_qsearch(){ 44 global $conf; 45 global $lang; 46 global $INPUT; 47 48 $query = $INPUT->post->str('q'); 49 if(empty($query)) $query = $INPUT->get->str('q'); 50 if(empty($query)) return; 51 52 $query = urldecode($query); 53 54 $data = ft_pageLookup($query, true, useHeading('navigation')); 55 56 if(!count($data)) return; 57 58 print '<strong>'.$lang['quickhits'].'</strong>'; 59 print '<ul>'; 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 print '</ul>'; 74} 75 76/** 77 * Support OpenSearch suggestions 78 * 79 * @link http://www.opensearch.org/Specifications/OpenSearch/Extensions/Suggestions/1.0 80 * @author Mike Frysinger <vapier@gentoo.org> 81 */ 82function ajax_suggestions() { 83 global $conf; 84 global $lang; 85 global $INPUT; 86 87 $query = cleanID($INPUT->post->str('q')); 88 if(empty($query)) $query = cleanID($INPUT->get->str('q')); 89 if(empty($query)) return; 90 91 $data = array(); 92 $data = ft_pageLookup($query); 93 if(!count($data)) return; 94 $data = array_keys($data); 95 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); 102 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 109 ); 110 $json = new JSON(); 111 112 header('Content-Type: application/x-suggestions+json'); 113 print $json->encode($suggestions); 114} 115 116/** 117 * Refresh a page lock and save draft 118 * 119 * Andreas Gohr <andi@splitbrain.org> 120 */ 121function ajax_lock(){ 122 global $conf; 123 global $lang; 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 if (!$INFO['writable']) { 134 echo 'Permission denied'; 135 return; 136 } 137 138 if(!checklock($ID)){ 139 lock($ID); 140 echo 1; 141 } 142 143 if($conf['usedraft'] && $INPUT->post->str('wikitext')){ 144 $client = $_SERVER['REMOTE_USER']; 145 if(!$client) $client = clientIP(true); 146 147 $draft = array('id' => $ID, 148 'prefix' => substr($INPUT->post->str('prefix'), 0, -1), 149 'text' => $INPUT->post->str('wikitext'), 150 'suffix' => $INPUT->post->str('suffix'), 151 'date' => $INPUT->post->int('date'), 152 'client' => $client, 153 ); 154 $cname = getCacheName($draft['client'].$ID,'.draft'); 155 if(io_saveFile($cname,serialize($draft))){ 156 echo $lang['draftdate'].' '.dformat(); 157 } 158 } 159 160} 161 162/** 163 * Delete a draft 164 * 165 * @author Andreas Gohr <andi@splitbrain.org> 166 */ 167function ajax_draftdel(){ 168 global $INPUT; 169 $id = cleanID($INPUT->str('id')); 170 if(empty($id)) return; 171 172 $client = $_SERVER['REMOTE_USER']; 173 if(!$client) $client = clientIP(true); 174 175 $cname = getCacheName($client.$id,'.draft'); 176 @unlink($cname); 177} 178 179/** 180 * Return subnamespaces for the Mediamanager 181 * 182 * @author Andreas Gohr <andi@splitbrain.org> 183 */ 184function ajax_medians(){ 185 global $conf; 186 global $INPUT; 187 188 // wanted namespace 189 $ns = cleanID($INPUT->post->str('ns')); 190 $dir = utf8_encodeFN(str_replace(':','/',$ns)); 191 192 $lvl = count(explode(':',$ns)); 193 194 $data = array(); 195 search($data,$conf['mediadir'],'search_index',array('nofiles' => true),$dir); 196 foreach(array_keys($data) as $item){ 197 $data[$item]['level'] = $lvl+1; 198 } 199 echo html_buildlist($data, 'idx', 'media_nstree_item', 'media_nstree_li'); 200} 201 202/** 203 * Return list of files for the Mediamanager 204 * 205 * @author Andreas Gohr <andi@splitbrain.org> 206 */ 207function ajax_medialist(){ 208 global $conf; 209 global $NS; 210 global $INPUT; 211 212 $NS = cleanID($INPUT->post->str('ns')); 213 if ($INPUT->post->str('do') == 'media') { 214 tpl_mediaFileList(); 215 } else { 216 tpl_mediaContent(true); 217 } 218} 219 220/** 221 * Return the content of the right column 222 * (image details) for the Mediamanager 223 * 224 * @author Kate Arzamastseva <pshns@ukr.net> 225 */ 226function ajax_mediadetails(){ 227 global $DEL, $NS, $IMG, $AUTH, $JUMPTO, $REV, $lang, $fullscreen, $conf, $INPUT; 228 $fullscreen = true; 229 require_once(DOKU_INC.'lib/exe/mediamanager.php'); 230 231 if ($INPUT->has('image')) $image = cleanID($INPUT->str('image')); 232 if (isset($IMG)) $image = $IMG; 233 if (isset($JUMPTO)) $image = $JUMPTO; 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 * @author Kate Arzamastseva <pshns@ukr.net> 243 */ 244function ajax_mediadiff(){ 245 global $NS; 246 global $INPUT; 247 248 if ($INPUT->has('image')) $image = cleanID($INPUT->str('image')); 249 $NS = $INPUT->post->str('ns'); 250 $auth = auth_quickaclcheck("$NS:*"); 251 media_diff($image, $NS, $auth, true); 252} 253 254function ajax_mediaupload(){ 255 global $NS, $MSG, $INPUT; 256 257 if ($_FILES['qqfile']['tmp_name']) { 258 $id = $INPUT->post->str('mediaid', $_FILES['qqfile']['name']); 259 } elseif ($INPUT->get->has('qqfile')) { 260 $id = $INPUT->get->str('qqfile'); 261 } 262 263 $id = cleanID($id); 264 265 $NS = $INPUT->str('ns'); 266 $ns = $NS.':'.getNS($id); 267 268 $AUTH = auth_quickaclcheck("$ns:*"); 269 if($AUTH >= AUTH_UPLOAD) { io_createNamespace("$ns:xxx", 'media'); } 270 271 if ($_FILES['qqfile']['error']) unset($_FILES['qqfile']); 272 273 if ($_FILES['qqfile']['tmp_name']) $res = media_upload($NS, $AUTH, $_FILES['qqfile']); 274 if ($INPUT->get->has('qqfile')) $res = media_upload_xhr($NS, $AUTH); 275 276 if ($res) $result = array('success' => true, 277 'link' => media_managerURL(array('ns' => $ns, 'image' => $NS.':'.$id), '&'), 278 'id' => $NS.':'.$id, 'ns' => $NS); 279 280 if (!$result) { 281 $error = ''; 282 if (isset($MSG)) { 283 foreach($MSG as $msg) $error .= $msg['msg']; 284 } 285 $result = array('error' => $msg['msg'], 'ns' => $NS); 286 } 287 $json = new JSON; 288 echo htmlspecialchars($json->encode($result), ENT_NOQUOTES); 289} 290 291function dir_delete($path) { 292 if (!is_string($path) || $path == "") return false; 293 294 if (is_dir($path) && !is_link($path)) { 295 if (!$dh = @opendir($path)) return false; 296 297 while ($f = readdir($dh)) { 298 if ($f == '..' || $f == '.') continue; 299 dir_delete("$path/$f"); 300 } 301 302 closedir($dh); 303 return @rmdir($path); 304 } else { 305 return @unlink($path); 306 } 307 308 return false; 309} 310 311/** 312 * Return sub index for index view 313 * 314 * @author Andreas Gohr <andi@splitbrain.org> 315 */ 316function ajax_index(){ 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 echo html_buildlist($data, 'idx', 'html_list_index', 'html_li_index'); 332} 333 334/** 335 * List matching namespaces and pages for the link wizard 336 * 337 * @author Andreas Gohr <gohr@cosmocode.de> 338 */ 339function ajax_linkwiz(){ 340 global $conf; 341 global $lang; 342 global $INPUT; 343 344 $q = ltrim(trim($INPUT->post->str('q')),':'); 345 $id = noNS($q); 346 $ns = getNS($q); 347 348 $ns = cleanID($ns); 349 $id = cleanID($id); 350 351 $nsd = utf8_encodeFN(str_replace(':','/',$ns)); 352 $idd = utf8_encodeFN(str_replace(':','/',$id)); 353 354 $data = array(); 355 if($q && !$ns){ 356 357 // use index to lookup matching pages 358 $pages = array(); 359 $pages = ft_pageLookup($id,true); 360 361 // result contains matches in pages and namespaces 362 // we now extract the matching namespaces to show 363 // them seperately 364 $dirs = array(); 365 366 foreach($pages as $pid => $title){ 367 if(strpos(noNS($pid),$id) === false){ 368 // match was in the namespace 369 $dirs[getNS($pid)] = 1; // assoc array avoids dupes 370 }else{ 371 // it is a matching page, add it to the result 372 $data[] = array( 373 'id' => $pid, 374 'title' => $title, 375 'type' => 'f', 376 ); 377 } 378 unset($pages[$pid]); 379 } 380 foreach($dirs as $dir => $junk){ 381 $data[] = array( 382 'id' => $dir, 383 'type' => 'd', 384 ); 385 } 386 387 }else{ 388 389 $opts = array( 390 'depth' => 1, 391 'listfiles' => true, 392 'listdirs' => true, 393 'pagesonly' => true, 394 'firsthead' => true, 395 'sneakyacl' => $conf['sneaky_index'], 396 ); 397 if($id) $opts['filematch'] = '^.*\/'.$id; 398 if($id) $opts['dirmatch'] = '^.*\/'.$id; 399 search($data,$conf['datadir'],'search_universal',$opts,$nsd); 400 401 // add back to upper 402 if($ns){ 403 array_unshift($data,array( 404 'id' => getNS($ns), 405 'type' => 'u', 406 )); 407 } 408 } 409 410 // fixme sort results in a useful way ? 411 412 if(!count($data)){ 413 echo $lang['nothingfound']; 414 exit; 415 } 416 417 // output the found data 418 $even = 1; 419 foreach($data as $item){ 420 $even *= -1; //zebra 421 422 if(($item['type'] == 'd' || $item['type'] == 'u') && $item['id']) $item['id'] .= ':'; 423 $link = wl($item['id']); 424 425 echo '<div class="'.(($even > 0)?'even':'odd').' type_'.$item['type'].'">'; 426 427 if($item['type'] == 'u'){ 428 $name = $lang['upperns']; 429 }else{ 430 $name = htmlspecialchars($item['id']); 431 } 432 433 echo '<a href="'.$link.'" title="'.htmlspecialchars($item['id']).'" class="wikilink1">'.$name.'</a>'; 434 435 if($item['title']){ 436 echo '<span>'.htmlspecialchars($item['title']).'</span>'; 437 } 438 echo '</div>'; 439 } 440 441} 442 443//Setup VIM: ex: et ts=2 : 444