2016-08-26 12:43:45 +00:00
|
|
|
<?php
|
|
|
|
|
2018-05-23 10:14:20 +00:00
|
|
|
require_once(__DIR__.'/lib/method.php');
|
|
|
|
require_once(__DIR__.'/lib/message.php');
|
2016-08-26 12:43:45 +00:00
|
|
|
|
2016-10-17 12:36:52 +00:00
|
|
|
use Vn\Lib\Type;
|
|
|
|
|
2018-05-23 10:14:20 +00:00
|
|
|
class Load extends Edi\Method {
|
|
|
|
function ediRun($db) {
|
|
|
|
$this->ediSchema = Edi\Message::loadSchema('CLOCKT');
|
2016-08-26 12:43:45 +00:00
|
|
|
|
|
|
|
if (!$this->ediSchema)
|
2018-05-23 10:14:20 +00:00
|
|
|
throw new Exception('Can not load EDI schema.');
|
2016-08-26 12:43:45 +00:00
|
|
|
|
2019-05-24 13:30:48 +00:00
|
|
|
$this->restrictToSenders = $db->getValue(
|
|
|
|
"SELECT restrictToSenders FROM exchangeConfig LIMIT 1");
|
2018-05-23 10:14:20 +00:00
|
|
|
$this->params = $db->query(
|
2019-05-24 13:30:48 +00:00
|
|
|
"SELECT code, name, subname, position, type, required FROM param");
|
2016-08-26 12:43:45 +00:00
|
|
|
|
2018-05-23 10:14:20 +00:00
|
|
|
$inbox = imap_search($this->imap, 'ALL');
|
2016-08-26 12:43:45 +00:00
|
|
|
|
2018-05-23 10:14:20 +00:00
|
|
|
if ($inbox) {
|
2018-06-06 11:08:17 +00:00
|
|
|
foreach ($inbox as $msg)
|
2018-05-23 10:14:20 +00:00
|
|
|
$this->loadMail($db, $msg);
|
2016-08-26 12:43:45 +00:00
|
|
|
|
2018-05-23 10:14:20 +00:00
|
|
|
$inboxCount = count($inbox);
|
2016-08-26 12:43:45 +00:00
|
|
|
|
|
|
|
if ($inboxCount > 0)
|
2019-05-24 13:30:48 +00:00
|
|
|
echo "Total $inboxCount messages processed.\n";
|
2016-08-26 12:43:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-23 10:14:20 +00:00
|
|
|
function loadMail($db, $msg) {
|
2016-08-26 12:43:45 +00:00
|
|
|
$imap = $this->imap;
|
|
|
|
|
|
|
|
// Gets EKT messages from email
|
|
|
|
|
2018-05-23 10:14:20 +00:00
|
|
|
try {
|
2019-05-24 13:30:48 +00:00
|
|
|
$msgStructure = imap_fetchstructure($imap, $msg);
|
|
|
|
$result = [];
|
|
|
|
|
|
|
|
// Gets the mail sender and Message-ID
|
2016-08-26 12:43:45 +00:00
|
|
|
|
2019-05-24 13:30:48 +00:00
|
|
|
$header = imap_headerinfo($imap, $msg);
|
|
|
|
$from = $header->from;
|
2016-08-26 12:43:45 +00:00
|
|
|
|
2019-05-24 13:30:48 +00:00
|
|
|
if (property_exists($header, 'message_id'))
|
|
|
|
$messageId = trim($header->message_id, '<>');
|
|
|
|
else
|
|
|
|
$messageId = NULL;
|
2016-08-26 12:43:45 +00:00
|
|
|
|
2019-05-24 13:30:48 +00:00
|
|
|
if ($from && count($from) > 0)
|
|
|
|
$sender = $from[0]->mailbox .'@'. $from[0]->host;
|
|
|
|
else
|
|
|
|
$sender = NULL;
|
|
|
|
|
|
|
|
$db->query('CALL mail_new(#messageId, #sender, @mailFk)', [
|
|
|
|
'messageId' => $messageId,
|
2016-08-26 12:43:45 +00:00
|
|
|
'sender' => $sender
|
|
|
|
]);
|
2019-05-24 13:30:48 +00:00
|
|
|
$mailId = $db->getValue("SELECT @mailFk");
|
2016-08-26 12:43:45 +00:00
|
|
|
|
2019-05-24 13:30:48 +00:00
|
|
|
echo "Message from: $sender\n";
|
|
|
|
echo " -> Message id: $messageId\n";
|
2016-08-26 12:43:45 +00:00
|
|
|
|
2019-05-24 13:30:48 +00:00
|
|
|
if ($this->restrictToSenders) {
|
|
|
|
$isAllowed = $db->getValue(
|
|
|
|
"SELECT COUNT(*) > 0 FROM mailSender WHERE mail = #",
|
|
|
|
[$sender]
|
|
|
|
);
|
|
|
|
|
|
|
|
if (!$isAllowed)
|
|
|
|
throw new Exception('Mail processing from unknown senders is disabled');
|
|
|
|
}
|
|
|
|
|
|
|
|
// Searches the EDI message on mail parts
|
|
|
|
|
|
|
|
$matchTypes = [TYPEAPPLICATION, TYPETEXT];
|
|
|
|
$this->imapFindParts($msgStructure, $matchTypes, [], $result);
|
|
|
|
|
|
|
|
$count = 0;
|
|
|
|
$error = NULL;
|
|
|
|
|
|
|
|
foreach ($result as $msgSection)
|
|
|
|
try {
|
|
|
|
$part = imap_bodystruct($imap, $msg, $msgSection);
|
|
|
|
$ediString = imap_fetchbody($imap, $msg, $msgSection);
|
|
|
|
|
|
|
|
switch ($part->encoding) {
|
|
|
|
case ENCBASE64:
|
|
|
|
$ediString = imap_base64($ediString);
|
|
|
|
break;
|
|
|
|
case ENCQUOTEDPRINTABLE:
|
|
|
|
$ediString = imap_qprint($ediString);
|
|
|
|
break;
|
2016-08-26 12:43:45 +00:00
|
|
|
}
|
|
|
|
|
2019-05-24 13:30:48 +00:00
|
|
|
if (!Edi\Message::isEdiString($ediString))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// Creates the EDI object and loads its exchanges
|
2016-08-26 12:43:45 +00:00
|
|
|
|
2019-05-24 13:30:48 +00:00
|
|
|
$ediMessage = new Edi\Message();
|
|
|
|
$ediMessage->parse($ediString, $this->ediSchema);
|
2016-08-26 12:43:45 +00:00
|
|
|
|
2019-05-24 13:30:48 +00:00
|
|
|
$db->query('START TRANSACTION');
|
|
|
|
|
|
|
|
$unb = $ediMessage->section;
|
|
|
|
$unhs = $unb->childs['UNH'];
|
|
|
|
|
|
|
|
foreach ($unhs as $unh)
|
|
|
|
foreach ($lins = $unh->childs['LIN'] as $lin) {
|
|
|
|
$ediValues = ['mailId' => $mailId];
|
|
|
|
|
|
|
|
// Gets the exchange params
|
|
|
|
|
|
|
|
$this->params->data_seek(0);
|
|
|
|
|
|
|
|
while ($row = $this->params->fetch_assoc()) {
|
|
|
|
switch ($row['type']) {
|
|
|
|
case 'INTEGER':
|
|
|
|
$type = Type::INTEGER;
|
|
|
|
break;
|
|
|
|
case 'DOUBLE':
|
|
|
|
$type = Type::DOUBLE;
|
|
|
|
break;
|
|
|
|
case 'DATE':
|
|
|
|
$type = Type::DATE;
|
|
|
|
break;
|
|
|
|
case 'TIME':
|
|
|
|
$type = Type::TIME;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
$type = Type::STRING;
|
|
|
|
}
|
2016-08-26 12:43:45 +00:00
|
|
|
|
2019-05-24 13:30:48 +00:00
|
|
|
$value = $lin->getValue(
|
|
|
|
$row['name'], $row['position'], $type, $row['subname']);
|
2016-08-26 12:43:45 +00:00
|
|
|
|
2019-05-24 13:30:48 +00:00
|
|
|
if (!isset($value) && $row['required'])
|
|
|
|
throw new Exception('Missing required parameter: '. $row['code']);
|
2016-08-26 12:43:45 +00:00
|
|
|
|
2019-05-24 13:30:48 +00:00
|
|
|
$ediValues[$row['code']] = $value;
|
|
|
|
}
|
2016-08-26 12:43:45 +00:00
|
|
|
|
2019-05-24 13:30:48 +00:00
|
|
|
// Gets the exchange features
|
|
|
|
|
|
|
|
$res = $db->query(
|
|
|
|
'SELECT presentation_order, feature
|
|
|
|
FROM item_feature
|
|
|
|
WHERE item_id = #ref
|
|
|
|
AND entry_date <= CURDATE()
|
|
|
|
AND(expiry_date IS NULL OR expiry_date >= CURDATE())
|
|
|
|
GROUP BY presentation_order'
|
|
|
|
,$ediValues
|
|
|
|
);
|
|
|
|
|
|
|
|
if ($res)
|
|
|
|
while ($row = $res->fetch_assoc()) {
|
|
|
|
$value = $lin->getValue('IMD', 2, Type::INTEGER, $row['feature']);
|
|
|
|
$ediValues['s'.$row['presentation_order']] = $value;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
throw new Exception('Can\'t get the item features.');
|
|
|
|
|
|
|
|
for ($i = 1; $i <= 6; $i++)
|
|
|
|
if (!isset($ediValues['s'.$i]))
|
|
|
|
$ediValues['s'.$i] = NULL;
|
|
|
|
|
|
|
|
// Adds the exchange to the Database
|
|
|
|
|
|
|
|
$res = $db->queryFromFile(__DIR__.'/sql/exchange-new', $ediValues);
|
|
|
|
|
|
|
|
if (!$res)
|
|
|
|
throw new Exception('Failed to insert the line.');
|
|
|
|
|
|
|
|
$count++;
|
|
|
|
}
|
2016-08-26 12:43:45 +00:00
|
|
|
|
2019-05-24 13:30:48 +00:00
|
|
|
$db->query('COMMIT');
|
|
|
|
} catch (Exception $e) {
|
|
|
|
$db->query('ROLLBACK');
|
|
|
|
throw $e;
|
2016-08-26 12:43:45 +00:00
|
|
|
}
|
2019-05-24 13:30:48 +00:00
|
|
|
|
|
|
|
if ($count == 0)
|
|
|
|
throw new Exception('No part with EDI format was found');
|
|
|
|
|
|
|
|
echo " -> Mail id: $mailId\n";
|
|
|
|
echo " -> Loaded exchanges: $count\n";
|
|
|
|
|
|
|
|
$folder = $this->imapConf['successFolder'];
|
|
|
|
$db->query(
|
|
|
|
"UPDATE mail
|
|
|
|
SET nExchanges = #,
|
|
|
|
error = NULL
|
|
|
|
WHERE id = #",
|
|
|
|
[$count, $mailId]
|
|
|
|
);
|
2016-08-26 12:43:45 +00:00
|
|
|
|
2018-05-23 11:09:55 +00:00
|
|
|
} catch (Exception $e) {
|
2018-05-23 10:14:20 +00:00
|
|
|
$error = $e->getMessage();
|
2019-05-24 13:30:48 +00:00
|
|
|
error_log($error);
|
|
|
|
|
|
|
|
$folder = $this->imapConf['errorFolder'];
|
|
|
|
$db->query(
|
|
|
|
"UPDATE mail
|
|
|
|
SET error = #
|
|
|
|
WHERE id = #",
|
|
|
|
[$error, $mailId]
|
|
|
|
);
|
2016-08-26 12:43:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Moves the mail to another folder
|
|
|
|
|
2018-05-23 10:14:20 +00:00
|
|
|
$folder = sprintf('%s', $folder);
|
2016-08-26 12:43:45 +00:00
|
|
|
|
2018-05-23 10:14:20 +00:00
|
|
|
if (!imap_mail_move($imap, $msg, $folder))
|
|
|
|
error_log('Can\'t move message to %s: %s'
|
2016-08-26 12:43:45 +00:00
|
|
|
,$folder
|
2018-05-23 10:14:20 +00:00
|
|
|
,imap_last_error()
|
2016-08-26 12:43:45 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2018-05-23 10:14:20 +00:00
|
|
|
function imapFindParts(&$part, &$matchTypes, $section, &$result) {
|
|
|
|
if (in_array($part->type, $matchTypes)) {
|
|
|
|
if (count($section) > 0)
|
|
|
|
$result[] = implode('.', $section);
|
2016-08-26 12:43:45 +00:00
|
|
|
else
|
|
|
|
$result[] = '1';
|
2018-05-23 11:09:55 +00:00
|
|
|
} elseif ($part->type == TYPEMULTIPART)
|
2018-06-06 11:08:17 +00:00
|
|
|
foreach ($part->parts as $i => $subpart) {
|
2018-05-23 11:09:55 +00:00
|
|
|
array_push($section, $i + 1);
|
|
|
|
$this->imapFindParts($subpart, $matchTypes, $section, $result);
|
|
|
|
array_pop($section);
|
|
|
|
}
|
2016-08-26 12:43:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|