Code

renamed opsi function
[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){
262           $items[$name] = $val['0']['VALUE'];
263         }
264       }
265     }
266     return($items);
267   }
270   /*! \brief            Set product properties, globally or per host. 
271     @param
272     @return             
273    */
274   public function set_product_properties($productId,$cfg,$hostId = "")
275   {
276     $data = array("productId" => $productId);
278     /* Append host attribute to query data 
279      */
280     if(!empty($hostId)){
281       $data['hostId'] = trim($hostId);
282     }
283    
284     /* Check parameter */ 
285     if(empty($productId)){
286       trigger_error("No valid product id given, check parameter 1.");
287       return(array());
288     }
290     if(!count($cfg)) return;
291     
292     /* Add properties */
293     $data['item'] = array();
294     foreach($cfg as $name => $value){
295       $data['item'][] = "<name>".$name."</name><value>".$value."</value>";
296     }  
298     /* Query SI server */
299     $res    = $this->send_data("gosa_opsi_set_product_properties",$this->target,$data,TRUE);
300   }
303   /*! \brief            Adds a given product to a client.
304     @param
305     @return             
306    */
307   public function add_product_to_client($productId,$hostId)
308   {
309     $data = array("productId" => $productId,"hostId" => $hostId);
311     /* Check parameter */ 
312     if(empty($productId)){
313       trigger_error("No valid product id given, check parameter 1.");
314       return;
315     }
316     if(empty($hostId)){
317       trigger_error("No valid host id given, check parameter 2.");
318       return;
319     }
321     /* Query SI server */
322     $res    = $this->send_data("gosa_opsi_add_product_to_client",$this->target,$data,TRUE);
323   }
326   /*! \brief      Removes a given product from a client.
327     @param
328     @return
329    */
330   public function del_product_from_client($productId,$hostId)
331   {
332     $data = array("productId" => $productId,"hostId" => $hostId);
334     /* Check parameter */ 
335     if(empty($productId)){
336       trigger_error("No valid product id given, check parameter 1.");
337       return;
338     }
339     if(empty($hostId)){
340       trigger_error("No valid host id given, check parameter 2.");
341       return;
342     }
344     /* Query SI server */
345     $res    = $this->send_data("gosa_opsi_del_product_from_client",$this->target,$data,TRUE);
346   }
349   /*! \brief            Returns the clients hardware setup.
350     @param
351     @return             
352    */
353   public function get_client_hardware($hostId)
354   {
355     $data = array("hostId" => $hostId);
357     /* Check parameter */ 
358     if(empty($hostId)){
359       trigger_error("No valid host id given, check parameter 1.");
360       return;
361     }
363     /* Query SI server */
364     $res    = $this->send_data("gosa_opsi_get_client_hardware",$this->target,$data,TRUE);
365     if(isset($res['XML'][0]['ITEM'])){
366       return($res['XML'][0]['ITEM']);
367     }
368     return(array());
369   }
372   /*! \brief            Returns the clients software setup.
373     @param
374     @return             
375    */
376   public function get_client_software($hostId)
377   {
378     $data = array("hostId" => $hostId);
380     /* Check parameter */ 
381     if(empty($hostId)){
382       trigger_error("No valid host id given, check parameter 1.");
383       return;
384     }
386     /* Query SI server */
387     $res    = $this->send_data("gosa_opsi_get_client_software",$this->target,$data,TRUE);
388     if(isset($res['XML'][0]['ITEM'])){
389       return($res['XML'][0]['ITEM']);
390     }
391     return(array());
392   }
396   /*! \brief            Deletes the given opsi client.
397     @param
398     @return             
399    */
400   public function del_client($hostId)
401   {
402     $data = array("hostId" => $hostId);
404     /* Check parameter */ 
405     if(empty($hostId)){
406       trigger_error("No valid host id given, check parameter 1.");
407       return;
408     }
410     /* Query SI server */
411     $res    = $this->send_data("gosa_opsi_del_client",$this->target,$data,TRUE);
412     if(isset($res['XML'][0]['ITEM'])){
413       return($res['XML'][0]['ITEM']);
414     }
415     return(array());
416   }
419   /*! \brief            Triggers install/reinstall of an opsi client.
420     @param
421     @return             
422    */
423   public function job_opsi_install_client($hostId,$mac)
424   {
425     $data = array("hostId" => $hostId,"macaddress"=>$mac);
427     /* Check parameter */ 
428     if(empty($hostId)){
429       trigger_error("No valid host id given, check parameter 1.");
430       return;
431     }
433     /* Query SI server */
434     $this->send_data("job_opsi_install_client",$this->target,$data,TRUE);
435   }
437 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
438 ?>