functions.relayhost.inc.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. <?php
  2. function relayhost($_action, $_data = null) {
  3. global $pdo;
  4. global $lang;
  5. $_data_log = $_data;
  6. switch ($_action) {
  7. case 'add':
  8. if ($_SESSION['mailcow_cc_role'] != "admin") {
  9. $_SESSION['return'][] = array(
  10. 'type' => 'danger',
  11. 'log' => array(__FUNCTION__, $_action, $_data_log),
  12. 'msg' => 'access_denied'
  13. );
  14. return false;
  15. }
  16. $hostname = trim($_data['hostname']);
  17. $username = str_replace(':', '\:', trim($_data['username']));
  18. $password = str_replace(':', '\:', trim($_data['password']));
  19. if (empty($hostname)) {
  20. $_SESSION['return'][] = array(
  21. 'type' => 'danger',
  22. 'log' => array(__FUNCTION__, $_action, $_data_log),
  23. 'msg' => array('invalid_host', htmlspecialchars($host))
  24. );
  25. return false;
  26. }
  27. try {
  28. $stmt = $pdo->prepare("INSERT INTO `relayhosts` (`hostname`, `username` ,`password`, `active`)
  29. VALUES (:hostname, :username, :password, :active)");
  30. $stmt->execute(array(
  31. ':hostname' => $hostname,
  32. ':username' => $username,
  33. ':password' => str_replace(':', '\:', $password),
  34. ':active' => '1'
  35. ));
  36. }
  37. catch (PDOException $e) {
  38. $_SESSION['return'][] = array(
  39. 'type' => 'danger',
  40. 'log' => array(__FUNCTION__, $_action, $_data_log),
  41. 'msg' => array('mysql_error', $e)
  42. );
  43. return false;
  44. }
  45. $_SESSION['return'][] = array(
  46. 'type' => 'success',
  47. 'log' => array(__FUNCTION__, $_action, $_data_log),
  48. 'msg' => array('relayhost_added', htmlspecialchars(implode(', ', $hosts)))
  49. );
  50. break;
  51. case 'edit':
  52. if ($_SESSION['mailcow_cc_role'] != "admin") {
  53. $_SESSION['return'][] = array(
  54. 'type' => 'danger',
  55. 'log' => array(__FUNCTION__, $_action, $_data_log),
  56. 'msg' => 'access_denied'
  57. );
  58. return false;
  59. }
  60. $ids = (array)$_data['id'];
  61. foreach ($ids as $id) {
  62. $is_now = relayhost('details', $id);
  63. if (!empty($is_now)) {
  64. $hostname = (!empty($_data['hostname'])) ? trim($_data['hostname']) : $is_now['hostname'];
  65. $username = (isset($_data['username'])) ? trim($_data['username']) : $is_now['username'];
  66. $password = (isset($_data['password'])) ? trim($_data['password']) : $is_now['password'];
  67. $active = (isset($_data['active'])) ? intval($_data['active']) : $is_now['active_int'];
  68. }
  69. else {
  70. $_SESSION['return'][] = array(
  71. 'type' => 'danger',
  72. 'log' => array(__FUNCTION__, $_action, $_data_log),
  73. 'msg' => array('relayhost_invalid', $id)
  74. );
  75. continue;
  76. }
  77. try {
  78. $stmt = $pdo->prepare("UPDATE `relayhosts` SET
  79. `hostname` = :hostname,
  80. `username` = :username,
  81. `password` = :password,
  82. `active` = :active
  83. WHERE `id` = :id");
  84. $stmt->execute(array(
  85. ':id' => $id,
  86. ':hostname' => $hostname,
  87. ':username' => $username,
  88. ':password' => $password,
  89. ':active' => $active
  90. ));
  91. }
  92. catch (PDOException $e) {
  93. $_SESSION['return'][] = array(
  94. 'type' => 'danger',
  95. 'log' => array(__FUNCTION__, $_action, $_data_log),
  96. 'msg' => array('mysql_error', $e)
  97. );
  98. continue;
  99. }
  100. $_SESSION['return'][] = array(
  101. 'type' => 'success',
  102. 'log' => array(__FUNCTION__, $_action, $_data_log),
  103. 'msg' => array('object_modified', htmlspecialchars(implode(', ', $hostnames)))
  104. );
  105. }
  106. break;
  107. case 'delete':
  108. if ($_SESSION['mailcow_cc_role'] != "admin") {
  109. $_SESSION['return'][] = array(
  110. 'type' => 'danger',
  111. 'log' => array(__FUNCTION__, $_action, $_data_log),
  112. 'msg' => 'access_denied'
  113. );
  114. return false;
  115. }
  116. $ids = (array)$_data['id'];
  117. foreach ($ids as $id) {
  118. try {
  119. $stmt = $pdo->prepare("DELETE FROM `relayhosts` WHERE `id`= :id");
  120. $stmt->execute(array(':id' => $id));
  121. $stmt = $pdo->prepare("UPDATE `domain` SET `relayhost` = '0' WHERE `relayhost`= :id");
  122. $stmt->execute(array(':id' => $id));
  123. }
  124. catch (PDOException $e) {
  125. $_SESSION['return'][] = array(
  126. 'type' => 'danger',
  127. 'log' => array(__FUNCTION__, $_action, $_data_log),
  128. 'msg' => array('mysql_error', $e)
  129. );
  130. continue;
  131. }
  132. $_SESSION['return'][] = array(
  133. 'type' => 'success',
  134. 'log' => array(__FUNCTION__, $_action, $_data_log),
  135. 'msg' => array('relayhost_removed', htmlspecialchars($id))
  136. );
  137. }
  138. break;
  139. case 'get':
  140. if ($_SESSION['mailcow_cc_role'] != "admin") {
  141. return false;
  142. }
  143. $relayhosts = array();
  144. $stmt = $pdo->query("SELECT `id`, `hostname`, `username` FROM `relayhosts`");
  145. $relayhosts = $stmt->fetchAll(PDO::FETCH_ASSOC);
  146. return $relayhosts;
  147. break;
  148. case 'details':
  149. if ($_SESSION['mailcow_cc_role'] != "admin" || !isset($_data)) {
  150. return false;
  151. }
  152. $relayhostdata = array();
  153. $stmt = $pdo->prepare("SELECT `id`,
  154. `hostname`,
  155. `username`,
  156. `password`,
  157. `active` AS `active_int`,
  158. CONCAT(LEFT(`password`, 3), '...') AS `password_short`,
  159. CASE `active` WHEN 1 THEN '".$lang['mailbox']['yes']."' ELSE '".$lang['mailbox']['no']."' END AS `active`
  160. FROM `relayhosts`
  161. WHERE `id` = :id");
  162. $stmt->execute(array(':id' => $_data));
  163. $relayhostdata = $stmt->fetch(PDO::FETCH_ASSOC);
  164. if (!empty($relayhostdata)) {
  165. $stmt = $pdo->prepare("SELECT GROUP_CONCAT(`domain` SEPARATOR ', ') AS `used_by_domains` FROM `domain` WHERE `relayhost` = :id");
  166. $stmt->execute(array(':id' => $_data));
  167. $used_by_domains = $stmt->fetch(PDO::FETCH_ASSOC)['used_by_domains'];
  168. $used_by_domains = (empty($used_by_domains)) ? '' : $used_by_domains;
  169. $relayhostdata['used_by_domains'] = $used_by_domains;
  170. }
  171. return $relayhostdata;
  172. break;
  173. }
  174. }