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.

189 lines
6.5 KiB

  1. <?php
  2. /**
  3. * Handlers User level alias actions - e.g. add alias, get aliases, update etc.
  4. */
  5. class AliasHandler {
  6. private $username = null;
  7. /**
  8. * @param string $username
  9. */
  10. public function __construct($username) {
  11. $this->username = $username;
  12. }
  13. /**
  14. * @return array - list of email addresses the user's mail is forwarded to.
  15. * (may be an empty list, especially if $CONF['alias_control'] is turned off...
  16. * @param boolean - by default we don't return special addresses (e.g. vacation and mailbox alias); pass in true here if you wish to.
  17. */
  18. public function get($all=false) {
  19. $username = escape_string($this->username);
  20. $table_alias = table_by_key('alias');
  21. $sql = "SELECT * FROM $table_alias WHERE address='$username'";
  22. $result = db_query($sql);
  23. if($result['rows'] == 1) {
  24. $row = db_array ($result['result']);
  25. // At the moment Postfixadmin stores aliases in it's database in a comma seperated list; this may change one day.
  26. $list = explode(',', $row['goto']);
  27. if($all) {
  28. return $list;
  29. }
  30. $new_list = array();
  31. /* if !$all, remove vacation & mailbox aliases */
  32. foreach($list as $address) {
  33. if($address != '' ) {
  34. if($this->is_vacation_address($address) || $this->is_mailbox_alias($address)) {
  35. }
  36. else {
  37. $new_list[] = $address;
  38. }
  39. }
  40. }
  41. $list = $new_list;
  42. return $list;
  43. }
  44. return array();
  45. }
  46. /**
  47. * @param string $address
  48. * @param string $username
  49. * @return boolean true if the username is an alias for the mailbox AND we have alias_control turned off.
  50. */
  51. public function is_mailbox_alias($address) {
  52. global $CONF;
  53. $username = $this->username;
  54. if($address == $username) {
  55. return true;
  56. }
  57. return false;
  58. }
  59. /**
  60. * @param string $address
  61. * @return boolean true if the address contains the vacation domain
  62. */
  63. public function is_vacation_address($address) {
  64. global $CONF;
  65. if($CONF['vacation'] == 'YES') {
  66. if(stripos($address, '@' . $CONF['vacation_domain'])) {
  67. return true;
  68. }
  69. }
  70. return false;
  71. }
  72. /**
  73. * @return boolean true on success
  74. * @param string $username
  75. * @param array $addresses - list of aliases to set for the user.
  76. * @param string flags - forward_and_store or remote_only or ''
  77. * @param boolean $vacation_persist - set to false to stop the vacation address persisting across updates
  78. * Set the user's aliases to those provided. If $addresses ends up being empty the alias record is removed.
  79. */
  80. public function update($addresses, $flags = '', $vacation_persist=true) {
  81. // find out if the user is on vacation or not; if they are,
  82. // then the vacation alias needs adding to the db (as we strip it out in the get method)
  83. // likewise with the alias_control address.
  84. $valid_flags = array('', 'forward_and_store', 'remote_only');
  85. if(!in_array($flags, $valid_flags)) {
  86. die("Invalid flag passed into update()... : $flag - valid options are :" . implode(',', $valid_flags));
  87. }
  88. $addresses = array_unique($addresses);
  89. $original = $this->get(true);
  90. $tmp = preg_split('/@/', $this->username);
  91. $domain = $tmp[1];
  92. foreach($original as $address) {
  93. if($vacation_persist) {
  94. if($this->is_vacation_address($address)) {
  95. $addresses[] = $address;
  96. }
  97. }
  98. if($flags != 'remote_only') {
  99. if($this->is_mailbox_alias($address)) {
  100. $addresses[] = $address;
  101. }
  102. }
  103. }
  104. $addresses = array_unique($addresses);
  105. $new_list = array();
  106. if($flags == 'remote_only') {
  107. foreach($addresses as $address) {
  108. // strip out our username... if it's in the list given.
  109. if($address != $this->username) {
  110. $new_list[] = $address;
  111. }
  112. }
  113. $addresses = $new_list;
  114. }
  115. if($flags == 'forward_and_store') {
  116. if(!in_array($this->username, $addresses)) {
  117. $addresses[] = $this->username;
  118. }
  119. }
  120. $new_list = array();
  121. foreach($addresses as $address) {
  122. if($address != '') {
  123. $new_list[] = $address;
  124. }
  125. }
  126. $addresses = array_unique($new_list);
  127. $username = escape_string($this->username);
  128. $goto = escape_string(implode(',', $addresses));
  129. $table_alias = table_by_key('alias');
  130. if(sizeof($addresses) == 0) {
  131. $sql = "DELETE FROM $table_alias WHERE address = '$username'";
  132. }
  133. if($this->hasAliasRecord() == false) {
  134. $true = db_get_boolean(True);
  135. $sql = "INSERT INTO $table_alias (address, goto, domain, created, modified, active) VALUES ('$username', '$goto', '$domain', NOW(), NOW(), '$true')";
  136. }
  137. else {
  138. $sql = "UPDATE $table_alias SET goto = '$goto', modified = NOW() WHERE address = '$username'";
  139. }
  140. $result = db_query($sql);
  141. if($result['rows'] != 1) {
  142. return false;
  143. }
  144. db_log($username, $domain, 'edit_alias', "$username -> $goto");
  145. return true;
  146. }
  147. /**
  148. * Determine whether a local delivery address is present. This is
  149. * stores as an alias with the same name as the mailbox name (username)
  150. * @return boolean true if local delivery is enabled
  151. */
  152. public function hasStoreAndForward() {
  153. $aliases = $this->get(true);
  154. if(in_array($this->username, $aliases)) {
  155. return true;
  156. }
  157. return false;
  158. }
  159. /**
  160. * @return boolean true if the user has an alias record (i.e row in alias table); else false.
  161. */
  162. public function hasAliasRecord() {
  163. $username = escape_string($this->username);
  164. $table_alias = table_by_key('alias');
  165. $sql = "SELECT * FROM $table_alias WHERE address = '$username'";
  166. $result = db_query($sql);
  167. if($result['rows'] == 1) {
  168. return true;
  169. }
  170. return false;
  171. }
  172. }
  173. /* vim: set expandtab softtabstop=4 tabstop=4 shiftwidth=4: */