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 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( isset($this->attrs) && isset($this->attrs['kolabHomeServer'])){
59 $this->mail_Account= true;
60 }else{
61 $this->is_account = false;
62 $this->mail_Account = false;
63 }
64 }
67 function execute()
68 {
69 /* Call parent execute */
70 plugin::execute();
72 /* Show tab dialog headers */
73 $display= "";
75 /* Show main page */
76 $smarty= get_smarty();
78 /* Load attributes */
79 foreach($this->attributes as $val){
80 $smarty->assign("$val", $this->$val);
81 }
83 $tmp = $this->plInfo();
84 foreach($tmp['plProvidedAcls'] as $acl => $description){
85 $smarty->assign($acl."ACL",$this->getacl($acl,$this->ReadOnly));
86 $smarty->assign($acl."_W", $this->acl_is_writeable($acl,$this->ReadOnly));
87 }
88 $smarty->assign("is_account" , $this->is_account);
90 if($this->ReadOnly){
91 $smarty->assign("is_removeable",false);
92 $smarty->assign("is_createable",false);
93 }else{
94 $smarty->assign("is_removeable",$this->acl_is_removeable());
95 $smarty->assign("is_createable",$this->acl_is_createable());
96 }
98 /* Check for invitation action */
99 $nr= 0;
100 while (isset($_POST["policy$nr"])){
101 if (isset($_POST["add$nr"])){
102 $this->kolabInvitationPolicy[]= ": ACT_MANUAL";
103 }
104 if (isset($_POST["remove$nr"])){
105 $new= array();
106 foreach ($this->kolabInvitationPolicy as $entry){
107 if (!preg_match("/^".$this->imapping[$nr].":/", $entry)){
108 $new[]= $entry;
109 }
110 }
111 $this->kolabInvitationPolicy= $new;
112 }
113 $nr++;
114 }
116 /* Unify addresses */
117 $new= array();
118 foreach($this->kolabInvitationPolicy as $value){
119 $address= preg_replace('/^([^:]+:).*$/', '\1', $value);
120 $new[$address]= $value;
121 }
122 $this->kolabInvitationPolicy= array();
123 foreach($new as $value){
124 $this->kolabInvitationPolicy[]= $value;
125 }
127 /* Add delegation */
128 if (isset($_POST['add_delegation'])){
129 if ($_POST['delegate_address'] != ""){
131 /* Valid email address specified? */
132 $address= $_POST['delegate_address'];
133 $valid= FALSE;
134 if (!is_email($address)){
135 if (!is_email($address, TRUE)){
136 print_red (_("You're trying to add an invalid email address to the list of delegations."));
137 }
138 } else {
140 $ldap= $this->config->get_ldap_link();
141 $ldap->cd ($this->config->current['BASE']);
142 $ldap->search('(mail='.$address.')',array("mail"));
143 if ($ldap->count() == 0){
144 print_red (_("The mail address you're trying to add is no primary mail address of an existing user."));
145 } else {
146 $valid= TRUE;
147 }
148 }
150 if ($valid){
151 /* Add it */
152 if ($this->acl_is_writeable("kolabDelegate")){
153 $this->addDelegate ($address);
154 $this->is_modified= TRUE;
155 }
157 }
158 }
159 }
161 /* Delete forward email addresses */
162 if ((isset($_POST['delete_delegation'])) && (isset($_POST['delegate_list']))){
163 if (count($_POST['delegate_list']) && $this->acl_is_writeable("kolabDelegate")){
164 $this->delDelegate ($_POST['delegate_list']);
165 }
166 }
168 /* Assemble policies */
169 $policies= array( 'ACT_ALWAYS_ACCEPT' => _("Always accept"),
170 'ACT_ALWAYS_REJECT' => _("Always reject"),
171 'ACT_REJECT_IF_CONFLICTS' => _("Reject if conflicts"),
172 'ACT_MANUAL_IF_CONFLICTS' => _("Manual if conflicts"),
173 'ACT_MANUAL' => _("Manual"));
174 $smarty->assign('policies', $policies);
176 /* Adjust checkbox */
177 if ($this->unrestrictedMailSize){
178 $smarty->assign('unrestrictedMailSizeState', "checked");
179 } else {
180 $smarty->assign('unrestrictedMailSizeState', "");
181 }
183 /* Transfer delegation list */
184 if (!count($this->kolabDelegate)){
185 /* Smarty will produce <option value=""></option> and tidy don't like that, so tell smarty to create no option (array();)*/
186 $smarty->assign("kolabDelegate", array());
187 } else {
188 $smarty->assign("kolabDelegate", $this->kolabDelegate);
189 }
191 $smarty->assign("mail_account",$this->mail_Account);
193 /* Create InvitationPolicy table */
194 $invitation= "";
195 $this->imapping= array();
196 $nr= 0;
197 $changeState = "";
198 foreach ($this->kolabInvitationPolicy as $entry){
200 if($this->acl_is_writeable("kolabInvitationPolicy")){
201 $changeState .= "changeState('address".$nr."'); \n changeState('policy".$nr."'); \n
202 changeState('add".$nr."'); \n changeState('remove".$nr."'); \n";
203 }
205 if(!$this->acl_is_writeable("kolabInvitationPolicy")){
206 $dis = " disabled ";
207 }else{
208 if($this->is_account){
209 $dis = " ";
210 }else{
211 $dis = " disabled ";
212 }
213 }
214 $invitation.= "<tr><td>";
216 if(!$this->acl_is_readable("kolabInvitationPolicy")) {
217 }
219 /* The default entry does not have colons... */
220 if (!preg_match('/:/', $entry)){
221 $invitation.= _("Anonymous");
222 $name= "";
223 $mode= $entry;
224 } else {
225 $name= preg_replace('/:.*$/', '', $entry);
226 $mode= preg_replace('/^[^:]*: */', '', $entry);
228 if(!$this->acl_is_readable("kolabInvitationPolicy")){
229 $name='';
230 }
231 $invitation.= "<input name=\"address$nr\" size=16 maxlength=60 value=\"$name\" id='address".$nr."' ".$dis.">";
232 }
233 $invitation.= "</td>";
235 /* Add mode switch */
236 $invitation.= "<td><select size=\"1\" name=\"policy$nr\" id='policy".$nr."' ".$dis.">";
237 foreach($policies as $key => $value){
238 if ($key == $mode){
239 $invitation.= "<option value=\"$key\" selected>$value</option>";
240 } else {
241 $invitation.= "<option value=\"$key\">$value</option>";
242 }
243 }
245 /* Assign buttons */
246 $button= "";
247 if ($nr == count($this->kolabInvitationPolicy)-1){
248 $button= "<input type=submit name=\"add$nr\" value=\""._("Add")."\" id='add".$nr."' ".$dis.">";
249 }
250 if ($nr != 0) {
251 $button.= "<input type=submit name=\"remove$nr\" value=\""._("Remove")."\" id='remove".$nr."' ".$dis.">";
252 }
254 $invitation.= "</select> $button</td></tr>\n";
255 $this->imapping[$nr]= $name;
256 $nr++;
257 }
258 $smarty->assign("invitation", $invitation);
259 $smarty->assign("changeState", $changeState);
260 $smarty->assign("kolabState",$this->is_account);
261 $display.= $smarty->fetch (get_template_path('kolab.tpl', TRUE, dirname(__FILE__)));
264 return ($display);
265 }
267 function remove_from_parent()
268 {
269 /* Optionally execute a command after we're done */
270 plugin::remove_from_parent();
272 if(!in_array("kolabInetOrgPerson",$this->attrs['objectClass'])){
273 $this->attrs['objectClass'][] = "kolabInetOrgPerson";
274 }
276 $ldap = $this->config->get_ldap_linK();
277 $ldap->cd($this->config->current['BASE']);
278 $ldap->cd ($this->dn);
279 $ldap->modify($this->attrs);
281 $this->handle_post_events('remove');
282 show_ldap_error($ldap->get_error(), sprintf(_("Removing of user/kolab account with dn '%s' failed."),$this->dn));
283 }
286 function check()
287 {
288 /* Call common method to give check the hook */
289 $message= plugin::check();
291 /* FBFuture is in days... */
292 if ($this->kolabFreeBusyFuture != "" && !preg_match('/^[0-9]+$/', $this->kolabFreeBusyFuture)){
293 $message[]= _("The value specified as Free Busy future needs to be an integer.");
294 }
296 /* Check for URL scheme... */
297 if ($this->calFBURL != "" && !@preg_match('/^[^:/]+://[a-z0-9_/.+~-]+$/i', $this->calFBURL)){
298 $message[]= _("The value specified as Free Busy Information URL is invalid.");
299 }
301 /* Check invitation policy for existing mail addresses */
302 foreach($this->kolabInvitationPolicy as $policy){
304 /* Ignore anonymous string */
305 if (!preg_match('/:/', $policy)){
306 continue;
307 }
309 $address= preg_replace('/^([^:]+).*$/', '\1', $policy);
310 if (!is_email($address)){
311 if (!is_email($address, TRUE)){
312 $message[]= sprintf(_("The invitation policy entry for address '%s' is not valid."), $address);
313 }
314 } else {
316 $ldap= $this->config->get_ldap_link();
317 $ldap->cd ($this->config->current['BASE']);
318 $ldap->search('(mail='.$address.')',array("mail"));
319 if ($ldap->count() == 0){
320 $message[]= sprintf(_("There's no mail user with address '%s' for your invitation policy!"), $address);
321 } else {
322 $valid= TRUE;
323 }
324 }
325 }
327 return ($message);
328 }
330 /* Save data to object */
331 function save_object()
332 {
333 /* Do we need to flip is_account state? */
334 if (isset($_POST['connectivityTab'])){
336 if(isset($_POST["kolabState"])){
337 if($this->acl_is_createable()){
338 $this->is_account = true;
339 }
340 }else{
341 if($this->acl_is_removeable()){
342 $this->is_account = false;
343 }
344 }
346 if ($this->acl_is_writeable("unrestrictedMailSize")){
347 if (isset($_POST['unrestrictedMailSize']) && $_POST['unrestrictedMailSize'] == 1){
348 $this->unrestrictedMailSize= 1;
349 } else {
350 $this->unrestrictedMailSize= 0;
351 }
352 }
353 }
355 plugin::save_object();
357 /* Save changes done in invitation policies */
358 if($this->acl_is_writeable("kolabInvitationPolicy")){
359 $nr= 0;
360 $this->kolabInvitationPolicy= array();
361 while (isset($_POST["policy$nr"])){
363 /* Anonymous? */
364 if (!isset($_POST["address$nr"])){
365 $this->kolabInvitationPolicy[]= $_POST["policy$nr"];
366 } else {
367 $this->kolabInvitationPolicy[]= $_POST["address$nr"].": ".$_POST["policy$nr"];
368 }
370 $nr++;
371 }
373 /* If this one is empty, preset with ACT_MANUAL for anonymous users */
374 if (count ($this->kolabInvitationPolicy) == 0){
375 $this->kolabInvitationPolicy= array("ACT_MANUAL");
376 }
377 }
378 }
381 /* Save to LDAP */
382 function save()
383 {
384 /* Check mailmethod before doing something useful */
385 plugin::save();
387 /* Transfer arrays */
388 $this->attrs['kolabDelegate']= $this->kolabDelegate;
389 $this->attrs['kolabInvitationPolicy']= $this->kolabInvitationPolicy;
391 /* unrestrictedMailSize is boolean */
392 if($this->attrs['unrestrictedMailSize']){
393 $this->attrs['unrestrictedMailSize'] = "TRUE";
394 }else{
395 $this->attrs['unrestrictedMailSize'] = "FALSE";
396 }
398 /* Write back to ldap */
399 $ldap= $this->config->get_ldap_link();
400 $ldap->cd($this->dn);
401 $this->cleanup();
402 $ldap->modify ($this->attrs);
404 show_ldap_error($ldap->get_error(), sprintf(_("Saving of user/kolab account with dn '%s' failed."),$this->dn));
406 /* Optionally execute a command after we're done */
407 if ($this->initially_was_account == $this->is_account){
408 if ($this->is_modified){
409 $this->handle_post_events("mofify");
410 }
411 } else {
412 $this->handle_post_events("add");
413 }
414 }
417 /* Add entry to delegation list */
418 function addDelegate($address)
419 {
420 $this->kolabDelegate[]= $address;
421 $this->kolabDelegate= array_unique ($this->kolabDelegate);
423 sort ($this->kolabDelegate);
424 reset ($this->kolabDelegate);
425 $this->is_modified= TRUE;
426 }
428 function delDelegate($addresses)
429 {
430 $this->kolabDelegate= array_remove_entries ($addresses, $this->kolabDelegate);
431 $this->is_modified= TRUE;
432 }
435 /* Return plugin informations for acl handling */
436 function plInfo()
437 {
438 return (array(
439 "plShortName" => _("Kolab"),
440 "plDescription" => _("Kolab account settings"),
441 "plSelfModify" => TRUE,
442 "plDepends" => array("user"),
443 "plPriority" => 2, // Position in tabs
444 "plSection" => "personal", // This belongs to personal
445 "plCategory" => array("users"),
446 "plOptions" => array(),
448 "plProvidedAcls" => array(
449 "kolabFreeBusyFuture" => _("Free busy future"),
450 "unrestrictedMailSize" => _("Mail size restriction"),
451 "calFBURL" => _("Free busy information"),
452 "kolabDelegate" => _("Delegations"),
453 "kolabInvitationPolicy" => _("Invitation policy"))
454 ));
455 }
456 }
458 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
459 ?>