Code

New bunch of config values
[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(tests::is_mac($this->target));
77   }
79   /******************
80     Opsi handling 
81    ******************/
83   function get_hosts_for_system_management()
84   {
85     $res = $this->list_clients();
86     $data = array();
87     foreach($res as $entry){
88       if(!isset($entry['MAC'][0]['VALUE'])) $entry['MAC'][0]['VALUE'] = "";
89       $data[] = array(
90         "dn"          => "opsi:=".$entry['NAME'][0]['VALUE'].",".get_ou("sambaMachineAccountRDN").$this->config->current['BASE'],
91         "objectClass" => array("gosa_opsi_client"),
92         "cn"          => array(0 => $entry['NAME'][0]['VALUE']),
93         "description" => array(0 => $entry['DESCRIPTION'][0]['VALUE']),
94         "macAddress"  => array(0 => $entry['MAC'][0]['VALUE']),
95         "opsi_notes"  => array(0 => $entry['NOTES'][0]['VALUE']));
96     }
97     return($data);
98   }
101   /*! \brief  Maps all xml to array conversion to an alternative method
102                 then used in the parent class 'gosaSupportDaemon'.
103               The alternative method is able to handle more complex data.
104    */
105   private function xml_to_array($xml,$alternative_method = FALSE)
106   {
107     return(gosaSupportDaemon::xml_to_array($xml,TRUE));
108   }
111   /*! \brief  Trigger an event like wake or install for a specific hostId. 
112    */
113   public function send_action($type,$hostId,$mac)
114   {
115     switch($type){
116       case 'install'  :  $this->job_opsi_install_client($hostId,$mac); break;
117       default         :  trigger_error('Unknown type '.$type.'.');
118     }
119   }
122   /******************
123     SI Communication functions
124    ******************/
128   /*! \brief            Returns a list of all opsi clients.
129     @param
130     @return             
131    */
132   public function list_clients( $hostId = "")
133   {
134     $data   = array();
135     $res    = $this->send_data("gosa_opsi_list_clients",$this->target,$data,TRUE);
136     $items  = array();
137     if(isset($res['XML'][0]['ITEM'])){
138       $items = $res['XML'][0]['ITEM'];
139     }
140     return($items);
141   }
144   /*! \brief            Adds a new opsi client.
145     @param
146     @return             
147    */
148   public function add_client($hostId,$macaddress,$notes,$description)
149   {
150     $data = array("hostId" => $hostId,"macaddress" => $macaddress);
152     if(empty($hostId)){
153       trigger_error("No valid host id given, check parameter 1.");
154       return;
155     }
156   
157     /* Add optional attributes */ 
158     foreach(array("notes","description") as $attr) {
159       if(!empty($$attr)){
160         $data[$attr] = $$attr;
161       }
162     }
164     /* Query SI server */
165     $res    = $this->send_data("gosa_opsi_add_client",$this->target,$data,TRUE);
166   }
169   /*! \brief            Modify an opsi client.
170     @param
171     @return             
172    */
173   public function modify_client($hostId,$mac,$notes,$description)
174   {
175     $data = array("hostId" => $hostId,"mac" => $mac);
177     if(empty($hostId)){
178       trigger_error("No valid host id given, check parameter 1.");
179       return;
180     }
181   
182     /* Add optional attributes */ 
183     foreach(array("notes","description") as $attr) {
184       if(!empty($$attr)){
185         $data[$attr] = $$attr;
186       }
187     }
189     /* Query SI server */
190     $res = $this->send_data("gosa_opsi_modify_client",$this->target,$data,TRUE);
191   }
195   /*! \brief            Returns a list of netboot products.
196     @param
197     @return             
198    */
199   public function get_netboot_products($host = "")
200   {
201     /* Append host attribute to query data 
202      */
203     $data = array();
204     if(!empty($host)){
205       $data['hostId'] = trim($host);
206     }
208     $res    = $this->send_data("gosa_opsi_get_netboot_products",$this->target,$data,TRUE);
209     $items = array();
210     if(isset($res['XML'][0]['ITEM'])){
211       foreach($res['XML'][0]['ITEM'] as $entry){
212         $e = array("DESC" => $entry['DESCRIPTION'][0]['VALUE'],
213                    "NAME" => $entry['PRODUCTID'][0]['VALUE']);
214         $items[$entry['PRODUCTID'][0]['VALUE']] = $e;
215       }
216     }
217     return($items);
218   }
221   /*! \brief            Returns a list of all local products.
222     @param
223     @return             
224    */
225   public function get_local_products($host = "")
226   {
227     /* Append host attribute to query data 
228      */
229     $data = array();
230     if(!empty($host)){
231       $data['hostId'] = trim($host);
232     }
234     $res    = $this->send_data("gosa_opsi_get_local_products",$this->target,$data,TRUE);
235     $items = array();
236     if(isset($res['XML'][0]['ITEM'])){
237       foreach($res['XML'][0]['ITEM'] as $entry){
238         $e = array("DESC" => $entry['DESCRIPTION'][0]['VALUE'],
239                    "NAME" => $entry['PRODUCTID'][0]['VALUE']);
240         $items[$entry['PRODUCTID'][0]['VALUE']] = $e; 
241       }
242     }
243     return($items);
244   }
247   /*! \brief            Returns a list of all product properties. \ 
248     .           Additionally you can specify the host parameter to \
249     .           get host specific product properties
250     @param
251     @return             
252    */
253   public function get_product_properties($productId,$hostId = "")
254   {
255     $data = array("productId" => $productId);
257     /* Append host attribute to query data 
258      */
259     if(!empty($hostId)){
260       $data['hostId'] = trim($hostId);
261     }
262    
263     /* Check parameter */ 
264     if(empty($productId)){
265       trigger_error("No valid product id given, check parameter 1.");
266       return(array());
267     }
269     /* Query SI server */
270     $res    = $this->send_data("gosa_opsi_get_product_properties",$this->target,$data,TRUE);
271     $items  = array();
272     if(isset($res['XML'][0]['ITEM'])){   
273       foreach($res['XML'][0]['ITEM'] as $entry){
274         foreach($entry as $name => $val){
276           foreach(array("DESCRIPTION","DEFAULT") as $attr){
277             $items[$name][$attr] = "";
278             if(isset($val[0][$attr])){
279               $items[$name][$attr] = $val[0][$attr][0]['VALUE'];
280             }
281           }
282           $items[$name]['VALUE'] = array();
283           if(isset($val['0']['VALUE'])){
284             foreach($val['0']['VALUE'] as $value){
285               $items[$name]['VALUE'][] = $value['VALUE'];
286             }
287           }
288           $items[$name]['VALUE_CNT'] = count($items[$name]['VALUE']);
289         }
290       }
291     }
292     return($items);
293   }
296   /*! \brief            Set product properties, globally or per host. 
297     @param
298     @return             
299    */
300   public function set_product_properties($productId,$cfg,$hostId = "")
301   {
302     $data = array("productId" => $productId);
304     /* Append host attribute to query data 
305      */
306     if(!empty($hostId)){
307       $data['hostId'] = trim($hostId);
308     }
309    
310     /* Check parameter */ 
311     if(empty($productId)){
312       trigger_error("No valid product id given, check parameter 1.");
313       return(array());
314     }
316     if(!count($cfg)) return;
317     
318     /* Add properties */
319     $data['item'] = array();
320     foreach($cfg as $name => $value){
321       $data['item'][] = "<name>".$name."</name><value>".$value['DEFAULT']."</value>";
322     }  
324     /* Query SI server */
325     $res    = $this->send_data("gosa_opsi_set_product_properties",$this->target,$data,TRUE);
326   }
329   /*! \brief            Adds a given product to a client.
330     @param
331     @return             
332    */
333   public function add_product_to_client($productId,$hostId)
334   {
335     $data = array("productId" => $productId,"hostId" => $hostId);
337     /* Check parameter */ 
338     if(empty($productId)){
339       trigger_error("No valid product id given, check parameter 1.");
340       return;
341     }
342     if(empty($hostId)){
343       trigger_error("No valid host id given, check parameter 2.");
344       return;
345     }
347     /* Query SI server */
348     $res    = $this->send_data("gosa_opsi_add_product_to_client",$this->target,$data,TRUE);
349   }
352   /*! \brief      Removes a given product from a client.
353     @param
354     @return
355    */
356   public function del_product_from_client($productId,$hostId)
357   {
358     $data = array("productId" => $productId,"hostId" => $hostId);
360     /* Check parameter */ 
361     if(empty($productId)){
362       trigger_error("No valid product id given, check parameter 1.");
363       return;
364     }
365     if(empty($hostId)){
366       trigger_error("No valid host id given, check parameter 2.");
367       return;
368     }
370     /* Query SI server */
371     $res    = $this->send_data("gosa_opsi_del_product_from_client",$this->target,$data,TRUE);
372   }
375   /*! \brief            Returns the clients hardware setup.
376     @param
377     @return             
378    */
379   public function get_client_hardware($hostId)
380   {
381     $data = array("hostId" => $hostId);
383     /* Check parameter */ 
384     if(empty($hostId)){
385       trigger_error("No valid host id given, check parameter 1.");
386       return;
387     }
389     /* Query SI server */
390     $res    = $this->send_data("gosa_opsi_get_client_hardware",$this->target,$data,TRUE);
391     if(isset($res['XML'][0]['ITEM'])){
392       return($res['XML'][0]['ITEM']);
393     }
394     return(array());
395   }
398   /*! \brief            Returns the clients software setup.
399     @param
400     @return             
401    */
402   public function get_client_software($hostId)
403   {
404     $data = array("hostId" => $hostId);
406     /* Check parameter */ 
407     if(empty($hostId)){
408       trigger_error("No valid host id given, check parameter 1.");
409       return;
410     }
412     /* Query SI server */
413     $res    = $this->send_data("gosa_opsi_get_client_software",$this->target,$data,TRUE);
414     if(isset($res['XML'][0]['ITEM'])){
415       return($res['XML'][0]['ITEM']);
416     }
417     return(array());
418   }
422   /*! \brief            Deletes the given opsi client.
423     @param
424     @return             
425    */
426   public function del_client($hostId)
427   {
428     $data = array("hostId" => $hostId);
430     /* Check parameter */ 
431     if(empty($hostId)){
432       trigger_error("No valid host id given, check parameter 1.");
433       return;
434     }
436     /* Query SI server */
437     $res    = $this->send_data("gosa_opsi_del_client",$this->target,$data,TRUE);
438     if(isset($res['XML'][0]['ITEM'])){
439       return($res['XML'][0]['ITEM']);
440     }
441     return(array());
442   }
445   /*! \brief            Triggers install/reinstall of an opsi client.
446     @param
447     @return             
448    */
449   public function job_opsi_install_client($hostId,$mac)
450   {
451     $data = array("hostId" => $hostId,"macaddress"=>$mac);
453     /* Check parameter */ 
454     if(empty($hostId)){
455       trigger_error("No valid host id given, check parameter 1.");
456       return;
457     }
459     /* Query SI server */
460     $this->send_data("job_opsi_install_client",$this->target,$data,TRUE);
461   }
463 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
464 ?>