import.php 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. $body = trim(imap_fetchbody($mbox, $m, '1'));
  22. if (substr($body, 0, 4) == "--b2") {
  23. $body = "";
  24. }
  25. array_push($rec, $body);
  26. $structure = imap_fetchstructure($mbox, $m);
  27. $attachments = array();
  28. if (isset($structure->parts) && count($structure->parts)) {
  29. for ($i = 0; $i < count($structure->parts); $i++) {
  30. $attachments[$i] = array(
  31. 'is_attachment' => false,
  32. 'filename' => '',
  33. 'name' => '',
  34. 'attachment' => ''
  35. );
  36. if ($structure->parts[$i]->ifdparameters) {
  37. foreach ($structure->parts[$i]->dparameters as $object) {
  38. if (strtolower($object->attribute) == 'filename') {
  39. $attachments[$i]['is_attachment'] = true;
  40. $attachments[$i]['filename'] = $object->value;
  41. }
  42. }
  43. }
  44. if ($structure->parts[$i]->ifparameters) {
  45. foreach ($structure->parts[$i]->parameters as $object) {
  46. if (strtolower($object->attribute) == 'name') {
  47. $attachments[$i]['is_attachment'] = true;
  48. $attachments[$i]['name'] = $object->value;
  49. }
  50. }
  51. }
  52. if ($attachments[$i]['is_attachment']) {
  53. $attachments[$i]['attachment'] = imap_fetchbody($mbox, $m, $i + 1);
  54. if ($structure->parts[$i]->encoding == 3) { // 3 = BASE64
  55. $attachments[$i]['attachment'] = base64_decode($attachments[$i]['attachment']);
  56. } elseif ($structure->parts[$i]->encoding == 4) { // 4 = QUOTED-PRINTABLE
  57. $attachments[$i]['attachment'] = quoted_printable_decode($attachments[$i]['attachment']);
  58. }
  59. }
  60. }
  61. }
  62. $a = "";
  63. foreach ($attachments as $key => $attachment) {
  64. if ($attachment['is_attachment'] && preg_match("/\.bat\.log/", $attachment['filename'])) {
  65. $a .= $attachment['attachment'];
  66. }
  67. }
  68. array_push($rec, addslashes($a));
  69. $dbh->query("INSERT INTO statusmail (kunde, start, ende, datum, anzahl, fehlerbericht, logdatei) VALUES ('" . implode("','", $rec) . "')");
  70. imap_delete($mbox, $m);
  71. if (!in_array($rec[0], $customers)) {
  72. $dbh->query("INSERT INTO kunden (kunde, start_soll, ende_soll, erster_status) VALUES ('{$rec[0]}', '{$rec[1]}', '{$rec[2]}', '{$rec[3]}')");
  73. array_push($customers, $rec[0]);
  74. }
  75. }
  76. imap_expunge($mbox);
  77. imap_close($mbox);
  78. echo json_encode($result);