Code

Moved 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" ,($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){
192       if($this->acl_is_writeable("gosaProxyQuota")){
193         if (isset($_POST["quota_size"])){
194           if ($_POST["quota_size"] == "gosaProxyQuota"){
195             $message[]= _("Numerical value for Quota Setting is empty.");
196           }elseif ($_POST["quota_size"] <= 0){
197             $message[]= _("Numerical value for Quota Setting is not valid.");
198           }
199         }
200       }
201     }
202     return $message;
203   }
205   /*! \brief Save POST data to object 
206    */
207   public function save_object()
208   {
209     /* Do we need to flip is_account state? */
210     if (isset($_POST['connectivityTab'])){
211       if (isset($_POST['proxy'])){
212         if (!$this->is_account && $_POST['proxy'] == "B"){
213           if($this->acl_is_createable()){
214             $this->is_account= TRUE;
215           }
216         }
217       } else {
218         if($this->acl_is_removeable()){
219           $this->is_account= FALSE;
220         }
221       }
222     }
224     /* Save flag value */
225     if ($this->is_account || $this->multiple_support_active){
227       $flags= "";
228       $acl= "";
229       foreach(array("F", "T", "B") as $key){
230         if($this->acl_is_writeable("gosaProxyFlag".$key)){
231   
232           /* Add acl */
233           if (isset($_POST["filter$key"])){
234             $flags.= $key;
235           }
236         }else{
237           
238           /* Keep all flags that can't be written*/
239           if(preg_match("/".$key."/",$this->gosaProxyAcctFlags)){
240             $flags .=$key;
241           }
242         }
243       }
244       if ("[$flags]" != $this->gosaProxyAcctFlags){
245         $this->is_modified= TRUE;
246       }
247       $this->gosaProxyAcctFlags= "[$flags]";
249       /* Save time values */
250       if ($this->acl_is_writeable("gosaProxyFlagT")){
251         if(isset($_POST['startMinute'])){
252           $old= $this->gosaProxyWorkingStart;
253           $this->gosaProxyWorkingStart= $_POST["startHour"] * 60 + $_POST["startMinute"];
254           $this->is_modified= ($old != $this->gosaProxyWorkingStart)?TRUE:$this->is_modified;
255         }
257         if (isset($_POST['stopMinute'])){
258           $old= $this->gosaProxyWorkingStop;
259           $this->gosaProxyWorkingStop = $_POST["stopHour"]  * 60 + $_POST["stopMinute"];
260           $this->is_modified= ($old != $this->gosaProxyWorkingStop)?TRUE:$this->is_modified;
261         }
262       }
264       /* Save quota values */
265       if ($this->acl_is_writeable("gosaProxyFlagB")){
266         if(isset($_POST["quota_size"]) && isset($_POST["quota_unit"])){
267           $this->gosaProxyQuota= $_POST["quota_size"].$_POST["quota_unit"];
268         }
269       }
270   
271       /*Save quota period */
272       if($this->acl_is_writeable("gosaProxyFlagB")){
273         if(isset($_POST["gosaProxyQuotaPeriod"])){
274           $this->gosaProxyQuotaPeriod = $_POST["gosaProxyQuotaPeriod"];
275         }
276       }
277     }
278   }
281   /*! \brief Save settings to ldap 
282    */
283   public function save()
284   {
285     plugin::save();
287     /* Write back to ldap */
288     $ldap= $this->config->get_ldap_link();
289     $ldap->cd($this->dn);
290     $this->cleanup();
291     $ldap->modify ($this->attrs); 
293     /* Log last action */
294     if($this->initially_was_account){
295       new log("modify","users/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
296     }else{
297       new log("create","users/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
298     }
300     show_ldap_error($ldap->get_error(), sprintf(_("Saving of user/proxy account with dn '%s' failed."),$this->dn));
302     /* Optionally execute a command after we're done */
303     if ($this->initially_was_account == $this->is_account){
304       if ($this->is_modified){
305         $this->handle_post_events("modify",array("uid" => $this->uid));
306       }
307     } else {
308       $this->handle_post_events("add",array("uid" => $this->uid));
309     }
311   }
313     
314   /*! \brief  Static  Function returning an ACL information array.
315       @return Array   Returns an ACL array 
316    */
317   static function plInfo()
318   {
319     return (array(
320           "plShortName"     => _("Proxy"),
321           "plDescription"   => _("Proxy account")."&nbsp;:&nbsp;<u>"._("Connectivity addon")."</u>",
322           "plSelfModify"    => TRUE,
323           "plDepends"       => array("user"),
324           "plPriority"      => 21,                                 // Position in tabs
325           "plSection"     => array("personal" => _("My account")),
326           "plCategory"    => array("users"),
327           "plOptions"       => array(),
329           "plProvidedAcls"  => array(
330             "gosaProxyFlagF"       => _("Filter unwanted content"), 
331             "gosaProxyFlagT"       => _("Limit proxy access"),
332             "gosaProxyFlagB"       => _("Restrict proxy usage by quota"))
333           ));
334   }
337   /*! \brief  Save html POSTs in multiple edit.
338    */
339   public function multiple_save_object()
340   {
341     if (isset($_POST['connectivityTab'])){
342       plugin::multiple_save_object();
343       if(isset($_POST['use_proxy'])){
344         $this->multi_boxes[] = "proxy";
345       }
346       foreach(array("T","B","F") as $attr){
347         if(isset($_POST["use_filter".$attr])){
348           $this->multi_boxes[] = "filter".$attr;
349         }
350       }
351       $this->save_object();
352     }
353   }
356   /*! \brief  Returns all modified values. \
357               All selected an modified values will be returned \
358               in an array.
359       @return array   Returns an array containing all attribute modifications  
360    */
361   public function get_multi_edit_values()
362   {
363     $ret = plugin::get_multi_edit_values();
364     if(in_array("proxy",$this->multi_boxes)){
365       $ret['is_account'] = $this->is_account;
366     }
368     if(in_array("filterT",$this->multi_boxes)){
369       $ret['gosaProxyWorkingStart']  = $this->gosaProxyWorkingStart;
370       $ret['gosaProxyWorkingStop']   = $this->gosaProxyWorkingStop;
371     }
372     if(in_array("filterB",$this->multi_boxes)){
373       $ret['gosaProxyQuota']        = $this->gosaProxyQuota;
374       $ret['gosaProxyQuotaPeriod']  = $this->gosaProxyQuotaPeriod;
375     }
377     foreach(array("B","T","F") as $attr){
378       $name = "filter".$attr;
379       if(in_array($name,$this->multi_boxes)){
380         $ret[$name] = preg_match("/".$attr."/",$this->gosaProxyAcctFlags);
381       }
382     }
384     return($ret);
385   }
388   /*! \brief  Sets modified attributes in mutliple edit. \
389               All collected values from "get_multi_edit_values()" \
390               will be applied to this plugin.
391       @param  array   An array containing modified attributes returned by get_multi_edit_values();
392    */
393   public function set_multi_edit_values($values)
394   {
395     plugin::set_multi_edit_values($values);
396     if(isset($values['is_account'])){
397       $this->is_account = $values['is_account'];
398     }
399     foreach(array("B","T","F") as $attr){
400       $name = "filter".$attr;
401       if(isset($values[$name])){
402         if($values[$name] && !preg_match("/".$attr."/",$this->gosaProxyAcctFlags)){
403           $this->gosaProxyAcctFlags = preg_replace("/\]/",$attr."]",$this->gosaProxyAcctFlags);
404         }elseif(!$values[$name] && preg_match("/".$attr."/",$this->gosaProxyAcctFlags)){
405           $this->gosaProxyAcctFlags = preg_replace("/".$attr."/","",$this->gosaProxyAcctFlags);
406         }
407       }
408     }
409   }
412   /*! \brief  Initialize multiple edit ui for this plugin. \
413               This function sets plugin defaults in multiple edit.
414       @param  array   Attributes used in all object 
415       @param  array   All used attributes.
416    */
417   public function init_multiple_support($attrs,$all)
418   {
419     plugin::init_multiple_support($attrs,$all);
420     if(isset($attrs['objectClass']) && in_array("gosaProxyAccount",$attrs['objectClass'])){
421       $this->is_account = TRUE;
422     }
423   }
426 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
427 ?>