Code

Updated get_sub_list.
[gosa.git] / gosa-core / include / class_gosaSupportDaemon.inc
index 944e1fa2d8778888dc088f5b934a094f706b56f1..a657419d6303eee9825965a2763d44630f13bd5a 100644 (file)
@@ -41,9 +41,9 @@ Function overview:
 
 class gosaSupportDaemon
 {
-  static private $s_host       = "";
-  static private $i_port       = 0;
-  static private $s_encryption_key = "";
+  private $s_host       = "";
+  private $i_port       = 0;
+  private $s_encryption_key = "";
 
   private $o_sock       = NULL;
   private $f_timeout    = 2;
@@ -68,14 +68,10 @@ class gosaSupportDaemon
     # load from config, store statically
     if (isset($config->current['GOSA_SI'])){
 
-      if (gosaSupportDaemon::$s_host == ""){
-        $host= preg_replace("/^.*@([^:]+):.*$/", "$1", $config->current['GOSA_SI']);
-        $port= preg_replace("/^.*@[^:]+:(.*)$/", "$1", $config->current['GOSA_SI']);
-        $key = preg_replace("/^(.*)@[^:]+:.*$/", "$1", $config->current['GOSA_SI']);
-        
-        gosaSupportDaemon::$s_host           = $host;
-        gosaSupportDaemon::$i_port           = $port;
-        gosaSupportDaemon::$s_encryption_key = $key;
+      if ($this->s_host == ""){
+        $this->s_host= preg_replace("/^.*@([^:]+):.*$/", "$1", $config->current['GOSA_SI']);
+        $this->i_port= preg_replace("/^.*@[^:]+:(.*)$/", "$1", $config->current['GOSA_SI']);
+        $this->s_encryption_key = preg_replace("/^(.*)@[^:]+:.*$/", "$1", $config->current['GOSA_SI']);
       }
 
       $this->f_timeout = $timeout;
@@ -91,12 +87,13 @@ class gosaSupportDaemon
    */
   public function connect()
   {
-    $this->o_sock = new Socket_Client(gosaSupportDaemon::$s_host,gosaSupportDaemon::$i_port,TRUE,$this->f_timeout);
+    $this->o_sock = new Socket_Client($this->s_host,$this->i_port,TRUE,$this->f_timeout);
     if($this->o_sock->connected()){ 
-      $this->o_sock->setEncryptionKey(gosaSupportDaemon::$s_encryption_key); 
+      $this->o_sock->setEncryptionKey($this->s_encryption_key); 
       $this->is_connected = TRUE;
     }else{
-      $this->error = $this->o_sock->get_error();
+      $this->s_error = $this->o_sock->get_error();
+      $this->b_error = TRUE;
       $this->disconnect();
     }
     return($this->is_connected);
@@ -143,7 +140,7 @@ class gosaSupportDaemon
   /*! \brief  Returns an array containing all queued entries.
     @return Array All queued entries as an array.
    */
-  public function get_queued_entries($from=0,$to=10,$sort="timestamp DESC")
+  public function get_queued_entries($from=-1,$to=-1,$sort="timestamp DESC")
   {
     $this->b_error = FALSE;
     $this->s_error = "";
@@ -159,11 +156,15 @@ class gosaSupportDaemon
         </phrase>
       </clause>
       </where>
-      <orderby>".$sort."</orderby>
+      <orderby>".$sort."</orderby>";
+if($from != -1 && $to != -1){
+$xml_msg.= "
       <limit>
-      <from>".$from."</from>
-      <to>".$to."</to>
-      </limit>
+       <from>".$from."</from>
+       <to>".$to."</to>
+      </limit>";
+}
+$xml_msg.= "
       </xml>";
 
     if($this->connect()){
@@ -171,12 +172,23 @@ class gosaSupportDaemon
       $str = trim($this->o_sock->read());
       $entries = $this->xml_to_array($str);
       if(isset($entries['XML']) && is_array($entries['XML'])){
-        $ret = $entries; 
+
+        /* Check if returned values represent a valid answer */
+        if($entries['XML']['HEADER'] == "answer"){
+          
+          /* Unset header tags */
+          foreach(array("HEADER","SOURCE","TARGET") as $type){
+            unset($entries['XML'][$type]);
+          }
+          $ret = $entries['XML']; 
+        }
       }
     }
+    
     return($ret);
   }
 
+
   /*! \brief  Checks if the given ids are used queue ids.
     @param  Array   The ids we want to check..
     @return Array   An array containing all ids as index and TRUE/FALSE as value. 
@@ -213,7 +225,9 @@ class gosaSupportDaemon
       $entries = $this->xml_to_array($str);
       if(isset($entries['XML']) && is_array($entries['XML'])){
         foreach($entries['XML'] as $entry){
-          $ret[] = $entry['ID'];
+          if(isset($entry['ID'])){
+            $ret[] = $entry['ID'];
+          }
         }
       }
     }
@@ -246,7 +260,6 @@ class gosaSupportDaemon
         <operator>eq</operator>
         <id>".$id."</id>
         </phrase>";
-      $ret[$id] = FALSE;
     }
     $xml_msg .= "</clause>
       </where>
@@ -257,7 +270,11 @@ class gosaSupportDaemon
       $str = trim($this->o_sock->read());
       $entries = $this->xml_to_array($str); 
       if(isset($entries['XML'])){
-        $ret = $entries['XML'];
+        foreach($entries['XML'] as $name => $entry){
+          if(preg_match("/^ANSWER[0-9]*$/",$name)){
+            $ret[$name] = $entry;
+          }
+        }
       }
     }
     return($ret);
@@ -293,7 +310,9 @@ class gosaSupportDaemon
       $this->o_sock->write($xml_msg);
       $str = trim($this->o_sock->read());
       $entries = $this->xml_to_array($str); 
-      if(isset($entries['XML']['ANSWER1'])){
+      if( isset($entries['XML']['HEADER']) && 
+          $entries['XML']['HEADER']=="answer" && 
+          isset($entries['XML']['ANSWER1'])){
         return(TRUE);
       }
     }
@@ -330,7 +349,9 @@ class gosaSupportDaemon
       $this->o_sock->write($xml_msg);
       $str = trim($this->o_sock->read());
       $entries = $this->xml_to_array($str); 
-      if(isset($entries['XML']['ANSWER1'])){
+      if( isset($entries['XML']['HEADER']) &&
+          $entries['XML']['HEADER']=="answer" &&
+          isset($entries['XML']['ANSWER1'])){
         $ret = $entries['XML']['ANSWER1'];
       }
     }
@@ -452,10 +473,14 @@ class gosaSupportDaemon
         }
       }
     }
+
     if(!isset($params['XML'])){
-      $this->set_error(_("Could not parse XML."));
-      $params = array();
+      if (!array_key_exists('XML', $params)){
+        $this->set_error(_("Could not parse XML."));
+      }
+      $params = array("COUNT" => 0);
     }
+
     return($params); 
   }
 
@@ -534,7 +559,7 @@ class gosaSupportDaemon
   /*! \brief  Returns an array containing all queued entries.
     @return Array All queued entries as an array.
    */
-  public function send($data, $answer_expected= FALSE)
+  public function _send($data, $answer_expected= FALSE)
   {
     $this->b_error = FALSE;
     $this->s_error = "";
@@ -542,17 +567,46 @@ class gosaSupportDaemon
 
     if($this->connect()){
       $this->o_sock->write($data);
-      if ($answer_exepcted){
-       $str = trim($this->o_sock->read());
-       $entries = $this->xml_to_array($str);
-       if(isset($entries['XML']) && is_array($entries['XML'])){
-         $ret = $entries; 
-       }
+      if ($answer_expected){
+        $str = trim($this->o_sock->read());
+        $entries = $this->xml_to_array($str);
+        if(isset($entries['XML']) && is_array($entries['XML'])){
+          $ret = $entries; 
+        }
       }
     }
     return($ret);
   }
 
+
+  static function send($header, $to, $data= array(), $answer_expected = FALSE)
+  {
+    $xml_message= "";
+
+    /* Get communication object */
+    $d= new gosaSupportDaemon(TRUE,10);
+
+    /* Prepare data */
+    foreach ($data as $key => $value){
+      $xml_message.= "<$key>$value</$key>";
+    }
+
+    return $d->_send("<xml><header>$header</header><source>GOSA</source><target>$to</target>".$xml_message."</xml>",$answer_expected);
+  }
+
+
+  static function ping($target)
+  {
+    if (tests::is_mac($target)){
+    /* Get communication object */
+       $d= new gosaSupportDaemon(TRUE,0.5);
+       $answer= $d->_send("<xml><header>gosa_ping</header><source>GOSA</source><target>$target</target></xml>", TRUE);
+       return (count($answer) ? TRUE:FALSE);
+    }
+
+    return (FALSE);
+  }
+
 }
 
 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler: