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(isset($_POST['call'])){ 18 $call = $_POST['call']; 19}else if(isset($_GET['call'])){ 20 $call = $_GET['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 47 $query = $_POST['q']; 48 if(empty($query)) $query = $_GET['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 foreach($data as $id => $title){ 60 if (useHeading('navigation')) { 61 $name = $title; 62 } else { 63 $ns = getNS($id); 64 if($ns){ 65 $name = noNS($id).' ('.$ns.')'; 66 }else{ 67 $name = $id; 68 } 69 } 70 echo '<li>' . html_wikilink(':'.$id,$name) . '</li>'; 71 } 72 print '</ul>'; 73} 74 75/** 76 * Support OpenSearch suggestions 77 * 78 * @link http://www.opensearch.org/Specifications/OpenSearch/Extensions/Suggestions/1.0 79 * @author Mike Frysinger <vapier@gentoo.org> 80 */ 81function ajax_suggestions() { 82 global $conf; 83 global $lang; 84 85 $query = cleanID($_POST['q']); 86 if(empty($query)) $query = cleanID($_GET['q']); 87 if(empty($query)) return; 88 89 $data = array(); 90 $data = ft_pageLookup($query); 91 if(!count($data)) return; 92 $data = array_keys($data); 93 94 // limit results to 15 hits 95 $data = array_slice($data, 0, 15); 96 $data = array_map('trim',$data); 97 $data = array_map('noNS',$data); 98 $data = array_unique($data); 99 sort($data); 100 101 /* now construct a json */ 102 $suggestions = array( 103 $query, // the original query 104 $data, // some suggestions 105 array(), // no description 106 array() // no urls 107 ); 108 $json = new JSON(); 109 110 header('Content-Type: application/x-suggestions+json'); 111 print $json->encode($suggestions); 112} 113 114/** 115 * Refresh a page lock and save draft 116 * 117 * Andreas Gohr <andi@splitbrain.org> 118 */ 119function ajax_lock(){ 120 global $conf; 121 global $lang; 122 global $ID; 123 global $INFO; 124 125 $ID = cleanID($_POST['id']); 126 if(empty($ID)) return; 127 128 $INFO = pageinfo(); 129 130 if (!$INFO['writable']) { 131 echo 'Permission denied'; 132 return; 133 } 134 135 if(!checklock($ID)){ 136 lock($ID); 137 echo 1; 138 } 139 140 if($conf['usedraft'] && $_POST['wikitext']){ 141 $client = $_SERVER['REMOTE_USER']; 142 if(!$client) $client = clientIP(true); 143 144 $draft = array('id' => $ID, 145 'prefix' => substr($_POST['prefix'], 0, -1), 146 'text' => $_POST['wikitext'], 147 'suffix' => $_POST['suffix'], 148 'date' => (int) $_POST['date'], 149 'client' => $client, 150 ); 151 $cname = getCacheName($draft['client'].$ID,'.draft'); 152 if(io_saveFile($cname,serialize($draft))){ 153 echo $lang['draftdate'].' '.dformat(); 154 } 155 } 156 157} 158 159/** 160 * Delete a draft 161 * 162 * @author Andreas Gohr <andi@splitbrain.org> 163 */ 164function ajax_draftdel(){ 165 $id = cleanID($_REQUEST['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 */ 180function ajax_medians(){ 181 global $conf; 182 183 // wanted namespace 184 $ns = cleanID($_POST['ns']); 185 $dir = utf8_encodeFN(str_replace(':','/',$ns)); 186 187 $lvl = count(explode(':',$ns)); 188 189 $data = array(); 190 search($data,$conf['mediadir'],'search_index',array('nofiles' => true),$dir); 191 foreach(array_keys($data) as $item){ 192 $data[$item]['level'] = $lvl+1; 193 } 194 echo html_buildlist($data, 'idx', 'media_nstree_item', 'media_nstree_li'); 195} 196 197/** 198 * Return list of files for the Mediamanager 199 * 200 * @author Andreas Gohr <andi@splitbrain.org> 201 */ 202function ajax_medialist(){ 203 global $conf; 204 global $NS; 205 206 $NS = cleanID($_POST['ns']); 207 if ($_POST['do'] == 'media') { 208 tpl_mediaFileList(); 209 } else { 210 tpl_mediaContent(true); 211 } 212} 213 214/** 215 * Return the content of the right column 216 * (image details) for the Mediamanager 217 * 218 * @author Kate Arzamastseva <pshns@ukr.net> 219 */ 220function ajax_mediadetails(){ 221 global $DEL, $NS, $IMG, $AUTH, $JUMPTO, $REV, $lang, $fullscreen, $conf; 222 $fullscreen = true; 223 require_once(DOKU_INC.'lib/exe/mediamanager.php'); 224 225 if ($_REQUEST['image']) $image = cleanID($_REQUEST['image']); 226 if (isset($IMG)) $image = $IMG; 227 if (isset($JUMPTO)) $image = $JUMPTO; 228 if (isset($REV) && !$JUMPTO) $rev = $REV; 229 230 html_msgarea(); 231 tpl_mediaFileDetails($image, $rev); 232} 233 234/** 235 * Returns image diff representation for mediamanager 236 * @author Kate Arzamastseva <pshns@ukr.net> 237 */ 238function ajax_mediadiff(){ 239 global $NS; 240 241 if ($_REQUEST['image']) $image = cleanID($_REQUEST['image']); 242 $NS = $_POST['ns']; 243 $auth = auth_quickaclcheck("$ns:*"); 244 media_diff($image, $NS, $auth, true); 245} 246 247function ajax_mediaupload(){ 248 global $NS, $MSG; 249 250 if ($_FILES['qqfile']['tmp_name']) { 251 $id = ((empty($_POST['mediaid'])) ? $_FILES['qqfile']['name'] : $_POST['mediaid']); 252 } elseif (isset($_GET['qqfile'])) { 253 $id = $_GET['qqfile']; 254 } 255 256 $id = cleanID($id); 257 258 $NS = $_REQUEST['ns']; 259 $ns = $NS.':'.getNS($id); 260 261 $AUTH = auth_quickaclcheck("$ns:*"); 262 if($AUTH >= AUTH_UPLOAD) { io_createNamespace("$ns:xxx", 'media'); } 263 264 if ($_FILES['qqfile']['error']) unset($_FILES['qqfile']); 265 266 if ($_FILES['qqfile']['tmp_name']) $res = media_upload($NS, $AUTH, $_FILES['qqfile']); 267 if (isset($_GET['qqfile'])) $res = media_upload_xhr($NS, $AUTH); 268 269 if ($res) $result = array('success' => true, 270 'link' => media_managerURL(array('ns' => $ns, 'image' => $NS.':'.$id), '&'), 271 'id' => $NS.':'.$id, 'ns' => $NS); 272 273 if (!$result) { 274 $error = ''; 275 if (isset($MSG)) { 276 foreach($MSG as $msg) $error .= $msg['msg']; 277 } 278 $result = array('error' => $msg['msg'], 'ns' => $NS); 279 } 280 $json = new JSON; 281 echo htmlspecialchars($json->encode($result), ENT_NOQUOTES); 282} 283 284function dir_delete($path) { 285 if (!is_string($path) || $path == "") return false; 286 287 if (is_dir($path) && !is_link($path)) { 288 if (!$dh = @opendir($path)) return false; 289 290 while ($f = readdir($dh)) { 291 if ($f == '..' || $f == '.') continue; 292 dir_delete("$path/$f"); 293 } 294 295 closedir($dh); 296 return @rmdir($path); 297 } else { 298 return @unlink($path); 299 } 300 301 return false; 302} 303 304/** 305 * Return sub index for index view 306 * 307 * @author Andreas Gohr <andi@splitbrain.org> 308 */ 309function ajax_index(){ 310 global $conf; 311 312 // wanted namespace 313 $ns = cleanID($_POST['idx']); 314 $dir = utf8_encodeFN(str_replace(':','/',$ns)); 315 316 $lvl = count(explode(':',$ns)); 317 318 $data = array(); 319 search($data,$conf['datadir'],'search_index',array('ns' => $ns),$dir); 320 foreach(array_keys($data) as $item){ 321 $data[$item]['level'] = $lvl+1; 322 } 323 echo html_buildlist($data, 'idx', 'html_list_index', 'html_li_index'); 324} 325 326/** 327 * List matching namespaces and pages for the link wizard 328 * 329 * @author Andreas Gohr <gohr@cosmocode.de> 330 */ 331function ajax_linkwiz(){ 332 global $conf; 333 global $lang; 334 335 $q = ltrim(trim($_POST['q']),':'); 336 $id = noNS($q); 337 $ns = getNS($q); 338 339 $ns = cleanID($ns); 340 $id = cleanID($id); 341 342 $nsd = utf8_encodeFN(str_replace(':','/',$ns)); 343 $idd = utf8_encodeFN(str_replace(':','/',$id)); 344 345 $data = array(); 346 if($q && !$ns){ 347 348 // use index to lookup matching pages 349 $pages = array(); 350 $pages = ft_pageLookup($id,true); 351 352 // result contains matches in pages and namespaces 353 // we now extract the matching namespaces to show 354 // them seperately 355 $dirs = array(); 356 357 foreach($pages as $pid => $title){ 358 if(strpos(noNS($pid),$id) === false){ 359 // match was in the namespace 360 $dirs[getNS($pid)] = 1; // assoc array avoids dupes 361 }else{ 362 // it is a matching page, add it to the result 363 $data[] = array( 364 'id' => $pid, 365 'title' => $title, 366 'type' => 'f', 367 ); 368 } 369 unset($pages[$pid]); 370 } 371 foreach($dirs as $dir => $junk){ 372 $data[] = array( 373 'id' => $dir, 374 'type' => 'd', 375 ); 376 } 377 378 }else{ 379 380 $opts = array( 381 'depth' => 1, 382 'listfiles' => true, 383 'listdirs' => true, 384 'pagesonly' => true, 385 'firsthead' => true, 386 'sneakyacl' => $conf['sneaky_index'], 387 ); 388 if($id) $opts['filematch'] = '^.*\/'.$id; 389 if($id) $opts['dirmatch'] = '^.*\/'.$id; 390 search($data,$conf['datadir'],'search_universal',$opts,$nsd); 391 392 // add back to upper 393 if($ns){ 394 array_unshift($data,array( 395 'id' => getNS($ns), 396 'type' => 'u', 397 )); 398 } 399 } 400 401 // fixme sort results in a useful way ? 402 403 if(!count($data)){ 404 echo $lang['nothingfound']; 405 exit; 406 } 407 408 // output the found data 409 $even = 1; 410 foreach($data as $item){ 411 $even *= -1; //zebra 412 413 if(($item['type'] == 'd' || $item['type'] == 'u') && $item['id']) $item['id'] .= ':'; 414 $link = wl($item['id']); 415 416 echo '<div class="'.(($even > 0)?'even':'odd').' type_'.$item['type'].'">'; 417 418 if($item['type'] == 'u'){ 419 $name = $lang['upperns']; 420 }else{ 421 $name = htmlspecialchars($item['id']); 422 } 423 424 echo '<a href="'.$link.'" title="'.htmlspecialchars($item['id']).'" class="wikilink1">'.$name.'</a>'; 425 426 if($item['title']){ 427 echo '<span>'.htmlspecialchars($item['title']).'</span>'; 428 } 429 echo '</div>'; 430 } 431 432} 433 434//Setup VIM: ex: et ts=2 : 435