Code

Replaced in_array calls for gosa-plugins
[gosa.git] / gosa-plugins / squid / personal / connectivity / squid / class_proxyAccount.inc
1 <?php
2 class proxyAccount extends plugin
3 {
4   /* Definitions */
5   var $plHeadline   = "Proxy";
6   var $plDescription= "This does something";
8   /* Proxy attributes */
9   var $gosaProxyAcctFlags   = "[N    ]";
10   var $gosaProxyID          = "";
11   var $gosaProxyWorkingStart= 420;
12   var $gosaProxyWorkingStop = 1020;
13   var $gosaProxyQuota       = "5g";
14   var $gosaProxyQuotaPeriod = "m";
16   /* attribute list for save action */
17   var $attributes   = array("gosaProxyID", "gosaProxyAcctFlags", "gosaProxyWorkingStart",
18                             "gosaProxyWorkingStop", "gosaProxyQuota", "gosaProxyQuotaPeriod");
20   var $objectclasses    = array("gosaProxyAccount");
21   var $ReadOnly         = false;
22   var $uid              = "";
23   var $view_logged      = FALSE;
24   var $multiple_support = TRUE;
26   /*! \brief Initialize proxy acoount 
27       @param $config GOsa configuration object 
28       @param $dn     Object dn
29    */
30   public function proxyAccount (&$config, $dn= NULL)
31   {
32     plugin::plugin ($config, $dn);
33     
34     /* Setting uid to default */
35     if(isset($this->attrs['uid'][0])){
36       $this->uid = $this->attrs['uid'][0];
37     }
38   }
41   /*! \brief Create html output for this class 
42    */
43   public function execute()
44   {
45     /* Call parent execute */
46     plugin::execute();
48     /* Log view */
49     if($this->is_account && !$this->view_logged){
50       $this->view_logged = TRUE;
51       new log("view","users/".get_class($this),$this->dn);
52     }
54     $display= "";
55     $smarty= get_smarty();
56     
57     /* Assign radio boxes */
58     foreach (array("F", "T", "B", "N") as $val){
59       if (preg_match("/".$val."/",$this->gosaProxyAcctFlags)){
60         $smarty->assign("filter$val", "checked");
61         $smarty->assign($val."state", "");
62       } else {
63         $smarty->assign("filter$val", "");
64         if(session::get('js')==1){
65           $smarty->assign($val."state", "disabled");
66         }else{
67           $smarty->assign($val."state", "");
68         }
69       }
70     }
72     /* Assign ACLs */
73     $tmp = $this->plInfo();
74     foreach($tmp['plProvidedAcls'] as $acl => $desc){
75       $smarty->assign($acl."ACL",$this->getacl($acl,$this->ReadOnly));
76       $smarty->assign($acl."_W",$this->acl_is_writeable($acl,$this->ReadOnly));
77     }
79     /* Assign working time */
80     $smarty->assign("starthour" ,floor($this->gosaProxyWorkingStart / 60));
81     $smarty->assign("startminute", ($this->gosaProxyWorkingStart % 60));
82     $smarty->assign("stophour", floor($this->gosaProxyWorkingStop / 60));
83     $smarty->assign("stopminute", ($this->gosaProxyWorkingStop % 60));
84     $hours= array();
85     for($i=0; $i<24; $i++){
86       $hours[]= sprintf("%02d",$i);
87     }
88     $smarty->assign("hours", $hours);
89     $smarty->assign("minutes", array("00","15","30","45"));
91     /* Assign quota values */
92     $smarty->assign("quota_unit", array("k" => _("KB"), "m" => _("MB"), "g" => _("GB")));
93     $smarty->assign("quota_time", array("h" => _("hour"), "d" => _("day"), "w" => _("week"), "m" => _("month")));
94     $smarty->assign("gosaProxyQuotaPeriod", $this->gosaProxyQuotaPeriod);
95     $smarty->assign("quota_size", preg_replace("/[a-z]$/i", "", $this->gosaProxyQuota));
96     $smarty->assign("quota_u", preg_replace("/^[0-9]+/", "", $this->gosaProxyQuota));
97     if ($this->is_account){
98       $smarty->assign("proxyState", "checked");
99     } else {
100       $smarty->assign("proxyState", "");
101     }
104     /* Handle input grey out and javascript enabled/disable of input fields
105      */
106     if($this->multiple_support_active){
108       /* In Multiple edit, everything is enabled */
109       $changeB = "";
110       $smarty->assign("pstate", "");
111       $smarty->assign("ProxyWorkingStateChange","");
112     }else{
114       /* Depeding on the account status, we disable or 
115        *  enable all input fields 
116        */
117       if (!$this->is_account){
118         $smarty->assign("pstate", "disabled");
119       } else {
120         $smarty->assign("pstate", "");
121       }
123       /* Create JS activation string for everal input fields  */
124       $ProxyWorkingStateChange ="\n";
125       if($this->acl_is_writeable("gosaProxyFlagT")){
126         $ProxyWorkingStateChange.=  "changeState('startHour'); \n";
127         $ProxyWorkingStateChange.=  "changeState('startMinute'); \n";
128         $ProxyWorkingStateChange.=  "changeState('stopHour'); \n";
129         $ProxyWorkingStateChange.=  "changeState('stopMinute'); \n";
130       }
131       $smarty->assign("ProxyWorkingStateChange",$ProxyWorkingStateChange);
132       $changeB = "";
133       if($this->acl_is_writeable("gosaProxyFlagB")){
134         $changeB = 
135           "changeSubselectState('filterB', 'quota_size');
136         changeSubselectState('filterB', 'quota_unit');
137         changeSubselectState('filterB', 'gosaProxyQuotaPeriod');";
138       }
139     }
141     /* Assign filter settings */
142     $smarty->assign("changeB",$changeB);
143     foreach(array("T","B","F") as $attr){
144       if(in_array_strict("filter".$attr,$this->multi_boxes)){
145         $smarty->assign("use_filter".$attr,TRUE);
146       }else{
147         $smarty->assign("use_filter".$attr,FALSE);
148       }
149     }
151     /* check if we are allowed to switch the checkbox.
152      */
153     $smarty->assign('proxyAccountACL', preg_replace("/w/","",$this->getacl("",$this->ReadOnly)));
154     if(($this->acl_is_removeable() && $this->is_account) ||
155         ($this->acl_is_createable() && !$this->is_account)){
156       $smarty->assign('proxyAccountACL', $this->getacl("",$this->ReadOnly));
157     }
159     $smarty->assign("use_proxy",in_array_strict("proxy",$this->multi_boxes));
160     $smarty->assign("multiple_support",$this->multiple_support_active);
161     $display.= $smarty->fetch(get_template_path('proxy.tpl', TRUE, dirname(__FILE__)));
162     return($display);
163   }
166   /*! \brief Removes proxy account from current object
167    */
168   public function remove_from_parent()
169   {
170     if($this->acl_is_removeable() && $this->initially_was_account){   
171       plugin::remove_from_parent();
172       $ldap= $this->config->get_ldap_link();
173       @DEBUG (DEBUG_LDAP, __LINE__, __FUNCTION__, __FILE__,
174           $this->attributes, "Save");
175       $ldap->cd($this->dn);
176       $this->cleanup();
177       $ldap->modify ($this->attrs); 
179       /* Log last action */
180       new log("remove","users/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
182       if (!$ldap->success()){
183         msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), $this->dn, LDAP_MOD, get_class()));
184       }
186       /* Optionally execute a command after we're done */
187       $this->handle_post_events("remove",array("uid" => $this->uid));
188     }
189   }
192   /*! \brief Check given input
193       @return array Returns an array of error messages  
194    */
195   public function check()
196   {
197     /* Call common method to give check the hook */
198     $message= plugin::check();
199     
200     /* We've got only one value to check for positive integer or emtpy field */
201     if ($this->is_account && $this->acl_is_writeable("gosaProxyQuota")){
202       if (isset($_POST["quota_size"])){
203         if ($_POST["quota_size"] == "gosaProxyQuota"){
204           $message[]= msgPool::invalid(_("Quota Setting"));
205         }elseif ($_POST["quota_size"] <= 0){
206           $message[]= msgPool::invalid(_("Quota Setting"),$_POST["quota_size"],"/^[0-9]/");
207         }
208       }
209     }
210     return $message;
211   }
213   /*! \brief Save POST data to object 
214    */
215   public function save_object()
216   {
217     /* Do we need to flip is_account state? */
218     if (isset($_POST['connectivityTab'])){
219       if (isset($_POST['proxy'])){
220         if (!$this->is_account && $_POST['proxy'] == "B"){
221           if($this->acl_is_createable()){
222             $this->is_account= TRUE;
223           }
224         }
225       } else {
226         if($this->acl_is_removeable()){
227           $this->is_account= FALSE;
228         }
229       }
230     }
232     /* Save flag value */
233     if ($this->is_account || $this->multiple_support_active){
235       $flags= "";
236       $acl= "";
237       foreach(array("F", "T", "B") as $key){
238         if($this->acl_is_writeable("gosaProxyFlag".$key)){
239   
240           /* Add acl */
241           if (isset($_POST["filter$key"])){
242             $flags.= $key;
243           }
244         }else{
245           
246           /* Keep all flags that can't be written*/
247           if(preg_match("/".$key."/",$this->gosaProxyAcctFlags)){
248             $flags .=$key;
249           }
250         }
251       }
252       if ("[$flags]" != $this->gosaProxyAcctFlags){
253         $this->is_modified= TRUE;
254       }
255       $this->gosaProxyAcctFlags= "[$flags]";
257       /* Save time values */
258       if ($this->acl_is_writeable("gosaProxyFlagT")){
259         if(isset($_POST['startMinute'])){
260           $old= $this->gosaProxyWorkingStart;
261           $this->gosaProxyWorkingStart= $_POST["startHour"] * 60 + $_POST["startMinute"];
262           $this->is_modified= ($old != $this->gosaProxyWorkingStart)?TRUE:$this->is_modified;
263         }
265         if (isset($_POST['stopMinute'])){
266           $old= $this->gosaProxyWorkingStop;
267           $this->gosaProxyWorkingStop = $_POST["stopHour"]  * 60 + $_POST["stopMinute"];
268           $this->is_modified= ($old != $this->gosaProxyWorkingStop)?TRUE:$this->is_modified;
269         }
270       }
272       /* Save quota values */
273       if ($this->acl_is_writeable("gosaProxyFlagB")){
274         if(isset($_POST["quota_size"]) && isset($_POST["quota_unit"])){
275           $this->gosaProxyQuota= $_POST["quota_size"].$_POST["quota_unit"];
276         }
277       }
278   
279       /*Save quota period */
280       if($this->acl_is_writeable("gosaProxyFlagB")){
281         if(isset($_POST["gosaProxyQuotaPeriod"])){
282           $this->gosaProxyQuotaPeriod = $_POST["gosaProxyQuotaPeriod"];
283         }
284       }
285     }
286   }
289   /*! \brief Save settings to ldap 
290    */
291   public function save()
292   {
293     plugin::save();
295     /* Write back to ldap */
296     $ldap= $this->config->get_ldap_link();
297     $ldap->cd($this->dn);
298     $this->cleanup();
299     $ldap->modify ($this->attrs); 
301     /* Log last action */
302     if($this->initially_was_account){
303       new log("modify","users/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
304     }else{
305       new log("create","users/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
306     }
308     if (!$ldap->success()){
309       msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), $this->dn, LDAP_MOD, get_class()));
310     }
312     /* Optionally execute a command after we're done */
313     if ($this->initially_was_account == $this->is_account){
314       if ($this->is_modified){
315         $this->handle_post_events("modify",array("uid" => $this->uid));
316       }
317     } else {
318       $this->handle_post_events("add",array("uid" => $this->uid));
319     }
321   }
323     
324   /*! \brief  Static  Function returning an ACL information array.
325       @return Array   Returns an ACL array 
326    */
327   static function plInfo()
328   {
329     return (array(
330           "plShortName"     => _("Proxy"),
331           "plDescription"   => _("Proxy account")."&nbsp;("._("Connectivity addon").")",
332           "plSelfModify"    => TRUE,
333           "plDepends"       => array("user"),
334           "plPriority"      => 21,                                 // Position in tabs
335           "plSection"     => array("personal" => _("My account")),
336           "plCategory"    => array("users"),
337           "plOptions"       => array(),
339           "plProvidedAcls"  => array(
340             "gosaProxyFlagF"       => _("Filter unwanted content"), 
341             "gosaProxyFlagT"       => _("Limit proxy access"),
342             "gosaProxyFlagB"       => _("Restrict proxy usage by quota"))
343           ));
344   }
347   /*! \brief  Save html POSTs in multiple edit.
348    */
349   public function multiple_save_object()
350   {
351     if (isset($_POST['connectivityTab'])){
352       plugin::multiple_save_object();
353       if(isset($_POST['use_proxy'])){
354         $this->multi_boxes[] = "proxy";
355       }
356       foreach(array("T","B","F") as $attr){
357         if(isset($_POST["use_filter".$attr])){
358           $this->multi_boxes[] = "filter".$attr;
359         }
360       }
361       $this->save_object();
362     }
363   }
366   /*! \brief  Returns all modified values. \
367               All selected an modified values will be returned \
368               in an array.
369       @return array   Returns an array containing all attribute modifications  
370    */
371   public function get_multi_edit_values()
372   {
373     $ret = plugin::get_multi_edit_values();
374     if(in_array_strict("proxy",$this->multi_boxes)){
375       $ret['is_account'] = $this->is_account;
376     }
378     if(in_array_strict("filterT",$this->multi_boxes)){
379       $ret['gosaProxyWorkingStart']  = $this->gosaProxyWorkingStart;
380       $ret['gosaProxyWorkingStop']   = $this->gosaProxyWorkingStop;
381     }
382     if(in_array_strict("filterB",$this->multi_boxes)){
383       $ret['gosaProxyQuota']        = $this->gosaProxyQuota;
384       $ret['gosaProxyQuotaPeriod']  = $this->gosaProxyQuotaPeriod;
385     }
387     foreach(array("B","T","F") as $attr){
388       $name = "filter".$attr;
389       if(in_array_strict($name,$this->multi_boxes)){
390         $ret[$name] = preg_match("/".$attr."/",$this->gosaProxyAcctFlags);
391       }
392     }
394     return($ret);
395   }
398   /*! \brief  Sets modified attributes in mutliple edit. \
399               All collected values from "get_multi_edit_values()" \
400               will be applied to this plugin.
401       @param  array   An array containing modified attributes returned by get_multi_edit_values();
402    */
403   public function set_multi_edit_values($values)
404   {
405     plugin::set_multi_edit_values($values);
406     if(isset($values['is_account'])){
407       $this->is_account = $values['is_account'];
408     }
409     foreach(array("B","T","F") as $attr){
410       $name = "filter".$attr;
411       if(isset($values[$name])){
412         if($values[$name] && !preg_match("/".$attr."/",$this->gosaProxyAcctFlags)){
413           $this->gosaProxyAcctFlags = preg_replace("/\]/",$attr."]",$this->gosaProxyAcctFlags);
414         }elseif(!$values[$name] && preg_match("/".$attr."/",$this->gosaProxyAcctFlags)){
415           $this->gosaProxyAcctFlags = preg_replace("/".$attr."/","",$this->gosaProxyAcctFlags);
416         }
417       }
418     }
419   }
422   /*! \brief  Initialize multiple edit ui for this plugin. \
423               This function sets plugin defaults in multiple edit.
424       @param  array   Attributes used in all object 
425       @param  array   All used attributes.
426    */
427   public function init_multiple_support($attrs,$all)
428   {
429     plugin::init_multiple_support($attrs,$all);
430     if(isset($attrs['objectClass']) && in_array_strict("gosaProxyAccount",$attrs['objectClass'])){
431       $this->is_account = TRUE;
432     }
433   }
436 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
437 ?>