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