index d9084f61c0913dba6c4bbcefb8668f69ddc754dd..12bb2af727e989d6c9d9579cd3dd5580c14952e5 100644 (file)
<?php
+
+
class jsonRPC {
private $curlHandler = NULL;
private $lastStats = array();
private $lastAction = "none";
- public function __construct($config)
+
+ private $connectUrl = "";
+ private $username = "";
+ private $userPassword = "";
+ private $authModeDigest = FALSE;
+
+
+ /*! \brief Constructs a new jsonRPC handle which is connected to a given URL.
+ * It can either connect using a rpc method or via auth method digest.
+ * @param object The gosa configuration object (class_config)
+ * @param string The url to connect to.
+ * @param string The username for authentication
+ * @param string The password to use for authentication
+ * @param boolean Whether to use DIGEST authentication or not.
+ * @return
+ */
+ public function __construct($config, $connectUrl, $username, $userPassword, $authModeDigest=FALSE)
{
$this->config = $config;
$this->id = 0;
+
+ // Get connection data
+ $this->connectUrl = $connectUrl;
+ $this->username = $username;
+ $this->userPassword = $userPassword;
+ $this->authModeDigest = $authModeDigest;
+
+ // Put some usefull info in the logs
+ DEBUG (DEBUG_RPC, __LINE__, __FUNCTION__, __FILE__,bold($this->connectUrl), "Initiated RPC ");
+ DEBUG (DEBUG_RPC, __LINE__, __FUNCTION__, __FILE__,bold($this->username), "RPC user: ");
+ DEBUG (DEBUG_RPC, __LINE__, __FUNCTION__, __FILE__,bold($this->userPassword),"RPC password: ");
+ DEBUG (DEBUG_RPC, __LINE__, __FUNCTION__, __FILE__,bold($this->authModeDigest),"Digest Auth (0: No, 1: Yes): ");
+
$this->__login();
}
+
+ /*! \brief
+ * @param
+ * @return
+ */
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');
-
- DEBUG (DEBUG_RPC, __LINE__, __FUNCTION__, __FILE__,bold($url), "Initiated RPC ");
- DEBUG (DEBUG_RPC, __LINE__, __FUNCTION__, __FILE__,bold($user), "RPC user: ");
- DEBUG (DEBUG_RPC, __LINE__, __FUNCTION__, __FILE__,bold($passwd),"RPC password: ");
-
// Init Curl handler
- $this->curlHandler = curl_init($url);
+ $this->curlHandler = curl_init($this->connectUrl);
// 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_SSL_VERIFYPEER, TRUE);
- curl_setopt($this->curlHandler, CURLOPT_HTTPHEADER , array('Content-Type: application/json'));
+ curl_setopt($this->curlHandler, CURLOPT_URL , $this->connectUrl);
+ 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'));
+ curl_setopt($this->curlHandler, CURLOPT_SSL_VERIFYPEER, FALSE);
// Try to login
- $this->login($user, $passwd);
-
+ if($this->authModeDigest){
+ curl_setopt($this->curlHandler, CURLOPT_USERPWD , "{$this->username}:{$this->userPassword}");
+ curl_setopt($this->curlHandler, CURLOPT_HTTPAUTH , CURLAUTH_ANYSAFE);
+ }else{
+ curl_setopt($this->curlHandler, CURLOPT_COOKIESESSION , TRUE);
+ curl_setopt($this->curlHandler, CURLOPT_COOKIEFILE, 'cookiefile.txt');
+ $this->login($this->username, $this->userPassword);
+ }
}
-
+
+ /*! \brief Returns the last HTTP status code.
+ * @return int The last status code.
+ */
public function getHTTPstatusCode()
{
return((isset($this->lastStats['http_code']))? $this->lastStats['http_code'] : -1 );
}
+
+ /*! \brief Returns the last error string.
+ * @return string The last error message.
+ */
public function get_error()
{
if($this->lastStats['http_code'] != 200){
}
}
+
+ /*! \brief Returns TRUE if the last action was successfull else FALSE.
+ * @return boolean TRUE on success else FALSE.
+ */
public function success()
{
return(curl_errno($this->curlHandler) == 0 && $this->lastStats['http_code'] == 200);
}
+
+ /*! \brief The class destructor, it destroys open rpc handles if needed.
+ */
public function __destruct()
{
if($this->curlHandler){
- curl_close($this->curlHandler);
+ curl_close($this->curlHandler);
}
}
+
+ /*! \brief This is some kind of catch-all method, all unknown method names will
+ * will be interpreted as rpc request.
+ * If you call "$this->blafasel" this method will initiate an rpc request
+ * for method 'blafasel'.
+ * @param string method The rpc method to execute.
+ * @param params array The parameter to use.
+ * @return mixed The request result.
+ */
public function __call($method,$params)
{
// Check if handle is still valid!
if(!$this->curlHandler && $this->lastAction != 'login'){
- $this->__login();
+ $this->__login();
}
// Start request
$response = $this->request($method,$params);
if($this->success()){
DEBUG (DEBUG_RPC, __LINE__, __FUNCTION__, __FILE__,
- (is_array($response['result']))?$response['result']:bold($response['result']), "Result: ");
+ (is_array($response['result']))?$response['result']:bold($response['result']), "Result: ");
}else{
DEBUG (DEBUG_RPC, __LINE__, __FUNCTION__, __FILE__,bold($this->get_error())."<br>".$response, "Result (FAILED): ");
}
-
+ if(isset($response['error']) && !empty($response['error'])) print_a(array($response));
return($response['result']);
}
-
+
+ /*! \brief This method finally initiates the real RPC requests and handles
+ * the result from the server.
+ * @param string method The method to call
+ * @param array params The paramter to use.
+ * @return mixed The server response.
+ */
private function request($method, $params)
{
// Set last action
// 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)) {
// Set current result stats.
$this->lastStats = curl_getinfo($this->curlHandler);
-
+
return($response);
}
-
+
+ /*! \brief Returns the HTTP status message for a given HTTP status code.
+ * @param int code The status to code to return a message for.
+ * @return string The corresponding status message.
+ */
public static function getHttpStatusCodeMessage($code)
{
$codes = array(