forked from subutux/json-rpc2php
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jsonRPC2Server.php
291 lines (270 loc) · 8.79 KB
/
jsonRPC2Server.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
<?php
error_reporting(0);
/*
COPYRIGHT
Copyright 2012 Stijn Van Campenhout <[email protected]>
This file is part of JSON-RPC2PHP.
JSON-RPC2PHP is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
JSON-RPC2PHP is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with JSON-RPC2PHP; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
/**
* This class builds a json-RPC 2.0 Server
* http://www.jsonrpc.org/spec.html
*
* original idea from jsonrpcphp class of Sergio Vaccaro <[email protected]>, http://jsonrpcphp.org/
* @author stijn <[email protected]>
* @version 1.2
*/
class jsonRPCServer {
public $classes = array();
public $request;
public $extension;
public $response;
private $errorMessages = array(
'-32700' => 'Parse error',
'-32600' => 'Invalid request',
'-32601' => 'Method not found',
'-32602' => 'Invalid parameters',
'-32603' => 'Internal error',
'-32604' => 'Authentication error',
'-32000' => 'Extension not found'
);
private $errorMessagesFull = array(
'-32700' => 'Invalid JSON was received by the server. An error occurred on the server while parsing the JSON string.',
'-32600' => 'The JSON sent is not a valid Request object.',
'-32601' => 'The method does not exist / is not available.',
'-32602' => 'Invalid method parameters.',
'-32603' => 'Internal Server error.',
'-32000' => 'The requested extension does not exist / is not available.',
'-32604' => 'User unknown / Password / Session id incorrect.'
);
private $errorCodes = array(
'parseError' => '-32700',
'invalidRequest' => '-32600',
'methodNotFound' => '-32601',
'invalidParameters' => '-32602',
'internalError' => '-32603',
'authenticationError' => '-32604',
'extensionNotFound' => '-32000'
);
private $users = array();
/**
* Register a class as an extension
* methods will be available as [class].[method]
*
* @param object $obj
* return boolean
*/
public function registerClass($obj){
$this->classes[get_class($obj)] = $obj;
return true;
}
/**
* Adds a user that's allowed to access the RPC
* @param string $user Username
* @param string $password Password
* @return Bool Return true
*/
public function registerUser($user,$password){
$this->users[$user] = $password;
foreach ($this->users as $user => $pass){
}
return true;
}
/**
* Handles the authentication
* @param Array $HTTPHeaders Contains the apache_request_headers()
*/
private function authenticate($HTTPHeaders){
foreach($HTTPHeaders as $i => $c){
$HTTPHeaders[strtolower($i)] = $c;
}
if (isset($HTTPHeaders['x-rpc-auth-username']) && isset($HTTPHeaders['x-rpc-auth-password'])){
if ($this->users[$HTTPHeaders['x-rpc-auth-username']] == $HTTPHeaders['x-rpc-auth-password']){
session_start();
$sid = session_id();
$_SESSION["ip"] = $_SERVER["REMOTE_ADDR"];
header('x-RPC-Auth-Session: ' . $sid);
} else {
throw new Exception($this->errorCodes['authenticationError']);
}
} else if (isset($HTTPHeaders['x-rpc-auth-session'])){
session_id($HTTPHeaders['x-rpc-auth-session']);
session_start();
if ($_SESSION['ip'] == $_SERVER["REMOTE_ADDR"]){
return true;
} else {
throw new Exception($this->errorCodes['authenticationError']);
}
} else {
throw new Exception($this->errorCodes['authenticationError']);
}
}
/**
* responses to 'rpc.' calls.
*
*/
public function rpcCalls() {
if ($this->request['method'] == "listMethods"){
foreach ($this->classes as $ext => $class){
$methods[$ext] = get_class_methods($class);
}
if (isset($this->request['params']['extension'])){
if (array_key_exists($this->request['params']['extension'],$this->classes)){
$this->ok(array($this->request['params']['extension'] => $methods[$this->request['params']['extension']]));
$this->sendResponse();
} else {
$this->error($this->errorCodes['extensionNotFound'],"requested extension not found in extension list." );
$this->sendResponse();
}
} else {
$this->ok($methods);
$this->sendResponse();
}
} else {
$this->error($this->errorCodes['methodNotFound']);
$this->sendResponse();
}
return true;
}
/**
* This function validates the incoming json string
* - checks the request method
* - checks if we can parse the json
* - checks if the extension exists
* - checks if the method exists in the given extension
*
* @return boolean
*/
private function validate() {
try {
if ($_SERVER['REQUEST_METHOD'] != 'POST' || empty($_SERVER['CONTENT_TYPE']) || strpos($_SERVER['CONTENT_TYPE'], 'application/json') === false) {
throw new Exception($this->errorCodes['invalidRequest']);
}
$this->request = json_decode(file_get_contents('php://input'),true);
if (empty($this->request)){
throw new Exception($this->errorCodes['parseError']);
}
$requestMethod = explode('.',$this->request['method']);
$this->extension = $requestMethod[0];
if (!isset($this->classes[$this->extension]) && $this->extension != "rpc"){
throw new Exception($this->errorCodes['extensionNotFound']);
}
$this->request['method'] = $requestMethod[1];
if (!isset($this->classes[$this->extension]) && !method_exists($this->classes[$this->extension],$this->request['method']) && $this->extension != "rpc"){
throw new Exception($this->errorCodes['methodNotFound']);
};
} catch (Exception $e) {
$this->error($e->getMessage());
$this->sendResponse();
return false;
}
return true;
}
/**
* Builds the error response
*
* @todo make this in a execption class
* @param string $c the error code
* @param string $fmsg the full message of the error
*/
private function error($c,$fmsg=false,$errorData = Array('no data')){
$this->response = array (
'jsonrpc' => '2.0',
'id' => (isset($this->request['id'])) ? $this->request['id'] : NULL,
'result' => NULL,
'error' => array(
'code' => (int)$c,
'message' => (isset($this->errorMessages[$c])) ? $this->errorMessages[$c] : 'internalError',
'data' => array(
'request' => (isset($this->request)) ? $this->request : NULL,
'extension' => (isset($this->extension)) ? $this->extension : NULL,
'fullMessage' => ($fmsg) ? $fmsg : $this->errorMessagesFull[$c],
'debugData' => $errorData
)
)
);
return true;
}
private function toUtf8(array $array) {
$convertedArray = array();
foreach($array as $key => $value) {
if(!mb_check_encoding($key, 'UTF-8')) $key = utf8_encode($key);
if(is_array($value)) $value = $this->toUtf8($value);
$convertedArray[$key] = $value;
}
return $convertedArray;
}
private function ok($result){
//print_r($result);
$this->response = array (
'jsonrpc' => '2.0',
'id' => $this->request['id'],
'result' => $result,
'error' => NULL
);
}
/**
* check if there is a response needed & sends the response
*
*/
private function sendResponse(){
if (!empty($this->request['id'])) { // notifications don't want response
header('content-type: application/json');
die( json_encode($this->response) );
}
}
/**
* main class method. starts all the magic
*
*/
public function handle() {
/* If there are no users defined, don't use authentication */
if ($this->extension != "rpc"){
$this->validate();
}
try {
if (!empty($this->users)){
$this->authenticate(apache_request_headers());
}
if ($this->extension == "rpc"){
$this->rpcCalls();
}
$obj = $this->classes[$this->extension];
if (($result = call_user_func_array(array($obj,$this->request['method']),$this->request['params'])) !== false) {
$this->ok((is_array($result)) ? $result : Array($result));
} else {
throw new jsonRPCException('Method function returned false.');
}
} catch (jsonRPCException $e) {
$c = ($e->getCode() != 0) ? $e->getCode() : $this->errorCodes['internalError'];
$this->error($c,$e->getMessage(),$e->_getErrorData());
}
$this->sendResponse();
return true;
}
}
Class jsonRPCException extends Exception {
private $_errorData;
public function __construct($message,
$code = 0,
Exception $previous = null,
$errorData = Array('no data'))
{
parent::__construct($message,$code,$previous);
$this->_errorData = $errorData;
}
public function _getErrorData(){
return $this->_errorData;
}
}
?>