Code

renamed opsi function
[gosa.git] / gosa-plugins / opsi / admin / opsi / class_opsi.inc
index 7be570879b615fb3d0d59d15014f40fe9c2ea8b8..a60ce9085464a6a735a91787dbcceec0a2f2e345 100644 (file)
@@ -44,6 +44,7 @@
 class opsi extends gosaSupportDaemon 
 {
   private $config = NULL;
+  protected $use_alternative_xml_parse_method = TRUE;
 
   /*! \brief           Create opsi object.
     @param
@@ -56,6 +57,12 @@ class opsi extends gosaSupportDaemon
     $this->target = "00:01:6c:9d:b9:fa";
   }
 
+  
+  public function enabled()
+  {
+    return(TRUE);
+  }
+
   /******************
     Opsi handling 
    ******************/
@@ -63,54 +70,136 @@ class opsi extends gosaSupportDaemon
   function get_hosts_for_system_management()
   {
     $res = $this->list_clients();
-
     $data = array();
     foreach($res as $entry){
+      if(!isset($entry['MAC'][0]['VALUE'])) $entry['MAC'][0]['VALUE'] = "";
       $data[] = array(
-        "dn"          => "opsi:=".$entry['NAME'].",".get_ou("winstations").$this->config->current['BASE'],
+        "dn"          => "opsi:=".$entry['NAME'][0]['VALUE'].",".get_ou("winstations").$this->config->current['BASE'],
         "objectClass" => array("gosa_opsi_client"),
-        "cn"          => array(0 => $entry['NAME']),
-        "description" => array(0 => $entry['DESCRIPTION']),
-        "macAddress"  => array(0 => $entry['MAC']),
-        "opsi_notes"  => array(0 => $entry['NOTES']));
-        
+        "cn"          => array(0 => $entry['NAME'][0]['VALUE']),
+        "description" => array(0 => $entry['DESCRIPTION'][0]['VALUE']),
+        "macAddress"  => array(0 => $entry['MAC'][0]['VALUE']),
+        "opsi_notes"  => array(0 => $entry['NOTES'][0]['VALUE']));
     }
-  
     return($data);
   }
 
 
+  /*! \brief  Maps all xml to array conversion to an alternative method
+                then used in the parent class 'gosaSupportDaemon'.
+              The alternative method is able to handle more complex data.
+   */
+  private function xml_to_array($xml,$alternative_method = FALSE)
+  {
+    return(gosaSupportDaemon::xml_to_array($xml,TRUE));
+  }
+
+
+  /*! \brief  Trigger an event like wake or install for a specific hostId. 
+   */
+  public function send_action($type,$hostId,$mac)
+  {
+    switch($type){
+      case 'install'  :  $this->job_opsi_install_client($hostId,$mac); break;
+      default         :  trigger_error('Unknown type '.$type.'.');
+    }
+  }
+
+
   /******************
     SI Communication functions
    ******************/
 
 
+
+  /*! \brief           Returns a list of all opsi clients.
+    @param
+    @return            
+   */
+  public function list_clients( $hostId = "")
+  {
+    $data   = array();
+    $res    = $this->send_data("gosa_opsi_list_clients",$this->target,$data,TRUE);
+    $items  = array();
+    if(isset($res['XML'][0]['ITEM'])){
+      $items = $res['XML'][0]['ITEM'];
+    }
+    return($items);
+  }
+
+
+  /*! \brief           Adds a new opsi client.
+    @param
+    @return            
+   */
+  public function add_client($hostId,$macaddress,$notes,$description)
+  {
+    $data = array("hostId" => $hostId,"macaddress" => $macaddress);
+
+    if(empty($hostId)){
+      trigger_error("No valid host id given, check parameter 1.");
+      return;
+    }
+  
+    /* Add optional attributes */ 
+    foreach(array("notes","description") as $attr) {
+      if(!empty($$attr)){
+        $data[$attr] = $$attr;
+      }
+    }
+
+    /* Query SI server */
+    $res    = $this->send_data("gosa_opsi_add_client",$this->target,$data,TRUE);
+  }
+
+
+  /*! \brief           Modify an opsi client.
+    @param
+    @return            
+   */
+  public function modify_client($hostId,$mac,$notes,$description)
+  {
+    $data = array("hostId" => $hostId,"mac" => $mac);
+
+    if(empty($hostId)){
+      trigger_error("No valid host id given, check parameter 1.");
+      return;
+    }
+  
+    /* Add optional attributes */ 
+    foreach(array("notes","description") as $attr) {
+      if(!empty($$attr)){
+        $data[$attr] = $$attr;
+      }
+    }
+
+    /* Query SI server */
+    $res = $this->send_data("gosa_opsi_modify_client",$this->target,$data,TRUE);
+  }
+
+
+
   /*! \brief           Returns a list of netboot products.
     @param
     @return            
    */
   public function get_netboot_products($host = "")
   {
-    $data = array();
-
     /* Append host attribute to query data 
      */
+    $data = array();
     if(!empty($host)){
       $data['hostId'] = trim($host);
     }
 
     $res    = $this->send_data("gosa_opsi_get_netboot_products",$this->target,$data,TRUE);
-    $items  = array();
-    if($res['XML']['ITEM']['PRODUCTID']){
-      if(!is_array($res['XML']['ITEM']['PRODUCTID'])){
-        $items[$res['XML']['ITEM']['PRODUCTID']]['NAME'] = $res['XML']['ITEM']['PRODUCTID'];
-        $items[$res['XML']['ITEM']['PRODUCTID']]['DESC'] = $res['XML']['ITEM']['DESCRIPTION'];
-      }else{
-        foreach($res['XML']['ITEM']['PRODUCTID'] as $id => $name){
-          $items[$name]['NAME'] = $name;
-          $items[$name]['DESC'] = $res['XML']['ITEM']['DESCRIPTION'][$id];
-        }
-      } 
+    $items = array();
+    if(isset($res['XML'][0]['ITEM'])){
+      foreach($res['XML'][0]['ITEM'] as $entry){
+        $e = array("DESC" => $entry['DESCRIPTION'][0]['VALUE'],
+                   "NAME" => $entry['PRODUCTID'][0]['VALUE']);
+        $items[$entry['PRODUCTID'][0]['VALUE']] = $e;
+      }
     }
     return($items);
   }
@@ -122,26 +211,21 @@ class opsi extends gosaSupportDaemon
    */
   public function get_local_products($host = "")
   {
-    $data = array();
-
     /* Append host attribute to query data 
      */
+    $data = array();
     if(!empty($host)){
       $data['hostId'] = trim($host);
     }
 
     $res    = $this->send_data("gosa_opsi_get_local_products",$this->target,$data,TRUE);
-    $items  = array();
-    if($res['XML']['ITEM']['PRODUCTID']){
-      if(!is_array($res['XML']['ITEM']['PRODUCTID'])){
-        $items[$res['XML']['ITEM']['PRODUCTID']]['NAME'] = $res['XML']['ITEM']['PRODUCTID'];
-        $items[$res['XML']['ITEM']['PRODUCTID']]['DESC'] = $res['XML']['ITEM']['DESCRIPTION'];
-      }else{
-        foreach($res['XML']['ITEM']['PRODUCTID'] as $id => $name){
-          $items[$name]['NAME'] = $name;
-          $items[$name]['DESC'] = $res['XML']['ITEM']['DESCRIPTION'][$id];
-        }
-      } 
+    $items = array();
+    if(isset($res['XML'][0]['ITEM'])){
+      foreach($res['XML'][0]['ITEM'] as $entry){
+        $e = array("DESC" => $entry['DESCRIPTION'][0]['VALUE'],
+                   "NAME" => $entry['PRODUCTID'][0]['VALUE']);
+        $items[$entry['PRODUCTID'][0]['VALUE']] = $e; 
+      }
     }
     return($items);
   }
@@ -171,10 +255,15 @@ class opsi extends gosaSupportDaemon
 
     /* Query SI server */
     $res    = $this->send_data("gosa_opsi_get_product_properties",$this->target,$data,TRUE);
-    if(isset($res['XML']['ITEM'])){   
-      return($res['XML']['ITEM']);
+    $items  = array();
+    if(isset($res['XML'][0]['ITEM'])){   
+      foreach($res['XML'][0]['ITEM'] as $entry){
+        foreach($entry as $name => $val){
+          $items[$name] = $val['0']['VALUE'];
+        }
+      }
     }
-    return(array());
+    return($items);
   }
 
 
@@ -182,176 +271,167 @@ class opsi extends gosaSupportDaemon
     @param
     @return            
    */
-  public function set_product_properties()
+  public function set_product_properties($productId,$cfg,$hostId = "")
   {
-    /*         <xml> 
-        <header>gosa_opsi_set_product_properties</header> 
-        <source>GOSA</source> 
-        <target>00:01:6c:9d:b9:fa</target> 
-        <productId>firefox</productId> 
-        <item>
-        <name>askbeforeinst</name>
-        <value>false</value>
-        </item>
-        </xml>
-        <xml> 
-        <header>gosa_opsi_set_product_properties</header> 
-        <source>GOSA</source> 
-        <target>00:01:6c:9d:b9:fa</target> 
-        <hostId>limux-cl-2.intranet.gonicus.de</hostId> 
-        <productId>firefox</productId> 
-        <item> 
-        <name>askbeforeinst</name> 
-        <value>false</value> 
-        </item> 
-        </xml> 
+    $data = array("productId" => $productId);
+
+    /* Append host attribute to query data 
      */
-  }
+    if(!empty($hostId)){
+      $data['hostId'] = trim($hostId);
+    }
+   
+    /* Check parameter */ 
+    if(empty($productId)){
+      trigger_error("No valid product id given, check parameter 1.");
+      return(array());
+    }
 
+    if(!count($cfg)) return;
+    
+    /* Add properties */
+    $data['item'] = array();
+    foreach($cfg as $name => $value){
+      $data['item'][] = "<name>".$name."</name><value>".$value."</value>";
+    }  
 
-  /*! \brief           Returns the clients hardware setup.
-    @param
-    @return            
-   */
-  public function get_client_hardware()
-  {
-    /* <xml> 
-        <header>gosa_opsi_get_client_hardware</header> 
-        <source>GOSA</source> 
-        <target>GOSA</target> 
-        <hostId>limux-cl-2.intranet.gonicus.de</hostId> 
-        </xml> 
-     */
+    /* Query SI server */
+    $res    = $this->send_data("gosa_opsi_set_product_properties",$this->target,$data,TRUE);
   }
 
 
-  /*! \brief           Returns the clients software setup.
+  /*! \brief           Adds a given product to a client.
     @param
     @return            
    */
-  public function get_client_software()
+  public function add_product_to_client($productId,$hostId)
   {
-    /*  <xml> 
-        <header>gosa_opsi_get_client_software</header> 
-        <source>GOSA</source> 
-        <target>GOSA</target> 
-        <hostId>limux-cl-2.intranet.gonicus.de</hostId> 
-        </xml> i
-     */
+    $data = array("productId" => $productId,"hostId" => $hostId);
+
+    /* Check parameter */ 
+    if(empty($productId)){
+      trigger_error("No valid product id given, check parameter 1.");
+      return;
+    }
+    if(empty($hostId)){
+      trigger_error("No valid host id given, check parameter 2.");
+      return;
+    }
+
+    /* Query SI server */
+    $res    = $this->send_data("gosa_opsi_add_product_to_client",$this->target,$data,TRUE);
   }
 
 
-  /*! \brief           Returns a list of all opsi clients.
+  /*! \brief      Removes a given product from a client.
     @param
-    @return            
+    @return
    */
-  public function list_clients()
+  public function del_product_from_client($productId,$hostId)
   {
-    $data   = array();
-    $res    = $this->send_data("gosa_opsi_list_clients",$this->target,$data,TRUE);
-    $items  = array();
-    if(isset($res['XML']['ITEM'])){
-      if(!is_array($res['XML']['ITEM']['NAME'])){
-        $obj = $res['XML']['ITEM'];
-        $items[$obj['NAME']] = $obj;
-      }else{
-        foreach($res['XML']['ITEM'] as $type => $val){
-          foreach($val as $key => $value){
-            $items[$key][$type] = $value;
-          }
-        }
-      }
+    $data = array("productId" => $productId,"hostId" => $hostId);
+
+    /* Check parameter */ 
+    if(empty($productId)){
+      trigger_error("No valid product id given, check parameter 1.");
+      return;
     }
-    return($items);
+    if(empty($hostId)){
+      trigger_error("No valid host id given, check parameter 2.");
+      return;
+    }
+
+    /* Query SI server */
+    $res    = $this->send_data("gosa_opsi_del_product_from_client",$this->target,$data,TRUE);
   }
 
 
-  /*! \brief           Deletes the given opsi client.
+  /*! \brief           Returns the clients hardware setup.
     @param
     @return            
    */
-  public function del_client()
+  public function get_client_hardware($hostId)
   {
-    /*  <xml> 
-        <header>gosa_opsi_del_client</header> 
-        <source>GOSA</source> 
-        <target>00:01:6c:9d:b9:fa</target> 
-        <hostId>limux-cl-2.intranet.gonicus.de</hostId>
-        </xml>
-     */
+    $data = array("hostId" => $hostId);
+
+    /* Check parameter */ 
+    if(empty($hostId)){
+      trigger_error("No valid host id given, check parameter 1.");
+      return;
+    }
+
+    /* Query SI server */
+    $res    = $this->send_data("gosa_opsi_get_client_hardware",$this->target,$data,TRUE);
+    if(isset($res['XML'][0]['ITEM'])){
+      return($res['XML'][0]['ITEM']);
+    }
+    return(array());
   }
 
 
-  /*! \brief           Triggers install/reinstall of an opsi client.
+  /*! \brief           Returns the clients software setup.
     @param
     @return            
    */
-  public function job_opsi_install_client()
+  public function get_client_software($hostId)
   {
-    /*  <xml> 
-        <header>job_opsi_install_client</header> 
-        <source>GOSA</source> 
-        <target>00:01:6c:9d:b9:fa</target> 
-        <hostId>limux-cl-2.intranet.gonicus.de</hostId> 
-        <macaddress>00:11:25:4b:8c:e5</macaddress> 
-        </xml>
-     */
+    $data = array("hostId" => $hostId);
+
+    /* Check parameter */ 
+    if(empty($hostId)){
+      trigger_error("No valid host id given, check parameter 1.");
+      return;
+    }
+
+    /* Query SI server */
+    $res    = $this->send_data("gosa_opsi_get_client_software",$this->target,$data,TRUE);
+    if(isset($res['XML'][0]['ITEM'])){
+      return($res['XML'][0]['ITEM']);
+    }
+    return(array());
   }
 
 
-  /*! \brief           Adds a new opsi client.
+
+  /*! \brief           Deletes the given opsi client.
     @param
     @return            
    */
-  public function add_client()
+  public function del_client($hostId)
   {
-    /*  <xml> 
-        <header>gosa_opsi_add_client</header> 
-        <source>GOSA</source> 
-        <target>00:01:6c:9d:b9:fa</target> 
-        <hostId>limux-cl-2.intranet.gonicus.de</hostId> 
-        <macaddress>00:11:25:4b:8c:e5</macaddress> 
-        <description>Test halt</description> 
-        <ip>1.2.3.4</ip> 
-        <notes>Im a note</notes> 
-        </xml>
-     */
+    $data = array("hostId" => $hostId);
+
+    /* Check parameter */ 
+    if(empty($hostId)){
+      trigger_error("No valid host id given, check parameter 1.");
+      return;
+    }
+
+    /* Query SI server */
+    $res    = $this->send_data("gosa_opsi_del_client",$this->target,$data,TRUE);
+    if(isset($res['XML'][0]['ITEM'])){
+      return($res['XML'][0]['ITEM']);
+    }
+    return(array());
   }
 
 
-  /*! \brief           Adds a given product to a client.
+  /*! \brief           Triggers install/reinstall of an opsi client.
     @param
     @return            
    */
-  public function add_product_to_client()
+  public function job_opsi_install_client($hostId,$mac)
   {
-    /*  <xml> 
-        <header>gosa_opsi_add_product_to_client</header> 
-        <source>GOSA</source> 
-        <target>00:01:6c:9d:b9:fa</target> 
-        <macaddress>00:11:25:4b:8c:e5</macaddress> 
-        <hostId>limux-cl-2.intranet.gonicus.de</hostId> 
-        <productId>winxppro</productId> 
-        </xml>
-     */
-  }
+    $data = array("hostId" => $hostId,"macaddress"=>$mac);
 
+    /* Check parameter */ 
+    if(empty($hostId)){
+      trigger_error("No valid host id given, check parameter 1.");
+      return;
+    }
 
-  /*! \brief      Removes a given product from a client.
-    @param
-    @return
-   */
-  public function del_product_from_client()
-  {
-    /* <xml> 
-       <header>gosa_opsi_del_product_from_client</header> 
-       <source>GOSA</source> 
-       <target>00:01:6c:9d:b9:fa</target> 
-       <hostId>limux-cl-1.intranet.gonicus.de</hostId> 
-       <macaddress>00:11:25:4b:8c:e5</macaddress> 
-       <productId>softprod</productId>  
-       </xml>
-     */
+    /* Query SI server */
+    $this->send_data("job_opsi_install_client",$this->target,$data,TRUE);
   }
 }
 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler: