Code

Replaced in_array calls with in_array_strict
[gosa.git] / gosa-plugins / opsi / admin / opsiLicenses / class_licenseGeneric.inc
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 licenseGeneric extends plugin 
25 {
27     // License attributes
28     var $si = NULL;
29     var $data = array();
30     var $cn = "";
31     var $orig_cn = "";
32     var $description = "";  
33     var $partner = "";
35     // Date attributes 
36     var $conclusionDate = "";
37     var $expirationDate = "";
38     var $notificationDate = "";
40     // License model related attribues
41     var $licenseModel = "";
42     var $licenseKey = array();
43     var $orig_licenseModel = "";
44     var $licensePoolId = "";
45     var $boundToHost= array(); // Reserved for Host.   
46     var $usedByHost = array(); // Used by Host.   
48     var $maximumInstallations = 0;
49     var $opsiHosts; 
51     var $attributes = array(
52             "cn","description","partner","conclusionDate","expirationDate",
53             "notificationDate","licenseModel","licenseKey","maximumInstallations",
54             "licensePoolId", "usedByHost","boundToHost");
56     function __construct(&$config, $dn, $license, $hosts = array())
57     {
58         $this->initTime = microtime(TRUE);
59         $this->config = $config;
60         $this->data = $license;
61         $this->dn = $dn;
62         $this->si = new opsiLicenceHandler($this->config);
63         $this->opsiHosts = $hosts;
65         // Detect account state.
66         if(count($this->data) == 0){
67             $this->initially_was_account = FALSE;
68         }else{
69             $this->initially_was_account = TRUE;
70         }
71         $this->is_account = TRUE;
73         // Extract pool name out of the fake dn.
74         $this->init();
76         // Create statistic table entry
77         stats::log('plugin', $class = get_class($this), $category = array($this->acl_category),  $action = 'open',
78                 $amount = 1, $duration = (microtime(TRUE) - $this->initTime));
79     }
82     function init()
83     {
84         // Extract license information out of the license object ($this->data)
85         $this->boundToHost = array('0'=>"");
86         $this->usedByHost = array('0'=>"");
87         $this->licenseKey = array('0'=>"");
88         if($this->initially_was_account){
89             foreach($this->attributes as $attr){
90                 $this->$attr = $this->data[$attr];
91             }
93             // Fix dates 
94             foreach(array("notificationDate","expirationDate","conclusionDate") as $date) {
95                 if(!empty($this->$date)){
96                     $this->$date = date("d.m.Y",strtotime($this->$date));
97                 }
98             }
99         }
101         $this->orig_cn = $this->cn;
102         $this->orig_licenseModel = $this->licenseModel;
103         $this->init_successfull = TRUE;
104         return;    
105     }
108     function execute()
109     {
110         plugin::execute();
111         // Handle initialization failures.
112         if(isset($_POST['retry_init'])) $this->init();
113         if(!$this->init_successfull){
114             $smarty = get_smarty();
115             $smarty->assign("init_successfull", $this->init_successfull);
116             return($smarty->fetch(get_template_path('licenseGeneric.tpl',TRUE,dirname(__FILE__))));
117         }
119         $smarty = get_smarty();
121         // Assign attributes and its ACls 
122         $plInfo = $this->plInfo();
123         foreach($plInfo['plProvidedAcls'] as $name => $desc){
124             $smarty->assign("{$name}ACL",$this->getacl($name));
125         }
126         foreach($this->attributes as $attr){
127             $smarty->assign($attr,set_post($this->$attr));
128         }
130         // Assign list of available license models
131         $smarty->assign("licenseModels",array(
132                     "RETAIL" => _("Retail"),
133                     "OEM"=>_("OEM"),
134                     "VOLUME" => _("Volume")));
137         $smarty->assign("init_successfull", $this->init_successfull);
138         $smarty->assign("initially_was_account", $this->initially_was_account);
139         $smarty->assign("hosts", $this->getHosts());
141         $ui = get_userinfo();
142         $acl_base = $this->dn;
143         if($acl_base == "new"){
144             $acl_base = $this->config->current['BASE'];
145         }
146         $smarty->assign("licenseACL", $ui->get_permissions($acl_base,"opsi/licensePoolGeneric","licenses"));
147         $smarty->assign("writeable", preg_match("/w/",$ui->get_permissions($acl_base,"opsi/licensePoolGeneric","licenses")));
148         $smarty->assign("notUsedHosts", array_diff($this->getHosts(), $this->usedByHost));
149         $smarty->assign("boundToHost", set_post($this->boundToHost[0]));
150         $smarty->assign("licenseKey", set_post($this->licenseKey[0]));
152         // w3c fix, do not show empty options.
153         $tmp = $this->usedByHost;
154         if(isset($tmp[0]) && empty($tmp[0])) unset($tmp[0]);
155         $smarty->assign('usedByHost', $tmp);
157         foreach(array("notificationDate","expirationDate","conclusionDate") as $date) {
158             $smarty->assign($date."Writeable", $this->acl_is_writeable($date));
159         }
161         return($smarty->fetch(get_template_path('licenseGeneric.tpl',TRUE,dirname(__FILE__))));
162     }
165     function getHosts()
166     {
167         $ret = array();
168         foreach($this->opsiHosts as $host){
169             $cn = $host['NAME'][0]['VALUE'];
170             $ret[$cn] = $cn;
171         }
172         return($ret);
173     }
177     /* Save HTML inputs
178      */
179     function save_object()
180     {
182         if(isset($_POST['opsiLicensesPosted'])){
183             plugin::save_object();
185             if(isset($_POST['addLicenseUsage']) && isset($_POST['selectedHostToAdd'])){
186                 $host = get_post('selectedHostToAdd');
187                 if(!empty($host) && 
188                         in_array_strict($host,$this->getHosts()) && 
189                         !in_array_strict($host, $this->usedByHost)){
190                     $this->usedByHost[] = $host;
191                 }
192             }
194             if(isset($_POST['removeLicenseUsage']) && isset($_POST['selectedUsedHosts'])){
195                 $todel = get_post('selectedUsedHosts');
196                 foreach($todel as $host){
197                     if(isset($this->usedByHost[$host])){
198                         unset($this->usedByHost[$host]);
199                     }
200                 }
201             }
203             // Force licenseKey to be of type array.
204             if(!is_array($this->licenseKey)){
205                 $this->licenseKey = array($this->licenseKey);
206             }
208             // BoundToHost maybe multiple too, later.
209             if(!is_array($this->boundToHost)){
210                 $this->boundToHost = array($this->boundToHost);
211             }    
213             if($this->initially_was_account){
214                 $this->cn = $this->orig_cn;
215                 $this->licenseModel = $this->orig_licenseModel;
216             }
217         }
218     }  
221     /* Check user input and return a list of 'invalid input' messages.
222      */
223     function check()
224     {
225         $message = plugin::check();
227         // Very simple date input checks
228         if(!empty($this->expirationDate) && 
229                 !preg_match("/^[0-9]{2}\.[0-9]{2}\.[0-9]{4}$/",$this->expirationDate)){
230             $message[] = msgPool::invalid(_("Expiration date"),$this->expirationDate,"","23.02.2009");
231         }
232         if(!empty($this->conclusionDate) && 
233                 !preg_match("/^[0-9]{2}\.[0-9]{2}\.[0-9]{4}$/",$this->conclusionDate)){
234             $message[] = msgPool::invalid(_("Expiration date"),$this->conclusionDate,"","23.02.2009");
235         }
236         if(!empty($this->notificationDate) && 
237                 !preg_match("/^[0-9]{2}\.[0-9]{2}\.[0-9]{4}$/",$this->notificationDate)){
238             $message[] = msgPool::invalid(_("Expiration date"),$this->notificationDate,"","23.02.2009");
239         }
241         if(empty($this->cn)){
242             $message[] = msgPool::required(_("Name"));
243         }
245         if(empty($this->licenseKey[0])){
246             $message[] = msgPool::required(_("License key"));
247         }
249         return($message);
250     }
253     /* Removes the object from the opsi database
254      */ 
255     function remove_from_parent() {}
258     /* Saves object modifications
259      */  
260     function save()
261     {
262         $data = array();
263         foreach($this->attributes as $target){
264             $data[$target] = $this->$target;
265         }
267         // Return opsi like dates.
268         foreach(array("notificationDate","expirationDate","conclusionDate") as $date) {
269             if(!empty($this->$date)){
270                 $data[$date] = date("Y-m-d",strtotime($this->$date));
271             }
272         }
274         return($data);
275     }
277     static function plInfo()
278     {
279         return (array(
280                     "plShortName"   => _("Generic"),
281                     "plDescription" => _("License generic"),
282                     "plSelfModify"  => FALSE,
283                     "plDepends"     => array(),
284                     "plPriority"    => 8,
285                     "plSection"     => array("administration"),
286                     "plCategory"    => array("opsi"),
287                     "plProvidedAcls"=> array(
288                         "cn"                  => _("Name"),
289                         "partner"             => _("Partner"),
290                         "licenseKey"          => _("License key"),
291                         "licenseModel"        => _("License model"),
292                         "licensePoolId"       => _("License pool id"),
293                         "maximumInstallations"=> _("Maximum installations"),
294                         "boundToHost"         => _("Reserved for host"),
295                         "usedByHost"          => _("Usage"),
296                         "notificationDate"    => _("Notification date"),
297                         "conclusionDate"      => _("Conclusion date"),
298                         "expirationDate"      => _("Expiration date"),
299                         "description"         => _("Description"))
300                         ));
301     }
305 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
306 ?>