Code

Updated message strings, replace with msgPool
[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" ,($this->gosaProxyWorkingStart / 60));
81     $smarty->assign("startminute", ($this->gosaProxyWorkingStart % 60));
82     $smarty->assign("stophour", ($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("filter".$attr,$this->multi_boxes)){
145         $smarty->assign("use_filter".$attr,TRUE);
146       }else{
147         $smarty->assign("use_filter".$attr,FALSE);
148       }
149     }
150     $smarty->assign("proxyAccountACL",$this->getacl("",$this->ReadOnly));
151     $smarty->assign("use_proxy",in_array("proxy",$this->multi_boxes));
152     $smarty->assign("multiple_support",$this->multiple_support_active);
153     $display.= $smarty->fetch(get_template_path('proxy.tpl', TRUE, dirname(__FILE__)));
154     return($display);
155   }
158   /*! \brief Removes proxy account from current object
159    */
160   public function remove_from_parent()
161   {
162     if($this->acl_is_removeable() && $this->initially_was_account){   
163       plugin::remove_from_parent();
164       $ldap= $this->config->get_ldap_link();
165       @DEBUG (DEBUG_LDAP, __LINE__, __FUNCTION__, __FILE__,
166           $this->attributes, "Save");
167       $ldap->cd($this->dn);
168       $this->cleanup();
169       $ldap->modify ($this->attrs); 
171       /* Log last action */
172       new log("remove","users/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
174       show_ldap_error($ldap->get_error(), sprintf(_("Removing of user/proxy account with dn '%s' failed."),$this->dn));
176       /* Optionally execute a command after we're done */
177       $this->handle_post_events("remove",array("uid" => $this->uid));
178     }
179   }
182   /*! \brief Check given input
183       @return array Returns an array of error messages  
184    */
185   public function check()
186   {
187     /* Call common method to give check the hook */
188     $message= plugin::check();
189     
190     /* We've got only one value to check for positive integer or emtpy field */
191     if ($this->is_account && $this->acl_is_writeable("gosaProxyQuota")){
192       if (isset($_POST["quota_size"])){
193         if ($_POST["quota_size"] == "gosaProxyQuota"){
194           $message[]= msgPool::invalid(_("Quota Setting"));
195         }elseif ($_POST["quota_size"] <= 0){
196           $message[]= msgPool::invalid(_("Quota Setting"),$_POST["quota_size"],"/^[0-9]/");
197         }
198       }
199     }
200     return $message;
201   }
203   /*! \brief Save POST data to object 
204    */
205   public function save_object()
206   {
207     /* Do we need to flip is_account state? */
208     if (isset($_POST['connectivityTab'])){
209       if (isset($_POST['proxy'])){
210         if (!$this->is_account && $_POST['proxy'] == "B"){
211           if($this->acl_is_createable()){
212             $this->is_account= TRUE;
213           }
214         }
215       } else {
216         if($this->acl_is_removeable()){
217           $this->is_account= FALSE;
218         }
219       }
220     }
222     /* Save flag value */
223     if ($this->is_account || $this->multiple_support_active){
225       $flags= "";
226       $acl= "";
227       foreach(array("F", "T", "B") as $key){
228         if($this->acl_is_writeable("gosaProxyFlag".$key)){
229   
230           /* Add acl */
231           if (isset($_POST["filter$key"])){
232             $flags.= $key;
233           }
234         }else{
235           
236           /* Keep all flags that can't be written*/
237           if(preg_match("/".$key."/",$this->gosaProxyAcctFlags)){
238             $flags .=$key;
239           }
240         }
241       }
242       if ("[$flags]" != $this->gosaProxyAcctFlags){
243         $this->is_modified= TRUE;
244       }
245       $this->gosaProxyAcctFlags= "[$flags]";
247       /* Save time values */
248       if ($this->acl_is_writeable("gosaProxyFlagT")){
249         if(isset($_POST['startMinute'])){
250           $old= $this->gosaProxyWorkingStart;
251           $this->gosaProxyWorkingStart= $_POST["startHour"] * 60 + $_POST["startMinute"];
252           $this->is_modified= ($old != $this->gosaProxyWorkingStart)?TRUE:$this->is_modified;
253         }
255         if (isset($_POST['stopMinute'])){
256           $old= $this->gosaProxyWorkingStop;
257           $this->gosaProxyWorkingStop = $_POST["stopHour"]  * 60 + $_POST["stopMinute"];
258           $this->is_modified= ($old != $this->gosaProxyWorkingStop)?TRUE:$this->is_modified;
259         }
260       }
262       /* Save quota values */
263       if ($this->acl_is_writeable("gosaProxyFlagB")){
264         if(isset($_POST["quota_size"]) && isset($_POST["quota_unit"])){
265           $this->gosaProxyQuota= $_POST["quota_size"].$_POST["quota_unit"];
266         }
267       }
268   
269       /*Save quota period */
270       if($this->acl_is_writeable("gosaProxyFlagB")){
271         if(isset($_POST["gosaProxyQuotaPeriod"])){
272           $this->gosaProxyQuotaPeriod = $_POST["gosaProxyQuotaPeriod"];
273         }
274       }
275     }
276   }
279   /*! \brief Save settings to ldap 
280    */
281   public function save()
282   {
283     plugin::save();
285     /* Write back to ldap */
286     $ldap= $this->config->get_ldap_link();
287     $ldap->cd($this->dn);
288     $this->cleanup();
289     $ldap->modify ($this->attrs); 
291     /* Log last action */
292     if($this->initially_was_account){
293       new log("modify","users/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
294     }else{
295       new log("create","users/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
296     }
298     show_ldap_error($ldap->get_error(), sprintf(_("Saving of user/proxy account with dn '%s' failed."),$this->dn));
300     /* Optionally execute a command after we're done */
301     if ($this->initially_was_account == $this->is_account){
302       if ($this->is_modified){
303         $this->handle_post_events("modify",array("uid" => $this->uid));
304       }
305     } else {
306       $this->handle_post_events("add",array("uid" => $this->uid));
307     }
309   }
311     
312   /*! \brief  Static  Function returning an ACL information array.
313       @return Array   Returns an ACL array 
314    */
315   static function plInfo()
316   {
317     return (array(
318           "plShortName"     => _("Proxy"),
319           "plDescription"   => _("Proxy account")."&nbsp;:&nbsp;<u>"._("Connectivity addon")."</u>",
320           "plSelfModify"    => TRUE,
321           "plDepends"       => array("user"),
322           "plPriority"      => 21,                                 // Position in tabs
323           "plSection"     => array("personal" => _("My account")),
324           "plCategory"    => array("users"),
325           "plOptions"       => array(),
327           "plProvidedAcls"  => array(
328             "gosaProxyFlagF"       => _("Filter unwanted content"), 
329             "gosaProxyFlagT"       => _("Limit proxy access"),
330             "gosaProxyFlagB"       => _("Restrict proxy usage by quota"))
331           ));
332   }
335   /*! \brief  Save html POSTs in multiple edit.
336    */
337   public function multiple_save_object()
338   {
339     if (isset($_POST['connectivityTab'])){
340       plugin::multiple_save_object();
341       if(isset($_POST['use_proxy'])){
342         $this->multi_boxes[] = "proxy";
343       }
344       foreach(array("T","B","F") as $attr){
345         if(isset($_POST["use_filter".$attr])){
346           $this->multi_boxes[] = "filter".$attr;
347         }
348       }
349       $this->save_object();
350     }
351   }
354   /*! \brief  Returns all modified values. \
355               All selected an modified values will be returned \
356               in an array.
357       @return array   Returns an array containing all attribute modifications  
358    */
359   public function get_multi_edit_values()
360   {
361     $ret = plugin::get_multi_edit_values();
362     if(in_array("proxy",$this->multi_boxes)){
363       $ret['is_account'] = $this->is_account;
364     }
366     if(in_array("filterT",$this->multi_boxes)){
367       $ret['gosaProxyWorkingStart']  = $this->gosaProxyWorkingStart;
368       $ret['gosaProxyWorkingStop']   = $this->gosaProxyWorkingStop;
369     }
370     if(in_array("filterB",$this->multi_boxes)){
371       $ret['gosaProxyQuota']        = $this->gosaProxyQuota;
372       $ret['gosaProxyQuotaPeriod']  = $this->gosaProxyQuotaPeriod;
373     }
375     foreach(array("B","T","F") as $attr){
376       $name = "filter".$attr;
377       if(in_array($name,$this->multi_boxes)){
378         $ret[$name] = preg_match("/".$attr."/",$this->gosaProxyAcctFlags);
379       }
380     }
382     return($ret);
383   }
386   /*! \brief  Sets modified attributes in mutliple edit. \
387               All collected values from "get_multi_edit_values()" \
388               will be applied to this plugin.
389       @param  array   An array containing modified attributes returned by get_multi_edit_values();
390    */
391   public function set_multi_edit_values($values)
392   {
393     plugin::set_multi_edit_values($values);
394     if(isset($values['is_account'])){
395       $this->is_account = $values['is_account'];
396     }
397     foreach(array("B","T","F") as $attr){
398       $name = "filter".$attr;
399       if(isset($values[$name])){
400         if($values[$name] && !preg_match("/".$attr."/",$this->gosaProxyAcctFlags)){
401           $this->gosaProxyAcctFlags = preg_replace("/\]/",$attr."]",$this->gosaProxyAcctFlags);
402         }elseif(!$values[$name] && preg_match("/".$attr."/",$this->gosaProxyAcctFlags)){
403           $this->gosaProxyAcctFlags = preg_replace("/".$attr."/","",$this->gosaProxyAcctFlags);
404         }
405       }
406     }
407   }
410   /*! \brief  Initialize multiple edit ui for this plugin. \
411               This function sets plugin defaults in multiple edit.
412       @param  array   Attributes used in all object 
413       @param  array   All used attributes.
414    */
415   public function init_multiple_support($attrs,$all)
416   {
417     plugin::init_multiple_support($attrs,$all);
418     if(isset($attrs['objectClass']) && in_array("gosaProxyAccount",$attrs['objectClass'])){
419       $this->is_account = TRUE;
420     }
421   }
424 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
425 ?>