hedera-web/rest/tpv/confirm-mail.php

99 lines
2.0 KiB
PHP
Raw Normal View History

2016-07-22 20:00:27 +00:00
<?php
2018-05-23 10:14:20 +00:00
require_once(__DIR__.'/tpv.php');
2016-07-22 20:00:27 +00:00
/**
* Gets transaction confirmations from the IMAP mailbox.
**/
2018-05-23 10:14:20 +00:00
class ConfirmMail extends Vn\Lib\Method {
function run($db) {
2016-07-22 20:00:27 +00:00
$imap = NULL;
2018-05-23 10:14:20 +00:00
$imapConf = $db->getObject(
2017-12-20 11:34:04 +00:00
'SELECT host, user, pass, cleanPeriod, successFolder, errorFolder
FROM tpvImapConfig'
2016-07-22 20:00:27 +00:00
);
2018-05-23 10:14:20 +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
2018-05-23 10:14:20 +00:00
$imap = imap_open($mailbox
2017-12-20 11:34:04 +00:00
,$imapConf->user
2018-05-23 10:14:20 +00:00
,base64_decode($imapConf->pass)
2016-07-22 20:00:27 +00:00
);
if (!$imap)
2018-05-23 10:14:20 +00:00
throw new Exception(imap_last_error());
2016-07-22 20:00:27 +00:00
// Fetchs and confirms new transaction mails
$count = 0;
2018-05-23 10:14:20 +00:00
$inbox = imap_search($imap, 'ALL');
2016-07-22 20:00:27 +00:00
if ($inbox)
2018-05-23 10:14:20 +00:00
foreach($inbox as $msg) {
2016-07-22 20:00:27 +00:00
// Decodes the mail body
$params = [];
2018-05-23 10:14:20 +00:00
$body = imap_fetchbody($imap, $msg, '1');
$strings = explode(';', $body);
2016-07-22 20:00:27 +00:00
2018-05-23 10:14:20 +00:00
foreach($strings as $string) {
$x = explode(':', $string);
$params[trim($x[0])] = trim($x[1]);
2016-07-22 20:00:27 +00:00
}
// Confirms the transaction
$success = FALSE;
2016-07-22 20:00:27 +00:00
try {
2018-05-23 10:14:20 +00:00
$success = Tpv::confirm($db, $params);
2018-05-23 11:09:55 +00:00
} catch (\Exception $e) {
2018-05-23 10:14:20 +00:00
trigger_error($e->getMessage(), E_USER_WARNING);
2016-07-22 20:00:27 +00:00
}
// 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
2018-05-23 10:14:20 +00:00
if (!imap_mail_move($imap, $msg, "$folder"))
trigger_error(imap_last_error(), E_USER_WARNING);
$count++;
2016-07-22 20:00:27 +00:00
}
2018-05-23 10:14:20 +00:00
imap_expunge($imap);
2016-07-22 20:00:27 +00:00
// Cleans the old mails
$deleted = 0;
2016-07-22 20:00:27 +00:00
2018-05-23 10:14:20 +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
);
2018-05-23 10:14:20 +00:00
$date = new \DateTime(NULL);
$date->sub(new \DateInterval($imapConf->cleanPeriod));
$filter = sprintf('BEFORE "%s"', $date->format('D, j M Y'));
2016-07-22 20:00:27 +00:00
2018-05-23 10:14:20 +00:00
foreach($folders as $folder)
if (imap_reopen($imap, $mailbox.'.'.$folder))
if ($messages = imap_search($imap, $filter)) {
foreach($messages as $message)
imap_delete($imap, $message);
2016-07-22 20:00:27 +00:00
2018-05-23 10:14:20 +00:00
imap_expunge($imap);
$deleted += count($messages);
2016-07-22 20:00:27 +00:00
}
}
echo "$count mails processed, $deleted mails deleted.\n";
2016-07-22 20:00:27 +00:00
}
}