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 $this->si = new opsiLicenceHandler($this->config);
70 $this->is_account=TRUE;
71 if($this->dn == "new"){
72 $this->initially_was_account = FALSE;
73 }else{
74 $this->initially_was_account = TRUE;
75 $this->cn = $this->orig_cn = preg_replace("/^opsi:cn=([^,]*),.*$/","\\1",$dn);
76 }
78 // Extract pool name out of the fake dn.
79 $this->init();
80 }
83 function init()
84 {
85 // Load local Boot Products
86 $res = $this->si->get_local_products();
87 $this->availableProductIds=array();
88 if($this->si->is_error()){
89 msg_dialog::display(_("Error"),msgPool::siError($this->si->get_error()),ERROR_DIALOG);
90 $this->init_successfull = FALSE;
91 return(FALSE);
92 }
93 $this->availableProductIds=array_keys($res);
95 // Load opsi hosts
96 $res = $this->si->list_clients();
97 $this->opsiHosts=array();
98 if($this->si->is_error()){
99 msg_dialog::display(_("Error"),msgPool::siError($this->si->get_error()),ERROR_DIALOG);
100 $this->init_successfull = FALSE;
101 return(FALSE);
102 }
103 $this->opsiHosts=$res;
105 // Load Pool
106 if(!$this->initially_was_account){
107 $this->init_successfull = TRUE;
108 }else{
110 $res = $this->si->getPool($this->cn);
112 if($this->si->is_error()){
113 $this->init_successfull = FALSE;
114 msg_dialog::display(_("Error"),msgPool::siError($this->si->get_error()),ERROR_DIALOG);
115 return(FALSE);
116 }else{
117 $this->data = $this->orig_data = $res;
118 $this->description = $this->data['description'][0];
120 // Load software IDs
121 $this->softwareIds = array();
122 if(isset($this->data['softwareId'])){
123 for($i = 0; $i < $this->data['softwareId']['count']; $i++){
124 $this->softwareIds[] = $this->data['softwareId'][$i];
125 }
126 }
128 // Load product IDs
129 $this->productIds = array();
130 if(isset($this->data['productId'])){
131 for($i = 0; $i < $this->data['productId']['count']; $i++){
132 $this->productIds[] = $this->data['productId'][$i];
133 }
134 }
136 // Load Licences
137 $this->licenses = array();
138 if(isset($this->data['licenses'])){
139 for($i =0; $i< $this->data['licenses']['count']; $i++){
140 $license = $this->data['licenses'][$i];
142 // Do not parse invalid licenses
143 if(!is_array($license) || !isset($license['LICENSEPOOLIDS'])){
144 echo "Invalid license";
145 continue;
146 }
148 // Prepare Software License Key(s)
149 $upper = strtoupper($license['LICENSEPOOLIDS'][0]['VALUE']);
150 $license['LICENSEKEYS'] = $license['LICENSEKEYS'][0][$upper];
152 $entry = array();
153 foreach($this->licenseMap as $source => $target){
154 $entry[$target] = "";
155 if(isset($license[$source])){
156 if(count($license[$source]) >= 2){
157 $entry[$target] = array();
158 foreach($license[$source] as $sub){
159 $entry[$target][] = $sub['VALUE'];
160 }
161 }elseif(isset($license[$source][0]['VALUE'])){
162 $entry[$target] = $license[$source][0]['VALUE'];
163 }
164 }
165 }
167 $lData= $license['LICENSECONTRACTDATA'][0];
168 foreach($this->licenseContractMap as $source => $target){
169 if(isset($lData[$source])){
170 if(count($lData[$source]) >= 2){
171 $entry[$target] = array();
172 foreach($lData[$source] as $sub){
173 $entry[$target][] = $sub['VALUE'];
174 }
175 }elseif(isset($lData[$source][0]['VALUE'])){
176 $entry[$target] = $lData[$source][0]['VALUE'];
177 }
178 }
179 }
181 // There are some multi value attributes - force them to be of type array.
182 foreach(array("boundToHost","usedByHost","licenseKey") as $attr){
183 if(!is_array($entry[$attr])){
184 $entry[$attr] = array($entry[$attr]);
185 }
186 }
187 $entry['MODIFIED'] = FALSE;
188 $this->licenses[$entry['cn']] = $entry;
189 }
190 }
192 $this->orig_licenses = $this->licenses;
193 $this->init_successfull = TRUE;
194 return;
195 }
196 }
197 }
200 function execute()
201 {
202 // Handle initialization failures.
203 if(isset($_POST['retry_init'])) $this->init();
204 if(!$this->init_successfull){
205 $smarty = get_smarty();
206 $smarty->assign("init_successfull", $this->init_successfull);
207 return($smarty->fetch(get_template_path('licensePoolGeneric.tpl',TRUE,dirname(__FILE__))));
208 }
210 // Display dialogs!
211 if($this->dialog instanceOf plugin){
212 $this->dialog->save_object();
213 $display = $this->dialog->execute();
214 $display.= "<p style=\"text-align:right\">";
216 if($this->acl_is_writeable("licenses")){
217 $display.="<input type=submit name=\"license_finish\" style=\"width:80px\"
218 value=\"".msgPool::okButton(). "\"> ";
219 }
221 $display.="<input type=submit name=\"license_cancel\" value=\"".msgPool::cancelButton()."\"></p>";
222 return($display);
223 }
225 $smarty = get_smarty();
227 // Assign ACls
228 $plInfo = $this->plInfo();
229 foreach($plInfo['plProvidedAcls'] as $name => $desc){
230 $smarty->assign("{$name}ACL",$this->getacl($name));
231 }
232 foreach($this->attributes as $attr){
233 $smarty->assign($attr,$this->$attr);
234 }
235 $smarty->assign("init_successfull", $this->init_successfull);
236 $smarty->assign("availableProductIds", array_diff( $this->availableProductIds, $this->productIds));
237 $smarty->assign("productIds", $this->productIds);
238 $smarty->assign("softwareIds", $this->softwareIds);
239 $smarty->assign("licenses", $this->getLicenseList());
240 $smarty->assign("initially_was_account", $this->initially_was_account);
241 return($smarty->fetch(get_template_path('licensePoolGeneric.tpl',TRUE,dirname(__FILE__))));
242 }
245 function getLicenseList()
246 {
247 $list = new divSelectBox("test");
248 $list->setHeight(100);
250 if($this->acl_is_readable("licenses")){
251 foreach($this->licenses as $i => $license){
252 $keys = implode($license['licenseKey'],", ");
253 $link = "<input type='image' class='center' src='images/lists/edit.png' name='editLicense_{$i}'>";
254 if(preg_match("/w/", $this->getacl("licenses"))){
255 $link.= "<input type='image' class='center' src='images/lists/trash.png' name='removeLicense_{$i}'>";
256 }
257 $f1 = array("string" => $license['cn']);
258 $f2 = array("string" => $license['licenseModel']);
259 $f3 = array("string" => $license['expirationDate']);
260 $f4 = array("string" => $license['maximumInstallations']);
261 $f5 = array("string" => rtrim($keys,", "));
262 $f6 = array("string" => $link, "attach" => "style='border-right: 0px; width:32px;'");
263 $list->addEntry(array($f1,$f2,$f3,$f4,$f5,$f6));
264 }
265 }
266 return($list->DrawList());
267 }
270 /* Save HTML inputs
271 */
272 function save_object()
273 {
274 // Close license edit dialogs.
275 if($this->dialog instanceOf plugin && isset($_POST['license_cancel'])){
276 $this->dialog = NULL;
277 return;
278 }
280 // Save license modifications
281 if($this->dialog instanceOf plugin && isset($_POST['license_finish'])){
282 $this->dialog->save_object();
284 $msgs = $this->dialog->check();
285 if(count($msgs)){
286 msg_dialog::displayChecks($msgs);
287 }else{
288 $attrs = $this->dialog->save();
289 $attrs['MODIFIED'] = TRUE;
290 $this->licenses[$attrs['cn']] = $attrs;
291 $this->dialog = NULL;
292 }
293 return;
294 }
296 if(isset($_POST['opsiLicensePoolPosted'])){
297 plugin::save_object();
299 // Restore license cn, to avoid creating a copy...
300 if($this->initially_was_account) $this->cn = $this->orig_cn;
302 // We've to add prodcuts here
303 if(preg_match("/w/",$this->getacl("productIds"))){
304 if(isset($_POST['availableProduct']) && isset($_POST['addProduct'])){
305 $pro = get_post('availableProduct');
306 if(isset($this->availableProductIds[$pro]) && !in_array($this->availableProductIds[$pro], $this->productIds)){
307 $this->productIds[] =$this->availableProductIds[$pro];
308 }
309 }
310 }
312 // We've to remove products here
313 if(preg_match("/w/",$this->getacl("productIds"))){
314 if(isset($_POST['productIds']) && isset($_POST['removeProduct'])){
315 foreach($_POST['productIds'] as $key){
316 if(isset($this->productIds[$key])){
317 unset($this->productIds[$key]);
318 }
319 }
320 }
321 }
323 // We've to add software here
324 if(preg_match("/w/",$this->getacl("windowsSoftwareIds"))){
325 if(isset($_POST['newSoftwareId']) && isset($_POST['addSoftware'])){
326 $soft = trim(get_post('newSoftwareId'));
327 if(!empty($soft) && !in_array($soft, $this->softwareIds)){
328 $this->softwareIds[] = $soft;
329 }
330 }
331 }
333 // We've to remove software Ids here
334 if(preg_match("/w/",$this->getacl("windowsSoftwareIds"))){
335 if(isset($_POST['softwareIds']) && isset($_POST['removeSoftware'])){
336 foreach($_POST['softwareIds'] as $key){
337 if(isset($this->softwareIds[$key])){
338 unset($this->softwareIds[$key]);
339 }
340 }
341 }
342 }
344 // We've to create a new license
345 if(preg_match("/w/",$this->getacl("licenses"))){
346 if(isset($_POST['addLicense'])){
347 $this->dialog = new licenseGeneric($this->config,$this->dn,array(), $this->opsiHosts);
348 $this->dialog->set_acl_base($this->config->current['BASE']);
349 }
350 }
352 // Search post for image button clicks.
353 if(preg_match("/r/",$this->getacl("licenses"))){
354 foreach($_POST as $name => $value){
355 if(preg_match("/^editLicense_/",$name)){
356 $id = preg_replace("/^editLicense_(.*)_.$/","\\1",$name);
357 if(isset($this->licenses[$id])){
358 $this->dialog = new licenseGeneric($this->config,$this->dn,$this->licenses[$id], $this->opsiHosts);
359 $this->dialog->set_acl_base($this->config->current['BASE']);
360 }
361 break;
362 }
363 if(preg_match("/^removeLicense_/",$name)){
364 $id = preg_replace("/^removeLicense_(.*)_.$/","\\1",$name);
365 if(isset($this->licenses[$id])){
366 unset($this->licenses[$id]);
367 }
368 break;
369 }
370 }
371 }
372 }
373 }
376 /* Check user input and return a list of 'invalid input' messages.
377 */
378 function check()
379 {
380 $message = plugin::check();
381 return($message);
382 }
386 /* Removes the object from the opsi database
387 */
388 function remove_from_parent()
389 {
390 $this->si->deletePool($this->orig_cn);
391 if($this->si->is_error()){
392 msg_dialog::display(_("Error"),msgPool::siError($this->si->get_error()),ERROR_DIALOG);
393 }else{
395 // Trigger remove signal
396 $this->handle_post_events("remove");
397 }
399 new log("remove","users/".get_class($this),$this->dn,array_keys($this->attrs),$this->si->get_error());
400 }
403 /* Saves object modifications
404 */
405 function save()
406 {
407 plugin::save();
409 // Send modify/add events
410 $mode = "modify";
411 if($this->orig_dn == "new"){
412 $mode = "add";
413 }
415 // Create or update the pool
416 $this->si->createPool($this->cn, $this->description,$this->productIds,$this->softwareIds);#
417 if($this->si->is_error()){
418 msg_dialog::display(_("Error"),msgPool::siError($this->si->get_error()),ERROR_DIALOG);
419 }else{
420 $this->handle_post_events($mode);
421 }
423 // Create, remove or update licenses
424 $add = array_diff_assoc($this->licenses,$this->orig_licenses);
425 $del = array_diff_assoc($this->orig_licenses,$this->licenses);
426 $update = array_intersect($this->licenses,$this->orig_licenses);
428 // Remove licenses
429 foreach($del as $license){
430 $this->si->removeLicenseFromPool($this->cn, $license['cn']);
431 if($this->si->is_error()){
432 msg_dialog::display(_("Error"),msgPool::siError($this->si->get_error()),ERROR_DIALOG);
433 }
434 }
436 // Add licenses
437 foreach($add as $license){
438 $this->si->createLicense(
439 $this->cn, // Pool id
440 $license['cn'],
441 $license['licenseKey'][0],
442 $license['licenseModel'],
443 $license['partner'],
444 $license['conclusionDate'],
445 $license['notificationDate'],
446 $license['description'],
447 $license['cn'],
448 $license['maximumInstallations'],
449 $license['boundToHost'],
450 $license['expirationDate']);
452 if($this->si->is_error()){
453 msg_dialog::display(_("Error"),msgPool::siError($this->si->get_error()),ERROR_DIALOG);
454 }
455 }
457 // Update licenses
458 foreach($update as $license){
460 // Do not save untouched licenses
461 if(!$license['MODIFIED']){
462 continue;
463 }
465 $this->si->createLicense(
466 $this->cn, // Pool id
467 $license['cn'],
468 $license['licenseKey'][0],
469 $license['licenseModel'],
470 $license['partner'],
471 $license['conclusionDate'],
472 $license['notificationDate'],
473 $license['description'],
474 $license['cn'],
475 $license['maximumInstallations'],
476 $license['boundToHost'],
477 $license['expirationDate']);
479 if($this->si->is_error()){
480 msg_dialog::display(_("Error"),msgPool::siError($this->si->get_error()),ERROR_DIALOG);
481 }
482 }
484 // Log action
485 if($mode == "modify"){
486 new log("modify","users/".get_class($this),$this->dn,array_keys($this->attrs),$this->si->get_error());
487 }else{
488 new log("create","users/".get_class($this),$this->dn,array_keys($this->attrs),$this->si->get_error());
489 }
491 return 0;
492 }
494 static function plInfo()
495 {
496 return (array(
497 "plShortName" => _("Pool generic"),
498 "plDescription" => _("License pool generic"),
499 "plSelfModify" => FALSE,
500 "plDepends" => array(),
501 "plPriority" => 1,
502 "plSection" => array("administration"),
503 "plCategory" => array("opsi"),
504 "plProvidedAcls"=> array(
505 "cn" => _("Name"),
506 "description" => _("Description"),
507 "productIds" => _("Applications"),
508 "windowsSoftwareIds" => _("Windows software IDs"),
509 "licenses" => _("Licenses"))
510 ));
511 }
512 }
515 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
516 ?>