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   __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;
49   /*! \brief            Create opsi object.
50     @param
51     @return             
52    */
53   public function __construct($config)
54   {
55     $this->config = $config;
56     gosaSupportDaemon::__construct($config);
57     $this->target = "00:01:6c:9d:b9:fa";
58   }
60   
61   public function enabled()
62   {
63     return(TRUE);
64   }
66   /******************
67     Opsi handling 
68    ******************/
70   function get_hosts_for_system_management()
71   {
72     $res = $this->list_clients();
73     $data = array();
74     foreach($res as $entry){
75       if(!isset($entry['MAC'][0]['VALUE'])) $entry['MAC'][0]['VALUE'] = "";
76       $data[] = array(
77         "dn"          => "opsi:=".$entry['NAME'][0]['VALUE'].",".get_ou("winstations").$this->config->current['BASE'],
78         "objectClass" => array("gosa_opsi_client"),
79         "cn"          => array(0 => $entry['NAME'][0]['VALUE']),
80         "description" => array(0 => $entry['DESCRIPTION'][0]['VALUE']),
81         "macAddress"  => array(0 => $entry['MAC'][0]['VALUE']),
82         "opsi_notes"  => array(0 => $entry['NOTES'][0]['VALUE']));
83     }
84     return($data);
85   }
88   /*! \brief  Maps all xml to array conversion to an alternative method
89                 then used in the parent class 'gosaSupportDaemon'.
90               The alternative method is able to handle more complex data.
91    */
92   private function xml_to_array($xml,$alternative_method = FALSE)
93   {
94     return(gosaSupportDaemon::xml_to_array($xml,TRUE));
95   }
98   /*! \brief  Trigger an event like wake or install for a specific hostId. 
99    */
100   public function send_action($type,$hostId,$mac)
101   {
102     switch($type){
103       case 'install'  :  $this->job_opsi_install_client($hostId,$mac); break;
104       default         :  trigger_error('Unknown type '.$type.'.');
105     }
106   }
109   /******************
110     SI Communication functions
111    ******************/
115   /*! \brief            Returns a list of all opsi clients.
116     @param
117     @return             
118    */
119   public function list_clients( $hostId = "")
120   {
121     $data   = array();
122     $res    = $this->send_data("gosa_opsi_list_clients",$this->target,$data,TRUE);
123     $items  = array();
124     if(isset($res['XML'][0]['ITEM'])){
125       $items = $res['XML'][0]['ITEM'];
126     }
127     return($items);
128   }
131   /*! \brief            Adds a new opsi client.
132     @param
133     @return             
134    */
135   public function add_client($hostId,$macaddress,$notes,$description)
136   {
137     $data = array("hostId" => $hostId,"macaddress" => $macaddress);
139     if(empty($hostId)){
140       trigger_error("No valid host id given, check parameter 1.");
141       return;
142     }
143   
144     /* Add optional attributes */ 
145     foreach(array("notes","description") as $attr) {
146       if(!empty($$attr)){
147         $data[$attr] = $$attr;
148       }
149     }
151     /* Query SI server */
152     $res    = $this->send_data("gosa_opsi_add_client",$this->target,$data,TRUE);
153   }
156   /*! \brief            Modify an opsi client.
157     @param
158     @return             
159    */
160   public function modify_client($hostId,$mac,$notes,$description)
161   {
162     $data = array("hostId" => $hostId,"mac" => $mac);
164     if(empty($hostId)){
165       trigger_error("No valid host id given, check parameter 1.");
166       return;
167     }
168   
169     /* Add optional attributes */ 
170     foreach(array("notes","description") as $attr) {
171       if(!empty($$attr)){
172         $data[$attr] = $$attr;
173       }
174     }
176     /* Query SI server */
177     $res = $this->send_data("gosa_opsi_modify_client",$this->target,$data,TRUE);
178   }
182   /*! \brief            Returns a list of netboot products.
183     @param
184     @return             
185    */
186   public function get_netboot_products($host = "")
187   {
188     /* Append host attribute to query data 
189      */
190     $data = array();
191     if(!empty($host)){
192       $data['hostId'] = trim($host);
193     }
195     $res    = $this->send_data("gosa_opsi_get_netboot_products",$this->target,$data,TRUE);
196     $items = array();
197     if(isset($res['XML'][0]['ITEM'])){
198       foreach($res['XML'][0]['ITEM'] as $entry){
199         $e = array("DESC" => $entry['DESCRIPTION'][0]['VALUE'],
200                    "NAME" => $entry['PRODUCTID'][0]['VALUE']);
201         $items[$entry['PRODUCTID'][0]['VALUE']] = $e;
202       }
203     }
204     return($items);
205   }
208   /*! \brief            Returns a list of all local products.
209     @param
210     @return             
211    */
212   public function get_local_products($host = "")
213   {
214     /* Append host attribute to query data 
215      */
216     $data = array();
217     if(!empty($host)){
218       $data['hostId'] = trim($host);
219     }
221     $res    = $this->send_data("gosa_opsi_get_local_products",$this->target,$data,TRUE);
222     $items = array();
223     if(isset($res['XML'][0]['ITEM'])){
224       foreach($res['XML'][0]['ITEM'] as $entry){
225         $e = array("DESC" => $entry['DESCRIPTION'][0]['VALUE'],
226                    "NAME" => $entry['PRODUCTID'][0]['VALUE']);
227         $items[$entry['PRODUCTID'][0]['VALUE']] = $e; 
228       }
229     }
230     return($items);
231   }
234   /*! \brief            Returns a list of all product properties. \ 
235     .           Additionally you can specify the host parameter to \
236     .           get host specific product properties
237     @param
238     @return             
239    */
240   public function get_product_properties($productId,$hostId = "")
241   {
242     $data = array("productId" => $productId);
244     /* Append host attribute to query data 
245      */
246     if(!empty($hostId)){
247       $data['hostId'] = trim($hostId);
248     }
249    
250     /* Check parameter */ 
251     if(empty($productId)){
252       trigger_error("No valid product id given, check parameter 1.");
253       return(array());
254     }
256     /* Query SI server */
257     $res    = $this->send_data("gosa_opsi_get_product_properties",$this->target,$data,TRUE);
258     $items  = array();
259     if(isset($res['XML'][0]['ITEM'])){   
260       foreach($res['XML'][0]['ITEM'] as $entry){
261         foreach($entry as $name => $val){
263           foreach(array("DESCRIPTION","DEFAULT") as $attr){
264             $items[$name][$attr] = "";
265             if(isset($val[0][$attr])){
266               $items[$name][$attr] = $val[0][$attr][0]['VALUE'];
267             }
268           }
269           $items[$name]['VALUE'] = array();
270           if(isset($val['0']['VALUE'])){
271             foreach($val['0']['VALUE'] as $value){
272               $items[$name]['VALUE'][] = $value['VALUE'];
273             }
274           }
275           $items[$name]['VALUE_CNT'] = count($items[$name]['VALUE']);
276         }
277       }
278     }
279     return($items);
280   }
283   /*! \brief            Set product properties, globally or per host. 
284     @param
285     @return             
286    */
287   public function set_product_properties($productId,$cfg,$hostId = "")
288   {
289     $data = array("productId" => $productId);
291     /* Append host attribute to query data 
292      */
293     if(!empty($hostId)){
294       $data['hostId'] = trim($hostId);
295     }
296    
297     /* Check parameter */ 
298     if(empty($productId)){
299       trigger_error("No valid product id given, check parameter 1.");
300       return(array());
301     }
303     if(!count($cfg)) return;
304     
305     /* Add properties */
306     $data['item'] = array();
307     foreach($cfg as $name => $value){
308       $data['item'][] = "<name>".$name."</name><value>".$value['DEFAULT']."</value>";
309     }  
311     /* Query SI server */
312     $res    = $this->send_data("gosa_opsi_set_product_properties",$this->target,$data,TRUE);
313   }
316   /*! \brief            Adds a given product to a client.
317     @param
318     @return             
319    */
320   public function add_product_to_client($productId,$hostId)
321   {
322     $data = array("productId" => $productId,"hostId" => $hostId);
324     /* Check parameter */ 
325     if(empty($productId)){
326       trigger_error("No valid product id given, check parameter 1.");
327       return;
328     }
329     if(empty($hostId)){
330       trigger_error("No valid host id given, check parameter 2.");
331       return;
332     }
334     /* Query SI server */
335     $res    = $this->send_data("gosa_opsi_add_product_to_client",$this->target,$data,TRUE);
336   }
339   /*! \brief      Removes a given product from a client.
340     @param
341     @return
342    */
343   public function del_product_from_client($productId,$hostId)
344   {
345     $data = array("productId" => $productId,"hostId" => $hostId);
347     /* Check parameter */ 
348     if(empty($productId)){
349       trigger_error("No valid product id given, check parameter 1.");
350       return;
351     }
352     if(empty($hostId)){
353       trigger_error("No valid host id given, check parameter 2.");
354       return;
355     }
357     /* Query SI server */
358     $res    = $this->send_data("gosa_opsi_del_product_from_client",$this->target,$data,TRUE);
359   }
362   /*! \brief            Returns the clients hardware setup.
363     @param
364     @return             
365    */
366   public function get_client_hardware($hostId)
367   {
368     $data = array("hostId" => $hostId);
370     /* Check parameter */ 
371     if(empty($hostId)){
372       trigger_error("No valid host id given, check parameter 1.");
373       return;
374     }
376     /* Query SI server */
377     $res    = $this->send_data("gosa_opsi_get_client_hardware",$this->target,$data,TRUE);
378     if(isset($res['XML'][0]['ITEM'])){
379       return($res['XML'][0]['ITEM']);
380     }
381     return(array());
382   }
385   /*! \brief            Returns the clients software setup.
386     @param
387     @return             
388    */
389   public function get_client_software($hostId)
390   {
391     $data = array("hostId" => $hostId);
393     /* Check parameter */ 
394     if(empty($hostId)){
395       trigger_error("No valid host id given, check parameter 1.");
396       return;
397     }
399     /* Query SI server */
400     $res    = $this->send_data("gosa_opsi_get_client_software",$this->target,$data,TRUE);
401     if(isset($res['XML'][0]['ITEM'])){
402       return($res['XML'][0]['ITEM']);
403     }
404     return(array());
405   }
409   /*! \brief            Deletes the given opsi client.
410     @param
411     @return             
412    */
413   public function del_client($hostId)
414   {
415     $data = array("hostId" => $hostId);
417     /* Check parameter */ 
418     if(empty($hostId)){
419       trigger_error("No valid host id given, check parameter 1.");
420       return;
421     }
423     /* Query SI server */
424     $res    = $this->send_data("gosa_opsi_del_client",$this->target,$data,TRUE);
425     if(isset($res['XML'][0]['ITEM'])){
426       return($res['XML'][0]['ITEM']);
427     }
428     return(array());
429   }
432   /*! \brief            Triggers install/reinstall of an opsi client.
433     @param
434     @return             
435    */
436   public function job_opsi_install_client($hostId,$mac)
437   {
438     $data = array("hostId" => $hostId,"macaddress"=>$mac);
440     /* Check parameter */ 
441     if(empty($hostId)){
442       trigger_error("No valid host id given, check parameter 1.");
443       return;
444     }
446     /* Query SI server */
447     $this->send_data("job_opsi_install_client",$this->target,$data,TRUE);
448   }
450 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
451 ?>