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_SSL_VERIFYPEER, TRUE);
38 curl_setopt($this->curlHandler, CURLOPT_HTTPHEADER , array('Content-Type: application/json'));
40 // Try to login
41 $this->login($user, $passwd);
43 }
46 public function getHTTPstatusCode()
47 {
48 return((isset($this->lastStats['http_code']))? $this->lastStats['http_code'] : -1 );
49 }
51 public function get_error()
52 {
53 if($this->lastStats['http_code'] != 200){
54 return($this->getHttpStatusCodeMessage($this->lastStats['http_code']));
55 }else{
56 return(curl_error($this->curlHandler));
57 }
58 }
60 public function success()
61 {
62 return(curl_errno($this->curlHandler) == 0 && $this->lastStats['http_code'] == 200);
63 }
65 public function __destruct()
66 {
67 if($this->curlHandler){
68 curl_close($this->curlHandler);
69 }
70 }
72 public function __call($method,$params)
73 {
74 // Check if handle is still valid!
75 if(!$this->curlHandler && $this->lastAction != 'login'){
76 $this->__login();
77 }
79 // Start request
80 DEBUG (DEBUG_RPC, __LINE__, __FUNCTION__, __FILE__,"{$method}", "Calling: ");
81 $response = $this->request($method,$params);
82 if($this->success()){
83 DEBUG (DEBUG_RPC, __LINE__, __FUNCTION__, __FILE__,
84 (is_array($response['result']))?$response['result']:bold($response['result']), "Result: ");
85 }else{
86 DEBUG (DEBUG_RPC, __LINE__, __FUNCTION__, __FILE__,bold($this->get_error())."<br>".$response, "Result (FAILED): ");
87 }
89 return($response['result']);
90 }
93 private function request($method, $params)
94 {
95 // Set last action
96 $this->lastAction = $method;
98 // Reset stats of last request.
99 $this->lastStats = array();
101 // Validate input values
102 if (!is_scalar($method)) trigger_error('jsonRPC::__call requires a scalar value as first parameter!');
103 if (is_array($params)) {
104 $params = array_values($params);
105 } else {
106 trigger_error('jsonRPC::__call requires an array value as second parameter!');
107 }
109 // prepares the request
110 $this->id ++;
111 $request = json_encode(array('method' => $method,'params' => $params,'id' => $this->id));
113 // Set curl options
114 curl_setopt($this->curlHandler, CURLOPT_POSTFIELDS , $request);
115 $response = curl_exec($this->curlHandler);
116 $response = json_decode($response,true);
118 // Set current result stats.
119 $this->lastStats = curl_getinfo($this->curlHandler);
121 return($response);
122 }
125 public static function getHttpStatusCodeMessage($code)
126 {
127 $codes = array(
128 '100' => 'Continue',
129 '101' => 'Switching Protocols',
130 '102' => 'Processing',
131 '200' => 'OK',
132 '201' => 'Created',
133 '202' => 'Accepted',
134 '203' => 'Non-Authoritative Information',
135 '204' => 'No Content',
136 '205' => 'Reset Content',
137 '206' => 'Partial Content',
138 '207' => 'Multi-Status',
139 '300' => 'Multiple Choice',
140 '301' => 'Moved Permanently',
141 '302' => 'Found',
142 '303' => 'See Other',
143 '304' => 'Not Modified',
144 '305' => 'Use Proxy',
145 '306' => 'reserved',
146 '307' => 'Temporary Redirect',
147 '400' => 'Bad Request',
148 '401' => 'Unauthorized',
149 '402' => 'Payment Required',
150 '403' => 'Forbidden',
151 '404' => 'Not Found',
152 '405' => 'Method Not Allowed',
153 '406' => 'Not Acceptable',
154 '407' => 'Proxy Authentication Required',
155 '408' => 'Request Time-out',
156 '409' => 'Conflict',
157 '410' => 'Gone',
158 '411' => 'Length Required',
159 '412' => 'Precondition Failed',
160 '413' => 'Request Entity Too Large',
161 '414' => 'Request-URI Too Long',
162 '415' => 'Unsupported Media Type',
163 '416' => 'Requested range not satisfiable',
164 '417' => 'Expectation Failed',
165 '421' => 'There are too many connections from your internet address',
166 '422' => 'Unprocessable Entity',
167 '423' => 'Locked',
168 '424' => 'Failed Dependency',
169 '425' => 'Unordered Collection',
170 '426' => 'Upgrade Required',
171 '500' => 'Internal Server Error',
172 '501' => 'Not Implemented',
173 '502' => 'Bad Gateway',
174 '503' => 'Service Unavailable',
175 '504' => 'Gateway Time-out',
176 '505' => 'HTTP Version not supported',
177 '506' => 'Variant Also Negotiates',
178 '507' => 'Insufficient Storage',
179 '509' => 'Bandwidth Limit Exceeded',
180 '510' => 'Not Extended');
181 return((isset($codes[$code]))? $codes[$code] : sprintf(_("Unknown HTTP status code '%s'!"), $code));
182 }
183 }
184 ?>