Code

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