1<?php 2if(!defined('DOKU_INC')) define('DOKU_INC',dirname(__FILE__).'/../../'); 3 4// fix when '<?xml' isn't on the very first line 5if(isset($HTTP_RAW_POST_DATA)) $HTTP_RAW_POST_DATA = trim($HTTP_RAW_POST_DATA); 6 7 8require_once(DOKU_INC.'inc/init.php'); 9require_once(DOKU_INC.'inc/common.php'); 10require_once(DOKU_INC.'inc/auth.php'); 11session_write_close(); //close session 12 13if(!$conf['xmlrpc']) { 14 die('XML-RPC server not enabled.'); 15 // FIXME check for groups allowed 16} 17 18require_once(DOKU_INC.'inc/IXR_Library.php'); 19 20 21/** 22 * Contains needed wrapper functions and registers all available 23 * XMLRPC functions. 24 */ 25class dokuwiki_xmlrpc_server extends IXR_IntrospectionServer { 26 var $methods = array(); 27 28 /** 29 * Constructor. Register methods and run Server 30 */ 31 function dokuwiki_xmlrpc_server(){ 32 $this->IXR_IntrospectionServer(); 33 34 /* DokuWiki's own methods */ 35 $this->addCallback( 36 'dokuwiki.getVersion', 37 'getVersion', 38 array('string'), 39 'Returns the running DokuWiki version.' 40 ); 41 42 /* Wiki API v2 http://www.jspwiki.org/wiki/WikiRPCInterface2 */ 43 $this->addCallback( 44 'wiki.getRPCVersionSupported', 45 'this:wiki_RPCVersion', 46 array('int'), 47 'Returns 2 with the supported RPC API version.' 48 ); 49 $this->addCallback( 50 'wiki.getPage', 51 'this:rawPage', 52 array('string','string'), 53 'Get the raw Wiki text of page, latest version.' 54 ); 55 $this->addCallback( 56 'wiki.getPageVersion', 57 'this:rawPage', 58 array('string','string','int'), 59 'Get the raw Wiki text of page.' 60 ); 61 $this->addCallback( 62 'wiki.getPageHTML', 63 'this:htmlPage', 64 array('string','string'), 65 'Return page in rendered HTML, latest version.' 66 ); 67 $this->addCallback( 68 'wiki.getPageHTMLVersion', 69 'this:htmlPage', 70 array('string','string','int'), 71 'Return page in rendered HTML.' 72 ); 73 $this->addCallback( 74 'wiki.getAllPages', 75 'this:listPages', 76 array('struct'), 77 'Returns a list of all pages. The result is an array of utf8 pagenames.' 78 ); 79 $this->addCallback( 80 'wiki.getAttachments', 81 'this:listAttachments', 82 array('struct'), 83 'Returns a list of all media files.' 84 ); 85 $this->addCallback( 86 'wiki.getBackLinks', 87 'this:listBackLinks', 88 array('struct','string'), 89 'Returns the pages that link to this page.' 90 ); 91 $this->addCallback( 92 'wiki.getPageInfo', 93 'this:pageInfo', 94 array('struct','string'), 95 'Returns a struct with infos about the page.' 96 ); 97 $this->addCallback( 98 'wiki.getPageInfoVersion', 99 'this:pageInfo', 100 array('struct','string','int'), 101 'Returns a struct with infos about the page.' 102 ); 103 $this->addCallback( 104 'wiki.getPageVersions', 105 'this:pageVersions', 106 array('struct','string','int'), 107 'Returns the available revisions of the page.' 108 ); 109 $this->addCallback( 110 'wiki.putPage', 111 'this:putPage', 112 array('int', 'string', 'string', 'struct'), 113 'Saves a wiki page.' 114 ); 115 $this->addCallback( 116 'wiki.listLinks', 117 'this:listLinks', 118 array('struct','string'), 119 'Lists all links contained in a wiki page.' 120 ); 121 $this->addCallback( 122 'wiki.getRecentChanges', 123 'this:getRecentChanges', 124 array('struct','int'), 125 'Returns a strukt about all recent changes since given timestamp.' 126 ); 127 $this->addCallback( 128 'wiki.aclCheck', 129 'this:aclCheck', 130 array('struct', 'string'), 131 'Returns the permissions of a given wiki page.' 132 ); 133 $this->addCallback( 134 'wiki.putAttachment', 135 'this:putAttachment', 136 array('struct', 'string', 'base64', 'struct'), 137 'Upload a file to the wiki.' 138 ); 139 140 $this->serve(); 141 } 142 143 /** 144 * Return a raw wiki page 145 */ 146 function rawPage($id,$rev=''){ 147 if(auth_quickaclcheck($id) < AUTH_READ){ 148 return new IXR_Error(1, 'You are not allowed to read this page'); 149 } 150 $text = rawWiki($id,$rev); 151 if(!$text) { 152 $data = array($id); 153 return trigger_event('HTML_PAGE_FROMTEMPLATE',$data,'pageTemplate',true); 154 } else { 155 return $text; 156 } 157 } 158 159 /** 160 * Return a wiki page rendered to html 161 */ 162 function htmlPage($id,$rev=''){ 163 if(auth_quickaclcheck($id) < AUTH_READ){ 164 return new IXR_Error(1, 'You are not allowed to read this page'); 165 } 166 return p_wiki_xhtml($id,$rev,false); 167 } 168 169 /** 170 * List all pages - we use the indexer list here 171 */ 172 function listPages(){ 173 require_once(DOKU_INC.'inc/fulltext.php'); 174 return ft_pageLookup(''); 175 } 176 177 /** 178 * List all media files. 179 */ 180 function listAttachments($ns) { 181 global $conf; 182 global $lang; 183 184 $ns = cleanID($ns); 185 186 if(auth_quickaclcheck($ns.':*') >= AUTH_READ) { 187 $dir = utf8_encodeFN(str_replace(':', '/', $ns)); 188 189 $data = array(); 190 require_once(DOKU_INC.'inc/search.php'); 191 search($data, $conf['mediadir'], 'search_media', array(), $dir); 192 193 if(!count($data)) { 194 return array(); 195 } 196 197 $files = array(); 198 foreach($data as $item) { 199 $file = array(); 200 $file['id'] = $item['id']; 201 $file['size'] = $item['size']; 202 $file['mtime'] = $item['mtime']; 203 $file['isimg'] = $item['isimg']; 204 $file['writable'] = $item['writeable']; 205 array_push($files, $file); 206 } 207 208 return $files; 209 210 } else { 211 return new IXR_Error(1, 'You are not allowed to list media files.'); 212 } 213 } 214 215 /** 216 * Return a list of backlinks 217 */ 218 function listBackLinks($id){ 219 require_once(DOKU_INC.'inc/fulltext.php'); 220 return ft_backlinks($id); 221 } 222 223 /** 224 * Return some basic data about a page 225 */ 226 function pageInfo($id,$rev=''){ 227 if(auth_quickaclcheck($id) < AUTH_READ){ 228 return new IXR_Error(1, 'You are not allowed to read this page'); 229 } 230 $file = wikiFN($id,$rev); 231 $time = @filemtime($file); 232 if(!$time){ 233 return new IXR_Error(10, 'The requested page does not exist'); 234 } 235 236 $info = getRevisionInfo($id, $time, 1024); 237 238 $data = array( 239 'name' => $id, 240 'lastModified' => new IXR_Date($time), 241 'author' => (($info['user']) ? $info['user'] : $info['ip']), 242 'version' => $time 243 ); 244 245 return ($data); 246 } 247 248 /** 249 * Save a wiki page 250 * 251 * @author Michael Klier <chi@chimeric.de> 252 */ 253 function putPage($id, $text, $params) { 254 global $TEXT; 255 global $lang; 256 global $conf; 257 258 $id = cleanID($id); 259 $TEXT = trim($text); 260 $sum = $params['sum']; 261 $minor = $params['minor']; 262 263 if(empty($id)) 264 return new IXR_Error(1, 'Empty page ID'); 265 266 if(!page_exists($id) && empty($TEXT)) { 267 return new IXR_ERROR(1, 'Refusing to write an empty new wiki page'); 268 } 269 270 if(auth_quickaclcheck($id) < AUTH_EDIT) 271 return new IXR_Error(1, 'You are not allowed to edit this page'); 272 273 // Check, if page is locked 274 if(checklock($id)) 275 return new IXR_Error(1, 'The page is currently locked'); 276 277 // SPAM check 278 if(checkwordblock()) 279 return new IXR_Error(1, 'Positive wordblock check'); 280 281 // autoset summary on new pages 282 if(!page_exists($id) && empty($sum)) { 283 $sum = $lang['created']; 284 } 285 286 // autoset summary on deleted pages 287 if(page_exists($id) && empty($TEXT) && empty($sum)) { 288 $sum = $lang['deleted']; 289 } 290 291 lock($id); 292 293 saveWikiText($id,$TEXT,$sum,$minor); 294 295 unlock($id); 296 297 // run the indexer if page wasn't indexed yet 298 if(!@file_exists(metaFN($id, '.indexed'))) { 299 // try to aquire a lock 300 $lock = $conf['lockdir'].'/_indexer.lock'; 301 while(!@mkdir($lock,$conf['dmode'])){ 302 usleep(50); 303 if(time()-@filemtime($lock) > 60*5){ 304 // looks like a stale lock - remove it 305 @rmdir($lock); 306 }else{ 307 return false; 308 } 309 } 310 if($conf['dperm']) chmod($lock, $conf['dperm']); 311 312 require_once(DOKU_INC.'inc/indexer.php'); 313 314 // do the work 315 idx_addPage($id); 316 317 // we're finished - save and free lock 318 io_saveFile(metaFN($id,'.indexed'),INDEXER_VERSION); 319 @rmdir($lock); 320 } 321 322 return 0; 323 } 324 325 /** 326 * Uploads a file to the wiki. 327 * 328 * Michael Klier <chi@chimeric.de> 329 */ 330 function putAttachment($ns, $file, $params) { 331 global $conf; 332 global $lang; 333 334 $auth = auth_quickaclcheck($ns.':*'); 335 if($auth >= AUTH_UPLOAD) { 336 if(!isset($params['name'])) { 337 return new IXR_ERROR(1, 'Filename not given.'); 338 } 339 340 $ftmp = $conf['tmpdir'] . '/' . $params['name']; 341 $name = $params['name']; 342 343 // save temporary file 344 @unlink($ftmp); 345 $buff = base64_decode($file); 346 io_saveFile($ftmp, $buff); 347 348 // get filename 349 list($iext, $imime) = mimetype($name); 350 $id = cleanID($ns.':'.$name); 351 $fn = mediaFN($id); 352 353 // get filetype regexp 354 $types = array_keys(getMimeTypes()); 355 $types = array_map(create_function('$q','return preg_quote($q,"/");'),$types); 356 $regex = join('|',$types); 357 358 // because a temp file was created already 359 if(preg_match('/\.('.$regex.')$/i',$fn)) { 360 //check for overwrite 361 if(@file_exists($fn) && (!$params['ow'] || $auth < AUTH_DELETE)) { 362 return new IXR_ERROR(1, $lang['uploadexist']); 363 } 364 // check for valid content 365 @require_once(DOKU_INC.'inc/media.php'); 366 $ok = media_contentcheck($ftmp, $imime); 367 if($ok == -1) { 368 return new IXR_ERROR(1, sprintf($lang['uploadexist'], ".$iext")); 369 } elseif($ok == -2) { 370 return new IXR_ERROR(1, $lang['uploadspam']); 371 } elseif($ok == -3) { 372 return new IXR_ERROR(1, $lang['uploadxss']); 373 } 374 375 // prepare event data 376 $data[0] = $ftmp; 377 $data[1] = $fn; 378 $data[2] = $id; 379 $data[3] = $imime; 380 381 // trigger event 382 require_once(DOKU_INC.'inc/events.php'); 383 return trigger_event('MEDIA_UPLOAD_FINISH', $data, array($this, '_media_upload_action'), true); 384 385 } else { 386 return new IXR_ERROR(1, $lang['uploadwrong']); 387 } 388 } else { 389 return new IXR_ERROR(1, "You don't have permissions to upload files."); 390 } 391 } 392 393 /** 394 * Moves the temporary file to its final destination. 395 * 396 * Michael Klier <chi@chimeric.de> 397 */ 398 function _media_upload_action($data) { 399 global $conf; 400 401 if(is_array($data) && count($data)===4) { 402 io_createNamespace($data[2], 'media'); 403 if(rename($data[0], $data[1])) { 404 chmod($data[1], $conf['fmode']); 405 media_notify($data[2], $data[1], $data[3]); 406 return $data[2]; 407 } else { 408 return new IXR_ERROR(1, 'Upload failed.'); 409 } 410 } else { 411 return new IXR_ERROR(1, 'Upload failed.'); 412 } 413 } 414 415 /** 416 * Returns the permissions of a given wiki page 417 */ 418 function aclCheck($id) { 419 return auth_quickaclcheck($id); 420 } 421 422 /** 423 * Lists all links contained in a wiki page 424 * 425 * @author Michael Klier <chi@chimeric.de> 426 */ 427 function listLinks($id) { 428 if(auth_quickaclcheck($id) < AUTH_READ){ 429 return new IXR_Error(1, 'You are not allowed to read this page'); 430 } 431 $links = array(); 432 433 // resolve page instructions 434 $ins = p_cached_instructions(wikiFN(cleanID($id))); 435 436 // instantiate new Renderer - needed for interwiki links 437 include(DOKU_INC.'inc/parser/xhtml.php'); 438 $Renderer = new Doku_Renderer_xhtml(); 439 $Renderer->interwiki = getInterwiki(); 440 441 // parse parse instructions 442 foreach($ins as $in) { 443 $link = array(); 444 switch($in[0]) { 445 case 'internallink': 446 $link['type'] = 'local'; 447 $link['page'] = $in[1][0]; 448 $link['href'] = wl($in[1][0]); 449 array_push($links,$link); 450 break; 451 case 'externallink': 452 $link['type'] = 'extern'; 453 $link['page'] = $in[1][0]; 454 $link['href'] = $in[1][0]; 455 array_push($links,$link); 456 break; 457 case 'interwikilink': 458 $url = $Renderer->_resolveInterWiki($in[1][2],$in[1][3]); 459 $link['type'] = 'extern'; 460 $link['page'] = $url; 461 $link['href'] = $url; 462 array_push($links,$link); 463 break; 464 } 465 } 466 467 return ($links); 468 } 469 470 /** 471 * Returns a list of recent changes since give timestamp 472 * 473 * @author Michael Klier <chi@chimeric.de> 474 */ 475 function getRecentChanges($timestamp) { 476 global $conf; 477 478 if(strlen($timestamp) != 10) 479 return new IXR_Error(20, 'The provided value is not a valid timestamp'); 480 481 $changes = array(); 482 483 require_once(DOKU_INC.'inc/changelog.php'); 484 require_once(DOKU_INC.'inc/pageutils.php'); 485 486 // read changes 487 $lines = @file($conf['changelog']); 488 489 if(empty($lines)) 490 return new IXR_Error(10, 'The changelog could not be read'); 491 492 // we start searching at the end of the list 493 $lines = array_reverse($lines); 494 495 // cache seen pages and skip them 496 $seen = array(); 497 498 foreach($lines as $line) { 499 500 if(empty($line)) continue; // skip empty lines 501 502 $logline = parseChangelogLine($line); 503 504 if($logline === false) continue; 505 506 // skip seen ones 507 if(isset($seen[$logline['id']])) continue; 508 509 // skip minors 510 if($logline['type'] === DOKU_CHANGE_TYPE_MINOR_EDIT && ($flags & RECENTS_SKIP_MINORS)) continue; 511 512 // remember in seen to skip additional sights 513 $seen[$logline['id']] = 1; 514 515 // check if it's a hidden page 516 if(isHiddenPage($logline['id'])) continue; 517 518 // check ACL 519 if(auth_quickaclcheck($logline['id']) < AUTH_READ) continue; 520 521 // check existance 522 if((!@file_exists(wikiFN($logline['id']))) && ($flags & RECENTS_SKIP_DELETED)) continue; 523 524 // check if logline is still in the queried time frame 525 if($logline['date'] >= $timestamp) { 526 $change['name'] = $logline['id']; 527 $change['lastModified'] = new IXR_Date($logline['date']); 528 $change['author'] = $logline['user']; 529 $change['version'] = $logline['date']; 530 array_push($changes, $change); 531 } else { 532 $changes = array_reverse($changes); 533 return ($changes); 534 } 535 } 536 // in case we still have nothing at this point 537 return new IXR_Error(30, 'There are no changes in the specified timeframe'); 538 } 539 540 /** 541 * Returns a list of available revisions of a given wiki page 542 * 543 * @author Michael Klier <chi@chimeric.de> 544 */ 545 function pageVersions($id, $first) { 546 global $conf; 547 548 $versions = array(); 549 550 if(empty($id)) 551 return new IXR_Error(1, 'Empty page ID'); 552 553 require_once(DOKU_INC.'inc/changelog.php'); 554 555 $revisions = getRevisions($id, $first, $conf['recent']+1); 556 557 if(count($revisions)==0 && $first!=0) { 558 $first=0; 559 $revisions = getRevisions($id, $first, $conf['recent']+1); 560 } 561 562 if(count($revisions)>0 && $first==0) { 563 array_unshift($revisions, ''); // include current revision 564 array_pop($revisions); // remove extra log entry 565 } 566 567 $hasNext = false; 568 if(count($revisions)>$conf['recent']) { 569 $hasNext = true; 570 array_pop($revisions); // remove extra log entry 571 } 572 573 if(!empty($revisions)) { 574 foreach($revisions as $rev) { 575 $file = wikiFN($id,$rev); 576 $time = @filemtime($file); 577 // we check if the page actually exists, if this is not the 578 // case this can lead to less pages being returned than 579 // specified via $conf['recent'] 580 if($time){ 581 $info = getRevisionInfo($id, $time, 1024); 582 if(!empty($info)) { 583 $data['user'] = $info['user']; 584 $data['ip'] = $info['ip']; 585 $data['type'] = $info['type']; 586 $data['sum'] = $info['sum']; 587 $data['modified'] = new IXR_Date($info['date']); 588 $data['version'] = $info['date']; 589 array_push($versions, $data); 590 } 591 } 592 } 593 return $versions; 594 } else { 595 return array(); 596 } 597 } 598 599 /** 600 * The version of Wiki RPC API supported 601 */ 602 function wiki_RPCVersion(){ 603 return 2; 604 } 605} 606 607$server = new dokuwiki_xmlrpc_server(); 608 609// vim:ts=4:sw=4:et:enc=utf-8: 610