Code

Fixed si function
[gosa.git] / gosa-core / include / class_gosaSupportDaemon.inc
index 6d6b3b4a2a987b96bfbe0fc071286f080bf2334b..7d8a4f494c94a1a8a041f0909cff547805d6fbcf 100644 (file)
@@ -41,7 +41,7 @@ class gosaSupportDaemon
     @param boolean  Connect Directly connect to daemon socket.
     @param float    Timeout The timelimit for all socket actions.
    */
-  public function __construct($connect=TRUE,$timeout=2)
+  public function __construct($connect=TRUE,$timeout=10)
   {
     #FIXME: bad idea about referencing global variables from within classes
     global $config;
@@ -68,13 +68,18 @@ class gosaSupportDaemon
    */
   public function connect()
   {
-    $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($this->s_encryption_key); 
-      $this->is_connected = TRUE;
+    if(!empty($this->s_host) && !empty($this->i_port)){
+      $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($this->s_encryption_key); 
+        $this->is_connected = TRUE;
+      }else{
+        $this->set_error($this->o_sock->get_error());
+        $this->disconnect();
+        new log("debug","gosaSupportDaemon::connect()", "Cannot connect to si-server", array(),$this->get_error());
+      }
     }else{
-      $this->set_error($this->o_sock->get_error());
-      $this->disconnect();
+      $this->set_error(msgPool::cmdnotfound("GOSA_SI",_("GOsa support daemon")));
     }
     return($this->is_connected);
   }
@@ -129,6 +134,272 @@ class gosaSupportDaemon
   }
 
 
+  public function FAI_get_kernels($release)
+  {
+    $xml_msg = 
+      "<xml>".
+      "<header>gosa_get_available_kernel</header>".
+      "<source>GOSA</source>".
+      "<target>GOSA</target>".
+      "<release>".$release."</release>".
+      "</xml>";
+
+    $ret = array();
+    if($this->connect()){
+      $this->o_sock->write($xml_msg);
+      $str = trim($this->o_sock->read());
+
+      /* Check if something went wrong while reading */
+      if($this->o_sock->is_error()){
+        $this->set_error($this->o_sock->get_error());
+        return($ret);
+      }
+
+      $entries = $this->xml_to_array($str);
+      if(isset($entries['XML']) && is_array($entries['XML'])){
+
+        /* Check if returned values represent a valid answer */
+        if(isset($entries['XML'])){
+          if(isset($entries['XML']['ERROR_STRING'])) {
+            $this->set_error($entries['XML']['ERROR_STRING']);
+            new log("debug","GOsa-si",
+                get_class($this)."::".__FUNCTION__, array(),
+                "FAILED error was ".$this->get_error());
+            return($ret);
+          }
+
+          /* Unset header tags */
+          $ret = $entries['XML'];
+          foreach($ret as $key => $entry){
+            if(!preg_match("/^answer/i",$key)){
+              unset($ret[$key]);
+            }
+          }
+        }
+      }
+    }
+    return($ret);
+  }
+
+
+  public function FAI_get_package_sections($release)
+  {
+    $xml_msg = "<xml><header>gosa_query_packages_list</header><target>GOSA</target><source>GOSA</source>".
+      "<select>distinct section</select>".
+      "<where><clause><phrase><distribution>".$release."</distribution></phrase></clause></where></xml>";
+
+    if($this->connect()){
+      $this->o_sock->write($xml_msg);
+      $str = trim($this->o_sock->read());
+
+      /* Check if something went wrong while reading */
+      if($this->o_sock->is_error()){
+        $this->set_error($this->o_sock->get_error());
+        return($ret);
+      }
+
+      $entries = $this->xml_to_array($str);
+      if(isset($entries['XML']) && is_array($entries['XML'])){
+
+        /* Check if returned values represent a valid answer */
+        if(isset($entries['XML'])){
+          if(isset($entries['XML']['ERROR_STRING'])) {
+            $this->set_error($entries['XML']['ERROR_STRING']);
+            new log("debug","GOsa-si",
+                get_class($this)."::".__FUNCTION__, array(),
+                "FAILED error was ".$this->get_error());
+            return($ret);
+          }
+
+          /* Unset header tags */
+          foreach(array("HEADER","SOURCE","TARGET","SESSION_ID") as $type){
+            if(isset($entries['XML'][$type])){
+              unset($entries['XML'][$type]);
+            }
+          }
+          $ret = $entries['XML'];
+        }
+      }
+    }
+    return($ret);
+  }
+
+
+  public function FAI_get_packages($release,$attrs,$package,$from=-1,$to=-1)
+  {
+    $this->reset_error();
+    $ret = array();
+
+    /* Check Parameter */
+    if(!is_array($attrs) || !count($attrs)){
+      trigger_error("Second parameter must be an array. With at least one attribute name.");
+      return($ret);
+    }
+
+    /* Check Parameter */
+    if(!is_array($package)){
+      trigger_error("Third parameter must be an array. With at least one attribute name.");
+      return($ret);
+    }
+
+    /* Create list of attributes to fetch */
+    $attr = ""; 
+    foreach($attrs as $at){
+      $attr.= "<select>".$at."</select>";
+    }
+
+    /* If no package is given, search for all */
+    if(!count($package)) $package = array("%");
+
+    /* Create limit tag */
+    if($from == -1){
+      $limit =""; 
+    }else{
+      $limit = "<limit><from>".$from."</from><to>".$to."</to></limit>";
+    }
+
+    /* Create list of attributes to fetch */
+    $pkgs = ""; 
+    foreach($package as $pkg){
+      $pkgs .="<phrase><operator>like</operator><package>".$pkg."</package></phrase>";
+    }
+
+    $xml_msg = "<xml><header>gosa_query_packages_list</header><target>GOSA</target><source>GOSA</source>".
+      $attr.
+      "<where>
+      <clause><phrase><distribution>".$release."</distribution></phrase></clause>
+      <clause><connector>OR</connector>
+      ".$pkgs."
+      </clause>
+      </where>".
+      $limit.
+      "</xml>";
+
+    if($this->connect()){
+      $this->o_sock->write($xml_msg);
+      $str = trim($this->o_sock->read());
+
+      /* Check if something went wrong while reading */
+      if($this->o_sock->is_error()){
+        $this->set_error($this->o_sock->get_error());
+        return($ret);
+      }
+
+      $entries = $this->xml_to_array($str);
+      if(isset($entries['XML']) && is_array($entries['XML'])){
+
+        /* Check if returned values represent a valid answer */
+        if(isset($entries['XML'])){
+          if(isset($entries['XML']['ERROR_STRING'])) {
+            $this->set_error($entries['XML']['ERROR_STRING']);
+            new log("debug","GOsa-si",
+                get_class($this)."::".__FUNCTION__, array(),
+                "FAILED error was ".$this->get_error());
+            return($ret);
+          }
+
+          /* Unset header tags */
+          foreach(array("HEADER","SOURCE","TARGET","SESSION_ID") as $type){
+            if(isset($entries['XML'][$type])){
+              unset($entries['XML'][$type]);
+            }
+          }
+          $ret = $entries['XML'];
+        }
+      }
+    }
+    return($ret);
+
+    
+  }
+
+
+  public function FAI_get_server($name = "")
+  {
+    $this->reset_error();
+
+    $xml_msg = "<xml><header>gosa_query_fai_server</header><target>GOSA</target><source>GOSA</source></xml>";
+    $ret = array();
+    if($this->connect()){
+      $this->o_sock->write($xml_msg);
+      $str = trim($this->o_sock->read());
+
+      /* Check if something went wrong while reading */
+      if($this->o_sock->is_error()){
+        $this->set_error($this->o_sock->get_error());
+        return($ret);
+      }
+
+      $entries = $this->xml_to_array($str);
+      if(isset($entries['XML']) && is_array($entries['XML'])){
+
+        /* Check if returned values represent a valid answer */
+        if(isset($entries['XML'])){
+          if(isset($entries['XML']['ERROR_STRING'])) {
+            $this->set_error($entries['XML']['ERROR_STRING']);
+            new log("debug","GOsa-si", 
+              get_class($this)."::".__FUNCTION__, array(),
+              "FAILED error was ".$this->get_error());
+            return($ret);
+          }
+
+          /* Unset header tags */
+          foreach(array("HEADER","SOURCE","TARGET","SESSION_ID") as $type){
+            if(isset($entries['XML'][$type])){
+              unset($entries['XML'][$type]);
+            }
+          }
+          $ret = $entries['XML']; 
+        }
+      }
+    }
+    return($ret);
+  }
+
+
+  public function FAI_get_classes($name)
+  {
+    $this->reset_error();
+    $xml_msg = "<xml><header>gosa_query_fai_release</header><target>GOSA</target><source>GOSA</source>".
+                  "<where><clause><phrase><release>".$name."</release></phrase></clause></where></xml>";;
+    $ret = array();
+    if($this->connect()){
+      $this->o_sock->write($xml_msg);
+      $str = trim($this->o_sock->read());
+
+      /* Check if something went wrong while reading */
+      if($this->o_sock->is_error()){
+        $this->set_error($this->o_sock->get_error());
+        return($ret);
+      }
+
+      $entries = $this->xml_to_array($str);
+      if(isset($entries['XML']) && is_array($entries['XML'])){
+
+        /* Check if returned values represent a valid answer */
+        if(isset($entries['XML'])){
+          if(isset($entries['XML']['ERROR_STRING'])) {
+            $this->set_error($entries['XML']['ERROR_STRING']);
+            new log("debug","GOsa-si", 
+              get_class($this)."::".__FUNCTION__, array($name),
+              "FAILED error was ".$this->get_error());
+            return($ret);
+          }
+
+          /* Unset header tags */
+          foreach(array("HEADER","SOURCE","TARGET","SESSION_ID") as $type){
+            if(isset($entries['XML'][$type])){
+              unset($entries['XML'][$type]);
+            }
+          }
+          $ret = $entries['XML']; 
+        }
+      }
+    }
+    return($ret);
+  }
+
+
   /*! \brief  Returns an array containing all queued entries.
     @return Array All queued entries as an array.
    */
@@ -148,26 +419,34 @@ class gosaSupportDaemon
       $tags = "<where><clause>".$tags."</clause></where>";
     }
 
-    $xml_msg = "<xml>
+    $xml_msg = 
+      "<xml>
       <header>gosa_query_jobdb</header>
       <target>GOSA</target>
       <source>GOSA</source>
       ".$tags."
 
       <orderby>".$sort."</orderby>";
-if($from != -1 && $to != -1){
-$xml_msg.= "
-      <limit>
-       <from>".$from."</from>
-       <to>".$to."</to>
-      </limit>";
-}
-$xml_msg.= "
+    if($from != -1 && $to != -1){
+      $xml_msg.= "
+        <limit>
+        <from>".$from."</from>
+        <to>".$to."</to>
+        </limit>";
+    }
+    $xml_msg.= "
       </xml>";
 
     if($this->connect()){
       $this->o_sock->write($xml_msg);
       $str = trim($this->o_sock->read());
+
+      /* Check if something went wrong while reading */
+      if($this->o_sock->is_error()){
+        $this->set_error($this->o_sock->get_error());
+        return($ret);
+      }
+
       $entries = $this->xml_to_array($str);
       if(isset($entries['XML']) && is_array($entries['XML'])){
 
@@ -183,6 +462,9 @@ $xml_msg.= "
       }
     }
     
+    /* Remove session ID. No one is interested in this... */
+    unset($ret['SESSION_ID']);
+
     return($ret);
   }
 
@@ -221,6 +503,13 @@ $xml_msg.= "
     if($this->connect()){
       $this->o_sock->write($xml_msg);
       $str = trim($this->o_sock->read());
+
+      /* Check if something went wrong while reading */
+      if($this->o_sock->is_error()){
+        $this->set_error($this->o_sock->get_error());
+        return($ret);
+      }
+
       $entries = $this->xml_to_array($str);
       if(isset($entries['XML']) && is_array($entries['XML'])){
         foreach($entries['XML'] as $entry){
@@ -234,6 +523,60 @@ $xml_msg.= "
   }
 
 
+  /*! \brief  Returns an entry containing all requested ids.
+    @param  Array   The IDs of the entries we want to return.
+    @return Array   Of the requested entries. 
+   */
+  public function get_entries_by_mac($macs)
+  {
+    if(!is_array($macs)){
+      trigger_error("Requires an array as parameter.");
+      return;
+    }
+    $this->reset_error();
+
+    $ret = array();
+
+    $xml_msg = "<xml>
+      <header>gosa_query_jobdb</header>
+      <target>GOSA</target>
+      <source>GOSA</source>
+      <where>
+      <clause>
+      <connector>or</connector>";
+    foreach($macs as $mac){
+      $xml_msg .= "<phrase>
+        <operator>eq</operator>
+        <macaddress>".$mac."</macaddress>
+        </phrase>";
+    }
+    $xml_msg .= "</clause>
+      </where>
+      </xml>";
+
+    if($this->connect()){
+      $this->o_sock->write($xml_msg);
+      $str = trim($this->o_sock->read());
+
+      /* Check if something went wrong while reading */
+      if($this->o_sock->is_error()){
+        $this->set_error($this->o_sock->get_error());
+        return($ret);
+      }
+
+      $entries = $this->xml_to_array($str); 
+      if(isset($entries['XML'])){
+        foreach($entries['XML'] as $name => $entry){
+          if(preg_match("/^ANSWER[0-9]*$/",$name)){
+            $ret[$name] = $entry;
+          }
+        }
+      }
+    }
+    return($ret);
+  }
+
+
   /*! \brief  Returns an entry containing all requested ids.
     @param  Array   The IDs of the entries we want to return.
     @return Array   Of the requested entries. 
@@ -268,6 +611,13 @@ $xml_msg.= "
     if($this->connect()){
       $this->o_sock->write($xml_msg);
       $str = trim($this->o_sock->read());
+
+      /* Check if something went wrong while reading */
+      if($this->o_sock->is_error()){
+        $this->set_error($this->o_sock->get_error());
+        return($ret);
+      }
+
       $entries = $this->xml_to_array($str); 
       if(isset($entries['XML'])){
         foreach($entries['XML'] as $name => $entry){
@@ -311,6 +661,13 @@ $xml_msg.= "
     if($this->connect()){
       $this->o_sock->write($xml_msg);
       $str = trim($this->o_sock->read());
+
+      /* Check if something went wrong while reading */
+      if($this->o_sock->is_error()){
+        $this->set_error($this->o_sock->get_error());
+        return(FALSE);
+      }
+
       $entries = $this->xml_to_array($str); 
       if( isset($entries['XML']['HEADER']) && 
           $entries['XML']['HEADER']=="answer" && 
@@ -351,6 +708,13 @@ $xml_msg.= "
     if($this->connect()){
       $this->o_sock->write($xml_msg);
       $str = trim($this->o_sock->read());
+
+      /* Check if something went wrong while reading */
+      if($this->o_sock->is_error()){
+        $this->set_error($this->o_sock->get_error());
+        return($ret);
+      }
+
       $entries = $this->xml_to_array($str); 
       if( isset($entries['XML']['HEADER']) &&
           $entries['XML']['HEADER']=="answer" &&
@@ -397,9 +761,19 @@ $xml_msg.= "
     if($this->connect()){
       $this->o_sock->write($xml_msg);
       $str = $this->o_sock->read();
+
+      /* Check if something went wrong while reading */
+      if($this->o_sock->is_error()){
+        $this->set_error($this->o_sock->get_error());
+        return($ret);
+      }
+
       $entries = $this->xml_to_array($str);
       if(isset($entries['XML']) || isset($entries['COUNT'])){
+        new log("debug","DaemonEvent (IDS) ", "gosaSupportDaemon::remove_entries()", $ids,"SUCCESS");
         return(TRUE);
+      }else{
+        new log("debug","DaemonEvent (IDS) ", "gosaSupportDaemon::remove_entries()", $ids,"FAILED ".$this->get_error());
       }
     }
     return(FALSE);
@@ -413,30 +787,7 @@ $xml_msg.= "
    */
   public function remove_entry($id)
   {
-    $this->reset_error();
-
-    $xml_msg = "<xml>
-      <header>gosa_delete_jobdb_entry</header>
-      <target>GOSA</target>
-      <source>GOSA</source>
-      <where>
-      <clause>
-      <phrase>
-      <operator>eq</operator>
-      <id>".$id."</id>
-      </phrase>
-      </clause>
-      </where>
-      </xml>";
-    if($this->connect()){
-      $this->o_sock->write($xml_msg);
-      $str = $this->o_sock->read();
-      $entries = $this->xml_to_array($str);
-      if(isset($entries['XML'])){
-        return(TRUE);
-      }
-    }
-    return(FALSE);
+    return($this->remove_entries(array($id)));
   }
 
 
@@ -480,7 +831,7 @@ $xml_msg.= "
 
     if(!isset($params['XML'])){
       if (!array_key_exists('XML', $params)){
-        $this->set_error(_("Could not parse XML."));
+        $this->set_error(_("Cannot not parse XML!"));
       }
       $params = array("COUNT" => 0);
     }
@@ -542,12 +893,21 @@ $xml_msg.= "
 
       $this->o_sock->write($xml_msg);
       $str      = trim($this->o_sock->read());
+
+      /* Check if something went wrong while reading */
+      if($this->o_sock->is_error()){
+        $this->set_error($this->o_sock->get_error());
+        return(FALSE);
+      }
+
       $entries = $this->xml_to_array($str);
       if(isset($entries['XML'])){
         if(isset($entries['XML']['ERROR_STRING'])) {
           $this->set_error($entries['XML']['ERROR_STRING']);
+          new log("debug","DaemonEvent (IDS) ", "gosaSupportDaemon::update_entries()", $ids,"FAILED setting (".$attr.") error was ".$this->get_error());
           return(FALSE);
         }
+        new log("debug","DaemonEvent (IDS) ", "gosaSupportDaemon::update_entries()", $ids,"SUCCESS");
         return(TRUE);
       }
     }
@@ -558,13 +918,41 @@ $xml_msg.= "
   /*! \brief  Returns the number of currently queued objects.
       @return Integer  
    */
-  public function number_of_queued_entries()
+  public function number_of_queued_entries($event_types)
   {
+    $tags = "";
+    foreach($event_types as $type){
+      $tags .= "<phrase><headertag>".$type."</headertag></phrase>";
+    }
+    if(count($event_types) > 1){
+      $tags = "<connector>or</connector>".$tags;
+    }
+    if(count($event_types)){
+      $tags = "<where><clause>".$tags."</clause></where>";
+    }
+
+
+    $xml_msg =
+      "<xml>".
+      "<header>gosa_query_jobdb</header>".
+      "<target>GOSA</target>".
+      "<source>GOSA</source>".
+      "<select> count ID</select>".
+      $tags.
+      "</xml>";
+
     $xml_msg ="<xml><header>gosa_count_jobdb</header><target>GOSA</target><source>GOSA</source></xml>";
     $this->connect();
     if($this->connect()){
       $this->o_sock->write($xml_msg);
       $str     = trim($this->o_sock->read());
+
+      /* Check if something went wrong while reading */
+      if($this->o_sock->is_error()){
+        $this->set_error($this->o_sock->get_error());
+        return(0);
+      }
+
       $entries = $this->xml_to_array($str);
       if(isset($entries['XML'])){
         return($entries['XML']['COUNT']);
@@ -581,7 +969,7 @@ $xml_msg.= "
     /* Prepare data */
     foreach ($data as $key => $value){
       if(is_array($value)){
-        foreach($value as $sub_val){
+        foreach($value as $sub_value){
           $xml_message.= "<$key>$sub_value</$key>";
         }
       }else{
@@ -623,7 +1011,6 @@ $xml_msg.= "
       $request_answer = FALSE;
       if($event->get_type() == SCHEDULED_EVENT){
         $action = $event->get_schedule_action();
-        $request_answer = TRUE;
       }elseif($event->get_type() == TRIGGERED_EVENT){
         $action = $event->get_trigger_action();
       }else{
@@ -672,13 +1059,25 @@ $xml_msg.= "
       $this->o_sock->write($data);
       if ($answer_expected){
         $str = trim($this->o_sock->read());
+
+        /* Check if something went wrong while reading */
+        if($this->o_sock->is_error()){
+          $this->set_error($this->o_sock->get_error());
+          return($ret);
+        }
+
         $entries = $this->xml_to_array($str);
         if(isset($entries['XML']) && is_array($entries['XML'])){
           $ret = $entries;
           if(isset($entries['XML']['ERROR_STRING'])) {
             $this->set_error($entries['XML']['ERROR_STRING']);
+            new log("debug","DaemonEvent (IDS) ", "gosaSupportDaemon::_send()", array($data=>$data),"FAILED ".$this->get_error());
+          }else{
+            new log("debug","DaemonEvent (IDS) ", "gosaSupportDaemon::_send()", array($data=>$data),"SUCCESS");
           }
         }
+      }else{
+        new log("debug","DaemonEvent (IDS) ", "gosaSupportDaemon::_send()", array($data=>$data),"Fire & forget, not result.! ".$this->get_error());
       }
     }
     return($ret);
@@ -730,22 +1129,9 @@ $xml_msg.= "
     /* First of all we have to check which jobs are startet 
      *  for $mac 
      */
-    $xml_msg ="
-      <xml>
-       <header>gosa_query_jobdb</header>
-       <target>GOSA</target>
-       <source>GOSA</source>
-       <where>
-        <clause>
-         <phrase>
-          <macaddress>".$mac."</macaddress>
-         </phrase>
-        </clause>
-       </where>
-      </xml>
-    ";  
+    $xml_msg ="<xml><header>gosa_query_jobdb</header><target>GOSA</target><source>GOSA</source><where><clause><phrase><macaddress>".$mac."</macaddress></phrase></clause></where></xml>";  
     
-    new log("debug","gosaSupportDaemon::clean_queue_from_mac", "Removing all entries for mac ".$mac."", array(),"follows next.");
+    new log("debug","DaemonEvent ", "gosaSupportDaemon::clean_queue_from_mac()", array($mac => $mac)," start cleaning.");
  
     $data = $this->_send($xml_msg,TRUE);
     if(is_array($data) && isset($data['XML'])){
@@ -767,12 +1153,12 @@ $xml_msg.= "
                 $tmp->add_targets(array($mac));
                 $tmp->set_type(TRIGGERED_EVENT);
                 if(!$this->append($tmp)){
-                  msg_dialog::display(_("Error"), sprintf(_("Cannot send abort event for entry: %s"),$entry['ID']) , ERROR_DIALOG);
-                  new log("debug","gosaSupportDaemon::clean_queue_from_mac", 
-                      "Sending 'DaemonEvent_faireboot' for ID '".$entry['ID']."' FAILED!",array(), "FAILED");
+                  msg_dialog::display(_("Error"), sprintf(_("Cannot send abort event for entry %s!"),$entry['ID']) , ERROR_DIALOG);
+                  new log("debug","DaemonEvent ", "gosaSupportDaemon::clean_queue_from_mac()", array($mac => $mac),
+                      "FAILED, could not send 'DaemonEvent_faireboot' for entry ID (".$entry['ID'].") - ".$this->get_error());
                 }else{
-                  new log("debug","gosaSupportDaemon::clean_queue_from_mac", 
-                      "Sending 'DaemonEvent_faireboot' for ID '".$entry['ID']."' successfull",array(), "SUCCESS");
+                  new log("debug","DaemonEvent ", "gosaSupportDaemon::clean_queue_from_mac()", array($mac => $mac),
+                      "SUCCESS, send 'DaemonEvent_faireboot' for entry ID (".$entry['ID'].")");
                 }
                 ;break;
               }else{
@@ -787,12 +1173,7 @@ $xml_msg.= "
                *  Failed or waiting events, can be removed without any trouble.
                */ 
               if(!$this->remove_entries(array($entry['ID']))){
-                msg_dialog::display(_("Error"), sprintf(_("Cannot remove entry: %s"),$entry['ID']) , ERROR_DIALOG);
-                new log("debug","gosaSupportDaemon::clean_queue_from_mac", 
-                    "Removing entry ID '".$entry['ID']."' FAILED!",array(), "FAILED");
-              }else{
-                new log("debug","gosaSupportDaemon::clean_queue_from_mac",
-                    "Removing entry ID '".$entry['ID']."' successfull!",array(),"SUCCESS");
+                msg_dialog::display(_("Error"), sprintf(_("Cannot remove entry %s!"),$entry['ID']) , ERROR_DIALOG);
               }
               ;break;
           }