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__,bold($response['result']), "Result: ");
83 }else{
84 DEBUG (DEBUG_RPC, __LINE__, __FUNCTION__, __FILE__,bold($this->get_error())."<br>".$response, "Result (FAILED): ");
85 }
87 return($response['result']);
88 }
91 private function request($method, $params)
92 {
93 // Set last action
94 $this->lastAction = $method;
96 // Reset stats of last request.
97 $this->lastStats = array();
99 // Validate input values
100 if (!is_scalar($method)) trigger_error('jsonRPC::__call requires a scalar value as first parameter!');
101 if (is_array($params)) {
102 $params = array_values($params);
103 } else {
104 trigger_error('jsonRPC::__call requires an array value as second parameter!');
105 }
107 // prepares the request
108 $this->id ++;
109 $request = json_encode(array('method' => $method,'params' => $params,'id' => $this->id));
111 // Set curl options
112 curl_setopt($this->curlHandler, CURLOPT_POSTFIELDS , $request);
113 $response = curl_exec($this->curlHandler);
114 $response = json_decode($response,true);
116 // Set current result stats.
117 $this->lastStats = curl_getinfo($this->curlHandler);
119 return($response);
120 }
123 public static function getHttpStatusCodeMessage($code)
124 {
125 $codes = array(
126 '100' =>_('Continue'),
127 '101' =>_('Switching Protocols'),
128 '102' =>_('Processing'),
129 '200' =>_('OK'),
130 '201' =>_('Created'),
131 '202' =>_('Accepted'),
132 '203' =>_('Non-Authoritative Information'),
133 '204' =>_('No Content'),
134 '205' =>_('Reset Content'),
135 '206' =>_('Partial Content'),
136 '207' =>_('Multi-Status'),
137 '300' =>_('Multiple Choice'),
138 '301' =>_('Moved Permanently'),
139 '302' =>_('Found'),
140 '303' =>_('See Other'),
141 '304' =>_('Not Modified'),
142 '305' =>_('Use Proxy'),
143 '306' =>_('(reserviert)'),
144 '307' =>_('Temporary Redirect'),
145 '400' =>_('Bad Request'),
146 '401' =>_('Unauthorized'),
147 '402' =>_('Payment Required'),
148 '403' =>_('Forbidden'),
149 '404' =>_('Not Found'),
150 '405' =>_('Method Not Allowed'),
151 '406' =>_('Not Acceptable'),
152 '407' =>_('Proxy Authentication Required'),
153 '408' =>_('Request Time-out'),
154 '409' =>_('Conflict'),
155 '410' =>_('Gone'),
156 '411' =>_('Length Required'),
157 '412' =>_('Precondition Failed'),
158 '413' =>_('Request Entity Too Large'),
159 '414' =>_('Request-URI Too Long'),
160 '415' =>_('Unsupported Media Type'),
161 '416' =>_('Requested range not satisfiable'),
162 '417' =>_('Expectation Failed'),
163 '421' =>_('There are too many connections from your internet address'),
164 '422' =>_('Unprocessable Entity'),
165 '423' =>_('Locked'),
166 '424' =>_('Failed Dependency'),
167 '425' =>_('Unordered Collection'),
168 '426' =>_('Upgrade Required'),
169 '500' =>_('Internal Server Error'),
170 '501' =>_('Not Implemented'),
171 '502' =>_('Bad Gateway'),
172 '503' =>_('Service Unavailable'),
173 '504' =>_('Gateway Time-out'),
174 '505' =>_('HTTP Version not supported'),
175 '506' =>_('Variant Also Negotiates'),
176 '507' =>_('Insufficient Storage'),
177 '509' =>_('Bandwidth Limit Exceeded'),
178 '510' =>_('Not Extended'));
179 return((isset($codes[$code]))? $codes[$code] : sprintf(_("Unknown HTTP status code '%s'!"), $code));
180 }
181 }
182 ?>