1*f657e5d0SAndreas Gohr<?php 2*f657e5d0SAndreas Gohr 3*f657e5d0SAndreas Gohrnamespace dokuwiki\Remote; 4*f657e5d0SAndreas Gohr 5*f657e5d0SAndreas Gohr/** 6*f657e5d0SAndreas Gohr * Provide the Remote XMLRPC API as a JSON based API 7*f657e5d0SAndreas Gohr */ 8*f657e5d0SAndreas Gohrclass JsonRpcServer 9*f657e5d0SAndreas Gohr{ 10*f657e5d0SAndreas Gohr 11*f657e5d0SAndreas Gohr protected $remote; 12*f657e5d0SAndreas Gohr 13*f657e5d0SAndreas Gohr /** 14*f657e5d0SAndreas Gohr * JsonRpcServer constructor. 15*f657e5d0SAndreas Gohr */ 16*f657e5d0SAndreas Gohr public function __construct() 17*f657e5d0SAndreas Gohr { 18*f657e5d0SAndreas Gohr $this->remote = new Api(); 19*f657e5d0SAndreas Gohr $this->remote->setFileTransformation(array($this, 'toFile')); 20*f657e5d0SAndreas Gohr } 21*f657e5d0SAndreas Gohr 22*f657e5d0SAndreas Gohr /** 23*f657e5d0SAndreas Gohr * Serve the request 24*f657e5d0SAndreas Gohr * 25*f657e5d0SAndreas Gohr * @return mixed 26*f657e5d0SAndreas Gohr * @throws RemoteException 27*f657e5d0SAndreas Gohr */ 28*f657e5d0SAndreas Gohr public function serve() 29*f657e5d0SAndreas Gohr { 30*f657e5d0SAndreas Gohr global $conf; 31*f657e5d0SAndreas Gohr if (!$conf['remote']) { 32*f657e5d0SAndreas Gohr http_status(404); 33*f657e5d0SAndreas Gohr throw new RemoteException("JSON-RPC server not enabled.", -32605); 34*f657e5d0SAndreas Gohr } 35*f657e5d0SAndreas Gohr if (!empty($conf['remotecors'])) { 36*f657e5d0SAndreas Gohr header('Access-Control-Allow-Origin: ' . $conf['remotecors']); 37*f657e5d0SAndreas Gohr } 38*f657e5d0SAndreas Gohr 39*f657e5d0SAndreas Gohr global $INPUT; 40*f657e5d0SAndreas Gohr 41*f657e5d0SAndreas Gohr $call = $INPUT->server->str('PATH_INFO'); 42*f657e5d0SAndreas Gohr $call = trim($call, '/'); 43*f657e5d0SAndreas Gohr $args = json_decode(file_get_contents('php://input'), true); 44*f657e5d0SAndreas Gohr if (!is_array($args)) $args = []; 45*f657e5d0SAndreas Gohr 46*f657e5d0SAndreas Gohr return $this->call($call, $args); 47*f657e5d0SAndreas Gohr } 48*f657e5d0SAndreas Gohr 49*f657e5d0SAndreas Gohr /** 50*f657e5d0SAndreas Gohr * Call an API method 51*f657e5d0SAndreas Gohr * 52*f657e5d0SAndreas Gohr * @param string $methodname 53*f657e5d0SAndreas Gohr * @param array $args 54*f657e5d0SAndreas Gohr * @return mixed 55*f657e5d0SAndreas Gohr * @throws RemoteException 56*f657e5d0SAndreas Gohr */ 57*f657e5d0SAndreas Gohr public function call($methodname, $args) 58*f657e5d0SAndreas Gohr { 59*f657e5d0SAndreas Gohr try { 60*f657e5d0SAndreas Gohr $result = $this->remote->call($methodname, $args); 61*f657e5d0SAndreas Gohr return $result; 62*f657e5d0SAndreas Gohr } catch (AccessDeniedException $e) { 63*f657e5d0SAndreas Gohr if (!isset($_SERVER['REMOTE_USER'])) { 64*f657e5d0SAndreas Gohr http_status(401); 65*f657e5d0SAndreas Gohr throw new RemoteException("server error. not authorized to call method $methodname", -32603); 66*f657e5d0SAndreas Gohr } else { 67*f657e5d0SAndreas Gohr http_status(403); 68*f657e5d0SAndreas Gohr throw new RemoteException("server error. forbidden to call the method $methodname", -32604); 69*f657e5d0SAndreas Gohr } 70*f657e5d0SAndreas Gohr } catch (RemoteException $e) { 71*f657e5d0SAndreas Gohr http_status(400); 72*f657e5d0SAndreas Gohr throw $e; 73*f657e5d0SAndreas Gohr } 74*f657e5d0SAndreas Gohr } 75*f657e5d0SAndreas Gohr 76*f657e5d0SAndreas Gohr /** 77*f657e5d0SAndreas Gohr * @param string $data 78*f657e5d0SAndreas Gohr * @return string 79*f657e5d0SAndreas Gohr */ 80*f657e5d0SAndreas Gohr public function toFile($data) 81*f657e5d0SAndreas Gohr { 82*f657e5d0SAndreas Gohr return base64_encode($data); 83*f657e5d0SAndreas Gohr } 84*f657e5d0SAndreas Gohr 85*f657e5d0SAndreas Gohr} 86