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', 'string', '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('int', '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 $this->addCallback( 140 'wiki.getAttachment', 141 'this:getAttachment', 142 array('base64', 'string'), 143 'Download a file from the wiki.' 144 ); 145 $this->addCallback( 146 'wiki.getAttachmentInfo', 147 'this:getAttachmentInfo', 148 array('struct', 'string'), 149 'Returns a struct with infos about the attachment.' 150 ); 151 152 /** 153 * Trigger XMLRPC_CALLBACK_REGISTER, action plugins can use this event 154 * to extend the XMLRPC interface and register their own callbacks. 155 * 156 * Event data: 157 * The XMLRPC server object: 158 * 159 * $event->data->addCallback() - register a callback, the second 160 * paramter has to be of the form "plugin:<pluginname>:<plugin 161 * method>" 162 * 163 * $event->data->callbacks - an array which holds all awaylable 164 * callbacks 165 */ 166 trigger_event('XMLRPC_CALLBACK_REGISTER', $this); 167 168 $this->serve(); 169 } 170 171 /** 172 * Return a raw wiki page 173 */ 174 function rawPage($id,$rev=''){ 175 if(auth_quickaclcheck($id) < AUTH_READ){ 176 return new IXR_Error(1, 'You are not allowed to read this page'); 177 } 178 $text = rawWiki($id,$rev); 179 if(!$text) { 180 $data = array($id); 181 return trigger_event('HTML_PAGE_FROMTEMPLATE',$data,'pageTemplate',true); 182 } else { 183 return $text; 184 } 185 } 186 187 /** 188 * Return a media file encoded in base64 189 * 190 * @author Gina Haeussge <osd@foosel.net> 191 */ 192 function getAttachment($id){ 193 $id = cleanID($id); 194 if (auth_quickaclcheck(getNS($id).':*') < AUTH_READ) 195 return new IXR_Error(1, 'You are not allowed to read this file'); 196 197 $file = mediaFN($id); 198 if (!@ file_exists($file)) 199 return new IXR_Error(1, 'The requested file does not exist'); 200 201 $data = io_readFile($file, false); 202 $base64 = base64_encode($data); 203 return $base64; 204 } 205 206 /** 207 * Return info about a media file 208 * 209 * @author Gina Haeussge <osd@foosel.net> 210 */ 211 function getAttachmentInfo($id){ 212 $id = cleanID($id); 213 $info = array( 214 'lastModified' => 0, 215 'size' => 0, 216 ); 217 218 $file = mediaFN($id); 219 if ((auth_quickaclcheck(getNS($id).':*') >= AUTH_READ) && file_exists($file)){ 220 $info['lastModified'] = new IXR_Date(filemtime($file)); 221 $info['size'] = filesize($file); 222 } 223 224 return $info; 225 } 226 227 /** 228 * Return a wiki page rendered to html 229 */ 230 function htmlPage($id,$rev=''){ 231 if(auth_quickaclcheck($id) < AUTH_READ){ 232 return new IXR_Error(1, 'You are not allowed to read this page'); 233 } 234 return p_wiki_xhtml($id,$rev,false); 235 } 236 237 /** 238 * List all pages - we use the indexer list here 239 */ 240 function listPages(){ 241 global $conf; 242 243 $list = array(); 244 $pages = file($conf['indexdir'] . '/page.idx'); 245 $pages = array_filter($pages, 'isVisiblePage'); 246 247 foreach(array_keys($pages) as $idx) { 248 if(page_exists($pages[$idx])) { 249 $perm = auth_quickaclcheck($pages[$idx]); 250 if($perm >= AUTH_READ) { 251 $page = array(); 252 $page['id'] = trim($pages[$idx]); 253 $page['perms'] = $perm; 254 $page['size'] = @filesize(wikiFN($pages[$idx])); 255 $page['lastModified'] = new IXR_Date(@filemtime($pages[$idx])); 256 $list[] = $page; 257 } 258 } 259 } 260 261 return $list; 262 } 263 264 /** 265 * List all media files. 266 * 267 * Available options are 'recursive' for also including the subnamespaces 268 * in the listing, and 'pattern' for filtering the returned files against 269 * a regular expression matching their name. 270 * 271 * @author Gina Haeussge <osd@foosel.net> 272 */ 273 function listAttachments($ns, $options = array()) { 274 global $conf; 275 global $lang; 276 277 $ns = cleanID($ns); 278 279 if (!is_array($options)) 280 $options = array(); 281 282 if (!isset($options['recursive'])) $options['recursive'] = false; 283 284 if(auth_quickaclcheck($ns.':*') >= AUTH_READ) { 285 $dir = utf8_encodeFN(str_replace(':', '/', $ns)); 286 287 $data = array(); 288 require_once(DOKU_INC.'inc/search.php'); 289 search($data, $conf['mediadir'], 'search_media', array('recursive' => $options['recursive']), $dir); 290 291 if(!count($data)) { 292 return array(); 293 } 294 295 $files = array(); 296 foreach($data as $item) { 297 if (isset($options['pattern']) && !@preg_match($options['pattern'], $item['id'])) 298 continue; 299 $file = array(); 300 $file['id'] = $item['id']; 301 $file['size'] = $item['size']; 302 $file['lastModified'] = $item['mtime']; 303 $file['isimg'] = $item['isimg']; 304 $file['writable'] = $item['writeable']; 305 $file['perms'] = auth_quickaclcheck(getNS($item['id']).':*'); 306 array_push($files, $file); 307 } 308 309 return $files; 310 311 } else { 312 return new IXR_Error(1, 'You are not allowed to list media files.'); 313 } 314 } 315 316 /** 317 * Return a list of backlinks 318 */ 319 function listBackLinks($id){ 320 require_once(DOKU_INC.'inc/fulltext.php'); 321 return ft_backlinks($id); 322 } 323 324 /** 325 * Return some basic data about a page 326 */ 327 function pageInfo($id,$rev=''){ 328 if(auth_quickaclcheck($id) < AUTH_READ){ 329 return new IXR_Error(1, 'You are not allowed to read this page'); 330 } 331 $file = wikiFN($id,$rev); 332 $time = @filemtime($file); 333 if(!$time){ 334 return new IXR_Error(10, 'The requested page does not exist'); 335 } 336 337 $info = getRevisionInfo($id, $time, 1024); 338 339 $data = array( 340 'name' => $id, 341 'lastModified' => new IXR_Date($time), 342 'author' => (($info['user']) ? $info['user'] : $info['ip']), 343 'version' => $time 344 ); 345 346 return ($data); 347 } 348 349 /** 350 * Save a wiki page 351 * 352 * @author Michael Klier <chi@chimeric.de> 353 */ 354 function putPage($id, $text, $params) { 355 global $TEXT; 356 global $lang; 357 global $conf; 358 359 $id = cleanID($id); 360 $TEXT = trim($text); 361 $sum = $params['sum']; 362 $minor = $params['minor']; 363 364 if(empty($id)) 365 return new IXR_Error(1, 'Empty page ID'); 366 367 if(!page_exists($id) && empty($TEXT)) { 368 return new IXR_ERROR(1, 'Refusing to write an empty new wiki page'); 369 } 370 371 if(auth_quickaclcheck($id) < AUTH_EDIT) 372 return new IXR_Error(1, 'You are not allowed to edit this page'); 373 374 // Check, if page is locked 375 if(checklock($id)) 376 return new IXR_Error(1, 'The page is currently locked'); 377 378 // SPAM check 379 if(checkwordblock()) 380 return new IXR_Error(1, 'Positive wordblock check'); 381 382 // autoset summary on new pages 383 if(!page_exists($id) && empty($sum)) { 384 $sum = $lang['created']; 385 } 386 387 // autoset summary on deleted pages 388 if(page_exists($id) && empty($TEXT) && empty($sum)) { 389 $sum = $lang['deleted']; 390 } 391 392 lock($id); 393 394 saveWikiText($id,$TEXT,$sum,$minor); 395 396 unlock($id); 397 398 // run the indexer if page wasn't indexed yet 399 if(!@file_exists(metaFN($id, '.indexed'))) { 400 // try to aquire a lock 401 $lock = $conf['lockdir'].'/_indexer.lock'; 402 while(!@mkdir($lock,$conf['dmode'])){ 403 usleep(50); 404 if(time()-@filemtime($lock) > 60*5){ 405 // looks like a stale lock - remove it 406 @rmdir($lock); 407 }else{ 408 return false; 409 } 410 } 411 if($conf['dperm']) chmod($lock, $conf['dperm']); 412 413 require_once(DOKU_INC.'inc/indexer.php'); 414 415 // do the work 416 idx_addPage($id); 417 418 // we're finished - save and free lock 419 io_saveFile(metaFN($id,'.indexed'),INDEXER_VERSION); 420 @rmdir($lock); 421 } 422 423 return 0; 424 } 425 426 /** 427 * Uploads a file to the wiki. 428 * 429 * Michael Klier <chi@chimeric.de> 430 */ 431 function putAttachment($ns, $file, $params) { 432 global $conf; 433 global $lang; 434 435 $auth = auth_quickaclcheck($ns.':*'); 436 if($auth >= AUTH_UPLOAD) { 437 if(!isset($params['name'])) { 438 return new IXR_ERROR(1, 'Filename not given.'); 439 } 440 441 $ftmp = $conf['tmpdir'] . '/' . $params['name']; 442 $name = $params['name']; 443 444 // save temporary file 445 @unlink($ftmp); 446 $buff = base64_decode($file); 447 io_saveFile($ftmp, $buff); 448 449 // get filename 450 list($iext, $imime) = mimetype($name); 451 $id = cleanID($ns.':'.$name); 452 $fn = mediaFN($id); 453 454 // get filetype regexp 455 $types = array_keys(getMimeTypes()); 456 $types = array_map(create_function('$q','return preg_quote($q,"/");'),$types); 457 $regex = join('|',$types); 458 459 // because a temp file was created already 460 if(preg_match('/\.('.$regex.')$/i',$fn)) { 461 //check for overwrite 462 if(@file_exists($fn) && (!$params['ow'] || $auth < AUTH_DELETE)) { 463 return new IXR_ERROR(1, $lang['uploadexist']); 464 } 465 // check for valid content 466 @require_once(DOKU_INC.'inc/media.php'); 467 $ok = media_contentcheck($ftmp, $imime); 468 if($ok == -1) { 469 return new IXR_ERROR(1, sprintf($lang['uploadexist'], ".$iext")); 470 } elseif($ok == -2) { 471 return new IXR_ERROR(1, $lang['uploadspam']); 472 } elseif($ok == -3) { 473 return new IXR_ERROR(1, $lang['uploadxss']); 474 } 475 476 // prepare event data 477 $data[0] = $ftmp; 478 $data[1] = $fn; 479 $data[2] = $id; 480 $data[3] = $imime; 481 482 // trigger event 483 require_once(DOKU_INC.'inc/events.php'); 484 return trigger_event('MEDIA_UPLOAD_FINISH', $data, array($this, '_media_upload_action'), true); 485 486 } else { 487 return new IXR_ERROR(1, $lang['uploadwrong']); 488 } 489 } else { 490 return new IXR_ERROR(1, "You don't have permissions to upload files."); 491 } 492 } 493 494 /** 495 * Moves the temporary file to its final destination. 496 * 497 * Michael Klier <chi@chimeric.de> 498 */ 499 function _media_upload_action($data) { 500 global $conf; 501 502 if(is_array($data) && count($data)===4) { 503 io_createNamespace($data[2], 'media'); 504 if(rename($data[0], $data[1])) { 505 chmod($data[1], $conf['fmode']); 506 media_notify($data[2], $data[1], $data[3]); 507 return $data[2]; 508 } else { 509 return new IXR_ERROR(1, 'Upload failed.'); 510 } 511 } else { 512 return new IXR_ERROR(1, 'Upload failed.'); 513 } 514 } 515 516 /** 517 * Returns the permissions of a given wiki page 518 */ 519 function aclCheck($id) { 520 return auth_quickaclcheck($id); 521 } 522 523 /** 524 * Lists all links contained in a wiki page 525 * 526 * @author Michael Klier <chi@chimeric.de> 527 */ 528 function listLinks($id) { 529 if(auth_quickaclcheck($id) < AUTH_READ){ 530 return new IXR_Error(1, 'You are not allowed to read this page'); 531 } 532 $links = array(); 533 534 // resolve page instructions 535 $ins = p_cached_instructions(wikiFN(cleanID($id))); 536 537 // instantiate new Renderer - needed for interwiki links 538 include(DOKU_INC.'inc/parser/xhtml.php'); 539 $Renderer = new Doku_Renderer_xhtml(); 540 $Renderer->interwiki = getInterwiki(); 541 542 // parse parse instructions 543 foreach($ins as $in) { 544 $link = array(); 545 switch($in[0]) { 546 case 'internallink': 547 $link['type'] = 'local'; 548 $link['page'] = $in[1][0]; 549 $link['href'] = wl($in[1][0]); 550 array_push($links,$link); 551 break; 552 case 'externallink': 553 $link['type'] = 'extern'; 554 $link['page'] = $in[1][0]; 555 $link['href'] = $in[1][0]; 556 array_push($links,$link); 557 break; 558 case 'interwikilink': 559 $url = $Renderer->_resolveInterWiki($in[1][2],$in[1][3]); 560 $link['type'] = 'extern'; 561 $link['page'] = $url; 562 $link['href'] = $url; 563 array_push($links,$link); 564 break; 565 } 566 } 567 568 return ($links); 569 } 570 571 /** 572 * Returns a list of recent changes since give timestamp 573 * 574 * @author Michael Klier <chi@chimeric.de> 575 */ 576 function getRecentChanges($timestamp) { 577 global $conf; 578 579 if(strlen($timestamp) != 10) 580 return new IXR_Error(20, 'The provided value is not a valid timestamp'); 581 582 $changes = array(); 583 584 require_once(DOKU_INC.'inc/changelog.php'); 585 require_once(DOKU_INC.'inc/pageutils.php'); 586 587 // read changes 588 $lines = @file($conf['changelog']); 589 590 if(empty($lines)) 591 return new IXR_Error(10, 'The changelog could not be read'); 592 593 // we start searching at the end of the list 594 $lines = array_reverse($lines); 595 596 // cache seen pages and skip them 597 $seen = array(); 598 599 foreach($lines as $line) { 600 601 if(empty($line)) continue; // skip empty lines 602 603 $logline = parseChangelogLine($line); 604 605 if($logline === false) continue; 606 607 // skip seen ones 608 if(isset($seen[$logline['id']])) continue; 609 610 // skip minors 611 if($logline['type'] === DOKU_CHANGE_TYPE_MINOR_EDIT && ($flags & RECENTS_SKIP_MINORS)) continue; 612 613 // remember in seen to skip additional sights 614 $seen[$logline['id']] = 1; 615 616 // check if it's a hidden page 617 if(isHiddenPage($logline['id'])) continue; 618 619 // check ACL 620 if(auth_quickaclcheck($logline['id']) < AUTH_READ) continue; 621 622 // check existance 623 if((!@file_exists(wikiFN($logline['id']))) && ($flags & RECENTS_SKIP_DELETED)) continue; 624 625 // check if logline is still in the queried time frame 626 if($logline['date'] >= $timestamp) { 627 $change['name'] = $logline['id']; 628 $change['lastModified'] = new IXR_Date($logline['date']); 629 $change['author'] = $logline['user']; 630 $change['version'] = $logline['date']; 631 array_push($changes, $change); 632 } else { 633 $changes = array_reverse($changes); 634 return ($changes); 635 } 636 } 637 // in case we still have nothing at this point 638 return new IXR_Error(30, 'There are no changes in the specified timeframe'); 639 } 640 641 /** 642 * Returns a list of available revisions of a given wiki page 643 * 644 * @author Michael Klier <chi@chimeric.de> 645 */ 646 function pageVersions($id, $first) { 647 global $conf; 648 649 $versions = array(); 650 651 if(empty($id)) 652 return new IXR_Error(1, 'Empty page ID'); 653 654 require_once(DOKU_INC.'inc/changelog.php'); 655 656 $revisions = getRevisions($id, $first, $conf['recent']+1); 657 658 if(count($revisions)==0 && $first!=0) { 659 $first=0; 660 $revisions = getRevisions($id, $first, $conf['recent']+1); 661 } 662 663 if(count($revisions)>0 && $first==0) { 664 array_unshift($revisions, ''); // include current revision 665 array_pop($revisions); // remove extra log entry 666 } 667 668 $hasNext = false; 669 if(count($revisions)>$conf['recent']) { 670 $hasNext = true; 671 array_pop($revisions); // remove extra log entry 672 } 673 674 if(!empty($revisions)) { 675 foreach($revisions as $rev) { 676 $file = wikiFN($id,$rev); 677 $time = @filemtime($file); 678 // we check if the page actually exists, if this is not the 679 // case this can lead to less pages being returned than 680 // specified via $conf['recent'] 681 if($time){ 682 $info = getRevisionInfo($id, $time, 1024); 683 if(!empty($info)) { 684 $data['user'] = $info['user']; 685 $data['ip'] = $info['ip']; 686 $data['type'] = $info['type']; 687 $data['sum'] = $info['sum']; 688 $data['modified'] = new IXR_Date($info['date']); 689 $data['version'] = $info['date']; 690 array_push($versions, $data); 691 } 692 } 693 } 694 return $versions; 695 } else { 696 return array(); 697 } 698 } 699 700 /** 701 * The version of Wiki RPC API supported 702 */ 703 function wiki_RPCVersion(){ 704 return 2; 705 } 706} 707 708$server = new dokuwiki_xmlrpc_server(); 709 710// vim:ts=4:sw=4:et:enc=utf-8: 711