Code

Updated Opsi
[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   public function __construct($config)
24   public function enabled()
25   function get_hosts_for_system_management()
26   private function xml_to_array($xml,$alternative_method = FALSE)
27   public function send_action($type,$hostId,$mac)
28   public function list_clients( $hostId = "")
29   public function add_client($hostId,$macaddress,$notes,$description)
30   public function modify_client($hostId,$mac,$notes,$description)
31   public function get_netboot_products($host = "")
32   public function get_local_products($host = "")
33   public function get_product_properties($productId,$hostId = "")
34   public function set_product_properties($productId,$cfg,$hostId = "")
35   public function add_product_to_client($productId,$hostId)
36   public function del_product_from_client($productId,$hostId)
37   public function get_client_hardware($hostId)
38   public function get_client_software($hostId)
39   public function del_client($hostId)
40   public function job_opsi_install_client($hostId,$mac)
42  ********/
45 /*! \brief  This is the opsi base class, it handles 
46   .          gosa daemon requests and prepares data for opsi plugins.
47  */
48 class opsi extends gosaSupportDaemon 
49 {
50   private $config = NULL;
51   protected $use_alternative_xml_parse_method = TRUE;
52   protected $target = "";
54   /*! \brief            Create opsi object.
55     @param
56     @return             
57    */
58   public function __construct($config)
59   {
60     $this->config = $config;
61     gosaSupportDaemon::__construct($config);
63     /* Detect the target opsi host 
64      */
65     $opsi_hosts = $this->get_hosts_with_module("opsi_com");
66       
67     /* Just use the first result of the opsi hosts 
68      */
69     if(count($opsi_hosts) == 1 && isset($opsi_hosts[0])){
70       $this->target = $opsi_hosts[0];
71     }elseif(count($opsi_hosts) > 1){
72       $this->target = $opsi_hosts[0];
73       msg_dialog::display(_("Opsi"),sprintf(_("More than one Opsi server were found, using the first result '%s'."),$this->target));
74     }
75   }
77   
78   public function enabled()
79   {
80     return(!empty($this->target));
81   }
84   /******************
85     Opsi handling 
86    ******************/
88   function get_hosts_for_system_management()
89   {
90     $res = $this->list_clients();
91     $data = array();
92     foreach($res as $entry){
93       if(!isset($entry['MAC'][0]['VALUE'])) $entry['MAC'][0]['VALUE'] = "";
94       $data[] = array(
95         "dn"          => "opsi:=".$entry['NAME'][0]['VALUE'].",".get_ou("sambaMachineAccountRDN").$this->config->current['BASE'],
96         "objectClass" => array("gosa_opsi_client"),
97         "cn"          => array(0 => $entry['NAME'][0]['VALUE']),
98         "description" => array(0 => $entry['DESCRIPTION'][0]['VALUE']),
99         "macAddress"  => array(0 => $entry['MAC'][0]['VALUE']),
100         "opsi_notes"  => array(0 => $entry['NOTES'][0]['VALUE']));
101     }
102     return($data);
103   }
106   /*! \brief  Maps all xml to array conversion to an alternative method
107                 then used in the parent class 'gosaSupportDaemon'.
108               The alternative method is able to handle more complex data.
109    */
110   private function xml_to_array($xml,$alternative_method = FALSE)
111   {
112     return(gosaSupportDaemon::xml_to_array($xml,TRUE));
113   }
116   /*! \brief  Trigger an event like wake or install for a specific hostId. 
117    */
118   public function send_action($type,$hostId,$mac)
119   {
120     switch($type){
121       case 'install'  :  $this->job_opsi_install_client($hostId,$mac); break;
122       default         :  trigger_error('Unknown type '.$type.'.');
123     }
124   }
127   /******************
128     SI Communication functions
129    ******************/
133   /*! \brief            Returns a list of all opsi clients.
134     @param
135     @return             
136    */
137   public function list_clients( $hostId = "")
138   {
139     $data   = array();
140     $res    = $this->send_data("gosa_opsi_list_clients",$this->target,$data,TRUE);
141     $items  = array();
142     if(isset($res['XML'][0]['ITEM'])){
143       $items = $res['XML'][0]['ITEM'];
144     }
145     return($items);
146   }
149   /*! \brief            Adds a new opsi client.
150     @param
151     @return             
152    */
153   public function add_client($hostId,$macaddress,$notes,$description)
154   {
155     $data = array("hostId" => $hostId,"macaddress" => $macaddress);
157     if(empty($hostId)){
158       trigger_error("No valid host id given, check parameter 1.");
159       return;
160     }
161   
162     /* Add optional attributes */ 
163     foreach(array("notes","description") as $attr) {
164       if(!empty($$attr)){
165         $data[$attr] = $$attr;
166       }
167     }
169     /* Query SI server */
170     $res    = $this->send_data("gosa_opsi_add_client",$this->target,$data,TRUE);
171   }
174   /*! \brief            Modify an opsi client.
175     @param
176     @return             
177    */
178   public function modify_client($hostId,$mac,$notes,$description)
179   {
180     $data = array("hostId" => $hostId,"mac" => $mac);
182     if(empty($hostId)){
183       trigger_error("No valid host id given, check parameter 1.");
184       return;
185     }
186   
187     /* Add optional attributes */ 
188     foreach(array("notes","description") as $attr) {
189       if(!empty($$attr)){
190         $data[$attr] = $$attr;
191       }
192     }
194     /* Query SI server */
195     $res = $this->send_data("gosa_opsi_modify_client",$this->target,$data,TRUE);
196   }
200   /*! \brief            Returns a list of netboot products.
201     @param
202     @return             
203    */
204   public function get_netboot_products($host = "")
205   {
206     /* Append host attribute to query data 
207      */
208     $data = array();
209     if(!empty($host)){
210       $data['hostId'] = trim($host);
211     }
213     $res    = $this->send_data("gosa_opsi_get_netboot_products",$this->target,$data,TRUE);
214     $items = array();
215     if(isset($res['XML'][0]['ITEM'])){
216       foreach($res['XML'][0]['ITEM'] as $entry){
217         $e = array("DESC" => $entry['DESCRIPTION'][0]['VALUE'],
218                    "NAME" => $entry['PRODUCTID'][0]['VALUE']);
219         $items[$entry['PRODUCTID'][0]['VALUE']] = $e;
220       }
221     }
222     return($items);
223   }
226   /*! \brief            Returns a list of all local products.
227     @param
228     @return             
229    */
230   public function get_local_products($host = "")
231   {
232     /* Append host attribute to query data 
233      */
234     $data = array();
235     if(!empty($host)){
236       $data['hostId'] = trim($host);
237     }
239     $res    = $this->send_data("gosa_opsi_get_local_products",$this->target,$data,TRUE);
240     $items = array();
241     if(isset($res['XML'][0]['ITEM'])){
242       foreach($res['XML'][0]['ITEM'] as $entry){
243         $e = array("DESC" => $entry['DESCRIPTION'][0]['VALUE'],
244                    "NAME" => $entry['PRODUCTID'][0]['VALUE']);
245         $items[$entry['PRODUCTID'][0]['VALUE']] = $e; 
246       }
247     }
248     return($items);
249   }
252   /*! \brief            Returns a list of all product properties. \ 
253     .           Additionally you can specify the host parameter to \
254     .           get host specific product properties
255     @param
256     @return             
257    */
258   public function get_product_properties($productId,$hostId = "")
259   {
260     $data = array("productId" => $productId);
262     /* Append host attribute to query data 
263      */
264     if(!empty($hostId)){
265       $data['hostId'] = trim($hostId);
266     }
267    
268     /* Check parameter */ 
269     if(empty($productId)){
270       trigger_error("No valid product id given, check parameter 1.");
271       return(array());
272     }
274     /* Query SI server */
275     $res    = $this->send_data("gosa_opsi_get_product_properties",$this->target,$data,TRUE);
276     $items  = array();
277     if(isset($res['XML'][0]['ITEM'])){   
278       foreach($res['XML'][0]['ITEM'] as $entry){
279         foreach($entry as $name => $val){
281           foreach(array("DESCRIPTION","DEFAULT") as $attr){
282             $items[$name][$attr] = "";
283             if(isset($val[0][$attr])){
284               $items[$name][$attr] = $val[0][$attr][0]['VALUE'];
285             }
286           }
287           $items[$name]['VALUE'] = array();
288           if(isset($val['0']['VALUE'])){
289             foreach($val['0']['VALUE'] as $value){
290               $items[$name]['VALUE'][] = $value['VALUE'];
291             }
292           }
293           $items[$name]['VALUE_CNT'] = count($items[$name]['VALUE']);
294         }
295       }
296     }
297     return($items);
298   }
301   /*! \brief            Set product properties, globally or per host. 
302     @param
303     @return             
304    */
305   public function set_product_properties($productId,$cfg,$hostId = "")
306   {
307     $data = array("productId" => $productId);
309     /* Append host attribute to query data 
310      */
311     if(!empty($hostId)){
312       $data['hostId'] = trim($hostId);
313     }
314    
315     /* Check parameter */ 
316     if(empty($productId)){
317       trigger_error("No valid product id given, check parameter 1.");
318       return(array());
319     }
321     if(!count($cfg)) return;
322     
323     /* Add properties */
324     $data['item'] = array();
325     foreach($cfg as $name => $value){
326       $data['item'][] = "<name>".$name."</name><value>".$value['DEFAULT']."</value>";
327     }  
329     /* Query SI server */
330     $res    = $this->send_data("gosa_opsi_set_product_properties",$this->target,$data,TRUE);
331   }
334   /*! \brief            Adds a given product to a client.
335     @param
336     @return             
337    */
338   public function add_product_to_client($productId,$hostId)
339   {
340     $data = array("productId" => $productId,"hostId" => $hostId);
342     /* Check parameter */ 
343     if(empty($productId)){
344       trigger_error("No valid product id given, check parameter 1.");
345       return;
346     }
347     if(empty($hostId)){
348       trigger_error("No valid host id given, check parameter 2.");
349       return;
350     }
352     /* Query SI server */
353     $res    = $this->send_data("gosa_opsi_add_product_to_client",$this->target,$data,TRUE);
354   }
357   /*! \brief      Removes a given product from a client.
358     @param
359     @return
360    */
361   public function del_product_from_client($productId,$hostId)
362   {
363     $data = array("productId" => $productId,"hostId" => $hostId);
365     /* Check parameter */ 
366     if(empty($productId)){
367       trigger_error("No valid product id given, check parameter 1.");
368       return;
369     }
370     if(empty($hostId)){
371       trigger_error("No valid host id given, check parameter 2.");
372       return;
373     }
375     /* Query SI server */
376     $res    = $this->send_data("gosa_opsi_del_product_from_client",$this->target,$data,TRUE);
377   }
380   /*! \brief            Returns the clients hardware setup.
381     @param
382     @return             
383    */
384   public function get_client_hardware($hostId)
385   {
386     $data = array("hostId" => $hostId);
388     /* Check parameter */ 
389     if(empty($hostId)){
390       trigger_error("No valid host id given, check parameter 1.");
391       return;
392     }
394     /* Query SI server */
395     $res    = $this->send_data("gosa_opsi_get_client_hardware",$this->target,$data,TRUE);
396     if(isset($res['XML'][0]['ITEM'])){
397       return($res['XML'][0]['ITEM']);
398     }
399     return(array());
400   }
403   /*! \brief            Returns the clients software setup.
404     @param
405     @return             
406    */
407   public function get_client_software($hostId)
408   {
409     $data = array("hostId" => $hostId);
411     /* Check parameter */ 
412     if(empty($hostId)){
413       trigger_error("No valid host id given, check parameter 1.");
414       return;
415     }
417     /* Query SI server */
418     $res    = $this->send_data("gosa_opsi_get_client_software",$this->target,$data,TRUE);
419     if(isset($res['XML'][0]['ITEM'])){
420       return($res['XML'][0]['ITEM']);
421     }
422     return(array());
423   }
427   /*! \brief            Deletes the given opsi client.
428     @param
429     @return             
430    */
431   public function del_client($hostId)
432   {
433     $data = array("hostId" => $hostId);
435     /* Check parameter */ 
436     if(empty($hostId)){
437       trigger_error("No valid host id given, check parameter 1.");
438       return;
439     }
441     /* Query SI server */
442     $res    = $this->send_data("gosa_opsi_del_client",$this->target,$data,TRUE);
443     if(isset($res['XML'][0]['ITEM'])){
444       return($res['XML'][0]['ITEM']);
445     }
446     return(array());
447   }
450   /*! \brief            Triggers install/reinstall of an opsi client.
451     @param
452     @return             
453    */
454   public function job_opsi_install_client($hostId,$mac)
455   {
456     $data = array("hostId" => $hostId,"macaddress"=>$mac);
458     /* Check parameter */ 
459     if(empty($hostId)){
460       trigger_error("No valid host id given, check parameter 1.");
461       return;
462     }
464     /* Query SI server */
465     $this->send_data("job_opsi_install_client",$this->target,$data,TRUE);
466   }
468 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
469 ?>