qitem_details.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. <?php
  2. session_start();
  3. header("Content-Type: application/json");
  4. require_once $_SERVER['DOCUMENT_ROOT'] . '/inc/prerequisites.inc.php';
  5. if (!isset($_SESSION['mailcow_cc_role'])) {
  6. exit();
  7. }
  8. function rrmdir($src) {
  9. $dir = opendir($src);
  10. while(false !== ( $file = readdir($dir)) ) {
  11. if (( $file != '.' ) && ( $file != '..' )) {
  12. $full = $src . '/' . $file;
  13. if ( is_dir($full) ) {
  14. rrmdir($full);
  15. }
  16. else {
  17. unlink($full);
  18. }
  19. }
  20. }
  21. closedir($dir);
  22. rmdir($src);
  23. }
  24. function addAddresses(&$list, $mail, $headerName) {
  25. $addresses = $mail->getAddresses($headerName);
  26. foreach ($addresses as $address) {
  27. $list[] = array('address' => $address['address'], 'type' => $headerName);
  28. }
  29. }
  30. if (!empty($_GET['id']) && ctype_alnum($_GET['id'])) {
  31. $tmpdir = '/tmp/' . $_GET['id'] . '/';
  32. $mailc = quarantine('details', $_GET['id']);
  33. if (strlen($mailc['msg']) > 10485760) {
  34. echo json_encode(array('error' => 'Message size exceeds 10 MiB.'));
  35. exit;
  36. }
  37. if (!empty($mailc['msg'])) {
  38. // Init message array
  39. $data = array();
  40. // Init parser
  41. $mail_parser = new PhpMimeMailParser\Parser();
  42. $html2text = new Html2Text\Html2Text();
  43. // Load msg to parser
  44. $mail_parser->setText($mailc['msg']);
  45. // Get mail recipients
  46. {
  47. $recipientsList = array();
  48. addAddresses($recipientsList, $mail_parser, 'to');
  49. addAddresses($recipientsList, $mail_parser, 'cc');
  50. addAddresses($recipientsList, $mail_parser, 'bcc');
  51. $data['recipients'] = $recipientsList;
  52. }
  53. // Get rspamd score
  54. $data['score'] = $mailc['score'];
  55. // Get rspamd symbols
  56. $data['symbols'] = json_decode($mailc['symbols']);
  57. // Get text/plain content
  58. $data['text_plain'] = $mail_parser->getMessageBody('text');
  59. // Get html content and convert to text
  60. $data['text_html'] = $html2text->convert($mail_parser->getMessageBody('html'));
  61. if (empty($data['text_plain']) && empty($data['text_html'])) {
  62. // Failed to parse content, try raw
  63. $text = trim(substr($mailc['msg'], strpos($mailc['msg'], "\r\n\r\n") + 1));
  64. // Only return html->text
  65. $data['text_plain'] = 'Parser failed, assuming HTML';
  66. $data['text_html'] = $html2text->convert($text);
  67. }
  68. (empty($data['text_plain'])) ? $data['text_plain'] = '-' : null;
  69. // Get subject
  70. $data['subject'] = $mail_parser->getHeader('subject');
  71. (empty($data['subject'])) ? $data['subject'] = '-' : null;
  72. // Get attachments
  73. if (is_dir($tmpdir)) {
  74. rrmdir($tmpdir);
  75. }
  76. mkdir('/tmp/' . $_GET['id']);
  77. $mail_parser->saveAttachments($tmpdir, true);
  78. $atts = $mail_parser->getAttachments(true);
  79. if (count($atts) > 0) {
  80. foreach ($atts as $key => $val) {
  81. $data['attachments'][$key] = array(
  82. // Index
  83. // 0 => file name
  84. // 1 => mime type
  85. // 2 => file size
  86. // 3 => vt link by sha256
  87. $val->getFilename(),
  88. $val->getContentType(),
  89. filesize($tmpdir . $val->getFilename()),
  90. 'https://www.virustotal.com/file/' . hash_file('SHA256', $tmpdir . $val->getFilename()) . '/analysis/'
  91. );
  92. }
  93. }
  94. if (isset($_GET['eml'])) {
  95. $dl_filename = str_replace('/', '_', $data['subject']);
  96. header('Pragma: public');
  97. header('Expires: 0');
  98. header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
  99. header('Cache-Control: private', false);
  100. header('Content-Type: message/rfc822');
  101. header('Content-Disposition: attachment; filename="'. $dl_filename . '.eml";');
  102. header('Content-Transfer-Encoding: binary');
  103. header('Content-Length: ' . strlen($mailc['msg']));
  104. echo $mailc['msg'];
  105. exit;
  106. }
  107. if (isset($_GET['att'])) {
  108. if ($_SESSION['acl']['quarantine_attachments'] == 0) {
  109. exit(json_encode('Forbidden'));
  110. }
  111. $dl_id = intval($_GET['att']);
  112. $dl_filename = $data['attachments'][$dl_id][0];
  113. if (!is_dir($tmpdir . $dl_filename) && file_exists($tmpdir . $dl_filename)) {
  114. header('Pragma: public');
  115. header('Expires: 0');
  116. header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
  117. header('Cache-Control: private', false);
  118. header('Content-Type: ' . $data['attachments'][$dl_id][1]);
  119. header('Content-Disposition: attachment; filename="'. $dl_filename . '";');
  120. header('Content-Transfer-Encoding: binary');
  121. header('Content-Length: ' . $data['attachments'][$dl_id][2]);
  122. readfile($tmpdir . $dl_filename);
  123. exit;
  124. }
  125. }
  126. echo json_encode($data);
  127. }
  128. }
  129. ?>