import.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. <?php
  2. header('Access-Control-Allow-Origin: *');
  3. header('Cache-Control: no-cache, must-revalidate');
  4. header('Content-type: application/json');
  5. $dbh = new PDO("mysql:host=localhost;dbname=tasks", "root", "gc01mysql");
  6. $dbh->setAttribute(PDO::ATTR_CASE, PDO::CASE_LOWER);
  7. $server = "{imap.1und1.de:993/imap/ssl}INBOX";
  8. $user = "status@global-cube.de";
  9. $passwd = "gc01smtp";
  10. function fehlerberichtAufbereiten ($fehlerbericht, &$fehlerAnzahl, $whitelist) {
  11. $fehlerbericht = str_replace("\r\`", "`", str_replace("\r\"", "\"", str_replace("\r\n", "", $fehlerbericht)));
  12. $fehlerbericht = json_decode($fehlerbericht, true);
  13. if ($whitelist) {
  14. if (!isset($whitelist['Layer'])) $whitelist['Layer'] = array();
  15. if (!isset($whitelist['Report'])) $whitelist['Report'] = array();
  16. if (!isset($whitelist['User'])) $whitelist['User'] = array();
  17. }
  18. foreach ($fehlerbericht as $i => $log) {
  19. if (preg_match("/\.bat$/", $log['Name'])) {
  20. $fehlerbericht[$i]['Errors'][0] = preg_replace("/!! Bitte E-Mailadresse fuer Statusbericht angeben !!/im", "! Bitte E-Mailadresse fuer Statusbericht angeben !", $fehlerbericht[$i]['Errors'][0]);
  21. $fehlerbericht[$i]['Errors'][0] = preg_replace("/!! SMTP-Konfiguration bitte anpassen !!/im", "! SMTP-Konfiguration bitte anpassen !", $fehlerbericht[$i]['Errors'][0]);
  22. $count = 0;
  23. $fehlerbericht[$i]['Errors'][0] = preg_replace("/(!![^\n]*!!)/im", "<b>$1</b>",$fehlerbericht[$i]['Errors'][0], -1, $count);
  24. $count2 = 0;
  25. $fehlerbericht[$i]['Errors'][0] = preg_replace("/(\(\w{2}\d{4}\) [^\r]*)/im", "<b>$1</b>", $fehlerbericht[$i]['Errors'][0], -1, $count2);
  26. //die(print($log['Errors'][0]));
  27. if ($count > 0 || $count2 > 0) {
  28. $fehlerbericht[$i]['ErrorLevel'] = 2;
  29. for ($j = 1; $j < $count; $j++) {
  30. $fehlerbericht[$i]['Errors'][] = "";
  31. }
  32. $fehlerAnzahl += 1;
  33. }
  34. } else if ($whitelist && count($fehlerbericht[$i]['Errors']) > 0 && ($fehlerbericht[$i]['Type'] == 'Versand' || $fehlerbericht[$i]['Type'] == 'Portal')) {
  35. $fehlerbericht[$i]['Errors2'] = array();
  36. foreach($fehlerbericht[$i]['Errors'] as $j => $e) {
  37. if (in_array($e['Layer'], $whitelist['Layer']) || in_array($e['Report'], $whitelist['Report']) || in_array($e['User'], $whitelist['User'])) {
  38. $fehlerbericht[$i]['Errors2'][] = $fehlerbericht[$i]['Errors'][$j];
  39. unset($fehlerbericht[$i]['Errors'][$j]);
  40. }
  41. }
  42. if (count($fehlerbericht[$i]['Errors']) == 0) {
  43. $fehlerbericht[$i]['ErrorLevel'] = 3;
  44. $fehlerAnzahl -= 1;
  45. }
  46. }
  47. }
  48. return addslashes(json_encode($fehlerbericht));
  49. }
  50. $q = $dbh->query("SELECT kunde, whitelist FROM kunden");
  51. $cust = $q->fetchAll(PDO::FETCH_ASSOC);
  52. $customers = array();
  53. foreach ($cust as $c) {
  54. $customers[$c['kunde']] = json_decode(stripslashes($c['whitelist']), true);
  55. }
  56. $mbox = imap_open($server, $user, $passwd) or die("Could not open Mailbox - try again later!");
  57. $message_count = imap_num_msg($mbox);
  58. $result = array();
  59. for ($m = 1; $m <= $message_count; $m++) {
  60. $fehlerAnzahl = 0;
  61. $header = imap_headerinfo($mbox, $m);
  62. $rec = explode(";", $header->Subject);
  63. if (count($rec) < 3) continue;
  64. $date = date("Y-m-d", strtotime($header->MailDate));
  65. $result[] = $date . " " . $rec[2] . " - " . $rec[0];
  66. $structure = imap_fetchstructure($mbox, $m);
  67. $attachments = array();
  68. if (isset($structure->parts) && count($structure->parts)) {
  69. for ($i = 0; $i < count($structure->parts); $i++) {
  70. $attachments[$i] = array(
  71. 'is_attachment' => false,
  72. 'filename' => '',
  73. 'name' => '',
  74. 'attachment' => ''
  75. );
  76. if ($structure->parts[$i]->ifdparameters) {
  77. foreach ($structure->parts[$i]->dparameters as $object) {
  78. if (strtolower($object->attribute) == 'filename') {
  79. $attachments[$i]['is_attachment'] = true;
  80. $attachments[$i]['filename'] = $object->value;
  81. }
  82. }
  83. }
  84. if ($structure->parts[$i]->ifparameters) {
  85. foreach ($structure->parts[$i]->parameters as $object) {
  86. if (strtolower($object->attribute) == 'name') {
  87. $attachments[$i]['is_attachment'] = true;
  88. $attachments[$i]['name'] = $object->value;
  89. }
  90. }
  91. }
  92. if ($attachments[$i]['is_attachment']) {
  93. $attachments[$i]['attachment'] = imap_fetchbody($mbox, $m, $i + 1);
  94. if ($structure->parts[$i]->encoding == 3) { // 3 = BASE64
  95. $attachments[$i]['attachment'] = base64_decode($attachments[$i]['attachment']);
  96. } elseif ($structure->parts[$i]->encoding == 4) { // 4 = QUOTED-PRINTABLE
  97. $attachments[$i]['attachment'] = quoted_printable_decode($attachments[$i]['attachment']);
  98. }
  99. }
  100. }
  101. }
  102. $fehlerbericht = addslashes(trim(imap_fetchbody($mbox, $m, '1')));
  103. $info = 0;
  104. foreach ($attachments as $key => $attachment) {
  105. if ($attachment['is_attachment']) {
  106. if ($attachment['filename'] == "info.json") {
  107. $info = addslashes($attachment['attachment']);
  108. }
  109. if ($attachment['filename'] == "fehlerbericht.json") {
  110. $fehlerbericht = fehlerberichtAufbereiten($attachment['attachment'], $fehlerAnzahl, $customers[$rec[0]]);
  111. }
  112. }
  113. }
  114. $rec[] = $fehlerbericht;
  115. $rec[4] += $fehlerAnzahl;
  116. $insertQuery = "INSERT INTO status_meldung (kunde, start, ende, datum, anzahl, fehlerbericht) VALUES ('" . implode("','", $rec) . "')";
  117. $c = $dbh->query($insertQuery);
  118. if ($c) {
  119. imap_delete($mbox, $m);
  120. } else {
  121. $result[] = $insertQuery;
  122. $result[] = $dbh->errorInfo();
  123. }
  124. if (!isset($customers[$rec[0]])) {
  125. $dbh->query("INSERT INTO kunden (kunde, start_soll, ende_soll, erster_status, system) VALUES ('{$rec[0]}', '{$rec[1]}', '{$rec[2]}', '{$rec[3]}', '?')");
  126. $customers[$rec[0]] = array();
  127. }
  128. if ($info) {
  129. $q = $dbh->query("SELECT * FROM kunden_config WHERE kunde = '{$rec[0]}' ORDER BY datum DESC LIMIT 1");
  130. $last = ($q && $q->rowCount() == 1) ? $q->fetch(PDO::FETCH_ASSOC) : false;
  131. if (!$last || addslashes($last['info']) != $info) {
  132. $dbh->query("INSERT INTO kunden_config (kunde, datum, info) VALUES ('{$rec[0]}', '{$rec[3]}', '{$info}')");
  133. }
  134. }
  135. }
  136. imap_expunge($mbox);
  137. imap_close($mbox);
  138. echo json_encode($result);