XmlRpcServer.php 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. <?php
  2. namespace dokuwiki\Remote;
  3. use IXR\DataType\Base64;
  4. use IXR\DataType\Date;
  5. use IXR\Exception\ServerException;
  6. use IXR\Server\Server;
  7. /**
  8. * Contains needed wrapper functions and registers all available XMLRPC functions.
  9. */
  10. class XmlRpcServer extends Server
  11. {
  12. protected $remote;
  13. /**
  14. * Constructor. Register methods and run Server
  15. */
  16. public function __construct($wait=false)
  17. {
  18. $this->remote = new Api();
  19. $this->remote->setDateTransformation(array($this, 'toDate'));
  20. $this->remote->setFileTransformation(array($this, 'toFile'));
  21. parent::__construct(false, false, $wait);
  22. }
  23. /** @inheritdoc */
  24. public function serve($data = false)
  25. {
  26. global $conf;
  27. if (!$conf['remote']) {
  28. throw new ServerException("XML-RPC server not enabled.", -32605);
  29. }
  30. if (!empty($conf['remotecors'])) {
  31. header('Access-Control-Allow-Origin: ' . $conf['remotecors']);
  32. }
  33. parent::serve($data);
  34. }
  35. /**
  36. * @inheritdoc
  37. */
  38. public function call($methodname, $args)
  39. {
  40. try {
  41. $result = $this->remote->call($methodname, $args);
  42. return $result;
  43. } catch (AccessDeniedException $e) {
  44. if (!isset($_SERVER['REMOTE_USER'])) {
  45. http_status(401);
  46. return new ServerException("server error. not authorized to call method $methodname", -32603);
  47. } else {
  48. http_status(403);
  49. return new ServerException("server error. forbidden to call the method $methodname", -32604);
  50. }
  51. } catch (RemoteException $e) {
  52. return new ServerException($e->getMessage(), $e->getCode());
  53. }
  54. }
  55. /**
  56. * @param string|int $data iso date(yyyy[-]mm[-]dd[ hh:mm[:ss]]) or timestamp
  57. * @return Date
  58. */
  59. public function toDate($data)
  60. {
  61. return new Date($data);
  62. }
  63. /**
  64. * @param string $data
  65. * @return Base64
  66. */
  67. public function toFile($data)
  68. {
  69. return new Base64($data);
  70. }
  71. }