qitem_details.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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 symbols
  62. $data['symbols'] = json_decode($mailc['symbols']);
  63. $data['subject'] = $mail_parser->getHeader('subject');
  64. (empty($data['subject'])) ? $data['subject'] = '-' : null;
  65. echo json_encode($data);
  66. }
  67. }
  68. elseif (!empty($_GET['id']) && ctype_alnum($_GET['id'])) {
  69. if (!isset($_SESSION['mailcow_cc_role'])) {
  70. echo json_encode(array('error' => 'Access denied'));
  71. exit();
  72. }
  73. $tmpdir = '/tmp/' . $_GET['id'] . '/';
  74. $mailc = quarantine('details', $_GET['id']);
  75. if ($mailc === false) {
  76. echo json_encode(array('error' => 'Access denied'));
  77. exit;
  78. }
  79. if (strlen($mailc['msg']) > 10485760) {
  80. echo json_encode(array('error' => 'Message size exceeds 10 MiB.'));
  81. exit;
  82. }
  83. if (!empty($mailc['msg'])) {
  84. if (isset($_GET['quick_release'])) {
  85. $hash = hash('sha256', $mailc['id'] . $mailc['qid']);
  86. header('Location: /qhandler/release/' . $hash);
  87. exit;
  88. }
  89. if (isset($_GET['quick_delete'])) {
  90. $hash = hash('sha256', $mailc['id'] . $mailc['qid']);
  91. header('Location: /qhandler/delete/' . $hash);
  92. exit;
  93. }
  94. // Init message array
  95. $data = array();
  96. // Init parser
  97. $mail_parser = new PhpMimeMailParser\Parser();
  98. $html2text = new Html2Text\Html2Text();
  99. // Load msg to parser
  100. $mail_parser->setText($mailc['msg']);
  101. // Get mail recipients
  102. {
  103. $recipientsList = array();
  104. addAddresses($recipientsList, $mail_parser, 'to');
  105. addAddresses($recipientsList, $mail_parser, 'cc');
  106. addAddresses($recipientsList, $mail_parser, 'bcc');
  107. $recipientsList[] = array('address' => $mailc['rcpt'], 'type' => 'smtp');
  108. $data['recipients'] = $recipientsList;
  109. }
  110. // Get from
  111. $data['header_from'] = $mail_parser->getHeader('from');
  112. $data['env_from'] = $mailc['sender'];
  113. // Get rspamd score
  114. $data['score'] = $mailc['score'];
  115. // Get rspamd symbols
  116. $data['symbols'] = json_decode($mailc['symbols']);
  117. // Get text/plain content
  118. $data['text_plain'] = $mail_parser->getMessageBody('text');
  119. // Get html content and convert to text
  120. $data['text_html'] = $html2text->convert($mail_parser->getMessageBody('html'));
  121. if (empty($data['text_plain']) && empty($data['text_html'])) {
  122. // Failed to parse content, try raw
  123. $text = trim(substr($mailc['msg'], strpos($mailc['msg'], "\r\n\r\n") + 1));
  124. // Only return html->text
  125. $data['text_plain'] = 'Parser failed, assuming HTML';
  126. $data['text_html'] = $html2text->convert($text);
  127. }
  128. (empty($data['text_plain'])) ? $data['text_plain'] = '-' : null;
  129. // Get subject
  130. $data['subject'] = $mail_parser->getHeader('subject');
  131. (empty($data['subject'])) ? $data['subject'] = '-' : null;
  132. // Get attachments
  133. if (is_dir($tmpdir)) {
  134. rrmdir($tmpdir);
  135. }
  136. mkdir('/tmp/' . $_GET['id']);
  137. $mail_parser->saveAttachments($tmpdir, true);
  138. $atts = $mail_parser->getAttachments(true);
  139. if (count($atts) > 0) {
  140. foreach ($atts as $key => $val) {
  141. $data['attachments'][$key] = array(
  142. // Index
  143. // 0 => file name
  144. // 1 => mime type
  145. // 2 => file size
  146. // 3 => vt link by sha256
  147. $val->getFilename(),
  148. $val->getContentType(),
  149. filesize($tmpdir . $val->getFilename()),
  150. 'https://www.virustotal.com/file/' . hash_file('SHA256', $tmpdir . $val->getFilename()) . '/analysis/'
  151. );
  152. }
  153. }
  154. if (isset($_GET['eml'])) {
  155. $dl_filename = filter_var($data['subject'], FILTER_SANITIZE_STRING);
  156. $dl_filename = strlen($dl_filename) > 30 ? substr($dl_filename,0,30) : $dl_filename;
  157. header('Pragma: public');
  158. header('Expires: 0');
  159. header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
  160. header('Cache-Control: private', false);
  161. header('Content-Type: message/rfc822');
  162. header('Content-Disposition: attachment; filename="'. $dl_filename . '.eml";');
  163. header('Content-Transfer-Encoding: binary');
  164. header('Content-Length: ' . strlen($mailc['msg']));
  165. echo $mailc['msg'];
  166. exit;
  167. }
  168. if (isset($_GET['att'])) {
  169. if ($_SESSION['acl']['quarantine_attachments'] == 0) {
  170. exit(json_encode('Forbidden'));
  171. }
  172. $dl_id = intval($_GET['att']);
  173. $dl_filename = filter_var($data['attachments'][$dl_id][0], FILTER_SANITIZE_STRING);
  174. $dl_filename = strlen($dl_filename) > 30 ? substr($dl_filename,0,30) : $dl_filename;
  175. if (!is_dir($tmpdir . $dl_filename) && file_exists($tmpdir . $dl_filename)) {
  176. header('Pragma: public');
  177. header('Expires: 0');
  178. header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
  179. header('Cache-Control: private', false);
  180. header('Content-Type: ' . $data['attachments'][$dl_id][1]);
  181. header('Content-Disposition: attachment; filename="'. $dl_filename . '";');
  182. header('Content-Transfer-Encoding: binary');
  183. header('Content-Length: ' . $data['attachments'][$dl_id][2]);
  184. readfile($tmpdir . $dl_filename);
  185. exit;
  186. }
  187. }
  188. echo json_encode($data);
  189. }
  190. }
  191. ?>