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 // Handle initialization failures.
205 if(isset($_POST['retry_init'])) $this->init();
206 if(!$this->init_successfull){
207 $smarty = get_smarty();
208 $smarty->assign("init_successfull", $this->init_successfull);
209 return($smarty->fetch(get_template_path('licensePoolGeneric.tpl',TRUE,dirname(__FILE__))));
210 }
212 // Display dialogs!
213 if($this->dialog instanceOf plugin){
214 $this->dialog->save_object();
215 $display = $this->dialog->execute();
216 $display.= "<p style=\"text-align:right\">";
218 if($this->acl_is_writeable("licenses")){
219 $display.="<input type=submit name=\"license_finish\" style=\"width:80px\"
220 value=\"".msgPool::okButton(). "\"> ";
221 }
223 $display.="<input type=submit name=\"license_cancel\" value=\"".msgPool::cancelButton()."\"></p>";
224 return($display);
225 }
227 $smarty = get_smarty();
229 // Assign ACls
230 $plInfo = $this->plInfo();
231 foreach($plInfo['plProvidedAcls'] as $name => $desc){
232 $smarty->assign("{$name}ACL",$this->getacl($name));
233 }
234 foreach($this->attributes as $attr){
235 $smarty->assign($attr,$this->$attr);
236 }
237 $smarty->assign("init_successfull", $this->init_successfull);
238 $smarty->assign("availableProductIds", array_diff( $this->availableProductIds, $this->productIds));
239 $smarty->assign("productIds", $this->productIds);
240 $smarty->assign("softwareIds", $this->softwareIds);
241 $smarty->assign("licenses", $this->getLicenseList());
242 $smarty->assign("initially_was_account", $this->initially_was_account);
243 return($smarty->fetch(get_template_path('licensePoolGeneric.tpl',TRUE,dirname(__FILE__))));
244 }
247 // Creates a divSelectBox and fills it with license entries
248 function getLicenseList()
249 {
250 $list = new divSelectBox("test");
251 $list->setHeight(100);
252 if($this->acl_is_readable("licenses")){
253 foreach($this->licenses as $i => $license){
254 $link = "<input type='image' class='center' src='images/lists/edit.png' name='editLicense_{$i}'>";
255 if(preg_match("/w/", $this->getacl("licenses"))){
256 $link.= "<input type='image' class='center' src='images/lists/trash.png' name='removeLicense_{$i}'>";
257 }
258 $f1 = array("string" => $license['cn']);
260 $maxInst = "";
261 if($license['maximumInstallations'] == 0){
262 $maxInst = "("._("unlimited").")";
263 }else{
264 $maxInst = "(".$license['maximumInstallations'].")";;
265 }
267 $map = array(
268 "VOLUME" => sprintf(_("Volume license (#%s)"), $maxInst),
269 "OEM"=>_("OEM"),
270 "RETAIL"=>_("Retail"),
271 "CONCURRENT"=>_("Concurrent"));
273 $f2 = array("string" => $map[$license['licenseModel']]);
274 $f6 = array("string" => $link, "attach" => "style='border-right: 0px; width:32px;'");
275 $list->addEntry(array($f1,$f2,$f6));
276 }
277 }
278 return($list->DrawList());
279 }
282 /* Save HTML inputs
283 */
284 function save_object()
285 {
286 // Close license edit dialogs.
287 if($this->dialog instanceOf plugin && isset($_POST['license_cancel'])){
288 $this->dialog = NULL;
289 return;
290 }
292 // Save license modifications
293 if($this->dialog instanceOf plugin && isset($_POST['license_finish'])){
294 $this->dialog->save_object();
295 $msgs = $this->dialog->check();
296 if(count($msgs)){
297 msg_dialog::displayChecks($msgs);
298 }else{
299 $attrs = $this->dialog->save();
300 $attrs['MODIFIED'] = TRUE;
301 $this->licenses[$attrs['cn']] = $attrs;
302 $this->dialog = NULL;
303 }
304 return;
305 }
307 if(isset($_POST['opsiLicensePoolPosted'])){
308 plugin::save_object();
310 // Restore license cn, to avoid creating a copy...
311 if($this->initially_was_account) $this->cn = $this->orig_cn;
313 // We've to add prodcuts here
314 if(preg_match("/w/",$this->getacl("productIds"))){
315 if(isset($_POST['availableProduct']) && isset($_POST['addProduct'])){
316 $pro = get_post('availableProduct');
317 if(isset($this->availableProductIds[$pro]) && !in_array($this->availableProductIds[$pro], $this->productIds)){
318 $this->productIds[] =$this->availableProductIds[$pro];
319 }
320 }
321 }
323 // We've to remove products here
324 if(preg_match("/w/",$this->getacl("productIds"))){
325 if(isset($_POST['productIds']) && isset($_POST['removeProduct'])){
326 foreach($_POST['productIds'] as $key){
327 if(isset($this->productIds[$key])){
328 unset($this->productIds[$key]);
329 }
330 }
331 }
332 }
334 // We've to add software here
335 if(preg_match("/w/",$this->getacl("windowsSoftwareIds"))){
336 if(isset($_POST['newSoftwareId']) && isset($_POST['addSoftware'])){
337 $soft = trim(get_post('newSoftwareId'));
338 if(!empty($soft) && !in_array($soft, $this->softwareIds)){
339 $this->softwareIds[] = $soft;
340 }
341 }
342 }
344 // We've to remove software Ids here
345 if(preg_match("/w/",$this->getacl("windowsSoftwareIds"))){
346 if(isset($_POST['softwareIds']) && isset($_POST['removeSoftware'])){
347 foreach($_POST['softwareIds'] as $key){
348 if(isset($this->softwareIds[$key])){
349 unset($this->softwareIds[$key]);
350 }
351 }
352 }
353 }
355 // We've to create a new license
356 if(preg_match("/w/",$this->getacl("licenses"))){
357 if(isset($_POST['addLicense'])){
358 $this->dialog = new licenseGeneric($this->config,$this->dn,array(), $this->opsiHosts);
359 $this->dialog->set_acl_base($this->config->current['BASE']);
360 }
361 }
363 // Search post for image button clicks.
364 if(preg_match("/r/",$this->getacl("licenses"))){
365 foreach($_POST as $name => $value){
366 if(preg_match("/^editLicense_/",$name)){
367 $id = preg_replace("/^editLicense_(.*)_.$/","\\1",$name);
368 if(isset($this->licenses[$id])){
369 $this->dialog = new licenseGeneric($this->config,$this->dn,$this->licenses[$id], $this->opsiHosts);
370 $this->dialog->set_acl_base($this->config->current['BASE']);
371 }
372 break;
373 }
374 if(preg_match("/^removeLicense_/",$name)){
375 $id = preg_replace("/^removeLicense_(.*)_.$/","\\1",$name);
376 if(isset($this->licenses[$id])){
377 unset($this->licenses[$id]);
378 }
379 break;
380 }
381 }
382 }
383 }
384 }
387 /* Check user input and return a list of 'invalid input' messages.
388 */
389 function check()
390 {
391 $message = plugin::check();
392 return($message);
393 }
397 /* Removes the object from the opsi database
398 */
399 function remove_from_parent()
400 {
401 $this->si->deletePool($this->orig_cn);
402 if($this->si->is_error()){
403 msg_dialog::display(_("Error"),msgPool::siError($this->si->get_error()),ERROR_DIALOG);
404 }else{
406 // Trigger remove signal
407 $this->handle_post_events("remove");
408 }
410 new log("remove","users/".get_class($this),$this->dn,array_keys($this->attrs),$this->si->get_error());
411 }
414 /* Saves object modifications
415 */
416 function save()
417 {
418 plugin::save();
420 // Send modify/add events
421 $mode = "modify";
422 if($this->orig_dn == "new"){
423 $mode = "add";
424 }
426 // Create or update the pool
427 $this->si->createPool($this->cn, $this->description,$this->productIds,$this->softwareIds);#
428 if($this->si->is_error()){
429 msg_dialog::display(_("Error"),msgPool::siError($this->si->get_error()),ERROR_DIALOG);
430 }else{
431 $this->handle_post_events($mode);
432 }
434 // Create, remove or update licenses
435 $add = array_diff_assoc($this->licenses,$this->orig_licenses);
436 $del = array_diff_assoc($this->orig_licenses,$this->licenses);
437 $update = array_intersect($this->licenses,$this->orig_licenses);
439 // Remove licenses
440 foreach($del as $license){
441 $this->si->removeLicenseFromPool($this->cn, $license['cn']);
442 if($this->si->is_error()){
443 msg_dialog::display(_("Error"),msgPool::siError($this->si->get_error()),ERROR_DIALOG);
444 }
445 }
447 // Add licenses
448 foreach($add as $license){
449 $this->si->createLicense(
450 $this->cn, // Pool id
451 $license['cn'],
452 $license['licenseKey'][0],
453 $license['licenseModel'],
454 $license['partner'],
455 $license['conclusionDate'],
456 $license['notificationDate'],
457 $license['description'],
458 $license['cn'],
459 $license['maximumInstallations'],
460 $license['boundToHost'],
461 $license['expirationDate']);
463 if($this->si->is_error()){
464 msg_dialog::display(_("Error"),msgPool::siError($this->si->get_error()),ERROR_DIALOG);
465 }
466 }
468 // Update licenses
469 foreach($update as $license){
471 // Do not save untouched licenses
472 if(!$license['MODIFIED']){
473 continue;
474 }
476 $this->si->createLicense(
477 $this->cn, // Pool id
478 $license['cn'],
479 $license['licenseKey'][0],
480 $license['licenseModel'],
481 $license['partner'],
482 $license['conclusionDate'],
483 $license['notificationDate'],
484 $license['description'],
485 $license['cn'],
486 $license['maximumInstallations'],
487 $license['boundToHost'],
488 $license['expirationDate']);
490 if($this->si->is_error()){
491 msg_dialog::display(_("Error"),msgPool::siError($this->si->get_error()),ERROR_DIALOG);
492 }
493 }
495 // Log action
496 if($mode == "modify"){
497 new log("modify","users/".get_class($this),$this->dn,array_keys($this->attrs),$this->si->get_error());
498 }else{
499 new log("create","users/".get_class($this),$this->dn,array_keys($this->attrs),$this->si->get_error());
500 }
502 return 0;
503 }
505 static function plInfo()
506 {
507 return (array(
508 "plShortName" => _("Pool generic"),
509 "plDescription" => _("License pool generic"),
510 "plSelfModify" => FALSE,
511 "plDepends" => array(),
512 "plPriority" => 1,
513 "plSection" => array("administration"),
514 "plCategory" => array("opsi"),
515 "plProvidedAcls"=> array(
516 "cn" => _("Name"),
517 "description" => _("Description"),
518 "productIds" => _("Applications"),
519 "windowsSoftwareIds" => _("Windows software IDs"),
520 "licenses" => _("Licenses"))
521 ));
522 }
523 }
526 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
527 ?>