Code

Opsi updates
[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();
69     $data = array();
70     foreach($res as $entry){
71       if(!isset($entry['MAC'][0]['VALUE'])) $entry['MAC'][0]['VALUE'] = "";
72       $data[] = array(
73         "dn"          => "opsi:=".$entry['NAME'][0]['VALUE'].",".get_ou("winstations").$this->config->current['BASE'],
74         "objectClass" => array("gosa_opsi_client"),
75         "cn"          => array(0 => $entry['NAME'][0]['VALUE']),
76         "description" => array(0 => $entry['DESCRIPTION'][0]['VALUE']),
77         "macAddress"  => array(0 => $entry['MAC'][0]['VALUE']),
78         "opsi_notes"  => array(0 => $entry['NOTES'][0]['VALUE']));
79     }
80     return($data);
81   }
84   /*! \brief  Maps all xml to array conversion to an alternative method
85                 then used in the parent class 'gosaSupportDaemon'.
86               The alternative method is able to handle more complex data.
87    */
88   private function xml_to_array($xml,$alternative_method = FALSE)
89   {
90     return(gosaSupportDaemon::xml_to_array($xml,TRUE));
91   }
94   /******************
95     SI Communication functions
96    ******************/
100   /*! \brief            Returns a list of all opsi clients.
101     @param
102     @return             
103    */
104   public function list_clients( $hostId = "")
105   {
106     $data   = array();
107     $res    = $this->send_data("gosa_opsi_list_clients",$this->target,$data,TRUE);
108     $items  = array();
109     if(isset($res['XML'][0]['ITEM'])){
110       $items = $res['XML'][0]['ITEM'];
111     }
112     return($items);
113   }
116   /*! \brief            Adds a new opsi client.
117     @param
118     @return             
119    */
120   public function add_client($hostId,$macaddress,$notes,$description)
121   {
122     $data = array("hostId" => $hostId,"macaddress" => $macaddress);
124     if(empty($hostId)){
125       trigger_error("No valid host id given, check parameter 1.");
126       return;
127     }
128   
129     /* Add optional attributes */ 
130     foreach(array("notes","description") as $attr) {
131       if(!empty($$attr)){
132         $data[$attr] = $$attr;
133       }
134     }
136     /* Query SI server */
137     $res    = $this->send_data("gosa_opsi_add_client",$this->target,$data,TRUE);
138   }
141   /*! \brief            Modify an opsi client.
142     @param
143     @return             
144    */
145   public function modify_client($hostId,$mac,$notes,$description)
146   {
147     $data = array("hostId" => $hostId,"mac" => $mac);
149     if(empty($hostId)){
150       trigger_error("No valid host id given, check parameter 1.");
151       return;
152     }
153   
154     /* Add optional attributes */ 
155     foreach(array("notes","description") as $attr) {
156       if(!empty($$attr)){
157         $data[$attr] = $$attr;
158       }
159     }
161     /* Query SI server */
162     $res = $this->send_data("gosa_opsi_modify_client",$this->target,$data,TRUE);
163   }
167   /*! \brief            Returns a list of netboot products.
168     @param
169     @return             
170    */
171   public function get_netboot_products($host = "")
172   {
173     /* Append host attribute to query data 
174      */
175     $data = array();
176     if(!empty($host)){
177       $data['hostId'] = trim($host);
178     }
180     $res    = $this->send_data("gosa_opsi_get_netboot_products",$this->target,$data,TRUE);
181     $items = array();
182     if(isset($res['XML'][0]['ITEM'])){
183       foreach($res['XML'][0]['ITEM'] as $entry){
184         $e = array("DESC" => $entry['DESCRIPTION'][0]['VALUE'],
185                    "NAME" => $entry['PRODUCTID'][0]['VALUE']);
186         $items[$entry['PRODUCTID'][0]['VALUE']] = $e;
187       }
188     }
189     return($items);
190   }
193   /*! \brief            Returns a list of all local products.
194     @param
195     @return             
196    */
197   public function get_local_products($host = "")
198   {
199     /* Append host attribute to query data 
200      */
201     $data = array();
202     if(!empty($host)){
203       $data['hostId'] = trim($host);
204     }
206     $res    = $this->send_data("gosa_opsi_get_local_products",$this->target,$data,TRUE);
207     $items = array();
208     if(isset($res['XML'][0]['ITEM'])){
209       foreach($res['XML'][0]['ITEM'] as $entry){
210         $e = array("DESC" => $entry['DESCRIPTION'][0]['VALUE'],
211                    "NAME" => $entry['PRODUCTID'][0]['VALUE']);
212         $items[$entry['PRODUCTID'][0]['VALUE']] = $e; 
213       }
214     }
215     return($items);
216   }
219   /*! \brief            Returns a list of all product properties. \ 
220     .           Additionally you can specify the host parameter to \
221     .           get host specific product properties
222     @param
223     @return             
224    */
225   public function get_product_properties($productId,$hostId = "")
226   {
227     $data = array("productId" => $productId);
229     /* Append host attribute to query data 
230      */
231     if(!empty($hostId)){
232       $data['hostId'] = trim($hostId);
233     }
234    
235     /* Check parameter */ 
236     if(empty($productId)){
237       trigger_error("No valid product id given, check parameter 1.");
238       return(array());
239     }
241     /* Query SI server */
242     $res    = $this->send_data("gosa_opsi_get_product_properties",$this->target,$data,TRUE);
243     $items  = array();
244     if(isset($res['XML'][0]['ITEM'])){   
245       foreach($res['XML'][0]['ITEM'] as $entry){
246         foreach($entry as $name => $val){
247           $items[$name] = $val['0']['VALUE'];
248         }
249       }
250     }
251     return($items);
252   }
255   /*! \brief            Set product properties, globally or per host. 
256     @param
257     @return             
258    */
259   public function set_product_properties($productId,$cfg,$hostId = "")
260   {
261     $data = array("productId" => $productId);
263     /* Append host attribute to query data 
264      */
265     if(!empty($hostId)){
266       $data['hostId'] = trim($hostId);
267     }
268    
269     /* Check parameter */ 
270     if(empty($productId)){
271       trigger_error("No valid product id given, check parameter 1.");
272       return(array());
273     }
275     if(!count($cfg)) return;
276     
277     /* Add properties */
278     $data['item'] = array();
279     foreach($cfg as $name => $value){
280       $data['item'][] = "<name>".$name."</name><value>".$value."</value>";
281     }  
283     /* Query SI server */
284     $res    = $this->send_data("gosa_opsi_set_product_properties",$this->target,$data,TRUE);
285   }
288   /*! \brief            Adds a given product to a client.
289     @param
290     @return             
291    */
292   public function add_product_to_client($productId,$hostId)
293   {
294     $data = array("productId" => $productId,"hostId" => $hostId);
296     /* Check parameter */ 
297     if(empty($productId)){
298       trigger_error("No valid product id given, check parameter 1.");
299       return;
300     }
301     if(empty($hostId)){
302       trigger_error("No valid host id given, check parameter 2.");
303       return;
304     }
306     /* Query SI server */
307     $res    = $this->send_data("gosa_opsi_add_product_to_client",$this->target,$data,TRUE);
308   }
311   /*! \brief      Removes a given product from a client.
312     @param
313     @return
314    */
315   public function del_product_from_client($productId,$hostId)
316   {
317     $data = array("productId" => $productId,"hostId" => $hostId);
319     /* Check parameter */ 
320     if(empty($productId)){
321       trigger_error("No valid product id given, check parameter 1.");
322       return;
323     }
324     if(empty($hostId)){
325       trigger_error("No valid host id given, check parameter 2.");
326       return;
327     }
329     /* Query SI server */
330     $res    = $this->send_data("gosa_opsi_del_product_from_client",$this->target,$data,TRUE);
331   }
334   /*! \brief            Returns the clients hardware setup.
335     @param
336     @return             
337    */
338   public function get_client_hardware($hostId)
339   {
340     $data = array("hostId" => $hostId);
342     /* Check parameter */ 
343     if(empty($hostId)){
344       trigger_error("No valid host id given, check parameter 1.");
345       return;
346     }
348     /* Query SI server */
349     $res    = $this->send_data("gosa_opsi_get_client_hardware",$this->target,$data,TRUE);
350     if(isset($res['XML'][0]['ITEM'])){
351       return($res['XML'][0]['ITEM']);
352     }
353     return(array());
354   }
357   /*! \brief            Returns the clients software setup.
358     @param
359     @return             
360    */
361   public function get_client_software($hostId)
362   {
363     $data = array("hostId" => $hostId);
365     /* Check parameter */ 
366     if(empty($hostId)){
367       trigger_error("No valid host id given, check parameter 1.");
368       return;
369     }
371     /* Query SI server */
372     $res    = $this->send_data("gosa_opsi_get_client_software",$this->target,$data,TRUE);
373     if(isset($res['XML'][0]['ITEM'])){
374       return($res['XML'][0]['ITEM']);
375     }
376     return(array());
377   }
381   /*! \brief            Deletes the given opsi client.
382     @param
383     @return             
384    */
385   public function del_client()
386   {
387     /*  <xml> 
388         <header>gosa_opsi_del_client</header> 
389         <source>GOSA</source> 
390         <target>00:01:6c:9d:b9:fa</target> 
391         <hostId>limux-cl-2.intranet.gonicus.de</hostId>
392         </xml>
393      */
394   }
397   /*! \brief            Triggers install/reinstall of an opsi client.
398     @param
399     @return             
400    */
401   public function job_opsi_install_client()
402   {
403     /*  <xml> 
404         <header>job_opsi_install_client</header> 
405         <source>GOSA</source> 
406         <target>00:01:6c:9d:b9:fa</target> 
407         <hostId>limux-cl-2.intranet.gonicus.de</hostId> 
408         <macaddress>00:11:25:4b:8c:e5</macaddress> 
409         </xml>
410      */
411   }
413 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
414 ?>