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();
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 }
53 }
56 function execute()
57 {
58 /* Call parent execute */
59 plugin::execute();
61 /* Show tab dialog headers */
62 $display= "";
64 /* Show main page */
65 $smarty= get_smarty();
67 /* Load attributes */
68 foreach($this->attributes as $val){
69 $smarty->assign("$val", $this->$val);
70 $smarty->assign($val."ACL", chkacl($this->acl, "$val"));
71 }
72 $smarty->assign("kolabAccountACL", chkacl($this->acl, "kolabAccountACL"));
74 /* Check for invitation action */
75 $nr= 0;
76 while (isset($_POST["policy$nr"])){
77 if (isset($_POST["add$nr"])){
78 $this->kolabInvitationPolicy[]= ": ACT_MANUAL";
79 }
80 if (isset($_POST["remove$nr"])){
81 $new= array();
82 foreach ($this->kolabInvitationPolicy as $entry){
83 if (!preg_match("/^".$this->imapping[$nr].":/", $entry)){
84 $new[]= $entry;
85 }
86 }
87 $this->kolabInvitationPolicy= $new;
88 }
89 $nr++;
90 }
92 /* Unify addresses */
93 $new= array();
94 foreach($this->kolabInvitationPolicy as $value){
95 $address= preg_replace('/^([^:]+:).*$/', '\1', $value);
96 $new[$address]= $value;
97 }
98 $this->kolabInvitationPolicy= array();
99 foreach($new as $value){
100 $this->kolabInvitationPolicy[]= $value;
101 }
103 /* Add delegation */
104 if (isset($_POST['add_delegation'])){
105 if ($_POST['delegate_address'] != ""){
107 /* Valid email address specified? */
108 $address= $_POST['delegate_address'];
109 $valid= FALSE;
110 if (!is_email($address)){
111 if (!is_email($address, TRUE)){
112 print_red (_("You're trying to add an invalid email address to the list of delegations."));
113 }
114 } else {
116 $ldap= $this->config->get_ldap_link();
117 $ldap->cd ($this->config->current['BASE']);
118 $ldap->search('(mail='.$address.')',array("mail"));
119 if ($ldap->count() == 0){
120 print_red (_("The mail address you're trying to add is no primary mail address of an existing user."));
121 } else {
122 $valid= TRUE;
123 }
124 }
126 if ($valid){
127 /* Add it */
128 if (chkacl ($this->acl, "kolabDelegate") == ""){
129 $this->addDelegate ($address);
130 $this->is_modified= TRUE;
131 }
133 }
134 }
135 }
137 /* Delete forward email addresses */
138 if (isset($_POST['delete_delegation'])){
139 if (count($_POST['delegate_list'])
140 && chkacl ($this->acl, "kolabDelegate") == ""){
142 $this->delDelegate ($_POST['delegate_list']);
143 }
144 }
146 /* Assemble policies */
147 $policies= array( 'ACT_ALWAYS_ACCEPT' => _("Always accept"),
148 'ACT_ALWAYS_REJECT' => _("Always reject"),
149 'ACT_REJECT_IF_CONFLICTS' => _("Reject if conflicts"),
150 'ACT_MANUAL_IF_CONFLICTS' => _("Manual if conflicts"),
151 'ACT_MANUAL' => _("Manual"));
152 $smarty->assign('policies', $policies);
154 /* Adjust checkbox */
155 if ($this->unrestrictedMailSize){
156 $smarty->assign('unrestrictedMailSizeState', "checked");
157 } else {
158 $smarty->assign('unrestrictedMailSizeState', "");
159 }
161 /* Transfer account states for this union */
162 if (isset($this->parent) && $this->parent->by_object['mailAccount']->is_account){
163 $smarty->assign('mail_account', 'true');
164 } elseif($this-> initially_was_account && (isset($this->attrs['objectClass']) && (in_array("kolabInetOrgPerson",$this->attrs['objectClass'])))){
165 $smarty->assign('mail_account', 'true');
166 }else{
167 $smarty->assign('mail_account', '');
168 }
170 /* Transfer delegation list */
171 if (!count($this->kolabDelegate)){
172 /* Smarty will produce <option value=""></option> and tidy don't like that, so tell smarty to create no option (array();)*/
173 $smarty->assign("kolabDelegate", array());
174 } else {
175 $smarty->assign("kolabDelegate", $this->kolabDelegate);
176 }
177 $smarty->assign("kolabDelegateACL", chkacl($this->acl, $this->kolabDelegate));
179 /* Create InvitationPolicy table */
180 $invitation= "";
181 $this->imapping= array();
182 $nr= 0;
183 $acl= chkacl($this->acl, "kolabInvitationPolicy");
184 $changeState = "";
185 foreach ($this->kolabInvitationPolicy as $entry){
187 $changeState .= "changeState('address".$nr."'); \n changeState('policy".$nr."'); \n
188 changeState('add".$nr."'); \n changeState('remove".$nr."'); \n";
190 $invitation.= "<tr><td>";
191 if($this->is_account){
192 $dis = " ";
193 }else{
194 $dis = " disabled ";
195 }
198 /* The default entry does not have colons... */
199 if (!preg_match('/:/', $entry)){
200 $invitation.= _("Anonymous");
201 $name= "";
202 $mode= $entry;
203 } else {
204 $name= preg_replace('/:.*$/', '', $entry);
205 $mode= preg_replace('/^[^:]*: */', '', $entry);
206 $invitation.= "<input name=\"address$nr\" size=16 maxlength=60 $acl value=\"$name\" id='address".$nr."' ".$dis.">";
207 }
208 $invitation.= "</td>";
210 /* Add mode switch */
211 $invitation.= "<td><select size=\"1\" name=\"policy$nr\" $acl id='policy".$nr."' ".$dis.">";
212 foreach($policies as $key => $value){
213 if ($key == $mode){
214 $invitation.= "<option value=\"$key\" selected>$value</option>";
215 } else {
216 $invitation.= "<option value=\"$key\">$value</option>";
217 }
218 }
220 /* Assign buttons */
221 $button= "";
222 if ($nr == count($this->kolabInvitationPolicy)-1){
223 $button= "<input type=submit name=\"add$nr\" value=\""._("Add")."\" id='add".$nr."' ".$dis.">";
224 }
225 if ($nr != 0) {
226 $button.= "<input type=submit name=\"remove$nr\" value=\""._("Remove")."\" id='remove".$nr."' ".$dis.">";
227 }
229 $invitation.= "</select> $button</td></tr>\n";
230 $this->imapping[$nr]= $name;
231 $nr++;
232 }
233 $smarty->assign("invitation", $invitation);
234 $smarty->assign("changeState", $changeState);
235 $smarty->assign("kolabState",$this->is_account);
236 $display.= $smarty->fetch (get_template_path('kolab.tpl', TRUE, dirname(__FILE__)));
239 return ($display);
240 }
242 function remove_from_parent()
243 {
244 /* Optionally execute a command after we're done */
245 plugin::remove_from_parent();
247 if(!in_array("kolabInetOrgPerson",$this->attrs['objectClass'])){
248 $this->attrs['objectClass'][] = "kolabInetOrgPerson";
249 }
251 $ldap = $this->config->get_ldap_linK();
252 $ldap->cd($this->config->current['BASE']);
253 $ldap->cd ($this->dn);
254 $ldap->modify($this->attrs);
256 $this->handle_post_events('remove');
257 show_ldap_error($ldap->get_error(), sprintf(_("Removing of user/kolab account with dn '%s' failed."),$this->dn));
258 }
261 function check()
262 {
263 /* Call common method to give check the hook */
264 $message= plugin::check();
266 /* FBFuture is in days... */
267 if ($this->kolabFreeBusyFuture != "" && !preg_match('/^[0-9]+$/', $this->kolabFreeBusyFuture)){
268 $message[]= _("The value specified as Free Busy future needs to be an integer.");
269 }
271 /* Check for URL scheme... */
272 if ($this->calFBURL != "" && !@preg_match('/^[^:/]+://[a-z0-9_/.+~-]+$/i', $this->calFBURL)){
273 $message[]= _("The value specified as Free Busy Information URL is invalid.");
274 }
276 /* Check invitation policy for existing mail addresses */
277 foreach($this->kolabInvitationPolicy as $policy){
279 /* Ignore anonymous string */
280 if (!preg_match('/:/', $policy)){
281 continue;
282 }
284 $address= preg_replace('/^([^:]+).*$/', '\1', $policy);
285 if (!is_email($address)){
286 if (!is_email($address, TRUE)){
287 $message[]= sprintf(_("The invitation policy entry for address '%s' is not valid."), $address);
288 }
289 } else {
291 $ldap= $this->config->get_ldap_link();
292 $ldap->cd ($this->config->current['BASE']);
293 $ldap->search('(mail='.$address.')',array("mail"));
294 if ($ldap->count() == 0){
295 $message[]= sprintf(_("There's no mail user with address '%s' for your invitation policy!"), $address);
296 } else {
297 $valid= TRUE;
298 }
299 }
300 }
302 return ($message);
303 }
305 /* Save data to object */
306 function save_object()
307 {
308 /* Do we need to flip is_account state? */
309 if (isset($_POST['connectivityTab'])){
311 if(isset($_POST["kolabState"])){
312 $this->is_account = true;
313 }else{
314 $this->is_account = false;
315 }
317 if (chkacl('unrestrictedMailSize', $this->acl == "")){
318 if (isset($_POST['unrestrictedMailSize']) && $_POST['unrestrictedMailSize'] == 1){
319 $this->unrestrictedMailSize= 1;
320 } else {
321 $this->unrestrictedMailSize= 0;
322 }
323 }
324 }
326 plugin::save_object();
328 /* Save changes done in invitation policies */
329 $nr= 0;
330 $this->kolabInvitationPolicy= array();
331 while (isset($_POST["policy$nr"])){
333 /* Anonymous? */
334 if (!isset($_POST["address$nr"])){
335 $this->kolabInvitationPolicy[]= $_POST["policy$nr"];
336 } else {
337 $this->kolabInvitationPolicy[]= $_POST["address$nr"].": ".$_POST["policy$nr"];
338 }
340 $nr++;
341 }
343 /* If this one is empty, preset with ACT_MANUAL for anonymous users */
344 if (count ($this->kolabInvitationPolicy) == 0){
345 $this->kolabInvitationPolicy= array("ACT_MANUAL");
346 }
348 }
351 /* Save to LDAP */
352 function save()
353 {
354 /* Check mailmethod before doing something useful */
355 plugin::save();
357 /* Transfer arrays */
358 $this->attrs['kolabDelegate']= $this->kolabDelegate;
359 $this->attrs['kolabInvitationPolicy']= $this->kolabInvitationPolicy;
361 /* unrestrictedMailSize is boolean */
362 if($this->attrs['unrestrictedMailSize']){
363 $this->attrs['unrestrictedMailSize'] = "TRUE";
364 }else{
365 $this->attrs['unrestrictedMailSize'] = "FALSE";
366 }
368 /* Write back to ldap */
369 $ldap= $this->config->get_ldap_link();
370 $ldap->cd($this->dn);
371 $this->cleanup();
372 $ldap->modify ($this->attrs);
374 show_ldap_error($ldap->get_error(), sprintf(_("Saving of user/kolab account with dn '%s' failed."),$this->dn));
376 /* Optionally execute a command after we're done */
377 if ($this->initially_was_account == $this->is_account){
378 if ($this->is_modified){
379 $this->handle_post_events("mofify");
380 }
381 } else {
382 $this->handle_post_events("add");
383 }
384 }
387 /* Add entry to delegation list */
388 function addDelegate($address)
389 {
390 $this->kolabDelegate[]= $address;
391 $this->kolabDelegate= array_unique ($this->kolabDelegate);
393 sort ($this->kolabDelegate);
394 reset ($this->kolabDelegate);
395 $this->is_modified= TRUE;
396 }
398 function delDelegate($addresses)
399 {
400 $this->kolabDelegate= array_remove_entries ($addresses, $this->kolabDelegate);
401 $this->is_modified= TRUE;
402 }
405 /* Return plugin informations for acl handling */
406 function plInfo()
407 {
408 return (array(
409 "plDescription" => _("Koalb account settings"),
410 "plSelfModify" => TRUE,
411 "plDepends" => array("objectClass" => "gosaAccount"),
412 "kolabFreeBusyFuture" => _("Free busy future"),
413 "unrestrictedMailSize" => _("Mail size restriction"),
414 "calFBURL" => _("Free busy information"),
415 "kolabDelegate" => _("Delegations")));
416 }
417 }
419 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
420 ?>