Code

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