Code

Fixed typo
[gosa.git] / gosa-core / include / class_socketClient.inc
index fd71b01f09fb8bd2efc3c6b8cd52b32972074fb7..4442e1c5017729efdbccdfd6513655a279c079e7 100644 (file)
@@ -1,4 +1,24 @@
 <?php
+/*
+ * This code is part of GOsa (http://www.gosa-project.org)
+ * Copyright (C) 2003-2008 GONICUS GmbH
+ *
+ * ID: $$Id$$
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ */
 
 class Socket_Client
 {
@@ -11,6 +31,8 @@ class Socket_Client
   private $handle      = NULL;
   private $bytes_read = 0;
   private $error = "";
+  private $is_error   = FALSE;
+  private $b_encrypt  = FALSE;
 
   /* Crypto information */
   private $td= NULL;
@@ -23,6 +45,7 @@ class Socket_Client
     $this->host= $host;
     $this->port= $port;
     $this->timeout= $timeout;
+    $this->reset_error();
 
     /* Connect if needed */
     if($connect){
@@ -34,33 +57,39 @@ class Socket_Client
   public function setEncryptionKey($key)
   {
     if(!function_exists("mcrypt_get_iv_size")){
-      $this->error = _("The mcrypt module was not found. Please install php5-mcrypt.");
+      $this->set_error(_("The mcrypt module was not found. Please install php5-mcrypt."));
       $this->ckey = "";
-      return FALSE ;
+      $this->b_encrypt = FALSE;
     }
 
     if ($this->connected()){
       $this->ckey = substr(md5($key), 0, $this->ks);
-      return TRUE;
+      $this->b_encrypt = TRUE;
     }
 
-    return FALSE;
+    return($this->b_encrypt);
   }
 
 
   private function encrypt($data)
   {
-    mcrypt_generic_init($this->td, $this->ckey, $this->iv);
-    return base64_encode(mcrypt_generic($this->td, $data));
+    if($this->b_encrypt){
+      mcrypt_generic_init($this->td, $this->ckey, $this->iv);
+      $data = base64_encode(mcrypt_generic($this->td, $data));
+    }
+    return($data);
   }
 
 
   private function decrypt($data)
   {
     /* decrypt data */
-    $data = base64_decode($data);
-    mcrypt_generic_init($this->td, $this->ckey, $this->iv);
-    return mdecrypt_generic($this->td, $data);
+    if($this->b_encrypt && strlen($data)){
+      $data = base64_decode($data);
+      mcrypt_generic_init($this->td, $this->ckey, $this->iv);
+      $data = mdecrypt_generic($this->td, $data);
+    }
+    return($data);
   }
 
 
@@ -72,10 +101,11 @@ class Socket_Client
 
   public function open()
   {
+    $this->reset_error();
     $this->handle = @fsockopen($this->host, $this->port, $this->errno, $this->errstr, $this->timeout);
     if(!$this->handle){
       $this->handle = NULL;
-      $this->error = $this->errstr;
+      $this->set_error(sprintf(_("Socket connection to host '%s:%s' failed: %s"),$this->host,$this->port,$this->errstr));
     }else{
       $this->b_data_send = TRUE;
 
@@ -89,6 +119,26 @@ class Socket_Client
   }
 
 
+  public function set_error($str)
+  {
+    $this->is_error =TRUE;
+    $this->error=$str;
+  }
+
+
+  public function reset_error()
+  {
+    $this->is_error =FALSE;
+    $this->error = "";
+  }
+
+
+  public function is_error()
+  {
+    return($this->is_error);
+  }
+
+
   public function get_error()
   {
     return $this->error;
@@ -111,6 +161,7 @@ class Socket_Client
   public function read()
   {
     // Output the request results
+    $this->reset_error();
     $str = "";
     $data = "test";
     socket_set_timeout($this->handle,$this->timeout);                  
@@ -129,11 +180,15 @@ class Socket_Client
         $str .= $data;
       } else {
         if(strlen($str) != 0){
-          break;
+          
+          /* We got <xml> but </xml> is still missing, keep on reading */
+          if(preg_match("/<\/xml>/",$this->decrypt($str))){
+            break;
+          }
         }
       }
-      if((microtime(TRUE) - $start) > $this->timeout ){      
-        trigger_error(sprintf("Exceeded timeout %f while reading from socket",$this->timeout));
+      if((microtime(TRUE) - $start) > $this->timeout ){     
+        $this->set_error(sprintf(_("Socket timeout of %s seconds reached."),$this->timeout));     
         break;
       }
     }