Code

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