forked from verdnatura/hedera-web
111 lines
2.2 KiB
PHP
111 lines
2.2 KiB
PHP
<?php
|
|
|
|
require_once ('vn/web/json-request.php');
|
|
|
|
/**
|
|
* Gets transaction confirmations from the IMAP mailbox.
|
|
**/
|
|
class TpvConfirmMail extends Vn\Web\JsonRequest
|
|
{
|
|
function run ()
|
|
{
|
|
$db = $this->getSysConn ();
|
|
|
|
$imap = NULL;
|
|
$imapConf = $db->getRow (
|
|
'SELECT host, user, pass, clean_period, success_folder, error_folder
|
|
FROM tpv_imap_config'
|
|
);
|
|
|
|
$mailbox = sprintf ('{%s/imap/ssl/novalidate-cert}INBOX',
|
|
$imapConf['host']);
|
|
|
|
$imap = imap_open ($mailbox
|
|
,$imapConf['user']
|
|
,base64_decode ($imapConf['pass'])
|
|
);
|
|
|
|
if (!$imap)
|
|
throw new Exception (imap_last_error ());
|
|
|
|
// Fetchs and confirms new transaction mails
|
|
|
|
$inbox = imap_search ($imap, 'ALL');
|
|
|
|
if ($inbox)
|
|
foreach ($inbox as $msg)
|
|
{
|
|
// Decodes the mail body
|
|
|
|
$params = [];
|
|
$body = imap_fetchbody ($imap, $msg, '1');
|
|
$strings = explode (';', $body);
|
|
|
|
foreach ($strings as $string)
|
|
{
|
|
$x = explode (':', $string);
|
|
$params[trim ($x[0])] = trim ($x[1]);
|
|
}
|
|
|
|
// Confirms the transaction
|
|
|
|
try {
|
|
$success = $this->confirm ($params, $db);
|
|
}
|
|
catch (\Exception $e)
|
|
{
|
|
trigger_error ($e->getMessage (), E_USER_WARNING);
|
|
}
|
|
|
|
// Moves the processed mail to another folder
|
|
|
|
if ($success)
|
|
$folder = $imapConf['success_folder'];
|
|
else
|
|
$folder = $imapConf['error_folder'];
|
|
|
|
$folder = sprintf ('INBOX.%s', $folder);
|
|
|
|
if (!imap_mail_move ($imap, $msg, $folder))
|
|
trigger_error (imap_last_error (), E_USER_WARNING);
|
|
}
|
|
|
|
imap_expunge ($imap);
|
|
|
|
// Cleans the old mails
|
|
|
|
$deleted = NULL;
|
|
|
|
if (rand (1, 20) == 1)
|
|
{
|
|
$folders = array (
|
|
$imapConf['success_folder']
|
|
,$imapConf['error_folder']
|
|
);
|
|
|
|
$deleted = 0;
|
|
$date = new \DateTime (NULL);
|
|
$date->sub (new \DateInterval ($imapConf['clean_period']));
|
|
$filter = sprintf ('BEFORE "%s"', $date->format('D, j M Y'));
|
|
|
|
foreach ($folders as $folder)
|
|
if (imap_reopen ($imap, $mailbox.'.'.$folder))
|
|
if ($messages = imap_search ($imap, $filter))
|
|
{
|
|
foreach ($messages as $message)
|
|
imap_delete ($imap, $message);
|
|
|
|
imap_expunge ($imap);
|
|
$deleted += count ($messages);
|
|
}
|
|
}
|
|
|
|
return [
|
|
'processed' => $count,
|
|
'deleted' => $deleted
|
|
];
|
|
}
|
|
}
|
|
|
|
?>
|