0
1
Fork 0
hedera-web-mindshore/rest/tpv/confirm-mail.php

107 lines
2.1 KiB
PHP
Raw Normal View History

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
);
$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
$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
$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
if (!imap_mail_move ($imap, $msg, "$folder"))
2016-07-22 20:00:27 +00:00
trigger_error (imap_last_error (), E_USER_WARNING);
$count++;
2016-07-22 20:00:27 +00:00
}
imap_expunge ($imap);
// Cleans the old mails
$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);
}
}
echo "$count mails processed, $deleted mails deleted.\n";
2016-07-22 20:00:27 +00:00
}
}