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