From 4a64f062bce337d61a3ee18dea4c2d1b88e8561a Mon Sep 17 00:00:00 2001 From: hickert Date: Fri, 4 Jun 2010 10:35:34 +0000 Subject: [PATCH] Updated RPC handling -Added HTTP return codes -Updated login git-svn-id: https://oss.gonicus.de/repositories/gosa/trunk@18844 594d385d-05f5-0310-b6e9-bd551577e9d8 --- gosa-core/include/class_config.inc | 18 ++--- gosa-core/include/class_jsonRPC.inc | 115 ++++++++++++++++++++++++---- 2 files changed, 109 insertions(+), 24 deletions(-) diff --git a/gosa-core/include/class_config.inc b/gosa-core/include/class_config.inc index faf44aab3..7eb2d38b6 100644 --- a/gosa-core/include/class_config.inc +++ b/gosa-core/include/class_config.inc @@ -64,6 +64,8 @@ class config { var $filename = ""; var $last_modified = 0; + private $jsonRPChandle = NULL; + public $configRegistry = NULL; /*! \brief Class constructor of the config class @@ -297,17 +299,13 @@ class config { function getRpcHandle() { - $server = $this->get_cfg_value('core','gosaRpcServer'); - $user = $this->get_cfg_value('core','gosaRpcUser'); - $passwd = $this->get_cfg_value('core','gosaRpcPassword'); - - $handle = new jsonRPC($server); - $handle->login($user, $passwd); - - //Fixme Add checks here - login successful aso. - return($handle); + // Create jsonRPC handle on demand. + if(!$this->jsonRPChandle){ + $this->jsonRPChandle = new jsonRPC($this); + } + return($this->jsonRPChandle); } - + /*! \brief Get a LDAP link object * diff --git a/gosa-core/include/class_jsonRPC.inc b/gosa-core/include/class_jsonRPC.inc index 5058c59a7..98c0d0bf1 100644 --- a/gosa-core/include/class_jsonRPC.inc +++ b/gosa-core/include/class_jsonRPC.inc @@ -3,34 +3,58 @@ class jsonRPC { private $curlHandler = NULL; private $debug; - private $url; + private $config; private $id; + private $lastStats = array(); - public function __construct($url,$debug = false) + public function __construct($config, $debug = false) { - - $this->url = $url; + $this->config = $config; $this->debug = $debug; $this->id = 0; + $this->__login(); + } + + private function __login() + { + // Get connection data + $url = $this->config->get_cfg_value('core','gosaRpcServer'); + $user = $this->config->get_cfg_value('core','gosaRpcUser'); + $passwd = $this->config->get_cfg_value('core','gosaRpcPassword'); // Init Curl handler - $this->curlHandler = curl_init($this->url); - curl_setopt($this->curlHandler, CURLOPT_URL , $this->url); + $this->curlHandler = curl_init($url); + + // Set curl options + curl_setopt($this->curlHandler, CURLOPT_URL , $url); curl_setopt($this->curlHandler, CURLOPT_COOKIESESSION , TRUE); curl_setopt($this->curlHandler, CURLOPT_COOKIEFILE, 'cookiefile.txt'); curl_setopt($this->curlHandler, CURLOPT_POST , TRUE); curl_setopt($this->curlHandler, CURLOPT_RETURNTRANSFER , TRUE); curl_setopt($this->curlHandler, CURLOPT_HTTPHEADER , array('Content-Type: application/json')); + + // Try to login + $this->login($user, $passwd); + } + + + public function getHTTPstatusCode() + { + return((isset($this->lastStats['http_code']))? $this->lastStats['http_code'] : -1 ); } public function get_error() { - return(curl_error($this->curlHandler)); + if($this->lastStats['http_code'] != 200){ + return($this->getHttpStatusCodeMessage($this->lastStats['http_code'])); + }else{ + return(curl_error($this->curlHandler)); + } } public function success() { - return(curl_errno($this->curlHandler) == 0); + return(curl_errno($this->curlHandler) == 0 && $this->lastStats['http_code'] == 200); } public function __destruct() @@ -40,8 +64,14 @@ class jsonRPC { public function __call($method,$params) { - if (!is_scalar($method)) trigger_error('jsonRPC::__call requires a scalar value as first parameter!'); + // Check if handle is still valid! + if(! $this->curlHandler) $this->__login(); + // Reset stats of last request. + $this->lastStats = array(); + + // Validate input values + if (!is_scalar($method)) trigger_error('jsonRPC::__call requires a scalar value as first parameter!'); if (is_array($params)) { $params = array_values($params); } else { @@ -57,12 +87,69 @@ class jsonRPC { $response = curl_exec($this->curlHandler); $response = json_decode($response,true); - // Check responce id - if ($response['id'] != $this->id){ - #trigger_error('jsonPRC returned incorrect response '. - # 'id (request id: '.$this->id.', response id: '.$response['id'].')'); - } + // Set current result stats. + $this->lastStats = curl_getinfo($this->curlHandler); return($response['result']); } + + + public static function getHttpStatusCodeMessage($code) + { + $codes = array( + '100' =>_('Continue'), + '101' =>_('Switching Protocols'), + '102' =>_('Processing'), + '200' =>_('OK'), + '201' =>_('Created'), + '202' =>_('Accepted'), + '203' =>_('Non-Authoritative Information'), + '204' =>_('No Content'), + '205' =>_('Reset Content'), + '206' =>_('Partial Content'), + '207' =>_('Multi-Status'), + '300' =>_('Multiple Choice'), + '301' =>_('Moved Permanently'), + '302' =>_('Found'), + '303' =>_('See Other'), + '304' =>_('Not Modified'), + '305' =>_('Use Proxy'), + '306' =>_('(reserviert)'), + '307' =>_('Temporary Redirect'), + '400' =>_('Bad Request'), + '401' =>_('Unauthorized'), + '402' =>_('Payment Required'), + '403' =>_('Forbidden'), + '404' =>_('Not Found'), + '405' =>_('Method Not Allowed'), + '406' =>_('Not Acceptable'), + '407' =>_('Proxy Authentication Required'), + '408' =>_('Request Time-out'), + '409' =>_('Conflict'), + '410' =>_('Gone'), + '411' =>_('Length Required'), + '412' =>_('Precondition Failed'), + '413' =>_('Request Entity Too Large'), + '414' =>_('Request-URI Too Long'), + '415' =>_('Unsupported Media Type'), + '416' =>_('Requested range not satisfiable'), + '417' =>_('Expectation Failed'), + '421' =>_('There are too many connections from your internet address'), + '422' =>_('Unprocessable Entity'), + '423' =>_('Locked'), + '424' =>_('Failed Dependency'), + '425' =>_('Unordered Collection'), + '426' =>_('Upgrade Required'), + '500' =>_('Internal Server Error'), + '501' =>_('Not Implemented'), + '502' =>_('Bad Gateway'), + '503' =>_('Service Unavailable'), + '504' =>_('Gateway Time-out'), + '505' =>_('HTTP Version not supported'), + '506' =>_('Variant Also Negotiates'), + '507' =>_('Insufficient Storage'), + '509' =>_('Bandwidth Limit Exceeded'), + '510' =>_('Not Extended')); + return((isset($codes[$code]))? $codes[$code] : sprintf(_("Unknown HTTP status code '%s'!"), $code)); + } } ?> -- 2.30.2