functions.quarantine.inc.php 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682
  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. $subject = $_data['subject'];
  300. $sender = $_data['sender'];
  301. $html = $_data['html_tmpl'];
  302. $exclude_domains = (array)$_data['exclude_domains'];
  303. try {
  304. $redis->Set('Q_RETENTION_SIZE', intval($retention_size));
  305. $redis->Set('Q_MAX_SIZE', intval($max_size));
  306. $redis->Set('Q_EXCLUDE_DOMAINS', json_encode($exclude_domains));
  307. $redis->Set('Q_RELEASE_FORMAT', $release_format);
  308. $redis->Set('Q_SENDER', $sender);
  309. $redis->Set('Q_SUBJ', $subject);
  310. $redis->Set('Q_HTML', $html);
  311. }
  312. catch (RedisException $e) {
  313. $_SESSION['return'][] = array(
  314. 'type' => 'danger',
  315. 'log' => array(__FUNCTION__, $_action, $_data_log),
  316. 'msg' => array('redis_error', $e)
  317. );
  318. return false;
  319. }
  320. $_SESSION['return'][] = array(
  321. 'type' => 'success',
  322. 'log' => array(__FUNCTION__, $_action, $_data_log),
  323. 'msg' => 'saved_settings'
  324. );
  325. }
  326. // Release item
  327. elseif ($_data['action'] == 'release') {
  328. if (!is_array($_data['id'])) {
  329. $ids = array();
  330. $ids[] = $_data['id'];
  331. }
  332. else {
  333. $ids = $_data['id'];
  334. }
  335. foreach ($ids as $id) {
  336. if (!is_numeric($id)) {
  337. $_SESSION['return'][] = array(
  338. 'type' => 'danger',
  339. 'log' => array(__FUNCTION__, $_action, $_data_log),
  340. 'msg' => 'access_denied'
  341. );
  342. continue;
  343. }
  344. $stmt = $pdo->prepare('SELECT `msg`, `qid`, `sender`, `rcpt` FROM `quarantine` WHERE `id` = :id');
  345. $stmt->execute(array(':id' => $id));
  346. $row = $stmt->fetch(PDO::FETCH_ASSOC);
  347. if (!hasMailboxObjectAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $row['rcpt'])) {
  348. $_SESSION['return'][] = array(
  349. 'type' => 'danger',
  350. 'msg' => 'access_denied'
  351. );
  352. continue;
  353. }
  354. $sender = (isset($row['sender'])) ? $row['sender'] : 'sender-unknown@rspamd';
  355. if (!empty(gethostbynamel('postfix-mailcow'))) {
  356. $postfix = 'postfix-mailcow';
  357. }
  358. if (!empty(gethostbynamel('postfix'))) {
  359. $postfix = 'postfix';
  360. }
  361. else {
  362. $_SESSION['return'][] = array(
  363. 'type' => 'warning',
  364. 'log' => array(__FUNCTION__, $_action, $_data_log),
  365. 'msg' => array('release_send_failed', 'Cannot determine Postfix host')
  366. );
  367. continue;
  368. }
  369. try {
  370. $release_format = $redis->Get('Q_RELEASE_FORMAT');
  371. }
  372. catch (RedisException $e) {
  373. $_SESSION['return'][] = array(
  374. 'type' => 'danger',
  375. 'log' => array(__FUNCTION__, $_action, $_data_log),
  376. 'msg' => array('redis_error', $e)
  377. );
  378. return false;
  379. }
  380. if ($release_format == 'attachment') {
  381. try {
  382. $mail = new PHPMailer(true);
  383. $mail->isSMTP();
  384. $mail->SMTPDebug = 0;
  385. $mail->SMTPOptions = array(
  386. 'ssl' => array(
  387. 'verify_peer' => false,
  388. 'verify_peer_name' => false,
  389. 'allow_self_signed' => true
  390. )
  391. );
  392. if (!empty(gethostbynamel('postfix-mailcow'))) {
  393. $postfix = 'postfix-mailcow';
  394. }
  395. if (!empty(gethostbynamel('postfix'))) {
  396. $postfix = 'postfix';
  397. }
  398. else {
  399. $_SESSION['return'][] = array(
  400. 'type' => 'warning',
  401. 'log' => array(__FUNCTION__, $_action, $_data_log),
  402. 'msg' => array('release_send_failed', 'Cannot determine Postfix host')
  403. );
  404. continue;
  405. }
  406. $mail->Host = $postfix;
  407. $mail->Port = 590;
  408. $mail->setFrom($sender);
  409. $mail->CharSet = 'UTF-8';
  410. $mail->Subject = sprintf($lang['quarantine']['release_subject'], $row['qid']);
  411. $mail->addAddress($row['rcpt']);
  412. $mail->IsHTML(false);
  413. $msg_tmpf = tempnam("/tmp", $row['qid']);
  414. file_put_contents($msg_tmpf, $row['msg']);
  415. $mail->addAttachment($msg_tmpf, $row['qid'] . '.eml');
  416. $mail->Body = sprintf($lang['quarantine']['release_body']);
  417. $mail->send();
  418. unlink($msg_tmpf);
  419. }
  420. catch (phpmailerException $e) {
  421. unlink($msg_tmpf);
  422. $_SESSION['return'][] = array(
  423. 'type' => 'warning',
  424. 'log' => array(__FUNCTION__, $_action, $_data_log),
  425. 'msg' => array('release_send_failed', $e->errorMessage())
  426. );
  427. continue;
  428. }
  429. }
  430. elseif ($release_format == 'raw') {
  431. $postfix_talk = array(
  432. array('220', 'HELO quarantine' . chr(10)),
  433. array('250', 'MAIL FROM: ' . $sender . chr(10)),
  434. array('250', 'RCPT TO: ' . $row['rcpt'] . chr(10)),
  435. array('250', 'DATA' . chr(10)),
  436. array('354', $row['msg'] . chr(10) . '.' . chr(10)),
  437. array('250', 'QUIT' . chr(10)),
  438. array('221', '')
  439. );
  440. // Thanks to https://stackoverflow.com/questions/6632399/given-an-email-as-raw-text-how-can-i-send-it-using-php
  441. $smtp_connection = fsockopen($postfix, 590, $errno, $errstr, 1);
  442. if (!$smtp_connection) {
  443. $_SESSION['return'][] = array(
  444. 'type' => 'warning',
  445. 'log' => array(__FUNCTION__, $_action, $_data_log),
  446. 'msg' => 'Cannot connect to Postfix'
  447. );
  448. return false;
  449. }
  450. for ($i=0; $i < count($postfix_talk); $i++) {
  451. $smtp_resource = fgets($smtp_connection, 256);
  452. if (substr($smtp_resource, 0, 3) !== $postfix_talk[$i][0]) {
  453. $ret = substr($smtp_resource, 0, 3);
  454. $ret = (empty($ret)) ? '-' : $ret;
  455. $_SESSION['return'][] = array(
  456. 'type' => 'warning',
  457. 'log' => array(__FUNCTION__, $_action, $_data_log),
  458. 'msg' => 'Postfix returned SMTP code ' . $smtp_resource . ', expected ' . $postfix_talk[$i][0]
  459. );
  460. return false;
  461. }
  462. if ($postfix_talk[$i][1] !== '') {
  463. fputs($smtp_connection, $postfix_talk[$i][1]);
  464. }
  465. }
  466. fclose($smtp_connection);
  467. }
  468. try {
  469. $stmt = $pdo->prepare("DELETE FROM `quarantine` WHERE `id` = :id");
  470. $stmt->execute(array(
  471. ':id' => $id
  472. ));
  473. }
  474. catch (PDOException $e) {
  475. $_SESSION['return'][] = array(
  476. 'type' => 'danger',
  477. 'log' => array(__FUNCTION__, $_action, $_data_log),
  478. 'msg' => array('mysql_error', $e)
  479. );
  480. continue;
  481. }
  482. $_SESSION['return'][] = array(
  483. 'type' => 'success',
  484. 'log' => array(__FUNCTION__, $_action, $_data_log),
  485. 'msg' => array('item_released', $id)
  486. );
  487. }
  488. }
  489. elseif ($_data['action'] == 'learnspam') {
  490. if (!is_array($_data['id'])) {
  491. $ids = array();
  492. $ids[] = $_data['id'];
  493. }
  494. else {
  495. $ids = $_data['id'];
  496. }
  497. foreach ($ids as $id) {
  498. if (!is_numeric($id)) {
  499. $_SESSION['return'][] = array(
  500. 'type' => 'danger',
  501. 'log' => array(__FUNCTION__, $_action, $_data_log),
  502. 'msg' => 'access_denied'
  503. );
  504. continue;
  505. }
  506. $stmt = $pdo->prepare('SELECT `msg`, `rcpt` FROM `quarantine` WHERE `id` = :id');
  507. $stmt->execute(array(':id' => $id));
  508. $row = $stmt->fetch(PDO::FETCH_ASSOC);
  509. if (!hasMailboxObjectAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $row['rcpt']) && $_SESSION['mailcow_cc_role'] != 'admin') {
  510. $_SESSION['return'][] = array(
  511. 'type' => 'danger',
  512. 'msg' => 'access_denied'
  513. );
  514. continue;
  515. }
  516. $curl = curl_init();
  517. curl_setopt($curl, CURLOPT_UNIX_SOCKET_PATH, '/var/lib/rspamd/rspamd.sock');
  518. curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
  519. curl_setopt($curl, CURLOPT_POST, 1);
  520. curl_setopt($curl, CURLOPT_TIMEOUT, 30);
  521. curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Type: text/plain'));
  522. curl_setopt($curl, CURLOPT_URL,"http://rspamd/learnspam");
  523. curl_setopt($curl, CURLOPT_POSTFIELDS, $row['msg']);
  524. $response = curl_exec($curl);
  525. if (!curl_errno($curl)) {
  526. $response = json_decode($response, true);
  527. if (isset($response['error'])) {
  528. if (stripos($response['error'], 'already learned') === false) {
  529. $_SESSION['return'][] = array(
  530. 'type' => 'danger',
  531. 'log' => array(__FUNCTION__),
  532. 'msg' => array('spam_learn_error', $response['error'])
  533. );
  534. continue;
  535. }
  536. }
  537. curl_close($curl);
  538. $curl = curl_init();
  539. curl_setopt($curl, CURLOPT_UNIX_SOCKET_PATH, '/var/lib/rspamd/rspamd.sock');
  540. curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
  541. curl_setopt($curl, CURLOPT_POST, 1);
  542. curl_setopt($curl, CURLOPT_TIMEOUT, 30);
  543. curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Type: text/plain', 'Flag: 11'));
  544. curl_setopt($curl, CURLOPT_URL,"http://rspamd/fuzzyadd");
  545. curl_setopt($curl, CURLOPT_POSTFIELDS, $row['msg']);
  546. $response = curl_exec($curl);
  547. if (!curl_errno($curl)) {
  548. $response = json_decode($response, true);
  549. if (isset($response['error'])) {
  550. $_SESSION['return'][] = array(
  551. 'type' => 'warning',
  552. 'log' => array(__FUNCTION__),
  553. 'msg' => array('fuzzy_learn_error', $response['error'])
  554. );
  555. }
  556. curl_close($curl);
  557. try {
  558. $stmt = $pdo->prepare("DELETE FROM `quarantine` WHERE `id` = :id");
  559. $stmt->execute(array(
  560. ':id' => $id
  561. ));
  562. }
  563. catch (PDOException $e) {
  564. $_SESSION['return'][] = array(
  565. 'type' => 'danger',
  566. 'log' => array(__FUNCTION__, $_action, $_data_log),
  567. 'msg' => array('mysql_error', $e)
  568. );
  569. continue;
  570. }
  571. $_SESSION['return'][] = array(
  572. 'type' => 'success',
  573. 'log' => array(__FUNCTION__),
  574. 'msg' => array('qlearn_spam', $id)
  575. );
  576. continue;
  577. }
  578. else {
  579. curl_close($curl);
  580. $_SESSION['return'][] = array(
  581. 'type' => 'danger',
  582. 'log' => array(__FUNCTION__),
  583. 'msg' => array('spam_learn_error', 'Curl: ' . curl_strerror(curl_errno($curl)))
  584. );
  585. continue;
  586. }
  587. curl_close($curl);
  588. $_SESSION['return'][] = array(
  589. 'type' => 'danger',
  590. 'log' => array(__FUNCTION__),
  591. 'msg' => array('learn_spam_error', 'unknown')
  592. );
  593. continue;
  594. }
  595. else {
  596. $_SESSION['return'][] = array(
  597. 'type' => 'danger',
  598. 'log' => array(__FUNCTION__),
  599. 'msg' => array('spam_learn_error', 'Curl: ' . curl_strerror(curl_errno($curl)))
  600. );
  601. curl_close($curl);
  602. continue;
  603. }
  604. curl_close($curl);
  605. $_SESSION['return'][] = array(
  606. 'type' => 'danger',
  607. 'log' => array(__FUNCTION__),
  608. 'msg' => array('learn_spam_error', 'unknown')
  609. );
  610. continue;
  611. }
  612. }
  613. return true;
  614. break;
  615. case 'get':
  616. if ($_SESSION['mailcow_cc_role'] == "user") {
  617. $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');
  618. $stmt->execute(array(':mbox' => $_SESSION['mailcow_cc_username']));
  619. $rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
  620. while($row = array_shift($rows)) {
  621. $q_meta[] = $row;
  622. }
  623. }
  624. elseif ($_SESSION['mailcow_cc_role'] == "admin") {
  625. $stmt = $pdo->query('SELECT `id`, `qid`, `subject`, LOCATE("VIRUS_FOUND", `symbols`) AS `virus_flag`, `rcpt`, `sender`, UNIX_TIMESTAMP(`created`) AS `created` FROM `quarantine`');
  626. $rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
  627. while($row = array_shift($rows)) {
  628. $q_meta[] = $row;
  629. }
  630. }
  631. else {
  632. $domains = array_merge(mailbox('get', 'domains'), mailbox('get', 'alias_domains'));
  633. foreach ($domains as $domain) {
  634. $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');
  635. $stmt->execute(array(':domain' => '@' . $domain . '$'));
  636. $rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
  637. while($row = array_shift($rows)) {
  638. $q_meta[] = $row;
  639. }
  640. }
  641. }
  642. return $q_meta;
  643. break;
  644. case 'settings':
  645. try {
  646. if ($_SESSION['mailcow_cc_role'] == "admin") {
  647. $settings['exclude_domains'] = json_decode($redis->Get('Q_EXCLUDE_DOMAINS'), true);
  648. }
  649. $settings['max_size'] = $redis->Get('Q_MAX_SIZE');
  650. $settings['retention_size'] = $redis->Get('Q_RETENTION_SIZE');
  651. $settings['release_format'] = $redis->Get('Q_RELEASE_FORMAT');
  652. $settings['subject'] = $redis->Get('Q_SUBJ');
  653. $settings['sender'] = $redis->Get('Q_SENDER');
  654. $settings['html_tmpl'] = htmlspecialchars($redis->Get('Q_HTML'));
  655. if (empty($settings['html_tmpl'])) {
  656. $settings['html_tmpl'] = htmlspecialchars(file_get_contents("/tpls/quarantine.tpl"));
  657. }
  658. }
  659. catch (RedisException $e) {
  660. $_SESSION['return'][] = array(
  661. 'type' => 'danger',
  662. 'log' => array(__FUNCTION__, $_action, $_data_log),
  663. 'msg' => array('redis_error', $e)
  664. );
  665. return false;
  666. }
  667. return $settings;
  668. break;
  669. case 'details':
  670. if (!is_numeric($_data) || empty($_data)) {
  671. return false;
  672. }
  673. $stmt = $pdo->prepare('SELECT `rcpt`, `symbols`, `msg`, `domain` FROM `quarantine` WHERE `id`= :id');
  674. $stmt->execute(array(':id' => $_data));
  675. $row = $stmt->fetch(PDO::FETCH_ASSOC);
  676. if (hasMailboxObjectAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $row['rcpt'])) {
  677. return $row;
  678. }
  679. return false;
  680. break;
  681. }
  682. }