functions.tls_policy_maps.inc.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. <?php
  2. function tls_policy_maps($_action, $_data = null, $attr = null) {
  3. global $pdo;
  4. global $lang;
  5. if ($_SESSION['mailcow_cc_role'] != "admin") {
  6. return false;
  7. }
  8. switch ($_action) {
  9. case 'add':
  10. $dest = idn_to_ascii(trim($_data['dest']), 0, INTL_IDNA_VARIANT_UTS46);
  11. $policy = strtolower(trim($_data['policy']));
  12. $parameters = (isset($_data['parameters']) && !empty($_data['parameters'])) ? $_data['parameters'] : '';
  13. if (empty($dest) || in_array($dest, array('.', '*', '@'))) {
  14. $_SESSION['return'][] = array(
  15. 'type' => 'danger',
  16. 'log' => array(__FUNCTION__, $_action, $_data, $_attr),
  17. 'msg' => 'tls_policy_map_dest_invalid'
  18. );
  19. return false;
  20. }
  21. if (!empty($parameters)) {
  22. foreach (explode(' ', $parameters) as $parameter) {
  23. if (!preg_match('/(.+)\=(.+)/i', $parameter)) {
  24. $_SESSION['return'][] = array(
  25. 'type' => 'danger',
  26. 'log' => array(__FUNCTION__, $_action, $_data, $_attr),
  27. 'msg' => 'tls_policy_map_parameter_invalid'
  28. );
  29. return false;
  30. }
  31. }
  32. }
  33. $active = intval($_data['active']);
  34. $tls_policy_maps = tls_policy_maps('get');
  35. foreach ($tls_policy_maps as $tls_policy_map) {
  36. if (tls_policy_maps('details', $tls_policy_map)['dest'] == $dest) {
  37. $_SESSION['return'][] = array(
  38. 'type' => 'danger',
  39. 'log' => array(__FUNCTION__, $_action, $_data, $_attr),
  40. 'msg' => array('tls_policy_map_entry_exists', htmlspecialchars($dest))
  41. );
  42. return false;
  43. }
  44. }
  45. $stmt = $pdo->prepare("INSERT INTO `tls_policy_override` (`dest`, `policy`, `parameters`, `active`) VALUES
  46. (:dest, :policy, :parameters, :active)");
  47. $stmt->execute(array(
  48. ':dest' => $dest,
  49. ':policy' => $policy,
  50. ':parameters' => $parameters,
  51. ':active' => $active
  52. ));
  53. $_SESSION['return'][] = array(
  54. 'type' => 'success',
  55. 'log' => array(__FUNCTION__, $_action, $_data, $_attr),
  56. 'msg' => array('tls_policy_map_entry_saved', htmlspecialchars($dest))
  57. );
  58. break;
  59. case 'edit':
  60. $ids = (array)$_data['id'];
  61. foreach ($ids as $id) {
  62. $is_now = tls_policy_maps('details', $id);
  63. if (!empty($is_now)) {
  64. $active = (isset($_data['active'])) ? intval($_data['active']) : $is_now['active'];
  65. $dest = (!empty($_data['dest'])) ? $_data['dest'] : $is_now['dest'];
  66. $policy = (!empty($_data['policy'])) ? $_data['policy'] : $is_now['policy'];
  67. $parameters = (isset($_data['parameters'])) ? $_data['parameters'] : $is_now['parameters'];
  68. }
  69. else {
  70. $_SESSION['return'][] = array(
  71. 'type' => 'danger',
  72. 'log' => array(__FUNCTION__, $_action, $_data, $_attr),
  73. 'msg' => 'access_denied'
  74. );
  75. continue;
  76. }
  77. if (empty($dest) || in_array($dest, array('.', '*', '@'))) {
  78. $_SESSION['return'][] = array(
  79. 'type' => 'danger',
  80. 'log' => array(__FUNCTION__, $_action, $_data, $_attr),
  81. 'msg' => 'tls_policy_map_dest_invalid'
  82. );
  83. return false;
  84. }
  85. if (!empty($parameters)) {
  86. foreach (explode(' ', $parameters) as $parameter) {
  87. if (!preg_match('/(.+)\=(.+)/i', $parameter)) {
  88. $_SESSION['return'][] = array(
  89. 'type' => 'danger',
  90. 'log' => array(__FUNCTION__, $_action, $_data, $_attr),
  91. 'msg' => 'tls_policy_map_parameter_invalid'
  92. );
  93. return false;
  94. }
  95. }
  96. }
  97. $tls_policy_maps = tls_policy_maps('get');
  98. foreach ($tls_policy_maps as $tls_policy_map) {
  99. if ($tls_policy_map == $id) { continue; }
  100. if (tls_policy_maps('details', $tls_policy_map)['dest'] == $dest) {
  101. $_SESSION['return'][] = array(
  102. 'type' => 'danger',
  103. 'log' => array(__FUNCTION__, $_action, $_data, $_attr),
  104. 'msg' => array('recipient_map_entry_exists', htmlspecialchars($dest))
  105. );
  106. return false;
  107. }
  108. }
  109. $stmt = $pdo->prepare("UPDATE `tls_policy_override` SET
  110. `dest` = :dest,
  111. `policy` = :policy,
  112. `parameters` = :parameters,
  113. `active` = :active
  114. WHERE `id`= :id");
  115. $stmt->execute(array(
  116. ':dest' => $dest,
  117. ':policy' => $policy,
  118. ':parameters' => $parameters,
  119. ':active' => $active,
  120. ':id' => $id
  121. ));
  122. $_SESSION['return'][] = array(
  123. 'type' => 'success',
  124. 'log' => array(__FUNCTION__, $_action, $_data, $_attr),
  125. 'msg' => array('tls_policy_map_entry_saved', htmlspecialchars($dest))
  126. );
  127. }
  128. break;
  129. case 'details':
  130. $mapdata = array();
  131. $id = intval($_data);
  132. $stmt = $pdo->prepare("SELECT `id`,
  133. `dest`,
  134. `policy`,
  135. `parameters`,
  136. `active` AS `active`,
  137. `created`,
  138. `modified` FROM `tls_policy_override`
  139. WHERE `id` = :id");
  140. $stmt->execute(array(':id' => $id));
  141. $mapdata = $stmt->fetch(PDO::FETCH_ASSOC);
  142. return $mapdata;
  143. break;
  144. case 'get':
  145. $mapdata = array();
  146. $all_items = array();
  147. $id = intval($_data);
  148. $stmt = $pdo->query("SELECT `id` FROM `tls_policy_override`");
  149. $all_items = $stmt->fetchAll(PDO::FETCH_ASSOC);
  150. foreach ($all_items as $i) {
  151. $mapdata[] = $i['id'];
  152. }
  153. $all_items = null;
  154. return $mapdata;
  155. break;
  156. case 'delete':
  157. $ids = (array)$_data['id'];
  158. foreach ($ids as $id) {
  159. if (!is_numeric($id)) {
  160. return false;
  161. }
  162. $stmt = $pdo->prepare("DELETE FROM `tls_policy_override` WHERE `id`= :id");
  163. $stmt->execute(array(':id' => $id));
  164. $_SESSION['return'][] = array(
  165. 'type' => 'success',
  166. 'log' => array(__FUNCTION__, $_action, $_data, $_attr),
  167. 'msg' => array('tls_policy_map_entry_deleted', htmlspecialchars($id))
  168. );
  169. }
  170. break;
  171. }
  172. }