7ff2a81d52d59f3245fccf3d594636ea20de6693
1 <?php
2 /*
3 * This code is part of GOsa (http://www.gosa-project.org)
4 * Copyright (C) 2003-2008 GONICUS GmbH
5 *
6 * ID: $$Id: class_opsiLicenses.inc 13520 2009-03-09 14:54:13Z hickert $$
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 */
24 class licensePoolGeneric extends plugin
25 {
27 var $cn = "";
28 var $orig_cn = "";
29 var $description = "";
30 var $orig_dn = "";
32 var $data = array();
33 var $orig_data = array();
34 var $productIds = array();
35 var $softwareIds= array();
36 var $licenses = array();
37 var $orig_licenses = array();
39 var $availableProductIds = array();
40 var $attributes =array("cn","description");
41 var $si = NULL;
43 var $opsiHosts = array();
45 var $licenseMap = array(
46 "BOUNDTOHOST"=> "boundToHost",
47 "HOSTIDS"=> "usedByHost",
48 "LICENSEPOOLIDS"=> "licensePoolId",
49 "LICENSETYPE"=> "licenseModel",
50 "LICENSEKEYS"=> "licenseKey",
51 "MAXINSTALLATIONS"=> "maximumInstallations",
52 "EXPIRATIONDATE"=> "expirationDate",
53 "SOFTWARELICENSEID"=> "cn"
54 );
56 var $licenseContractMap = array(
57 "CONCLUSIONDATE"=> "conclusionDate",
58 "NOTIFICATIONDATE"=> "notificationDate",
59 "NOTES"=> "description",
60 "PARTNER"=> "partner"
61 );
63 function __construct(&$config,$dn)
64 {
65 $this->config = $config;
66 $this->dn = $this->orig_dn = $dn;
68 // initialize gosa-si handle for opsi licenses
69 $this->si = new opsiLicenceHandler($this->config);
71 // Detect if this is a new or existings license
72 $this->is_account=TRUE;
73 if($this->dn == "new"){
74 $this->initially_was_account = FALSE;
75 }else{
76 $this->initially_was_account = TRUE;
78 // Extract pool name out of the fake dn.
79 $this->cn = $this->orig_cn = preg_replace("/^opsi:cn=([^,]*),.*$/","\\1",$dn);
80 }
82 $this->init();
83 }
86 function init()
87 {
88 // Load local Boot Products
89 $res = $this->si->get_local_products();
90 $this->availableProductIds=array();
91 if($this->si->is_error()){
92 msg_dialog::display(_("Error"),msgPool::siError($this->si->get_error()),ERROR_DIALOG);
93 $this->init_successfull = FALSE;
94 return(FALSE);
95 }
96 $this->availableProductIds=array_keys($res);
98 // Load opsi hosts
99 $res = $this->si->list_clients();
100 $this->opsiHosts=array();
101 if($this->si->is_error()){
102 msg_dialog::display(_("Error"),msgPool::siError($this->si->get_error()),ERROR_DIALOG);
103 $this->init_successfull = FALSE;
104 return(FALSE);
105 }
106 $this->opsiHosts=$res;
108 // Load Pool
109 if(!$this->initially_was_account){
110 $this->init_successfull = TRUE;
111 }else{
112 $res = $this->si->getPool($this->cn);
113 if($this->si->is_error()){
114 $this->init_successfull = FALSE;
115 msg_dialog::display(_("Error"),msgPool::siError($this->si->get_error()),ERROR_DIALOG);
116 return(FALSE);
117 }else{
118 $this->data = $this->orig_data = $res;
119 $this->description = $this->data['description'][0];
121 // Load software IDs
122 $this->softwareIds = array();
123 if(isset($this->data['softwareId'])){
124 for($i = 0; $i < $this->data['softwareId']['count']; $i++){
125 $this->softwareIds[] = $this->data['softwareId'][$i];
126 }
127 }
129 // Load product IDs
130 $this->productIds = array();
131 if(isset($this->data['productId'])){
132 for($i = 0; $i < $this->data['productId']['count']; $i++){
133 $this->productIds[] = $this->data['productId'][$i];
134 }
135 }
137 // Load Licences
138 $this->licenses = array();
139 if(isset($this->data['licenses'])){
140 for($i =0; $i< $this->data['licenses']['count']; $i++){
141 $license = $this->data['licenses'][$i];
143 // Do not parse invalid licenses
144 if(!is_array($license) || !isset($license['LICENSEPOOLIDS'])){
145 continue;
146 }
148 // Prepare Software License Key(s)
149 $upper = strtoupper($license['LICENSEPOOLIDS'][0]['VALUE']);
150 $license['LICENSEKEYS'] = $license['LICENSEKEYS'][0][$upper];
152 // Map license attributes to a useable format
153 $entry = array();
154 foreach($this->licenseMap as $source => $target){
155 $entry[$target] = "";
156 if(isset($license[$source])){
157 if(count($license[$source]) >= 2){
158 $entry[$target] = array();
159 foreach($license[$source] as $sub){
160 $entry[$target][] = $sub['VALUE'];
161 }
162 }elseif(isset($license[$source][0]['VALUE'])){
163 $entry[$target] = $license[$source][0]['VALUE'];
164 }
165 }
166 }
168 // Extract contract data
169 $lData= $license['LICENSECONTRACTDATA'][0];
170 foreach($this->licenseContractMap as $source => $target){
171 if(isset($lData[$source])){
172 if(count($lData[$source]) >= 2){
173 $entry[$target] = array();
174 foreach($lData[$source] as $sub){
175 $entry[$target][] = $sub['VALUE'];
176 }
177 }elseif(isset($lData[$source][0]['VALUE'])){
178 $entry[$target] = $lData[$source][0]['VALUE'];
179 }
180 }
181 }
183 // There are some multi value attributes - force them to be of type array.
184 foreach(array("boundToHost","usedByHost","licenseKey") as $attr){
185 if(!is_array($entry[$attr])){
186 $entry[$attr] = array($entry[$attr]);
187 }
188 }
189 $entry['MODIFIED'] = FALSE;
190 $this->licenses[$entry['cn']] = $entry;
191 }
192 }
194 $this->orig_licenses = $this->licenses;
195 $this->init_successfull = TRUE;
196 return;
197 }
198 }
199 }
202 function execute()
203 {
204 plugin::execute();
205 // Handle initialization failures.
206 if(isset($_POST['retry_init'])) $this->init();
207 if(!$this->init_successfull){
208 $smarty = get_smarty();
209 $smarty->assign("init_successfull", $this->init_successfull);
210 return($smarty->fetch(get_template_path('licensePoolGeneric.tpl',TRUE,dirname(__FILE__))));
211 }
213 // Display dialogs!
214 if($this->dialog instanceOf plugin){
215 $this->dialog->save_object();
216 $display = $this->dialog->execute();
217 $display.= "<p style=\"text-align:right\">";
219 if($this->acl_is_writeable("licenses")){
220 $display.="<input type=submit name=\"license_finish\" style=\"width:80px\"
221 value=\"".msgPool::okButton(). "\"> ";
222 }
224 $display.="<input type=submit name=\"license_cancel\" value=\"".msgPool::cancelButton()."\"></p>";
225 return($display);
226 }
228 $smarty = get_smarty();
230 // Assign ACls
231 $plInfo = $this->plInfo();
232 foreach($plInfo['plProvidedAcls'] as $name => $desc){
233 $smarty->assign("{$name}ACL",$this->getacl($name));
234 }
235 foreach($this->attributes as $attr){
236 $smarty->assign($attr,$this->$attr);
237 }
238 $smarty->assign("init_successfull", $this->init_successfull);
239 $smarty->assign("availableProductIds", array_diff( $this->availableProductIds, $this->productIds));
240 $smarty->assign("productIds", $this->productIds);
241 $smarty->assign("softwareIds", $this->softwareIds);
242 $smarty->assign("licenses", $this->getLicenseList());
243 $smarty->assign("initially_was_account", $this->initially_was_account);
244 return($smarty->fetch(get_template_path('licensePoolGeneric.tpl',TRUE,dirname(__FILE__))));
245 }
248 // Creates a divSelectBox and fills it with license entries
249 function getLicenseList()
250 {
251 $list = new divSelectBox("test");
252 $list->setHeight(100);
253 if($this->acl_is_readable("licenses")){
254 $editlink = "<a href='?plug=".$_GET['plug']."&act=edit&id=%s'>%s</a>";
255 foreach($this->licenses as $i => $license){
256 $link = "<input type='image' class='center' src='images/lists/edit.png' name='editLicense_{$i}'>";
257 if(preg_match("/w/", $this->getacl("licenses"))){
258 $link.= "<input type='image' class='center' src='images/lists/trash.png' name='removeLicense_{$i}'>";
259 }
261 $maxInst = "";
262 if($license['maximumInstallations'] == 0){
263 $maxInst = _("unlimited");
264 }else{
265 $maxInst = $license['maximumInstallations'];
266 }
269 $map = array(
270 "VOLUME" => sprintf(_("Volume license (#%s)"), $maxInst),
271 "OEM"=>_("OEM"),
272 "RETAIL"=>_("Retail"),
273 "CONCURRENT"=>_("Concurrent"));
275 $f1 = array("string" => sprintf($editlink,$i,$license['cn']));
276 $f2 = array("string" => sprintf($editlink,$i,$map[$license['licenseModel']]));
277 $f6 = array("string" => $link, "attach" => "style='border-right: 0px; width:32px;'");
278 $list->addEntry(array($f1,$f2,$f6));
279 }
280 }
281 return($list->DrawList());
282 }
285 /* Save HTML inputs
286 */
287 function save_object()
288 {
289 // Allow license edit via href links
290 if(isset($_GET['act']) && $_GET['act'] == 'edit' && isset($_GET['id'])){
291 $id = trim($_GET['id']);
292 if(isset($this->licenses[$id])){
293 $this->dialog = new licenseGeneric($this->config,$this->dn,$this->licenses[$id], $this->opsiHosts);
294 $this->dialog->set_acl_base($this->config->current['BASE']);
295 }
296 }
298 // Close license edit dialogs.
299 if($this->dialog instanceOf plugin && isset($_POST['license_cancel'])){
300 $this->dialog = NULL;
301 return;
302 }
304 // Save license modifications
305 if($this->dialog instanceOf plugin && isset($_POST['license_finish'])){
306 $this->dialog->save_object();
307 $msgs = $this->dialog->check();
308 if(count($msgs)){
309 msg_dialog::displayChecks($msgs);
310 }else{
311 $attrs = $this->dialog->save();
312 $attrs['MODIFIED'] = TRUE;
313 $this->licenses[$attrs['cn']] = $attrs;
314 $this->dialog = NULL;
315 }
316 return;
317 }
319 if(isset($_POST['opsiLicensePoolPosted'])){
320 plugin::save_object();
322 // Restore license cn, to avoid creating a copy...
323 if($this->initially_was_account) $this->cn = $this->orig_cn;
325 // We've to add prodcuts here
326 if(preg_match("/w/",$this->getacl("productIds"))){
327 if(isset($_POST['availableProduct']) && isset($_POST['addProduct'])){
328 $pro = get_post('availableProduct');
329 if(isset($this->availableProductIds[$pro]) && !in_array($this->availableProductIds[$pro], $this->productIds)){
330 $this->productIds[] =$this->availableProductIds[$pro];
331 }
332 }
333 }
335 // We've to remove products here
336 if(preg_match("/w/",$this->getacl("productIds"))){
337 if(isset($_POST['productIds']) && isset($_POST['removeProduct'])){
338 foreach($_POST['productIds'] as $key){
339 if(isset($this->productIds[$key])){
340 unset($this->productIds[$key]);
341 }
342 }
343 }
344 }
346 // We've to add software here
347 if(preg_match("/w/",$this->getacl("windowsSoftwareIds"))){
348 if(isset($_POST['newSoftwareId']) && isset($_POST['addSoftware'])){
349 $soft = trim(get_post('newSoftwareId'));
350 if(!empty($soft) && !in_array($soft, $this->softwareIds)){
351 $this->softwareIds[] = $soft;
352 }
353 }
354 }
356 // We've to remove software Ids here
357 if(preg_match("/w/",$this->getacl("windowsSoftwareIds"))){
358 if(isset($_POST['softwareIds']) && isset($_POST['removeSoftware'])){
359 foreach($_POST['softwareIds'] as $key){
360 if(isset($this->softwareIds[$key])){
361 unset($this->softwareIds[$key]);
362 }
363 }
364 }
365 }
367 // We've to create a new license
368 if(preg_match("/w/",$this->getacl("licenses"))){
369 if(isset($_POST['addLicense'])){
370 $this->dialog = new licenseGeneric($this->config,$this->dn,array(), $this->opsiHosts);
371 $this->dialog->set_acl_base($this->config->current['BASE']);
372 }
373 }
375 // Search post for image button clicks.
376 if(preg_match("/r/",$this->getacl("licenses"))){
377 foreach($_POST as $name => $value){
378 if(preg_match("/^editLicense_/",$name)){
379 $id = preg_replace("/^editLicense_(.*)_.$/","\\1",$name);
380 if(isset($this->licenses[$id])){
381 $this->dialog = new licenseGeneric($this->config,$this->dn,$this->licenses[$id], $this->opsiHosts);
382 $this->dialog->set_acl_base($this->config->current['BASE']);
383 }
384 break;
385 }
386 if(preg_match("/^removeLicense_/",$name)){
387 $id = preg_replace("/^removeLicense_(.*)_.$/","\\1",$name);
388 if(isset($this->licenses[$id])){
389 unset($this->licenses[$id]);
390 }
391 break;
392 }
393 }
394 }
395 }
396 }
399 /* Check user input and return a list of 'invalid input' messages.
400 */
401 function check()
402 {
403 $message = plugin::check();
404 return($message);
405 }
409 /* Removes the object from the opsi database
410 */
411 function remove_from_parent()
412 {
413 $this->si->deletePool($this->orig_cn);
414 if($this->si->is_error()){
415 msg_dialog::display(_("Error"),msgPool::siError($this->si->get_error()),ERROR_DIALOG);
416 }else{
418 // Trigger remove signal
419 $this->handle_post_events("remove");
420 }
422 new log("remove","users/".get_class($this),$this->dn,array_keys($this->attrs),$this->si->get_error());
423 }
426 /* Saves object modifications
427 */
428 function save()
429 {
430 plugin::save();
432 // Send modify/add events
433 $mode = "modify";
434 if($this->orig_dn == "new"){
435 $mode = "add";
436 }
438 // Create or update the pool
439 $this->si->createPool($this->cn, $this->description,$this->productIds,$this->softwareIds);#
440 if($this->si->is_error()){
441 msg_dialog::display(_("Error"),msgPool::siError($this->si->get_error()),ERROR_DIALOG);
442 }else{
443 $this->handle_post_events($mode);
444 }
446 // Create, remove or update licenses
447 $add = array_diff_assoc($this->licenses,$this->orig_licenses);
448 $del = array_diff_assoc($this->orig_licenses,$this->licenses);
449 $update = array_intersect($this->licenses,$this->orig_licenses);
451 // Remove licenses
452 foreach($del as $license){
453 $this->si->removeLicenseFromPool($this->cn, $license['cn']);
454 if($this->si->is_error()){
455 msg_dialog::display(_("Error"),msgPool::siError($this->si->get_error()),ERROR_DIALOG);
456 }
457 }
459 // Add licenses
460 foreach($add as $license){
461 $this->si->createLicense(
462 $this->cn, // Pool id
463 $license['cn'],
464 $license['licenseKey'][0],
465 $license['licenseModel'],
466 $license['partner'],
467 $license['conclusionDate'],
468 $license['notificationDate'],
469 $license['description'],
470 $license['cn'],
471 $license['maximumInstallations'],
472 $license['boundToHost'],
473 $license['expirationDate']);
475 if($this->si->is_error()){
476 msg_dialog::display(_("Error"),msgPool::siError($this->si->get_error()),ERROR_DIALOG);
477 }
478 }
480 // Update licenses
481 foreach($update as $license){
483 // Do not save untouched licenses
484 if(!$license['MODIFIED']){
485 continue;
486 }
488 $this->si->createLicense(
489 $this->cn, // Pool id
490 $license['cn'],
491 $license['licenseKey'][0],
492 $license['licenseModel'],
493 $license['partner'],
494 $license['conclusionDate'],
495 $license['notificationDate'],
496 $license['description'],
497 $license['cn'],
498 $license['maximumInstallations'],
499 $license['boundToHost'],
500 $license['expirationDate']);
502 if($this->si->is_error()){
503 msg_dialog::display(_("Error"),msgPool::siError($this->si->get_error()),ERROR_DIALOG);
504 }
505 }
507 // Log action
508 if($mode == "modify"){
509 new log("modify","users/".get_class($this),$this->dn,array_keys($this->attrs),$this->si->get_error());
510 }else{
511 new log("create","users/".get_class($this),$this->dn,array_keys($this->attrs),$this->si->get_error());
512 }
514 return 0;
515 }
517 static function plInfo()
518 {
519 return (array(
520 "plShortName" => _("Pool generic"),
521 "plDescription" => _("License pool generic"),
522 "plSelfModify" => FALSE,
523 "plDepends" => array(),
524 "plPriority" => 7,
525 "plSection" => array("administration"),
526 "plCategory" => array("opsi"),
527 "plProvidedAcls"=> array(
528 "cn" => _("Name"),
529 "description" => _("Description"),
530 "productIds" => _("Applications"),
531 "windowsSoftwareIds" => _("Windows software IDs"),
532 "licenses" => _("Licenses"))
533 ));
534 }
535 }
538 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
539 ?>