Code

Updated opsi enabled check.
[gosa.git] / gosa-plugins / opsi / admin / opsi / class_opsi.inc
1 <?php
2 /*
3    This code is part of GOsa (https://gosa.gonicus.de)
4    Copyright (C) 2008  Fabian Hickert
6    This program is free software: you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation, either version 3 of the License, or
9    (at your option) any later version.
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
16    You should have received a copy of the GNU General Public License
17    along with this program.  If not, see <http://www.gnu.org/licenses/>.
18  */
21 /********
23   __construct($config)
24   get_hosts_for_system_management()
25   get_netboot_products($host = "")
26   get_local_products($host = "")
27   get_product_properties()
28   set_product_properties()
29   get_client_hardware()
30   get_client_software()
31   list_clients()
32   del_client()
33   job_opsi_install_client()
34   add_client()
35   add_product_to_client()
36   del_product_from_client()
38  ********/
41 /*! \brief  This is the opsi base class, it handles 
42   .          gosa daemon requests and prepares data for opsi plugins.
43  */
44 class opsi extends gosaSupportDaemon 
45 {
46   private $config = NULL;
47   protected $use_alternative_xml_parse_method = TRUE;
48   protected $target = "";
50   /*! \brief            Create opsi object.
51     @param
52     @return             
53    */
54   public function __construct($config)
55   {
56     $this->config = $config;
57     gosaSupportDaemon::__construct($config);
59     /* Detect the target opsi host 
60      */
61     $opsi_hosts = $this->get_hosts_with_module("opsi_com");
62       
63     /* Just use the first result of the opsi hosts 
64      */
65     if(count($opsi_hosts) == 1 && isset($opsi_hosts[0])){
66       $this->target = $opsi_hosts[0];
67     }elseif(count($opsi_hosts) > 1){
68       $this->target = $opsi_hosts[0];
69       msg_dialog::display(_("Opsi"),sprintf(_("More than one Opsi server were found, using the first result '%s'."),$this->target));
70     }
71   }
73   
74   public function enabled()
75   {
76     return(!empty($this->target));
77   }
80   /******************
81     Opsi handling 
82    ******************/
84   function get_hosts_for_system_management()
85   {
86     $res = $this->list_clients();
87     $data = array();
88     foreach($res as $entry){
89       if(!isset($entry['MAC'][0]['VALUE'])) $entry['MAC'][0]['VALUE'] = "";
90       $data[] = array(
91         "dn"          => "opsi:=".$entry['NAME'][0]['VALUE'].",".get_ou("sambaMachineAccountRDN").$this->config->current['BASE'],
92         "objectClass" => array("gosa_opsi_client"),
93         "cn"          => array(0 => $entry['NAME'][0]['VALUE']),
94         "description" => array(0 => $entry['DESCRIPTION'][0]['VALUE']),
95         "macAddress"  => array(0 => $entry['MAC'][0]['VALUE']),
96         "opsi_notes"  => array(0 => $entry['NOTES'][0]['VALUE']));
97     }
98     return($data);
99   }
102   /*! \brief  Maps all xml to array conversion to an alternative method
103                 then used in the parent class 'gosaSupportDaemon'.
104               The alternative method is able to handle more complex data.
105    */
106   private function xml_to_array($xml,$alternative_method = FALSE)
107   {
108     return(gosaSupportDaemon::xml_to_array($xml,TRUE));
109   }
112   /*! \brief  Trigger an event like wake or install for a specific hostId. 
113    */
114   public function send_action($type,$hostId,$mac)
115   {
116     switch($type){
117       case 'install'  :  $this->job_opsi_install_client($hostId,$mac); break;
118       default         :  trigger_error('Unknown type '.$type.'.');
119     }
120   }
123   /******************
124     SI Communication functions
125    ******************/
129   /*! \brief            Returns a list of all opsi clients.
130     @param
131     @return             
132    */
133   public function list_clients( $hostId = "")
134   {
135     $data   = array();
136     $res    = $this->send_data("gosa_opsi_list_clients",$this->target,$data,TRUE);
137     $items  = array();
138     if(isset($res['XML'][0]['ITEM'])){
139       $items = $res['XML'][0]['ITEM'];
140     }
141     return($items);
142   }
145   /*! \brief            Adds a new opsi client.
146     @param
147     @return             
148    */
149   public function add_client($hostId,$macaddress,$notes,$description)
150   {
151     $data = array("hostId" => $hostId,"macaddress" => $macaddress);
153     if(empty($hostId)){
154       trigger_error("No valid host id given, check parameter 1.");
155       return;
156     }
157   
158     /* Add optional attributes */ 
159     foreach(array("notes","description") as $attr) {
160       if(!empty($$attr)){
161         $data[$attr] = $$attr;
162       }
163     }
165     /* Query SI server */
166     $res    = $this->send_data("gosa_opsi_add_client",$this->target,$data,TRUE);
167   }
170   /*! \brief            Modify an opsi client.
171     @param
172     @return             
173    */
174   public function modify_client($hostId,$mac,$notes,$description)
175   {
176     $data = array("hostId" => $hostId,"mac" => $mac);
178     if(empty($hostId)){
179       trigger_error("No valid host id given, check parameter 1.");
180       return;
181     }
182   
183     /* Add optional attributes */ 
184     foreach(array("notes","description") as $attr) {
185       if(!empty($$attr)){
186         $data[$attr] = $$attr;
187       }
188     }
190     /* Query SI server */
191     $res = $this->send_data("gosa_opsi_modify_client",$this->target,$data,TRUE);
192   }
196   /*! \brief            Returns a list of netboot products.
197     @param
198     @return             
199    */
200   public function get_netboot_products($host = "")
201   {
202     /* Append host attribute to query data 
203      */
204     $data = array();
205     if(!empty($host)){
206       $data['hostId'] = trim($host);
207     }
209     $res    = $this->send_data("gosa_opsi_get_netboot_products",$this->target,$data,TRUE);
210     $items = array();
211     if(isset($res['XML'][0]['ITEM'])){
212       foreach($res['XML'][0]['ITEM'] as $entry){
213         $e = array("DESC" => $entry['DESCRIPTION'][0]['VALUE'],
214                    "NAME" => $entry['PRODUCTID'][0]['VALUE']);
215         $items[$entry['PRODUCTID'][0]['VALUE']] = $e;
216       }
217     }
218     return($items);
219   }
222   /*! \brief            Returns a list of all local products.
223     @param
224     @return             
225    */
226   public function get_local_products($host = "")
227   {
228     /* Append host attribute to query data 
229      */
230     $data = array();
231     if(!empty($host)){
232       $data['hostId'] = trim($host);
233     }
235     $res    = $this->send_data("gosa_opsi_get_local_products",$this->target,$data,TRUE);
236     $items = array();
237     if(isset($res['XML'][0]['ITEM'])){
238       foreach($res['XML'][0]['ITEM'] as $entry){
239         $e = array("DESC" => $entry['DESCRIPTION'][0]['VALUE'],
240                    "NAME" => $entry['PRODUCTID'][0]['VALUE']);
241         $items[$entry['PRODUCTID'][0]['VALUE']] = $e; 
242       }
243     }
244     return($items);
245   }
248   /*! \brief            Returns a list of all product properties. \ 
249     .           Additionally you can specify the host parameter to \
250     .           get host specific product properties
251     @param
252     @return             
253    */
254   public function get_product_properties($productId,$hostId = "")
255   {
256     $data = array("productId" => $productId);
258     /* Append host attribute to query data 
259      */
260     if(!empty($hostId)){
261       $data['hostId'] = trim($hostId);
262     }
263    
264     /* Check parameter */ 
265     if(empty($productId)){
266       trigger_error("No valid product id given, check parameter 1.");
267       return(array());
268     }
270     /* Query SI server */
271     $res    = $this->send_data("gosa_opsi_get_product_properties",$this->target,$data,TRUE);
272     $items  = array();
273     if(isset($res['XML'][0]['ITEM'])){   
274       foreach($res['XML'][0]['ITEM'] as $entry){
275         foreach($entry as $name => $val){
277           foreach(array("DESCRIPTION","DEFAULT") as $attr){
278             $items[$name][$attr] = "";
279             if(isset($val[0][$attr])){
280               $items[$name][$attr] = $val[0][$attr][0]['VALUE'];
281             }
282           }
283           $items[$name]['VALUE'] = array();
284           if(isset($val['0']['VALUE'])){
285             foreach($val['0']['VALUE'] as $value){
286               $items[$name]['VALUE'][] = $value['VALUE'];
287             }
288           }
289           $items[$name]['VALUE_CNT'] = count($items[$name]['VALUE']);
290         }
291       }
292     }
293     return($items);
294   }
297   /*! \brief            Set product properties, globally or per host. 
298     @param
299     @return             
300    */
301   public function set_product_properties($productId,$cfg,$hostId = "")
302   {
303     $data = array("productId" => $productId);
305     /* Append host attribute to query data 
306      */
307     if(!empty($hostId)){
308       $data['hostId'] = trim($hostId);
309     }
310    
311     /* Check parameter */ 
312     if(empty($productId)){
313       trigger_error("No valid product id given, check parameter 1.");
314       return(array());
315     }
317     if(!count($cfg)) return;
318     
319     /* Add properties */
320     $data['item'] = array();
321     foreach($cfg as $name => $value){
322       $data['item'][] = "<name>".$name."</name><value>".$value['DEFAULT']."</value>";
323     }  
325     /* Query SI server */
326     $res    = $this->send_data("gosa_opsi_set_product_properties",$this->target,$data,TRUE);
327   }
330   /*! \brief            Adds a given product to a client.
331     @param
332     @return             
333    */
334   public function add_product_to_client($productId,$hostId)
335   {
336     $data = array("productId" => $productId,"hostId" => $hostId);
338     /* Check parameter */ 
339     if(empty($productId)){
340       trigger_error("No valid product id given, check parameter 1.");
341       return;
342     }
343     if(empty($hostId)){
344       trigger_error("No valid host id given, check parameter 2.");
345       return;
346     }
348     /* Query SI server */
349     $res    = $this->send_data("gosa_opsi_add_product_to_client",$this->target,$data,TRUE);
350   }
353   /*! \brief      Removes a given product from a client.
354     @param
355     @return
356    */
357   public function del_product_from_client($productId,$hostId)
358   {
359     $data = array("productId" => $productId,"hostId" => $hostId);
361     /* Check parameter */ 
362     if(empty($productId)){
363       trigger_error("No valid product id given, check parameter 1.");
364       return;
365     }
366     if(empty($hostId)){
367       trigger_error("No valid host id given, check parameter 2.");
368       return;
369     }
371     /* Query SI server */
372     $res    = $this->send_data("gosa_opsi_del_product_from_client",$this->target,$data,TRUE);
373   }
376   /*! \brief            Returns the clients hardware setup.
377     @param
378     @return             
379    */
380   public function get_client_hardware($hostId)
381   {
382     $data = array("hostId" => $hostId);
384     /* Check parameter */ 
385     if(empty($hostId)){
386       trigger_error("No valid host id given, check parameter 1.");
387       return;
388     }
390     /* Query SI server */
391     $res    = $this->send_data("gosa_opsi_get_client_hardware",$this->target,$data,TRUE);
392     if(isset($res['XML'][0]['ITEM'])){
393       return($res['XML'][0]['ITEM']);
394     }
395     return(array());
396   }
399   /*! \brief            Returns the clients software setup.
400     @param
401     @return             
402    */
403   public function get_client_software($hostId)
404   {
405     $data = array("hostId" => $hostId);
407     /* Check parameter */ 
408     if(empty($hostId)){
409       trigger_error("No valid host id given, check parameter 1.");
410       return;
411     }
413     /* Query SI server */
414     $res    = $this->send_data("gosa_opsi_get_client_software",$this->target,$data,TRUE);
415     if(isset($res['XML'][0]['ITEM'])){
416       return($res['XML'][0]['ITEM']);
417     }
418     return(array());
419   }
423   /*! \brief            Deletes the given opsi client.
424     @param
425     @return             
426    */
427   public function del_client($hostId)
428   {
429     $data = array("hostId" => $hostId);
431     /* Check parameter */ 
432     if(empty($hostId)){
433       trigger_error("No valid host id given, check parameter 1.");
434       return;
435     }
437     /* Query SI server */
438     $res    = $this->send_data("gosa_opsi_del_client",$this->target,$data,TRUE);
439     if(isset($res['XML'][0]['ITEM'])){
440       return($res['XML'][0]['ITEM']);
441     }
442     return(array());
443   }
446   /*! \brief            Triggers install/reinstall of an opsi client.
447     @param
448     @return             
449    */
450   public function job_opsi_install_client($hostId,$mac)
451   {
452     $data = array("hostId" => $hostId,"macaddress"=>$mac);
454     /* Check parameter */ 
455     if(empty($hostId)){
456       trigger_error("No valid host id given, check parameter 1.");
457       return;
458     }
460     /* Query SI server */
461     $this->send_data("job_opsi_install_client",$this->target,$data,TRUE);
462   }
464 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
465 ?>