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.

179 lines
5.5 KiB

  1. <?php
  2. /**
  3. * Postfix Admin
  4. *
  5. * LICENSE
  6. * This source file is subject to the GPL license that is bundled with
  7. * this package in the file LICENSE.TXT.
  8. *
  9. * Further details on the project are available at :
  10. * http://www.postfixadmin.com or http://postfixadmin.sf.net
  11. *
  12. * @version $Id$
  13. * @license GNU GPL v2 or later.
  14. *
  15. * File: edit-alias.php
  16. * Used to update an alias.
  17. *
  18. * Template File: edit-alias.php
  19. *
  20. * Template Variables:
  21. *
  22. * tMessage
  23. * tGoto
  24. *
  25. * Form POST \ GET Variables:
  26. *
  27. * fAddress
  28. * fDomain
  29. * fGoto
  30. */
  31. require_once('common.php');
  32. authentication_require_role('admin');
  33. $SESSID_USERNAME = authentication_get_username();
  34. if($CONF['alias_control_admin'] == 'NO' && !authentication_has_role('global-admin')) {
  35. die("Check config.inc.php - domain administrators do not have the ability to edit user's aliases (alias_control_admin)");
  36. }
  37. /* retrieve existing alias record for the user first... may be via GET or POST */
  38. if(isset($_GET['address']) && isset($_GET['domain'])) {
  39. $fAddress = escape_string($_GET['address']);
  40. $fDomain = escape_string($_GET['domain']);
  41. }
  42. elseif(isset($_POST['address']) && isset($_POST['domain'])) {
  43. $fAddress = escape_string($_POST['address']);
  44. $fDomain = escape_string($_POST['domain']);
  45. }
  46. else {
  47. die("Required parameters not present");
  48. }
  49. /* Check the user is able to edit the domain's aliases */
  50. if(!check_owner($SESSID_USERNAME, $fDomain) && !authentication_has_role('global-admin'))
  51. {
  52. die("You lack permission to do this. yes.");
  53. }
  54. $table_alias = table_by_key('alias');
  55. $alias_list = array();
  56. $orig_alias_list = array();
  57. $result = db_query ("SELECT * FROM $table_alias WHERE address='$fAddress' AND domain='$fDomain'");
  58. if ($result['rows'] == 1)
  59. {
  60. $row = db_array ($result['result']);
  61. $tGoto = $row['goto'];
  62. $orig_alias_list = explode(',', $tGoto);
  63. $alias_list = $orig_alias_list;
  64. //. if we are not a global admin, and special_alias_control is NO, hide the alias that's the mailbox name.
  65. if($CONF['special_alias_control'] == 'NO' && !authentication_has_role('global-admin')) {
  66. /* Has a mailbox as well? Remove the address from $tGoto in order to edit just the real aliases */
  67. $result = db_query ("SELECT * FROM $table_mailbox WHERE username='$fAddress' AND domain='$fDomain'");
  68. if ($result['rows'] == 1)
  69. {
  70. $alias_list = array(); // empty it, repopulated again below
  71. foreach($orig_alias_list as $alias) {
  72. if(strtolower($alias) == strtolower($fAddress)) {
  73. // mailbox address is dropped if they don't have special_alias_control enabled, and/or not a global-admin
  74. }
  75. else {
  76. $alias_list[] = $alias;
  77. }
  78. }
  79. }
  80. }
  81. }
  82. else {
  83. die("Invalid alias / domain combination");
  84. }
  85. if ($_SERVER['REQUEST_METHOD'] == "POST")
  86. {
  87. $pEdit_alias_goto = $PALANG['pEdit_alias_goto'];
  88. if (isset ($_POST['fGoto'])) $fGoto = escape_string ($_POST['fGoto']);
  89. $fGoto = strtolower ($fGoto);
  90. if (!check_alias_owner ($SESSID_USERNAME, $fAddress))
  91. {
  92. $error = 1;
  93. $tGoto = $fGoto;
  94. $tMessage = $PALANG['pEdit_alias_result_error'];
  95. }
  96. $goto = preg_replace ('/\\\r\\\n/', ',', $fGoto);
  97. $goto = preg_replace ('/\r\n/', ',', $goto);
  98. $goto = preg_replace ('/[\s]+/i', '', $goto);
  99. $goto = preg_replace ('/,*$|^,*/', '', $goto);
  100. $goto = preg_replace ('/,,*/', ',', $goto);
  101. if (empty ($goto) && !authentication_has_role('global-admin'))
  102. {
  103. $error = 1;
  104. $tGoto = $_POST['fGoto'];
  105. $tMessage = $PALANG['pEdit_alias_goto_text_error1'];
  106. }
  107. $new_aliases = array();
  108. if ($error != 1)
  109. {
  110. $new_aliases = explode(',', $goto);
  111. }
  112. $new_aliases = array_unique($new_aliases);
  113. foreach($new_aliases as $address) {
  114. if (in_array($address, $CONF['default_aliases'])) continue;
  115. if (empty($address)) continue; # TODO: should never happen - remove after 2.2 release
  116. if (!check_email($address))
  117. {
  118. $error = 1;
  119. $tGoto = $goto;
  120. $tMessage = $PALANG['pEdit_alias_goto_text_error2'] . "$address</span>";
  121. }
  122. }
  123. $result = db_query ("SELECT * FROM $table_mailbox WHERE username='$fAddress' AND domain='$fDomain'");
  124. if ($result['rows'] == 1)
  125. {
  126. if($CONF['alias_control_admin'] == 'NO' && !authentication_has_role('global-admin')) {
  127. // if original record had a mailbox alias, so ensure the updated one does too.
  128. if(in_array($orig_alias_list, $fAddress)) {
  129. $new_aliases[] = $fAddress;
  130. }
  131. }
  132. }
  133. // duplicates suck, mmkay..
  134. $new_aliases = array_unique($new_aliases);
  135. $goto = implode(',', $new_aliases);
  136. if ($error != 1)
  137. {
  138. $goto = escape_string($goto);
  139. $result = db_query ("UPDATE $table_alias SET goto='$goto',modified=NOW() WHERE address='$fAddress' AND domain='$fDomain'");
  140. if ($result['rows'] != 1)
  141. {
  142. $tMessage = $PALANG['pEdit_alias_result_error'];
  143. }
  144. else
  145. {
  146. db_log ($SESSID_USERNAME, $fDomain, 'edit_alias', "$fAddress -> $goto");
  147. header ("Location: list-virtual.php?domain=$fDomain");
  148. exit;
  149. }
  150. }
  151. }
  152. $fAddress = htmlentities($fAddress, ENT_QUOTES);
  153. $fDomain = htmlentities($fDomain, ENT_QUOTES);
  154. include ("templates/header.php");
  155. include ("templates/menu.php");
  156. include ("templates/edit-alias.php");
  157. include ("templates/footer.php");
  158. /* vim: set expandtab softtabstop=3 tabstop=3 shiftwidth=3: */
  159. ?>