3 namespace dokuwiki\Remote
;
6 * Provide the Remote XMLRPC API as a JSON based API
13 * JsonRpcServer constructor.
15 public function __construct()
17 $this->remote
= new Api();
18 $this->remote
->setFileTransformation([$this, 'toFile']);
25 * @throws RemoteException
27 public function serve()
32 if (!$conf['remote']) {
34 throw new RemoteException("JSON-RPC server not enabled.", -32605);
36 if (!empty($conf['remotecors'])) {
37 header('Access-Control-Allow-Origin: ' . $conf['remotecors']);
39 if ($INPUT->server
->str('REQUEST_METHOD') !== 'POST') {
41 header('Allow: POST');
42 throw new RemoteException("JSON-RPC server only accepts POST requests.", -32606);
44 if ($INPUT->server
->str('CONTENT_TYPE') !== 'application/json') {
46 throw new RemoteException("JSON-RPC server only accepts application/json requests.", -32606);
49 $call = $INPUT->server
->str('PATH_INFO');
50 $call = trim($call, '/');
52 $args = json_decode(file_get_contents('php://input'), true, 512, JSON_THROW_ON_ERROR
);
53 } catch (\Exception
$e) {
56 if (!is_array($args)) $args = [];
58 return $this->call($call, $args);
64 * @param string $methodname
67 * @throws RemoteException
69 public function call($methodname, $args)
72 $result = $this->remote
->call($methodname, $args);
74 } catch (AccessDeniedException
$e) {
75 if (!isset($_SERVER['REMOTE_USER'])) {
77 throw new RemoteException("server error. not authorized to call method $methodname", -32603);
80 throw new RemoteException("server error. forbidden to call the method $methodname", -32604);
82 } catch (RemoteException
$e) {
92 public function toFile($data)
94 return base64_encode($data);