Code

Updated opsi Pruduct Property editor to use ACLs
[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");
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       $obj = 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         "macAddress"  => array(0 => $entry['MAC'][0]['VALUE']),
99         "opsi_notes"  => array(0 => $entry['NOTES'][0]['VALUE']));
101       if(!empty($entry['DESCRIPTION'][0]['VALUE'])){ 
102         $obj["description"] =$entry['DESCRIPTION'][0]['VALUE'];
103       }
104       $data[] = $obj;
105     }
106     return($data);
107   }
110   /*! \brief  Maps all xml to array conversion to an alternative method
111                 then used in the parent class 'gosaSupportDaemon'.
112               The alternative method is able to handle more complex data.
113    */
114   private function xml_to_array($xml,$alternative_method = FALSE)
115   {
116     return(gosaSupportDaemon::xml_to_array($xml,TRUE));
117   }
120   /*! \brief  Trigger an event like wake or install for a specific hostId. 
121    */
122   public function send_action($type,$hostId,$mac)
123   {
124     switch($type){
125       case 'install'  :  $this->job_opsi_install_client($hostId,$mac); break;
126       default         :  trigger_error('Unknown type '.$type.'.');
127     }
128   }
131   /******************
132     SI Communication functions
133    ******************/
137   /*! \brief            Returns a list of all opsi clients.
138     @param
139     @return             
140    */
141   public function list_clients( $hostId = "")
142   {
143     $data   = array();
144     $res    = $this->send_data("gosa_opsi_list_clients",$this->target,$data,TRUE);
145     $items  = array();
146     if(isset($res['XML'][0]['ITEM'])){
147       $items = $res['XML'][0]['ITEM'];
148     }
149     return($items);
150   }
153   /*! \brief            Adds a new opsi client.
154     @param
155     @return             
156    */
157   public function add_client($hostId,$macaddress,$notes,$description)
158   {
159     $data = array("hostId" => $hostId,"macaddress" => $macaddress);
161     if(empty($hostId)){
162       trigger_error("No valid host id given, check parameter 1.");
163       return;
164     }
165   
166     /* Add optional attributes */ 
167     foreach(array("notes","description") as $attr) {
168       if(!empty($$attr)){
169         $data[$attr] = $$attr;
170       }
171     }
173     /* Query SI server */
174     $res    = $this->send_data("gosa_opsi_add_client",$this->target,$data,TRUE);
175   }
178   /*! \brief            Modify an opsi client.
179     @param
180     @return             
181    */
182   public function modify_client($hostId,$mac,$notes,$description)
183   {
184     $data = array("hostId" => $hostId,"mac" => $mac);
186     if(empty($hostId)){
187       trigger_error("No valid host id given, check parameter 1.");
188       return;
189     }
190   
191     /* Add optional attributes */ 
192     foreach(array("notes","description") as $attr) {
193       $data[$attr] = $$attr;
194     }
196     /* Query SI server */
197     $res = $this->send_data("gosa_opsi_modify_client",$this->target,$data,TRUE);
198   }
202   /*! \brief            Returns a list of netboot products.
203     @param
204     @return             
205    */
206   public function get_netboot_products($host = "")
207   {
208     /* Append host attribute to query data 
209      */
210     $data = array();
211     if(!empty($host)){
212       $data['hostId'] = trim($host);
213     }
215     $res    = $this->send_data("gosa_opsi_get_netboot_products",$this->target,$data,TRUE);
216     $items = array();
217     if(isset($res['XML'][0]['ITEM'])){
218       foreach($res['XML'][0]['ITEM'] as $entry){
219         $e = array("DESC" => $entry['DESCRIPTION'][0]['VALUE'],
220                    "NAME" => $entry['PRODUCTID'][0]['VALUE']);
221         $items[$entry['PRODUCTID'][0]['VALUE']] = $e;
222       }
223     }
224     return($items);
225   }
228   /*! \brief            Returns a list of all local products.
229     @param
230     @return             
231    */
232   public function get_local_products($host = "")
233   {
234     /* Append host attribute to query data 
235      */
236     $data = array();
237     if(!empty($host)){
238       $data['hostId'] = trim($host);
239     }
241     $res    = $this->send_data("gosa_opsi_get_local_products",$this->target,$data,TRUE);
242     $items = array();
243     if(isset($res['XML'][0]['ITEM'])){
244       foreach($res['XML'][0]['ITEM'] as $entry){
245         $e = array("DESC" => $entry['DESCRIPTION'][0]['VALUE'],
246                    "NAME" => $entry['PRODUCTID'][0]['VALUE']);
247         $items[$entry['PRODUCTID'][0]['VALUE']] = $e; 
248       }
249     }
250     return($items);
251   }
254   /*! \brief            Returns a list of all product properties. \ 
255     .           Additionally you can specify the host parameter to \
256     .           get host specific product properties
257     @param
258     @return             
259    */
260   public function get_product_properties($productId,$hostId = "")
261   {
262     $data = array("productId" => $productId);
264     /* Append host attribute to query data 
265      */
266     if(!empty($hostId)){
267       $data['hostId'] = trim($hostId);
268     }
269    
270     /* Check parameter */ 
271     if(empty($productId)){
272       trigger_error("No valid product id given, check parameter 1.");
273       return(array());
274     }
276     /* Query SI server */
277     $res    = $this->send_data("gosa_opsi_get_product_properties",$this->target,$data,TRUE);
278     $items  = array();
279     if(isset($res['XML'][0]['ITEM'])){   
280       foreach($res['XML'][0]['ITEM'] as $entry){
281         foreach($entry as $name => $val){
283           foreach(array("DESCRIPTION","DEFAULT") as $attr){
284             $items[$name][$attr] = "";
285             if(isset($val[0][$attr])){
286               $items[$name][$attr] = $val[0][$attr][0]['VALUE'];
287             }
288           }
289           $items[$name]['VALUE'] = array();
290           if(isset($val['0']['VALUE'])){
291             foreach($val['0']['VALUE'] as $value){
292               $items[$name]['VALUE'][] = $value['VALUE'];
293             }
294           }
295           $items[$name]['VALUE_CNT'] = count($items[$name]['VALUE']);
296         }
297       }
298     }
299     return($items);
300   }
303   /*! \brief            Set product properties, globally or per host. 
304     @param
305     @return             
306    */
307   public function set_product_properties($productId,$cfg,$hostId = "")
308   {
309     $data = array("productId" => $productId);
311     /* Append host attribute to query data 
312      */
313     if(!empty($hostId)){
314       $data['hostId'] = trim($hostId);
315     }
316    
317     /* Check parameter */ 
318     if(empty($productId)){
319       trigger_error("No valid product id given, check parameter 1.");
320       return(array());
321     }
323     if(!count($cfg)) return;
324     
325     /* Add properties */
326     $data['item'] = array();
327     foreach($cfg as $name => $value){
328       $data['item'][] = "<name>".$name."</name><value>".$value['DEFAULT']."</value>";
329     }  
331     /* Query SI server */
332     $res    = $this->send_data("gosa_opsi_set_product_properties",$this->target,$data,TRUE);
333   }
336   /*! \brief            Adds a given product to a client.
337     @param
338     @return             
339    */
340   public function add_product_to_client($productId,$hostId)
341   {
342     $data = array("productId" => $productId,"hostId" => $hostId);
344     /* Check parameter */ 
345     if(empty($productId)){
346       trigger_error("No valid product id given, check parameter 1.");
347       return;
348     }
349     if(empty($hostId)){
350       trigger_error("No valid host id given, check parameter 2.");
351       return;
352     }
354     /* Query SI server */
355     $res    = $this->send_data("gosa_opsi_add_product_to_client",$this->target,$data,TRUE);
356   }
359   /*! \brief      Removes a given product from a client.
360     @param
361     @return
362    */
363   public function del_product_from_client($productId,$hostId)
364   {
365     $data = array("productId" => $productId,"hostId" => $hostId);
367     /* Check parameter */ 
368     if(empty($productId)){
369       trigger_error("No valid product id given, check parameter 1.");
370       return;
371     }
372     if(empty($hostId)){
373       trigger_error("No valid host id given, check parameter 2.");
374       return;
375     }
377     /* Query SI server */
378     $res    = $this->send_data("gosa_opsi_del_product_from_client",$this->target,$data,TRUE);
379   }
382   /*! \brief            Returns the clients hardware setup.
383     @param
384     @return             
385    */
386   public function get_client_hardware($hostId)
387   {
388     $data = array("hostId" => $hostId);
390     /* Check parameter */ 
391     if(empty($hostId)){
392       trigger_error("No valid host id given, check parameter 1.");
393       return;
394     }
396     /* Query SI server */
397     $res    = $this->send_data("gosa_opsi_get_client_hardware",$this->target,$data,TRUE);
398     if(isset($res['XML'][0]['ITEM'])){
399       return($res['XML'][0]['ITEM']);
400     }
401     return(array());
402   }
405   /*! \brief            Returns the clients software setup.
406     @param
407     @return             
408    */
409   public function get_client_software($hostId)
410   {
411     $data = array("hostId" => $hostId);
413     /* Check parameter */ 
414     if(empty($hostId)){
415       trigger_error("No valid host id given, check parameter 1.");
416       return;
417     }
419     /* Query SI server */
420     $res    = $this->send_data("gosa_opsi_get_client_software",$this->target,$data,TRUE);
421     if(isset($res['XML'][0]['ITEM'])){
422       return($res['XML'][0]['ITEM']);
423     }
424     return(array());
425   }
429   /*! \brief            Deletes the given opsi client.
430     @param
431     @return             
432    */
433   public function del_client($hostId)
434   {
435     $data = array("hostId" => $hostId);
437     /* Check parameter */ 
438     if(empty($hostId)){
439       trigger_error("No valid host id given, check parameter 1.");
440       return;
441     }
443     /* Query SI server */
444     $res    = $this->send_data("gosa_opsi_del_client",$this->target,$data,TRUE);
445     if(isset($res['XML'][0]['ITEM'])){
446       return($res['XML'][0]['ITEM']);
447     }
448     return(array());
449   }
452   /*! \brief            Triggers install/reinstall of an opsi client.
453     @param
454     @return             
455    */
456   public function job_opsi_install_client($hostId,$mac)
457   {
458     $data = array("hostId" => $hostId,"macaddress"=>$mac);
460     /* Check parameter */ 
461     if(empty($hostId)){
462       trigger_error("No valid host id given, check parameter 1.");
463       return;
464     }
466     /* Query SI server */
467     $this->send_data("job_opsi_install_client",$this->target,$data,TRUE);
468   }
470 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
471 ?>