functions.transports.inc.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  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. }
  175. function transport($_action, $_data = null) {
  176. global $pdo;
  177. global $lang;
  178. $_data_log = $_data;
  179. switch ($_action) {
  180. case 'add':
  181. if ($_SESSION['mailcow_cc_role'] != "admin") {
  182. $_SESSION['return'][] = array(
  183. 'type' => 'danger',
  184. 'log' => array(__FUNCTION__, $_action, $_data_log),
  185. 'msg' => 'access_denied'
  186. );
  187. return false;
  188. }
  189. $destination = trim($_data['destination']);
  190. $nexthop = trim($_data['nexthop']);
  191. $username = str_replace(':', '\:', trim($_data['username']));
  192. $password = str_replace(':', '\:', trim($_data['password']));
  193. if (empty($destination) || (is_valid_domain_name(preg_replace('/^' . preg_quote('.', '/') . '/', '', $destination)) === false && $destination != '*')) {
  194. $_SESSION['return'][] = array(
  195. 'type' => 'danger',
  196. 'log' => array(__FUNCTION__, $_action, $_data_log),
  197. 'msg' => 'invalid_destination'
  198. );
  199. return false;
  200. }
  201. if (empty($nexthop)) {
  202. $_SESSION['return'][] = array(
  203. 'type' => 'danger',
  204. 'log' => array(__FUNCTION__, $_action, $_data_log),
  205. 'msg' => array('invalid_nexthop')
  206. );
  207. return false;
  208. }
  209. if (!empty($username)) {
  210. $transports = transport('get');
  211. if (!empty($transports)) {
  212. foreach ($transports as $transport) {
  213. if (transport('details', $transport['id'])['nexthop'] == $nexthop && transport('details', $transport['id'])['username'] != $username) {
  214. $_SESSION['return'][] = array(
  215. 'type' => 'danger',
  216. 'log' => array(__FUNCTION__, $_action, $_data_log),
  217. 'msg' => 'invalid_nexthop_authenticated'
  218. );
  219. return false;
  220. }
  221. }
  222. }
  223. }
  224. try {
  225. $stmt = $pdo->prepare("INSERT INTO `transports` (`nexthop`, `destination`, `username` ,`password`, `active`)
  226. VALUES (:nexthop, :destination, :username, :password, :active)");
  227. $stmt->execute(array(
  228. ':nexthop' => $nexthop,
  229. ':destination' => $destination,
  230. ':username' => $username,
  231. ':password' => str_replace(':', '\:', $password),
  232. ':active' => '1'
  233. ));
  234. $stmt = $pdo->prepare("UPDATE `transports` SET
  235. `username` = :username,
  236. `password` = :password
  237. WHERE `nexthop` = :nexthop");
  238. $stmt->execute(array(
  239. ':nexthop' => $nexthop,
  240. ':username' => $username,
  241. ':password' => $password
  242. ));
  243. }
  244. catch (PDOException $e) {
  245. $_SESSION['return'][] = array(
  246. 'type' => 'danger',
  247. 'log' => array(__FUNCTION__, $_action, $_data_log),
  248. 'msg' => array('mysql_error', $e)
  249. );
  250. return false;
  251. }
  252. $_SESSION['return'][] = array(
  253. 'type' => 'success',
  254. 'log' => array(__FUNCTION__, $_action, $_data_log),
  255. 'msg' => array('relayhost_added', htmlspecialchars(implode(', ', $hosts)))
  256. );
  257. break;
  258. case 'edit':
  259. if ($_SESSION['mailcow_cc_role'] != "admin") {
  260. $_SESSION['return'][] = array(
  261. 'type' => 'danger',
  262. 'log' => array(__FUNCTION__, $_action, $_data_log),
  263. 'msg' => 'access_denied'
  264. );
  265. return false;
  266. }
  267. $ids = (array)$_data['id'];
  268. foreach ($ids as $id) {
  269. $is_now = transport('details', $id);
  270. if (!empty($is_now)) {
  271. $destination = (!empty($_data['destination'])) ? trim($_data['destination']) : $is_now['destination'];
  272. $nexthop = (!empty($_data['nexthop'])) ? trim($_data['nexthop']) : $is_now['nexthop'];
  273. $username = (isset($_data['username'])) ? trim($_data['username']) : $is_now['username'];
  274. $password = (isset($_data['password'])) ? trim($_data['password']) : $is_now['password'];
  275. $active = (isset($_data['active'])) ? intval($_data['active']) : $is_now['active_int'];
  276. }
  277. if (empty($username)) {
  278. $password = '';
  279. }
  280. else {
  281. $_SESSION['return'][] = array(
  282. 'type' => 'danger',
  283. 'log' => array(__FUNCTION__, $_action, $_data_log),
  284. 'msg' => array('relayhost_invalid', $id)
  285. );
  286. continue;
  287. }
  288. try {
  289. $stmt = $pdo->prepare("UPDATE `transports` SET
  290. `destination` = :destination,
  291. `nexthop` = :nexthop,
  292. `username` = :username,
  293. `password` = :password,
  294. `active` = :active
  295. WHERE `id` = :id");
  296. $stmt->execute(array(
  297. ':id' => $id,
  298. ':destination' => $destination,
  299. ':nexthop' => $nexthop,
  300. ':username' => $username,
  301. ':password' => $password,
  302. ':active' => $active
  303. ));
  304. $stmt = $pdo->prepare("UPDATE `transports` SET
  305. `username` = :username,
  306. `password` = :password
  307. WHERE `nexthop` = :nexthop");
  308. $stmt->execute(array(
  309. ':nexthop' => $nexthop,
  310. ':username' => $username,
  311. ':password' => $password
  312. ));
  313. }
  314. catch (PDOException $e) {
  315. $_SESSION['return'][] = array(
  316. 'type' => 'danger',
  317. 'log' => array(__FUNCTION__, $_action, $_data_log),
  318. 'msg' => array('mysql_error', $e)
  319. );
  320. continue;
  321. }
  322. $_SESSION['return'][] = array(
  323. 'type' => 'success',
  324. 'log' => array(__FUNCTION__, $_action, $_data_log),
  325. 'msg' => array('object_modified', htmlspecialchars(implode(', ', $hostnames)))
  326. );
  327. }
  328. break;
  329. case 'delete':
  330. if ($_SESSION['mailcow_cc_role'] != "admin") {
  331. $_SESSION['return'][] = array(
  332. 'type' => 'danger',
  333. 'log' => array(__FUNCTION__, $_action, $_data_log),
  334. 'msg' => 'access_denied'
  335. );
  336. return false;
  337. }
  338. $ids = (array)$_data['id'];
  339. foreach ($ids as $id) {
  340. try {
  341. $stmt = $pdo->prepare("DELETE FROM `transports` WHERE `id`= :id");
  342. $stmt->execute(array(':id' => $id));
  343. }
  344. catch (PDOException $e) {
  345. $_SESSION['return'][] = array(
  346. 'type' => 'danger',
  347. 'log' => array(__FUNCTION__, $_action, $_data_log),
  348. 'msg' => array('mysql_error', $e)
  349. );
  350. continue;
  351. }
  352. $_SESSION['return'][] = array(
  353. 'type' => 'success',
  354. 'log' => array(__FUNCTION__, $_action, $_data_log),
  355. 'msg' => array('relayhost_removed', htmlspecialchars($id))
  356. );
  357. }
  358. break;
  359. case 'get':
  360. if ($_SESSION['mailcow_cc_role'] != "admin") {
  361. return false;
  362. }
  363. $transports = array();
  364. $stmt = $pdo->query("SELECT `id`, `destination`, `nexthop`, `username` FROM `transports`");
  365. $transports = $stmt->fetchAll(PDO::FETCH_ASSOC);
  366. return $transports;
  367. break;
  368. case 'details':
  369. if ($_SESSION['mailcow_cc_role'] != "admin" || !isset($_data)) {
  370. return false;
  371. }
  372. $transportdata = array();
  373. $stmt = $pdo->prepare("SELECT `id`,
  374. `destination`,
  375. `nexthop`,
  376. `username`,
  377. `password`,
  378. `active` AS `active_int`,
  379. CONCAT(LEFT(`password`, 3), '...') AS `password_short`,
  380. CASE `active` WHEN 1 THEN '".$lang['mailbox']['yes']."' ELSE '".$lang['mailbox']['no']."' END AS `active`
  381. FROM `transports`
  382. WHERE `id` = :id");
  383. $stmt->execute(array(':id' => $_data));
  384. $transportdata = $stmt->fetch(PDO::FETCH_ASSOC);
  385. return $transportdata;
  386. break;
  387. }
  388. }