qitem_details.php 7.8 KB

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