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