qitem_details.php 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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. if (!empty($_GET['id']) && ctype_alnum($_GET['id'])) {
  25. $tmpdir = '/tmp/' . $_GET['id'] . '/';
  26. $mailc = quarantine('details', $_GET['id']);
  27. if (strlen($mailc['msg']) > 10485760) {
  28. echo json_encode(array('error' => 'Message size exceeds 10 MiB.'));
  29. exit;
  30. }
  31. if (!empty($mailc['msg'])) {
  32. // Init message array
  33. $data = array();
  34. // Init parser
  35. $mail_parser = new PhpMimeMailParser\Parser();
  36. $html2text = new Html2Text\Html2Text();
  37. // Load msg to parser
  38. $mail_parser->setText($mailc['msg']);
  39. // Get text/plain content
  40. $data['text_plain'] = $mail_parser->getMessageBody('text');
  41. // Get html content and convert to text
  42. $data['text_html'] = $html2text->convert($mail_parser->getMessageBody('html'));
  43. if (empty($data['text_plain']) && empty($data['text_html'])) {
  44. // Failed to parse content, try raw
  45. $text = trim(substr($mailc['msg'], strpos($mailc['msg'], "\r\n\r\n") + 1));
  46. // Only return html->text
  47. $data['text_plain'] = 'Parser failed, assuming HTML';
  48. $data['text_html'] = $html2text->convert($text);
  49. }
  50. (empty($data['text_plain'])) ? $data['text_plain'] = '-' : null;
  51. // Get subject
  52. $data['subject'] = $mail_parser->getHeader('subject');
  53. (empty($data['subject'])) ? $data['subject'] = '-' : null;
  54. // Get attachments
  55. if (is_dir($tmpdir)) {
  56. rrmdir($tmpdir);
  57. }
  58. mkdir('/tmp/' . $_GET['id']);
  59. $mail_parser->saveAttachments($tmpdir, true);
  60. $atts = $mail_parser->getAttachments(true);
  61. if (count($atts) > 0) {
  62. foreach ($atts as $key => $val) {
  63. $data['attachments'][$key] = array(
  64. // Index
  65. // 0 => file name
  66. // 1 => mime type
  67. // 2 => file size
  68. // 3 => vt link by sha256
  69. $val->getFilename(),
  70. $val->getContentType(),
  71. filesize($tmpdir . $val->getFilename()),
  72. 'https://www.virustotal.com/file/' . hash_file('SHA256', $tmpdir . $val->getFilename()) . '/analysis/'
  73. );
  74. }
  75. }
  76. if (isset($_GET['att'])) {
  77. $dl_id = intval($_GET['att']);
  78. $dl_filename = $data['attachments'][$dl_id][0];
  79. if (!is_dir($tmpdir . $dl_filename) && file_exists($tmpdir . $dl_filename)) {
  80. header('Pragma: public');
  81. header('Expires: 0');
  82. header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
  83. header('Cache-Control: private', false);
  84. header('Content-Type: ' . $data['attachments'][$dl_id][1]);
  85. header('Content-Disposition: attachment; filename="'. $dl_filename . '";');
  86. header('Content-Transfer-Encoding: binary');
  87. header('Content-Length: ' . $data['attachments'][$dl_id][2]);
  88. readfile($tmpdir . $dl_filename);
  89. exit;
  90. }
  91. }
  92. echo json_encode($data);
  93. }
  94. }
  95. ?>