qitem_details.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. <?php
  2. session_start();
  3. header("Content-Type: application/json");
  4. require_once $_SERVER['DOCUMENT_ROOT'] . '/inc/prerequisites.inc.php';
  5. function rrmdir($src) {
  6. $dir = opendir($src);
  7. while(false !== ( $file = readdir($dir)) ) {
  8. if (( $file != '.' ) && ( $file != '..' )) {
  9. $full = $src . '/' . $file;
  10. if ( is_dir($full) ) {
  11. rrmdir($full);
  12. }
  13. else {
  14. unlink($full);
  15. }
  16. }
  17. }
  18. closedir($dir);
  19. rmdir($src);
  20. }
  21. function addAddresses(&$list, $mail, $headerName) {
  22. $addresses = $mail->getAddresses($headerName);
  23. foreach ($addresses as $address) {
  24. if (filter_var($address['address'], FILTER_VALIDATE_EMAIL)) {
  25. $list[] = array('address' => $address['address'], 'type' => $headerName);
  26. }
  27. }
  28. }
  29. if (!empty($_GET['hash']) && ctype_alnum($_GET['hash'])) {
  30. $mailc = quarantine('hash_details', $_GET['hash']);
  31. if ($mailc === false) {
  32. echo json_encode(array('error' => 'Message invalid'));
  33. exit;
  34. }
  35. if (strlen($mailc['msg']) > 10485760) {
  36. echo json_encode(array('error' => 'Message size exceeds 10 MiB.'));
  37. exit;
  38. }
  39. if (!empty($mailc['msg'])) {
  40. // Init message array
  41. $data = array();
  42. // Init parser
  43. $mail_parser = new PhpMimeMailParser\Parser();
  44. $html2text = new Html2Text\Html2Text();
  45. // Load msg to parser
  46. $mail_parser->setText($mailc['msg']);
  47. // Get mail recipients
  48. {
  49. $recipientsList = array();
  50. addAddresses($recipientsList, $mail_parser, 'to');
  51. addAddresses($recipientsList, $mail_parser, 'cc');
  52. addAddresses($recipientsList, $mail_parser, 'bcc');
  53. $recipientsList[] = array('address' => $mailc['rcpt'], 'type' => 'smtp');
  54. $data['recipients'] = $recipientsList;
  55. }
  56. // Get from
  57. $data['header_from'] = $mail_parser->getHeader('from');
  58. $data['env_from'] = $mailc['sender'];
  59. // Get rspamd score
  60. $data['score'] = $mailc['score'];
  61. // Get rspamd action
  62. $data['action'] = $mailc['action'];
  63. // Get rspamd symbols
  64. $data['symbols'] = json_decode($mailc['symbols']);
  65. // Get fuzzy hashes
  66. $data['fuzzy_hashes'] = json_decode($mailc['fuzzy_hashes']);
  67. $data['subject'] = $mail_parser->getHeader('subject');
  68. (empty($data['subject'])) ? $data['subject'] = '-' : null;
  69. echo json_encode($data);
  70. }
  71. }
  72. elseif (!empty($_GET['id']) && ctype_alnum($_GET['id'])) {
  73. if (!isset($_SESSION['mailcow_cc_role'])) {
  74. echo json_encode(array('error' => 'Access denied'));
  75. exit();
  76. }
  77. $tmpdir = '/tmp/' . $_GET['id'] . '/';
  78. $mailc = quarantine('details', $_GET['id']);
  79. if ($mailc === false) {
  80. echo json_encode(array('error' => 'Access denied'));
  81. exit;
  82. }
  83. if (strlen($mailc['msg']) > 10485760) {
  84. echo json_encode(array('error' => 'Message size exceeds 10 MiB.'));
  85. exit;
  86. }
  87. if (!empty($mailc['msg'])) {
  88. if (isset($_GET['quick_release'])) {
  89. $hash = hash('sha256', $mailc['id'] . $mailc['qid']);
  90. header('Location: /qhandler/release/' . $hash);
  91. exit;
  92. }
  93. if (isset($_GET['quick_delete'])) {
  94. $hash = hash('sha256', $mailc['id'] . $mailc['qid']);
  95. header('Location: /qhandler/delete/' . $hash);
  96. exit;
  97. }
  98. // Init message array
  99. $data = array();
  100. // Init parser
  101. $mail_parser = new PhpMimeMailParser\Parser();
  102. $html2text = new Html2Text\Html2Text();
  103. // Load msg to parser
  104. $mail_parser->setText($mailc['msg']);
  105. // Get mail recipients
  106. {
  107. $recipientsList = array();
  108. addAddresses($recipientsList, $mail_parser, 'to');
  109. addAddresses($recipientsList, $mail_parser, 'cc');
  110. addAddresses($recipientsList, $mail_parser, 'bcc');
  111. $recipientsList[] = array('address' => $mailc['rcpt'], 'type' => 'smtp');
  112. $data['recipients'] = $recipientsList;
  113. }
  114. // Get from
  115. $data['header_from'] = $mail_parser->getHeader('from');
  116. $data['env_from'] = $mailc['sender'];
  117. // Get rspamd score
  118. $data['score'] = $mailc['score'];
  119. // Get rspamd action
  120. $data['action'] = $mailc['action'];
  121. // Get rspamd symbols
  122. $data['symbols'] = json_decode($mailc['symbols']);
  123. // Get fuzzy hashes
  124. $data['fuzzy_hashes'] = json_decode($mailc['fuzzy_hashes']);
  125. // Get text/plain content
  126. $data['text_plain'] = $mail_parser->getMessageBody('text');
  127. // Get html content and convert to text
  128. $data['text_html'] = $html2text->convert($mail_parser->getMessageBody('html'));
  129. if (empty($data['text_plain']) && empty($data['text_html'])) {
  130. // Failed to parse content, try raw
  131. $text = trim(substr($mailc['msg'], strpos($mailc['msg'], "\r\n\r\n") + 1));
  132. // Only return html->text
  133. $data['text_plain'] = 'Parser failed, assuming HTML';
  134. $data['text_html'] = $html2text->convert($text);
  135. }
  136. (empty($data['text_plain'])) ? $data['text_plain'] = '-' : null;
  137. // Get subject
  138. $data['subject'] = $mail_parser->getHeader('subject');
  139. (empty($data['subject'])) ? $data['subject'] = '-' : null;
  140. // Get attachments
  141. if (is_dir($tmpdir)) {
  142. rrmdir($tmpdir);
  143. }
  144. mkdir('/tmp/' . $_GET['id']);
  145. $mail_parser->saveAttachments($tmpdir, true);
  146. $atts = $mail_parser->getAttachments(true);
  147. if (count($atts) > 0) {
  148. foreach ($atts as $key => $val) {
  149. $data['attachments'][$key] = array(
  150. // Index
  151. // 0 => file name
  152. // 1 => mime type
  153. // 2 => file size
  154. // 3 => vt link by sha256
  155. $val->getFilename(),
  156. $val->getContentType(),
  157. filesize($tmpdir . $val->getFilename()),
  158. 'https://www.virustotal.com/file/' . hash_file('SHA256', $tmpdir . $val->getFilename()) . '/analysis/'
  159. );
  160. }
  161. }
  162. if (isset($_GET['eml'])) {
  163. $dl_filename = filter_var($data['subject'], FILTER_SANITIZE_STRING);
  164. $dl_filename = strlen($dl_filename) > 30 ? substr($dl_filename,0,30) : $dl_filename;
  165. header('Pragma: public');
  166. header('Expires: 0');
  167. header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
  168. header('Cache-Control: private', false);
  169. header('Content-Type: message/rfc822');
  170. header('Content-Disposition: attachment; filename="'. $dl_filename . '.eml";');
  171. header('Content-Transfer-Encoding: binary');
  172. header('Content-Length: ' . strlen($mailc['msg']));
  173. echo $mailc['msg'];
  174. exit;
  175. }
  176. if (isset($_GET['att'])) {
  177. if ($_SESSION['acl']['quarantine_attachments'] == 0) {
  178. exit(json_encode('Forbidden'));
  179. }
  180. $dl_id = intval($_GET['att']);
  181. $dl_filename = filter_var($data['attachments'][$dl_id][0], FILTER_SANITIZE_STRING);
  182. $dl_filename = strlen($dl_filename) > 30 ? substr($dl_filename,0,30) : $dl_filename;
  183. if (!is_dir($tmpdir . $dl_filename) && file_exists($tmpdir . $dl_filename)) {
  184. header('Pragma: public');
  185. header('Expires: 0');
  186. header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
  187. header('Cache-Control: private', false);
  188. header('Content-Type: ' . $data['attachments'][$dl_id][1]);
  189. header('Content-Disposition: attachment; filename="'. $dl_filename . '";');
  190. header('Content-Transfer-Encoding: binary');
  191. header('Content-Length: ' . $data['attachments'][$dl_id][2]);
  192. readfile($tmpdir . $dl_filename);
  193. exit;
  194. }
  195. }
  196. echo json_encode($data);
  197. }
  198. }
  199. ?>