1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798 |
- <?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";
- $q = $dbh->query("SELECT kunde FROM kunden");
- $customers = $q->fetchAll(PDO::FETCH_COLUMN, 0);
- $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) {
- $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];
- $body = trim(imap_fetchbody($mbox, $m, '1'));
- if (substr($body, 0, 4) == "--b2") {
- $body = "";
- }
- array_push($rec, $body);
- $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']);
- }
- }
- }
- }
- $a = "";
- foreach ($attachments as $key => $attachment) {
- if ($attachment['is_attachment'] && preg_match("/\.bat\.log/", $attachment['filename'])) {
- $a .= $attachment['attachment'];
- }
- }
- array_push($rec, addslashes($a));
- $dbh->query("INSERT INTO statusmail (kunde, start, ende, datum, anzahl, fehlerbericht, logdatei) VALUES ('" . implode("','", $rec) . "')");
- imap_delete($mbox, $m);
- if (!in_array($rec[0], $customers)) {
- $dbh->query("INSERT INTO kunden (kunde, start_soll, ende_soll, erster_status) VALUES ('{$rec[0]}', '{$rec[1]}', '{$rec[2]}', '{$rec[3]}')");
- array_push($customers, $rec[0]);
- }
- }
- imap_expunge($mbox);
- imap_close($mbox);
- echo json_encode($result);
|