Code

Updated New Device && opsi host
[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     $tmp= $this->config->search("faiManagement", "CLASS",array('menu','tabs'));
66     if(!empty($tmp) && class_available("faiManagement")){
67       $opsi_hosts = $this->get_hosts_with_module("opsi_com");
69       /* Just use the first result of the opsi hosts 
70        */
71       if(count($opsi_hosts) == 1 && isset($opsi_hosts[0])){
72         $this->target = $opsi_hosts[0];
73       }elseif(count($opsi_hosts) > 1){
74         $this->target = $opsi_hosts[0];
75         msg_dialog::display(_("Opsi"),sprintf(_("More than one Opsi server were found, using the first result '%s'."),$this->target));
76       }
77     }
78   }
80   
81   public function enabled()
82   {
83     $tmp= $this->config->search("faiManagement", "CLASS",array('menu','tabs'));
84     if(!empty($tmp) && class_available("faiManagement") && !empty($this->target)){
85       return(TRUE);
86     }   
87     return(FALSE);
88   }
91   /******************
92     Opsi handling 
93    ******************/
95   function get_hosts_for_system_management()
96   {
97     $res = $this->list_clients();
98     $data = array();
99     $ui = get_userinfo();
100     foreach($res as $entry){
101       if(!isset($entry['MAC'][0]['VALUE'])) $entry['MAC'][0]['VALUE'] = "";
102       $obj = array(
103         "dn"          => "opsi:=".$entry['NAME'][0]['VALUE'].",".get_ou("sambaMachineAccountRDN").$this->config->current['BASE'],
104         "objectClass" => array("gosa_opsi_client"),
105         "cn"          => array(0 => $entry['NAME'][0]['VALUE']),
106         "macAddress"  => array(0 => $entry['MAC'][0]['VALUE']),
107         "opsi_notes"  => array(0 => $entry['NOTES'][0]['VALUE']));
109       /* Check permissions */
110       $opsi_acl = $ui->get_permissions($obj['dn'],"opsi/opsiGeneric");
111       if(preg_match("/r/",$opsi_acl)){
112         if(!empty($entry['DESCRIPTION'][0]['VALUE'])){ 
113           $obj["description"]= array();
114           $obj["description"][0]= $entry['DESCRIPTION'][0]['VALUE'];
115         }
116         $data[] = $obj;
117       }
118     }
120     return($data);
121   }
124   /*! \brief  Maps all xml to array conversion to an alternative method
125                 then used in the parent class 'gosaSupportDaemon'.
126               The alternative method is able to handle more complex data.
127    */
128   private function xml_to_array($xml,$alternative_method = FALSE)
129   {
130     return(gosaSupportDaemon::xml_to_array($xml,TRUE));
131   }
134   /*! \brief  Trigger an event like wake or install for a specific hostId. 
135    */
136   public function send_action($type,$hostId,$mac)
137   {
138     switch($type){
139       case 'install'  :  $this->job_opsi_install_client($hostId,$mac); break;
140       default         :  trigger_error('Unknown type '.$type.'.');
141     }
142   }
145   /******************
146     SI Communication functions
147    ******************/
151   /*! \brief            Returns a list of all opsi clients.
152     @param
153     @return             
154    */
155   public function list_clients( $hostId = "")
156   {
157     $data   = array();
158     $res    = $this->send_data("gosa_opsi_list_clients",$this->target,$data,TRUE);
159     $items  = array();
160     if(isset($res['XML'][0]['ITEM'])){
161       $items = $res['XML'][0]['ITEM'];
162     }
163     return($items);
164   }
167   /*! \brief            Adds a new opsi client.
168     @param
169     @return             
170    */
171   public function add_client($hostId,$macaddress,$notes,$description)
172   {
173     $data = array("hostId" => $hostId,"macaddress" => $macaddress);
175     if(empty($hostId)){
176       trigger_error("No valid host id given, check parameter 1.");
177       return;
178     }
179   
180     /* Add optional attributes */ 
181     foreach(array("notes","description") as $attr) {
182       if(!empty($$attr)){
183         $data[$attr] = $$attr;
184       }
185     }
187     /* Query SI server */
188     $res    = $this->send_data("gosa_opsi_add_client",$this->target,$data,TRUE);
189   }
192   /*! \brief            Modify an opsi client.
193     @param
194     @return             
195    */
196   public function modify_client($hostId,$mac,$notes,$description)
197   {
198     $data = array("hostId" => $hostId,"mac" => $mac);
200     if(empty($hostId)){
201       trigger_error("No valid host id given, check parameter 1.");
202       return;
203     }
204   
205     /* Add optional attributes */ 
206     foreach(array("notes","description") as $attr) {
207       $data[$attr] = $$attr;
208     }
210     /* Query SI server */
211     $res = $this->send_data("gosa_opsi_modify_client",$this->target,$data,TRUE);
212   }
216   /*! \brief            Returns a list of netboot products.
217     @param
218     @return             
219    */
220   public function get_netboot_products($host = "")
221   {
222     /* Append host attribute to query data 
223      */
224     $data = array();
225     if(!empty($host)){
226       $data['hostId'] = trim($host);
227     }
229     $res    = $this->send_data("gosa_opsi_get_netboot_products",$this->target,$data,TRUE);
230     $items = array();
231     if(isset($res['XML'][0]['ITEM'])){
232       foreach($res['XML'][0]['ITEM'] as $entry){
233         $e = array("DESC" => $entry['DESCRIPTION'][0]['VALUE'],
234                    "NAME" => $entry['PRODUCTID'][0]['VALUE']);
235         $items[$entry['PRODUCTID'][0]['VALUE']] = $e;
236       }
237     }
238     return($items);
239   }
242   /*! \brief            Returns a list of all local products.
243     @param
244     @return             
245    */
246   public function get_local_products($host = "")
247   {
248     /* Append host attribute to query data 
249      */
250     $data = array();
251     if(!empty($host)){
252       $data['hostId'] = trim($host);
253     }
255     $res    = $this->send_data("gosa_opsi_get_local_products",$this->target,$data,TRUE);
256     $items = array();
257     if(isset($res['XML'][0]['ITEM'])){
258       foreach($res['XML'][0]['ITEM'] as $entry){
259         $e = array("DESC" => $entry['DESCRIPTION'][0]['VALUE'],
260                    "NAME" => $entry['PRODUCTID'][0]['VALUE']);
261         $items[$entry['PRODUCTID'][0]['VALUE']] = $e; 
262       }
263     }
264     return($items);
265   }
268   /*! \brief            Returns a list of all product properties. \ 
269     .           Additionally you can specify the host parameter to \
270     .           get host specific product properties
271     @param
272     @return             
273    */
274   public function get_product_properties($productId,$hostId = "")
275   {
276     $data = array("productId" => $productId);
278     /* Append host attribute to query data 
279      */
280     if(!empty($hostId)){
281       $data['hostId'] = trim($hostId);
282     }
283    
284     /* Check parameter */ 
285     if(empty($productId)){
286       trigger_error("No valid product id given, check parameter 1.");
287       return(array());
288     }
290     /* Query SI server */
291     $res    = $this->send_data("gosa_opsi_get_product_properties",$this->target,$data,TRUE);
292     $items  = array();
293     if(isset($res['XML'][0]['ITEM'])){   
294       foreach($res['XML'][0]['ITEM'] as $entry){
295         foreach($entry as $name => $val){
297           foreach(array("DESCRIPTION","DEFAULT") as $attr){
298             $items[$name][$attr] = "";
299             if(isset($val[0][$attr])){
300               $items[$name][$attr] = $val[0][$attr][0]['VALUE'];
301             }
302           }
303           $items[$name]['VALUE'] = array();
304           if(isset($val['0']['VALUE'])){
305             foreach($val['0']['VALUE'] as $value){
306               $items[$name]['VALUE'][] = $value['VALUE'];
307             }
308           }
309           $items[$name]['VALUE_CNT'] = count($items[$name]['VALUE']);
310         }
311       }
312     }
313     return($items);
314   }
317   /*! \brief            Set product properties, globally or per host. 
318     @param
319     @return             
320    */
321   public function set_product_properties($productId,$cfg,$hostId = "")
322   {
323     $data = array("productId" => $productId);
325     /* Append host attribute to query data 
326      */
327     if(!empty($hostId)){
328       $data['hostId'] = trim($hostId);
329     }
330    
331     /* Check parameter */ 
332     if(empty($productId)){
333       trigger_error("No valid product id given, check parameter 1.");
334       return(array());
335     }
337     if(!count($cfg)) return;
338     
339     /* Add properties */
340     $data['item'] = array();
341     foreach($cfg as $name => $value){
342       $data['item'][] = "<name>".$name."</name><value>".$value['DEFAULT']."</value>";
343     }  
345     /* Query SI server */
346     $res    = $this->send_data("gosa_opsi_set_product_properties",$this->target,$data,TRUE);
347   }
350   /*! \brief            Adds a given product to a client.
351     @param
352     @return             
353    */
354   public function add_product_to_client($productId,$hostId)
355   {
356     $data = array("productId" => $productId,"hostId" => $hostId);
358     /* Check parameter */ 
359     if(empty($productId)){
360       trigger_error("No valid product id given, check parameter 1.");
361       return;
362     }
363     if(empty($hostId)){
364       trigger_error("No valid host id given, check parameter 2.");
365       return;
366     }
368     /* Query SI server */
369     $res    = $this->send_data("gosa_opsi_add_product_to_client",$this->target,$data,TRUE);
370   }
373   /*! \brief      Removes a given product from a client.
374     @param
375     @return
376    */
377   public function del_product_from_client($productId,$hostId)
378   {
379     $data = array("productId" => $productId,"hostId" => $hostId);
381     /* Check parameter */ 
382     if(empty($productId)){
383       trigger_error("No valid product id given, check parameter 1.");
384       return;
385     }
386     if(empty($hostId)){
387       trigger_error("No valid host id given, check parameter 2.");
388       return;
389     }
391     /* Query SI server */
392     $res    = $this->send_data("gosa_opsi_del_product_from_client",$this->target,$data,TRUE);
393   }
396   /*! \brief            Returns the clients hardware setup.
397     @param
398     @return             
399    */
400   public function get_client_hardware($hostId)
401   {
402     $data = array("hostId" => $hostId);
404     /* Check parameter */ 
405     if(empty($hostId)){
406       trigger_error("No valid host id given, check parameter 1.");
407       return;
408     }
410     /* Query SI server */
411     $res    = $this->send_data("gosa_opsi_get_client_hardware",$this->target,$data,TRUE);
412     if(isset($res['XML'][0]['ITEM'])){
413       return($res['XML'][0]['ITEM']);
414     }
415     return(array());
416   }
419   /*! \brief            Returns the clients software setup.
420     @param
421     @return             
422    */
423   public function get_client_software($hostId)
424   {
425     $data = array("hostId" => $hostId);
427     /* Check parameter */ 
428     if(empty($hostId)){
429       trigger_error("No valid host id given, check parameter 1.");
430       return;
431     }
433     /* Query SI server */
434     $res    = $this->send_data("gosa_opsi_get_client_software",$this->target,$data,TRUE);
435     if(isset($res['XML'][0]['ITEM'])){
436       return($res['XML'][0]['ITEM']);
437     }
438     return(array());
439   }
443   /*! \brief            Deletes the given opsi client.
444     @param
445     @return             
446    */
447   public function del_client($hostId)
448   {
449     $data = array("hostId" => $hostId);
451     /* Check parameter */ 
452     if(empty($hostId)){
453       trigger_error("No valid host id given, check parameter 1.");
454       return;
455     }
457     /* Query SI server */
458     $res    = $this->send_data("gosa_opsi_del_client",$this->target,$data,TRUE);
459     if(isset($res['XML'][0]['ITEM'])){
460       return($res['XML'][0]['ITEM']);
461     }
462     return(array());
463   }
466   /*! \brief            Triggers install/reinstall of an opsi client.
467     @param
468     @return             
469    */
470   public function job_opsi_install_client($hostId,$mac)
471   {
472     $data = array("hostId" => $hostId,"macaddress"=>$mac);
474     /* Check parameter */ 
475     if(empty($hostId)){
476       trigger_error("No valid host id given, check parameter 1.");
477       return;
478     }
480     /* Query SI server */
481     $this->send_data("job_opsi_install_client",$this->target,$data,TRUE);
482   }
484 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
485 ?>