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;
23 var $view_logged = 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->by_object['mailAccount']) && $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 /* Log view */
80 if($this->is_account && !$this->view_logged){
81 $this->view_logged = TRUE;
82 new log("view","users/".get_class($this),$this->dn);
83 }
85 /* Show tab dialog headers */
86 $display= "";
88 /* Show main page */
89 $smarty= get_smarty();
91 /* Load attributes */
92 foreach($this->attributes as $val){
93 $smarty->assign("$val", $this->$val);
94 }
96 $tmp = $this->plInfo();
97 foreach($tmp['plProvidedAcls'] as $acl => $description){
98 $smarty->assign($acl."ACL",$this->getacl($acl,$this->ReadOnly));
99 $smarty->assign($acl."_W", $this->acl_is_writeable($acl,$this->ReadOnly));
100 }
101 $smarty->assign("is_account" , $this->is_account);
103 if($this->ReadOnly){
104 $smarty->assign("is_removeable",false);
105 $smarty->assign("is_createable",false);
106 }else{
107 $smarty->assign("is_removeable",$this->acl_is_removeable());
108 $smarty->assign("is_createable",$this->acl_is_createable());
109 }
111 /* Check for invitation action */
112 $nr= 0;
113 while (isset($_POST["policy$nr"])){
114 if (isset($_POST["add$nr"])){
115 $this->kolabInvitationPolicy[]= ": ACT_MANUAL";
116 }
117 if (isset($_POST["remove$nr"])){
118 $new= array();
119 foreach ($this->kolabInvitationPolicy as $entry){
120 if (!preg_match("/^".$this->imapping[$nr].":/", $entry)){
121 $new[]= $entry;
122 }
123 }
124 $this->kolabInvitationPolicy= $new;
125 }
126 $nr++;
127 }
129 /* Unify addresses */
130 $new= array();
131 foreach($this->kolabInvitationPolicy as $value){
132 $address= preg_replace('/^([^:]+:).*$/', '\1', $value);
133 $new[$address]= $value;
134 }
135 $this->kolabInvitationPolicy= array();
136 foreach($new as $value){
137 $this->kolabInvitationPolicy[]= $value;
138 }
140 /* Add delegation */
141 if (isset($_POST['add_delegation'])){
142 if ($_POST['delegate_address'] != ""){
144 /* Valid email address specified? */
145 $address= $_POST['delegate_address'];
146 $valid= FALSE;
147 if (!is_email($address)){
148 if (!is_email($address, TRUE)){
149 print_red (_("You're trying to add an invalid email address to the list of delegations."));
150 }
151 } else {
153 $ldap= $this->config->get_ldap_link();
154 $ldap->cd ($this->config->current['BASE']);
155 $ldap->search('(mail='.$address.')',array("mail"));
156 if ($ldap->count() == 0){
157 print_red (_("The mail address you're trying to add is no primary mail address of an existing user."));
158 } else {
159 $valid= TRUE;
160 }
161 }
163 if ($valid){
164 /* Add it */
165 if ($this->acl_is_writeable("kolabDelegate")){
166 $this->addDelegate ($address);
167 $this->is_modified= TRUE;
168 }
170 }
171 }
172 }
174 /* Delete forward email addresses */
175 if ((isset($_POST['delete_delegation'])) && (isset($_POST['delegate_list']))){
176 if (count($_POST['delegate_list']) && $this->acl_is_writeable("kolabDelegate")){
177 $this->delDelegate ($_POST['delegate_list']);
178 }
179 }
181 /* Assemble policies */
182 $policies= array( 'ACT_ALWAYS_ACCEPT' => _("Always accept"),
183 'ACT_ALWAYS_REJECT' => _("Always reject"),
184 'ACT_REJECT_IF_CONFLICTS' => _("Reject if conflicts"),
185 'ACT_MANUAL_IF_CONFLICTS' => _("Manual if conflicts"),
186 'ACT_MANUAL' => _("Manual"));
187 $smarty->assign('policies', $policies);
189 /* Adjust checkbox */
190 if ($this->unrestrictedMailSize){
191 $smarty->assign('unrestrictedMailSizeState', "checked");
192 } else {
193 $smarty->assign('unrestrictedMailSizeState', "");
194 }
196 /* Transfer delegation list */
197 if (!count($this->kolabDelegate)){
198 /* Smarty will produce <option value=""></option> and tidy don't like that, so tell smarty to create no option (array();)*/
199 $smarty->assign("kolabDelegate", array());
200 } else {
201 $smarty->assign("kolabDelegate", $this->kolabDelegate);
202 }
204 $smarty->assign("mail_account",$this->mail_Account);
206 /* Create InvitationPolicy table */
207 $invitation= "";
208 $this->imapping= array();
209 $nr= 0;
210 $changeState = "";
211 foreach ($this->kolabInvitationPolicy as $entry){
213 if($this->acl_is_writeable("kolabInvitationPolicy")){
214 $changeState .= "changeState('address".$nr."'); \n changeState('policy".$nr."'); \n
215 changeState('add".$nr."'); \n changeState('remove".$nr."'); \n";
216 }
218 if(!$this->acl_is_writeable("kolabInvitationPolicy")){
219 $dis = " disabled ";
220 }else{
221 if($this->is_account){
222 $dis = " ";
223 }else{
224 $dis = " disabled ";
225 }
226 }
227 $invitation.= "<tr><td>";
229 if(!$this->acl_is_readable("kolabInvitationPolicy")) {
230 }
232 /* The default entry does not have colons... */
233 if (!preg_match('/:/', $entry)){
234 $invitation.= _("Anonymous");
235 $name= "";
236 $mode= $entry;
237 } else {
238 $name= preg_replace('/:.*$/', '', $entry);
239 $mode= preg_replace('/^[^:]*: */', '', $entry);
241 if(!$this->acl_is_readable("kolabInvitationPolicy")){
242 $name='';
243 }
244 $invitation.= "<input name=\"address$nr\" size=16 maxlength=60 value=\"$name\" id='address".$nr."' ".$dis.">";
245 }
246 $invitation.= "</td>";
248 /* Add mode switch */
249 $invitation.= "<td><select size=\"1\" name=\"policy$nr\" id='policy".$nr."' ".$dis.">";
250 foreach($policies as $key => $value){
251 if ($key == $mode){
252 $invitation.= "<option value=\"$key\" selected>$value</option>";
253 } else {
254 $invitation.= "<option value=\"$key\">$value</option>";
255 }
256 }
258 /* Assign buttons */
259 $button= "";
260 if ($nr == count($this->kolabInvitationPolicy)-1){
261 $button= "<input type=submit name=\"add$nr\" value=\""._("Add")."\" id='add".$nr."' ".$dis.">";
262 }
263 if ($nr != 0) {
264 $button.= "<input type=submit name=\"remove$nr\" value=\""._("Remove")."\" id='remove".$nr."' ".$dis.">";
265 }
267 $invitation.= "</select> $button</td></tr>\n";
268 $this->imapping[$nr]= $name;
269 $nr++;
270 }
271 $smarty->assign("invitation", $invitation);
272 $smarty->assign("changeState", $changeState);
273 $smarty->assign("kolabState",$this->is_account);
274 $display.= $smarty->fetch (get_template_path('kolab.tpl', TRUE, dirname(__FILE__)));
277 return ($display);
278 }
280 function remove_from_parent()
281 {
282 if(!$this->initially_was_account){
283 return;
284 }
286 /* Optionally execute a command after we're done */
287 plugin::remove_from_parent();
289 if(!in_array("kolabInetOrgPerson",$this->attrs['objectClass'])){
290 $this->attrs['objectClass'][] = "kolabInetOrgPerson";
291 }
293 $ldap = $this->config->get_ldap_linK();
294 $ldap->cd($this->config->current['BASE']);
295 $ldap->cd ($this->dn);
296 $ldap->modify($this->attrs);
298 /* Log last action */
299 new log("remove","users/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
301 $this->handle_post_events('remove',array("uid" => $this->uid));
302 show_ldap_error($ldap->get_error(), sprintf(_("Removing of user/kolab account with dn '%s' failed."),$this->dn));
303 }
306 function check()
307 {
308 /* Call common method to give check the hook */
309 $message= plugin::check();
311 /* FBFuture is in days... */
312 if ($this->kolabFreeBusyFuture != "" && !preg_match('/^[0-9]+$/', $this->kolabFreeBusyFuture)){
313 $message[]= _("The value specified as Free Busy future needs to be an integer.");
314 }
316 /* Check for URL scheme... */
317 if(!empty($this->calFBURL) && !preg_match("/http+(s)*:\/\//",$this->calFBURL)){
318 $message[]= _("The value specified as Free Busy Information URL is invalid.");
319 }
321 /* Check invitation policy for existing mail addresses */
322 foreach($this->kolabInvitationPolicy as $policy){
324 /* Ignore anonymous string */
325 if (!preg_match('/:/', $policy)){
326 continue;
327 }
329 $address= preg_replace('/^([^:]+).*$/', '\1', $policy);
330 if (!is_email($address)){
331 if (!is_email($address, TRUE)){
332 $message[]= sprintf(_("The invitation policy entry for address '%s' is not valid."), $address);
333 }
334 } else {
336 $ldap= $this->config->get_ldap_link();
337 $ldap->cd ($this->config->current['BASE']);
338 $ldap->search('(mail='.$address.')',array("mail"));
339 if ($ldap->count() == 0){
340 $message[]= sprintf(_("There's no mail user with address '%s' for your invitation policy!"), $address);
341 } else {
342 $valid= TRUE;
343 }
344 }
345 }
347 return ($message);
348 }
350 /* Save data to object */
351 function save_object()
352 {
353 /* Do we need to flip is_account state? */
354 if (isset($_POST['connectivityTab'])){
356 if(isset($_POST["kolabState"])){
357 if($this->acl_is_createable()){
358 $this->is_account = true;
359 }
360 }else{
361 if($this->acl_is_removeable()){
362 $this->is_account = false;
363 }
364 }
366 if ($this->acl_is_writeable("unrestrictedMailSize")){
367 if (isset($_POST['unrestrictedMailSize']) && $_POST['unrestrictedMailSize'] == 1){
368 $this->unrestrictedMailSize= 1;
369 } else {
370 $this->unrestrictedMailSize= 0;
371 }
372 }
373 }
375 plugin::save_object();
377 /* Save changes done in invitation policies */
378 if($this->acl_is_writeable("kolabInvitationPolicy")){
379 $nr= 0;
380 $this->kolabInvitationPolicy= array();
381 while (isset($_POST["policy$nr"])){
383 /* Anonymous? */
384 if (!isset($_POST["address$nr"])){
385 $this->kolabInvitationPolicy[]= $_POST["policy$nr"];
386 } else {
387 $this->kolabInvitationPolicy[]= $_POST["address$nr"].": ".$_POST["policy$nr"];
388 }
390 $nr++;
391 }
393 /* If this one is empty, preset with ACT_MANUAL for anonymous users */
394 if (count ($this->kolabInvitationPolicy) == 0){
395 $this->kolabInvitationPolicy= array("ACT_MANUAL");
396 }
397 }
398 }
401 /* Save to LDAP */
402 function save()
403 {
404 /* Check mailmethod before doing something useful */
405 plugin::save();
407 /* Transfer arrays */
408 $this->attrs['kolabDelegate']= $this->kolabDelegate;
409 $this->attrs['kolabInvitationPolicy']= $this->kolabInvitationPolicy;
411 /* unrestrictedMailSize is boolean */
412 if($this->attrs['unrestrictedMailSize']){
413 $this->attrs['unrestrictedMailSize'] = "TRUE";
414 }else{
415 $this->attrs['unrestrictedMailSize'] = "FALSE";
416 }
418 /* Write back to ldap */
419 $ldap= $this->config->get_ldap_link();
420 $ldap->cd($this->dn);
421 $this->cleanup();
422 $ldap->modify ($this->attrs);
424 /* Log last action */
425 if($this->initially_was_account){
426 new log("modify","users/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
427 }else{
428 new log("create","users/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
429 }
431 show_ldap_error($ldap->get_error(), sprintf(_("Saving of user/kolab account with dn '%s' failed."),$this->dn));
433 /* Optionally execute a command after we're done */
434 if ($this->initially_was_account == $this->is_account){
435 if ($this->is_modified){
436 $this->handle_post_events("modify",array("uid" => $this->uid));
437 }
438 } else {
439 $this->handle_post_events("add",array("uid" => $this->uid));
440 }
441 }
444 /* Add entry to delegation list */
445 function addDelegate($address)
446 {
447 $this->kolabDelegate[]= $address;
448 $this->kolabDelegate= array_unique ($this->kolabDelegate);
450 sort ($this->kolabDelegate);
451 reset ($this->kolabDelegate);
452 $this->is_modified= TRUE;
453 }
455 function delDelegate($addresses)
456 {
457 $this->kolabDelegate= array_remove_entries ($addresses, $this->kolabDelegate);
458 $this->is_modified= TRUE;
459 }
462 /* Return plugin informations for acl handling */
463 static function plInfo()
464 {
465 return (array(
466 "plShortName" => _("Kolab"),
467 "plDescription" => _("Kolab account settings")." : <u>"._("Connectivity addon")."</u>",
468 "plSelfModify" => TRUE,
469 "plDepends" => array("user"),
470 "plPriority" => 20, // Position in tabs
471 "plSection" => array("personal" => _("My account")),
472 "plCategory" => array("users"),
473 "plOptions" => array(),
475 "plProvidedAcls" => array(
476 "kolabFreeBusyFuture" => _("Free busy future"),
477 "unrestrictedMailSize" => _("Mail size restriction"),
478 "calFBURL" => _("Free busy information"),
479 "kolabDelegate" => _("Delegations"),
480 "kolabInvitationPolicy" => _("Invitation policy"))
481 ));
482 }
483 }
485 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
486 ?>