Code

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