123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164 |
- <?php
- header('Access-Control-Allow-Origin: *');
- header('Cache-Control: no-cache, must-revalidate');
- header('Content-type: application/json');
- $dbh = new PDO("mysql:host=localhost;dbname=tasks", "root", "gc01mysql");
- $dbh->setAttribute(PDO::ATTR_CASE, PDO::CASE_LOWER);
- $server = "{imap.1und1.de:993/imap/ssl}INBOX";
- $user = "status@global-cube.de";
- $passwd = "gc01smtp";
- function fehlerberichtAufbereiten ($fehlerbericht, &$fehlerAnzahl, $whitelist) {
- $fehlerbericht = str_replace("\r\`", "`", str_replace("\r\"", "\"", str_replace("\r\n", "", $fehlerbericht)));
- $fehlerbericht = json_decode($fehlerbericht, true);
- if ($whitelist) {
- if (!isset($whitelist['Layer'])) $whitelist['Layer'] = array();
- if (!isset($whitelist['Report'])) $whitelist['Report'] = array();
- if (!isset($whitelist['User'])) $whitelist['User'] = array();
- }
- foreach ($fehlerbericht as $i => $log) {
- if (preg_match("/\.bat$/", $log['Name'])) {
- $fehlerbericht[$i]['Errors'][0] = preg_replace("/!! Bitte E-Mailadresse fuer Statusbericht angeben !!/im", "! Bitte E-Mailadresse fuer Statusbericht angeben !", $fehlerbericht[$i]['Errors'][0]);
- $fehlerbericht[$i]['Errors'][0] = preg_replace("/!! SMTP-Konfiguration bitte anpassen !!/im", "! SMTP-Konfiguration bitte anpassen !", $fehlerbericht[$i]['Errors'][0]);
- $count = 0;
- $fehlerbericht[$i]['Errors'][0] = preg_replace("/(!![^\n]*!!)/im", "<b>$1</b>",$fehlerbericht[$i]['Errors'][0], -1, $count);
- $count2 = 0;
- $fehlerbericht[$i]['Errors'][0] = preg_replace("/(\(\w{2}\d{4}\) [^\r]*)/im", "<b>$1</b>", $fehlerbericht[$i]['Errors'][0], -1, $count2);
- //die(print($log['Errors'][0]));
- if ($count > 0 || $count2 > 0) {
- $fehlerbericht[$i]['ErrorLevel'] = 2;
- for ($j = 1; $j < $count; $j++) {
- $fehlerbericht[$i]['Errors'][] = "";
- }
- $fehlerAnzahl += 1;
- }
- } else if ($whitelist && count($fehlerbericht[$i]['Errors']) > 0 && ($fehlerbericht[$i]['Type'] == 'Versand' || $fehlerbericht[$i]['Type'] == 'Portal')) {
- $fehlerbericht[$i]['Errors2'] = array();
- foreach($fehlerbericht[$i]['Errors'] as $j => $e) {
- if (in_array($e['Layer'], $whitelist['Layer']) || in_array($e['Report'], $whitelist['Report']) || in_array($e['User'], $whitelist['User'])) {
- $fehlerbericht[$i]['Errors2'][] = $fehlerbericht[$i]['Errors'][$j];
- unset($fehlerbericht[$i]['Errors'][$j]);
- }
- }
- if (count($fehlerbericht[$i]['Errors']) == 0) {
- $fehlerbericht[$i]['ErrorLevel'] = 3;
- $fehlerAnzahl -= 1;
- }
- }
- }
- return addslashes(json_encode($fehlerbericht));
- }
- $q = $dbh->query("SELECT kunde, whitelist FROM kunden");
- $cust = $q->fetchAll(PDO::FETCH_ASSOC);
- $customers = array();
- foreach ($cust as $c) {
- $customers[$c['kunde']] = json_decode(stripslashes($c['whitelist']), true);
- }
- $mbox = imap_open($server, $user, $passwd) or die("Could not open Mailbox - try again later!");
- $message_count = imap_num_msg($mbox);
- $result = array();
- for ($m = 1; $m <= $message_count; $m++) {
- $fehlerAnzahl = 0;
- $header = imap_headerinfo($mbox, $m);
- $rec = explode(";", $header->Subject);
- if (count($rec) < 3) continue;
- $date = date("Y-m-d", strtotime($header->MailDate));
- $result[] = $date . " " . $rec[2] . " - " . $rec[0];
- $structure = imap_fetchstructure($mbox, $m);
- $attachments = array();
- if (isset($structure->parts) && count($structure->parts)) {
- for ($i = 0; $i < count($structure->parts); $i++) {
- $attachments[$i] = array(
- 'is_attachment' => false,
- 'filename' => '',
- 'name' => '',
- 'attachment' => ''
- );
- if ($structure->parts[$i]->ifdparameters) {
- foreach ($structure->parts[$i]->dparameters as $object) {
- if (strtolower($object->attribute) == 'filename') {
- $attachments[$i]['is_attachment'] = true;
- $attachments[$i]['filename'] = $object->value;
- }
- }
- }
- if ($structure->parts[$i]->ifparameters) {
- foreach ($structure->parts[$i]->parameters as $object) {
- if (strtolower($object->attribute) == 'name') {
- $attachments[$i]['is_attachment'] = true;
- $attachments[$i]['name'] = $object->value;
- }
- }
- }
- if ($attachments[$i]['is_attachment']) {
- $attachments[$i]['attachment'] = imap_fetchbody($mbox, $m, $i + 1);
- if ($structure->parts[$i]->encoding == 3) { // 3 = BASE64
- $attachments[$i]['attachment'] = base64_decode($attachments[$i]['attachment']);
- } elseif ($structure->parts[$i]->encoding == 4) { // 4 = QUOTED-PRINTABLE
- $attachments[$i]['attachment'] = quoted_printable_decode($attachments[$i]['attachment']);
- }
- }
- }
- }
- $fehlerbericht = addslashes(trim(imap_fetchbody($mbox, $m, '1')));
- $info = 0;
- foreach ($attachments as $key => $attachment) {
- if ($attachment['is_attachment']) {
- if ($attachment['filename'] == "info.json") {
- $info = addslashes($attachment['attachment']);
- }
- if ($attachment['filename'] == "fehlerbericht.json") {
- $fehlerbericht = fehlerberichtAufbereiten($attachment['attachment'], $fehlerAnzahl, $customers[$rec[0]]);
- }
- }
- }
- $rec[] = $fehlerbericht;
- $rec[4] += $fehlerAnzahl;
- $insertQuery = "INSERT INTO status_meldung (kunde, start, ende, datum, anzahl, fehlerbericht) VALUES ('" . implode("','", $rec) . "')";
- $c = $dbh->query($insertQuery);
- if ($c) {
- imap_delete($mbox, $m);
- } else {
- $result[] = $insertQuery;
- $result[] = $dbh->errorInfo();
- }
- if (!isset($customers[$rec[0]])) {
- $dbh->query("INSERT INTO kunden (kunde, start_soll, ende_soll, erster_status, system) VALUES ('{$rec[0]}', '{$rec[1]}', '{$rec[2]}', '{$rec[3]}', '?')");
- $customers[$rec[0]] = array();
- }
- if ($info) {
- $q = $dbh->query("SELECT * FROM kunden_config WHERE kunde = '{$rec[0]}' ORDER BY datum DESC LIMIT 1");
- $last = ($q && $q->rowCount() == 1) ? $q->fetch(PDO::FETCH_ASSOC) : false;
- if (!$last || addslashes($last['info']) != $info) {
- $dbh->query("INSERT INTO kunden_config (kunde, datum, info) VALUES ('{$rec[0]}', '{$rec[3]}', '{$info}')");
- }
- }
- }
- imap_expunge($mbox);
- imap_close($mbox);
- echo json_encode($result);
|