Code

Updated stats
[gosa.git] / gosa-core / include / class_jsonRPC.inc
1 <?php
2 class jsonRPC {
4     private $curlHandler = NULL;
5     private $config;
6     private $id;
7     private $lastStats = array();
8     private $lastAction = "none";
11     private $connectUrl = "";
12     private $username = "";
13     private $userPassword = "";
14     private $authModeDigest = FALSE; 
16     public function __construct($config, $connectUrl="", $username="", $userPassword="", $authModeDigest=FALSE) 
17     {
18         $this->config = $config;
19         $this->id = 0;
21         // Get connection data
22         $this->connectUrl   = (!empty($connectUrl))   ? $connectUrl   : $this->config->get_cfg_value('core','gosaRpcServer');
23         $this->username     = (!empty($username))     ? $username     : $this->config->get_cfg_value('core','gosaRpcUser');
24         $this->userPassword = (!empty($userPassword)) ? $userPassword : $this->config->get_cfg_value('core','gosaRpcPassword');
25         $this->authModeDigest = $authModeDigest;
27         // Put some usefull info in the logs 
28         DEBUG (DEBUG_RPC, __LINE__, __FUNCTION__, __FILE__,bold($this->connectUrl), "Initiated RPC "); 
29         DEBUG (DEBUG_RPC, __LINE__, __FUNCTION__, __FILE__,bold($this->username), "RPC user: "); 
30         DEBUG (DEBUG_RPC, __LINE__, __FUNCTION__, __FILE__,bold($this->userPassword),"RPC password: "); 
31         DEBUG (DEBUG_RPC, __LINE__, __FUNCTION__, __FILE__,bold($this->authModeDigest),"Digest Auth (0: No, 1: Yes): "); 
33         $this->__login();
34     }
36     private function __login()
37     {
38         // Init Curl handler
39         $this->curlHandler = curl_init($this->connectUrl);
41         // Set curl options
42         curl_setopt($this->curlHandler, CURLOPT_URL ,           $this->connectUrl);
43         curl_setopt($this->curlHandler, CURLOPT_POST ,          TRUE);
44         curl_setopt($this->curlHandler, CURLOPT_RETURNTRANSFER ,TRUE);
45         curl_setopt($this->curlHandler, CURLOPT_HTTPHEADER ,    array('Content-Type: application/json'));
46         curl_setopt($this->curlHandler, CURLOPT_SSL_VERIFYPEER, FALSE);
48         // Try to login 
49         if($this->authModeDigest){
50             curl_setopt($this->curlHandler, CURLOPT_USERPWD , "{$this->username}:{$this->userPassword}");
51             curl_setopt($this->curlHandler, CURLOPT_HTTPAUTH , CURLAUTH_ANYSAFE);
52         }else{
53             curl_setopt($this->curlHandler, CURLOPT_COOKIESESSION , TRUE);
54             curl_setopt($this->curlHandler, CURLOPT_COOKIEFILE, 'cookiefile.txt'); 
55             $this->login($this->username, $this->userPassword);
56         }
57     }
58         
60     public function getHTTPstatusCode()
61     {
62         return((isset($this->lastStats['http_code']))? $this->lastStats['http_code'] : -1 );
63     }
65     public function get_error()
66     {
67         if($this->lastStats['http_code'] != 200){
68             return($this->getHttpStatusCodeMessage($this->lastStats['http_code']));
69         }else{
70             return(curl_error($this->curlHandler));
71         }
72     }
74     public function success()
75     {
76         return(curl_errno($this->curlHandler) == 0 && $this->lastStats['http_code'] == 200);
77     }
79     public function __destruct()
80     {
81         if($this->curlHandler){
82              curl_close($this->curlHandler);
83         }
84     }
86     public function __call($method,$params) 
87     {
88         // Check if handle is still valid!
89         if(!$this->curlHandler && $this->lastAction != 'login'){
90              $this->__login();
91         }
93         // Start request
94         DEBUG (DEBUG_RPC, __LINE__, __FUNCTION__, __FILE__,"{$method}", "Calling: "); 
95         $response = $this->request($method,$params);
96         if($this->success()){
97             DEBUG (DEBUG_RPC, __LINE__, __FUNCTION__, __FILE__,
98                 (is_array($response['result']))?$response['result']:bold($response['result']), "Result: "); 
99         }else{
100             DEBUG (DEBUG_RPC, __LINE__, __FUNCTION__, __FILE__,bold($this->get_error())."<br>".$response, "Result (FAILED): "); 
101         }
103         return($response['result']);
104     }
106     
107     private function request($method, $params)
108     {
109         // Set last action 
110         $this->lastAction = $method;
112         // Reset stats of last request.
113         $this->lastStats = array();
114    
115         // Validate input  values
116         if (!is_scalar($method))  trigger_error('jsonRPC::__call requires a scalar value as first parameter!');
117         if (is_array($params)) {
118             $params = array_values($params);
119         } else {
120             trigger_error('jsonRPC::__call requires an array value as second parameter!');
121         }
123         // prepares the request
124         $this->id ++;
125         $request = json_encode(array('method' => $method,'params' => $params,'id' => $this->id));
127         // Set curl options
128         curl_setopt($this->curlHandler, CURLOPT_POSTFIELDS , $request);
129         $response = curl_exec($this->curlHandler);        
130         $response = json_decode($response,true);
132         // Set current result stats.
133         $this->lastStats = curl_getinfo($this->curlHandler);
134     
135         return($response);
136     }
137     
139     public static function getHttpStatusCodeMessage($code)
140     {
141         $codes  = array(
142                 '100' =>  'Continue',
143                 '101' =>  'Switching Protocols',
144                 '102' =>  'Processing',
145                 '200' =>  'OK',
146                 '201' =>  'Created',
147                 '202' =>  'Accepted',
148                 '203' =>  'Non-Authoritative Information',
149                 '204' =>  'No Content',
150                 '205' =>  'Reset Content',
151                 '206' =>  'Partial Content',
152                 '207' =>  'Multi-Status',
153                 '300' =>  'Multiple Choice',
154                 '301' =>  'Moved Permanently',
155                 '302' =>  'Found',
156                 '303' =>  'See Other',
157                 '304' =>  'Not Modified',
158                 '305' =>  'Use Proxy',
159                 '306' =>  'reserved',
160                 '307' =>  'Temporary Redirect',
161                 '400' =>  'Bad Request',
162                 '401' =>  'Unauthorized',
163                 '402' =>  'Payment Required',
164                 '403' =>  'Forbidden',
165                 '404' =>  'Not Found',
166                 '405' =>  'Method Not Allowed',
167                 '406' =>  'Not Acceptable',
168                 '407' =>  'Proxy Authentication Required',
169                 '408' =>  'Request Time-out',
170                 '409' =>  'Conflict',
171                 '410' =>  'Gone',
172                 '411' =>  'Length Required',
173                 '412' =>  'Precondition Failed',
174                 '413' =>  'Request Entity Too Large',
175                 '414' =>  'Request-URI Too Long',
176                 '415' =>  'Unsupported Media Type',
177                 '416' =>  'Requested range not satisfiable',
178                 '417' =>  'Expectation Failed',
179                 '421' =>  'There are too many connections from your internet address',
180                 '422' =>  'Unprocessable Entity',
181                 '423' =>  'Locked',
182                 '424' =>  'Failed Dependency',
183                 '425' =>  'Unordered Collection',
184                 '426' =>  'Upgrade Required',
185                 '500' =>  'Internal Server Error',
186                 '501' =>  'Not Implemented',
187                 '502' =>  'Bad Gateway',
188                 '503' =>  'Service Unavailable',
189                 '504' =>  'Gateway Time-out',
190                 '505' =>  'HTTP Version not supported',
191                 '506' =>  'Variant Also Negotiates',
192                 '507' =>  'Insufficient Storage',
193                 '509' =>  'Bandwidth Limit Exceeded',
194                 '510' =>  'Not Extended');
195         return((isset($codes[$code]))? $codes[$code] : sprintf(_("Unknown HTTP status code '%s'!"), $code));
196     }
198 ?>