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;
48 /*! \brief Create opsi object.
49 @param
50 @return
51 */
52 public function __construct($config)
53 {
54 $this->config = $config;
55 gosaSupportDaemon::__construct($config);
56 $this->target = "00:01:6c:9d:b9:fa";
57 }
60 /******************
61 Opsi handling
62 ******************/
64 function get_hosts_for_system_management()
65 {
66 $res = $this->list_clients();
68 $data = array();
69 foreach($res as $entry){
70 if(!isset($entry['MAC'])) $entry['MAC'] = "";;
71 $data[] = array(
72 "dn" => "opsi:=".$entry['NAME'].",".get_ou("winstations").$this->config->current['BASE'],
73 "objectClass" => array("gosa_opsi_client"),
74 "cn" => array(0 => $entry['NAME']),
75 "description" => array(0 => $entry['DESCRIPTION']),
76 "macAddress" => array(0 => $entry['MAC']),
77 "opsi_notes" => array(0 => $entry['NOTES']));
79 }
81 return($data);
82 }
85 /******************
86 SI Communication functions
87 ******************/
90 /*! \brief Returns a list of netboot products.
91 @param
92 @return
93 */
94 public function get_netboot_products($host = "")
95 {
96 $data = array();
98 /* Append host attribute to query data
99 */
100 if(!empty($host)){
101 $data['hostId'] = trim($host);
102 }
104 $res = $this->send_data("gosa_opsi_get_netboot_products",$this->target,$data,TRUE);
105 $items = array();
106 if(isset($res['XML']['ITEM']['PRODUCTID'])){
107 if(!is_array($res['XML']['ITEM']['PRODUCTID'])){
108 $items[$res['XML']['ITEM']['PRODUCTID']]['NAME'] = $res['XML']['ITEM']['PRODUCTID'];
109 $items[$res['XML']['ITEM']['PRODUCTID']]['DESC'] = $res['XML']['ITEM']['DESCRIPTION'];
110 }else{
111 foreach($res['XML']['ITEM']['PRODUCTID'] as $id => $name){
112 $items[$name]['NAME'] = $name;
113 $items[$name]['DESC'] = $res['XML']['ITEM']['DESCRIPTION'][$id];
114 }
115 }
116 }
117 return($items);
118 }
121 /*! \brief Returns a list of all local products.
122 @param
123 @return
124 */
125 public function get_local_products($host = "")
126 {
127 $data = array();
129 /* Append host attribute to query data
130 */
131 if(!empty($host)){
132 $data['hostId'] = trim($host);
133 }
135 $res = $this->send_data("gosa_opsi_get_local_products",$this->target,$data,TRUE);
136 $items = array();
137 if(isset($res['XML']['ITEM']['PRODUCTID'])){
138 if(!is_array($res['XML']['ITEM']['PRODUCTID'])){
139 $items[$res['XML']['ITEM']['PRODUCTID']]['NAME'] = $res['XML']['ITEM']['PRODUCTID'];
140 $items[$res['XML']['ITEM']['PRODUCTID']]['DESC'] = $res['XML']['ITEM']['DESCRIPTION'];
141 }else{
142 foreach($res['XML']['ITEM']['PRODUCTID'] as $id => $name){
143 $items[$name]['NAME'] = $name;
144 $items[$name]['DESC'] = $res['XML']['ITEM']['DESCRIPTION'][$id];
145 }
146 }
147 }
148 return($items);
149 }
152 /*! \brief Returns a list of all product properties. \
153 . Additionally you can specify the host parameter to \
154 . get host specific product properties
155 @param
156 @return
157 */
158 public function get_product_properties($productId,$hostId = "")
159 {
160 $data = array("productId" => $productId);
162 /* Append host attribute to query data
163 */
164 if(!empty($hostId)){
165 $data['hostId'] = trim($hostId);
166 }
168 /* Check parameter */
169 if(empty($productId)){
170 trigger_error("No valid product id given, check parameter 1.");
171 return(array());
172 }
174 /* Query SI server */
175 $res = $this->send_data("gosa_opsi_get_product_properties",$this->target,$data,TRUE);
176 if(isset($res['XML']['ITEM'])){
177 return($res['XML']['ITEM']);
178 }
179 return(array());
180 }
183 /*! \brief Set product properties, globally or per host.
184 @param
185 @return
186 */
187 public function set_product_properties($productId,$cfg,$hostId = "")
188 {
189 $data = array("productId" => $productId);
191 /* Append host attribute to query data
192 */
193 if(!empty($hostId)){
194 $data['hostId'] = trim($hostId);
195 }
197 /* Check parameter */
198 if(empty($productId)){
199 trigger_error("No valid product id given, check parameter 1.");
200 return(array());
201 }
203 if(!count($cfg)) return;
205 /* Add properties */
206 $data['item'] = array();
207 foreach($cfg as $name => $value){
208 $data['item'][] = "<name>".$name."</name><value>".$value."</value>";
209 }
211 /* Query SI server */
212 $res = $this->send_data("gosa_opsi_set_product_properties",$this->target,$data,TRUE);
213 }
216 /*! \brief Adds a given product to a client.
217 @param
218 @return
219 */
220 public function add_product_to_client($productId,$hostId)
221 {
222 $data = array("productId" => $productId,"hostId" => $hostId);
224 /* Check parameter */
225 if(empty($productId)){
226 trigger_error("No valid product id given, check parameter 1.");
227 return;
228 }
229 if(empty($hostId)){
230 trigger_error("No valid host id given, check parameter 2.");
231 return;
232 }
234 /* Query SI server */
235 $res = $this->send_data("gosa_opsi_add_product_to_client",$this->target,$data,TRUE);
236 }
239 /*! \brief Removes a given product from a client.
240 @param
241 @return
242 */
243 public function del_product_from_client($productId,$hostId)
244 {
245 $data = array("productId" => $productId,"hostId" => $hostId);
247 /* Check parameter */
248 if(empty($productId)){
249 trigger_error("No valid product id given, check parameter 1.");
250 return;
251 }
252 if(empty($hostId)){
253 trigger_error("No valid host id given, check parameter 2.");
254 return;
255 }
257 /* Query SI server */
258 $res = $this->send_data("gosa_opsi_del_product_from_client",$this->target,$data,TRUE);
259 }
262 /*! \brief Returns the clients hardware setup.
263 @param
264 @return
265 */
266 public function get_client_hardware($hostId)
267 {
268 $data = array("hostId" => $hostId);
270 /* Check parameter */
271 if(empty($hostId)){
272 trigger_error("No valid host id given, check parameter 1.");
273 return;
274 }
276 /* Query SI server */
277 $res = $this->send_data("gosa_opsi_get_client_hardware",$this->target,$data,TRUE);
278 if(isset($res['XML']['ITEM'])){
279 #print_a($res['XML']['ITEM']);
280 }
281 }
284 /*! \brief Returns the clients software setup.
285 @param
286 @return
287 */
288 public function get_client_software()
289 {
290 /* <xml>
291 <header>gosa_opsi_get_client_software</header>
292 <source>GOSA</source>
293 <target>GOSA</target>
294 <hostId>limux-cl-2.intranet.gonicus.de</hostId>
295 </xml> i
296 */
297 }
302 /*! \brief Returns a list of all opsi clients.
303 @param
304 @return
305 */
306 public function list_clients()
307 {
308 $data = array();
309 $res = $this->send_data("gosa_opsi_list_clients",$this->target,$data,TRUE);
310 $items = array();
311 if(isset($res['XML']['ITEM'])){
312 if(!is_array($res['XML']['ITEM']['NAME'])){
313 $obj = $res['XML']['ITEM'];
314 $items[$obj['NAME']] = $obj;
315 }else{
316 foreach($res['XML']['ITEM'] as $type => $val){
317 foreach($val as $key => $value){
318 $items[$key][$type] = $value;
319 }
320 }
321 }
322 }
323 return($items);
324 }
327 /*! \brief Deletes the given opsi client.
328 @param
329 @return
330 */
331 public function del_client()
332 {
333 /* <xml>
334 <header>gosa_opsi_del_client</header>
335 <source>GOSA</source>
336 <target>00:01:6c:9d:b9:fa</target>
337 <hostId>limux-cl-2.intranet.gonicus.de</hostId>
338 </xml>
339 */
340 }
343 /*! \brief Triggers install/reinstall of an opsi client.
344 @param
345 @return
346 */
347 public function job_opsi_install_client()
348 {
349 /* <xml>
350 <header>job_opsi_install_client</header>
351 <source>GOSA</source>
352 <target>00:01:6c:9d:b9:fa</target>
353 <hostId>limux-cl-2.intranet.gonicus.de</hostId>
354 <macaddress>00:11:25:4b:8c:e5</macaddress>
355 </xml>
356 */
357 }
360 /*! \brief Adds a new opsi client.
361 @param
362 @return
363 */
364 public function add_client($hostId,$ip,$macaddress,$notes,$description)
365 {
366 $data = array("hostId" => $hostId,"ip" => $ip,"macaddress" => $macaddress);
368 if(empty($hostId)){
369 trigger_error("No valid host id given, check parameter 1.");
370 return;
371 }
372 if(empty($ip)){
373 trigger_error("No valid ip address given, check parameter 2.");
374 return;
375 }
377 /* Add optional attributes */
378 foreach(array("notes","description") as $attr) {
379 if(!empty($$attr)){
380 $data[$attr] = $$attr;
381 }
382 }
384 /* Query SI server */
385 $res = $this->send_data("gosa_opsi_add_client",$this->target,$data,TRUE);
386 }
389 }
390 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
391 ?>