1 <?php
2 class jsonRPC {
4 private $curlHandler = NULL;
5 private $config;
6 private $id;
7 private $lastStats = array();
8 private $lastAction = "none";
11 private $url = "";
12 private $user = "";
13 private $password = "";
15 private $authModeDigest = FALSE;
17 public function __construct($config, $url = "", $user = "", $password = "", $digest = FALSE)
18 {
19 $this->config = $config;
20 $this->id = 0;
22 // Get connection data
23 $this->url = (!empty($url)) ? $url : $this->config->get_cfg_value('core','gosaRpcServer');
24 $this->user = (!empty($user)) ? $user : $this->config->get_cfg_value('core','gosaRpcUser');
25 $this->passwd = (!empty($password)) ? $password : $this->config->get_cfg_value('core','gosaRpcPassword');
26 $this->authModeDigest = $digest;
28 // Put some usefull info in the logs
29 DEBUG (DEBUG_RPC, __LINE__, __FUNCTION__, __FILE__,bold($this->url), "Initiated RPC ");
30 DEBUG (DEBUG_RPC, __LINE__, __FUNCTION__, __FILE__,bold($this->user), "RPC user: ");
31 DEBUG (DEBUG_RPC, __LINE__, __FUNCTION__, __FILE__,bold($this->passwd),"RPC password: ");
32 DEBUG (DEBUG_RPC, __LINE__, __FUNCTION__, __FILE__,bold($this->authModeDigest),"Digest Auth (0: No, 1: Yes): ");
34 $this->__login();
35 }
37 private function __login()
38 {
39 // Init Curl handler
40 $this->curlHandler = curl_init($this->url);
42 // Set curl options
43 curl_setopt($this->curlHandler, CURLOPT_URL , $this->url);
44 curl_setopt($this->curlHandler, CURLOPT_POST , TRUE);
45 curl_setopt($this->curlHandler, CURLOPT_RETURNTRANSFER ,TRUE);
46 curl_setopt($this->curlHandler, CURLOPT_HTTPHEADER , array('Content-Type: application/json'));
47 curl_setopt($this->curlHandler, CURLOPT_SSL_VERIFYPEER, FALSE);
49 // Try to login
50 if($this->authModeDigest){
51 curl_setopt($this->curlHandler, CURLOPT_USERPWD , "{$this->user}:{$this->passwd}");
52 curl_setopt($this->curlHandler, CURLOPT_HTTPAUTH , CURLAUTH_ANYSAFE);
53 }else{
54 curl_setopt($this->curlHandler, CURLOPT_COOKIESESSION , TRUE);
55 curl_setopt($this->curlHandler, CURLOPT_COOKIEFILE, 'cookiefile.txt');
56 $this->login($this->user, $this->passwd);
57 }
58 }
61 public function getHTTPstatusCode()
62 {
63 return((isset($this->lastStats['http_code']))? $this->lastStats['http_code'] : -1 );
64 }
66 public function get_error()
67 {
68 if($this->lastStats['http_code'] != 200){
69 return($this->getHttpStatusCodeMessage($this->lastStats['http_code']));
70 }else{
71 return(curl_error($this->curlHandler));
72 }
73 }
75 public function success()
76 {
77 return(curl_errno($this->curlHandler) == 0 && $this->lastStats['http_code'] == 200);
78 }
80 public function __destruct()
81 {
82 if($this->curlHandler){
83 curl_close($this->curlHandler);
84 }
85 }
87 public function __call($method,$params)
88 {
89 // Check if handle is still valid!
90 if(!$this->curlHandler && $this->lastAction != 'login'){
91 $this->__login();
92 }
94 // Start request
95 DEBUG (DEBUG_RPC, __LINE__, __FUNCTION__, __FILE__,"{$method}", "Calling: ");
96 $response = $this->request($method,$params);
97 if($this->success()){
98 DEBUG (DEBUG_RPC, __LINE__, __FUNCTION__, __FILE__,
99 (is_array($response['result']))?$response['result']:bold($response['result']), "Result: ");
100 }else{
101 DEBUG (DEBUG_RPC, __LINE__, __FUNCTION__, __FILE__,bold($this->get_error())."<br>".$response, "Result (FAILED): ");
102 }
104 return($response['result']);
105 }
108 private function request($method, $params)
109 {
110 // Set last action
111 $this->lastAction = $method;
113 // Reset stats of last request.
114 $this->lastStats = array();
116 // Validate input values
117 if (!is_scalar($method)) trigger_error('jsonRPC::__call requires a scalar value as first parameter!');
118 if (is_array($params)) {
119 $params = array_values($params);
120 } else {
121 trigger_error('jsonRPC::__call requires an array value as second parameter!');
122 }
124 // prepares the request
125 $this->id ++;
126 $request = json_encode(array('method' => $method,'params' => $params,'id' => $this->id));
128 // Set curl options
129 curl_setopt($this->curlHandler, CURLOPT_POSTFIELDS , $request);
130 $response = curl_exec($this->curlHandler);
131 $response = json_decode($response,true);
133 // Set current result stats.
134 $this->lastStats = curl_getinfo($this->curlHandler);
136 return($response);
137 }
140 public static function getHttpStatusCodeMessage($code)
141 {
142 $codes = array(
143 '100' => 'Continue',
144 '101' => 'Switching Protocols',
145 '102' => 'Processing',
146 '200' => 'OK',
147 '201' => 'Created',
148 '202' => 'Accepted',
149 '203' => 'Non-Authoritative Information',
150 '204' => 'No Content',
151 '205' => 'Reset Content',
152 '206' => 'Partial Content',
153 '207' => 'Multi-Status',
154 '300' => 'Multiple Choice',
155 '301' => 'Moved Permanently',
156 '302' => 'Found',
157 '303' => 'See Other',
158 '304' => 'Not Modified',
159 '305' => 'Use Proxy',
160 '306' => 'reserved',
161 '307' => 'Temporary Redirect',
162 '400' => 'Bad Request',
163 '401' => 'Unauthorized',
164 '402' => 'Payment Required',
165 '403' => 'Forbidden',
166 '404' => 'Not Found',
167 '405' => 'Method Not Allowed',
168 '406' => 'Not Acceptable',
169 '407' => 'Proxy Authentication Required',
170 '408' => 'Request Time-out',
171 '409' => 'Conflict',
172 '410' => 'Gone',
173 '411' => 'Length Required',
174 '412' => 'Precondition Failed',
175 '413' => 'Request Entity Too Large',
176 '414' => 'Request-URI Too Long',
177 '415' => 'Unsupported Media Type',
178 '416' => 'Requested range not satisfiable',
179 '417' => 'Expectation Failed',
180 '421' => 'There are too many connections from your internet address',
181 '422' => 'Unprocessable Entity',
182 '423' => 'Locked',
183 '424' => 'Failed Dependency',
184 '425' => 'Unordered Collection',
185 '426' => 'Upgrade Required',
186 '500' => 'Internal Server Error',
187 '501' => 'Not Implemented',
188 '502' => 'Bad Gateway',
189 '503' => 'Service Unavailable',
190 '504' => 'Gateway Time-out',
191 '505' => 'HTTP Version not supported',
192 '506' => 'Variant Also Negotiates',
193 '507' => 'Insufficient Storage',
194 '509' => 'Bandwidth Limit Exceeded',
195 '510' => 'Not Extended');
196 return((isset($codes[$code]))? $codes[$code] : sprintf(_("Unknown HTTP status code '%s'!"), $code));
197 }
198 }
199 ?>