Code

Fixed undefined index
[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   /* Kolab attributes */
9   var $kolabInvitationPolicy  = array();
10   var $kolabFreeBusyFuture    = 60;
11   var $unrestrictedMailSize   = 0;
12   var $calFBURL               = "";
13   var $kolabDelegate          = array();
15   /* attribute list for save action */
16   var $attributes     = array("kolabFreeBusyFuture", "unrestrictedMailSize", "calFBURL","kolabDelegate");
17   var $objectclasses  = array("kolabInetOrgPerson");
19   /* Helper */
20   var $imapping= array();
21   var $mail_Account   = false;
22   var $ReadOnly = false;
24   function kolabAccount ($config, $dn= NULL)
25   {
26     plugin::plugin ($config, $dn);
28     /* Pull arrays */
29     foreach(array("kolabDelegate", "kolabInvitationPolicy") as $attr){
30       if (isset($this->attrs["$attr"]["count"])){
31         $tmp = array();
32         for ($i= 0; $i<$this->attrs["$attr"]["count"]; $i++){
33           $tmp[]=$this->attrs["$attr"][$i];
34         }
35         $this->$attr = $tmp;
36       }
37     }
39     /* If this one is empty, preset with ACT_MANUAL for anonymous users */
40     if (count ($this->kolabInvitationPolicy) == 0){
41        $this->kolabInvitationPolicy= array("ACT_MANUAL");
42     }
44     /* Check is account state */
45     $this->is_account = false;
46     if(count($this->kolabDelegate)){
47       $this->is_account = true;
48     }
49     foreach(array("calFBURL","unrestrictedMailSize") as $attr){
50       if(!empty($this->$attr)){
51         $this->is_account = true;
52       }
53     } 
55     /* Transfer account states for this union */
56     if (isset($this->parent) && $this->parent->by_object['mailAccount']->is_account){
57       $this->mail_Account = true;
58     } elseif($this-> initially_was_account && (isset($this->attrs['objectClass']) && (in_array("kolabInetOrgPerson",$this->attrs['objectClass'])))){
59       $this->mail_Account = true;
60     }
61   }
64   function execute()
65   {
66         /* Call parent execute */
67         plugin::execute();
69     /* Show tab dialog headers */
70     $display= "";
72     /* Show main page */
73     $smarty= get_smarty();
75     /* Load attributes */
76     foreach($this->attributes as $val){
77       $smarty->assign("$val", $this->$val);
78     }
80     $tmp = $this->plInfo();
81     foreach($tmp['plProvidedAcls'] as $acl => $description){
82       $smarty->assign($acl."ACL",$this->getacl($acl,$this->ReadOnly));
83       $smarty->assign($acl."_W", $this->acl_is_writeable($acl,$this->ReadOnly));
84     }
85     $smarty->assign("is_account" ,  $this->is_account); 
87     if($this->ReadOnly){
88       $smarty->assign("is_removeable",false); 
89       $smarty->assign("is_createable",false); 
90     }else{
91       $smarty->assign("is_removeable",$this->acl_is_removeable()); 
92       $smarty->assign("is_createable",$this->acl_is_createable()); 
93     }
95     /* Check for invitation action */
96     $nr= 0;
97     while (isset($_POST["policy$nr"])){
98       if (isset($_POST["add$nr"])){
99         $this->kolabInvitationPolicy[]= ": ACT_MANUAL";
100       }
101       if (isset($_POST["remove$nr"])){
102         $new= array();
103         foreach ($this->kolabInvitationPolicy as $entry){
104           if (!preg_match("/^".$this->imapping[$nr].":/", $entry)){
105             $new[]= $entry;
106           }
107         }
108         $this->kolabInvitationPolicy= $new;
109       }
110       $nr++;
111     }
113     /* Unify addresses */
114     $new= array();
115     foreach($this->kolabInvitationPolicy as $value){
116       $address= preg_replace('/^([^:]+:).*$/', '\1', $value);
117       $new[$address]= $value;
118     }
119     $this->kolabInvitationPolicy= array();
120     foreach($new as $value){
121       $this->kolabInvitationPolicy[]= $value;
122     }
124     /* Add delegation */
125     if (isset($_POST['add_delegation'])){
126       if ($_POST['delegate_address'] != ""){
128         /* Valid email address specified? */
129         $address= $_POST['delegate_address'];
130         $valid= FALSE;
131         if (!is_email($address)){
132           if (!is_email($address, TRUE)){
133               print_red (_("You're trying to add an invalid email address to the list of delegations."));
134           }
135         } else {
137           $ldap= $this->config->get_ldap_link();
138           $ldap->cd ($this->config->current['BASE']);
139           $ldap->search('(mail='.$address.')',array("mail"));
140           if ($ldap->count() == 0){
141             print_red (_("The mail address you're trying to add is no primary mail address of an existing user."));
142           } else {
143             $valid= TRUE;
144           }
145         }
147         if ($valid){
148           /* Add it */
149           if ($this->acl_is_writeable("kolabDelegate")){
150             $this->addDelegate ($address);
151             $this->is_modified= TRUE;
152           }
154         }
155       }
156     }
158     /* Delete forward email addresses */
159     if ((isset($_POST['delete_delegation'])) && (isset($_POST['delegate_list']))){
160       if (count($_POST['delegate_list']) && $this->acl_is_writeable("kolabDelegate")){
161         $this->delDelegate ($_POST['delegate_list']);
162       }
163     }
165     /* Assemble policies */
166     $policies= array( 'ACT_ALWAYS_ACCEPT'       => _("Always accept"),
167                       'ACT_ALWAYS_REJECT'       => _("Always reject"),
168                       'ACT_REJECT_IF_CONFLICTS' => _("Reject if conflicts"),
169                       'ACT_MANUAL_IF_CONFLICTS' => _("Manual if conflicts"),
170                       'ACT_MANUAL'              => _("Manual"));
171     $smarty->assign('policies', $policies);
173     /* Adjust checkbox */
174     if ($this->unrestrictedMailSize){
175       $smarty->assign('unrestrictedMailSizeState', "checked");
176     } else {
177       $smarty->assign('unrestrictedMailSizeState', "");
178     }
180     /* Transfer delegation list */
181     if (!count($this->kolabDelegate)){
182       /* Smarty will produce <option value=""></option> and tidy don't like that, so tell smarty to create no option (array();)*/
183       $smarty->assign("kolabDelegate", array());
184     } else {
185       $smarty->assign("kolabDelegate", $this->kolabDelegate);
186     }
188     $smarty->assign("mail_account",$this->mail_Account);
190     /* Create InvitationPolicy table */
191     $invitation= "";
192     $this->imapping= array();
193     $nr= 0;
194     $changeState  = "";
195     foreach ($this->kolabInvitationPolicy as $entry){
197       if($this->acl_is_writeable("kolabInvitationPolicy")){
198         $changeState .= "changeState('address".$nr."'); \n changeState('policy".$nr."'); \n
199           changeState('add".$nr."'); \n changeState('remove".$nr."'); \n";
200       }
202       if(!$this->acl_is_writeable("kolabInvitationPolicy")){
203         $dis = " disabled ";
204       }else{
205         if($this->is_account){
206           $dis = " ";
207         }else{
208           $dis = " disabled ";
209         }
210       }
211       $invitation.= "<tr><td>";
213       if(!$this->acl_is_readable("kolabInvitationPolicy"))    {
214       }
216       /* The default entry does not have colons... */
217       if (!preg_match('/:/', $entry)){
218         $invitation.= _("Anonymous");
219         $name= "";
220         $mode= $entry;
221       } else {
222         $name= preg_replace('/:.*$/', '', $entry);
223         $mode= preg_replace('/^[^:]*: */', '', $entry);
225         if(!$this->acl_is_readable("kolabInvitationPolicy")){
226           $name='';
227         }
228         $invitation.= "<input name=\"address$nr\" size=16 maxlength=60 $acl value=\"$name\" id='address".$nr."' ".$dis.">";
229       }
230       $invitation.= "</td>";
232       /* Add mode switch */
233       $invitation.= "<td><select size=\"1\" name=\"policy$nr\" $acl  id='policy".$nr."' ".$dis.">";
234       foreach($policies as $key => $value){
235         if ($key == $mode){
236           $invitation.= "<option value=\"$key\" selected>$value</option>";
237         } else {
238           $invitation.= "<option value=\"$key\">$value</option>";
239         }
240       }
241       
242       /* Assign buttons */
243       $button= "";
244       if ($nr == count($this->kolabInvitationPolicy)-1){
245         $button= "<input type=submit name=\"add$nr\" value=\""._("Add")."\" id='add".$nr."' ".$dis.">";
246       }
247       if ($nr != 0) {
248         $button.= "<input type=submit name=\"remove$nr\" value=\""._("Remove")."\" id='remove".$nr."' ".$dis.">";
249       }
250       
251       $invitation.= "</select>&nbsp;$button</td></tr>\n";
252       $this->imapping[$nr]= $name;
253       $nr++;
254     }
255     $smarty->assign("invitation", $invitation);
256     $smarty->assign("changeState", $changeState);
257     $smarty->assign("kolabState",$this->is_account);
258     $display.= $smarty->fetch (get_template_path('kolab.tpl', TRUE, dirname(__FILE__)));
261     return ($display);
262   }
264   function remove_from_parent()
265   {
266     /* Optionally execute a command after we're done */
267     plugin::remove_from_parent();
268     
269     if(!in_array("kolabInetOrgPerson",$this->attrs['objectClass'])){
270       $this->attrs['objectClass'][] = "kolabInetOrgPerson";
271     }
273     $ldap = $this->config->get_ldap_linK(); 
274     $ldap->cd($this->config->current['BASE']);
275     $ldap->cd ($this->dn);
276     $ldap->modify($this->attrs);
278     $this->handle_post_events('remove');
279     show_ldap_error($ldap->get_error(), sprintf(_("Removing of user/kolab account with dn '%s' failed."),$this->dn));
280   }
283   function check()
284   {
285     /* Call common method to give check the hook */
286     $message= plugin::check();
288     /* FBFuture is in days... */
289     if ($this->kolabFreeBusyFuture != "" && !preg_match('/^[0-9]+$/', $this->kolabFreeBusyFuture)){
290       $message[]= _("The value specified as Free Busy future needs to be an integer.");
291     }
293     /* Check for URL scheme... */
294     if ($this->calFBURL != "" && !@preg_match('/^[^:/]+://[a-z0-9_/.+~-]+$/i', $this->calFBURL)){
295       $message[]= _("The value specified as Free Busy Information URL is invalid.");
296     }
298     /* Check invitation policy for existing mail addresses */
299     foreach($this->kolabInvitationPolicy as $policy){
300       
301       /* Ignore anonymous string */
302       if (!preg_match('/:/', $policy)){
303         continue;
304       }
305       
306       $address= preg_replace('/^([^:]+).*$/', '\1', $policy);
307       if (!is_email($address)){
308         if (!is_email($address, TRUE)){
309           $message[]= sprintf(_("The invitation policy entry for address '%s' is not valid."), $address);
310         }
311       } else {
313         $ldap= $this->config->get_ldap_link();
314         $ldap->cd ($this->config->current['BASE']);
315         $ldap->search('(mail='.$address.')',array("mail"));
316         if ($ldap->count() == 0){
317           $message[]= sprintf(_("There's no mail user with address '%s' for your invitation policy!"), $address);
318         } else {
319           $valid= TRUE;
320         }
321       }
322     }
324     return ($message);
325   }
327   /* Save data to object */
328   function save_object()
329   {
330     /* Do we need to flip is_account state? */
331     if (isset($_POST['connectivityTab'])){
333       if(isset($_POST["kolabState"])){
334         if($this->acl_is_createable()){
335           $this->is_account = true;
336         }
337       }else{
338         if($this->acl_is_removeable()){
339           $this->is_account = false;
340         }
341       }
342       
343       if ($this->acl_is_writeable("unrestrictedMailSize")){
344         if (isset($_POST['unrestrictedMailSize']) && $_POST['unrestrictedMailSize'] == 1){
345           $this->unrestrictedMailSize= 1;
346         } else {
347           $this->unrestrictedMailSize= 0;
348         }
349       }
350     }
352     plugin::save_object();
354     /* Save changes done in invitation policies */
355     if($this->acl_is_writeable("kolabInvitationPolicy")){
356       $nr= 0;
357       $this->kolabInvitationPolicy= array();
358       while (isset($_POST["policy$nr"])){
360         /* Anonymous? */
361         if (!isset($_POST["address$nr"])){
362           $this->kolabInvitationPolicy[]= $_POST["policy$nr"];
363         } else {
364           $this->kolabInvitationPolicy[]= $_POST["address$nr"].": ".$_POST["policy$nr"];
365         }
367         $nr++;
368       }
370       /* If this one is empty, preset with ACT_MANUAL for anonymous users */
371       if (count ($this->kolabInvitationPolicy) == 0){
372         $this->kolabInvitationPolicy= array("ACT_MANUAL");
373       }
374     }
375   }
378   /* Save to LDAP */
379   function save()
380   {
381     /* Check mailmethod before doing something useful */
382     plugin::save();
384     /* Transfer arrays */
385     $this->attrs['kolabDelegate']= $this->kolabDelegate;
386     $this->attrs['kolabInvitationPolicy']= $this->kolabInvitationPolicy;
388     /* unrestrictedMailSize is boolean */
389     if($this->attrs['unrestrictedMailSize']){
390       $this->attrs['unrestrictedMailSize'] = "TRUE";
391     }else{
392       $this->attrs['unrestrictedMailSize'] = "FALSE";
393     }
394   
395     /* Write back to ldap */
396     $ldap= $this->config->get_ldap_link();
397     $ldap->cd($this->dn);
398     $this->cleanup();
399     $ldap->modify ($this->attrs); 
401     show_ldap_error($ldap->get_error(), sprintf(_("Saving of user/kolab account with dn '%s' failed."),$this->dn));
403     /* Optionally execute a command after we're done */
404     if ($this->initially_was_account == $this->is_account){
405       if ($this->is_modified){
406         $this->handle_post_events("mofify");
407       }
408     } else {
409       $this->handle_post_events("add");
410     }
411   }
414   /* Add entry to delegation list */
415   function addDelegate($address)
416   {
417     $this->kolabDelegate[]= $address;
418     $this->kolabDelegate= array_unique ($this->kolabDelegate);
420     sort ($this->kolabDelegate);
421     reset ($this->kolabDelegate);
422     $this->is_modified= TRUE;
423   }
425   function delDelegate($addresses)
426   {
427     $this->kolabDelegate= array_remove_entries ($addresses, $this->kolabDelegate);
428     $this->is_modified= TRUE;
429   }
432   /* Return plugin informations for acl handling  */
433   function plInfo()
434   {
435     return (array(      
436           "plShortName"     => _("Kolab"),
437           "plDescription"   => _("Kolab account settings"),
438           "plSelfModify"    => TRUE,
439           "plDepends"       => array("user"),
440           "plPriority"      => 2,                                 // Position in tabs
441           "plSection"       => "personal",                        // This belongs to personal
442           "plCategory"      => array("users"),
443           "plOptions"       => array(),
445           "plProvidedAcls"  => array(
446             "kolabFreeBusyFuture"   => _("Free busy future"),
447             "unrestrictedMailSize"  => _("Mail size restriction"),
448             "calFBURL"              => _("Free busy information"),
449             "kolabDelegate"         => _("Delegations"),
450             "kolabInvitationPolicy" => _("Invitation policy"))
451           ));
452   }
455 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
456 ?>