From 24676a67a05ba427483faefcf3f9017cf39f1419 Mon Sep 17 00:00:00 2001 From: Luca Haid Date: Tue, 12 Nov 2024 14:09:02 +0100 Subject: [PATCH] fixed email fetching utf8 --- .../WarehouseEShopOrderController.php | 59 ++++++++++++++++--- lib/TTCrud/TTCrud.php | 10 +++- 2 files changed, 59 insertions(+), 10 deletions(-) diff --git a/application/WarehouseEShopOrder/WarehouseEShopOrderController.php b/application/WarehouseEShopOrder/WarehouseEShopOrderController.php index 1485710b3..61b0a8c31 100644 --- a/application/WarehouseEShopOrder/WarehouseEShopOrderController.php +++ b/application/WarehouseEShopOrder/WarehouseEShopOrderController.php @@ -430,6 +430,21 @@ class WarehouseEShopOrderController extends TTCrud { } protected function readGLSEmailAction() { + function decode_utf8($str){ + # paterns + $err="(=\?.{10,13}q\?_?|\?\=)"; + $pat = "/=([0-9A-F]{2})/"; + $cha="'.chr(hexdec("; + # erase null signs in string + $str = preg_replace("/\x00/", "", $str); + # to decode with eval and replace + eval("\$str='". + preg_replace($pat,$cha."'$1')).'",$str) + ."';"); + # return + return $str; + } + $host = '{mail.xinon.at:993/imap/ssl/novalidate-cert}INBOX'; $mbox = imap_open($host, 'eshop-versand@xinon.at', 'savemanfb545aw'); $emails = imap_search($mbox, 'ALL'); @@ -443,7 +458,7 @@ class WarehouseEShopOrderController extends TTCrud { $overview = imap_fetch_overview($mbox, $email_number, 0); if (strpos($overview[0]->from, 'gls') === false) { - //continue; + continue; } $message = imap_fetchbody($mbox, $email_number, 1); // 1 for plain text part @@ -468,18 +483,43 @@ class WarehouseEShopOrderController extends TTCrud { $addressLines = explode("\n", $address); $addressLine = trim(array_shift($addressLines)); + $plzCity = trim(array_shift($addressLines)); $plzCityParts = explode(' ', $plzCity); $plz = $plzCityParts[0]; + if (!isset($plzCityParts[1])) { + continue; + } + echo "---------------------------------------\n\n"; $city = $plzCityParts[1]; + + + // convert special characters to normal ones because i get J=C3=B6ss instead of Jöss + $addressLine = decode_utf8($addressLine); + $city = decode_utf8($city); + + echo "Found address: $addressLine, $plz, $city" . PHP_EOL; // END ADDRESS PARSING // START TRACKING NUMBER PARSING - $trackingNumber = ''; - preg_match('/\d{6,}/', $message, $matches); +// $trackingNumber = ''; +// preg_match('/\d{6,}/', $message, $matches); +// if (!empty($matches)) { +// // if addrline includes 27 +// if (strpos($addressLine, '27') !== false) { +// var_dump($matches); +// die($message); +// } +// +// $trackingNumber = $matches[0]; +// } + + // grab tracking number from https://gls-group.eu/track/53158006143 (example) from url + preg_match('/track\/(\d+)/', $message, $matches); if (!empty($matches)) { - $trackingNumber = $matches[0]; + $trackingNumber = $matches[1]; } + echo "Found tracking number: $trackingNumber" . PHP_EOL; // END TRACKING NUMBER PARSING @@ -487,11 +527,13 @@ class WarehouseEShopOrderController extends TTCrud { 'deliveryAddressPLZ' => $plz, 'deliveryAddressCity' => $city]); if (empty($orders)) { + echo "No order found with address: $addressLine, $plz, $city" . PHP_EOL; continue; } // now check if the trackingNumber is already set and if not set it and create a history entry $order = (array) $orders[0]; + echo "Found order with address: " . $order['id'] . PHP_EOL; // if ($order['trackingNumber']) { continue; @@ -509,11 +551,10 @@ class WarehouseEShopOrderController extends TTCrud { 'user_id' => 1, 'create' => date('U')]); - echo "Subject: " . $overview[0]->subject . "\n"; - echo "From: " . $overview[0]->from . "\n"; - echo "Date: " . $overview[0]->date . "\n"; - echo "Message:\n" . $message . "\n\n"; - echo "---------------------------------------\n\n"; +// echo "Subject: " . $overview[0]->subject . "\n"; +// echo "From: " . $overview[0]->from . "\n"; +// echo "Date: " . $overview[0]->date . "\n"; +// echo "Message:\n" . $message . "\n\n"; } } else { echo "No emails found."; diff --git a/lib/TTCrud/TTCrud.php b/lib/TTCrud/TTCrud.php index d6f17358c..f764c1280 100644 --- a/lib/TTCrud/TTCrud.php +++ b/lib/TTCrud/TTCrud.php @@ -70,7 +70,8 @@ class TTCrud extends mfBaseController { "CREATE_URL" => $this::getUrl($this->mod . "/create"), "TABLE_URL" => $this::getUrl($this->mod . "/get"), "UPDATE_URL" => $this::getUrl($this->mod . "/update"), - "DELETE_URL" => $this::getUrl($this->mod . "/delete"),]); + "DELETE_URL" => $this::getUrl($this->mod . "/delete"), + "USER_ID" => $this->user->id]); } /** @@ -189,6 +190,13 @@ class TTCrud extends mfBaseController { } protected function updateAction() { + if (property_exists($this->model, 'createBy') && !isset($this->postData['createBy'])) { + $this->postData['createBy'] = $this->user->id; + } + if (property_exists($this->model, 'create') && !isset($this->postData['create'])) { + $this->postData['create'] = time(); + } + Helper::validateArray($this->postData, array_merge($this->checkArray, ['id' => ['required' => true]])); if (method_exists($this, 'beforeUpdate') && !$this->beforeUpdate($this->postData)) {