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");
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     $ui = get_userinfo();
93     foreach($res as $entry){
94       if(!isset($entry['MAC'][0]['VALUE'])) $entry['MAC'][0]['VALUE'] = "";
95       $obj = array(
96         "dn"          => "opsi:=".$entry['NAME'][0]['VALUE'].",".get_ou("sambaMachineAccountRDN").$this->config->current['BASE'],
97         "objectClass" => array("gosa_opsi_client"),
98         "cn"          => array(0 => $entry['NAME'][0]['VALUE']),
99         "macAddress"  => array(0 => $entry['MAC'][0]['VALUE']),
100         "opsi_notes"  => array(0 => $entry['NOTES'][0]['VALUE']));
102       /* Check permissions */
103       $opsi_acl = $ui->get_permissions($obj['dn'],"opsi/opsiGeneric");
104       if(preg_match("/r/",$opsi_acl)){
105         if(!empty($entry['DESCRIPTION'][0]['VALUE'])){ 
106           $obj["description"] =$entry['DESCRIPTION'][0]['VALUE'];
107         }
108         $data[] = $obj;
109       }
110     }
111     return($data);
112   }
115   /*! \brief  Maps all xml to array conversion to an alternative method
116                 then used in the parent class 'gosaSupportDaemon'.
117               The alternative method is able to handle more complex data.
118    */
119   private function xml_to_array($xml,$alternative_method = FALSE)
120   {
121     return(gosaSupportDaemon::xml_to_array($xml,TRUE));
122   }
125   /*! \brief  Trigger an event like wake or install for a specific hostId. 
126    */
127   public function send_action($type,$hostId,$mac)
128   {
129     switch($type){
130       case 'install'  :  $this->job_opsi_install_client($hostId,$mac); break;
131       default         :  trigger_error('Unknown type '.$type.'.');
132     }
133   }
136   /******************
137     SI Communication functions
138    ******************/
142   /*! \brief            Returns a list of all opsi clients.
143     @param
144     @return             
145    */
146   public function list_clients( $hostId = "")
147   {
148     $data   = array();
149     $res    = $this->send_data("gosa_opsi_list_clients",$this->target,$data,TRUE);
150     $items  = array();
151     if(isset($res['XML'][0]['ITEM'])){
152       $items = $res['XML'][0]['ITEM'];
153     }
154     return($items);
155   }
158   /*! \brief            Adds a new opsi client.
159     @param
160     @return             
161    */
162   public function add_client($hostId,$macaddress,$notes,$description)
163   {
164     $data = array("hostId" => $hostId,"macaddress" => $macaddress);
166     if(empty($hostId)){
167       trigger_error("No valid host id given, check parameter 1.");
168       return;
169     }
170   
171     /* Add optional attributes */ 
172     foreach(array("notes","description") as $attr) {
173       if(!empty($$attr)){
174         $data[$attr] = $$attr;
175       }
176     }
178     /* Query SI server */
179     $res    = $this->send_data("gosa_opsi_add_client",$this->target,$data,TRUE);
180   }
183   /*! \brief            Modify an opsi client.
184     @param
185     @return             
186    */
187   public function modify_client($hostId,$mac,$notes,$description)
188   {
189     $data = array("hostId" => $hostId,"mac" => $mac);
191     if(empty($hostId)){
192       trigger_error("No valid host id given, check parameter 1.");
193       return;
194     }
195   
196     /* Add optional attributes */ 
197     foreach(array("notes","description") as $attr) {
198       $data[$attr] = $$attr;
199     }
201     /* Query SI server */
202     $res = $this->send_data("gosa_opsi_modify_client",$this->target,$data,TRUE);
203   }
207   /*! \brief            Returns a list of netboot products.
208     @param
209     @return             
210    */
211   public function get_netboot_products($host = "")
212   {
213     /* Append host attribute to query data 
214      */
215     $data = array();
216     if(!empty($host)){
217       $data['hostId'] = trim($host);
218     }
220     $res    = $this->send_data("gosa_opsi_get_netboot_products",$this->target,$data,TRUE);
221     $items = array();
222     if(isset($res['XML'][0]['ITEM'])){
223       foreach($res['XML'][0]['ITEM'] as $entry){
224         $e = array("DESC" => $entry['DESCRIPTION'][0]['VALUE'],
225                    "NAME" => $entry['PRODUCTID'][0]['VALUE']);
226         $items[$entry['PRODUCTID'][0]['VALUE']] = $e;
227       }
228     }
229     return($items);
230   }
233   /*! \brief            Returns a list of all local products.
234     @param
235     @return             
236    */
237   public function get_local_products($host = "")
238   {
239     /* Append host attribute to query data 
240      */
241     $data = array();
242     if(!empty($host)){
243       $data['hostId'] = trim($host);
244     }
246     $res    = $this->send_data("gosa_opsi_get_local_products",$this->target,$data,TRUE);
247     $items = array();
248     if(isset($res['XML'][0]['ITEM'])){
249       foreach($res['XML'][0]['ITEM'] as $entry){
250         $e = array("DESC" => $entry['DESCRIPTION'][0]['VALUE'],
251                    "NAME" => $entry['PRODUCTID'][0]['VALUE']);
252         $items[$entry['PRODUCTID'][0]['VALUE']] = $e; 
253       }
254     }
255     return($items);
256   }
259   /*! \brief            Returns a list of all product properties. \ 
260     .           Additionally you can specify the host parameter to \
261     .           get host specific product properties
262     @param
263     @return             
264    */
265   public function get_product_properties($productId,$hostId = "")
266   {
267     $data = array("productId" => $productId);
269     /* Append host attribute to query data 
270      */
271     if(!empty($hostId)){
272       $data['hostId'] = trim($hostId);
273     }
274    
275     /* Check parameter */ 
276     if(empty($productId)){
277       trigger_error("No valid product id given, check parameter 1.");
278       return(array());
279     }
281     /* Query SI server */
282     $res    = $this->send_data("gosa_opsi_get_product_properties",$this->target,$data,TRUE);
283     $items  = array();
284     if(isset($res['XML'][0]['ITEM'])){   
285       foreach($res['XML'][0]['ITEM'] as $entry){
286         foreach($entry as $name => $val){
288           foreach(array("DESCRIPTION","DEFAULT") as $attr){
289             $items[$name][$attr] = "";
290             if(isset($val[0][$attr])){
291               $items[$name][$attr] = $val[0][$attr][0]['VALUE'];
292             }
293           }
294           $items[$name]['VALUE'] = array();
295           if(isset($val['0']['VALUE'])){
296             foreach($val['0']['VALUE'] as $value){
297               $items[$name]['VALUE'][] = $value['VALUE'];
298             }
299           }
300           $items[$name]['VALUE_CNT'] = count($items[$name]['VALUE']);
301         }
302       }
303     }
304     return($items);
305   }
308   /*! \brief            Set product properties, globally or per host. 
309     @param
310     @return             
311    */
312   public function set_product_properties($productId,$cfg,$hostId = "")
313   {
314     $data = array("productId" => $productId);
316     /* Append host attribute to query data 
317      */
318     if(!empty($hostId)){
319       $data['hostId'] = trim($hostId);
320     }
321    
322     /* Check parameter */ 
323     if(empty($productId)){
324       trigger_error("No valid product id given, check parameter 1.");
325       return(array());
326     }
328     if(!count($cfg)) return;
329     
330     /* Add properties */
331     $data['item'] = array();
332     foreach($cfg as $name => $value){
333       $data['item'][] = "<name>".$name."</name><value>".$value['DEFAULT']."</value>";
334     }  
336     /* Query SI server */
337     $res    = $this->send_data("gosa_opsi_set_product_properties",$this->target,$data,TRUE);
338   }
341   /*! \brief            Adds a given product to a client.
342     @param
343     @return             
344    */
345   public function add_product_to_client($productId,$hostId)
346   {
347     $data = array("productId" => $productId,"hostId" => $hostId);
349     /* Check parameter */ 
350     if(empty($productId)){
351       trigger_error("No valid product id given, check parameter 1.");
352       return;
353     }
354     if(empty($hostId)){
355       trigger_error("No valid host id given, check parameter 2.");
356       return;
357     }
359     /* Query SI server */
360     $res    = $this->send_data("gosa_opsi_add_product_to_client",$this->target,$data,TRUE);
361   }
364   /*! \brief      Removes a given product from a client.
365     @param
366     @return
367    */
368   public function del_product_from_client($productId,$hostId)
369   {
370     $data = array("productId" => $productId,"hostId" => $hostId);
372     /* Check parameter */ 
373     if(empty($productId)){
374       trigger_error("No valid product id given, check parameter 1.");
375       return;
376     }
377     if(empty($hostId)){
378       trigger_error("No valid host id given, check parameter 2.");
379       return;
380     }
382     /* Query SI server */
383     $res    = $this->send_data("gosa_opsi_del_product_from_client",$this->target,$data,TRUE);
384   }
387   /*! \brief            Returns the clients hardware setup.
388     @param
389     @return             
390    */
391   public function get_client_hardware($hostId)
392   {
393     $data = array("hostId" => $hostId);
395     /* Check parameter */ 
396     if(empty($hostId)){
397       trigger_error("No valid host id given, check parameter 1.");
398       return;
399     }
401     /* Query SI server */
402     $res    = $this->send_data("gosa_opsi_get_client_hardware",$this->target,$data,TRUE);
403     if(isset($res['XML'][0]['ITEM'])){
404       return($res['XML'][0]['ITEM']);
405     }
406     return(array());
407   }
410   /*! \brief            Returns the clients software setup.
411     @param
412     @return             
413    */
414   public function get_client_software($hostId)
415   {
416     $data = array("hostId" => $hostId);
418     /* Check parameter */ 
419     if(empty($hostId)){
420       trigger_error("No valid host id given, check parameter 1.");
421       return;
422     }
424     /* Query SI server */
425     $res    = $this->send_data("gosa_opsi_get_client_software",$this->target,$data,TRUE);
426     if(isset($res['XML'][0]['ITEM'])){
427       return($res['XML'][0]['ITEM']);
428     }
429     return(array());
430   }
434   /*! \brief            Deletes the given opsi client.
435     @param
436     @return             
437    */
438   public function del_client($hostId)
439   {
440     $data = array("hostId" => $hostId);
442     /* Check parameter */ 
443     if(empty($hostId)){
444       trigger_error("No valid host id given, check parameter 1.");
445       return;
446     }
448     /* Query SI server */
449     $res    = $this->send_data("gosa_opsi_del_client",$this->target,$data,TRUE);
450     if(isset($res['XML'][0]['ITEM'])){
451       return($res['XML'][0]['ITEM']);
452     }
453     return(array());
454   }
457   /*! \brief            Triggers install/reinstall of an opsi client.
458     @param
459     @return             
460    */
461   public function job_opsi_install_client($hostId,$mac)
462   {
463     $data = array("hostId" => $hostId,"macaddress"=>$mac);
465     /* Check parameter */ 
466     if(empty($hostId)){
467       trigger_error("No valid host id given, check parameter 1.");
468       return;
469     }
471     /* Query SI server */
472     $this->send_data("job_opsi_install_client",$this->target,$data,TRUE);
473   }
475 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
476 ?>