functions.quarantine.inc.php 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688
  1. <?php
  2. function quarantine($_action, $_data = null) {
  3. global $pdo;
  4. global $redis;
  5. global $lang;
  6. $_data_log = $_data;
  7. switch ($_action) {
  8. case 'quick_delete':
  9. // Dont return results, just log
  10. $hash = trim($_data);
  11. if (preg_match("/^([a-f0-9]{64})$/", $hash) === false) {
  12. logger(array('return' => array(
  13. array(
  14. 'type' => 'danger',
  15. 'log' => array(__FUNCTION__, $_action, $_data_log),
  16. 'msg' => 'access_denied'
  17. )
  18. )));
  19. return;
  20. }
  21. $stmt = $pdo->prepare('SELECT `id` FROM `quarantine` LEFT OUTER JOIN `user_acl` ON `user_acl`.`username` = `rcpt`
  22. WHERE SHA2(CONCAT(`id`, `qid`), 256) = :hash
  23. AND user_acl.quarantine = 1
  24. AND rcpt IN (SELECT username FROM mailbox)');
  25. $stmt->execute(array(':hash' => $hash));
  26. $row = $stmt->fetch(PDO::FETCH_ASSOC);
  27. if (empty($row['id']) || !is_numeric($row['id'])) {
  28. logger(array('return' => array(
  29. array(
  30. 'type' => 'danger',
  31. 'log' => array(__FUNCTION__, $_action, $_data_log),
  32. 'msg' => 'access_denied'
  33. )
  34. )));
  35. return;
  36. }
  37. else {
  38. $stmt = $pdo->prepare("DELETE FROM `quarantine` WHERE id = :id");
  39. $stmt->execute(array(
  40. ':id' => $row['id']
  41. ));
  42. }
  43. logger(array('return' => array(
  44. array(
  45. 'type' => 'success',
  46. 'log' => array(__FUNCTION__, $_action, $_data_log),
  47. 'msg' => array('item_deleted', $row['id'])
  48. )
  49. )));
  50. break;
  51. case 'quick_release':
  52. // Dont return results, just log
  53. $hash = trim($_data);
  54. if (preg_match("/^([a-f0-9]{64})$/", $hash) === false) {
  55. logger(array('return' => array(
  56. array(
  57. 'type' => 'danger',
  58. 'log' => array(__FUNCTION__, $_action, $_data_log),
  59. 'msg' => 'access_denied'
  60. )
  61. )));
  62. return;
  63. }
  64. $stmt = $pdo->prepare('SELECT `id` FROM `quarantine` LEFT OUTER JOIN `user_acl` ON `user_acl`.`username` = `rcpt`
  65. WHERE SHA2(CONCAT(`id`, `qid`), 256) = :hash
  66. AND `user_acl`.`quarantine` = 1
  67. AND `username` IN (SELECT `username` FROM `mailbox`)');
  68. $stmt->execute(array(':hash' => $hash));
  69. $row = $stmt->fetch(PDO::FETCH_ASSOC);
  70. if (empty($row['id']) || !is_numeric($row['id'])) {
  71. logger(array('return' => array(
  72. array(
  73. 'type' => 'danger',
  74. 'log' => array(__FUNCTION__, $_action, $_data_log),
  75. 'msg' => 'access_denied'
  76. )
  77. )));
  78. return;
  79. }
  80. else {
  81. $stmt = $pdo->prepare('SELECT `msg`, `qid`, `sender`, `rcpt` FROM `quarantine` WHERE `id` = :id');
  82. $stmt->execute(array(':id' => $row['id']));
  83. $detail_row = $stmt->fetch(PDO::FETCH_ASSOC);
  84. $sender = (isset($detail_row['sender'])) ? $detail_row['sender'] : 'sender-unknown@rspamd';
  85. if (!empty(gethostbynamel('postfix-mailcow'))) {
  86. $postfix = 'postfix-mailcow';
  87. }
  88. if (!empty(gethostbynamel('postfix'))) {
  89. $postfix = 'postfix';
  90. }
  91. else {
  92. logger(array('return' => array(
  93. array(
  94. 'type' => 'warning',
  95. 'log' => array(__FUNCTION__, $_action, $_data_log),
  96. 'msg' => array('release_send_failed', 'Cannot determine Postfix host')
  97. )
  98. )));
  99. return;
  100. }
  101. try {
  102. $release_format = $redis->Get('Q_RELEASE_FORMAT');
  103. }
  104. catch (RedisException $e) {
  105. logger(array('return' => array(
  106. array(
  107. 'type' => 'danger',
  108. 'log' => array(__FUNCTION__, $_action, $_data_log),
  109. 'msg' => array('redis_error', $e)
  110. )
  111. )));
  112. return;
  113. }
  114. if ($release_format == 'attachment') {
  115. try {
  116. $mail = new PHPMailer(true);
  117. $mail->isSMTP();
  118. $mail->SMTPDebug = 0;
  119. $mail->SMTPOptions = array(
  120. 'ssl' => array(
  121. 'verify_peer' => false,
  122. 'verify_peer_name' => false,
  123. 'allow_self_signed' => true
  124. )
  125. );
  126. if (!empty(gethostbynamel('postfix-mailcow'))) {
  127. $postfix = 'postfix-mailcow';
  128. }
  129. if (!empty(gethostbynamel('postfix'))) {
  130. $postfix = 'postfix';
  131. }
  132. else {
  133. logger(array('return' => array(
  134. array(
  135. 'type' => 'warning',
  136. 'log' => array(__FUNCTION__, $_action, $_data_log),
  137. 'msg' => array('release_send_failed', 'Cannot determine Postfix host')
  138. )
  139. )));
  140. return;
  141. }
  142. $mail->Host = $postfix;
  143. $mail->Port = 590;
  144. $mail->setFrom($sender);
  145. $mail->CharSet = 'UTF-8';
  146. $mail->Subject = sprintf($lang['quarantine']['release_subject'], $detail_row['qid']);
  147. $mail->addAddress($detail_row['rcpt']);
  148. $mail->IsHTML(false);
  149. $msg_tmpf = tempnam("/tmp", $detail_row['qid']);
  150. file_put_contents($msg_tmpf, $detail_row['msg']);
  151. $mail->addAttachment($msg_tmpf, $detail_row['qid'] . '.eml');
  152. $mail->Body = sprintf($lang['quarantine']['release_body']);
  153. $mail->send();
  154. unlink($msg_tmpf);
  155. }
  156. catch (phpmailerException $e) {
  157. unlink($msg_tmpf);
  158. logger(array('return' => array(
  159. array(
  160. 'type' => 'warning',
  161. 'log' => array(__FUNCTION__, $_action, $_data_log),
  162. 'msg' => array('release_send_failed', $e->errorMessage())
  163. )
  164. )));
  165. return;
  166. }
  167. }
  168. elseif ($release_format == 'raw') {
  169. $postfix_talk = array(
  170. array('220', 'HELO quarantine' . chr(10)),
  171. array('250', 'MAIL FROM: ' . $sender . chr(10)),
  172. array('250', 'RCPT TO: ' . $detail_row['rcpt'] . chr(10)),
  173. array('250', 'DATA' . chr(10)),
  174. array('354', $detail_row['msg'] . chr(10) . '.' . chr(10)),
  175. array('250', 'QUIT' . chr(10)),
  176. array('221', '')
  177. );
  178. // Thanks to https://stackoverflow.com/questions/6632399/given-an-email-as-raw-text-how-can-i-send-it-using-php
  179. $smtp_connection = fsockopen($postfix, 590, $errno, $errstr, 1);
  180. if (!$smtp_connection) {
  181. logger(array('return' => array(
  182. array(
  183. 'type' => 'warning',
  184. 'log' => array(__FUNCTION__, $_action, $_data_log),
  185. 'msg' => 'Cannot connect to Postfix'
  186. )
  187. )));
  188. return false;
  189. }
  190. for ($i=0; $i < count($postfix_talk); $i++) {
  191. $smtp_resource = fgets($smtp_connection, 256);
  192. if (substr($smtp_resource, 0, 3) !== $postfix_talk[$i][0]) {
  193. $ret = substr($smtp_resource, 0, 3);
  194. $ret = (empty($ret)) ? '-' : $ret;
  195. logger(array('return' => array(
  196. array(
  197. 'type' => 'warning',
  198. 'log' => array(__FUNCTION__, $_action, $_data_log),
  199. 'msg' => 'Postfix returned SMTP code ' . $smtp_resource . ', expected ' . $postfix_talk[$i][0]
  200. )
  201. )));
  202. return;
  203. }
  204. if ($postfix_talk[$i][1] !== '') {
  205. fputs($smtp_connection, $postfix_talk[$i][1]);
  206. }
  207. }
  208. fclose($smtp_connection);
  209. }
  210. $stmt = $pdo->prepare("DELETE FROM `quarantine` WHERE id = :id");
  211. $stmt->execute(array(
  212. ':id' => $row['id']
  213. ));
  214. }
  215. logger(array('return' => array(
  216. array(
  217. 'type' => 'success',
  218. 'log' => array(__FUNCTION__, $_action, $_data_log),
  219. 'msg' => array('item_released', $hash)
  220. )
  221. )));
  222. break;
  223. case 'delete':
  224. if (!is_array($_data['id'])) {
  225. $ids = array();
  226. $ids[] = $_data['id'];
  227. }
  228. else {
  229. $ids = $_data['id'];
  230. }
  231. if (!isset($_SESSION['acl']['quarantine']) || $_SESSION['acl']['quarantine'] != "1" ) {
  232. $_SESSION['return'][] = array(
  233. 'type' => 'danger',
  234. 'log' => array(__FUNCTION__, $_action, $_data_log),
  235. 'msg' => 'access_denied'
  236. );
  237. return false;
  238. }
  239. foreach ($ids as $id) {
  240. if (!is_numeric($id)) {
  241. $_SESSION['return'][] = array(
  242. 'type' => 'danger',
  243. 'log' => array(__FUNCTION__, $_action, $_data_log),
  244. 'msg' => 'access_denied'
  245. );
  246. continue;
  247. }
  248. $stmt = $pdo->prepare('SELECT `rcpt` FROM `quarantine` WHERE `id` = :id');
  249. $stmt->execute(array(':id' => $id));
  250. $row = $stmt->fetch(PDO::FETCH_ASSOC);
  251. if (!hasMailboxObjectAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $row['rcpt']) && $_SESSION['mailcow_cc_role'] != 'admin') {
  252. $_SESSION['return'][] = array(
  253. 'type' => 'danger',
  254. 'log' => array(__FUNCTION__, $_action, $_data_log),
  255. 'msg' => 'access_denied'
  256. );
  257. continue;
  258. }
  259. else {
  260. $stmt = $pdo->prepare("DELETE FROM `quarantine` WHERE `id` = :id");
  261. $stmt->execute(array(
  262. ':id' => $id
  263. ));
  264. }
  265. $_SESSION['return'][] = array(
  266. 'type' => 'success',
  267. 'log' => array(__FUNCTION__, $_action, $_data_log),
  268. 'msg' => array('item_deleted', $id)
  269. );
  270. }
  271. break;
  272. case 'edit':
  273. if (!isset($_SESSION['acl']['quarantine']) || $_SESSION['acl']['quarantine'] != "1" ) {
  274. $_SESSION['return'][] = array(
  275. 'type' => 'danger',
  276. 'log' => array(__FUNCTION__, $_action, $_data_log),
  277. 'msg' => 'access_denied'
  278. );
  279. return false;
  280. }
  281. // Edit settings
  282. if ($_data['action'] == 'settings') {
  283. if ($_SESSION['mailcow_cc_role'] != "admin") {
  284. $_SESSION['return'][] = array(
  285. 'type' => 'danger',
  286. 'log' => array(__FUNCTION__, $_action, $_data_log),
  287. 'msg' => 'access_denied'
  288. );
  289. return false;
  290. }
  291. $retention_size = $_data['retention_size'];
  292. if ($_data['release_format'] == 'attachment' || $_data['release_format'] == 'raw') {
  293. $release_format = $_data['release_format'];
  294. }
  295. else {
  296. $release_format = 'raw';
  297. }
  298. $max_size = $_data['max_size'];
  299. $max_age = intval($_data['max_age']);
  300. $subject = $_data['subject'];
  301. $sender = $_data['sender'];
  302. $html = $_data['html_tmpl'];
  303. if ($max_age <= 0) {
  304. $max_age = 365;
  305. }
  306. $exclude_domains = (array)$_data['exclude_domains'];
  307. try {
  308. $redis->Set('Q_RETENTION_SIZE', intval($retention_size));
  309. $redis->Set('Q_MAX_SIZE', intval($max_size));
  310. $redis->Set('Q_MAX_AGE', $max_age);
  311. $redis->Set('Q_EXCLUDE_DOMAINS', json_encode($exclude_domains));
  312. $redis->Set('Q_RELEASE_FORMAT', $release_format);
  313. $redis->Set('Q_SENDER', $sender);
  314. $redis->Set('Q_SUBJ', $subject);
  315. $redis->Set('Q_HTML', $html);
  316. }
  317. catch (RedisException $e) {
  318. $_SESSION['return'][] = array(
  319. 'type' => 'danger',
  320. 'log' => array(__FUNCTION__, $_action, $_data_log),
  321. 'msg' => array('redis_error', $e)
  322. );
  323. return false;
  324. }
  325. $_SESSION['return'][] = array(
  326. 'type' => 'success',
  327. 'log' => array(__FUNCTION__, $_action, $_data_log),
  328. 'msg' => 'saved_settings'
  329. );
  330. }
  331. // Release item
  332. elseif ($_data['action'] == 'release') {
  333. if (!is_array($_data['id'])) {
  334. $ids = array();
  335. $ids[] = $_data['id'];
  336. }
  337. else {
  338. $ids = $_data['id'];
  339. }
  340. foreach ($ids as $id) {
  341. if (!is_numeric($id)) {
  342. $_SESSION['return'][] = array(
  343. 'type' => 'danger',
  344. 'log' => array(__FUNCTION__, $_action, $_data_log),
  345. 'msg' => 'access_denied'
  346. );
  347. continue;
  348. }
  349. $stmt = $pdo->prepare('SELECT `msg`, `qid`, `sender`, `rcpt` FROM `quarantine` WHERE `id` = :id');
  350. $stmt->execute(array(':id' => $id));
  351. $row = $stmt->fetch(PDO::FETCH_ASSOC);
  352. if (!hasMailboxObjectAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $row['rcpt'])) {
  353. $_SESSION['return'][] = array(
  354. 'type' => 'danger',
  355. 'msg' => 'access_denied'
  356. );
  357. continue;
  358. }
  359. $sender = (isset($row['sender'])) ? $row['sender'] : 'sender-unknown@rspamd';
  360. if (!empty(gethostbynamel('postfix-mailcow'))) {
  361. $postfix = 'postfix-mailcow';
  362. }
  363. if (!empty(gethostbynamel('postfix'))) {
  364. $postfix = 'postfix';
  365. }
  366. else {
  367. $_SESSION['return'][] = array(
  368. 'type' => 'warning',
  369. 'log' => array(__FUNCTION__, $_action, $_data_log),
  370. 'msg' => array('release_send_failed', 'Cannot determine Postfix host')
  371. );
  372. continue;
  373. }
  374. try {
  375. $release_format = $redis->Get('Q_RELEASE_FORMAT');
  376. }
  377. catch (RedisException $e) {
  378. $_SESSION['return'][] = array(
  379. 'type' => 'danger',
  380. 'log' => array(__FUNCTION__, $_action, $_data_log),
  381. 'msg' => array('redis_error', $e)
  382. );
  383. return false;
  384. }
  385. if ($release_format == 'attachment') {
  386. try {
  387. $mail = new PHPMailer(true);
  388. $mail->isSMTP();
  389. $mail->SMTPDebug = 0;
  390. $mail->SMTPOptions = array(
  391. 'ssl' => array(
  392. 'verify_peer' => false,
  393. 'verify_peer_name' => false,
  394. 'allow_self_signed' => true
  395. )
  396. );
  397. if (!empty(gethostbynamel('postfix-mailcow'))) {
  398. $postfix = 'postfix-mailcow';
  399. }
  400. if (!empty(gethostbynamel('postfix'))) {
  401. $postfix = 'postfix';
  402. }
  403. else {
  404. $_SESSION['return'][] = array(
  405. 'type' => 'warning',
  406. 'log' => array(__FUNCTION__, $_action, $_data_log),
  407. 'msg' => array('release_send_failed', 'Cannot determine Postfix host')
  408. );
  409. continue;
  410. }
  411. $mail->Host = $postfix;
  412. $mail->Port = 590;
  413. $mail->setFrom($sender);
  414. $mail->CharSet = 'UTF-8';
  415. $mail->Subject = sprintf($lang['quarantine']['release_subject'], $row['qid']);
  416. $mail->addAddress($row['rcpt']);
  417. $mail->IsHTML(false);
  418. $msg_tmpf = tempnam("/tmp", $row['qid']);
  419. file_put_contents($msg_tmpf, $row['msg']);
  420. $mail->addAttachment($msg_tmpf, $row['qid'] . '.eml');
  421. $mail->Body = sprintf($lang['quarantine']['release_body']);
  422. $mail->send();
  423. unlink($msg_tmpf);
  424. }
  425. catch (phpmailerException $e) {
  426. unlink($msg_tmpf);
  427. $_SESSION['return'][] = array(
  428. 'type' => 'warning',
  429. 'log' => array(__FUNCTION__, $_action, $_data_log),
  430. 'msg' => array('release_send_failed', $e->errorMessage())
  431. );
  432. continue;
  433. }
  434. }
  435. elseif ($release_format == 'raw') {
  436. $postfix_talk = array(
  437. array('220', 'HELO quarantine' . chr(10)),
  438. array('250', 'MAIL FROM: ' . $sender . chr(10)),
  439. array('250', 'RCPT TO: ' . $row['rcpt'] . chr(10)),
  440. array('250', 'DATA' . chr(10)),
  441. array('354', $row['msg'] . chr(10) . '.' . chr(10)),
  442. array('250', 'QUIT' . chr(10)),
  443. array('221', '')
  444. );
  445. // Thanks to https://stackoverflow.com/questions/6632399/given-an-email-as-raw-text-how-can-i-send-it-using-php
  446. $smtp_connection = fsockopen($postfix, 590, $errno, $errstr, 1);
  447. if (!$smtp_connection) {
  448. $_SESSION['return'][] = array(
  449. 'type' => 'warning',
  450. 'log' => array(__FUNCTION__, $_action, $_data_log),
  451. 'msg' => 'Cannot connect to Postfix'
  452. );
  453. return false;
  454. }
  455. for ($i=0; $i < count($postfix_talk); $i++) {
  456. $smtp_resource = fgets($smtp_connection, 256);
  457. if (substr($smtp_resource, 0, 3) !== $postfix_talk[$i][0]) {
  458. $ret = substr($smtp_resource, 0, 3);
  459. $ret = (empty($ret)) ? '-' : $ret;
  460. $_SESSION['return'][] = array(
  461. 'type' => 'warning',
  462. 'log' => array(__FUNCTION__, $_action, $_data_log),
  463. 'msg' => 'Postfix returned SMTP code ' . $smtp_resource . ', expected ' . $postfix_talk[$i][0]
  464. );
  465. return false;
  466. }
  467. if ($postfix_talk[$i][1] !== '') {
  468. fputs($smtp_connection, $postfix_talk[$i][1]);
  469. }
  470. }
  471. fclose($smtp_connection);
  472. }
  473. try {
  474. $stmt = $pdo->prepare("DELETE FROM `quarantine` WHERE `id` = :id");
  475. $stmt->execute(array(
  476. ':id' => $id
  477. ));
  478. }
  479. catch (PDOException $e) {
  480. $_SESSION['return'][] = array(
  481. 'type' => 'danger',
  482. 'log' => array(__FUNCTION__, $_action, $_data_log),
  483. 'msg' => array('mysql_error', $e)
  484. );
  485. continue;
  486. }
  487. $_SESSION['return'][] = array(
  488. 'type' => 'success',
  489. 'log' => array(__FUNCTION__, $_action, $_data_log),
  490. 'msg' => array('item_released', $id)
  491. );
  492. }
  493. }
  494. elseif ($_data['action'] == 'learnspam') {
  495. if (!is_array($_data['id'])) {
  496. $ids = array();
  497. $ids[] = $_data['id'];
  498. }
  499. else {
  500. $ids = $_data['id'];
  501. }
  502. foreach ($ids as $id) {
  503. if (!is_numeric($id)) {
  504. $_SESSION['return'][] = array(
  505. 'type' => 'danger',
  506. 'log' => array(__FUNCTION__, $_action, $_data_log),
  507. 'msg' => 'access_denied'
  508. );
  509. continue;
  510. }
  511. $stmt = $pdo->prepare('SELECT `msg`, `rcpt` FROM `quarantine` WHERE `id` = :id');
  512. $stmt->execute(array(':id' => $id));
  513. $row = $stmt->fetch(PDO::FETCH_ASSOC);
  514. if (!hasMailboxObjectAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $row['rcpt']) && $_SESSION['mailcow_cc_role'] != 'admin') {
  515. $_SESSION['return'][] = array(
  516. 'type' => 'danger',
  517. 'msg' => 'access_denied'
  518. );
  519. continue;
  520. }
  521. $curl = curl_init();
  522. curl_setopt($curl, CURLOPT_UNIX_SOCKET_PATH, '/var/lib/rspamd/rspamd.sock');
  523. curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
  524. curl_setopt($curl, CURLOPT_POST, 1);
  525. curl_setopt($curl, CURLOPT_TIMEOUT, 30);
  526. curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Type: text/plain'));
  527. curl_setopt($curl, CURLOPT_URL,"http://rspamd/learnspam");
  528. curl_setopt($curl, CURLOPT_POSTFIELDS, $row['msg']);
  529. $response = curl_exec($curl);
  530. if (!curl_errno($curl)) {
  531. $response = json_decode($response, true);
  532. if (isset($response['error'])) {
  533. if (stripos($response['error'], 'already learned') === false) {
  534. $_SESSION['return'][] = array(
  535. 'type' => 'danger',
  536. 'log' => array(__FUNCTION__),
  537. 'msg' => array('spam_learn_error', $response['error'])
  538. );
  539. continue;
  540. }
  541. }
  542. curl_close($curl);
  543. $curl = curl_init();
  544. curl_setopt($curl, CURLOPT_UNIX_SOCKET_PATH, '/var/lib/rspamd/rspamd.sock');
  545. curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
  546. curl_setopt($curl, CURLOPT_POST, 1);
  547. curl_setopt($curl, CURLOPT_TIMEOUT, 30);
  548. curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Type: text/plain', 'Flag: 11'));
  549. curl_setopt($curl, CURLOPT_URL,"http://rspamd/fuzzyadd");
  550. curl_setopt($curl, CURLOPT_POSTFIELDS, $row['msg']);
  551. $response = curl_exec($curl);
  552. if (!curl_errno($curl)) {
  553. $response = json_decode($response, true);
  554. if (isset($response['error'])) {
  555. $_SESSION['return'][] = array(
  556. 'type' => 'warning',
  557. 'log' => array(__FUNCTION__),
  558. 'msg' => array('fuzzy_learn_error', $response['error'])
  559. );
  560. }
  561. curl_close($curl);
  562. try {
  563. $stmt = $pdo->prepare("DELETE FROM `quarantine` WHERE `id` = :id");
  564. $stmt->execute(array(
  565. ':id' => $id
  566. ));
  567. }
  568. catch (PDOException $e) {
  569. $_SESSION['return'][] = array(
  570. 'type' => 'danger',
  571. 'log' => array(__FUNCTION__, $_action, $_data_log),
  572. 'msg' => array('mysql_error', $e)
  573. );
  574. continue;
  575. }
  576. $_SESSION['return'][] = array(
  577. 'type' => 'success',
  578. 'log' => array(__FUNCTION__),
  579. 'msg' => array('qlearn_spam', $id)
  580. );
  581. continue;
  582. }
  583. else {
  584. curl_close($curl);
  585. $_SESSION['return'][] = array(
  586. 'type' => 'danger',
  587. 'log' => array(__FUNCTION__),
  588. 'msg' => array('spam_learn_error', 'Curl: ' . curl_strerror(curl_errno($curl)))
  589. );
  590. continue;
  591. }
  592. curl_close($curl);
  593. $_SESSION['return'][] = array(
  594. 'type' => 'danger',
  595. 'log' => array(__FUNCTION__),
  596. 'msg' => array('learn_spam_error', 'unknown')
  597. );
  598. continue;
  599. }
  600. else {
  601. $_SESSION['return'][] = array(
  602. 'type' => 'danger',
  603. 'log' => array(__FUNCTION__),
  604. 'msg' => array('spam_learn_error', 'Curl: ' . curl_strerror(curl_errno($curl)))
  605. );
  606. curl_close($curl);
  607. continue;
  608. }
  609. curl_close($curl);
  610. $_SESSION['return'][] = array(
  611. 'type' => 'danger',
  612. 'log' => array(__FUNCTION__),
  613. 'msg' => array('learn_spam_error', 'unknown')
  614. );
  615. continue;
  616. }
  617. }
  618. return true;
  619. break;
  620. case 'get':
  621. if ($_SESSION['mailcow_cc_role'] == "user") {
  622. $stmt = $pdo->prepare('SELECT `id`, `qid`, `subject`, LOCATE("VIRUS_FOUND", `symbols`) AS `virus_flag`, `rcpt`, `sender`, UNIX_TIMESTAMP(`created`) AS `created` FROM `quarantine` WHERE `rcpt` = :mbox');
  623. $stmt->execute(array(':mbox' => $_SESSION['mailcow_cc_username']));
  624. $rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
  625. while($row = array_shift($rows)) {
  626. $q_meta[] = $row;
  627. }
  628. }
  629. elseif ($_SESSION['mailcow_cc_role'] == "admin") {
  630. $stmt = $pdo->query('SELECT `id`, `qid`, `subject`, LOCATE("VIRUS_FOUND", `symbols`) AS `virus_flag`, `rcpt`, `sender`, UNIX_TIMESTAMP(`created`) AS `created` FROM `quarantine`');
  631. $rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
  632. while($row = array_shift($rows)) {
  633. $q_meta[] = $row;
  634. }
  635. }
  636. else {
  637. $domains = array_merge(mailbox('get', 'domains'), mailbox('get', 'alias_domains'));
  638. foreach ($domains as $domain) {
  639. $stmt = $pdo->prepare('SELECT `id`, `qid`, `subject`, LOCATE("VIRUS_FOUND", `symbols`) AS `virus_flag`, `rcpt`, `sender`, UNIX_TIMESTAMP(`created`) AS `created` FROM `quarantine` WHERE `rcpt` REGEXP :domain');
  640. $stmt->execute(array(':domain' => '@' . $domain . '$'));
  641. $rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
  642. while($row = array_shift($rows)) {
  643. $q_meta[] = $row;
  644. }
  645. }
  646. }
  647. return $q_meta;
  648. break;
  649. case 'settings':
  650. try {
  651. if ($_SESSION['mailcow_cc_role'] == "admin") {
  652. $settings['exclude_domains'] = json_decode($redis->Get('Q_EXCLUDE_DOMAINS'), true);
  653. }
  654. $settings['max_size'] = $redis->Get('Q_MAX_SIZE');
  655. $settings['max_age'] = $redis->Get('Q_MAX_AGE');
  656. $settings['retention_size'] = $redis->Get('Q_RETENTION_SIZE');
  657. $settings['release_format'] = $redis->Get('Q_RELEASE_FORMAT');
  658. $settings['subject'] = $redis->Get('Q_SUBJ');
  659. $settings['sender'] = $redis->Get('Q_SENDER');
  660. $settings['html_tmpl'] = htmlspecialchars($redis->Get('Q_HTML'));
  661. if (empty($settings['html_tmpl'])) {
  662. $settings['html_tmpl'] = htmlspecialchars(file_get_contents("/tpls/quarantine.tpl"));
  663. }
  664. }
  665. catch (RedisException $e) {
  666. $_SESSION['return'][] = array(
  667. 'type' => 'danger',
  668. 'log' => array(__FUNCTION__, $_action, $_data_log),
  669. 'msg' => array('redis_error', $e)
  670. );
  671. return false;
  672. }
  673. return $settings;
  674. break;
  675. case 'details':
  676. if (!is_numeric($_data) || empty($_data)) {
  677. return false;
  678. }
  679. $stmt = $pdo->prepare('SELECT `rcpt`, `symbols`, `msg`, `domain` FROM `quarantine` WHERE `id`= :id');
  680. $stmt->execute(array(':id' => $_data));
  681. $row = $stmt->fetch(PDO::FETCH_ASSOC);
  682. if (hasMailboxObjectAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $row['rcpt'])) {
  683. return $row;
  684. }
  685. return false;
  686. break;
  687. }
  688. }