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.

182 lines
5.4 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.tpl
  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. $fAddress = safepost('address', safeget('address')); # escaped below
  39. $fDomain = escape_string(preg_replace("/.*@/", "", $fAddress));
  40. $fAddress = escape_string($fAddress); # escaped now
  41. if ($fAddress == "") {
  42. die("Required parameters not present");
  43. }
  44. /* Check the user is able to edit the domain's aliases */
  45. if(!check_owner($SESSID_USERNAME, $fDomain) && !authentication_has_role('global-admin'))
  46. {
  47. die("You lack permission to do this. yes.");
  48. }
  49. $table_alias = table_by_key('alias');
  50. $alias_list = array();
  51. $orig_alias_list = array();
  52. $result = db_query ("SELECT * FROM $table_alias WHERE address='$fAddress' AND domain='$fDomain'");
  53. if ($result['rows'] == 1)
  54. {
  55. $row = db_array ($result['result']);
  56. $tGoto = $row['goto'];
  57. $orig_alias_list = explode(',', $tGoto);
  58. $tGoto = str_replace(',', "\n", $tGoto);
  59. $alias_list = $orig_alias_list;
  60. //. if we are not a global admin, and alias_control_admin is NO, hide the alias that's the mailbox name.
  61. if($CONF['alias_control_admin'] == 'NO' && !authentication_has_role('global-admin')) {
  62. /* Has a mailbox as well? Remove the address from $tGoto in order to edit just the real aliases */
  63. $result = db_query ("SELECT * FROM $table_mailbox WHERE username='$fAddress' AND domain='$fDomain'");
  64. if ($result['rows'] == 1)
  65. {
  66. $alias_list = array(); // empty it, repopulated again below
  67. foreach($orig_alias_list as $alias) {
  68. if(strtolower($alias) == strtolower($fAddress)) {
  69. // mailbox address is dropped if they don't have special_alias_control enabled, and/or not a global-admin
  70. }
  71. else {
  72. $alias_list[] = $alias;
  73. }
  74. }
  75. }
  76. }
  77. }
  78. else {
  79. die("Invalid alias");
  80. }
  81. if ($_SERVER['REQUEST_METHOD'] == "POST")
  82. {
  83. $pEdit_alias_goto = $PALANG['pEdit_alias_goto'];
  84. if (isset ($_POST['fGoto'])) $fGoto = escape_string ($_POST['fGoto']);
  85. $fGoto = strtolower ($fGoto);
  86. if (!check_alias_owner ($SESSID_USERNAME, $fAddress))
  87. {
  88. $error = 1;
  89. $tGoto = $fGoto;
  90. $tMessage = $PALANG['pEdit_alias_result_error'];
  91. }
  92. $goto = preg_replace ('/\\\r\\\n/', ',', $fGoto);
  93. $goto = preg_replace ('/\r\n/', ',', $goto);
  94. $goto = preg_replace ('/,[\s]+/i', ',', $goto);
  95. $goto = preg_replace ('/[\s]+,/i', ',', $goto);
  96. $goto = preg_replace ('/,*$|^,*/', '', $goto);
  97. $goto = preg_replace ('/,,*/', ',', $goto);
  98. if (empty ($goto) && !authentication_has_role('global-admin'))
  99. {
  100. $error = 1;
  101. $tGoto = $_POST['fGoto'];
  102. $tMessage = $PALANG['pEdit_alias_goto_text_error1'];
  103. }
  104. $new_aliases = array();
  105. if ($error != 1)
  106. {
  107. $new_aliases = explode(',', $goto);
  108. }
  109. $new_aliases = array_unique($new_aliases);
  110. foreach($new_aliases as $address) {
  111. if (in_array($address, $CONF['default_aliases'])) continue;
  112. if (empty($address)) continue; # TODO: should never happen - remove after 2.2 release
  113. if (!check_email($address))
  114. {
  115. $error = 1;
  116. $tGoto = $goto;
  117. if (!empty($tMessage)) $tMessage .= "<br />";
  118. $tMessage .= $PALANG['pEdit_alias_goto_text_error2'] . "$address</span>";
  119. }
  120. }
  121. $result = db_query ("SELECT * FROM $table_mailbox WHERE username='$fAddress' AND domain='$fDomain'");
  122. if ($result['rows'] == 1)
  123. {
  124. if($CONF['alias_control_admin'] == 'NO' && !authentication_has_role('global-admin')) {
  125. // if original record had a mailbox alias, so ensure the updated one does too.
  126. if(in_array($fAddress, $orig_alias_list)) {
  127. $new_aliases[] = $fAddress;
  128. }
  129. }
  130. }
  131. // duplicates suck, mmkay..
  132. $new_aliases = array_unique($new_aliases);
  133. $goto = implode(',', $new_aliases);
  134. if ($error != 1)
  135. {
  136. $goto = escape_string($goto);
  137. $result = db_query ("UPDATE $table_alias SET goto='$goto',modified=NOW() WHERE address='$fAddress' AND domain='$fDomain'");
  138. if ($result['rows'] != 1)
  139. {
  140. $tMessage = $PALANG['pEdit_alias_result_error'];
  141. }
  142. else
  143. {
  144. db_log ($fDomain, 'edit_alias', "$fAddress -> $goto");
  145. header ("Location: list-virtual.php?domain=$fDomain");
  146. exit;
  147. }
  148. }
  149. }
  150. $array = preg_split ('/,/', $tGoto);
  151. $smarty->assign ('fAddress', $fAddress);
  152. $smarty->assign ('tGoto', $tGoto);
  153. $smarty->assign ('array', $array, false);
  154. $smarty->assign ('tMessage', $tMessage, false);
  155. $smarty->assign ('smarty_template', 'edit-alias');
  156. $smarty->display ('index.tpl');
  157. /* vim: set expandtab softtabstop=3 tabstop=3 shiftwidth=3: */
  158. ?>