Code

replaced plugbottom by pluin-actions
[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   }
144   
145   public function job_opsi_activate_client($id,$mac)
146   {
147     $events = DaemonEvent::get_event_types(SYSTEM_EVENT | HIDDEN_EVENT);
148     $o_queue = new gosaSupportDaemon();
149     if(isset($events['TRIGGERED']['DaemonEvent_activate'])){
150       $evt = $events['TRIGGERED']['DaemonEvent_activate'];
151       $tmp = new $evt['CLASS_NAME']($this->config);
152       $tmp->set_type(TRIGGERED_EVENT);
153       $tmp->add_targets(array($mac));
154       if(!$o_queue->append($tmp)){
155         msg_dialog::display(_("Service infrastructure"),msgPool::siError($o_queue->get_error()),ERROR_DIALOG);
156       }
157     }
158   }
162   /******************
163     SI Communication functions
164    ******************/
168   /*! \brief            Returns a list of all opsi clients.
169     @param
170     @return             
171    */
172   public function list_clients( $hostId = "")
173   {
174     $data   = array();
175     $res    = $this->send_data("gosa_opsi_list_clients",$this->target,$data,TRUE);
176     $items  = array();
177     if(isset($res['XML'][0]['ITEM'])){
178       $items = $res['XML'][0]['ITEM'];
179     }
180     return($items);
181   }
184   /*! \brief            Adds a new opsi client.
185     @param
186     @return             
187    */
188   public function add_client($hostId,$macaddress,$notes,$description)
189   {
190     $data = array("hostId" => $hostId,"macaddress" => $macaddress);
192     if(empty($hostId)){
193       trigger_error("No valid host id given, check parameter 1.");
194       return;
195     }
196   
197     /* Add optional attributes */ 
198     foreach(array("notes","description") as $attr) {
199       if(!empty($$attr)){
200         $data[$attr] = $$attr;
201       }
202     }
204     /* Query SI server */
205     $res    = $this->send_data("gosa_opsi_add_client",$this->target,$data,TRUE);
206   }
209   /*! \brief            Modify an opsi client.
210     @param
211     @return             
212    */
213   public function modify_client($hostId,$mac,$notes,$description)
214   {
215     $data = array("hostId" => $hostId,"mac" => $mac);
217     if(empty($hostId)){
218       trigger_error("No valid host id given, check parameter 1.");
219       return;
220     }
221   
222     /* Add optional attributes */ 
223     foreach(array("notes","description") as $attr) {
224       $data[$attr] = $$attr;
225     }
227     /* Query SI server */
228     $res = $this->send_data("gosa_opsi_modify_client",$this->target,$data,TRUE);
229   }
233   /*! \brief            Returns a list of netboot products.
234     @param
235     @return             
236    */
237   public function get_netboot_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_netboot_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 local products.
260     @param
261     @return             
262    */
263   public function get_local_products($host = "")
264   {
265     /* Append host attribute to query data 
266      */
267     $data = array();
268     if(!empty($host)){
269       $data['hostId'] = trim($host);
270     }
272     $res    = $this->send_data("gosa_opsi_get_local_products",$this->target,$data,TRUE);
273     $items = array();
274     if(isset($res['XML'][0]['ITEM'])){
275       foreach($res['XML'][0]['ITEM'] as $entry){
276         $e = array("DESC" => $entry['DESCRIPTION'][0]['VALUE'],
277                    "NAME" => $entry['PRODUCTID'][0]['VALUE']);
278         $items[$entry['PRODUCTID'][0]['VALUE']] = $e; 
279       }
280     }
281     return($items);
282   }
285   /*! \brief            Returns a list of netboot products.
286     @param
287     @return             
288    */
289   public function get_full_product_host_information($host = "")
290   {
291     /* Append host attribute to query data 
292      */
293     $data = array();
294     if(!empty($host)){
295       $data['hostId'] = trim($host);
296     }
298     $res    = $this->send_data("gosa_opsi_get_full_product_host_information",$this->target,$data,TRUE);
299     $items = array();
300     $attrs = array("TYPE","PRIORITY","ONCESCRIPT","LICENSEREQUIRED","PACKAGEVERSION","PRODUCTVERSION",
301         "ADVICE","SETUPSCRIPT","WINDOWSSOFTWAREIDS","PXECONFIGTEMPLATE","NAME","CREATIONTIMESTAMP", "TYPE",
302         "ALWAYSSCRIPT","PRODUCTID","DESCRIPTION","UNINSTALLSCRIPT","UPDATESCRIPT","PRODUCTCLASSNAMES");
304     if(isset($res['XML'][0]['ITEM'])){
305       foreach($res['XML'][0]['ITEM'] as $entry){
306         $e = array();
307         foreach($attrs as $attr){
308           if(isset($entry[$attr])){
309             foreach($entry[$attr] as $key => $value){
310               if(isset($value['VALUE'])){
311                 $e['data'][$attr] = $value['VALUE'];
312               }elseif($value['ELEMENT']){
313                 foreach($value['ELEMENT'] as $element){
314                   $e['data'][$attr][] = $element['VALUE'];
315                 }
316               }
317             }
318           }
319         }
321         $e["configurable"] = FALSE;
322         if(isset($entry['PROPERTIES']) && count($entry['PROPERTIES'])){
323           $e["configurable"] = TRUE;
324           $p_data = array();
326           foreach($entry['PROPERTIES'][0] as $p_name => $p_values){
327             if(empty($p_values)) continue;
328             $p_data[$p_name]= array();
329             foreach(array('CURRENT','DEFAULT','DESCRIPTION') as $p_tmp){
330               if(isset($p_values[0][$p_tmp])){
331                 if(isset($p_values[0][$p_tmp][0]['VALUE'])){
332                   $p_data[$p_name][$p_tmp] = $p_values[0][$p_tmp][0]['VALUE'];
333                 }
334               }
335             }
337             if(isset($p_values[0]['VALUES'][0]['ELEMENT'])){
338               foreach($p_values[0]['VALUES'][0]['ELEMENT'] as $val){
339                 $p_data[$p_name]['VALUE'][] = $val['VALUE'];
340               }
341               $p_data[$p_name]['VALUE_CNT'] = count($p_data[$p_name]['VALUE']);
342             } 
343           }
345           $e["data"]['PROPERTIES'] = $p_data;
346         }
349         $e["installed"] = FALSE;
350         if(isset($entry['ACTIONREQUEST'])){
351           $e["installed"] = TRUE;
352         }
354         $e["requires_licence"] = FALSE;
355         if(isset($e['data']['LICENSEREQUIRED']) && preg_match("/true/i",$e['data']['LICENSEREQUIRED'])){
356           $e["requires_licence"] = TRUE;
357         }
358         $items[$entry['PRODUCTID'][0]['VALUE']] = $e;
359       }
360     }
361     return($items);
362   }
365   /*! \brief            Returns a list of all product properties. \ 
366     .           Additionally you can specify the host parameter to \
367     .           get host specific product properties
368     @param
369     @return             
370    */
371   public function get_product_properties($productId,$hostId = "")
372   {
373     $data = array("productId" => $productId);
375     /* Append host attribute to query data 
376      */
377     if(!empty($hostId)){
378       $data['hostId'] = trim($hostId);
379     }
380    
381     /* Check parameter */ 
382     if(empty($productId)){
383       trigger_error("No valid product id given, check parameter 1.");
384       return(array());
385     }
387     /* Query SI server */
388     $res    = $this->send_data("gosa_opsi_get_product_properties",$this->target,$data,TRUE);
389     $items  = array();
390     if(isset($res['XML'][0]['ITEM'])){   
391       foreach($res['XML'][0]['ITEM'] as $entry){
392         foreach($entry as $name => $val){
394           foreach(array("DESCRIPTION","CURRENT") as $attr){
395             $items[$name][$attr] = "";
396             if(isset($val[0][$attr])){
397               $items[$name][$attr] = $val[0][$attr][0]['VALUE'];
398             }
399           }
400           $items[$name]['VALUE'] = array();
401           if(isset($val['0']['VALUE'])){
402             foreach($val['0']['VALUE'] as $value){
403               $items[$name]['VALUE'][] = $value['VALUE'];
404             }
405           }
406           $items[$name]['VALUE_CNT'] = count($items[$name]['VALUE']);
407         }
408       }
409     }
410     return($items);
411   }
414   /*! \brief            Set product properties, globally or per host. 
415     @param
416     @return             
417    */
418   public function set_product_properties($productId,$cfg,$hostId = "")
419   {
420     $data = array("productId" => $productId);
422     /* Append host attribute to query data 
423      */
424     if(!empty($hostId)){
425       $data['hostId'] = trim($hostId);
426     }
427    
428     /* Check parameter */ 
429     if(empty($productId)){
430       trigger_error("No valid product id given, check parameter 1.");
431       return(array());
432     }
434     if(!count($cfg)) return;
435     
436     /* Add properties */
437     $data['item'] = array();
438     foreach($cfg as $name => $value){
439       $data['item'][] = "<name>".$name."</name><value>".$value['CURRENT']."</value>";
440     }  
442     /* Query SI server */
443     $res    = $this->send_data("gosa_opsi_set_product_properties",$this->target,$data,TRUE);
444   }
447   /*! \brief            Adds a given product to a client.
448     @param
449     @return             
450    */
451   public function add_product_to_client($productId,$hostId)
452   {
453     $data = array("productId" => $productId,"hostId" => $hostId);
455     /* Check parameter */ 
456     if(empty($productId)){
457       trigger_error("No valid product id given, check parameter 1.");
458       return;
459     }
460     if(empty($hostId)){
461       trigger_error("No valid host id given, check parameter 2.");
462       return;
463     }
465     /* Query SI server */
466     $res    = $this->send_data("gosa_opsi_add_product_to_client",$this->target,$data,TRUE);
467   }
470   /*! \brief      Removes a given product from a client.
471     @param
472     @return
473    */
474   public function del_product_from_client($productId,$hostId)
475   {
476     $data = array("productId" => $productId,"hostId" => $hostId);
478     /* Check parameter */ 
479     if(empty($productId)){
480       trigger_error("No valid product id given, check parameter 1.");
481       return;
482     }
483     if(empty($hostId)){
484       trigger_error("No valid host id given, check parameter 2.");
485       return;
486     }
488     /* Query SI server */
489     $res    = $this->send_data("gosa_opsi_del_product_from_client",$this->target,$data,TRUE);
490   }
493   /*! \brief            Returns the clients hardware setup.
494     @param
495     @return             
496    */
497   public function get_client_hardware($hostId)
498   {
499     $data = array("hostId" => $hostId);
501     /* Check parameter */ 
502     if(empty($hostId)){
503       trigger_error("No valid host id given, check parameter 1.");
504       return;
505     }
507     /* Query SI server */
508     $res    = $this->send_data("gosa_opsi_get_client_hardware",$this->target,$data,TRUE);
509     if(isset($res['XML'][0]['ITEM'])){
510       return($res['XML'][0]['ITEM']);
511     }
512     return(array());
513   }
516   /*! \brief            Returns the clients software setup.
517     @param
518     @return             
519    */
520   public function get_client_software($hostId)
521   {
522     $data = array("hostId" => $hostId);
524     /* Check parameter */ 
525     if(empty($hostId)){
526       trigger_error("No valid host id given, check parameter 1.");
527       return;
528     }
530     /* Query SI server */
531     $res    = $this->send_data("gosa_opsi_get_client_software",$this->target,$data,TRUE);
532     if(isset($res['XML'][0]['ITEM'])){
533       return($res['XML'][0]['ITEM']);
534     }
535     return(array());
536   }
540   /*! \brief            Deletes the given opsi client.
541     @param
542     @return             
543    */
544   public function del_client($hostId)
545   {
546     $data = array("hostId" => $hostId);
548     /* Check parameter */ 
549     if(empty($hostId)){
550       trigger_error("No valid host id given, check parameter 1.");
551       return;
552     }
554     /* Query SI server */
555     $res    = $this->send_data("gosa_opsi_del_client",$this->target,$data,TRUE);
556     if(isset($res['XML'][0]['ITEM'])){
557       return($res['XML'][0]['ITEM']);
558     }
559     return(array());
560   }
563   /*! \brief            Triggers install/reinstall of an opsi client.
564     @param
565     @return             
566    */
567   public function job_opsi_install_client($hostId,$mac)
568   {
569     $data = array("hostId" => $hostId,"macaddress"=>$mac);
571     /* Check parameter */ 
572     if(empty($hostId)){
573       trigger_error("No valid host id given, check parameter 1.");
574       return;
575     }
577     /* Query SI server */
578     $this->send_data("job_opsi_install_client",$this->target,$data,TRUE);
579   }
581 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
582 ?>