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.

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