Code

Updated of opsi stuff
[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   }
61   /******************
62     Opsi handling 
63    ******************/
65   function get_hosts_for_system_management()
66   {
67     $res = $this->list_clients();
68     $data = array();
69     foreach($res as $entry){
70       if(!isset($entry['MAC'][0]['VALUE'])) $entry['MAC'][0]['VALUE'] = "";
71       $data[] = array(
72         "dn"          => "opsi:=".$entry['NAME'][0]['VALUE'].",".get_ou("winstations").$this->config->current['BASE'],
73         "objectClass" => array("gosa_opsi_client"),
74         "cn"          => array(0 => $entry['NAME'][0]['VALUE']),
75         "description" => array(0 => $entry['DESCRIPTION'][0]['VALUE']),
76         "macAddress"  => array(0 => $entry['MAC'][0]['VALUE']),
77         "opsi_notes"  => array(0 => $entry['NOTES'][0]['VALUE']));
78     }
79     return($data);
80   }
83   /*! \brief  Maps all xml to array conversion to an alternative method
84                 then used in the parent class 'gosaSupportDaemon'.
85               The alternative method is able to handle more complex data.
86    */
87   private function xml_to_array($xml,$alternative_method = FALSE)
88   {
89     return(gosaSupportDaemon::xml_to_array($xml,TRUE));
90   }
93   /*! \brief  Trigger an event like wake or install for a specific hostId. 
94    */
95   public function send_action($type,$hostId)
96   {
97     switch($type){
98       case 'install'  :  $this->job_opsi_install_client($hostId); break;
99       case 'wake'     :  $this->job_opsi_wake_client($hostId); break;
100       default         :  trigger_error('Unknown type '.$type.'.');
101     }
102   }
105   /******************
106     SI Communication functions
107    ******************/
111   /*! \brief            Returns a list of all opsi clients.
112     @param
113     @return             
114    */
115   public function list_clients( $hostId = "")
116   {
117     $data   = array();
118     $res    = $this->send_data("gosa_opsi_list_clients",$this->target,$data,TRUE);
119     $items  = array();
120     if(isset($res['XML'][0]['ITEM'])){
121       $items = $res['XML'][0]['ITEM'];
122     }
123     return($items);
124   }
127   /*! \brief            Adds a new opsi client.
128     @param
129     @return             
130    */
131   public function add_client($hostId,$macaddress,$notes,$description)
132   {
133     $data = array("hostId" => $hostId,"macaddress" => $macaddress);
135     if(empty($hostId)){
136       trigger_error("No valid host id given, check parameter 1.");
137       return;
138     }
139   
140     /* Add optional attributes */ 
141     foreach(array("notes","description") as $attr) {
142       if(!empty($$attr)){
143         $data[$attr] = $$attr;
144       }
145     }
147     /* Query SI server */
148     $res    = $this->send_data("gosa_opsi_add_client",$this->target,$data,TRUE);
149   }
152   /*! \brief            Modify an opsi client.
153     @param
154     @return             
155    */
156   public function modify_client($hostId,$mac,$notes,$description)
157   {
158     $data = array("hostId" => $hostId,"mac" => $mac);
160     if(empty($hostId)){
161       trigger_error("No valid host id given, check parameter 1.");
162       return;
163     }
164   
165     /* Add optional attributes */ 
166     foreach(array("notes","description") as $attr) {
167       if(!empty($$attr)){
168         $data[$attr] = $$attr;
169       }
170     }
172     /* Query SI server */
173     $res = $this->send_data("gosa_opsi_modify_client",$this->target,$data,TRUE);
174   }
178   /*! \brief            Returns a list of netboot products.
179     @param
180     @return             
181    */
182   public function get_netboot_products($host = "")
183   {
184     /* Append host attribute to query data 
185      */
186     $data = array();
187     if(!empty($host)){
188       $data['hostId'] = trim($host);
189     }
191     $res    = $this->send_data("gosa_opsi_get_netboot_products",$this->target,$data,TRUE);
192     $items = array();
193     if(isset($res['XML'][0]['ITEM'])){
194       foreach($res['XML'][0]['ITEM'] as $entry){
195         $e = array("DESC" => $entry['DESCRIPTION'][0]['VALUE'],
196                    "NAME" => $entry['PRODUCTID'][0]['VALUE']);
197         $items[$entry['PRODUCTID'][0]['VALUE']] = $e;
198       }
199     }
200     return($items);
201   }
204   /*! \brief            Returns a list of all local products.
205     @param
206     @return             
207    */
208   public function get_local_products($host = "")
209   {
210     /* Append host attribute to query data 
211      */
212     $data = array();
213     if(!empty($host)){
214       $data['hostId'] = trim($host);
215     }
217     $res    = $this->send_data("gosa_opsi_get_local_products",$this->target,$data,TRUE);
218     $items = array();
219     if(isset($res['XML'][0]['ITEM'])){
220       foreach($res['XML'][0]['ITEM'] as $entry){
221         $e = array("DESC" => $entry['DESCRIPTION'][0]['VALUE'],
222                    "NAME" => $entry['PRODUCTID'][0]['VALUE']);
223         $items[$entry['PRODUCTID'][0]['VALUE']] = $e; 
224       }
225     }
226     return($items);
227   }
230   /*! \brief            Returns a list of all product properties. \ 
231     .           Additionally you can specify the host parameter to \
232     .           get host specific product properties
233     @param
234     @return             
235    */
236   public function get_product_properties($productId,$hostId = "")
237   {
238     $data = array("productId" => $productId);
240     /* Append host attribute to query data 
241      */
242     if(!empty($hostId)){
243       $data['hostId'] = trim($hostId);
244     }
245    
246     /* Check parameter */ 
247     if(empty($productId)){
248       trigger_error("No valid product id given, check parameter 1.");
249       return(array());
250     }
252     /* Query SI server */
253     $res    = $this->send_data("gosa_opsi_get_product_properties",$this->target,$data,TRUE);
254     $items  = array();
255     if(isset($res['XML'][0]['ITEM'])){   
256       foreach($res['XML'][0]['ITEM'] as $entry){
257         foreach($entry as $name => $val){
258           $items[$name] = $val['0']['VALUE'];
259         }
260       }
261     }
262     return($items);
263   }
266   /*! \brief            Set product properties, globally or per host. 
267     @param
268     @return             
269    */
270   public function set_product_properties($productId,$cfg,$hostId = "")
271   {
272     $data = array("productId" => $productId);
274     /* Append host attribute to query data 
275      */
276     if(!empty($hostId)){
277       $data['hostId'] = trim($hostId);
278     }
279    
280     /* Check parameter */ 
281     if(empty($productId)){
282       trigger_error("No valid product id given, check parameter 1.");
283       return(array());
284     }
286     if(!count($cfg)) return;
287     
288     /* Add properties */
289     $data['item'] = array();
290     foreach($cfg as $name => $value){
291       $data['item'][] = "<name>".$name."</name><value>".$value."</value>";
292     }  
294     /* Query SI server */
295     $res    = $this->send_data("gosa_opsi_set_product_properties",$this->target,$data,TRUE);
296   }
299   /*! \brief            Adds a given product to a client.
300     @param
301     @return             
302    */
303   public function add_product_to_client($productId,$hostId)
304   {
305     $data = array("productId" => $productId,"hostId" => $hostId);
307     /* Check parameter */ 
308     if(empty($productId)){
309       trigger_error("No valid product id given, check parameter 1.");
310       return;
311     }
312     if(empty($hostId)){
313       trigger_error("No valid host id given, check parameter 2.");
314       return;
315     }
317     /* Query SI server */
318     $res    = $this->send_data("gosa_opsi_add_product_to_client",$this->target,$data,TRUE);
319   }
322   /*! \brief      Removes a given product from a client.
323     @param
324     @return
325    */
326   public function del_product_from_client($productId,$hostId)
327   {
328     $data = array("productId" => $productId,"hostId" => $hostId);
330     /* Check parameter */ 
331     if(empty($productId)){
332       trigger_error("No valid product id given, check parameter 1.");
333       return;
334     }
335     if(empty($hostId)){
336       trigger_error("No valid host id given, check parameter 2.");
337       return;
338     }
340     /* Query SI server */
341     $res    = $this->send_data("gosa_opsi_del_product_from_client",$this->target,$data,TRUE);
342   }
345   /*! \brief            Returns the clients hardware setup.
346     @param
347     @return             
348    */
349   public function get_client_hardware($hostId)
350   {
351     $data = array("hostId" => $hostId);
353     /* Check parameter */ 
354     if(empty($hostId)){
355       trigger_error("No valid host id given, check parameter 1.");
356       return;
357     }
359     /* Query SI server */
360     $res    = $this->send_data("gosa_opsi_get_client_hardware",$this->target,$data,TRUE);
361     if(isset($res['XML'][0]['ITEM'])){
362       return($res['XML'][0]['ITEM']);
363     }
364     return(array());
365   }
368   /*! \brief            Returns the clients software setup.
369     @param
370     @return             
371    */
372   public function get_client_software($hostId)
373   {
374     $data = array("hostId" => $hostId);
376     /* Check parameter */ 
377     if(empty($hostId)){
378       trigger_error("No valid host id given, check parameter 1.");
379       return;
380     }
382     /* Query SI server */
383     $res    = $this->send_data("gosa_opsi_get_client_software",$this->target,$data,TRUE);
384     if(isset($res['XML'][0]['ITEM'])){
385       return($res['XML'][0]['ITEM']);
386     }
387     return(array());
388   }
392   /*! \brief            Deletes the given opsi client.
393     @param
394     @return             
395    */
396   public function del_client($hostId)
397   {
398     $data = array("hostId" => $hostId);
400     /* Check parameter */ 
401     if(empty($hostId)){
402       trigger_error("No valid host id given, check parameter 1.");
403       return;
404     }
406     /* Query SI server */
407     $res    = $this->send_data("gosa_opsi_del_client",$this->target,$data,TRUE);
408     if(isset($res['XML'][0]['ITEM'])){
409       return($res['XML'][0]['ITEM']);
410     }
411     return(array());
412   }
415   /*! \brief            Triggers install/reinstall of an opsi client.
416     @param
417     @return             
418    */
419   public function job_opsi_install_client($hostId)
420   {
421     $data = array("hostId" => $hostId);
423     /* Check parameter */ 
424     if(empty($hostId)){
425       trigger_error("No valid host id given, check parameter 1.");
426       return;
427     }
429     /* Query SI server */
430     $this->send_data("job_opsi_install_client",$this->target,$data,TRUE);
431   }
434   /*! \brief            Triggers install/reinstall of an opsi client.
435     @param
436     @return             
437    */
438   public function job_opsi_wake_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     $this->send_data("job_opsi_wake_client",$this->target,$data,TRUE);
450   }
452 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
453 ?>