Code

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