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' => 'reserved',
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 ?>