functions.quarantine.inc.php 28 KB

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