Code

tidy
[gosa.git] / plugins / personal / connectivity / class_kolabAccount.inc
1 <?php
2 class kolabAccount extends plugin
3 {
4   /* Definitions */
5   var $plHeadline= "Kolab";
6   var $plDescription= "This does something";
8   /* CLI vars */
9   var $cli_summary= "Manage users Kolab account";
10   var $cli_description= "Some longer text\nfor help";
11   var $cli_parameters= array("eins" => "Eins ist toll", "zwei" => "Zwei ist noch besser");
13   /* Kolab attributes */
14   var $kolabInvitationPolicy= array();
15   var $kolabFreeBusyFuture= 60;
16   var $unrestrictedMailSize= 0;
17   var $calFBURL= "";
18   var $kolabDelegate= array();
20   /* attribute list for save action */
21   var $attributes= array( "kolabFreeBusyFuture", "unrestrictedMailSize", "calFBURL");
22   var $objectclasses= array();
24   /* Helper */
25   var $imapping= array();
27   function kolabAccount ($config, $dn= NULL)
28   {
29     plugin::plugin ($config, $dn);
31     /* Pull arrays */
32     foreach(array("kolabDelegate", "kolabInvitationPolicy") as $attr){
33       if (isset($this->attrs["$attr"]["count"])){
34         for ($i= 0; $i<$this->attrs["$attr"]["count"]; $i++){
35           array_push($this->$attr, $this->attrs["$attr"][$i]);
36         }
37       }
38     }
40     /* If this one is empty, preset with ACT_MANUAL for anonymous users */
41     if (count ($this->kolabInvitationPolicy) == 0){
42        $this->kolabInvitationPolicy= array("ACT_MANUAL");
43     }
45   }
47   function execute()
48   {
49     /* Show tab dialog headers */
50     $display= "";
52     /* Show main page */
53     $smarty= get_smarty();
55     /* Load attributes */
56     foreach($this->attributes as $val){
57       $smarty->assign("$val", $this->$val);
58       $smarty->assign($val."ACL", chkacl($this->acl, "$val"));
59     }
61     /* Check for invitation action */
62     $nr= 0;
63     while (isset($_POST["policy$nr"])){
64       if (isset($_POST["add$nr"])){
65         $this->kolabInvitationPolicy[]= ": ACT_MANUAL";
66       }
67       if (isset($_POST["remove$nr"])){
68         $new= array();
69         foreach ($this->kolabInvitationPolicy as $entry){
70           if (!preg_match("/^".$this->imapping[$nr].":/", $entry)){
71             $new[]= $entry;
72           }
73         }
74         $this->kolabInvitationPolicy= $new;
75       }
76       $nr++;
77     }
79     /* Unify addresses */
80     $new= array();
81     foreach($this->kolabInvitationPolicy as $value){
82       if (preg_match('/^:/', $value)){
83         continue;
84       }
85       $address= preg_replace('/^([^:]+:).*$/', '\1', $value);
86       $new[$address]= $value;
87     }
88     $this->kolabInvitationPolicy= array();
89     foreach($new as $value){
90       $this->kolabInvitationPolicy[]= $value;
91     }
93     /* Add delegation */
94     if (isset($_POST['add_delegation'])){
95       if ($_POST['delegate_address'] != ""){
97         /* Valid email address specified? */
98         $address= $_POST['delegate_address'];
99         $valid= FALSE;
100         if (!is_email($address)){
101           if (!is_email($address, TRUE)){
102               print_red (_("You're trying to add an invalid email address to the list of delegations."));
103           }
104         } else {
106           $ldap= $this->config->get_ldap_link();
107           $ldap->cd ($this->config->current['BASE']);
108           $ldap->search('(mail='.$address.')');
109           if ($ldap->count() == 0){
110             print_red (_("The mail address you're trying to add is no primary mail address of an existing user."));
111           } else {
112             $valid= TRUE;
113           }
114         }
116         if ($valid){
117           /* Add it */
118           if (chkacl ($this->acl, "kolabDelegate") == ""){
119             $this->addDelegate ($address);
120             $this->is_modified= TRUE;
121           }
123         }
124       }
125     }
127     /* Delete forward email addresses */
128     if (isset($_POST['delete_delegation'])){
129       if (count($_POST['delegate_list'])
130           && chkacl ($this->acl, "kolabDelegate") == ""){
132         $this->delDelegate ($_POST['delegate_list']);
133       }
134     }
136     /* Assemble policies */
137     $policies= array( 'ACT_ALWAYS_ACCEPT'       => _("Always accept"),
138                       'ACT_ALWAYS_REJECT'       => _("Always reject"),
139                       'ACT_REJECT_IF_CONFLICTS' => _("Reject if conflicts"),
140                       'ACT_MANUAL_IF_CONFLICTS' => _("Manual if conflicts"),
141                       'ACT_MANUAL'              => _("Manual"));
142     $smarty->assign('policies', $policies);
144     /* Adjust checkbox */
145     if ($this->unrestrictedMailSize){
146       $smarty->assign('unrestrictedMailSizeState', "checked");
147     } else {
148       $smarty->assign('unrestrictedMailSizeState', "");
149     }
151     /* Transfer account states for this union */
152     if (isset($this->parent) && $this->parent->by_object['mailAccount']->is_account){
153       $smarty->assign('is_account', 'true');
154     } else {
155       $smarty->assign('is_account', '');
156     }
158     /* Transfer delegation list */
159     if (!count($this->kolabDelegate)){
160       /* Smarty will produce <option value=""></option> and tidy don't like that, so tell smarty to create no option (array();)*/
161       $smarty->assign("kolabDelegate", array());
162     } else {
163       $smarty->assign("kolabDelegate", $this->kolabDelegate);
164     }
165     $smarty->assign("kolabDelegateACL", chkacl($this->acl, $this->kolabDelegate));
167     /* Create InvitationPolicy table */
168     $invitation= "";
169     $this->imapping= array();
170     $nr= 0;
171     $acl= chkacl($this->acl, "kolabInvitationPolicy");
172     foreach ($this->kolabInvitationPolicy as $entry){
173       $invitation.= "<tr><td>";
175       /* The default entry does not have colons... */
176       if (!preg_match('/:/', $entry)){
177         $invitation.= _("Anonymous");
178         $name= "";
179         $mode= $entry;
180       } else {
181         $name= preg_replace('/:.*$/', '', $entry);
182         $mode= preg_replace('/^[^:]*: */', '', $entry);
183         $invitation.= "<input name=\"address$nr\" size=16 maxlength=60 $acl value=\"$name\">";
184       }
185       $invitation.= "</td>";
187       /* Add mode switch */
188       $invitation.= "<td><select size=\"1\" name=\"policy$nr\" $acl>";
189       foreach($policies as $key => $value){
190         if ($key == $mode){
191           $invitation.= "<option value=\"$key\" selected>$value</option>";
192         } else {
193           $invitation.= "<option value=\"$key\">$value</option>";
194         }
195       }
196       
197       /* Assign buttons */
198       $button= "";
199       if ($nr == count($this->kolabInvitationPolicy)-1){
200         $button= "<input type=submit name=\"add$nr\" value=\""._("Add")."\">";
201       }
202       if ($nr != 0) {
203         $button.= "<input type=submit name=\"remove$nr\" value=\""._("Remove")."\">";
204       }
205       
206       $invitation.= "</select>&nbsp;$button</td></tr>\n";
207       $this->imapping[$nr]= $name;
208       $nr++;
209     }
210     $smarty->assign("invitation", $invitation);
212     $display.= $smarty->fetch (get_template_path('kolab.tpl', TRUE, dirname(__FILE__)));
213     return ($display);
214   }
216   function remove_from_parent()
217   {
218     /* Optionally execute a command after we're done */
219     $this->handle_post_events('remove');
220   }
223   function check()
224   {
225     $message= array();
227     /* FBFuture is in days... */
228     if ($this->kolabFreeBusyFuture != "" && !preg_match('/^[0-9]+$/', $this->kolabFreeBusyFuture)){
229       $message[]= _("The value specified as Free Busy future needs to be an integer.");
230     }
232     /* Check for URL scheme... */
233     if ($this->calFBURL != "" && !preg_match('/^[^:/]+://[a-z0-9_/.+~-]+$/i', $this->calFBURL)){
234       $message[]= _("The value specified as Free Busy Information URL is invalid.");
235     }
237     /* Check invitation policy for existing mail addresses */
238     foreach($this->kolabInvitationPolicy as $policy){
239       
240       /* Ignore anonymous string */
241       if (!preg_match('/:/', $policy)){
242         continue;
243       }
244       
245       $address= preg_replace('/^([^:]+).*$/', '\1', $policy);
246       if (!is_email($address)){
247         if (!is_email($address, TRUE)){
248           $message[]= sprintf(_("The invitation policy entry for address '%s' is not valid."), $address);
249         }
250       } else {
252         $ldap= $this->config->get_ldap_link();
253         $ldap->cd ($this->config->current['BASE']);
254         $ldap->search('(mail='.$address.')');
255         if ($ldap->count() == 0){
256           $message[]= sprintf(_("There's no mail user with address '%s' for your invitation policy!"), $address);
257         } else {
258           $valid= TRUE;
259         }
260       }
261     }
263     return ($message);
264   }
266   /* Save data to object */
267   function save_object()
268   {
269     /* Do we need to flip is_account state? */
270     if (isset($_POST['connectivityTab'])){
271       if (chkacl('unrestrictedMailSize', $this->acl == "")){
272         if (isset($_POST['unrestrictedMailSize']) && $_POST['unrestrictedMailSize'] == 1){
273           $this->unrestrictedMailSize= 1;
274         } else {
275           $this->unrestrictedMailSize= 0;
276         }
277       }
278     }
280     plugin::save_object();
282     /* Save changes done in invitation policies */
283     $nr= 0;
284     $this->kolabInvitationPolicy= array();
285     while (isset($_POST["policy$nr"])){
287       /* Anonymous? */
288       if (!isset($_POST["address$nr"])){
289         $this->kolabInvitationPolicy[]= $_POST["policy$nr"];
290       } else {
291         $this->kolabInvitationPolicy[]= $_POST["address$nr"].": ".$_POST["policy$nr"];
292       }
293       
294       $nr++;
295     }
296     
297     /* If this one is empty, preset with ACT_MANUAL for anonymous users */
298     if (count ($this->kolabInvitationPolicy) == 0){
299       $this->kolabInvitationPolicy= array("ACT_MANUAL");
300     }
302   }
305   /* Save to LDAP */
306   function save()
307   {
308     plugin::save();
310     /* Transfer arrays */
311     $this->attrs['kolabDelegate']= $this->kolabDelegate;
312     $this->attrs['kolabInvitationPolicy']= $this->kolabInvitationPolicy;
314     /* Write back to ldap */
315     $ldap= $this->config->get_ldap_link();
316     $ldap->cd($this->dn);
317     $ldap->modify($this->attrs);
318     show_ldap_error($ldap->get_error());
320     /* Optionally execute a command after we're done */
321     if ($this->initially_was_account == $this->is_account){
322       if ($this->is_modified){
323         $this->handle_post_events("mofify");
324       }
325     } else {
326       $this->handle_post_events("add");
327     }
328   }
331   /* Add entry to delegation list */
332   function addDelegate($address)
333   {
334     $this->kolabDelegate[]= $address;
335     $this->kolabDelegate= array_unique ($this->kolabDelegate);
337     sort ($this->kolabDelegate);
338     reset ($this->kolabDelegate);
339     $this->is_modified= TRUE;
340   }
342   function delDelegate($addresses)
343   {
344     $this->kolabDelegate= array_remove_entries ($addresses, $this->kolabDelegate);
345     $this->is_modified= TRUE;
346   }
350 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
351 ?>