forked from ParspooyeshFanavar/ibsng-docs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjsonrpc.inc
executable file
·229 lines (194 loc) · 5.87 KB
/
jsonrpc.inc
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
<?php
class jsonrpc_client {
private $debug;
private $path = '/';
private $server;
private $port = 80;
private $errno;
private $errstr;
private $id;
private $method = 'http';
private $username = '';
private $password = '';
public function __construct($url, $debug = false) {
$parts = parse_url($url);
$server = $parts['host'];
$path = isset($parts['path']) ? $parts['path'] : '';
if(isset($parts['query']))
{
$path .= '?'.$parts['query'];
}
if(isset($parts['fragment']))
{
$path .= '#'.$parts['fragment'];
}
if(isset($parts['port']))
{
$port = $parts['port'];
}
if(isset($parts['scheme']))
{
$method = $parts['scheme'];
}
if(isset($parts['user']))
{
$this->username = $parts['user'];
}
if(isset($parts['pass']))
{
$this->password = $parts['pass'];
}
if($path == '' || $path[0] != '/')
{
$this->path = '/'.$path;
}
else
{
$this->path = $path;
}
$this->server = $server;
if($port != '')
{
$this->port = $port;
}
if(isset($method) && $method != '')
{
$this->method = $method;
}
$this->debug = (bool) $debug;
// message id
$this->id = 1;
}
/**
* Sets the notification state of the object. In this state, notifications are performed, instead of requests.
*
* @param boolean $notification
*/
public function setRPCNotification($notification) {
empty($notification) ?
$this->notification = false
:
$this->notification = true;
}
/**
* Performs a jsonRCP request and gets the results as an array
*
* @param string $method
* @param array $params
* @return array
*/
public function send($method, $params, $timeout) {
// sets notification or request task
if (isset($this->notification) && $this->notification) {
$currentId = NULL;
} else {
$currentId = $this->id;
}
// prepares the request
$request = array(
'method' => $method,
'params' => $params,
'id' => $currentId
);
$request = json_encode($request);
$uri = '/';
$op= 'POST ' . $uri. " HTTP/1.0\r\n" .
'Host: '. $this->server . ':' . $this->port . "\r\n" .
"Content-Type: application/json\r\n".
'Content-Length: ' . strlen($request) . "\r\n\r\n" .
$request;
if($timeout > 0) {
$fp = @fsockopen($this->server, $this->port, $this->errno, $this->errstr, $timeout);
} else {
$fp = @fsockopen($this->server, $this->port, $this->errno, $this->errstr);
}
if($fp) {
if($timeout > 0 && function_exists('stream_set_timeout')) {
stream_set_timeout($fp, $timeout);
}
} else {
$this->errstr = 'Connect error: '. $this->errstr . ' (' . $this->errno . ')';
$r = new jsonrpcresp(0, 1, $this->errstr); // TODO: replace 1 with better errorno
return $r;
}
if(!fputs($fp, $op, strlen($op))) {
fclose($fp);
$this->errstr = 'Write error';
$r = new jsonrpcresp(0, 1, $this->errstr); // TODO: replace 1 with better errorno
return $r;
} else {
// reset errno and errstr on succesful socket connection
$this->errstr = '';
}
$response = '';
do {
$response .= fread($fp, 32768);
} while(!feof($fp));
fclose($fp);
list($error, $result) = $this->parseResponse($response);
$r = new jsonrpcresp($result, (bool)$error, (string)$error);
return $r;
}
public function parseResponse($response) {
$response = $this->stripHeaders($response);
$response = json_decode($response, true);
return array($response['error'], $response['result']);
}
public function stripHeaders($response) {
list($header, $content) = explode("\r\n\r\n", $response, 2);
return $content;
}
}
class jsonrpcresp
{
var $val = 0;
var $errno = 0;
var $errstr = '';
/**
* @param mixed $val either an jsonrpcval obj, a php value or the json serialization of an jsonrpcval (a string)
* @param integer $fcode set it to anything but 0 to create an error response
* @param string $fstr the error string, in case of an error response
*/
function jsonrpcresp($val, $fcode = 0, $fstr = '')
{
if($fcode != 0)
{
// error response
$this->errno = $fcode;
$this->errstr = $fstr;
}
else
{
// successful response
$this->val = $val;
}
}
/**
* Returns the error code of the response.
* @return integer the error code of this response (0 for not-error responses)
* @access public
*/
function faultCode()
{
return $this->errno;
}
/**
* Returns the error code of the response.
* @return string the error string of this response ('' for not-error responses)
* @access public
*/
function faultString()
{
return $this->errstr;
}
/**
* Returns the value received by the server.
* @return mixed the jsonrpcval object returned by the server. Might be an json string or php value if the response has been created by specially configured jsonrpc_client objects
* @access public
*/
function value()
{
return $this->val;
}
}
?>