import.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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. $q = $dbh->query("SELECT kunde FROM kunden");
  11. $customers = $q->fetchAll(PDO::FETCH_COLUMN, 0);
  12. $mbox = imap_open($server, $user, $passwd) or die("Could not open Mailbox - try again later!");
  13. $message_count = imap_num_msg($mbox);
  14. $result = array();
  15. for ($m = 1; $m <= $message_count; ++$m) {
  16. $header = imap_headerinfo($mbox, $m);
  17. $rec = explode(";", $header->Subject);
  18. if (count($rec) < 3) continue;
  19. $date = date("Y-m-d", strtotime($header->MailDate));
  20. $result[] = $date . " " . $rec[2] . " - " . $rec[0];
  21. $structure = imap_fetchstructure($mbox, $m);
  22. $attachments = array();
  23. if (isset($structure->parts) && count($structure->parts)) {
  24. for ($i = 0; $i < count($structure->parts); $i++) {
  25. $attachments[$i] = array(
  26. 'is_attachment' => false,
  27. 'filename' => '',
  28. 'name' => '',
  29. 'attachment' => ''
  30. );
  31. if ($structure->parts[$i]->ifdparameters) {
  32. foreach ($structure->parts[$i]->dparameters as $object) {
  33. if (strtolower($object->attribute) == 'filename') {
  34. $attachments[$i]['is_attachment'] = true;
  35. $attachments[$i]['filename'] = $object->value;
  36. }
  37. }
  38. }
  39. if ($structure->parts[$i]->ifparameters) {
  40. foreach ($structure->parts[$i]->parameters as $object) {
  41. if (strtolower($object->attribute) == 'name') {
  42. $attachments[$i]['is_attachment'] = true;
  43. $attachments[$i]['name'] = $object->value;
  44. }
  45. }
  46. }
  47. if ($attachments[$i]['is_attachment']) {
  48. $attachments[$i]['attachment'] = imap_fetchbody($mbox, $m, $i + 1);
  49. if ($structure->parts[$i]->encoding == 3) { // 3 = BASE64
  50. $attachments[$i]['attachment'] = base64_decode($attachments[$i]['attachment']);
  51. } elseif ($structure->parts[$i]->encoding == 4) { // 4 = QUOTED-PRINTABLE
  52. $attachments[$i]['attachment'] = quoted_printable_decode($attachments[$i]['attachment']);
  53. }
  54. }
  55. }
  56. }
  57. $fehlerbericht = addslashes(trim(imap_fetchbody($mbox, $m, '1')));
  58. $info = 0;
  59. foreach ($attachments as $key => $attachment) {
  60. if ($attachment['is_attachment']) {
  61. if ($attachment['filename'] == "info.json") {
  62. $info = addslashes($attachment['attachment']);
  63. }
  64. if ($attachment['filename'] == "fehlerbericht.json") {
  65. $fehlerbericht = addslashes($attachment['attachment']);
  66. }
  67. }
  68. }
  69. $rec[] = $fehlerbericht;
  70. $insertQuery = "INSERT INTO status_meldung (kunde, start, ende, datum, anzahl, fehlerbericht) VALUES ('" . implode("','", $rec) . "')";
  71. $c = $dbh->query($insertQuery);
  72. if ($c) {
  73. imap_delete($mbox, $m);
  74. } else {
  75. $result[] = $insertQuery;
  76. $result[] = $dbh->errorInfo();
  77. }
  78. if (!in_array($rec[0], $customers)) {
  79. $dbh->query("INSERT INTO kunden (kunde, start_soll, ende_soll, erster_status, system) VALUES ('{$rec[0]}', '{$rec[1]}', '{$rec[2]}', '{$rec[3]}', '?')");
  80. array_push($customers, $rec[0]);
  81. }
  82. if ($info) {
  83. $q = $dbh->query("SELECT * FROM kunden_config WHERE kunde = '{$rec[0]}' ORDER BY datum DESC LIMIT 1");
  84. $last = ($q && $q->rowCount() == 1) ? $q->fetch(PDO::FETCH_ASSOC) : false;
  85. if (!$last || addslashes($last['info']) != $info) {
  86. $dbh->query("INSERT INTO kunden_config (kunde, datum, info) VALUES ('{$rec[0]}', '{$rec[3]}', '{$info}')");
  87. }
  88. }
  89. }
  90. imap_expunge($mbox);
  91. imap_close($mbox);
  92. echo json_encode($result);