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.

242 lines
8.6 KiB

  1. <?php
  2. # $Id$
  3. /**
  4. * Handlers User level alias actions - e.g. add alias, get aliases, update etc.
  5. */
  6. class AliasHandler {
  7. private $username = null;
  8. /**
  9. * @param string $username
  10. */
  11. public function __construct($username) {
  12. $this->username = strtolower($username);
  13. }
  14. /**
  15. * @return array - list of email addresses the user's mail is forwarded to.
  16. * (may be an empty list, especially if $CONF['alias_control'] is turned off...)
  17. * @param boolean - by default we don't return special addresses (e.g. vacation and mailbox alias); pass in true here if you wish to.
  18. */
  19. public function get($all=false) {
  20. $username = escape_string($this->username);
  21. $table_alias = table_by_key('alias');
  22. $sql = "SELECT * FROM $table_alias WHERE address='$username'";
  23. $result = db_query($sql);
  24. if($result['rows'] != 1) {
  25. return false;
  26. }
  27. $row = db_array ($result['result']);
  28. // At the moment Postfixadmin stores aliases in it's database in a comma seperated list; this may change one day.
  29. $list = explode(',', $row['goto']);
  30. if($all) {
  31. $this->return = $list;
  32. return true;
  33. }
  34. $filtered_list = array();
  35. /* if !$all, remove vacation & mailbox aliases */
  36. foreach($list as $address) {
  37. if($address != '' ) {
  38. if($this->is_vacation_address($address) || $this->is_mailbox_alias($address)) {
  39. # TODO: store "vacation_active" and "mailbox" status - should be readable public
  40. }
  41. else {
  42. $filtered_list[] = $address;
  43. }
  44. }
  45. }
  46. $this->return = $filtered_list;
  47. return true;
  48. }
  49. /**
  50. * @param string $address
  51. * @param string $username
  52. * @return boolean true if the username is an alias for the mailbox AND we have alias_control turned off.
  53. * TODO: comment for @return: does alias_control really matter here?
  54. */
  55. public function is_mailbox_alias($address) {
  56. global $CONF;
  57. if($address != $this->username) { # avoid false positives if $address is a mailbox
  58. return false;
  59. }
  60. $table_mailbox = table_by_key('mailbox');
  61. $E_address = escape_string($address);
  62. $sql = "SELECT * FROM $table_mailbox WHERE username='$E_address'";
  63. $result = db_query($sql);
  64. if($result['rows'] != 1) {
  65. return false;
  66. } else {
  67. return true;
  68. }
  69. }
  70. /**
  71. * @param string $address
  72. * @return boolean true if the address contains the vacation domain
  73. */
  74. public function is_vacation_address($address) {
  75. global $CONF;
  76. if($CONF['vacation'] == 'YES') {
  77. if(stripos($address, '@' . $CONF['vacation_domain'])) { # TODO: check full vacation address user#domain.com@vacation_domain
  78. return true;
  79. }
  80. }
  81. return false;
  82. }
  83. /**
  84. * @return boolean true on success
  85. * @param string $username
  86. * @param array $addresses - list of aliases to set for the user.
  87. * @param string flags - forward_and_store or remote_only or ''
  88. * @param boolean $vacation_persist - set to false to stop the vacation address persisting across updates
  89. * Set the user's aliases to those provided. If $addresses ends up being empty the alias record is removed. # TODO: deleting that's buggy behaviour, error out instead
  90. */
  91. public function update($addresses, $flags = '', $vacation_persist=true) {
  92. // find out if the user is on vacation or not; if they are,
  93. // then the vacation alias needs adding to the db (as we strip it out in the get method)
  94. // likewise with the alias_control address.
  95. # TODO: move all validation from edit-alias/create-alias and users/edit-alias here
  96. $valid_flags = array('', 'forward_and_store', 'remote_only');
  97. if(!in_array($flags, $valid_flags)) {
  98. die("Invalid flag passed into update()... : $flag - valid options are :" . implode(',', $valid_flags));
  99. }
  100. $addresses = array_unique($addresses);
  101. list (/*NULL*/, $domain) = explode('@', $this->username);
  102. if ( ! $this->get(true) ) die("Alias not existing?"); # TODO: better error behaviour
  103. foreach($this->return as $address) {
  104. if($vacation_persist) {
  105. if($this->is_vacation_address($address)) {
  106. $addresses[] = $address;
  107. }
  108. }
  109. if($flags != 'remote_only') {
  110. if($this->is_mailbox_alias($address)) {
  111. $addresses[] = $address;
  112. }
  113. }
  114. }
  115. $addresses = array_unique($addresses);
  116. $new_list = array();
  117. if($flags == 'remote_only') {
  118. foreach($addresses as $address) { # TODO: write a remove_from_array function, see http://tech.petegraham.co.uk/2007/03/22/php-remove-values-from-array/
  119. // strip out our username... if it's in the list given.
  120. if($address != $this->username) {
  121. $new_list[] = $address;
  122. }
  123. }
  124. $addresses = $new_list;
  125. }
  126. if($flags == 'forward_and_store') {
  127. if(!in_array($this->username, $addresses)) {
  128. $addresses[] = $this->username;
  129. }
  130. }
  131. $new_list = array();
  132. foreach($addresses as $address) {
  133. if($address != '') {
  134. $new_list[] = $address; # TODO use remove_from_array, see above
  135. }
  136. }
  137. $addresses = array_unique($new_list);
  138. $E_username = escape_string($this->username);
  139. $goto = implode(',', $addresses);
  140. if(sizeof($addresses) == 0) {
  141. # $result = db_delete('alias', 'address', $this->username); # '"DELETE FROM $table_alias WHERE address = '$username'"; # TODO: should never happen and causes broken behaviour
  142. error_log("Alias set to empty / Attemp to delete: " . $this->username); # TODO: more/better error handling - maybe just return false?
  143. }
  144. if($this->hasAliasRecord() == false) { # TODO should never happen in update() - see also the comments on handling DELETE above
  145. $alias_data = array(
  146. 'address' => $this->username,
  147. 'goto' => $goto,
  148. 'domain' => $domain,
  149. 'active' => db_get_boolean(True),
  150. );
  151. $result = db_insert('alias', $alias_data);
  152. } else {
  153. $alias_data = array(
  154. 'goto' => $goto,
  155. );
  156. $result = db_update('alias', 'address', $this->username, $alias_data);
  157. }
  158. if($result != 1) {
  159. return false;
  160. }
  161. db_log ($domain, 'edit_alias', "$E_username -> $goto");
  162. return true;
  163. }
  164. /**
  165. * Determine whether a local delivery address is present. This is
  166. * stores as an alias with the same name as the mailbox name (username)
  167. * @return boolean true if local delivery is enabled
  168. */
  169. public function hasStoreAndForward() {
  170. $result = $this->get(true); # TODO: error checking?
  171. if(in_array($this->username, $this->return)) {
  172. return true;
  173. }
  174. return false;
  175. }
  176. /**
  177. * @return boolean true if the user has an alias record (i.e row in alias table); else false.
  178. */
  179. public function hasAliasRecord() {
  180. $username = escape_string($this->username);
  181. $table_alias = table_by_key('alias');
  182. $sql = "SELECT * FROM $table_alias WHERE address = '$username'";
  183. $result = db_query($sql);
  184. if($result['rows'] == 1) {
  185. return true;
  186. }
  187. return false;
  188. }
  189. /**
  190. * @return true on success false on failure
  191. */
  192. public function delete(){
  193. if( ! $this->get() ) {
  194. $this->errormsg[] = 'An alias with that address does not exist.'; # TODO: make translatable
  195. return false;
  196. }
  197. if ($this->is_mailbox_alias($this->username) ) {
  198. $this->errormsg[] = 'This alias belongs to a mailbox and can\'t be deleted.'; # TODO: make translatable
  199. return false;
  200. }
  201. $result = db_delete('alias', 'address', $this->username);
  202. if( $result == 1 ) {
  203. list(/*NULL*/,$domain) = explode('@', $this->username);
  204. db_log ($domain, 'delete_alias', $this->username);
  205. return true;
  206. }
  207. }
  208. /**
  209. * @return return value of previously called method
  210. */
  211. public function result() {
  212. return $this->return;
  213. }
  214. }
  215. /* vim: set expandtab softtabstop=4 tabstop=4 shiftwidth=4: */