qitem_details.php 7.2 KB

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