qitem_details.php 2.7 KB

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