f9ddf7c08288d058c516112890b22459cf1180f6
1 <?php
3 class groupware extends plugin
4 {
5 var $plHeadline = "Mail";
6 var $plDescription = "GOsa mail extension.";
7 var $view_logged = FALSE;
9 var $attributes = array(
10 "mailAddress",
11 "mailLocation",
12 "quotaUsage",
13 "quotaSize",
14 "alternateAddresses",
15 "forwardingAddresses",
16 "vacationEnabled",
17 "vacationStart",
18 "vacationStop",
19 "vacationMessage",
20 "mailBoxWarnLimitEnabled",
21 "mailBoxWarnLimitValue",
22 "mailBoxSendSizelimitEnabled",
23 "mailBoxSendSizelimitValue",
24 "mailBoxHardSizelimitEnabled",
25 "mailBoxHardSizelimitValue",
26 "mailBoxAutomaticRemovalEnabled",
27 "mailBoxAutomaticRemovalValue",
28 );
30 var $mailAddressSelectDialog = NULL;
31 var $vacationTemplates = array();
33 var $mailAddress = "";
34 var $mailLocation = "";
35 var $quotaUsage = "";
36 var $quotaSize = "";
37 var $alternateAddresses = "";
38 var $forwardingAddresses = "";
39 var $vacationEnabled = FALSE;
40 var $vacationStart = 0;
41 var $vacationStop = 0;
42 var $vacationMessage = "";
43 var $mailBoxWarnLimitEnabled = FALSE;
44 var $mailBoxWarnLimitValue = "";
45 var $mailBoxSendSizelimitEnabled = FALSE;
46 var $mailBoxSendSizelimitValue = "";
47 var $mailBoxHardSizelimitEnabled = FALSE;
48 var $mailBoxHardSizelimitValue = "";
49 var $mailBoxAutomaticRemovalEnabled = FALSE;
50 var $mailBoxAutomaticRemovalValue = "";
52 function __construct ($config, $dn= NULL)
53 {
54 plugin::plugin($config,$dn);
56 // Get attributes from parent object
57 foreach(array("uid","cn") as $attr){
58 if(isset($this->parent->by_object['group']) && isset($this->parent->by_object['group']->$attr)){
59 $this->$attr = &$this->parent->by_object['group']->$attr;
60 }elseif(isset($this->attrs[$attr])){
61 $this->$attr = $this->attrs[$attr][0];
62 }
63 }
65 // Set vacation start/stop if not set alreasy
66 $this->vacationStart = time();
67 $this->vacationStop = time() + (14 * 60*60*24);
69 // Prepare vacation start/stop time to be initially valid.
70 $this->vacationStart= date('d.m.Y', $this->vacationStart);
71 $this->vacationStop= date('d.m.Y', $this->vacationStop);
72 }
75 function execute()
76 {
78 // Register plugin execution
79 $display = plugin::execute();
81 // Log plugin execution.
82 if($this->is_account && !$this->view_logged){
83 $this->view_logged = TRUE;
84 new log("view","users/".get_class($this),$this->dn);
85 }
88 /****************
89 Account status
90 ****************/
92 if(isset($_POST['modify_state'])){
93 if($this->is_account && $this->acl_is_removeable() && $this->mailMethod->accountRemoveAble()){
94 $this->is_account= FALSE;
95 }elseif(!$this->is_account && $this->acl_is_createable() && $this->mailMethod->accountCreateable()){
96 $this->is_account= TRUE;
97 }
98 }
99 if(!$this->multiple_support_active){
100 if (!$this->is_account && $this->parent === NULL){
101 $display= "<img alt=\"\" src=\"images/small-error.png\" align=\"middle\"> <b>".
102 msgPool::noValidExtension(_("Mail"))."</b>";
103 $display.= back_to_main();
104 return ($display);
105 }
106 if ($this->parent !== NULL){
107 if ($this->is_account){
108 $display= $this->show_disable_header(msgPool::removeFeaturesButton(_("Mail")),msgPool::featuresEnabled(_("Mail")));
109 } else {
110 $display= $this->show_enable_header(msgPool::addFeaturesButton(_("Mail")),msgPool::featuresDisabled(_("Mail")));
111 return ($display);
112 }
113 }
114 }
116 /****************
117 Forward addresses
118 ****************/
120 // Display dialog to select a local fowarder
121 if (isset($_POST['addLocalForwardingAddress'])){
122 $this->mailAddressSelectDialog= new mailAddressSelect($this->config, get_userinfo());
123 $this->dialog= TRUE;
124 }
126 // Close dialogs, action was canceled
127 if (isset($_POST['mailAddressSelect_cancel'])){
128 $this->mailAddressSelectDialog= FALSE;
129 $this->dialog= FALSE;
130 }
132 // Append selected forwarding addresses now.
133 if (isset($_POST['mailAddressSelect_save']) && $this->mailAddressSelectDialog instanceOf mailAddressSelect){
134 if($this->acl_is_writeable("gosaMailForwardingAddress")){
135 $list = $this->mailAddressSelectDialog->save();
136 foreach ($list as $entry){
137 $val = $entry['mail'][0];
138 if (!in_array ($val, $this->gosaMailAlternateAddress) && $val != $this->mail){
139 $this->addForwarder($val);
140 $this->is_modified= TRUE;
141 }
142 }
143 $this->mailAddressSelectDialog= FALSE;
144 $this->dialog= FALSE;
145 } else {
146 msg_dialog::display(_("Error"), _("Please select an entry!"), ERROR_DIALOG);
147 }
148 }
150 // Display the address selection dialog.
151 if($this->mailAddressSelectDialog instanceOf mailAddressSelect){
152 $used = array();
153 $used['mail'] = array_values($this->gosaMailAlternateAddress);
154 $used['mail'] = array_merge($used['mail'], array_values($this->gosaMailForwardingAddress));
155 $used['mail'][] = $this->mail;
157 // Build up blocklist
158 session::set('filterBlacklist', $used);
159 return($this->mailAddressSelectDialog->execute());
160 }
162 // Add manually inserted forwarding address.
163 if (isset($_POST['addForwardingAddress'])){
164 if ($_POST['forwardingAddressInput'] != ""){
165 $address= get_post('forwardingAddressInput');
166 $valid= FALSE;
167 if (!tests::is_email($address)){
168 if (!tests::is_email($address, TRUE)){
169 if ($this->is_template){
170 $valid= TRUE;
171 } else {
172 msg_dialog::display(_("Error"), msgPool::invalid(_("Mail address"),
173 "","","your-address@your-domain.com"),ERROR_DIALOG);
174 }
175 }
176 } elseif ($address == $this->mail
177 || in_array($address, $this->gosaMailAlternateAddress)) {
178 msg_dialog::display(_("Error"),_("Cannot add primary address to the list of forwarders!") , ERROR_DIALOG);
179 } else {
180 $valid= TRUE;
181 }
182 if ($valid){
183 if($this->acl_is_writeable("gosaMailForwardingAddress")){
184 $this->addForwarder ($address);
185 $this->is_modified= TRUE;
186 }
187 }
188 }
189 }
190 if (isset($_POST['deleteForwardingAddress'])){
191 $this->delForwarder ($_POST['forwardingAddressList']);
192 }
195 /****************
196 Alternate addresses
197 ****************/
199 // Add manually inserted alternate mail address.
200 if (isset($_POST['addAlternateAddress'])){
201 $valid= FALSE;
202 if (!tests::is_email($_POST['alternateAddressInput'])){
203 if ($this->is_template){
204 if (!(tests::is_email($_POST['alternateAddressInput'], TRUE))){
205 msg_dialog::display(_("Error"),msgPool::invalid(_("Mail address"),
206 "","","your-domain@your-domain.com"),ERROR_DIALOG);
207 } else {
208 $valid= TRUE;
209 }
210 } else {
211 msg_dialog::display(_("Error"),msgPool::invalid(_("Mail address"),
212 "","","your-domain@your-domain.com"),ERROR_DIALOG);
213 }
214 } else {
215 $valid= TRUE;
216 }
217 if ($valid && ($user= $this->addAlternate (get_post('alternateAddressInput'))) != ""){
218 $ui= get_userinfo();
219 $addon= "";
220 if ($user[0] == "!") {
221 $addon= sprintf(_("Address is already in use by group '%s'."), mb_substr($user, 1));
222 } else {
223 $addon= sprintf(_("Address is already in use by user '%s'."), $user);
224 }
225 msg_dialog::display(_("Error"), msgPool::duplicated(_("Mail address"))."<br><br><i>".
226 "$addon</i>", ERROR_DIALOG);
227 }
228 }
230 // Remove alternate mail address.
231 if (isset($_POST['deleteAlternateAddress']) && isset($_POST['alternateAddressList'])){
232 $this->delAlternate ($_POST['alternateAddressList']);
233 }
236 /****************
237 SMARTY- Assign smarty variables
238 ****************/
240 $smarty = get_smarty();
241 foreach($this->attributes as $attr){
242 $smarty->assign($attr, $this->$attr);
243 }
245 /****************
246 SMARTY- Assign flags
247 ****************/
249 /****************
250 Smarty- Vacation settings
251 ****************/
253 $smarty->assign("mailLocations", array("tester"));
254 if (count($this->vacationTemplates)){
255 $smarty->assign("displayTemplateSelector", "true");
256 $smarty->assign("vacationTemplate", set_post($this->vacationTemplate));
257 $smarty->assign("vacationTemplates", set_post($this->vacationTemplates));
258 $smarty->assign("template", set_post(get_post('vacation_template')));
259 } else {
260 $smarty->assign("displayTemplateSelector", "false");
261 }
263 return($display.$smarty->fetch(get_template_path("generic.tpl",TRUE,dirname(__FILE__))));
264 }
268 /* Save data to object */
269 function save_object()
270 {
271 }
274 /*! \brief Parse vacation templates and build up an array
275 containing 'filename' => 'description'.
276 Used to fill vacation dropdown box.
277 @return Array All useable vacation templates.
278 */
279 function get_vacation_templates()
280 {
281 $vct = array();
282 if ($this->config->get_cfg_value("core","vacationTemplateDirectory") != ""){
283 $dir= $this->config->get_cfg_value("core","vacationTemplateDirectory");
284 if (is_dir($dir) && is_readable($dir)){
285 $dh = opendir($dir);
286 while($file = readdir($dh)){
287 $description= "";
288 if (is_file($dir."/".$file)){
289 $fh = fopen($dir."/".$file, "r");
290 $line= fgets($fh, 256);
291 if (!preg_match('/^DESC:/', $line)){
292 msg_dialog::display(_("Configuration error"), sprintf(_("No DESC tag in vacation template '%s'!"), $file), ERROR_DIALOG);
293 }else{
294 $description= trim(preg_replace('/^DESC:\s*/', '', $line));
295 }
296 fclose ($fh);
297 }
298 if ($description != ""){
299 $vct["$dir/$file"]= $description;
300 }
301 }
302 closedir($dh);
303 }
304 }
305 return($vct);
306 }
309 /*! \brief Adds the given mail address to the list of mail forwarders
310 */
311 function addForwarder($address)
312 {
313 if(empty($address)) return;
314 if($this->acl_is_writeable("gosaMailForwardingAddress")){
315 $this->gosaMailForwardingAddress[]= $address;
316 $this->gosaMailForwardingAddress= array_unique ($this->gosaMailForwardingAddress);
317 sort ($this->gosaMailForwardingAddress);
318 reset ($this->gosaMailForwardingAddress);
319 $this->is_modified= TRUE;
320 }else{
321 msg_dialog::display(_("Permission error"), _("You have no permission to modify these addresses!"), ERROR_DIALOG);
322 }
323 }
326 /*! \brief Removes the given mail address from the list of mail forwarders
327 */
328 function delForwarder($addresses)
329 {
330 if($this->acl_is_writeable("gosaMailForwardingAddress")){
331 $this->gosaMailForwardingAddress= array_remove_entries ($addresses, $this->gosaMailForwardingAddress);
332 $this->is_modified= TRUE;
333 }else{
334 msg_dialog::display(_("Permission error"), _("You have no permission to modify these addresses!"), ERROR_DIALOG);
335 }
336 }
339 /*! \brief Add given mail address to the list of alternate adresses ,
340 . check if this mal address is used, skip adding in this case
341 */
342 function addAlternate($address)
343 {
344 if(empty($address)) return;
345 if($this->acl_is_writeable("gosaMailAlternateAddress")){
346 $ldap= $this->config->get_ldap_link();
347 $address= strtolower($address);
349 /* Is this address already assigned in LDAP? */
350 $ldap->cd ($this->config->current['BASE']);
351 $ldap->search ("(&(!(objectClass=gosaUserTemplate))(objectClass=gosaMailAccount)(|(mail=$address)".
352 "(alias=$address)(gosaMailAlternateAddress=$address)))", array("uid", "cn"));
353 if ($ldap->count() > 0){
354 $attrs= $ldap->fetch ();
355 if (!isset($attrs["uid"])) {
356 return ("!".$attrs["cn"][0]);
357 }
358 return ($attrs["uid"][0]);
359 }
360 if (!in_array($address, $this->gosaMailAlternateAddress)){
361 $this->gosaMailAlternateAddress[]= $address;
362 $this->is_modified= TRUE;
363 }
364 sort ($this->gosaMailAlternateAddress);
365 reset ($this->gosaMailAlternateAddress);
366 return ("");
367 }else{
368 msg_dialog::display(_("Permission error"), _("You have no permission to modify these addresses!"), ERROR_DIALOG);
369 }
370 }
373 /*! \brief Removes the given mail address from the alternate addresses list
374 */
375 function delAlternate($addresses)
376 {
377 if($this->acl_is_writeable("gosaMailAlternateAddress")){
378 $this->gosaMailAlternateAddress= array_remove_entries ($addresses,$this->gosaMailAlternateAddress);
379 $this->is_modified= TRUE;
380 }else{
381 msg_dialog::display(_("Permission error"), _("You have no permission to modify these addresses!"), ERROR_DIALOG);
382 }
383 }
386 /*! \brief Prepare importet vacation string. \
387 . Replace placeholder like %givenName a.s.o.
388 @param string Vacation string
389 @return string Completed vacation string
390 */
391 private function prepare_vacation_template($contents)
392 {
393 /* Replace attributes */
394 $attrs = array();
395 $obj = NULL;
396 if(isset($this->parent->by_object['user'])){
397 $attrs = $this->parent->by_object['user']->attributes;
398 $obj = $this->parent->by_object['user'];
399 }else{
400 $obj = new user($this->config,$this->dn);
401 $attrs = $obj->attributes;
402 }
403 if($obj){
405 /* Replace vacation start and end time */
406 if($this->mailMethod->vacationRangeEnabled()){
407 if(preg_match("/%start/",$contents)){
408 $contents = preg_replace("/%start/",$this->vacationStart,$contents);
409 }
410 if(preg_match("/%end/",$contents)){
411 $contents = preg_replace("/%end/",$this->vacationStop,$contents);
412 }
413 }else{
414 if(preg_match("/%start/",$contents)){
415 $contents = preg_replace("/%start/", _("unknown"),$contents);
416 }
417 if(preg_match("/%end/",$contents)){
418 $contents = preg_replace("/%end/", _("unknown"), $contents);
419 }
420 }
422 foreach ($attrs as $val){
424 // We can only replace strings here
425 if(!is_string($obj->$val)) continue;
427 if(preg_match("/dateOfBirth/",$val)){
428 if($obj->use_dob){
429 $contents= preg_replace("/%$val/",date("Y-d-m",$obj->dateOfBirth),$contents);
430 }
431 }else {
432 $contents= preg_replace("/%$val/",
433 $obj->$val, $contents);
434 }
436 }
437 }
438 $contents = ltrim(preg_replace("/^DESC:.*$/m","",$contents),"\n ");
439 return($contents);
440 }
443 function remove_from_parent()
444 {
445 /* Cancel if there's nothing to do here */
446 if (!$this->initially_was_account){
447 return;
448 }
450 }
453 function save()
454 {
455 }
458 /*! \brief Check given values
459 */
460 function check()
461 {
462 if(!$this->is_account){
463 return(array());
464 }
466 $ldap= $this->config->get_ldap_link();
468 /* Call common method to give check the hook */
469 $message= plugin::check();
471 if(empty($this->gosaMailServer)){
472 $message[]= msgPool::noserver(_("Mail"));
473 }
475 /* Mail address checks */
476 $mail = $this->mail;
477 if(!(!$this->mailMethod->isModifyableMail() && $this->initially_was_account)){
479 if($this->mailMethod->domainSelectionEnabled()){
480 $mail.= "@".$this->mailDomainPart;
481 }
483 if (empty($mail)){
484 $message[]= msgPool::required(_("Primary address"));
485 }
486 if ($this->is_template){
487 if (!tests::is_email($mail, TRUE)){
488 $message[]= msgPool::invalid(_("Mail address"),"","","%givenName.%sn@your-domain.com");
489 }
490 } else {
491 if (!tests::is_email($mail)){
492 $message[]= msgPool::invalid(_("Mail address"),"","","your-address@your-domain.com");
493 }
494 }
496 /* Check if this mail address is already in use */
497 $ldap->cd($this->config->current['BASE']);
498 $filter = "(&(!(objectClass=gosaUserTemplate))(!(uid=".$this->uid."))".
499 "(objectClass=gosaMailAccount)".
500 "(|(mail=".$mail.")(alias=".$mail.")(gosaMailAlternateAddress=".$mail.")))";
501 $ldap->search($filter,array("uid", "cn"));
502 if ($ldap->count() != 0){
503 $entry= $ldap->fetch();
504 $addon= "";
505 if (!isset($entry['uid'])) {
506 $addon= sprintf(_("Address is already in use by group '%s'."), $entry['cn'][0]);
507 } else {
508 $addon= sprintf(_("Address is already in use by user '%s'."), $entry['uid'][0]);
509 }
510 $message[]= msgPool::duplicated(_("Mail address"))."<br><br><i>$addon</i>";
511 }
512 }
515 /* Check quota */
516 if ($this->gosaMailQuota != '' && $this->acl_is_writeable("gosaMailQuota")){
517 if (!is_numeric($this->gosaMailQuota)) {
518 $message[]= msgPool::invalid(_("Quota size"),$this->gosaMailQuota,"/^[0-9]*/");
519 } else {
520 $this->gosaMailQuota= (int) $this->gosaMailQuota;
521 }
522 }
524 /* Check rejectsize for integer */
525 if ($this->gosaMailMaxSize != '' && $this->acl_is_writeable("gosaMailMaxSize")){
526 if (!is_numeric($this->gosaMailMaxSize)){
527 $message[]= msgPool::invalid(_("Mail reject size"),$this->gosaMailMaxSize,"/^[0-9]*/");
528 } else {
529 $this->gosaMailMaxSize= (int) $this->gosaMailMaxSize;
530 }
531 }
533 /* Need gosaMailMaxSize if use_mailsize_limit is checked */
534 if (is_integer(strpos($this->gosaMailDeliveryMode, "R")) && $this->gosaMailMaxSize == ""){
535 $message[]= msgPool::required(_("Mail reject size"));
536 }
538 if((preg_match("/S/", $this->gosaMailDeliveryMode))&&(empty($this->gosaSpamMailbox))) {
539 $message[]= msgPool::required(_("Spam folder"));
540 }
542 if ($this->mailMethod->vacationRangeEnabled() && preg_match('/V/', $this->gosaMailDeliveryMode)){
544 /* Check date strings */
545 $state= true;
546 if ($this->vacationStart == "" || !tests::is_date($this->vacationStart)) {
547 $message[]= msgPool::invalid(_("from"),$this->vacationStart);
548 $state= false;
549 }
550 if ($this->vacationStart == "" || !tests::is_date($this->vacationStop)) {
551 $message[]= msgPool::invalid(_("to"),$this->vacationStop);
552 $state= false;
553 }
555 #TODO: take care of date format
556 if ($state) {
557 list($day, $month, $year)= explode('.', $this->vacationStart);
558 $start= mktime(0,0,0,$month, $day, $year);
559 list($day, $month, $year)= explode('.', $this->vacationStop);
560 $stop= mktime(0,0,0,$month, $day, $year);
561 if($start > $stop){
562 $message[]= msgPool::invalid(_("Vacation interval"));
563 }
564 }
565 }
566 return($message);
567 }
570 /*! \brief Adapt from template, using 'dn'
571 */
572 function adapt_from_template($dn, $skip= array())
573 {
574 plugin::adapt_from_template($dn, $skip);
576 }
579 /*! \brief ACL settings
580 */
581 static function plInfo()
582 {
583 return (array(
584 "plShortName" => _("Mail"),
585 "plDescription" => _("Mail settings"),
586 "plSelfModify" => TRUE,
587 "plDepends" => array("user"), // This plugin depends on
588 "plPriority" => 4, // Position in tabs
589 "plSection" => array("personal" => _("My account")),
590 "plCategory" => array("users"),
591 "plOptions" => array(),
592 "plProvidedAcls" => array()
593 ));
594 }
595 }
597 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
598 ?>