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 /******************
94 SI Communication functions
95 ******************/
99 /*! \brief Returns a list of all opsi clients.
100 @param
101 @return
102 */
103 public function list_clients( $hostId = "")
104 {
105 $data = array();
106 $res = $this->send_data("gosa_opsi_list_clients",$this->target,$data,TRUE);
107 $items = array();
108 if(isset($res['XML'][0]['ITEM'])){
109 $items = $res['XML'][0]['ITEM'];
110 }
111 return($items);
112 }
115 /*! \brief Adds a new opsi client.
116 @param
117 @return
118 */
119 public function add_client($hostId,$macaddress,$notes,$description)
120 {
121 $data = array("hostId" => $hostId,"macaddress" => $macaddress);
123 if(empty($hostId)){
124 trigger_error("No valid host id given, check parameter 1.");
125 return;
126 }
128 /* Add optional attributes */
129 foreach(array("notes","description") as $attr) {
130 if(!empty($$attr)){
131 $data[$attr] = $$attr;
132 }
133 }
135 /* Query SI server */
136 $res = $this->send_data("gosa_opsi_add_client",$this->target,$data,TRUE);
137 }
140 /*! \brief Modify an opsi client.
141 @param
142 @return
143 */
144 public function modify_client($hostId,$mac,$notes,$description)
145 {
146 $data = array("hostId" => $hostId,"mac" => $mac);
148 if(empty($hostId)){
149 trigger_error("No valid host id given, check parameter 1.");
150 return;
151 }
153 /* Add optional attributes */
154 foreach(array("notes","description") as $attr) {
155 if(!empty($$attr)){
156 $data[$attr] = $$attr;
157 }
158 }
160 /* Query SI server */
161 $res = $this->send_data("gosa_opsi_modify_client",$this->target,$data,TRUE);
162 }
166 /*! \brief Returns a list of netboot products.
167 @param
168 @return
169 */
170 public function get_netboot_products($host = "")
171 {
172 /* Append host attribute to query data
173 */
174 $data = array();
175 if(!empty($host)){
176 $data['hostId'] = trim($host);
177 }
179 $res = $this->send_data("gosa_opsi_get_netboot_products",$this->target,$data,TRUE);
180 $items = array();
181 if(isset($res['XML'][0]['ITEM'])){
182 foreach($res['XML'][0]['ITEM'] as $entry){
183 $e = array("DESC" => $entry['DESCRIPTION'][0]['VALUE'],
184 "NAME" => $entry['PRODUCTID'][0]['VALUE']);
185 $items[$entry['PRODUCTID'][0]['VALUE']] = $e;
186 }
187 }
188 return($items);
189 }
192 /*! \brief Returns a list of all local products.
193 @param
194 @return
195 */
196 public function get_local_products($host = "")
197 {
198 /* Append host attribute to query data
199 */
200 $data = array();
201 if(!empty($host)){
202 $data['hostId'] = trim($host);
203 }
205 $res = $this->send_data("gosa_opsi_get_local_products",$this->target,$data,TRUE);
206 $items = array();
207 if(isset($res['XML'][0]['ITEM'])){
208 foreach($res['XML'][0]['ITEM'] as $entry){
209 $e = array("DESC" => $entry['DESCRIPTION'][0]['VALUE'],
210 "NAME" => $entry['PRODUCTID'][0]['VALUE']);
211 $items[$entry['PRODUCTID'][0]['VALUE']] = $e;
212 }
213 }
214 return($items);
215 }
218 /*! \brief Returns a list of all product properties. \
219 . Additionally you can specify the host parameter to \
220 . get host specific product properties
221 @param
222 @return
223 */
224 public function get_product_properties($productId,$hostId = "")
225 {
226 $data = array("productId" => $productId);
228 /* Append host attribute to query data
229 */
230 if(!empty($hostId)){
231 $data['hostId'] = trim($hostId);
232 }
234 /* Check parameter */
235 if(empty($productId)){
236 trigger_error("No valid product id given, check parameter 1.");
237 return(array());
238 }
240 /* Query SI server */
241 $res = $this->send_data("gosa_opsi_get_product_properties",$this->target,$data,TRUE);
242 $items = array();
243 if(isset($res['XML'][0]['ITEM'])){
244 foreach($res['XML'][0]['ITEM'] as $entry){
245 foreach($entry as $name => $val){
246 $items[$name] = $val['0']['VALUE'];
247 }
248 }
249 }
250 return($items);
251 }
254 /*! \brief Set product properties, globally or per host.
255 @param
256 @return
257 */
258 public function set_product_properties($productId,$cfg,$hostId = "")
259 {
260 $data = array("productId" => $productId);
262 /* Append host attribute to query data
263 */
264 if(!empty($hostId)){
265 $data['hostId'] = trim($hostId);
266 }
268 /* Check parameter */
269 if(empty($productId)){
270 trigger_error("No valid product id given, check parameter 1.");
271 return(array());
272 }
274 if(!count($cfg)) return;
276 /* Add properties */
277 $data['item'] = array();
278 foreach($cfg as $name => $value){
279 $data['item'][] = "<name>".$name."</name><value>".$value."</value>";
280 }
282 /* Query SI server */
283 $res = $this->send_data("gosa_opsi_set_product_properties",$this->target,$data,TRUE);
284 }
287 /*! \brief Adds a given product to a client.
288 @param
289 @return
290 */
291 public function add_product_to_client($productId,$hostId)
292 {
293 $data = array("productId" => $productId,"hostId" => $hostId);
295 /* Check parameter */
296 if(empty($productId)){
297 trigger_error("No valid product id given, check parameter 1.");
298 return;
299 }
300 if(empty($hostId)){
301 trigger_error("No valid host id given, check parameter 2.");
302 return;
303 }
305 /* Query SI server */
306 $res = $this->send_data("gosa_opsi_add_product_to_client",$this->target,$data,TRUE);
307 }
310 /*! \brief Removes a given product from a client.
311 @param
312 @return
313 */
314 public function del_product_from_client($productId,$hostId)
315 {
316 $data = array("productId" => $productId,"hostId" => $hostId);
318 /* Check parameter */
319 if(empty($productId)){
320 trigger_error("No valid product id given, check parameter 1.");
321 return;
322 }
323 if(empty($hostId)){
324 trigger_error("No valid host id given, check parameter 2.");
325 return;
326 }
328 /* Query SI server */
329 $res = $this->send_data("gosa_opsi_del_product_from_client",$this->target,$data,TRUE);
330 }
333 /*! \brief Returns the clients hardware setup.
334 @param
335 @return
336 */
337 public function get_client_hardware($hostId)
338 {
339 $data = array("hostId" => $hostId);
341 /* Check parameter */
342 if(empty($hostId)){
343 trigger_error("No valid host id given, check parameter 1.");
344 return;
345 }
347 /* Query SI server */
348 $res = $this->send_data("gosa_opsi_get_client_hardware",$this->target,$data,TRUE);
349 if(isset($res['XML'][0]['ITEM'])){
350 return($res['XML'][0]['ITEM']);
351 }
352 return(array());
353 }
356 /*! \brief Returns the clients software setup.
357 @param
358 @return
359 */
360 public function get_client_software($hostId)
361 {
362 $data = array("hostId" => $hostId);
364 /* Check parameter */
365 if(empty($hostId)){
366 trigger_error("No valid host id given, check parameter 1.");
367 return;
368 }
370 /* Query SI server */
371 $res = $this->send_data("gosa_opsi_get_client_software",$this->target,$data,TRUE);
372 if(isset($res['XML'][0]['ITEM'])){
373 return($res['XML'][0]['ITEM']);
374 }
375 return(array());
376 }
380 /*! \brief Deletes the given opsi client.
381 @param
382 @return
383 */
384 public function del_client($hostId)
385 {
386 $data = array("hostId" => $hostId);
388 /* Check parameter */
389 if(empty($hostId)){
390 trigger_error("No valid host id given, check parameter 1.");
391 return;
392 }
394 /* Query SI server */
395 $res = $this->send_data("gosa_opsi_del_client",$this->target,$data,TRUE);
396 if(isset($res['XML'][0]['ITEM'])){
397 return($res['XML'][0]['ITEM']);
398 }
399 return(array());
400 }
403 /*! \brief Triggers install/reinstall of an opsi client.
404 @param
405 @return
406 */
407 public function job_opsi_install_client($hostId)
408 {
409 $data = array("hostId" => $hostId);
411 /* Check parameter */
412 if(empty($hostId)){
413 trigger_error("No valid host id given, check parameter 1.");
414 return;
415 }
417 /* Query SI server */
418 $res = $this->send_data("job_opsi_install_client",$this->target,$data,TRUE);
419 if(isset($res['XML'][0]['ITEM'])){
420 return($res['XML'][0]['ITEM']);
421 }
422 return(array());
423 }
424 }
425 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
426 ?>