1 <?php
3 class opsiLicenceHandler extends opsi {
7 /*******************
8 * POOLs
9 *******************/
11 protected $use_alternative_xml_parse_method = TRUE;
14 /* @brief Returns licensePoolId, description, productIds and windowsSoftwareIds
15 * for all found license pools.
16 */
17 function listPools()
18 {
19 $data= array();
20 $res = $this->send_data("gosa_opsi_getLicensePools_listOfHashes",$this->target,$data,TRUE);
21 $items = array();
22 if(isset($res['XML'][0]['ANSWER_OPSI_GETLICENSEPOOLS_LISTOFHASHES'])){
23 if(isset($res['XML'][0]['RESULT'][0]['HIT'])){
24 $items = $res['XML'][0]['RESULT'][0]['HIT'];
25 $data =array();
26 foreach($items as $item){
27 $entry = array();
28 foreach(
29 array(
30 "DESCRIPTION" => "description",
31 "LICENSEPOOLID" => "cn",
32 "PRODUCTIDS" => "productId",
33 "WINDOWSSOFTWAREIDS"=> "softwareId") as $source => $dest){
35 if(isset($item[$source])){
36 $entry[$dest] = array('count' => 0);
37 foreach($item[$source] as $obj){
38 $entry[$dest][] = $obj['VALUE'];
39 }
40 $entry[$dest]['count'] = (count($entry[$dest]) -1 );
41 }
42 }
43 $data[] =$entry;
44 }
45 return($data);
46 }else{
47 // No entries, but got an answer. We probably do not have any pools yet.
48 return(array());
49 }
50 }
51 return(FALSE);
52 }
55 /* @brief Create a license pool at Opsi server.
56 * @param licensePoolId The name of the pool (optional).
57 * @param description The description of the pool (optional).
58 * @param productIds A list of assigned porducts of the pool (optional).
59 * @param windowsSoftwareIds A list of windows software Ids associated to the pool (optional).
60 */
61 function createPool($poolId,$desc = "",$products = array(),$softwareIds = array())
62 {
63 $data= array();
64 $data['licensePoolId'] = htmlentities($poolId);
65 if(!empty($desc)){
66 $data['description'] = htmlentities($desc);
67 }
68 if(count($products)){
69 $data['productIds'] = $products;
70 }
71 if(count($softwareIds)){
72 $data['windowsSoftwareIds'] = $softwareIds;
73 }
75 $res = $this->send_data("gosa_opsi_createLicensePool",$this->target,$data,TRUE);
76 if(isset($res['XML'][0]['ANSWER_OPSI_CREATELICENSEPOOL'])){
77 return(TRUE);
78 }
79 return(FALSE);
80 }
83 /*
84 * @brief Return productIds, windowsSoftwareIds and description for a given licensePoolId
85 * @param licensePoolId The name of the pool.
86 */
87 function getPool($licensePoolId)
88 {
89 $data= array();
90 $data['licensePoolId'] = htmlentities($licensePoolId);
91 $res = $this->send_data("gosa_opsi_getPool",$this->target,$data,TRUE);
93 if(isset($res['XML'][0]['ANSWER_OPSI_GETPOOL'])){
94 $item = array();
95 foreach(array("LICENSEPOOLID" => "cn",
96 "DESCRIPTION" => "description",
97 "LICENSES" => "licenses",
98 "LICENSECONTRACTDATA" => "contract",
99 "WINDOWSSOFTWAREIDS" => "softwareId",
100 "PRODUCTIDS" => "productId") as $source => $target){
101 if(isset($res['XML'][0][$source])){
102 $item[$target] = array('count' => 0);
105 foreach($res['XML'][0][$source] as $data){
106 if(isset($data['VALUE'])){
107 $item[$target][] = $data['VALUE'];
108 }elseif(isset($data['HIT'])){
109 $item[$target] = array_merge($item[$target],$data['HIT']);
110 }
111 }
112 $item[$target]['count'] = count($item[$target]) -1 ;
113 }
114 }
115 return($item);
116 }
117 return(FALSE);
118 }
121 /*
122 * @brief
123 * Delete licnese pool by license pool Id.
124 * A pool can only be deleted if there are no software licenses bound to the pool.
125 * The fixed parameter deleteLicenses=True specifies that
126 * all software licenses bound to the pool are being deleted.
127 * @param licensePoolId The name of the pool.
128 */
129 function deletePool($poolId, $force = FALSE)
130 {
131 $data= array();
132 $data['licensePoolId'] = htmlentities($poolId);
133 if($force){
134 $data['deleteLicenses'] = 'TRUE';
135 }
136 $res = $this->send_data("gosa_opsi_deleteLicensePool",$this->target,$data,TRUE);
137 if(isset($res['XML'][0]['ANSWER_OPSI_DELETELICENSEPOOL'])){
138 return(TRUE);
139 }
140 return(FALSE);
141 }
144 /*******************
145 * LICENSES
146 *******************/
149 function listLicenses()
150 {
151 $data= array();
152 $res = $this->send_data("gosa_opsi_getAllSoftwareLicenses",$this->target,$data,TRUE);
154 if(isset($res['XML'][0]['ANSWER_OPSI_GETALLSOFTWARELICENSES'])){
156 $licenses = array();
157 if(isset($res['XML'][0]['LICENSES'][0]['HIT'])){
158 foreach($res['XML'][0]['LICENSES'][0]['HIT'] as $entry){
160 $item = array();
162 // License keys are indexed by the pool id, map it here.
163 if(isset($entry['LICENSEPOOLIDS'][0]['VALUE'])){
164 $n = strtoupper($entry['LICENSEPOOLIDS'][0]['VALUE']);
165 $entry['LICENSEKEYS'] = $entry['LICENSEKEYS'][0][$n];
166 }
168 // Now fake an ldap like result
169 foreach(array(
170 "BOUNDTOHOST" => "boundToHost",
171 "LICENSEKEYS" => "licenseKey",
172 "LICENSEPOOLIDS" => "licensePoolId",
173 "LICENSETYPE" => "licenseType",
174 "SOFTWARELICENSEID" => "softwareLicenseId") as $source => $target){
175 if(isset($entry[$source])){
176 $item[$target] = array('count' => 0);
177 foreach($entry[$source] as $data){
178 $item[$target][] = $data['VALUE'];
179 }
180 $item[$target]['count'] = count($item[$target]) -1 ;
181 }
182 }
184 $licenses[] = $item;
185 }
186 }
187 return($licenses);
188 }
189 return(FALSE);
190 }
193 /*
194 * @brief
195 * Create a license contract, create a software
196 * license and add the software license to the license pool
197 * @param licensePoolId The name of the pool the license should be assigned.
198 * @param licenseKey The license key.
199 * @param licenseTyp Typ of a licnese, either "OEM", "VOLUME" or "RETAIL"
200 * @param partner Name of the license partner (optional).
201 * @param conclusionDate Date of conclusion of license contract (optional)
202 * @param notificationDate Date of notification that license is running out soon (optional).
203 * @param notes This is the place for some notes (optional)
204 * @param softwareLicenseId Identificator of a license (optional).
205 * @param maxInstallations The number of clients use this license (optional).
206 * @param boundToHost The name of the client the license is bound to (optional).
207 * @param expirationDate The date when the license is running down (optional).
208 */
209 function createLicense($poolId, $licenseId, $licenseKey,$licenseType = "",
210 $partner = "",
211 $conclusionDate = "",
212 $notificationDate ="",
213 $notes = "",
214 $softwareLicenseId = "",
215 $maxInstallations = "",
216 $boundToHost = "",
217 $expirationDate = "")
218 {
220 $data= array();
221 $data['licensePoolId'] = htmlentities($poolId);
222 $data['licenseKey'] = htmlentities($licenseKey);
223 $data['licenseId'] = htmlentities($licenseId);
225 // Append optional attributes
226 foreach(array("partner","conclusionDate","notificationDate","notes","softwareLicenseId",
227 "licenseType","maxInstallations","boundToHost","expirationDate") as $attr){
228 if(!empty($$attr)){
229 $data[$attr] = $$attr;
230 }
231 }
233 $res = $this->send_data("gosa_opsi_createLicense",$this->target,$data,TRUE);
234 if(isset($res['XML'][0]['ANSWER_OPSI_GETSOFTWARELICENSE_HASH'])){
235 return(TRUE);
236 }
237 return(FALSE);
238 }
241 /* @brief Returns expirationDate, boundToHost, maxInstallation, licenseTyp,
242 * licensePoolIds and licenseKeys for a given softwareLicense Id.
243 * @param softwareLicenseId Identificator of a license.
244 */
245 function getLicense($softwareLicenseId)
246 {
247 $data= array();
248 $data['softwareLicenseId'] = htmlentities($softwareLicenseId);
249 $res = $this->send_data("gosa_opsi_getSoftwareLicense_hash",$this->target,$data,TRUE);
251 if(isset($res['XML'][0]['ANSWER_OPSI_GETSOFTWARELICENSE_HASH'])){
252 return($res);
253 }
254 return(FALSE);
255 }
258 function getReservedLicensesForHost($hostId)
259 {
260 $data= array();
261 if(!empty($hostId)){
262 $data['hostId'] = htmlentities($hostId);
263 }
264 $res = $this->send_data("gosa_opsi_getReservedLicenses",$this->target,$data,TRUE);
265 if(isset($res['XML'][0]['ANSWER_OPSI_GETRESERVEDLICENSES'])){
267 $items = array();
268 if(isset($res['XML'][0]['LICENSES'][0]['HIT'])){
269 foreach($res['XML'][0]['LICENSES'][0]['HIT'] as $entry){
270 $item = array();
271 foreach(array(
272 "LICENSEPOOLIDS" => "licensePoolId",
273 "SOFTWARELICENSEID" => "softwareLicenseId") as $source => $target){
274 if(isset($entry[$source])){
276 $item[$target] = array('count' => 0);
277 foreach($entry[$source] as $data){
278 $item[$target][] = $data['VALUE'];
279 }
280 $item[$target]['count'] = count($item[$target]) -1 ;
281 }
282 }
283 $items[] = $item;
284 }
285 }
286 return($items);
287 }
288 return(FALSE);
289 }
292 /*
293 * @brief Returns softwareLicenseId, notes, licenseKey, hostId and licensePoolId for optional given licensePoolId and hostId
294 * @param hostid Something like client_1.intranet.mydomain.de (optional).
295 * @param licensePoolId The name of the pool (optional).
296 */
297 function getLicenseUsage($hostId = "", $licensePoolId = "")
298 {
299 $data= array();
300 if(!empty($hostId)){
301 $data['hostId'] = htmlentities($hostId);
302 }
303 if(!empty($hostId)){
304 $data['licensePoolId'] = htmlentities($licensePoolId);
305 }
307 $res = $this->send_data("gosa_opsi_getSoftwareLicenseUsages",$this->target,$data,TRUE);
308 if(isset($res['XML'][0]['ANSWER_OPSI_GETSOFTWARELICENSEUSAGES'])){
310 $items = array();
311 if(isset($res['XML'][0]['RESULT'][0]['HIT'])){
312 foreach($res['XML'][0]['RESULT'][0]['HIT'] as $entry){
313 $item = array();
314 foreach(array(
315 "HOSTID" => "hostId",
316 "LICENSEKEY" => "licenseKey",
317 "LICENSEPOOLID" => "licensePoolId",
318 "NOTES" => "notes",
319 "SOFTWARELICENSEID" => "softwareLicenseId") as $source => $target){
320 if(isset($entry[$source])){
322 $item[$target] = array('count' => 0);
323 foreach($entry[$source] as $data){
324 $item[$target][] = $data['VALUE'];
325 }
326 $item[$target]['count'] = count($item[$target]) -1 ;
327 }
328 }
329 $items[] = $item;
330 }
331 }
332 return($items);
333 }
334 return(FALSE);
335 }
338 /* @brief Assigns a software license to a host
339 * @param hostId Something like client_1.intranet.mydomain.de
340 * @param licensePoolId The name of the pool.
341 */
342 function addLicenseToHost($licensePoolId,$hostId)
343 {
344 $data= array();
345 $data['licensePoolId'] = htmlentities($licensePoolId);
346 $data['hostId'] = htmlentities($hostId);
347 $res = $this->send_data("gosa_opsi_assignSoftwareLicenseToHost",$this->target,$data,TRUE);
348 if(isset($res['XML'][0]['ANSWER_OPSI_ASSIGNSOFTWARELICENSETOHOST'])){
349 return(TRUE);
350 }
351 return(FALSE);
352 }
355 /* @brief Reserve a software license to a host
356 * @param softwareLicenseId
357 * @param hostId Something like client_1.intranet.mydomain.de
358 */
359 function reserverLicenseForHost($softwareLicenseId,$hostId)
360 {
361 $data= array();
362 $data['softwareLicenseId'] = htmlentities($softwareLicenseId);
363 $data['hostId'] = htmlentities($hostId);
364 $res = $this->send_data("gosa_opsi_boundHostToLicense",$this->target,$data,TRUE);
365 if(isset($res['XML'][0]['ANSWER_OPSI_BOUNDHOSTTOLICENSE'])){
366 return(TRUE);
367 }
368 return(FALSE);
369 }
372 /*
373 * @brief Remove software licnese reservation for a host.
374 * @param softwareLicenseId
375 * @param hostid Something like client_1.intranet.mydomain.de
376 */
377 function removeLicenseReservationFromHost($softwareLicenseId,$hostId)
378 {
379 $data= array();
380 $data['softwareLicenseId'] = htmlentities($softwareLicenseId);
381 $data['hostId'] = htmlentities($hostId);
382 $res = $this->send_data("gosa_opsi_unboundHostFromLicense",$this->target,$data,TRUE);
383 if(isset($res['XML'][0]['ANSWER_OPSI_UNBOUNDHOSTFROMLICENSE'])){
384 return(TRUE);
385 }
386 return(FALSE);
387 }
390 /*
391 * @brief Unassign a software license from a host.
392 * @param hostid Something like client_1.intranet.mydomain.de
393 * @param licensePoolId The name of the pool.
394 */
395 function removeLicenseFromHost($licensePoolId,$hostId)
396 {
397 $data= array();
398 $data['licensePoolId'] = htmlentities($licensePoolId);
399 $data['hostId'] = htmlentities($hostId);
400 $res = $this->send_data("gosa_opsi_unassignSoftwareLicenseFromHost",$this->target,$data,TRUE);
401 if(isset($res['XML'][0]['ANSWER_OPSI_UNASSIGNSOFTWARELICENSEFROMHOST'])){
402 return(TRUE);
403 }
404 return(FALSE);
405 }
408 /*
409 * @brief Removes a single license from a license pool
410 * Attention, the software license has to exists
411 * otherwise it will lead to an Opsi internal server error.
412 * @param softwareLicenseId
413 * @param licensePoolId
414 */
415 function removeLicenseFromPool($licensePoolId,$softwareLicenseId)
416 {
417 $data= array();
418 $data['licensePoolId'] = htmlentities($licensePoolId);
419 $data['softwareLicenseId'] = htmlentities($softwareLicenseId);
420 $res = $this->send_data("gosa_opsi_removeLicense",$this->target,$data,TRUE);
421 if(isset($res['XML'][0]['ANSWER_OPSI_REMOVELICENSE'])){
422 return(TRUE);
423 }
424 return(FALSE);
425 }
428 /*
429 * @brief Unassign all software licenses from a host
430 * @param hostid Something like client_1.intranet.mydomain.de
431 */
432 function removeAllLicensesFromHost($hostId)
433 {
434 $data= array();
435 $data['hostId'] = htmlentities($hostid);
436 $res = $this->send_data("gosa_opsi_unassignAllSoftwareLicensesFromHost",$this->target,$data,TRUE);
437 if(isset($res['XML'][0]['ANSWER_OPSI_UNASSIGNALLSOFTWARELICENSESFROMHOST'])){
438 return(TRUE);
439 }
440 return(FALSE);
441 }
444 /* @brief
445 * Returns the assigned licensePoolId and licenses,
446 * how often the product is installed and at which host
447 * and the number of max and remaining installations for a given OPSI product.
448 * @param productId Identificator of an OPSI product.
449 */
450 function getLicensesForProduct($productId)
451 {
452 $data= array();
453 $data['productId'] = htmlentities($productId);
454 $res = $this->send_data("gosa_opsi_getSoftwareLicenseUsagesForProductId",$this->target,$data,TRUE);
455 if(isset($res['XML'][0]['ANSWER_OPSI_GETSOFTWARELICENSEUSAGESFORPRODUCTID'])){
456 if(isset($res['XML'][0]['RESULT'][0]['HIT'])){
457 return($res['XML'][0]['RESULT'][0]['HIT']);
458 }
459 return(array());
460 }
461 return(FALSE);
462 }
463 }
465 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
466 ?>