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