diff options
Diffstat (limited to 'vendor/fguillot/json-rpc/src/JsonRPC')
24 files changed, 2539 insertions, 0 deletions
diff --git a/vendor/fguillot/json-rpc/src/JsonRPC/Client.php b/vendor/fguillot/json-rpc/src/JsonRPC/Client.php new file mode 100644 index 00000000..fed1ce30 --- /dev/null +++ b/vendor/fguillot/json-rpc/src/JsonRPC/Client.php @@ -0,0 +1,194 @@ +<?php + +namespace JsonRPC; + +use Exception; +use JsonRPC\Request\RequestBuilder; +use JsonRPC\Response\ResponseParser; + +/** + * JsonRPC client class + * + * @package JsonRPC + * @author Frederic Guillot + */ +class Client +{ + /** + * If the only argument passed to a function is an array + * assume it contains named arguments + * + * @access private + * @var boolean + */ + private $isNamedArguments = true; + + /** + * Do not immediately throw an exception on error. Return it instead. + * + * @access public + * @var boolean + */ + private $returnException = false; + + /** + * True for a batch request + * + * @access private + * @var boolean + */ + private $isBatch = false; + + /** + * Batch payload + * + * @access private + * @var array + */ + private $batch = array(); + + /** + * Http Client + * + * @access private + * @var HttpClient + */ + private $httpClient; + + /** + * Constructor + * + * @access public + * @param string $url Server URL + * @param bool $returnException Return exceptions + * @param HttpClient $httpClient HTTP client object + */ + public function __construct($url = '', $returnException = false, HttpClient $httpClient = null) + { + $this->httpClient = $httpClient ?: new HttpClient($url); + $this->returnException = $returnException; + } + + /** + * Arguments passed are always positional + * + * @access public + * @return $this + */ + public function withPositionalArguments() + { + $this->isNamedArguments = false; + return $this; + } + + /** + * Get HTTP Client + * + * @access public + * @return HttpClient + */ + public function getHttpClient() + { + return $this->httpClient; + } + + /** + * Set username and password + * + * @access public + * @param string $username + * @param string $password + * @return $this + */ + public function authentication($username, $password) + { + $this->httpClient + ->withUsername($username) + ->withPassword($password); + + return $this; + } + + /** + * Automatic mapping of procedures + * + * @access public + * @param string $method Procedure name + * @param array $params Procedure arguments + * @return mixed + */ + public function __call($method, array $params) + { + if ($this->isNamedArguments && count($params) === 1 && is_array($params[0])) { + $params = $params[0]; + } + + return $this->execute($method, $params); + } + + /** + * Start a batch request + * + * @access public + * @return Client + */ + public function batch() + { + $this->isBatch = true; + $this->batch = array(); + return $this; + } + + /** + * Send a batch request + * + * @access public + * @return array + */ + public function send() + { + $this->isBatch = false; + return $this->sendPayload('['.implode(', ', $this->batch).']'); + } + + /** + * Execute a procedure + * + * @access public + * @param string $procedure Procedure name + * @param array $params Procedure arguments + * @param array $reqattrs + * @return mixed + */ + public function execute($procedure, array $params = array(), array $reqattrs = array()) + { + $payload = RequestBuilder::create() + ->withProcedure($procedure) + ->withParams($params) + ->withRequestAttributes($reqattrs) + ->build(); + + if ($this->isBatch) { + $this->batch[] = $payload; + return $this; + } + + return $this->sendPayload($payload); + } + + /** + * Send payload + * + * @access private + * @throws Exception + * @param string $payload + * @return Exception|Client + */ + private function sendPayload($payload) + { + return ResponseParser::create() + ->withReturnException($this->returnException) + ->withPayload($this->httpClient->execute($payload)) + ->parse(); + } +} diff --git a/vendor/fguillot/json-rpc/src/JsonRPC/Exception/AccessDeniedException.php b/vendor/fguillot/json-rpc/src/JsonRPC/Exception/AccessDeniedException.php new file mode 100644 index 00000000..8cb9bc2b --- /dev/null +++ b/vendor/fguillot/json-rpc/src/JsonRPC/Exception/AccessDeniedException.php @@ -0,0 +1,15 @@ +<?php + +namespace JsonRPC\Exception; + +use Exception; + +/** + * Class AccessDeniedException + * + * @package JsonRPC\Exception + * @author Frederic Guillot + */ +class AccessDeniedException extends Exception +{ +} diff --git a/vendor/fguillot/json-rpc/src/JsonRPC/Exception/AuthenticationFailureException.php b/vendor/fguillot/json-rpc/src/JsonRPC/Exception/AuthenticationFailureException.php new file mode 100644 index 00000000..6237256a --- /dev/null +++ b/vendor/fguillot/json-rpc/src/JsonRPC/Exception/AuthenticationFailureException.php @@ -0,0 +1,15 @@ +<?php + +namespace JsonRPC\Exception; + +use Exception; + +/** + * Class AuthenticationFailureException + * + * @package JsonRPC\Exception + * @author Frederic Guillot + */ +class AuthenticationFailureException extends Exception +{ +} diff --git a/vendor/fguillot/json-rpc/src/JsonRPC/Exception/ConnectionFailureException.php b/vendor/fguillot/json-rpc/src/JsonRPC/Exception/ConnectionFailureException.php new file mode 100644 index 00000000..6f4985bc --- /dev/null +++ b/vendor/fguillot/json-rpc/src/JsonRPC/Exception/ConnectionFailureException.php @@ -0,0 +1,15 @@ +<?php + +namespace JsonRPC\Exception; + +use Exception; + +/** + * Class ConnectionFailureException + * + * @package JsonRPC\Exception + * @author Frederic Guillot + */ +class ConnectionFailureException extends Exception +{ +} diff --git a/vendor/fguillot/json-rpc/src/JsonRPC/Exception/InvalidJsonFormatException.php b/vendor/fguillot/json-rpc/src/JsonRPC/Exception/InvalidJsonFormatException.php new file mode 100644 index 00000000..e5bb31cd --- /dev/null +++ b/vendor/fguillot/json-rpc/src/JsonRPC/Exception/InvalidJsonFormatException.php @@ -0,0 +1,15 @@ +<?php + +namespace JsonRPC\Exception; + +use Exception; + +/** + * Class InvalidJsonFormatException + * + * @package JsonRPC\Exception + * @author Frederic Guillot + */ +class InvalidJsonFormatException extends Exception +{ +} diff --git a/vendor/fguillot/json-rpc/src/JsonRPC/Exception/InvalidJsonRpcFormatException.php b/vendor/fguillot/json-rpc/src/JsonRPC/Exception/InvalidJsonRpcFormatException.php new file mode 100644 index 00000000..e2277379 --- /dev/null +++ b/vendor/fguillot/json-rpc/src/JsonRPC/Exception/InvalidJsonRpcFormatException.php @@ -0,0 +1,15 @@ +<?php + +namespace JsonRPC\Exception; + +use Exception; + +/** + * Class InvalidJsonRpcFormatException + * + * @package JsonRPC\Exception + * @author Frederic Guillot + */ +class InvalidJsonRpcFormatException extends Exception +{ +} diff --git a/vendor/fguillot/json-rpc/src/JsonRPC/Exception/ResponseEncodingFailureException.php b/vendor/fguillot/json-rpc/src/JsonRPC/Exception/ResponseEncodingFailureException.php new file mode 100644 index 00000000..0a40d806 --- /dev/null +++ b/vendor/fguillot/json-rpc/src/JsonRPC/Exception/ResponseEncodingFailureException.php @@ -0,0 +1,15 @@ +<?php + +namespace JsonRPC\Exception; + +use Exception; + +/** + * Class ResponseEncodingFailureException + * + * @package JsonRPC\Exception + * @author Frederic Guillot + */ +class ResponseEncodingFailureException extends Exception +{ +} diff --git a/vendor/fguillot/json-rpc/src/JsonRPC/Exception/ResponseException.php b/vendor/fguillot/json-rpc/src/JsonRPC/Exception/ResponseException.php new file mode 100644 index 00000000..98fb5c67 --- /dev/null +++ b/vendor/fguillot/json-rpc/src/JsonRPC/Exception/ResponseException.php @@ -0,0 +1,62 @@ +<?php + +namespace JsonRPC\Exception; + +use Exception; + +/** + * Class ResponseException + * + * @package JsonRPC\Exception + * @author Frederic Guillot + */ +class ResponseException extends Exception +{ + /** + * A value that contains additional information about the error. + * + * @access protected + * @link http://www.jsonrpc.org/specification#error_object + * @var mixed + */ + protected $data; + + /** + * Constructor + * + * @access public + * @param string $message [optional] The Exception message to throw. + * @param int $code [optional] The Exception code. + * @param Exception $previous [optional] The previous exception used for the exception chaining. Since 5.3.0 + * @param mixed $data [optional] A value that contains additional information about the error. + */ + public function __construct($message = '', $code = 0, Exception $previous = null, $data = null) + { + parent::__construct($message, $code, $previous); + $this->setData($data); + } + + /** + * Attach additional information + * + * @access public + * @param mixed $data [optional] A value that contains additional information about the error. + * @return \JsonRPC\Exception\ResponseException + */ + public function setData($data = null) + { + $this->data = $data; + return $this; + } + + /** + * Get additional information + * + * @access public + * @return mixed|null + */ + public function getData() + { + return $this->data; + } +} diff --git a/vendor/fguillot/json-rpc/src/JsonRPC/Exception/ServerErrorException.php b/vendor/fguillot/json-rpc/src/JsonRPC/Exception/ServerErrorException.php new file mode 100644 index 00000000..ab3ea584 --- /dev/null +++ b/vendor/fguillot/json-rpc/src/JsonRPC/Exception/ServerErrorException.php @@ -0,0 +1,15 @@ +<?php + +namespace JsonRPC\Exception; + +use Exception; + +/** + * Class ServerErrorException + * + * @package JsonRPC\Exception + * @author Frederic Guillot + */ +class ServerErrorException extends Exception +{ +} diff --git a/vendor/fguillot/json-rpc/src/JsonRPC/HttpClient.php b/vendor/fguillot/json-rpc/src/JsonRPC/HttpClient.php new file mode 100644 index 00000000..90ce705a --- /dev/null +++ b/vendor/fguillot/json-rpc/src/JsonRPC/HttpClient.php @@ -0,0 +1,365 @@ +<?php + +namespace JsonRPC; + +use Closure; +use JsonRPC\Exception\AccessDeniedException; +use JsonRPC\Exception\ConnectionFailureException; +use JsonRPC\Exception\ServerErrorException; + +/** + * Class HttpClient + * + * @package JsonRPC + * @author Frederic Guillot + */ +class HttpClient +{ + /** + * URL of the server + * + * @access private + * @var string + */ + private $url; + + /** + * HTTP client timeout + * + * @access private + * @var integer + */ + private $timeout = 5; + + /** + * Default HTTP headers to send to the server + * + * @access private + * @var array + */ + private $headers = array( + 'User-Agent: JSON-RPC PHP Client <https://github.com/fguillot/JsonRPC>', + 'Content-Type: application/json', + 'Accept: application/json', + 'Connection: close', + ); + + /** + * Username for authentication + * + * @access private + * @var string + */ + private $username; + + /** + * Password for authentication + * + * @access private + * @var string + */ + private $password; + + /** + * Enable debug output to the php error log + * + * @access private + * @var boolean + */ + private $debug = false; + + /** + * Cookies + * + * @access private + * @var array + */ + private $cookies = array(); + + /** + * SSL certificates verification + * + * @access private + * @var boolean + */ + private $verifySslCertificate = true; + + /** + * Callback called before the doing the request + * + * @access private + * @var Closure + */ + private $beforeRequest; + + /** + * HttpClient constructor + * + * @access public + * @param string $url + */ + public function __construct($url = '') + { + $this->url = $url; + } + + /** + * Set URL + * + * @access public + * @param string $url + * @return $this + */ + public function withUrl($url) + { + $this->url = $url; + return $this; + } + + /** + * Set username + * + * @access public + * @param string $username + * @return $this + */ + public function withUsername($username) + { + $this->username = $username; + return $this; + } + + /** + * Set password + * + * @access public + * @param string $password + * @return $this + */ + public function withPassword($password) + { + $this->password = $password; + return $this; + } + + /** + * Set timeout + * + * @access public + * @param integer $timeout + * @return $this + */ + public function withTimeout($timeout) + { + $this->timeout = $timeout; + return $this; + } + + /** + * Set timeout + * + * @access public + * @param array $headers + * @return $this + */ + public function withHeaders(array $headers) + { + $this->headers = array_merge($this->headers, $headers); + return $this; + } + + /** + * Set cookies + * + * @access public + * @param array $cookies + * @param boolean $replace + */ + public function withCookies(array $cookies, $replace = false) + { + if ($replace) { + $this->cookies = $cookies; + } else { + $this->cookies = array_merge($this->cookies, $cookies); + } + } + + /** + * Enable debug mode + * + * @access public + * @return $this + */ + public function withDebug() + { + $this->debug = true; + return $this; + } + + /** + * Disable SSL verification + * + * @access public + * @return $this + */ + public function withoutSslVerification() + { + $this->verifySslCertificate = false; + return $this; + } + + /** + * Assign a callback before the request + * + * @access public + * @param Closure $closure + * @return $this + */ + public function withBeforeRequestCallback(Closure $closure) + { + $this->beforeRequest = $closure; + return $this; + } + + /** + * Get cookies + * + * @access public + * @return array + */ + public function getCookies() + { + return $this->cookies; + } + + /** + * Do the HTTP request + * + * @access public + * @throws ConnectionFailureException + * @param string $payload + * @return array + */ + public function execute($payload) + { + if (is_callable($this->beforeRequest)) { + call_user_func_array($this->beforeRequest, array($this, $payload)); + } + + $stream = fopen(trim($this->url), 'r', false, $this->buildContext($payload)); + + if (! is_resource($stream)) { + throw new ConnectionFailureException('Unable to establish a connection'); + } + + $metadata = stream_get_meta_data($stream); + $headers = $metadata['wrapper_data']; + $response = json_decode(stream_get_contents($stream), true); + + if ($this->debug) { + error_log('==> Request: '.PHP_EOL.(is_string($payload) ? $payload : json_encode($payload, JSON_PRETTY_PRINT))); + error_log('==> Headers: '.PHP_EOL.var_export($headers, true)); + error_log('==> Response: '.PHP_EOL.json_encode($response, JSON_PRETTY_PRINT)); + } + + $this->handleExceptions($headers); + $this->parseCookies($headers); + + return $response; + } + + /** + * Prepare stream context + * + * @access private + * @param string $payload + * @return resource + */ + private function buildContext($payload) + { + $headers = $this->headers; + + if (! empty($this->username) && ! empty($this->password)) { + $headers[] = 'Authorization: Basic '.base64_encode($this->username.':'.$this->password); + } + + if (! empty($this->cookies)){ + $cookies = array(); + + foreach ($this->cookies as $key => $value) { + $cookies[] = $key.'='.$value; + } + + $headers[] = 'Cookie: '.implode('; ', $cookies); + } + + return stream_context_create(array( + 'http' => array( + 'method' => 'POST', + 'protocol_version' => 1.1, + 'timeout' => $this->timeout, + 'max_redirects' => 2, + 'header' => implode("\r\n", $headers), + 'content' => $payload, + 'ignore_errors' => true, + ), + 'ssl' => array( + 'verify_peer' => $this->verifySslCertificate, + 'verify_peer_name' => $this->verifySslCertificate, + ) + )); + } + + /** + * Parse cookies from response + * + * @access private + * @param array $headers + */ + private function parseCookies(array $headers) + { + foreach ($headers as $header) { + $pos = stripos($header, 'Set-Cookie:'); + + if ($pos !== false) { + $cookies = explode(';', substr($header, $pos + 11)); + + foreach ($cookies as $cookie) { + $item = explode('=', $cookie); + + if (count($item) === 2) { + $name = trim($item[0]); + $value = $item[1]; + $this->cookies[$name] = $value; + } + } + } + } + } + + /** + * Throw an exception according the HTTP response + * + * @access public + * @param array $headers + * @throws AccessDeniedException + * @throws ServerErrorException + */ + public function handleExceptions(array $headers) + { + $exceptions = array( + '401' => '\JsonRPC\Exception\AccessDeniedException', + '403' => '\JsonRPC\Exception\AccessDeniedException', + '404' => '\JsonRPC\Exception\ConnectionFailureException', + '500' => '\JsonRPC\Exception\ServerErrorException', + ); + + foreach ($headers as $header) { + foreach ($exceptions as $code => $exception) { + if (strpos($header, 'HTTP/1.0 '.$code) !== false || strpos($header, 'HTTP/1.1 '.$code) !== false) { + throw new $exception('Response: '.$header); + } + } + } + } +} diff --git a/vendor/fguillot/json-rpc/src/JsonRPC/MiddlewareHandler.php b/vendor/fguillot/json-rpc/src/JsonRPC/MiddlewareHandler.php new file mode 100644 index 00000000..61d5a2d2 --- /dev/null +++ b/vendor/fguillot/json-rpc/src/JsonRPC/MiddlewareHandler.php @@ -0,0 +1,114 @@ +<?php + +namespace JsonRPC; + +/** + * Class MiddlewareHandler + * + * @package JsonRPC + * @author Frederic Guillot + */ +class MiddlewareHandler +{ + /** + * Procedure Name + * + * @access protected + * @var string + */ + protected $procedureName = ''; + + /** + * Username + * + * @access protected + * @var string + */ + protected $username = ''; + + /** + * Password + * + * @access protected + * @var string + */ + protected $password = ''; + + /** + * List of middleware to execute before to call the method + * + * @access protected + * @var MiddlewareInterface[] + */ + protected $middleware = array(); + + /** + * Set username + * + * @access public + * @param string $username + * @return $this + */ + public function withUsername($username) + { + if (! empty($username)) { + $this->username = $username; + } + + return $this; + } + + /** + * Set password + * + * @access public + * @param string $password + * @return $this + */ + public function withPassword($password) + { + if (! empty($password)) { + $this->password = $password; + } + + return $this; + } + + /** + * Set procedure name + * + * @access public + * @param string $procedureName + * @return $this + */ + public function withProcedure($procedureName) + { + $this->procedureName = $procedureName; + return $this; + } + + /** + * Add a new middleware + * + * @access public + * @param MiddlewareInterface $middleware + * @return MiddlewareHandler + */ + public function withMiddleware(MiddlewareInterface $middleware) + { + $this->middleware[] = $middleware; + return $this; + } + + /** + * Execute all middleware + * + * @access public + */ + public function execute() + { + foreach ($this->middleware as $middleware) { + $middleware->execute($this->username, $this->password, $this->procedureName); + } + } +} diff --git a/vendor/fguillot/json-rpc/src/JsonRPC/MiddlewareInterface.php b/vendor/fguillot/json-rpc/src/JsonRPC/MiddlewareInterface.php new file mode 100644 index 00000000..ab55261d --- /dev/null +++ b/vendor/fguillot/json-rpc/src/JsonRPC/MiddlewareInterface.php @@ -0,0 +1,27 @@ +<?php + +namespace JsonRPC; + +use JsonRPC\Exception\AccessDeniedException; +use JsonRPC\Exception\AuthenticationFailureException; + +/** + * Interface MiddlewareInterface + * + * @package JsonRPC + * @author Frederic Guillot + */ +interface MiddlewareInterface +{ + /** + * Execute Middleware + * + * @access public + * @param string $username + * @param string $password + * @param string $procedureName + * @throws AccessDeniedException + * @throws AuthenticationFailureException + */ + public function execute($username, $password, $procedureName); +} diff --git a/vendor/fguillot/json-rpc/src/JsonRPC/ProcedureHandler.php b/vendor/fguillot/json-rpc/src/JsonRPC/ProcedureHandler.php new file mode 100644 index 00000000..1e4fd518 --- /dev/null +++ b/vendor/fguillot/json-rpc/src/JsonRPC/ProcedureHandler.php @@ -0,0 +1,264 @@ +<?php + +namespace JsonRPC; + +use BadFunctionCallException; +use Closure; +use InvalidArgumentException; +use ReflectionFunction; +use ReflectionMethod; + +/** + * Class ProcedureHandler + * + * @package JsonRPC + * @author Frederic Guillot + */ +class ProcedureHandler +{ + /** + * List of procedures + * + * @access protected + * @var array + */ + protected $callbacks = array(); + + /** + * List of classes + * + * @access protected + * @var array + */ + protected $classes = array(); + + /** + * List of instances + * + * @access protected + * @var array + */ + protected $instances = array(); + + /** + * Before method name to call + * + * @access protected + * @var string + */ + protected $beforeMethodName = ''; + + /** + * Register a new procedure + * + * @access public + * @param string $procedure Procedure name + * @param closure $callback Callback + * @return $this + */ + public function withCallback($procedure, Closure $callback) + { + $this->callbacks[$procedure] = $callback; + return $this; + } + + /** + * Bind a procedure to a class + * + * @access public + * @param string $procedure Procedure name + * @param mixed $class Class name or instance + * @param string $method Procedure name + * @return $this + */ + public function withClassAndMethod($procedure, $class, $method = '') + { + if ($method === '') { + $method = $procedure; + } + + $this->classes[$procedure] = array($class, $method); + return $this; + } + + /** + * Bind a class instance + * + * @access public + * @param mixed $instance + * @return $this + */ + public function withObject($instance) + { + $this->instances[] = $instance; + return $this; + } + + /** + * Set a before method to call + * + * @access public + * @param string $methodName + * @return $this + */ + public function withBeforeMethod($methodName) + { + $this->beforeMethodName = $methodName; + return $this; + } + + /** + * Execute the procedure + * + * @access public + * @param string $procedure Procedure name + * @param array $params Procedure params + * @return mixed + */ + public function executeProcedure($procedure, array $params = array()) + { + if (isset($this->callbacks[$procedure])) { + return $this->executeCallback($this->callbacks[$procedure], $params); + } elseif (isset($this->classes[$procedure]) && method_exists($this->classes[$procedure][0], $this->classes[$procedure][1])) { + return $this->executeMethod($this->classes[$procedure][0], $this->classes[$procedure][1], $params); + } + + foreach ($this->instances as $instance) { + if (method_exists($instance, $procedure)) { + return $this->executeMethod($instance, $procedure, $params); + } + } + + throw new BadFunctionCallException('Unable to find the procedure'); + } + + /** + * Execute a callback + * + * @access public + * @param Closure $callback Callback + * @param array $params Procedure params + * @return mixed + */ + public function executeCallback(Closure $callback, $params) + { + $reflection = new ReflectionFunction($callback); + + $arguments = $this->getArguments( + $params, + $reflection->getParameters(), + $reflection->getNumberOfRequiredParameters(), + $reflection->getNumberOfParameters() + ); + + return $reflection->invokeArgs($arguments); + } + + /** + * Execute a method + * + * @access public + * @param mixed $class Class name or instance + * @param string $method Method name + * @param array $params Procedure params + * @return mixed + */ + public function executeMethod($class, $method, $params) + { + $instance = is_string($class) ? new $class : $class; + $reflection = new ReflectionMethod($class, $method); + + $this->executeBeforeMethod($instance, $method); + + $arguments = $this->getArguments( + $params, + $reflection->getParameters(), + $reflection->getNumberOfRequiredParameters(), + $reflection->getNumberOfParameters() + ); + + return $reflection->invokeArgs($instance, $arguments); + } + + /** + * Execute before method if defined + * + * @access public + * @param mixed $object + * @param string $method + */ + public function executeBeforeMethod($object, $method) + { + if ($this->beforeMethodName !== '' && method_exists($object, $this->beforeMethodName)) { + call_user_func_array(array($object, $this->beforeMethodName), array($method)); + } + } + + /** + * Get procedure arguments + * + * @access public + * @param array $requestParams Incoming arguments + * @param array $methodParams Procedure arguments + * @param integer $nbRequiredParams Number of required parameters + * @param integer $nbMaxParams Maximum number of parameters + * @return array + */ + public function getArguments(array $requestParams, array $methodParams, $nbRequiredParams, $nbMaxParams) + { + $nbParams = count($requestParams); + + if ($nbParams < $nbRequiredParams) { + throw new InvalidArgumentException('Wrong number of arguments'); + } + + if ($nbParams > $nbMaxParams) { + throw new InvalidArgumentException('Too many arguments'); + } + + if ($this->isPositionalArguments($requestParams)) { + return $requestParams; + } + + return $this->getNamedArguments($requestParams, $methodParams); + } + + /** + * Return true if we have positional parameters + * + * @access public + * @param array $request_params Incoming arguments + * @return bool + */ + public function isPositionalArguments(array $request_params) + { + return array_keys($request_params) === range(0, count($request_params) - 1); + } + + /** + * Get named arguments + * + * @access public + * @param array $requestParams Incoming arguments + * @param array $methodParams Procedure arguments + * @return array + */ + public function getNamedArguments(array $requestParams, array $methodParams) + { + $params = array(); + + foreach ($methodParams as $p) { + $name = $p->getName(); + + if (isset($requestParams[$name])) { + $params[$name] = $requestParams[$name]; + } elseif ($p->isDefaultValueAvailable()) { + $params[$name] = $p->getDefaultValue(); + } else { + throw new InvalidArgumentException('Missing argument: '.$name); + } + } + + return $params; + } +} diff --git a/vendor/fguillot/json-rpc/src/JsonRPC/Request/BatchRequestParser.php b/vendor/fguillot/json-rpc/src/JsonRPC/Request/BatchRequestParser.php new file mode 100644 index 00000000..c0fc776e --- /dev/null +++ b/vendor/fguillot/json-rpc/src/JsonRPC/Request/BatchRequestParser.php @@ -0,0 +1,55 @@ +<?php + +namespace JsonRPC\Request; + +/** + * Class BatchRequestParser + * + * @package JsonRPC\Request + * @author Frederic Guillot + */ +class BatchRequestParser extends RequestParser +{ + /** + * Parse incoming request + * + * @access public + * @return string + */ + public function parse() + { + $responses = array(); + + foreach ($this->payload as $payload) { + $responses[] = RequestParser::create() + ->withPayload($payload) + ->withProcedureHandler($this->procedureHandler) + ->withMiddlewareHandler($this->middlewareHandler) + ->withLocalException($this->localExceptions) + ->parse(); + } + + $responses = array_filter($responses); + return empty($responses) ? '' : '['.implode(',', $responses).']'; + } + + /** + * Return true if we have a batch request + * + * ex : [ + * 0 => '...', + * 1 => '...', + * 2 => '...', + * 3 => '...', + * ] + * + * @static + * @access public + * @param array $payload + * @return bool + */ + public static function isBatchRequest(array $payload) + { + return array_keys($payload) === range(0, count($payload) - 1); + } +} diff --git a/vendor/fguillot/json-rpc/src/JsonRPC/Request/RequestBuilder.php b/vendor/fguillot/json-rpc/src/JsonRPC/Request/RequestBuilder.php new file mode 100644 index 00000000..145d21c1 --- /dev/null +++ b/vendor/fguillot/json-rpc/src/JsonRPC/Request/RequestBuilder.php @@ -0,0 +1,129 @@ +<?php + +namespace JsonRPC\Request; + +/** + * Class RequestBuilder + * + * @package JsonRPC\Request + * @author Frederic Guillot + */ +class RequestBuilder +{ + /** + * Request ID + * + * @access private + * @var mixed + */ + private $id = null; + + /** + * Method name + * + * @access private + * @var string + */ + private $procedure = ''; + + /** + * Method arguments + * + * @access private + * @var array + */ + private $params = array(); + + /** + * Additional request attributes + * + * @access private + * @var array + */ + private $reqattrs = array(); + + /** + * Get new object instance + * + * @static + * @access public + * @return RequestBuilder + */ + public static function create() + { + return new static(); + } + + /** + * Set id + * + * @access public + * @param null $id + * @return RequestBuilder + */ + public function withId($id) + { + $this->id = $id; + return $this; + } + + /** + * Set method + * + * @access public + * @param string $procedure + * @return RequestBuilder + */ + public function withProcedure($procedure) + { + $this->procedure = $procedure; + return $this; + } + + /** + * Set parameters + * + * @access public + * @param array $params + * @return RequestBuilder + */ + public function withParams(array $params) + { + $this->params = $params; + return $this; + } + + /** + * Set additional request attributes + * + * @access public + * @param array $reqattrs + * @return RequestBuilder + */ + public function withRequestAttributes(array $reqattrs) + { + $this->reqattrs = $reqattrs; + return $this; + } + + /** + * Build the payload + * + * @access public + * @return string + */ + public function build() + { + $payload = array_merge_recursive($this->reqattrs, array( + 'jsonrpc' => '2.0', + 'method' => $this->procedure, + 'id' => $this->id ?: mt_rand(), + )); + + if (! empty($this->params)) { + $payload['params'] = $this->params; + } + + return json_encode($payload); + } +} diff --git a/vendor/fguillot/json-rpc/src/JsonRPC/Request/RequestParser.php b/vendor/fguillot/json-rpc/src/JsonRPC/Request/RequestParser.php new file mode 100644 index 00000000..ea1b7d43 --- /dev/null +++ b/vendor/fguillot/json-rpc/src/JsonRPC/Request/RequestParser.php @@ -0,0 +1,200 @@ +<?php + +namespace JsonRPC\Request; + +use Exception; +use JsonRPC\Exception\AccessDeniedException; +use JsonRPC\Exception\AuthenticationFailureException; +use JsonRPC\Exception\InvalidJsonRpcFormatException; +use JsonRPC\MiddlewareHandler; +use JsonRPC\ProcedureHandler; +use JsonRPC\Response\ResponseBuilder; +use JsonRPC\Validator\JsonFormatValidator; +use JsonRPC\Validator\RpcFormatValidator; + +/** + * Class RequestParser + * + * @package JsonRPC + * @author Frederic Guillot + */ +class RequestParser +{ + /** + * Request payload + * + * @access protected + * @var mixed + */ + protected $payload; + + /** + * List of exceptions that should not be relayed to the client + * + * @access protected + * @var array() + */ + protected $localExceptions = array( + 'JsonRPC\Exception\AuthenticationFailureException', + 'JsonRPC\Exception\AccessDeniedException', + ); + + /** + * ProcedureHandler + * + * @access protected + * @var ProcedureHandler + */ + protected $procedureHandler; + + /** + * MiddlewareHandler + * + * @access protected + * @var MiddlewareHandler + */ + protected $middlewareHandler; + + /** + * Get new object instance + * + * @static + * @access public + * @return RequestParser + */ + public static function create() + { + return new static(); + } + + /** + * Set payload + * + * @access public + * @param mixed $payload + * @return $this + */ + public function withPayload($payload) + { + $this->payload = $payload; + return $this; + } + + /** + * Exception classes that should not be relayed to the client + * + * @access public + * @param mixed $exception + * @return $this + */ + public function withLocalException($exception) + { + if (is_array($exception)) { + $this->localExceptions = array_merge($this->localExceptions, $exception); + } else { + $this->localExceptions[] = $exception; + } + + return $this; + } + + /** + * Set procedure handler + * + * @access public + * @param ProcedureHandler $procedureHandler + * @return $this + */ + public function withProcedureHandler(ProcedureHandler $procedureHandler) + { + $this->procedureHandler = $procedureHandler; + return $this; + } + + /** + * Set middleware handler + * + * @access public + * @param MiddlewareHandler $middlewareHandler + * @return $this + */ + public function withMiddlewareHandler(MiddlewareHandler $middlewareHandler) + { + $this->middlewareHandler = $middlewareHandler; + return $this; + } + + /** + * Parse incoming request + * + * @access public + * @return string + * @throws AccessDeniedException + * @throws AuthenticationFailureException + */ + public function parse() + { + try { + + JsonFormatValidator::validate($this->payload); + RpcFormatValidator::validate($this->payload); + + $this->middlewareHandler + ->withProcedure($this->payload['method']) + ->execute(); + + $result = $this->procedureHandler->executeProcedure( + $this->payload['method'], + empty($this->payload['params']) ? array() : $this->payload['params'] + ); + + if (! $this->isNotification()) { + return ResponseBuilder::create() + ->withId($this->payload['id']) + ->withResult($result) + ->build(); + } + } catch (Exception $e) { + return $this->handleExceptions($e); + } + + return ''; + } + + /** + * Handle exceptions + * + * @access protected + * @param Exception $e + * @return string + * @throws Exception + */ + protected function handleExceptions(Exception $e) + { + foreach ($this->localExceptions as $exception) { + if ($e instanceof $exception) { + throw $e; + } + } + + if ($e instanceof InvalidJsonRpcFormatException || ! $this->isNotification()) { + return ResponseBuilder::create() + ->withId(isset($this->payload['id']) ? $this->payload['id'] : null) + ->withException($e) + ->build(); + } + + return ''; + } + + /** + * Return true if the message is a notification + * + * @access protected + * @return bool + */ + protected function isNotification() + { + return is_array($this->payload) && !isset($this->payload['id']); + } +} diff --git a/vendor/fguillot/json-rpc/src/JsonRPC/Response/ResponseBuilder.php b/vendor/fguillot/json-rpc/src/JsonRPC/Response/ResponseBuilder.php new file mode 100644 index 00000000..c1caff92 --- /dev/null +++ b/vendor/fguillot/json-rpc/src/JsonRPC/Response/ResponseBuilder.php @@ -0,0 +1,324 @@ +<?php + +namespace JsonRPC\Response; + +use BadFunctionCallException; +use Exception; +use InvalidArgumentException; +use JsonRPC\Exception\AccessDeniedException; +use JsonRPC\Exception\AuthenticationFailureException; +use JsonRPC\Exception\InvalidJsonFormatException; +use JsonRPC\Exception\InvalidJsonRpcFormatException; +use JsonRPC\Exception\ResponseEncodingFailureException; +use JsonRPC\Exception\ResponseException; +use JsonRPC\Validator\JsonEncodingValidator; + +/** + * Class ResponseBuilder + * + * @package JsonRPC + * @author Frederic Guillot + */ +class ResponseBuilder +{ + /** + * Payload ID + * + * @access private + * @var mixed + */ + private $id; + + /** + * Payload ID + * + * @access private + * @var mixed + */ + private $result; + + /** + * Payload error code + * + * @access private + * @var integer + */ + private $errorCode; + + /** + * Payload error message + * + * @access private + * @var string + */ + private $errorMessage; + + /** + * Payload error data + * + * @access private + * @var mixed + */ + private $errorData; + + /** + * HTTP Headers + * + * @access private + * @var array + */ + private $headers = array( + 'Content-Type' => 'application/json', + ); + + /** + * HTTP status + * + * @access private + * @var string + */ + private $status; + + /** + * Exception + * + * @access private + * @var ResponseException + */ + private $exception; + + /** + * Get new object instance + * + * @static + * @access public + * @return ResponseBuilder + */ + public static function create() + { + return new static(); + } + + /** + * Set id + * + * @access public + * @param mixed $id + * @return $this + */ + public function withId($id) + { + $this->id = $id; + return $this; + } + + /** + * Set result + * + * @access public + * @param mixed $result + * @return $this + */ + public function withResult($result) + { + $this->result = $result; + return $this; + } + + /** + * Set error + * + * @access public + * @param integer $code + * @param string $message + * @param string $data + * @return $this + */ + public function withError($code, $message, $data = '') + { + $this->errorCode = $code; + $this->errorMessage = $message; + $this->errorData = $data; + return $this; + } + + /** + * Set exception + * + * @access public + * @param Exception $exception + * @return $this + */ + public function withException(Exception $exception) + { + $this->exception = $exception; + return $this; + } + + /** + * Add HTTP header + * + * @access public + * @param string $name + * @param string $value + * @return $this + */ + public function withHeader($name, $value) + { + $this->headers[$name] = $value; + return $this; + } + + /** + * Add HTTP Status + * + * @access public + * @param string $status + * @return $this + */ + public function withStatus($status) + { + $this->status = $status; + return $this; + } + + /** + * Get status + * + * @access public + * @return string + */ + public function getStatus() + { + return $this->status; + } + + /** + * Get headers + * + * @access public + * @return string[] + */ + public function getHeaders() + { + return $this->headers; + } + + /** + * Build response + * + * @access public + * @return string + */ + public function build() + { + $encodedResponse = json_encode($this->buildResponse()); + JsonEncodingValidator::validate(); + + return $encodedResponse; + } + + /** + * Send HTTP headers + * + * @access public + * @return $this + */ + public function sendHeaders() + { + if (! empty($this->status)) { + header($this->status); + } + + foreach ($this->headers as $name => $value) { + header($name.': '.$value); + } + + return $this; + } + + /** + * Build response payload + * + * @access private + * @return array + */ + private function buildResponse() + { + $response = array('jsonrpc' => '2.0'); + $this->handleExceptions(); + + if (! empty($this->errorMessage)) { + $response['error'] = $this->buildErrorResponse(); + } else { + $response['result'] = $this->result; + } + + $response['id'] = $this->id; + return $response; + } + + /** + * Build response error payload + * + * @access private + * @return array + */ + private function buildErrorResponse() + { + $response = array( + 'code' => $this->errorCode, + 'message' => $this->errorMessage, + ); + + if (! empty($this->errorData)) { + $response['data'] = $this->errorData; + } + + return $response; + } + + /** + * Transform exceptions to JSON-RPC errors + * + * @access private + */ + private function handleExceptions() + { + if ($this->exception instanceof InvalidJsonFormatException) { + $this->errorCode = -32700; + $this->errorMessage = 'Parse error'; + $this->id = null; + } elseif ($this->exception instanceof InvalidJsonRpcFormatException) { + $this->errorCode = -32600; + $this->errorMessage = 'Invalid Request'; + $this->id = null; + } elseif ($this->exception instanceof BadFunctionCallException) { + $this->errorCode = -32601; + $this->errorMessage = 'Method not found'; + } elseif ($this->exception instanceof InvalidArgumentException) { + $this->errorCode = -32602; + $this->errorMessage = 'Invalid params'; + } elseif ($this->exception instanceof ResponseEncodingFailureException) { + $this->errorCode = -32603; + $this->errorMessage = 'Internal error'; + $this->errorData = $this->exception->getMessage(); + } elseif ($this->exception instanceof AuthenticationFailureException) { + $this->errorCode = 401; + $this->errorMessage = 'Unauthorized'; + $this->status = 'HTTP/1.0 401 Unauthorized'; + $this->withHeader('WWW-Authenticate', 'Basic realm="JsonRPC"'); + } elseif ($this->exception instanceof AccessDeniedException) { + $this->errorCode = 403; + $this->errorMessage = 'Forbidden'; + $this->status = 'HTTP/1.0 403 Forbidden'; + } elseif ($this->exception instanceof ResponseException) { + $this->errorCode = $this->exception->getCode(); + $this->errorMessage = $this->exception->getMessage(); + $this->errorData = $this->exception->getData(); + } elseif ($this->exception instanceof Exception) { + $this->errorCode = $this->exception->getCode(); + $this->errorMessage = $this->exception->getMessage(); + } + } +} diff --git a/vendor/fguillot/json-rpc/src/JsonRPC/Response/ResponseParser.php b/vendor/fguillot/json-rpc/src/JsonRPC/Response/ResponseParser.php new file mode 100644 index 00000000..02d449ba --- /dev/null +++ b/vendor/fguillot/json-rpc/src/JsonRPC/Response/ResponseParser.php @@ -0,0 +1,154 @@ +<?php + +namespace JsonRPC\Response; + +use BadFunctionCallException; +use InvalidArgumentException; +use Exception; +use JsonRPC\Exception\InvalidJsonFormatException; +use JsonRPC\Exception\InvalidJsonRpcFormatException; +use JsonRPC\Exception\ResponseException; +use JsonRPC\Validator\JsonFormatValidator; + +/** + * Class ResponseParser + * + * @package JsonRPC\Request + * @author Frederic Guillot + */ +class ResponseParser +{ + /** + * Payload + * + * @access private + * @var mixed + */ + private $payload; + + /** + * Do not immediately throw an exception on error. Return it instead. + * + * @var bool + */ + private $returnException = false; + + /** + * Get new object instance + * + * @static + * @access public + * @return ResponseParser + */ + public static function create() + { + return new static(); + } + + /** + * Set Return Exception Or Throw It + * + * @param $returnException + * @return ResponseParser + */ + public function withReturnException($returnException) + { + $this->returnException = $returnException; + return $this; + } + + /** + * Set payload + * + * @access public + * @param mixed $payload + * @return $this + */ + public function withPayload($payload) + { + $this->payload = $payload; + return $this; + } + + /** + * Parse response + * + * @return array|Exception|null + * @throws InvalidJsonFormatException + * @throws BadFunctionCallException + * @throws InvalidJsonRpcFormatException + * @throws InvalidArgumentException + * @throws Exception + * @throws ResponseException + */ + public function parse() + { + JsonFormatValidator::validate($this->payload); + + if ($this->isBatchResponse()) { + $results = array(); + + foreach ($this->payload as $response) { + $results[] = self::create() + ->withReturnException($this->returnException) + ->withPayload($response) + ->parse(); + } + + return $results; + } + + if (isset($this->payload['error']['code'])) { + try { + $this->handleExceptions(); + } catch (Exception $e) { + if ($this->returnException) { + return $e; + } + throw $e; + } + } + + return isset($this->payload['result']) ? $this->payload['result'] : null; + } + + /** + * Handle exceptions + * + * @access private + * @throws InvalidJsonFormatException + * @throws InvalidJsonRpcFormatException + * @throws ResponseException + */ + private function handleExceptions() + { + switch ($this->payload['error']['code']) { + case -32700: + throw new InvalidJsonFormatException('Parse error: '.$this->payload['error']['message']); + case -32600: + throw new InvalidJsonRpcFormatException('Invalid Request: '.$this->payload['error']['message']); + case -32601: + throw new BadFunctionCallException('Procedure not found: '.$this->payload['error']['message']); + case -32602: + throw new InvalidArgumentException('Invalid arguments: '.$this->payload['error']['message']); + default: + throw new ResponseException( + $this->payload['error']['message'], + $this->payload['error']['code'], + null, + isset($this->payload['error']['data']) ? $this->payload['error']['data'] : null + ); + } + } + + /** + * Return true if we have a batch response + * + * @access private + * @return boolean + */ + private function isBatchResponse() + { + return array_keys($this->payload) === range(0, count($this->payload) - 1); + } +} diff --git a/vendor/fguillot/json-rpc/src/JsonRPC/Server.php b/vendor/fguillot/json-rpc/src/JsonRPC/Server.php new file mode 100644 index 00000000..1ed075a4 --- /dev/null +++ b/vendor/fguillot/json-rpc/src/JsonRPC/Server.php @@ -0,0 +1,386 @@ +<?php + +namespace JsonRPC; + +use Closure; +use Exception; +use JsonRPC\Request\BatchRequestParser; +use JsonRPC\Request\RequestParser; +use JsonRPC\Response\ResponseBuilder; +use JsonRPC\Validator\HostValidator; +use JsonRPC\Validator\JsonFormatValidator; +use JsonRPC\Validator\UserValidator; + +/** + * JsonRPC server class + * + * @package JsonRPC + * @author Frederic Guillot + */ +class Server +{ + /** + * Allowed hosts + * + * @access protected + * @var array + */ + protected $hosts = array(); + + /** + * Data received from the client + * + * @access protected + * @var array + */ + protected $payload = array(); + + /** + * List of exceptions that should not be relayed to the client + * + * @access protected + * @var array() + */ + protected $localExceptions = array(); + + /** + * Username + * + * @access protected + * @var string + */ + protected $username = ''; + + /** + * Password + * + * @access protected + * @var string + */ + protected $password = ''; + + /** + * Allowed users + * + * @access protected + * @var array + */ + protected $users = array(); + + /** + * $_SERVER + * + * @access protected + * @var array + */ + protected $serverVariable; + + /** + * ProcedureHandler object + * + * @access protected + * @var ProcedureHandler + */ + protected $procedureHandler; + + /** + * MiddlewareHandler object + * + * @access protected + * @var MiddlewareHandler + */ + protected $middlewareHandler; + + /** + * Response builder + * + * @access protected + * @var ResponseBuilder + */ + protected $responseBuilder; + + /** + * Response builder + * + * @access protected + * @var RequestParser + */ + protected $requestParser; + + /** + * + * Batch request parser + * + * @access protected + * @var BatchRequestParser + */ + protected $batchRequestParser; + + /** + * Constructor + * + * @access public + * @param string $request + * @param array $server + * @param ResponseBuilder $responseBuilder + * @param RequestParser $requestParser + * @param BatchRequestParser $batchRequestParser + * @param ProcedureHandler $procedureHandler + * @param MiddlewareHandler $middlewareHandler + */ + public function __construct( + $request = '', + array $server = array(), + ResponseBuilder $responseBuilder = null, + RequestParser $requestParser = null, + BatchRequestParser $batchRequestParser = null, + ProcedureHandler $procedureHandler = null, + MiddlewareHandler $middlewareHandler = null + ) { + if ($request !== '') { + $this->payload = json_decode($request, true); + } else { + $this->payload = json_decode(file_get_contents('php://input'), true); + } + + $this->serverVariable = $server ?: $_SERVER; + $this->responseBuilder = $responseBuilder ?: ResponseBuilder::create(); + $this->requestParser = $requestParser ?: RequestParser::create(); + $this->batchRequestParser = $batchRequestParser ?: BatchRequestParser::create(); + $this->procedureHandler = $procedureHandler ?: new ProcedureHandler(); + $this->middlewareHandler = $middlewareHandler ?: new MiddlewareHandler(); + } + + /** + * Define alternative authentication header + * + * @access public + * @param string $header Header name + * @return $this + */ + public function setAuthenticationHeader($header) + { + if (! empty($header)) { + $header = 'HTTP_'.str_replace('-', '_', strtoupper($header)); + $value = $this->getServerVariable($header); + + if (! empty($value)) { + list($this->username, $this->password) = explode(':', base64_decode($value)); + } + } + + return $this; + } + + /** + * Get ProcedureHandler + * + * @access public + * @return ProcedureHandler + */ + public function getProcedureHandler() + { + return $this->procedureHandler; + } + + /** + * Get MiddlewareHandler + * + * @access public + * @return MiddlewareHandler + */ + public function getMiddlewareHandler() + { + return $this->middlewareHandler; + } + + /** + * Get username + * + * @access public + * @return string + */ + public function getUsername() + { + return $this->username ?: $this->getServerVariable('PHP_AUTH_USER'); + } + + /** + * Get password + * + * @access public + * @return string + */ + public function getPassword() + { + return $this->password ?: $this->getServerVariable('PHP_AUTH_PW'); + } + + /** + * IP based client restrictions + * + * @access public + * @param array $hosts List of hosts + * @return $this + */ + public function allowHosts(array $hosts) + { + $this->hosts = $hosts; + return $this; + } + + /** + * HTTP Basic authentication + * + * @access public + * @param array $users Dictionary of username/password + * @return $this + */ + public function authentication(array $users) + { + $this->users = $users; + return $this; + } + + /** + * Register a new procedure + * + * @access public + * @deprecated Use $server->getProcedureHandler()->withCallback($procedure, $callback) + * @param string $procedure Procedure name + * @param closure $callback Callback + * @return $this + */ + public function register($procedure, Closure $callback) + { + $this->procedureHandler->withCallback($procedure, $callback); + return $this; + } + + /** + * Bind a procedure to a class + * + * @access public + * @deprecated Use $server->getProcedureHandler()->withClassAndMethod($procedure, $class, $method); + * @param string $procedure Procedure name + * @param mixed $class Class name or instance + * @param string $method Procedure name + * @return $this + */ + public function bind($procedure, $class, $method = '') + { + $this->procedureHandler->withClassAndMethod($procedure, $class, $method); + return $this; + } + + /** + * Bind a class instance + * + * @access public + * @deprecated Use $server->getProcedureHandler()->withObject($instance); + * @param mixed $instance Instance name + * @return $this + */ + public function attach($instance) + { + $this->procedureHandler->withObject($instance); + return $this; + } + + /** + * Exception classes that should not be relayed to the client + * + * @access public + * @param Exception|string $exception + * @return $this + */ + public function withLocalException($exception) + { + $this->localExceptions[] = $exception; + return $this; + } + + /** + * Parse incoming requests + * + * @access public + * @return string + */ + public function execute() + { + try { + JsonFormatValidator::validate($this->payload); + HostValidator::validate($this->hosts, $this->getServerVariable('REMOTE_ADDR')); + UserValidator::validate($this->users, $this->getUsername(), $this->getPassword()); + + $this->middlewareHandler + ->withUsername($this->getUsername()) + ->withPassword($this->getPassword()) + ; + + $response = $this->parseRequest(); + + } catch (Exception $e) { + $response = $this->handleExceptions($e); + } + + $this->responseBuilder->sendHeaders(); + return $response; + } + + /** + * Handle exceptions + * + * @access protected + * @param Exception $e + * @return string + * @throws Exception + */ + protected function handleExceptions(Exception $e) + { + foreach ($this->localExceptions as $exception) { + if ($e instanceof $exception) { + throw $e; + } + } + + return $this->responseBuilder->withException($e)->build(); + } + + /** + * Parse incoming request + * + * @access protected + * @return string + */ + protected function parseRequest() + { + if (BatchRequestParser::isBatchRequest($this->payload)) { + return $this->batchRequestParser + ->withPayload($this->payload) + ->withProcedureHandler($this->procedureHandler) + ->withMiddlewareHandler($this->middlewareHandler) + ->withLocalException($this->localExceptions) + ->parse(); + } + + return $this->requestParser + ->withPayload($this->payload) + ->withProcedureHandler($this->procedureHandler) + ->withMiddlewareHandler($this->middlewareHandler) + ->withLocalException($this->localExceptions) + ->parse(); + } + + /** + * Check existence and get value of server variable + * + * @access protected + * @param string $variable + * @return string|null + */ + protected function getServerVariable($variable) + { + return isset($this->serverVariable[$variable]) ? $this->serverVariable[$variable] : null; + } +} diff --git a/vendor/fguillot/json-rpc/src/JsonRPC/Validator/HostValidator.php b/vendor/fguillot/json-rpc/src/JsonRPC/Validator/HostValidator.php new file mode 100644 index 00000000..7f8c0a04 --- /dev/null +++ b/vendor/fguillot/json-rpc/src/JsonRPC/Validator/HostValidator.php @@ -0,0 +1,30 @@ +<?php + +namespace JsonRPC\Validator; + +use JsonRPC\Exception\AccessDeniedException; + +/** + * Class HostValidator + * + * @package JsonRPC\Validator + * @author Frederic Guillot + */ +class HostValidator +{ + /** + * Validate + * + * @static + * @access public + * @param array $hosts + * @param string $remoteAddress + * @throws AccessDeniedException + */ + public static function validate(array $hosts, $remoteAddress) + { + if (! empty($hosts) && ! in_array($remoteAddress, $hosts)) { + throw new AccessDeniedException('Access Forbidden'); + } + } +} diff --git a/vendor/fguillot/json-rpc/src/JsonRPC/Validator/JsonEncodingValidator.php b/vendor/fguillot/json-rpc/src/JsonRPC/Validator/JsonEncodingValidator.php new file mode 100644 index 00000000..0bbc4abd --- /dev/null +++ b/vendor/fguillot/json-rpc/src/JsonRPC/Validator/JsonEncodingValidator.php @@ -0,0 +1,44 @@ +<?php + +namespace JsonRPC\Validator; + +use JsonRPC\Exception\ResponseEncodingFailureException; + +/** + * Class JsonEncodingValidator + * + * @package JsonRPC\Validator + * @author Frederic Guillot + */ +class JsonEncodingValidator +{ + public static function validate() + { + $jsonError = json_last_error(); + + if ($jsonError !== JSON_ERROR_NONE) { + switch ($jsonError) { + case JSON_ERROR_DEPTH: + $errorMessage = 'Maximum stack depth exceeded'; + break; + case JSON_ERROR_STATE_MISMATCH: + $errorMessage = 'Underflow or the modes mismatch'; + break; + case JSON_ERROR_CTRL_CHAR: + $errorMessage = 'Unexpected control character found'; + break; + case JSON_ERROR_SYNTAX: + $errorMessage = 'Syntax error, malformed JSON'; + break; + case JSON_ERROR_UTF8: + $errorMessage = 'Malformed UTF-8 characters, possibly incorrectly encoded'; + break; + default: + $errorMessage = 'Unknown error'; + break; + } + + throw new ResponseEncodingFailureException($errorMessage, $jsonError); + } + } +} diff --git a/vendor/fguillot/json-rpc/src/JsonRPC/Validator/JsonFormatValidator.php b/vendor/fguillot/json-rpc/src/JsonRPC/Validator/JsonFormatValidator.php new file mode 100644 index 00000000..ca8e7a69 --- /dev/null +++ b/vendor/fguillot/json-rpc/src/JsonRPC/Validator/JsonFormatValidator.php @@ -0,0 +1,30 @@ +<?php + +namespace JsonRPC\Validator; + +use JsonRPC\Exception\InvalidJsonFormatException; + +/** + * Class JsonFormatValidator + * + * @package JsonRPC\Validator + * @author Frederic Guillot + */ +class JsonFormatValidator +{ + /** + * Validate + * + * @static + * @access public + * @param mixed $payload + * @throws InvalidJsonFormatException + */ + public static function validate($payload) + { + if (! is_array($payload)) { + throw new InvalidJsonFormatException('Malformed payload'); + } + } +} + diff --git a/vendor/fguillot/json-rpc/src/JsonRPC/Validator/RpcFormatValidator.php b/vendor/fguillot/json-rpc/src/JsonRPC/Validator/RpcFormatValidator.php new file mode 100644 index 00000000..f253a5a1 --- /dev/null +++ b/vendor/fguillot/json-rpc/src/JsonRPC/Validator/RpcFormatValidator.php @@ -0,0 +1,35 @@ +<?php + +namespace JsonRPC\Validator; + +use JsonRPC\Exception\InvalidJsonRpcFormatException; + +/** + * Class RpcFormatValidator + * + * @package JsonRPC\Validator + * @author Frederic Guillot + */ +class RpcFormatValidator +{ + /** + * Validate + * + * @static + * @access public + * @param array $payload + * @throws InvalidJsonRpcFormatException + */ + public static function validate(array $payload) + { + if (! isset($payload['jsonrpc']) || + ! isset($payload['method']) || + ! is_string($payload['method']) || + $payload['jsonrpc'] !== '2.0' || + (isset($payload['params']) && ! is_array($payload['params']))) { + + throw new InvalidJsonRpcFormatException('Invalid JSON RPC payload'); + } + } +} + diff --git a/vendor/fguillot/json-rpc/src/JsonRPC/Validator/UserValidator.php b/vendor/fguillot/json-rpc/src/JsonRPC/Validator/UserValidator.php new file mode 100644 index 00000000..4f889719 --- /dev/null +++ b/vendor/fguillot/json-rpc/src/JsonRPC/Validator/UserValidator.php @@ -0,0 +1,21 @@ +<?php + +namespace JsonRPC\Validator; + +use JsonRPC\Exception\AuthenticationFailureException; + +/** + * Class UserValidator + * + * @package JsonRPC\Validator + * @author Frederic Guillot + */ +class UserValidator +{ + public static function validate(array $users, $username, $password) + { + if (! empty($users) && (! isset($users[$username]) || $users[$username] !== $password)) { + throw new AuthenticationFailureException('Access not allowed'); + } + } +} |