2016-07-22 20:00:27 +00:00
|
|
|
<?php
|
|
|
|
|
2016-08-22 10:41:05 +00:00
|
|
|
require_once (__DIR__.'/tpv.php');
|
2016-07-22 20:00:27 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets transaction confirmations from the IMAP mailbox.
|
|
|
|
**/
|
2016-08-23 13:15:19 +00:00
|
|
|
class ConfirmMail extends Vn\Lib\Method
|
2016-07-22 20:00:27 +00:00
|
|
|
{
|
2016-09-23 22:47:34 +00:00
|
|
|
function run ($db)
|
2016-07-22 20:00:27 +00:00
|
|
|
{
|
|
|
|
$imap = NULL;
|
2017-12-20 11:34:04 +00:00
|
|
|
$imapConf = $db->getObject (
|
|
|
|
'SELECT host, user, pass, cleanPeriod, successFolder, errorFolder
|
|
|
|
FROM tpvImapConfig'
|
2016-07-22 20:00:27 +00:00
|
|
|
);
|
|
|
|
|
2016-12-05 12:39:29 +00:00
|
|
|
$mailbox = sprintf ('{%s/imap/ssl/novalidate-cert}',
|
2017-12-20 11:34:04 +00:00
|
|
|
$imapConf->host);
|
2016-07-22 20:00:27 +00:00
|
|
|
|
|
|
|
$imap = imap_open ($mailbox
|
2017-12-20 11:34:04 +00:00
|
|
|
,$imapConf->user
|
|
|
|
,base64_decode ($imapConf->pass)
|
2016-07-22 20:00:27 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
if (!$imap)
|
|
|
|
throw new Exception (imap_last_error ());
|
|
|
|
|
|
|
|
// Fetchs and confirms new transaction mails
|
|
|
|
|
2016-08-26 12:43:45 +00:00
|
|
|
$count = 0;
|
2016-07-22 20:00:27 +00:00
|
|
|
$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
|
|
|
|
|
2016-08-26 12:43:45 +00:00
|
|
|
$success = FALSE;
|
|
|
|
|
2016-07-22 20:00:27 +00:00
|
|
|
try {
|
2016-08-22 10:41:05 +00:00
|
|
|
$success = Tpv::confirm ($db, $params);
|
2016-07-22 20:00:27 +00:00
|
|
|
}
|
|
|
|
catch (\Exception $e)
|
|
|
|
{
|
|
|
|
trigger_error ($e->getMessage (), E_USER_WARNING);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Moves the processed mail to another folder
|
|
|
|
|
|
|
|
if ($success)
|
2017-12-20 11:34:04 +00:00
|
|
|
$folder = $imapConf->successFolder;
|
2016-07-22 20:00:27 +00:00
|
|
|
else
|
2017-12-20 11:34:04 +00:00
|
|
|
$folder = $imapConf->errorFolder;
|
2016-07-22 20:00:27 +00:00
|
|
|
|
2016-12-05 12:39:29 +00:00
|
|
|
if (!imap_mail_move ($imap, $msg, "$folder"))
|
2016-07-22 20:00:27 +00:00
|
|
|
trigger_error (imap_last_error (), E_USER_WARNING);
|
2016-08-26 12:43:45 +00:00
|
|
|
|
|
|
|
$count++;
|
2016-07-22 20:00:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
imap_expunge ($imap);
|
|
|
|
|
|
|
|
// Cleans the old mails
|
|
|
|
|
2016-08-26 12:43:45 +00:00
|
|
|
$deleted = 0;
|
2016-07-22 20:00:27 +00:00
|
|
|
|
|
|
|
if (rand (1, 20) == 1)
|
|
|
|
{
|
|
|
|
$folders = array (
|
2017-12-20 11:34:04 +00:00
|
|
|
$imapConf->successFolder
|
|
|
|
,$imapConf->errorFolder
|
2016-07-22 20:00:27 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
$date = new \DateTime (NULL);
|
2017-12-20 11:34:04 +00:00
|
|
|
$date->sub (new \DateInterval ($imapConf->cleanPeriod));
|
2016-07-22 20:00:27 +00:00
|
|
|
$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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-26 12:43:45 +00:00
|
|
|
echo "$count mails processed, $deleted mails deleted.\n";
|
2016-07-22 20:00:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|