PostfixAdmin - web based virtual user administration interface for Postfix mail servers https://postfixadmin.github.io/postfixadmin/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

128 lines
4.9 KiB

  1. <?php
  2. class VacationHandler {
  3. protected $username = null;
  4. function __construct($username) {
  5. $this->username = $username;
  6. }
  7. /**
  8. * Removes the autoreply alias etc for this user; namely, if they're away we remove their vacation alias and
  9. * set the vacation table record to false.
  10. * @return boolean true on success.
  11. */
  12. function remove() {
  13. $ah = new AliasHandler($this->username);
  14. $aliases = $ah->get(true); // fetch all.
  15. $new_aliases = array();
  16. $table_vacation = table_by_key('vacation');
  17. $table_vacation_notification = table_by_key('vacation_notification');
  18. /* go through the user's aliases and remove any that look like a vacation address */
  19. foreach($aliases as $alias) {
  20. if(!$ah->is_vacation_address($alias)) {
  21. $new_aliases[] = $alias;
  22. }
  23. }
  24. $ah->update($new_aliases, '', false);
  25. // tidy up vacation table.
  26. $active = db_get_boolean(False);
  27. $username = escape_string($this->username);
  28. $result = db_query("UPDATE $table_vacation SET active = '$active' WHERE email='$username'");
  29. $result = db_query("DELETE FROM $table_vacation_notification WHERE on_vacation='$username'");
  30. /* crap error handling; oh for exceptions... */
  31. return true;
  32. }
  33. /**
  34. * @return boolean true indicates this server supports vacation messages, and users are able to change their own.
  35. * @global array $CONF
  36. */
  37. function vacation_supported() {
  38. global $CONF;
  39. return $CONF['vacation'] == 'YES' && $CONF['vacation_control'] == 'YES';
  40. }
  41. /**
  42. * @return boolean true if on vacation, otherwise false
  43. * Why do we bother storing true/false in the vacation table if the alias dictates it anyway?
  44. */
  45. function check_vacation() {
  46. $ah = new AliasHandler($this->username);
  47. $aliases = $ah->get(true); // fetch all.
  48. foreach($aliases as $alias) {
  49. if($ah->is_vacation_address($alias)) {
  50. return true;
  51. }
  52. }
  53. return false;
  54. }
  55. /**
  56. * Retrieve information on someone who is on vacation
  57. * @return struct|boolean stored information on vacation - array(subject - string, message - string, active - boolean)
  58. * will return false if no existing data
  59. */
  60. function get_details() {
  61. $table_vacation = table_by_key('vacation');
  62. $username = escape_string($this->username);
  63. $sql = "SELECT * FROM $table_vacation WHERE email = '$username'";
  64. $result = db_query($sql);
  65. if($result['rows'] == 1) {
  66. $row = db_array($result['result']);
  67. $boolean = ($row['active'] == db_get_boolean(true));
  68. return array( 'subject' => $row['subject'],
  69. 'body' => $row['body'],
  70. 'active' => $boolean );
  71. }
  72. return false;
  73. }
  74. /**
  75. * @param string $subject
  76. * @param string $body
  77. */
  78. function set_away($subject, $body) {
  79. $this->remove(); // clean out any notifications that might already have been sent.
  80. // is there an entry in the vacaton table for the user, or do we need to insert?
  81. $table_vacation = table_by_key('vacation');
  82. $username = escape_string($this->username);
  83. $body = escape_string($body);
  84. $subject = escape_string($subject);
  85. $result = db_query("SELECT * FROM $table_vacation WHERE email = '$username'");
  86. $active = db_get_boolean(True);
  87. // check if the user has a vacation entry already, if so just update it
  88. if($result['rows'] == 1) {
  89. $result = db_query("UPDATE $table_vacation SET active = '$active', body = '$body', subject = '$subject', created = NOW() WHERE email = '$username'");
  90. }
  91. else {
  92. $tmp = preg_split ('/@/', $username);
  93. $domain = escape_string($tmp[1]);
  94. $result = db_query ("INSERT INTO $table_vacation (email,subject,body,domain,created,active) VALUES ('$username','$subject','$body','$domain',NOW(),'$active')");
  95. }
  96. $ah = new AliasHandler($this->username);
  97. $aliases = $ah->get(true);
  98. $vacation_address = $this->getVacationAlias();
  99. $aliases[] = $vacation_address;
  100. return $ah->update($aliases, '', false);
  101. }
  102. /**
  103. * Returns the vacation alias for this user.
  104. * i.e. if this user's username was roger@example.com, and the autoreply domain was set to
  105. * autoreply.fish.net in config.inc.php we'd return roger#example.com@autoreply.fish.net
  106. * @return string an email alias.
  107. */
  108. public function getVacationAlias() {
  109. global $CONF;
  110. $vacation_domain = $CONF['vacation_domain'];
  111. $vacation_goto = preg_replace('/@/', '#', $this->username);
  112. $vacation_goto = "{$vacation_goto}@{$vacation_domain}";
  113. return $vacation_goto;
  114. }
  115. }
  116. /* vim: set expandtab softtabstop=4 tabstop=4 shiftwidth=4: */