qitem_details.php 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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. (empty($data['text_plain'])) ? $data['text_plain'] = '-' : null;
  44. // Get subject
  45. $data['subject'] = $mail_parser->getHeader('subject');
  46. (empty($data['subject'])) ? $data['subject'] = '-' : null;
  47. // Get attachments
  48. if (is_dir($tmpdir)) {
  49. rrmdir($tmpdir);
  50. }
  51. mkdir('/tmp/' . $_GET['id']);
  52. $mail_parser->saveAttachments($tmpdir, true);
  53. $atts = $mail_parser->getAttachments(true);
  54. if (count($atts) > 0) {
  55. foreach ($atts as $key => $val) {
  56. $data['attachments'][$key] = array(
  57. // Index
  58. // 0 => file name
  59. // 1 => mime type
  60. // 2 => file size
  61. // 3 => vt link by sha256
  62. $val->getFilename(),
  63. $val->getContentType(),
  64. filesize($tmpdir . $val->getFilename()),
  65. 'https://www.virustotal.com/file/' . hash_file('SHA256', $tmpdir . $val->getFilename()) . '/analysis/'
  66. );
  67. }
  68. }
  69. if (isset($_GET['att'])) {
  70. $dl_id = intval($_GET['att']);
  71. $dl_filename = $data['attachments'][$dl_id][0];
  72. if (!is_dir($tmpdir . $dl_filename) && file_exists($tmpdir . $dl_filename)) {
  73. header('Pragma: public');
  74. header('Expires: 0');
  75. header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
  76. header('Cache-Control: private', false);
  77. header('Content-Type: ' . $data['attachments'][$dl_id][1]);
  78. header('Content-Disposition: attachment; filename="'. $dl_filename . '";');
  79. header('Content-Transfer-Encoding: binary');
  80. header('Content-Length: ' . $data['attachments'][$dl_id][2]);
  81. readfile($tmpdir . $dl_filename);
  82. exit;
  83. }
  84. }
  85. echo json_encode($data);
  86. }
  87. }
  88. ?>