Code

Updated error handling
[gosa.git] / gosa-core / include / class_jsonRPC.inc
index 12bb2af727e989d6c9d9579cd3dd5580c14952e5..bf82d971cda725228084967a57319576340840a3 100644 (file)
@@ -7,6 +7,7 @@ class jsonRPC {
     private $config;
     private $id;
     private $lastStats = array();
+    private $lastResult = array();
     private $lastAction = "none";
 
 
@@ -16,6 +17,34 @@ class jsonRPC {
     private $authModeDigest = FALSE; 
 
 
+    /*! \brief  This function is used by the property editor and checks the 
+     *           given rpc connection informations.
+     */
+    public static function testConnectionProperties($message,$class,$name,$value, $type)
+    {
+        global $config;
+
+        // Get currently used connection usernamem and password.
+        // We use the temporary values, due to the fact, that we do not want to test
+        //  the current values, we want to test the modified values.
+        $user = $config->configRegistry->getProperty('core','gosaRpcUser');
+        $username =  $user->getValue($temporaryValue = TRUE);
+        $passwd = $config->configRegistry->getProperty('core','gosaRpcPassword');
+        $passwdString =  $passwd->getValue($temporaryValue = TRUE);
+
+        $connection = new jsonRPC($config, $value, $username, $passwdString);        
+        if(!$connection->success() && $message){
+            msg_dialog::display(_("Warning"),
+                    sprintf(_("The rpc connection (%s) specified for '%s:%s' is invalid! Error was: %s."),
+                        bold($value),bold($class),bold($name), bold($connection->get_error())),
+                    WARNING_DIALOG);
+
+        }
+        
+        return($connection->success());
+    }
+
+
     /*! \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)
@@ -64,12 +93,14 @@ class jsonRPC {
 
         // Try to login 
         if($this->authModeDigest){
-            curl_setopt($this->curlHandler, CURLOPT_USERPWD , "{$this->username}:{$this->userPassword}");
+            if(!empty($this->username)) 
+                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);
+            if(!empty($this->username)) 
+                $this->login($this->username, $this->userPassword);
         }
     }
 
@@ -89,12 +120,21 @@ class jsonRPC {
     public function get_error()
     {
         if($this->lastStats['http_code'] != 200){
-            return($this->getHttpStatusCodeMessage($this->lastStats['http_code']));
+            $error = $this->getHttpStatusCodeMessage($this->lastStats['http_code']);
+            if(isset($this->lastResult['error']['error']) && is_array($this->lastResult['error']['error'])){
+                $err = $this->lastResult['error']['error'];
+                $message = call_user_func_array(sprintf,$err);
+                $error .= $message;
+            }elseif(isset($this->lastResult['error']['message'])){
+                $error .= ": ".$this->lastResult['error']['message']; 
+            }
+            return($error);
         }else{
             return(curl_error($this->curlHandler));
         }
     }
 
+    
 
     /*! \brief      Returns TRUE if the last action was successfull else FALSE.
      *  @return     boolean     TRUE on success else FALSE. 
@@ -139,11 +179,59 @@ class jsonRPC {
         }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']);
+
+        global $config;
+        $debugLevel = $config->get_cfg_value('core', 'debugLevel'); 
+        if($debugLevel & DEBUG_RPC){
+            print_a(array('CALLED:' => array($method => $params)));
+            print_a(array('RESPONSE' => $response));
+        }
+        $return = $response['result'];
+        
+        // Inspect the result and replace predefined statements with their 
+        //  coresponding classes.
+        $return = $this->inspectJsonResult($return);
+
+        return($return);
     }
 
 
+
+    public function inspectJsonResult($result)
+    {
+        // Check for remove objects we've to create
+        if(isset($result['__jsonclass__']) && class_available('remoteObject')){
+
+            // Get all relevant class informations
+            $classDef = $result['__jsonclass__'][1];
+            $type = $classDef[0];
+            $ref_id = $classDef[1];
+            $object_id = $classDef[2];
+            $methods = $classDef[3];
+            $properties = $classDef[4];
+
+            // Prepare values
+            $values = array();
+            foreach($properties as $prop){
+                $values[$prop] = NULL;
+                if(isset($res[$prop])) $values[$prop] = $res[$prop];
+            }
+
+            // Build up remote object
+            $object = new remoteObject($rpc, $type, $properties, $values, $methods, $object_id, $ref_id);
+            return($object);
+        }
+        return($result);
+    }
+
+
+
+
+
+
+
+
+
     /*! \brief      This method finally initiates the real RPC requests and handles 
      *               the result from the server.
      *  @param      string  method      The method to call 
@@ -177,6 +265,7 @@ class jsonRPC {
 
         // Set current result stats.
         $this->lastStats = curl_getinfo($this->curlHandler);
+        $this->lastResult = $response;
 
         return($response);
     }