Add dogecoin address validation
[elbandi:minifaucet.git] / jsonRPCClient.php
1 <?php
2 /*
3                                         COPYRIGHT
4
5 Copyright 2007 Sergio Vaccaro <sergio@inservibile.org>
6
7 This file is part of JSON-RPC PHP.
8
9 JSON-RPC PHP is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
13
14 JSON-RPC PHP is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 GNU General Public License for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with JSON-RPC PHP; if not, write to the Free Software
21 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
22 */
23
24 /**
25  * The object of this class are generic jsonRPC 1.0 clients
26  * http://json-rpc.org/wiki/specification
27  *
28  * @author sergio <jsonrpcphp@inservibile.org>
29  */
30 class jsonRPCClient {
31         
32         /**
33          * Debug state
34          *
35          * @var boolean
36          */
37         private $debug;
38         
39         /**
40          * The server URL
41          *
42          * @var string
43          */
44         private $url;
45         /**
46          * The request id
47          *
48          * @var integer
49          */
50         private $id;
51         /**
52          * If true, notifications are performed instead of requests
53          *
54          * @var boolean
55          */
56         private $notification = false;
57         
58         /**
59          * Takes the connection parameters
60          *
61          * @param string $url
62          * @param boolean $debug
63          */
64         public function __construct($url,$debug = false) {
65                 // server URL
66                 $this->url = $url;
67                 // proxy
68                 empty($proxy) ? $this->proxy = '' : $this->proxy = $proxy;
69                 // debug state
70                 empty($debug) ? $this->debug = false : $this->debug = true;
71                 // message id
72                 $this->id = 1;
73         }
74         
75         /**
76          * Sets the notification state of the object. In this state, notifications are performed, instead of requests.
77          *
78          * @param boolean $notification
79          */
80         public function setRPCNotification($notification) {
81                 empty($notification) ?
82                                                         $this->notification = false
83                                                         :
84                                                         $this->notification = true;
85         }
86         
87         /**
88          * Performs a jsonRCP request and gets the results as an array
89          *
90          * @param string $method
91          * @param array $params
92          * @return array
93          */
94         public function __call($method,$params) {
95                 
96                 // check
97                 if (!is_scalar($method)) {
98                         throw new Exception('Method name has no scalar value');
99                 }
100                 
101                 // check
102                 if (is_array($params)) {
103                         // no keys
104                         $params = array_values($params);
105                 } else {
106                         throw new Exception('Params must be given as array');
107                 }
108                 
109                 // sets notification or request task
110                 if ($this->notification) {
111                         $currentId = NULL;
112                 } else {
113                         $currentId = $this->id;
114                 }
115                 
116                 // prepares the request
117                 $request = array(
118                                                 'method' => $method,
119                                                 'params' => $params,
120                                                 'id' => $currentId
121                                                 );
122                 $request = json_encode($request);
123                 $this->debug && $this->debug.='***** Request *****'."\n".$request."\n".'***** End Of request *****'."\n\n";
124                 
125                 // performs the HTTP POST using curl
126                 $curl = curl_init();
127                 curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
128                 curl_setopt($curl, CURLOPT_HTTPHEADER, array("Content-type: application/json"));
129                 curl_setopt($curl, CURLOPT_URL, $this->url);
130                 curl_setopt($curl, CURLOPT_POST, TRUE);
131                 curl_setopt($curl, CURLOPT_POSTFIELDS, $request);
132                 $response = curl_exec($curl);
133                 curl_close($curl);
134                 
135                 // process response
136                 if (!$response) {
137                         throw new Exception('Unable to connect to '.$this->url, 0);
138                 }
139                 
140                 $this->debug && $this->debug.='***** Server response *****'."\n".$response.'***** End of server response *****'."\n";
141                 $response = json_decode($response,true);
142                 
143                 // debug output
144                 if ($this->debug) {
145                         echo nl2br($debug);
146                 }
147                 
148                 // final checks and return
149                 if (!$this->notification) {
150                         // check
151                         if ($response['id'] != $currentId) {
152                                 throw new Exception('Incorrect response id (request id: '.$currentId.', response id: '.$response['id'].')');
153                         }
154                         if (!is_null($response['error'])) {
155                                 $error = $response['error'];
156                                 throw new Exception($error['message'], $error["code"]);
157                         }
158                         
159                         return $response['result'];
160                         
161                 } else {
162                         return true;
163                 }
164         }
165 }