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 "LICENSEKEY"=> "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 $this->init_successfull = FALSE;
90 return(FALSE);
91 }
92 $this->availableProductIds=array_keys($res);
94 // Load opsi hosts
95 $res = $this->si->list_clients();
96 $this->opsiHosts=array();
97 if($this->si->is_error()){
98 $this->init_successfull = FALSE;
99 return(FALSE);
100 }
101 $this->opsiHosts=$res;
103 // Load Pool
104 if(!$this->initially_was_account){
105 $this->init_successfull = TRUE;
106 }else{
108 $res = $this->si->getPool($this->cn);
109 if($this->si->is_error()){
110 $this->init_successfull = FALSE;
111 return(FALSE);
112 }else{
113 $this->data = $this->orig_data = $res;
114 $this->description = $this->data['description'][0];
116 // Load software IDs
117 $this->softwareIds = array();
118 if(isset($this->data['softwareId'])){
119 for($i = 0; $i < $this->data['softwareId']['count']; $i++){
120 $this->softwareIds[] = $this->data['softwareId'][$i];
121 }
122 }
124 // Load product IDs
125 $this->productIds = array();
126 if(isset($this->data['productId'])){
127 for($i = 0; $i < $this->data['productId']['count']; $i++){
128 $this->productIds[] = $this->data['productId'][$i];
129 }
130 }
132 // Load Licences
133 $this->licenses = array();
134 if(isset($this->data['licenses'])){
135 for($i =0; $i< $this->data['licenses']['count']; $i++){
136 $license = $this->data['licenses'][$i];
138 // Do not parse invalid licenses
139 if(!is_array($license) || !isset($license['LICENSEPOOLIDS'])){
140 echo "Invalid license";
141 continue;
142 }
144 // Prepare Software License Key(s)
145 $upper = strtoupper($license['LICENSEPOOLIDS'][0]['VALUE']);
146 $license['LICENSEKEYS'] = $license['LICENSEKEYS'][0][$upper];
148 $entry = array();
149 foreach($this->licenseMap as $source => $target){
150 $entry[$target] = "";
151 if(isset($license[$source])){
152 if(count($license[$source]) >= 2){
153 $entry[$target] = array();
154 foreach($license[$source] as $sub){
155 $entry[$target][] = $sub['VALUE'];
156 }
157 }elseif(isset($license[$source][0]['VALUE'])){
158 $entry[$target] = $license[$source][0]['VALUE'];
159 }
160 }
161 }
163 $lData= $license['LICENSECONTRACTDATA'][0];
164 foreach($this->licenseContractMap as $source => $target){
165 if(isset($lData[$source])){
166 if(count($lData[$source]) >= 2){
167 $entry[$target] = array();
168 foreach($lData[$source] as $sub){
169 $entry[$target][] = $sub['VALUE'];
170 }
171 }elseif(isset($lData[$source][0]['VALUE'])){
172 $entry[$target] = $lData[$source][0]['VALUE'];
173 }
174 }
175 }
177 // There are some multi value attributes - force them to be of type array.
178 foreach(array("boundToHost","usedByHost","licenseKey") as $attr){
179 if(!is_array($entry[$attr])){
180 $entry[$attr] = array($entry[$attr]);
181 }
182 }
183 $this->licenses[$entry['cn']] = $entry;
184 }
185 }
187 $this->orig_licenses = $this->licenses;
188 $this->init_successfull = TRUE;
189 return;
190 }
191 }
192 }
195 function execute()
196 {
197 // Handle initialization failures.
198 if(isset($_POST['retry_init'])) $this->init();
199 if(!$this->init_successfull){
200 $smarty = get_smarty();
201 $smarty->assign("init_successfull", $this->init_successfull);
202 return($smarty->fetch(get_template_path('licensePoolGeneric.tpl',TRUE,dirname(__FILE__))));
203 }
205 // Display dialogs!
206 if($this->dialog instanceOf plugin){
207 $this->dialog->save_object();
208 $display = $this->dialog->execute();
209 $display.=
210 "<p style=\"text-align:right\">
211 <input type=submit name=\"license_finish\" style=\"width:80px\"
212 value=\"".msgPool::okButton(). "\">
213 <input type=submit name=\"license_cancel\"
214 value=\"".msgPool::cancelButton()."\">
215 </p>";
216 return($display);
217 }
219 $smarty = get_smarty();
221 // Assign ACls
222 $plInfo = $this->plInfo();
223 foreach($plInfo['plProvidedAcls'] as $name => $desc){
224 $smarty->assign("{$name}ACL",$this->getacl($name));
225 }
226 foreach($this->attributes as $attr){
227 $smarty->assign($attr,$this->$attr);
228 }
229 $smarty->assign("init_successfull", $this->init_successfull);
230 $smarty->assign("availableProductIds", array_diff( $this->availableProductIds, $this->productIds));
231 $smarty->assign("productIds", $this->productIds);
232 $smarty->assign("softwareIds", $this->softwareIds);
233 $smarty->assign("licenses", $this->getLicenseList());
234 $smarty->assign("initially_was_account", $this->initially_was_account);
235 return($smarty->fetch(get_template_path('licensePoolGeneric.tpl',TRUE,dirname(__FILE__))));
236 }
239 function getLicenseList()
240 {
241 $list = new divSelectBox("test");
242 $list->setHeight(100);
243 foreach($this->licenses as $i => $license){
244 $keys = implode($license['licenseKey'],", ");
245 $link = "<input type='image' class='center' src='images/lists/edit.png' name='editLicense_{$i}'>";
246 $link.= "<input type='image' class='center' src='images/lists/trash.png' name='removeLicense_{$i}'>";
248 $f1 = array("string" => $license['cn']);
249 $f2 = array("string" => $license['licenseModel']);
250 $f3 = array("string" => $license['expirationDate']);
251 $f4 = array("string" => $license['maximumInstallations']);
252 $f5 = array("string" => rtrim($keys,", "));
253 $f6 = array("string" => $link, "attach" => "style='border-right: 0px; width:32px;'");
255 $list->addEntry(array($f1,$f2,$f3,$f4,$f5,$f6));
256 }
257 return($list->DrawList());
258 }
261 /* Save HTML inputs
262 */
263 function save_object()
264 {
265 // Close license edit dialogs.
266 if($this->dialog instanceOf plugin && isset($_POST['license_cancel'])){
267 $this->dialog = NULL;
268 return;
269 }
271 // Save license modifications
272 if($this->dialog instanceOf plugin && isset($_POST['license_finish'])){
273 $this->dialog->save_object();
275 $msgs = $this->dialog->check();
276 if(count($msgs)){
277 msg_dialog::displayChecks($msgs);
278 }else{
279 $attrs = $this->dialog->save();
280 $this->licenses[$attrs['cn']] = $attrs;
281 $this->dialog = NULL;
282 }
283 return;
284 }
286 if(isset($_POST['opsiLicensePoolPosted'])){
287 plugin::save_object();
289 // Restore license cn, to avoid creating a copy...
290 if($this->initially_was_account) $this->cn = $this->orig_cn;
292 // We've to add prodcuts here
293 if(isset($_POST['availableProduct']) && isset($_POST['addProduct'])){
294 $pro = get_post('availableProduct');
295 if(isset($this->availableProductIds[$pro]) && !in_array($this->availableProductIds[$pro], $this->productIds)){
296 $this->productIds[] =$this->availableProductIds[$pro];
297 }
298 }
300 // We've to remove products here
301 if(isset($_POST['productIds']) && isset($_POST['removeProduct'])){
302 foreach($_POST['productIds'] as $key){
303 if(isset($this->productIds[$key])){
304 unset($this->productIds[$key]);
305 }
306 }
307 }
309 // We've to add software here
310 if(isset($_POST['newSoftwareId']) && isset($_POST['addSoftware'])){
311 $soft = trim(get_post('newSoftwareId'));
312 if(!empty($soft) && !in_array($soft, $this->softwareIds)){
313 $this->softwareIds[] = $soft;
314 }
315 }
317 // We've to remove software Ids here
318 if(isset($_POST['softwareIds']) && isset($_POST['removeSoftware'])){
319 foreach($_POST['softwareIds'] as $key){
320 if(isset($this->softwareIds[$key])){
321 unset($this->softwareIds[$key]);
322 }
323 }
324 }
326 // We've to create a new license
327 if(isset($_POST['addLicense'])){
328 $this->dialog = new licenseGeneric($this->config,array(), $this->opsiHosts);
329 $this->dialog->set_acl_base($this->config->current['BASE']);
330 }
332 // Search post for image button clicks.
333 foreach($_POST as $name => $value){
334 if(preg_match("/^editLicense_/",$name)){
335 $id = preg_replace("/^editLicense_(.*)_.$/","\\1",$name);
336 if(isset($this->licenses[$id])){
337 $this->dialog = new licenseGeneric($this->config,$this->licenses[$id], $this->opsiHosts);
338 $this->dialog->set_acl_base($this->config->current['BASE']);
339 }
340 break;
341 }
342 if(preg_match("/^removeLicense_/",$name)){
343 $id = preg_replace("/^removeLicense_(.*)_.$/","\\1",$name);
344 if(isset($this->licenses[$id])){
345 unset($this->licenses[$id]);
346 }
347 break;
348 }
349 }
350 }
351 }
354 /* Check user input and return a list of 'invalid input' messages.
355 */
356 function check()
357 {
358 $message = plugin::check();
359 return($message);
360 }
364 /* Removes the object from the opsi database
365 */
366 function remove_from_parent()
367 {
368 $this->si->deletePool($this->orig_cn);
369 if($this->si->is_error()){
370 msg_dialog::display(_("Error"),msgPool::siError($this->si->get_error()),ERROR_DIALOG);
371 }else{
373 // Trigger remove signal
374 $this->handle_post_events("remove");
375 }
377 new log("remove","users/".get_class($this),$this->dn,array_keys($this->attrs),$this->si->get_error());
378 }
381 /* Saves object modifications
382 */
383 function save()
384 {
385 plugin::save();
387 // Send modify/add events
388 $mode = "modify";
389 if($this->orig_dn == "new"){
390 $mode = "add";
391 }
393 // Create or update the pool
394 $this->si->createPool($this->cn, $this->description,$this->productIds,$this->softwareIds);#
395 if($this->si->is_error()){
396 msg_dialog::display(_("Error"),msgPool::siError($this->si->get_error()),ERROR_DIALOG);
397 }else{
398 $this->handle_post_events($mode);
399 }
401 // Create, remove or update licenses
402 $add = array_diff_assoc($this->licenses,$this->orig_licenses);
403 $del = array_diff_assoc($this->orig_licenses,$this->licenses);
405 // Remove licenses
406 foreach($del as $license){
407 $this->si->removeLicenseFromPool($this->cn, $license['cn']);
408 if($this->si->is_error()){
409 msg_dialog::display(_("Error"),msgPool::siError($this->si->get_error()),ERROR_DIALOG);
410 }
411 }
413 // Add / update licenses
414 foreach($add as $license){
415 $this->si->createLicense(
416 $this->cn, // Pool id
417 $license['cn'],
418 $license['licenseKey'][0],
419 $license['licenseModel'],
420 $license['partner'],
421 $license['conclusionDate'],
422 $license['notificationDate'],
423 $license['description'],
424 $license['cn'],
425 $license['maximumInstallations'],
426 $license['boundToHost'],
427 $license['expirationDate']);
428 if($this->si->is_error()){
429 msg_dialog::display(_("Error"),msgPool::siError($this->si->get_error()),ERROR_DIALOG);
430 }
431 }
433 // Log action
434 if($mode == "modify"){
435 new log("modify","users/".get_class($this),$this->dn,array_keys($this->attrs),$this->si->get_error());
436 }else{
437 new log("create","users/".get_class($this),$this->dn,array_keys($this->attrs),$this->si->get_error());
438 }
440 return 0;
441 }
443 static function plInfo()
444 {
445 return (array(
446 "plShortName" => _("Generic"),
447 "plDescription" => _("License generic"),
448 "plSelfModify" => FALSE,
449 "plDepends" => array(),
450 "plPriority" => 1,
451 "plSection" => array("administration"),
452 "plCategory" => array("opsi"),
453 "plProvidedAcls"=> array(
454 "cn" => _("Name"),
455 "description" => _("Description"))
456 ));
457 }
458 }
461 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
462 ?>