-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathFetchersCron.php
177 lines (153 loc) · 7.3 KB
/
FetchersCron.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
<?php
/**
* This code is licensed under AGPLv3 license or Afterlogic Software License
* if commercial version of the product was purchased.
* For full statements of the licenses see LICENSE-AFTERLOGIC and LICENSE-AGPL3 files.
*/
namespace Aurora\Modules\MtaConnector;
require_once \dirname(__file__) . "/../../system/autoload.php";
\Aurora\System\Api::Init();
/**
* @license https://www.gnu.org/licenses/agpl-3.0.html AGPL-3.0
* @license https://afterlogic.com/products/common-licensing Afterlogic Software License
* @copyright Copyright (c) 2023, Afterlogic Corp.
*/
class CronFetcher
{
private $oMtaConnectorModule;
private $oFetchersManager;
private $oAccountsManager;
private $sFetchersCronMpopDataFolder;
private $sFetchersCronMpopScript;
private $sFetchersCronDeliveryScript;
public function __construct()
{
/** @var \Aurora\Modules\MtaConnector\Module $oMtaConnectorModule */
$oMtaConnectorModule = \Aurora\System\Api::GetModule('MtaConnector');
$this->oMtaConnectorModule = $oMtaConnectorModule;
$this->oFetchersManager = $this->oMtaConnectorModule->oFetchersManager;
/** @var \Aurora\Modules\Mail\Module $oMailModule */
$oMailModule = \Aurora\System\Api::GetModule('Mail');
$this->oAccountsManager = $oMailModule->getAccountsManager();
$this->sFetchersCronMpopDataFolder = $this->oMtaConnectorModule->oModuleSettings->FetchersCronMpopDataFolder;
$this->sFetchersCronMpopScript = $this->oMtaConnectorModule->oModuleSettings->FetchersCronMpopScript;
$this->sFetchersCronDeliveryScript = $this->oMtaConnectorModule->oModuleSettings->FetchersCronDeliveryScript;
}
public static function NewInstance()
{
return new self();
}
public function ExecuteFetcher($oFetcher)
{
$bDoFetch = true;
$oAccount = $this->oAccountsManager->getAccountById($oFetcher->IdAccount);
if (!isset($oAccount)) {
$this->log('There is no mail account with identifier ' . $oFetcher->IdAccount);
} else {
preg_match('/(.+)@(.+)$/', $oAccount->Email, $aMatches);
$sLogin = '';
$sDomain = '';
if ($aMatches && count($aMatches) > 1) {
$sLogin = $aMatches[1];
$sDomain = $aMatches[2];
}
$sMaildir = $this->sFetchersCronMpopDataFolder . '/' . $sDomain . '/' . $sLogin;
$this->log('Fetch mail from ' . $oFetcher->IncomingServer . ':' . $oFetcher->IncomingPort . '/' . $oFetcher->IncomingLogin . ' - ' . $sDomain . '/' . $sLogin);
if ($oFetcher->IsLocked) {
$this->log('The fetching process for a current mailbox is already running, exiting...');
$bDoFetch = false;
} else {
$iNow = microtime(true);
$iTimeInterval = $iNow - $oFetcher->CheckLastTime;
$iCheckIntervalSeconds = $oFetcher->CheckInterval * 60;
if ($iCheckIntervalSeconds > 0 && $iTimeInterval < $iCheckIntervalSeconds) {
$this->log('Last check was ' . floor($iTimeInterval) . ' seconds ago, check interval is ' . $iCheckIntervalSeconds . ' seconds, exiting...');
$bDoFetch = false;
} else {
$this->log('Mailbox is free, locking it...');
$oFetcher->IsLocked = true;
$oFetcher->CheckLastTime = $iNow;
}
}
if ($bDoFetch) {
$this->oFetchersManager->updateFetcher($oFetcher, false);
$sTls = 'off';
$sStartTls = 'off';
switch ($oFetcher->IncomingMailSecurity) {
case \MailSo\Net\Enumerations\ConnectionSecurityType::SSL:
$sTls = 'on';
break;
case \MailSo\Net\Enumerations\ConnectionSecurityType::STARTTLS:
$sTls = 'on';
$sStartTls = 'on';
break;
}
$sCommand = $this->sFetchersCronMpopScript;
$sCommand .= ' --host="' . $oFetcher->IncomingServer . '"';
$sCommand .= ' --port=' . $oFetcher->IncomingPort;
$sCommand .= ' --user="' . $oFetcher->IncomingLogin . '"';
$sCommand .= ' --auth=user';
$sCommand .= ' --uidls-file="' . $sMaildir . '/fetcher-id' . $oFetcher->EntityId . '-uidls"';
$sCommand .= ' --delivery=mda,"' . $this->sFetchersCronDeliveryScript . ' -d ' . $oAccount->Email . ' -m \'' . $oFetcher->Folder . '\'"';
$sCommand .= ' --keep=' . ($oFetcher->LeaveMessagesOnServer ? 'on' : 'off');
$sCommand .= ' --only-new=on';
$sCommand .= ' --tls=' . $sTls;
$sCommand .= ' --tls-certcheck=off';
$sCommand .= ' --tls-starttls=' . $sStartTls;
$sCommand .= ' --received-header=off';
$sCommand .= ' --half-quiet';
// $sCommand .= ' -d'; // debug mode
$sCommand .= ' --passwordeval="echo \'' . $oFetcher->IncomingPassword . '\'"';
$this->log('Execute MPOP...');
$aOutput = [];
$iResult = 0;
$sOutput = exec($sCommand, $aOutput, $iResult);
$this->log('Result: ' . $sOutput . '/' . $iResult);
$this->log('Result serialized: ' . json_encode($aOutput));
$this->log('Unlock mailbox...');
$oFetcher->IsLocked = false;
$this->oFetchersManager->updateFetcher($oFetcher, false);
}
}
}
public function ExecuteAllFetchers()
{
$iTimer = microtime(true);
$this->log('Start fetcher cron script');
$bAllowFetchersCrone = $this->oMtaConnectorModule->oModuleSettings->AllowFetchers;
if (!$bAllowFetchersCrone) {
$this->log('Fetchers are not allowed, exiting...');
}
// if (!file_exists($this->sFetchersCronMpopDataFolder))
// {
// $this->log('Cron data folder (' . $this->sFetchersCronMpopDataFolder . ') does not exist, exiting...');
// $bAllowFetchersCrone = false;
// }
//
// if (!file_exists($this->sFetchersCronMpopScript))
// {
// $this->log('Cron MPOP script (' . $this->sFetchersCronMpopScript . ') does not exist, exiting...');
// $bAllowFetchersCrone = false;
// }
//
// if (!file_exists($this->sFetchersCronDeliveryScript))
// {
// $this->log('Cron delivery script (' . $this->sFetchersCronDeliveryScript . ') does not exist, exiting...');
// $bAllowFetchersCrone = false;
// }
if ($bAllowFetchersCrone) {
$aFetchers = $this->oFetchersManager->getFetchers();
foreach ($aFetchers as $oFetcher) {
if ($oFetcher->IsEnabled) {
$this->ExecuteFetcher($oFetcher);
}
}
}
$this->log('Cron execution time: ' . (microtime(true) - $iTimer) . ' sec.');
}
private function log($sMessage)
{
\Aurora\System\Api::Log($sMessage, \Aurora\System\Enums\LogLevel::Full, 'cron-fetcher-');
}
}
CronFetcher::NewInstance()->ExecuteAllFetchers();