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   }
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    ******************/
99   /*! \brief            Returns a list of netboot products.
100     @param
101     @return             
102    */
103   public function get_netboot_products($host = "")
104   {
105     /* Append host attribute to query data 
106      */
107     $data = array();
108     if(!empty($host)){
109       $data['hostId'] = trim($host);
110     }
112     $res    = $this->send_data("gosa_opsi_get_netboot_products",$this->target,$data,TRUE);
113     $items = array();
114     if(isset($res['XML'][0]['ITEM'])){
115       foreach($res['XML'][0]['ITEM'] as $entry){
116         $e = array("DESC" => $entry['DESCRIPTION'][0]['VALUE'],
117                    "NAME" => $entry['PRODUCTID'][0]['VALUE']);
118         $items[$entry['PRODUCTID'][0]['VALUE']] = $e;
119       }
120     }
121     return($items);
122   }
125   /*! \brief            Returns a list of all local products.
126     @param
127     @return             
128    */
129   public function get_local_products($host = "")
130   {
131     /* Append host attribute to query data 
132      */
133     $data = array();
134     if(!empty($host)){
135       $data['hostId'] = trim($host);
136     }
138     $res    = $this->send_data("gosa_opsi_get_local_products",$this->target,$data,TRUE);
139     $items = array();
140     if(isset($res['XML'][0]['ITEM'])){
141       foreach($res['XML'][0]['ITEM'] as $entry){
142         $e = array("DESC" => $entry['DESCRIPTION'][0]['VALUE'],
143                    "NAME" => $entry['PRODUCTID'][0]['VALUE']);
144         $items[$entry['PRODUCTID'][0]['VALUE']] = $e; 
145       }
146     }
147     return($items);
148   }
151   /*! \brief            Returns a list of all product properties. \ 
152     .           Additionally you can specify the host parameter to \
153     .           get host specific product properties
154     @param
155     @return             
156    */
157   public function get_product_properties($productId,$hostId = "")
158   {
159     $data = array("productId" => $productId);
161     /* Append host attribute to query data 
162      */
163     if(!empty($hostId)){
164       $data['hostId'] = trim($hostId);
165     }
166    
167     /* Check parameter */ 
168     if(empty($productId)){
169       trigger_error("No valid product id given, check parameter 1.");
170       return(array());
171     }
173     /* Query SI server */
174     $res    = $this->send_data("gosa_opsi_get_product_properties",$this->target,$data,TRUE);
175     $items  = array();
176     if(isset($res['XML'][0]['ITEM'])){   
177       foreach($res['XML'][0]['ITEM'] as $entry){
178         foreach($entry as $name => $val){
179           $items[$name] = $val['0']['VALUE'];
180         }
181       }
182     }
183     return($items);
184   }
187   /*! \brief            Set product properties, globally or per host. 
188     @param
189     @return             
190    */
191   public function set_product_properties($productId,$cfg,$hostId = "")
192   {
193     $data = array("productId" => $productId);
195     /* Append host attribute to query data 
196      */
197     if(!empty($hostId)){
198       $data['hostId'] = trim($hostId);
199     }
200    
201     /* Check parameter */ 
202     if(empty($productId)){
203       trigger_error("No valid product id given, check parameter 1.");
204       return(array());
205     }
207     if(!count($cfg)) return;
208     
209     /* Add properties */
210     $data['item'] = array();
211     foreach($cfg as $name => $value){
212       $data['item'][] = "<name>".$name."</name><value>".$value."</value>";
213     }  
215     /* Query SI server */
216     $res    = $this->send_data("gosa_opsi_set_product_properties",$this->target,$data,TRUE);
217   }
220   /*! \brief            Adds a given product to a client.
221     @param
222     @return             
223    */
224   public function add_product_to_client($productId,$hostId)
225   {
226     $data = array("productId" => $productId,"hostId" => $hostId);
228     /* Check parameter */ 
229     if(empty($productId)){
230       trigger_error("No valid product id given, check parameter 1.");
231       return;
232     }
233     if(empty($hostId)){
234       trigger_error("No valid host id given, check parameter 2.");
235       return;
236     }
238     /* Query SI server */
239     $res    = $this->send_data("gosa_opsi_add_product_to_client",$this->target,$data,TRUE);
240   }
243   /*! \brief      Removes a given product from a client.
244     @param
245     @return
246    */
247   public function del_product_from_client($productId,$hostId)
248   {
249     $data = array("productId" => $productId,"hostId" => $hostId);
251     /* Check parameter */ 
252     if(empty($productId)){
253       trigger_error("No valid product id given, check parameter 1.");
254       return;
255     }
256     if(empty($hostId)){
257       trigger_error("No valid host id given, check parameter 2.");
258       return;
259     }
261     /* Query SI server */
262     $res    = $this->send_data("gosa_opsi_del_product_from_client",$this->target,$data,TRUE);
263   }
266   /*! \brief            Returns the clients hardware setup.
267     @param
268     @return             
269    */
270   public function get_client_hardware($hostId)
271   {
272     $data = array("hostId" => $hostId);
274     /* Check parameter */ 
275     if(empty($hostId)){
276       trigger_error("No valid host id given, check parameter 1.");
277       return;
278     }
280     /* Query SI server */
281     $res    = $this->send_data("gosa_opsi_get_client_hardware",$this->target,$data,TRUE);
282     print_a($res);
283   }
286   /*! \brief            Returns the clients software setup.
287     @param
288     @return             
289    */
290   public function get_client_software()
291   {
292     /*  <xml> 
293         <header>gosa_opsi_get_client_software</header> 
294         <source>GOSA</source> 
295         <target>GOSA</target> 
296         <hostId>limux-cl-2.intranet.gonicus.de</hostId> 
297         </xml> i
298      */
299   }
304   /*! \brief            Returns a list of all opsi clients.
305     @param
306     @return             
307    */
308   public function list_clients( $hostId = "")
309   {
310     $data   = array();
311     $res    = $this->send_data("gosa_opsi_list_clients",$this->target,$data,TRUE);
312     $items  = array();
313     if(isset($res['XML'][0]['ITEM'])){
314       $items = $res['XML'][0]['ITEM'];
315     }
316     return($items);
317   }
320   /*! \brief            Deletes the given opsi client.
321     @param
322     @return             
323    */
324   public function del_client()
325   {
326     /*  <xml> 
327         <header>gosa_opsi_del_client</header> 
328         <source>GOSA</source> 
329         <target>00:01:6c:9d:b9:fa</target> 
330         <hostId>limux-cl-2.intranet.gonicus.de</hostId>
331         </xml>
332      */
333   }
336   /*! \brief            Triggers install/reinstall of an opsi client.
337     @param
338     @return             
339    */
340   public function job_opsi_install_client()
341   {
342     /*  <xml> 
343         <header>job_opsi_install_client</header> 
344         <source>GOSA</source> 
345         <target>00:01:6c:9d:b9:fa</target> 
346         <hostId>limux-cl-2.intranet.gonicus.de</hostId> 
347         <macaddress>00:11:25:4b:8c:e5</macaddress> 
348         </xml>
349      */
350   }
353   /*! \brief            Adds a new opsi client.
354     @param
355     @return             
356    */
357   public function add_client($hostId,$macaddress,$notes,$description)
358   {
359     $data = array("hostId" => $hostId,"macaddress" => $macaddress);
361     if(empty($hostId)){
362       trigger_error("No valid host id given, check parameter 1.");
363       return;
364     }
365   
366     /* Add optional attributes */ 
367     foreach(array("notes","description") as $attr) {
368       if(!empty($$attr)){
369         $data[$attr] = $$attr;
370       }
371     }
373     /* Query SI server */
374     $res    = $this->send_data("gosa_opsi_add_client",$this->target,$data,TRUE);
375   }
378   /*! \brief            Modify an opsi client.
379     @param
380     @return             
381    */
382   public function modify_client($hostId,$mac,$notes,$description)
383   {
384     $data = array("hostId" => $hostId,"mac" => $mac);
386     if(empty($hostId)){
387       trigger_error("No valid host id given, check parameter 1.");
388       return;
389     }
390   
391     /* Add optional attributes */ 
392     foreach(array("notes","description") as $attr) {
393       if(!empty($$attr)){
394         $data[$attr] = $$attr;
395       }
396     }
398     /* Query SI server */
399     $res    = $this->send_data("gosa_opsi_modify_client",$this->target,$data,TRUE);
400   }
404 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
405 ?>