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