Code

iUpdated class 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       $data[$attr] = $$attr;
190     }
192     /* Query SI server */
193     $res = $this->send_data("gosa_opsi_modify_client",$this->target,$data,TRUE);
194   }
198   /*! \brief            Returns a list of netboot products.
199     @param
200     @return             
201    */
202   public function get_netboot_products($host = "")
203   {
204     /* Append host attribute to query data 
205      */
206     $data = array();
207     if(!empty($host)){
208       $data['hostId'] = trim($host);
209     }
211     $res    = $this->send_data("gosa_opsi_get_netboot_products",$this->target,$data,TRUE);
212     $items = array();
213     if(isset($res['XML'][0]['ITEM'])){
214       foreach($res['XML'][0]['ITEM'] as $entry){
215         $e = array("DESC" => $entry['DESCRIPTION'][0]['VALUE'],
216                    "NAME" => $entry['PRODUCTID'][0]['VALUE']);
217         $items[$entry['PRODUCTID'][0]['VALUE']] = $e;
218       }
219     }
220     return($items);
221   }
224   /*! \brief            Returns a list of all local products.
225     @param
226     @return             
227    */
228   public function get_local_products($host = "")
229   {
230     /* Append host attribute to query data 
231      */
232     $data = array();
233     if(!empty($host)){
234       $data['hostId'] = trim($host);
235     }
237     $res    = $this->send_data("gosa_opsi_get_local_products",$this->target,$data,TRUE);
238     $items = array();
239     if(isset($res['XML'][0]['ITEM'])){
240       foreach($res['XML'][0]['ITEM'] as $entry){
241         $e = array("DESC" => $entry['DESCRIPTION'][0]['VALUE'],
242                    "NAME" => $entry['PRODUCTID'][0]['VALUE']);
243         $items[$entry['PRODUCTID'][0]['VALUE']] = $e; 
244       }
245     }
246     return($items);
247   }
250   /*! \brief            Returns a list of all product properties. \ 
251     .           Additionally you can specify the host parameter to \
252     .           get host specific product properties
253     @param
254     @return             
255    */
256   public function get_product_properties($productId,$hostId = "")
257   {
258     $data = array("productId" => $productId);
260     /* Append host attribute to query data 
261      */
262     if(!empty($hostId)){
263       $data['hostId'] = trim($hostId);
264     }
265    
266     /* Check parameter */ 
267     if(empty($productId)){
268       trigger_error("No valid product id given, check parameter 1.");
269       return(array());
270     }
272     /* Query SI server */
273     $res    = $this->send_data("gosa_opsi_get_product_properties",$this->target,$data,TRUE);
274     $items  = array();
275     if(isset($res['XML'][0]['ITEM'])){   
276       foreach($res['XML'][0]['ITEM'] as $entry){
277         foreach($entry as $name => $val){
279           foreach(array("DESCRIPTION","DEFAULT") as $attr){
280             $items[$name][$attr] = "";
281             if(isset($val[0][$attr])){
282               $items[$name][$attr] = $val[0][$attr][0]['VALUE'];
283             }
284           }
285           $items[$name]['VALUE'] = array();
286           if(isset($val['0']['VALUE'])){
287             foreach($val['0']['VALUE'] as $value){
288               $items[$name]['VALUE'][] = $value['VALUE'];
289             }
290           }
291           $items[$name]['VALUE_CNT'] = count($items[$name]['VALUE']);
292         }
293       }
294     }
295     return($items);
296   }
299   /*! \brief            Set product properties, globally or per host. 
300     @param
301     @return             
302    */
303   public function set_product_properties($productId,$cfg,$hostId = "")
304   {
305     $data = array("productId" => $productId);
307     /* Append host attribute to query data 
308      */
309     if(!empty($hostId)){
310       $data['hostId'] = trim($hostId);
311     }
312    
313     /* Check parameter */ 
314     if(empty($productId)){
315       trigger_error("No valid product id given, check parameter 1.");
316       return(array());
317     }
319     if(!count($cfg)) return;
320     
321     /* Add properties */
322     $data['item'] = array();
323     foreach($cfg as $name => $value){
324       $data['item'][] = "<name>".$name."</name><value>".$value['DEFAULT']."</value>";
325     }  
327     /* Query SI server */
328     $res    = $this->send_data("gosa_opsi_set_product_properties",$this->target,$data,TRUE);
329   }
332   /*! \brief            Adds a given product to a client.
333     @param
334     @return             
335    */
336   public function add_product_to_client($productId,$hostId)
337   {
338     $data = array("productId" => $productId,"hostId" => $hostId);
340     /* Check parameter */ 
341     if(empty($productId)){
342       trigger_error("No valid product id given, check parameter 1.");
343       return;
344     }
345     if(empty($hostId)){
346       trigger_error("No valid host id given, check parameter 2.");
347       return;
348     }
350     /* Query SI server */
351     $res    = $this->send_data("gosa_opsi_add_product_to_client",$this->target,$data,TRUE);
352   }
355   /*! \brief      Removes a given product from a client.
356     @param
357     @return
358    */
359   public function del_product_from_client($productId,$hostId)
360   {
361     $data = array("productId" => $productId,"hostId" => $hostId);
363     /* Check parameter */ 
364     if(empty($productId)){
365       trigger_error("No valid product id given, check parameter 1.");
366       return;
367     }
368     if(empty($hostId)){
369       trigger_error("No valid host id given, check parameter 2.");
370       return;
371     }
373     /* Query SI server */
374     $res    = $this->send_data("gosa_opsi_del_product_from_client",$this->target,$data,TRUE);
375   }
378   /*! \brief            Returns the clients hardware setup.
379     @param
380     @return             
381    */
382   public function get_client_hardware($hostId)
383   {
384     $data = array("hostId" => $hostId);
386     /* Check parameter */ 
387     if(empty($hostId)){
388       trigger_error("No valid host id given, check parameter 1.");
389       return;
390     }
392     /* Query SI server */
393     $res    = $this->send_data("gosa_opsi_get_client_hardware",$this->target,$data,TRUE);
394     if(isset($res['XML'][0]['ITEM'])){
395       return($res['XML'][0]['ITEM']);
396     }
397     return(array());
398   }
401   /*! \brief            Returns the clients software setup.
402     @param
403     @return             
404    */
405   public function get_client_software($hostId)
406   {
407     $data = array("hostId" => $hostId);
409     /* Check parameter */ 
410     if(empty($hostId)){
411       trigger_error("No valid host id given, check parameter 1.");
412       return;
413     }
415     /* Query SI server */
416     $res    = $this->send_data("gosa_opsi_get_client_software",$this->target,$data,TRUE);
417     if(isset($res['XML'][0]['ITEM'])){
418       return($res['XML'][0]['ITEM']);
419     }
420     return(array());
421   }
425   /*! \brief            Deletes the given opsi client.
426     @param
427     @return             
428    */
429   public function del_client($hostId)
430   {
431     $data = array("hostId" => $hostId);
433     /* Check parameter */ 
434     if(empty($hostId)){
435       trigger_error("No valid host id given, check parameter 1.");
436       return;
437     }
439     /* Query SI server */
440     $res    = $this->send_data("gosa_opsi_del_client",$this->target,$data,TRUE);
441     if(isset($res['XML'][0]['ITEM'])){
442       return($res['XML'][0]['ITEM']);
443     }
444     return(array());
445   }
448   /*! \brief            Triggers install/reinstall of an opsi client.
449     @param
450     @return             
451    */
452   public function job_opsi_install_client($hostId,$mac)
453   {
454     $data = array("hostId" => $hostId,"macaddress"=>$mac);
456     /* Check parameter */ 
457     if(empty($hostId)){
458       trigger_error("No valid host id given, check parameter 1.");
459       return;
460     }
462     /* Query SI server */
463     $this->send_data("job_opsi_install_client",$this->target,$data,TRUE);
464   }
466 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
467 ?>