Code

Fixed mail method kolab account removement.
[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","kolabDelegate","kolabInvitationPolicy");
22   var $objectclasses  = array("kolabInetOrgPerson");
24   /* Helper */
25   var $imapping= array();
27   var $uid ="";
29   function kolabAccount ($config, $dn= NULL, $parent= NULL)
30   {
31     plugin::plugin ($config, $dn, $parent);
32  
33     /* Setting uid to default */
34     if(isset($this->attrs['uid'][0])){
35       $this->uid = $this->attrs['uid'][0];
36     }
37  
38     /* Pull arrays */
39     foreach(array("kolabDelegate", "kolabInvitationPolicy") as $attr){
40       if (isset($this->attrs["$attr"]["count"])){
41         $tmp = array();
42         for ($i= 0; $i<$this->attrs["$attr"]["count"]; $i++){
43           $tmp[]=$this->attrs["$attr"][$i];
44         }
45         $this->$attr = $tmp;
46       }
47     }
49     /* If this one is empty, preset with ACT_MANUAL for anonymous users */
50     if (count ($this->kolabInvitationPolicy) == 0){
51        $this->kolabInvitationPolicy= array("ACT_MANUAL");
52     }
54     /* Check is account state */
55     $this->is_account = false;
56     if(count($this->kolabDelegate)){
57       $this->is_account = true;
58     }
59     foreach(array("calFBURL","unrestrictedMailSize") as $attr){
60       if(!empty($this->$attr)){
61         $this->is_account = true;
62       }
63     } 
64   }
67   function execute()
68   {
69         /* Call parent execute */
70         plugin::execute();
72     /* Show tab dialog headers */
73     $display= "";
75     /* Show main page */
76     $smarty= get_smarty();
78     /* Load attributes */
79     foreach($this->attributes as $val){
80       $smarty->assign("$val", $this->$val);
81       $smarty->assign($val."ACL", chkacl($this->acl, "$val"));
82     }
83     $smarty->assign("kolabAccountACL", chkacl($this->acl, "kolabAccountACL"));
85     /* Check for invitation action */
86     $nr= 0;
87     while (isset($_POST["policy$nr"])){
88       if (isset($_POST["add$nr"])){
89         $this->kolabInvitationPolicy[]= ": ACT_MANUAL";
90       }
91       if (isset($_POST["remove$nr"])){
92         $new= array();
93         foreach ($this->kolabInvitationPolicy as $entry){
94           if (!preg_match("/^".$this->imapping[$nr].":/", $entry)){
95             $new[]= $entry;
96           }
97         }
98         $this->kolabInvitationPolicy= $new;
99       }
100       $nr++;
101     }
103     /* Unify addresses */
104     $new= array();
105     foreach($this->kolabInvitationPolicy as $value){
106       $address= preg_replace('/^([^:]+:).*$/', '\1', $value);
107       $new[$address]= $value;
108     }
109     $this->kolabInvitationPolicy= array();
110     foreach($new as $value){
111       $this->kolabInvitationPolicy[]= $value;
112     }
114     /* Add delegation */
115     if (isset($_POST['add_delegation'])){
116       if ($_POST['delegate_address'] != ""){
118         /* Valid email address specified? */
119         $address= $_POST['delegate_address'];
120         $valid= FALSE;
121         if (!is_email($address)){
122           if (!is_email($address, TRUE)){
123               print_red (_("You're trying to add an invalid email address to the list of delegations."));
124           }
125         } else {
127           $ldap= $this->config->get_ldap_link();
128           $ldap->cd ($this->config->current['BASE']);
129           $ldap->search('(mail='.$address.')',array("mail"));
130           if ($ldap->count() == 0){
131             print_red (_("The mail address you're trying to add is no primary mail address of an existing user."));
132           } else {
133             $valid= TRUE;
134           }
135         }
137         if ($valid){
138           /* Add it */
139           if (chkacl ($this->acl, "kolabDelegate") == ""){
140             $this->addDelegate ($address);
141             $this->is_modified= TRUE;
142           }
144         }
145       }
146     }
148     /* Delete forward email addresses */
149     if ((isset($_POST['delete_delegation'])) && (isset($_POST['delegate_list']))){
150       if (count($_POST['delegate_list'])
151           && chkacl ($this->acl, "kolabDelegate") == ""){
153         $this->delDelegate ($_POST['delegate_list']);
154       }
155     }
157     /* Assemble policies */
158     $policies= array( 'ACT_ALWAYS_ACCEPT'       => _("Always accept"),
159                       'ACT_ALWAYS_REJECT'       => _("Always reject"),
160                       'ACT_REJECT_IF_CONFLICTS' => _("Reject if conflicts"),
161                       'ACT_MANUAL_IF_CONFLICTS' => _("Manual if conflicts"),
162                       'ACT_MANUAL'              => _("Manual"));
163     $smarty->assign('policies', $policies);
165     /* Adjust checkbox */
166     if ($this->unrestrictedMailSize){
167       $smarty->assign('unrestrictedMailSizeState', "checked");
168     } else {
169       $smarty->assign('unrestrictedMailSizeState', "");
170     }
172     /* Transfer account states for this union */
173     if (isset($this->parent) && $this->parent->by_object['mailAccount']->is_account){
174       $smarty->assign('mail_account', 'true');
175     }elseif( isset($this->attrs) && isset($this->attrs['kolabHomeServer'])){
176       $smarty->assign('mail_account', 'true');
177     }else{
178       $this->is_account= false;
179       $smarty->assign('mail_account', '');
180     }
182     /* Transfer delegation list */
183     if (!count($this->kolabDelegate)){
184       /* Smarty will produce <option value=""></option> and tidy don't like that, so tell smarty to create no option (array();)*/
185       $smarty->assign("kolabDelegate", array());
186     } else {
187       $smarty->assign("kolabDelegate", $this->kolabDelegate);
188     }
189     $smarty->assign("kolabDelegateACL", chkacl($this->acl, $this->kolabDelegate));
191     /* Create InvitationPolicy table */
192     $invitation= "";
193     $this->imapping= array();
194     $nr= 0;
195     $acl= chkacl($this->acl, "kolabInvitationPolicy");
196     $changeState = "";
197     foreach ($this->kolabInvitationPolicy as $entry){
199       $changeState .= "changeState('address".$nr."'); \n changeState('policy".$nr."'); \n
200                          changeState('add".$nr."'); \n changeState('remove".$nr."'); \n";
202       $invitation.= "<tr><td>";
203       if(($this->is_account) && !preg_match("/disabled/",$acl)){
204         $dis = " ";
205       }else{
206         $dis = " disabled ";
207       }
208     
210       /* The default entry does not have colons... */
211       if (!preg_match('/:/', $entry)){
212         $invitation.= _("Anonymous");
213         $name= "";
214         $mode= $entry;
215       } else {
216         $name= preg_replace('/:.*$/', '', $entry);
217         $mode= preg_replace('/^[^:]*: */', '', $entry);
218         $invitation.= "<input name=\"address$nr\" size=16 maxlength=60 value=\"$name\" id='address".$nr."' ".$dis.">";
219       }
220       $invitation.= "</td>";
222       /* Add mode switch */
223       $invitation.= "<td><select size=\"1\" name=\"policy$nr\" id='policy".$nr."' ".$dis.">";
224       foreach($policies as $key => $value){
225         if ($key == $mode){
226           $invitation.= "<option value=\"$key\" selected>$value</option>";
227         } else {
228           $invitation.= "<option value=\"$key\">$value</option>";
229         }
230       }
231       
232       /* Assign buttons */
233       $button= "";
234       if ($nr == count($this->kolabInvitationPolicy)-1){
235         $button= "<input type=submit name=\"add$nr\" value=\""._("Add")."\" id='add".$nr."' ".$dis.">";
236       }
237       if ($nr != 0) {
238         $button.= "<input type=submit name=\"remove$nr\" value=\""._("Remove")."\" id='remove".$nr."' ".$dis.">";
239       }
240       
241       $invitation.= "</select>&nbsp;$button</td></tr>\n";
242       $this->imapping[$nr]= $name;
243       $nr++;
244     }
245     $smarty->assign("invitation", $invitation);
246     $smarty->assign("changeState", $changeState);
247     $smarty->assign("kolabState",$this->is_account);
248     $display.= $smarty->fetch (get_template_path('kolab.tpl', TRUE, dirname(__FILE__)));
251     return ($display);
252   }
254   function remove_from_parent()
255   {
256     /* Optionally execute a command after we're done */
257     plugin::remove_from_parent();
258     
259     /* This plugin and the mailAccount plugin are using the same objectClass 
260         check here if the mail account still activated. 
261         If mailAccount is activated we must re-add the the objectClass.
262     */
263     if(!in_array("kolabInetOrgPerson",$this->attrs['objectClass'])){
264       $this->attrs['objectClass'][] = "kolabInetOrgPerson";
265     }
267     $ldap = $this->config->get_ldap_linK(); 
268     $ldap->cd($this->config->current['BASE']);
269     $ldap->cd ($this->dn);
270     $ldap->modify($this->attrs);
272     $this->handle_post_events('remove',array("uid" => $this->uid));
273     show_ldap_error($ldap->get_error(), sprintf(_("Removing of user/kolab account with dn '%s' failed."),$this->dn));
274   }
277   function check()
278   {
279     /* Call common method to give check the hook */
280     $message= plugin::check();
282     /* FBFuture is in days... */
283     if (!empty($this->kolabFreeBusyFuture) && !preg_match('/^[0-9]+$/', $this->kolabFreeBusyFuture)){
284       $message[]= _("The value specified as Free Busy future needs to be an integer.");
285     }
287 #    /* Check for URL scheme... */
288 #    if ($this->calFBURL != "" && !@preg_match('/^[^:/]+://[a-z0-9_/.+~-]+$/i', $this->calFBURL)){
289 #      $message[]= _("The value specified as Free Busy Information URL is invalid.");
290 #    }
292     /* Check invitation policy for existing mail addresses */
293     foreach($this->kolabInvitationPolicy as $policy){
294       
295       /* Ignore anonymous string */
296       if (!preg_match('/:/', $policy)){
297         continue;
298       }
299       
300       $address= preg_replace('/^([^:]+).*$/', '\1', $policy);
301       if (!is_email($address)){
302         if (!is_email($address, TRUE)){
303           $message[]= sprintf(_("The invitation policy entry for address '%s' is not valid."), $address);
304         }
305       } else {
307         $ldap= $this->config->get_ldap_link();
308         $ldap->cd ($this->config->current['BASE']);
309         $ldap->search('(mail='.$address.')',array("mail"));
310         if ($ldap->count() == 0){
311           $message[]= sprintf(_("There's no mail user with address '%s' for your invitation policy!"), $address);
312         } else {
313           $valid= TRUE;
314         }
315       }
316     }
318     return ($message);
319   }
321   /* Save data to object */
322   function save_object()
323   {
324     /* Do we need to flip is_account state? */
325     if (isset($_POST['connectivityTab'])){
327       if(isset($_POST["kolabState"])){
328         $this->is_account = true;
329       }else{
330         $this->is_account = false;
331       }
333       if (chkacl('unrestrictedMailSize', $this->acl == "")){
334         if (isset($_POST['unrestrictedMailSize']) && $_POST['unrestrictedMailSize'] == 1){
335           $this->unrestrictedMailSize= 1;
336         } else {
337           $this->unrestrictedMailSize= 0;
338         }
339       }
340     }
342     plugin::save_object();
344     /* Save changes done in invitation policies */
345     $nr= 0;
346     $this->kolabInvitationPolicy= array();
347     while (isset($_POST["policy$nr"])){
349       /* Anonymous? */
350       if (!isset($_POST["address$nr"])){
351         $this->kolabInvitationPolicy[]= $_POST["policy$nr"];
352       } else {
353         $this->kolabInvitationPolicy[]= $_POST["address$nr"].": ".$_POST["policy$nr"];
354       }
355       
356       $nr++;
357     }
358     
359     /* If this one is empty, preset with ACT_MANUAL for anonymous users */
360     if (count ($this->kolabInvitationPolicy) == 0){
361       $this->kolabInvitationPolicy= array("ACT_MANUAL");
362     }
364   }
367   /* Save to LDAP */
368   function save()
369   {
370     /* Check mailmethod before doing something useful */
371     plugin::save();
373     /* Transfer arrays */
374     $this->attrs['kolabDelegate']= $this->kolabDelegate;
375     $this->attrs['kolabInvitationPolicy']= $this->kolabInvitationPolicy;
377     /* unrestrictedMailSize is boolean */
378     if($this->attrs['unrestrictedMailSize']){
379       $this->attrs['unrestrictedMailSize'] = "TRUE";
380     }else{
381       $this->attrs['unrestrictedMailSize'] = "FALSE";
382     }
383   
384     /* Write back to ldap */
385     $ldap= $this->config->get_ldap_link();
386     $ldap->cd($this->dn);
387     $this->cleanup();
388     $ldap->modify ($this->attrs); 
390     show_ldap_error($ldap->get_error(), sprintf(_("Saving of user/kolab account with dn '%s' failed."),$this->dn));
392     /* Optionally execute a command after we're done */
393     if ($this->initially_was_account == $this->is_account){
394       if ($this->is_modified){
395         $this->handle_post_events("modify",array("uid" => $this->uid));
396       }
397     } else {
398       $this->handle_post_events("add",array("uid" => $this->uid));
399     }
400   }
403   /* Add entry to delegation list */
404   function addDelegate($address)
405   {
406     $this->kolabDelegate[]= $address;
407     $this->kolabDelegate= array_unique ($this->kolabDelegate);
409     sort ($this->kolabDelegate);
410     reset ($this->kolabDelegate);
411     $this->is_modified= TRUE;
412   }
414   function delDelegate($addresses)
415   {
416     $this->kolabDelegate= array_remove_entries ($addresses, $this->kolabDelegate);
417     $this->is_modified= TRUE;
418   }
422 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
423 ?>