functions.quarantine.inc.php 27 KB

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