Code

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