Code

Fixed smaller "cosmetic" errors
[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 ($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     $smarty->assign("kolabDelegate", $this->kolabDelegate);
160     $smarty->assign("kolabDelegateACL", chkacl($this->acl, $this->kolabDelegate));
162     /* Create InvitationPolicy table */
163     $invitation= "";
164     $this->imapping= array();
165     $nr= 0;
166     $acl= chkacl($this->acl, "kolabInvitationPolicy");
167     foreach ($this->kolabInvitationPolicy as $entry){
168       $invitation.= "<tr><td>";
170       /* The default entry does not have colons... */
171       if (!preg_match('/:/', $entry)){
172         $invitation.= _("Anonymous");
173         $name= "";
174         $mode= $entry;
175       } else {
176         $name= preg_replace('/:.*$/', '', $entry);
177         $mode= preg_replace('/^[^:]*: */', '', $entry);
178         $invitation.= "<input name=\"address$nr\" size=16 maxlength=60 $acl value=\"$name\">";
179       }
180       $invitation.= "</td>";
182       /* Add mode switch */
183       $invitation.= "<td><select size=\"1\" name=\"policy$nr\" $acl>";
184       foreach($policies as $key => $value){
185         if ($key == $mode){
186           $invitation.= "<option value=\"$key\" selected>$value</option>";
187         } else {
188           $invitation.= "<option value=\"$key\">$value</option>";
189         }
190       }
191       
192       /* Assign buttons */
193       $button= "";
194       if ($nr == count($this->kolabInvitationPolicy)-1){
195         $button= "<input type=submit name=\"add$nr\" value=\""._("Add")."\">";
196       }
197       if ($nr != 0) {
198         $button.= "<input type=submit name=\"remove$nr\" value=\""._("Remove")."\">";
199       }
200       
201       $invitation.= "</select>&nbsp;$button</td></tr>\n";
202       $this->imapping[$nr]= $name;
203       $nr++;
204     }
205     $smarty->assign("invitation", $invitation);
207     $display.= $smarty->fetch (get_template_path('kolab.tpl', TRUE, dirname(__FILE__)));
208     return ($display);
209   }
211   function remove_from_parent()
212   {
213     /* Optionally execute a command after we're done */
214     $this->handle_post_events('remove');
215   }
218   function check()
219   {
220     $message= array();
222     /* FBFuture is in days... */
223     if ($this->kolabFreeBusyFuture != "" && !preg_match('/^[0-9]+$/', $this->kolabFreeBusyFuture)){
224       $message[]= _("The value specified as Free Busy future needs to be an integer.");
225     }
227     /* Check for URL scheme... */
228     if ($this->calFBURL != "" && !preg_match('/^[^:/]+://[a-z0-9_/.+~-]+$/i', $this->calFBURL)){
229       $message[]= _("The value specified as Free Busy Information URL is invalid.");
230     }
232     /* Check invitation policy for existing mail addresses */
233     foreach($this->kolabInvitationPolicy as $policy){
234       
235       /* Ignore anonymous string */
236       if (!preg_match('/:/', $policy)){
237         continue;
238       }
239       
240       $address= preg_replace('/^([^:]+).*$/', '\1', $policy);
241       if (!is_email($address)){
242         if (!is_email($address, TRUE)){
243           $message[]= sprintf(_("The invitation policy entry for address '%s' is not valid."), $address);
244         }
245       } else {
247         $ldap= $this->config->get_ldap_link();
248         $ldap->cd ($this->config->current['BASE']);
249         $ldap->search('(mail='.$address.')');
250         if ($ldap->count() == 0){
251           $message[]= sprintf(_("There's no mail user with address '%s' for your invitation policy!"), $address);
252         } else {
253           $valid= TRUE;
254         }
255       }
256     }
258     return ($message);
259   }
261   /* Save data to object */
262   function save_object()
263   {
264     /* Do we need to flip is_account state? */
265     if (isset($_POST['connectivityTab'])){
266       if (chkacl('unrestrictedMailSize', $this->acl == "")){
267         if (isset($_POST['unrestrictedMailSize']) && $_POST['unrestrictedMailSize'] == 1){
268           $this->unrestrictedMailSize= 1;
269         } else {
270           $this->unrestrictedMailSize= 0;
271         }
272       }
273     }
275     plugin::save_object();
277     /* Save changes done in invitation policies */
278     $nr= 0;
279     $this->kolabInvitationPolicy= array();
280     while (isset($_POST["policy$nr"])){
282       /* Anonymous? */
283       if (!isset($_POST["address$nr"])){
284         $this->kolabInvitationPolicy[]= $_POST["policy$nr"];
285       } else {
286         $this->kolabInvitationPolicy[]= $_POST["address$nr"].": ".$_POST["policy$nr"];
287       }
288       
289       $nr++;
290     }
291     
292   }
295   /* Save to LDAP */
296   function save()
297   {
298     plugin::save();
300     /* Transfer arrays */
301     $this->attrs['kolabDelegate']= $this->kolabDelegate;
302     $this->attrs['kolabInvitationPolicy']= $this->kolabInvitationPolicy;
304     /* Write back to ldap */
305     $ldap= $this->config->get_ldap_link();
306     $ldap->cd($this->dn);
307     $ldap->modify($this->attrs);
308     show_ldap_error($ldap->get_error());
310     /* Optionally execute a command after we're done */
311     if ($this->initially_was_account == $this->is_account){
312       if ($this->is_modified){
313         $this->handle_post_events("mofify");
314       }
315     } else {
316       $this->handle_post_events("add");
317     }
318   }
321   /* Add entry to delegation list */
322   function addDelegate($address)
323   {
324     $this->kolabDelegate[]= $address;
325     $this->kolabDelegate= array_unique ($this->kolabDelegate);
327     sort ($this->kolabDelegate);
328     reset ($this->kolabDelegate);
329     $this->is_modified= TRUE;
330   }
332   function delDelegate($addresses)
333   {
334     $this->kolabDelegate= array_remove_entries ($addresses, $this->kolabDelegate);
335     $this->is_modified= TRUE;
336   }
340 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
341 ?>