| 1 |
<?php |
| 2 |
|
| 3 |
namespace AcyMailing\Helpers; |
| 4 |
|
| 5 |
use AcyMailing\Classes\AutomationClass; |
| 6 |
use AcyMailing\Classes\CampaignClass; |
| 7 |
use AcyMailing\Classes\FollowupClass; |
| 8 |
use AcyMailing\Classes\MailArchiveClass; |
| 9 |
use AcyMailing\Classes\MailboxClass; |
| 10 |
use AcyMailing\Classes\MailClass; |
| 11 |
use AcyMailing\Classes\QueueClass; |
| 12 |
use AcyMailing\Classes\UserClass; |
| 13 |
use AcyMailing\Classes\UserStatClass; |
| 14 |
use AcyMailing\Core\AcymObject; |
| 15 |
|
| 16 |
class CronHelper extends AcymObject |
| 17 |
{ |
| 18 |
const STEP_SCHEDULE = 'schedule'; |
| 19 |
const STEP_CLEAN_QUEUE = 'cleanqueue'; |
| 20 |
const STEP_SEND = 'send'; |
| 21 |
const STEP_BOUNCE = 'bounce'; |
| 22 |
const STEP_AUTOMATION = 'automation'; |
| 23 |
const STEP_CAMPAIGN = 'campaign'; |
| 24 |
const STEP_SPECIFIC = 'specific'; |
| 25 |
const STEP_FOLLOWUP = 'followup'; |
| 26 |
const STEP_DELETE_HISTORY = 'delete_history'; |
| 27 |
const STEP_CLEAN_DATA_EXTERNAL_SENDING_METHOD = 'clean_data_external_sending_method'; |
| 28 |
const STEP_CLEAN_EXPORT_CHANGES = 'clean_export_changes'; |
| 29 |
const STEP_MAILBOX_ACTION = 'mailbox_action'; |
| 30 |
const STEP_ABTEST = 'abtest'; |
| 31 |
const STEP_SCENARIO = 'scenario'; |
| 32 |
|
| 33 |
const ALL_STEPS = [ |
| 34 |
self::STEP_SCHEDULE, |
| 35 |
self::STEP_CLEAN_QUEUE, |
| 36 |
self::STEP_SEND, |
| 37 |
self::STEP_BOUNCE, |
| 38 |
self::STEP_AUTOMATION, |
| 39 |
self::STEP_CAMPAIGN, |
| 40 |
self::STEP_SPECIFIC, |
| 41 |
self::STEP_FOLLOWUP, |
| 42 |
self::STEP_DELETE_HISTORY, |
| 43 |
self::STEP_CLEAN_DATA_EXTERNAL_SENDING_METHOD, |
| 44 |
self::STEP_CLEAN_EXPORT_CHANGES, |
| 45 |
self::STEP_MAILBOX_ACTION, |
| 46 |
self::STEP_ABTEST, |
| 47 |
self::STEP_SCENARIO, |
| 48 |
]; |
| 49 |
|
| 50 |
private array $messages = []; |
| 51 |
private array $detailMessages = []; |
| 52 |
private array $emailTypes = []; |
| 53 |
private array $skip = []; |
| 54 |
|
| 55 |
private int $cronTimeLimit = 0; |
| 56 |
private bool $cronTimeLimitReached = false; |
| 57 |
private int $startQueue; |
| 58 |
|
| 59 |
private string $mainMessage = ''; |
| 60 |
|
| 61 |
private bool $processed = false; |
| 62 |
|
| 63 |
private bool $errorDetected = false; |
| 64 |
|
| 65 |
private bool $externalSendingActivated = false; |
| 66 |
private bool $externalSendingRepeat; |
| 67 |
private bool $externalSendingNotFinished = false; |
| 68 |
|
| 69 |
public function __construct() |
| 70 |
{ |
| 71 |
parent::__construct(); |
| 72 |
$this->startQueue = acym_getVar('int', 'startqueue', 0); |
| 73 |
|
| 74 |
acym_trigger('onAcymProcessQueueExternalSendingCampaign', [&$this->externalSendingActivated]); |
| 75 |
|
| 76 |
$this->externalSendingRepeat = !empty(acym_getVar('int', 'external_sending_repeat', 0)); |
| 77 |
if (!empty($this->startQueue) || !empty($this->externalSendingRepeat)) { |
| 78 |
$this->skip = array_diff(self::ALL_STEPS, [self::STEP_SEND]); |
| 79 |
} |
| 80 |
} |
| 81 |
|
| 82 |
public function addSkipFromString(string $skipVar): void |
| 83 |
{ |
| 84 |
if (empty($skipVar)) { |
| 85 |
return; |
| 86 |
} |
| 87 |
|
| 88 |
$skipVar = explode(',', $skipVar); |
| 89 |
|
| 90 |
if (empty($skipVar)) { |
| 91 |
return; |
| 92 |
} |
| 93 |
|
| 94 |
$this->skip = array_unique(array_merge($this->skip, $skipVar)); |
| 95 |
} |
| 96 |
|
| 97 |
public function cron(): void |
| 98 |
{ |
| 99 |
|
| 100 |
$this->handleUnlinkLicenseCalls(); |
| 101 |
$this->triggeredMessage(); |
| 102 |
if (!$this->checkCronFrequency()) { |
| 103 |
return; |
| 104 |
} |
| 105 |
|
| 106 |
$this->queueScheduledCampaigns(); |
| 107 |
$this->cleanQueue(); |
| 108 |
$this->sendQueuedEmails(); |
| 109 |
$this->checkTimeRemaining(); |
| 110 |
$this->handleBounceMessages(); |
| 111 |
$this->checkTimeRemaining(); |
| 112 |
$this->handleAutomations(); |
| 113 |
$this->checkTimeRemaining(); |
| 114 |
$this->handleAutomaticCampaigns(); |
| 115 |
$this->checkTimeRemaining(); |
| 116 |
$this->handleSpecificEmails(); |
| 117 |
$this->checkTimeRemaining(); |
| 118 |
$this->handleFollowups(); |
| 119 |
$this->checkTimeRemaining(); |
| 120 |
$this->handleMailboxActions(); |
| 121 |
$this->checkTimeRemaining(); |
| 122 |
$this->cleanData(); |
| 123 |
$this->handleABTestCampaigns(); |
| 124 |
$this->handleScenario(); |
| 125 |
$this->continueCronCall(); |
| 126 |
$this->handleCronReport(); |
| 127 |
} |
| 128 |
|
| 129 |
public function saveReport(array $messages = [], array $detailMessages = []): void |
| 130 |
{ |
| 131 |
$saveReport = $this->config->get('cron_savereport'); |
| 132 |
$reportPath = $this->config->get('cron_savepath'); |
| 133 |
|
| 134 |
if (empty($saveReport) || empty($reportPath)) { |
| 135 |
return; |
| 136 |
} |
| 137 |
|
| 138 |
if (!(($saveReport == 2 && $this->processed) || $saveReport == 1 || ($saveReport == 3 && $this->errorDetected))) { |
| 139 |
return; |
| 140 |
} |
| 141 |
|
| 142 |
if (!empty($messages)) { |
| 143 |
$this->messages = $messages; |
| 144 |
} |
| 145 |
|
| 146 |
if (!empty($detailMessages)) { |
| 147 |
$this->detailMessages = $detailMessages; |
| 148 |
} |
| 149 |
|
| 150 |
$reportPath = str_replace(['{year}', '{month}'], [date('Y'), date('m')], $reportPath); |
| 151 |
$reportPath = acym_cleanPath(ACYM_ROOT.trim(html_entity_decode($reportPath))); |
| 152 |
acym_createDir(dirname($reportPath), true, true); |
| 153 |
|
| 154 |
$lr = "\r\n"; |
| 155 |
ob_start(); |
| 156 |
file_put_contents( |
| 157 |
$reportPath, |
| 158 |
$lr.$lr.'******************** '.acym_getDate(time()).' UTC ********************'.$lr.implode($lr, $this->messages), |
| 159 |
FILE_APPEND |
| 160 |
); |
| 161 |
|
| 162 |
if ($saveReport == 2 && !empty($this->detailMessages)) { |
| 163 |
file_put_contents( |
| 164 |
$reportPath, |
| 165 |
$lr.'---- Details ----'.$lr.implode($lr, $this->detailMessages), |
| 166 |
FILE_APPEND |
| 167 |
); |
| 168 |
} |
| 169 |
$potentialWarnings = ob_get_clean(); |
| 170 |
|
| 171 |
if (!empty($potentialWarnings)) { |
| 172 |
$this->messages[] = $potentialWarnings; |
| 173 |
} |
| 174 |
} |
| 175 |
|
| 176 |
public function setEmailTypes(array $emailTypes): void |
| 177 |
{ |
| 178 |
$this->emailTypes = $emailTypes; |
| 179 |
} |
| 180 |
|
| 181 |
public function addMessage(string $message): void |
| 182 |
{ |
| 183 |
$this->messages[] = $message; |
| 184 |
} |
| 185 |
|
| 186 |
public function handleCronReport(): void |
| 187 |
{ |
| 188 |
$sendReport = $this->config->get('cron_sendreport'); |
| 189 |
|
| 190 |
if (($sendReport == 2 && $this->processed) || $sendReport == 1 || ($sendReport == 3 && $this->errorDetected)) { |
| 191 |
$mailerHelper = new MailerHelper(); |
| 192 |
$mailerHelper->report = false; |
| 193 |
$mailerHelper->autoAddUser = true; |
| 194 |
$mailerHelper->addParam('report', implode('<br />', $this->messages)); |
| 195 |
$mailerHelper->addParam('mainreport', $this->mainMessage); |
| 196 |
$mailerHelper->addParam('detailreport', implode('<br />', $this->detailMessages)); |
| 197 |
|
| 198 |
$receiverString = $this->config->get('cron_sendto'); |
| 199 |
$receivers = []; |
| 200 |
if (substr_count($receiverString, '@') > 1) { |
| 201 |
$receivers = explode(' ', trim(preg_replace('# +#', ' ', str_replace([';', ','], ' ', $receiverString)))); |
| 202 |
} else { |
| 203 |
$receivers[] = trim($receiverString); |
| 204 |
} |
| 205 |
|
| 206 |
if (!empty($receivers)) { |
| 207 |
$mailClass = new MailClass(); |
| 208 |
foreach ($receivers as $oneReceiver) { |
| 209 |
if (empty($oneReceiver)) { |
| 210 |
continue; |
| 211 |
} |
| 212 |
|
| 213 |
try { |
| 214 |
$reportEmail = $mailClass->getOneByName('acy_report'); |
| 215 |
if (!empty($reportEmail)) { |
| 216 |
$mailerHelper->sendOne($reportEmail->id, $oneReceiver); |
| 217 |
} |
| 218 |
} catch (\Exception $e) { |
| 219 |
acym_logError('Error while sending the cron report to '.$oneReceiver.' : '.$e->getMessage()); |
| 220 |
} |
| 221 |
} |
| 222 |
} |
| 223 |
} |
| 224 |
|
| 225 |
$this->saveReport(); |
| 226 |
|
| 227 |
$newConfig = new \stdClass(); |
| 228 |
$newConfig->cron_report = implode("\n", $this->messages); |
| 229 |
|
| 230 |
if (strlen($newConfig->cron_report) > 800) { |
| 231 |
$newConfig->cron_report = substr($newConfig->cron_report, 0, 795).'...'; |
| 232 |
} |
| 233 |
|
| 234 |
$this->config->save($newConfig); |
| 235 |
} |
| 236 |
|
| 237 |
private function handleUnlinkLicenseCalls(): void |
| 238 |
{ |
| 239 |
if (acym_getVar('int', 'unlink', 0) !== 1) { |
| 240 |
return; |
| 241 |
} |
| 242 |
|
| 243 |
$callLicenseKey = acym_getVar('string', 'licenseKey'); |
| 244 |
$configLicenseKey = $this->config->get('license_key'); |
| 245 |
|
| 246 |
if (!empty($configLicenseKey) && $configLicenseKey === $callLicenseKey) { |
| 247 |
$this->config->save(['license_key' => '', 'active_cron' => 0]); |
| 248 |
} |
| 249 |
|
| 250 |
exit; |
| 251 |
} |
| 252 |
|
| 253 |
private function triggeredMessage(): void |
| 254 |
{ |
| 255 |
$firstMessage = acym_translationSprintf('ACYM_CRON_TRIGGERED', acym_date('now', 'd F Y H:i')); |
| 256 |
$this->messages[] = $firstMessage; |
| 257 |
acym_display($firstMessage, 'info'); |
| 258 |
} |
| 259 |
|
| 260 |
private function checkCronFrequency(): bool |
| 261 |
{ |
| 262 |
if (!empty($this->startQueue)) { |
| 263 |
return true; |
| 264 |
} |
| 265 |
|
| 266 |
$time = time(); |
| 267 |
$nextCronTime = $this->config->get('cron_next', 0); |
| 268 |
$cronFrequency = $this->config->get('cron_frequency', 900); |
| 269 |
|
| 270 |
if ($nextCronTime > $time) { |
| 271 |
if ($nextCronTime > ($time + $cronFrequency)) { |
| 272 |
$newConfig = new \stdClass(); |
| 273 |
$newConfig->cron_next = $time + $cronFrequency; |
| 274 |
$this->config->save($newConfig); |
| 275 |
} |
| 276 |
|
| 277 |
$notTimeMessage = acym_translationSprintf('ACYM_CRON_NEXT', acym_date($this->config->get('cron_next'), 'd F Y H:i')); |
| 278 |
$this->messages[] = $notTimeMessage; |
| 279 |
acym_display($notTimeMessage, 'info'); |
| 280 |
|
| 281 |
return false; |
| 282 |
} |
| 283 |
|
| 284 |
$newConfig = new \stdClass(); |
| 285 |
$newConfig->cron_last = $time; |
| 286 |
$newConfig->cron_fromip = acym_getIP(); |
| 287 |
$newConfig->cron_next = $nextCronTime + $cronFrequency; |
| 288 |
|
| 289 |
if ($newConfig->cron_next <= $time || $newConfig->cron_next > $time + $cronFrequency) { |
| 290 |
$newConfig->cron_next = $time + $cronFrequency; |
| 291 |
} |
| 292 |
|
| 293 |
$this->config->save($newConfig); |
| 294 |
|
| 295 |
return true; |
| 296 |
} |
| 297 |
|
| 298 |
private function queueScheduledCampaigns(): void |
| 299 |
{ |
| 300 |
if (in_array(self::STEP_SCHEDULE, $this->skip)) { |
| 301 |
return; |
| 302 |
} |
| 303 |
|
| 304 |
$queueClass = new QueueClass(); |
| 305 |
$nbScheduled = $queueClass->scheduleReady(); |
| 306 |
if ($nbScheduled) { |
| 307 |
$this->messages[] = acym_translationSprintf('ACYM_NB_SCHEDULED', $nbScheduled); |
| 308 |
$this->detailMessages = array_merge($this->detailMessages, $queueClass->messages); |
| 309 |
$this->processed = true; |
| 310 |
} |
| 311 |
} |
| 312 |
|
| 313 |
private function cleanQueue(): void |
| 314 |
{ |
| 315 |
if (in_array(self::STEP_CLEAN_QUEUE, $this->skip)) { |
| 316 |
return; |
| 317 |
} |
| 318 |
|
| 319 |
$queueClass = new QueueClass(); |
| 320 |
$deletedNb = $queueClass->cleanQueue(); |
| 321 |
|
| 322 |
if (!empty($deletedNb)) { |
| 323 |
$this->messages[] = acym_translationSprintf('ACYM_EMAILS_REMOVED_QUEUE_CLEAN', $deletedNb); |
| 324 |
$this->processed = true; |
| 325 |
} |
| 326 |
} |
| 327 |
|
| 328 |
private function sendQueuedEmails(): void |
| 329 |
{ |
| 330 |
if (in_array(self::STEP_SEND, $this->skip) || $this->config->get('queue_type') === 'manual') { |
| 331 |
return; |
| 332 |
} |
| 333 |
|
| 334 |
$fromHour = $this->config->get('queue_send_from_hour', '00'); |
| 335 |
$fromMinute = $this->config->get('queue_send_from_minute', '00'); |
| 336 |
$toHour = $this->config->get('queue_send_to_hour', '23'); |
| 337 |
$toMinute = $this->config->get('queue_send_to_minute', '59'); |
| 338 |
$time = time(); |
| 339 |
|
| 340 |
if ($fromHour != '00' || $fromMinute != '00' || $toHour != '23' || $toMinute != '59') { |
| 341 |
$dayBasedOnCMSTimezone = acym_date('now', 'Y-m-d'); |
| 342 |
$fromBasedOnCMSTimezoneAtSpecifiedHour = acym_getTimeFromCMSDate($dayBasedOnCMSTimezone.' '.$fromHour.':'.$fromMinute); |
| 343 |
$toBasedOnCMSTimezoneAtSpecifiedHour = acym_getTimeFromCMSDate($dayBasedOnCMSTimezone.' '.$toHour.':'.$toMinute); |
| 344 |
if ($fromBasedOnCMSTimezoneAtSpecifiedHour > $toBasedOnCMSTimezoneAtSpecifiedHour) { |
| 345 |
if ($time > $fromBasedOnCMSTimezoneAtSpecifiedHour) { |
| 346 |
$toBasedOnCMSTimezoneAtSpecifiedHour = acym_getTimeFromCMSDate(acym_date('tomorrow', 'Y-m-d').' '.$toHour.':'.$toMinute); |
| 347 |
} elseif ($time < $toBasedOnCMSTimezoneAtSpecifiedHour) { |
| 348 |
$fromBasedOnCMSTimezoneAtSpecifiedHour = acym_getTimeFromCMSDate(acym_date('yesterday', 'Y-m-d').' '.$fromHour.':'.$fromMinute); |
| 349 |
} |
| 350 |
} |
| 351 |
|
| 352 |
if ($time < $fromBasedOnCMSTimezoneAtSpecifiedHour || $time > $toBasedOnCMSTimezoneAtSpecifiedHour) { |
| 353 |
return; |
| 354 |
} |
| 355 |
} |
| 356 |
|
| 357 |
$dayOfWeek = acym_date('now', 'N'); |
| 358 |
if ($this->config->get('queue_stop_weekend', 0) && $dayOfWeek >= 6) { |
| 359 |
return; |
| 360 |
} |
| 361 |
|
| 362 |
$this->handleMultiCron(); |
| 363 |
$queueHelper = new QueueHelper(); |
| 364 |
$queueHelper->send_limit = (int)$this->config->get('queue_nbmail_auto'); |
| 365 |
$queueHelper->report = false; |
| 366 |
$queueHelper->emailTypes = $this->emailTypes; |
| 367 |
$queueHelper->process(); |
| 368 |
|
| 369 |
if (!empty($queueHelper->messages)) { |
| 370 |
$this->detailMessages = array_merge($this->detailMessages, $queueHelper->messages); |
| 371 |
} |
| 372 |
|
| 373 |
if (!empty($queueHelper->nbprocess)) { |
| 374 |
$this->processed = true; |
| 375 |
|
| 376 |
if (!$queueHelper->finish && $this->externalSendingActivated) { |
| 377 |
$this->externalSendingNotFinished = true; |
| 378 |
} |
| 379 |
} |
| 380 |
|
| 381 |
$this->mainMessage = acym_translationSprintf('ACYM_CRON_PROCESS', $queueHelper->nbprocess, $queueHelper->successSend, $queueHelper->errorSend); |
| 382 |
$this->messages[] = $this->mainMessage; |
| 383 |
|
| 384 |
if (!empty($queueHelper->errorSend)) { |
| 385 |
$this->errorDetected = true; |
| 386 |
} |
| 387 |
|
| 388 |
$this->cronTimeLimit = $queueHelper->stoptime; |
| 389 |
} |
| 390 |
|
| 391 |
private function checkTimeRemaining(): void |
| 392 |
{ |
| 393 |
$time = time(); |
| 394 |
if (empty($this->cronTimeLimit) || $time < $this->cronTimeLimit) { |
| 395 |
return; |
| 396 |
} |
| 397 |
|
| 398 |
$this->messages[] = acym_translation('ACYM_CRON_TIME_LIMIT_REACHED'); |
| 399 |
|
| 400 |
$this->cronTimeLimitReached = true; |
| 401 |
} |
| 402 |
|
| 403 |
private function handleMultiCron(): void |
| 404 |
{ |
| 405 |
if (!empty($this->startQueue) || !acym_level(ACYM_ENTERPRISE)) { |
| 406 |
return; |
| 407 |
} |
| 408 |
|
| 409 |
$emailsBatches = $this->config->get('queue_batch_auto', 1); |
| 410 |
$emailsBatches = intval($emailsBatches); |
| 411 |
$emailsPerBatches = $this->config->get('queue_nbmail_auto', 70); |
| 412 |
if ($emailsBatches < 2 || empty($emailsPerBatches)) { |
| 413 |
return; |
| 414 |
} |
| 415 |
|
| 416 |
$cronKey = ''; |
| 417 |
if (!empty($this->config->get('cron_security', 0)) && !empty($this->config->get('cron_key'))) { |
| 418 |
$cronKey = '&cronKey='.$this->config->get('cron_key'); |
| 419 |
} |
| 420 |
|
| 421 |
$urls = []; |
| 422 |
for ($i = 1 ; $i <= $emailsBatches - 1 ; $i++) { |
| 423 |
$urls[] = acym_frontendLink('cron&task=cron&startqueue='.($emailsPerBatches * $i).'&t='.time().$cronKey); |
| 424 |
} |
| 425 |
|
| 426 |
acym_asyncCurlCall($urls); |
| 427 |
} |
| 428 |
|
| 429 |
private function handleBounceMessages(): void |
| 430 |
{ |
| 431 |
$time = time(); |
| 432 |
$autoBounceHandlingActive = (int)$this->config->get('auto_bounce', 0) === 1; |
| 433 |
$autoBounceHandlingNextTime = (int)$this->config->get('auto_bounce_next', 0); |
| 434 |
$autoBounceHandlingFrequency = (int)$this->config->get('auto_bounce_frequency', 0); |
| 435 |
$isEnterprise = acym_level(ACYM_ENTERPRISE); |
| 436 |
|
| 437 |
if (in_array(self::STEP_BOUNCE, $this->skip) || $this->cronTimeLimitReached || !$isEnterprise || !$autoBounceHandlingActive || $time < $autoBounceHandlingNextTime) { |
| 438 |
return; |
| 439 |
} |
| 440 |
|
| 441 |
$newConfig = new \stdClass(); |
| 442 |
$newConfig->auto_bounce_last = $time; |
| 443 |
$newConfig->auto_bounce_next = $time + $autoBounceHandlingFrequency; |
| 444 |
$this->config->save($newConfig); |
| 445 |
|
| 446 |
$bounceHelper = new BounceHelper(); |
| 447 |
$bounceHelper->report = false; |
| 448 |
$bounceHelper->stoptime = $this->cronTimeLimit; |
| 449 |
|
| 450 |
$newConfig = new \stdClass(); |
| 451 |
if ($bounceHelper->init() && $bounceHelper->connect()) { |
| 452 |
$nbMessages = $bounceHelper->getNBMessages(); |
| 453 |
$nbMessagesReport = acym_translationSprintf('ACYM_NB_MAIL_MAILBOX', $nbMessages); |
| 454 |
$this->messages[] = $nbMessagesReport; |
| 455 |
$newConfig->auto_bounce_report = $nbMessagesReport; |
| 456 |
$this->detailMessages[] = $nbMessagesReport; |
| 457 |
if (!empty($nbMessages)) { |
| 458 |
$bounceHelper->handleMessages(); |
| 459 |
$this->processed = true; |
| 460 |
} |
| 461 |
$this->detailMessages = array_merge($this->detailMessages, $bounceHelper->messages); |
| 462 |
} else { |
| 463 |
$bounceErrors = $bounceHelper->getErrors(); |
| 464 |
$newConfig->auto_bounce_report = implode('<br />', $bounceErrors); |
| 465 |
if (!empty($bounceErrors[0])) { |
| 466 |
$bounceErrors[0] = acym_translation('ACYM_BOUNCE_HANDLING').' : '.$bounceErrors[0]; |
| 467 |
} |
| 468 |
$this->messages = array_merge($this->messages, $bounceErrors); |
| 469 |
$this->processed = true; |
| 470 |
$this->errorDetected = true; |
| 471 |
} |
| 472 |
|
| 473 |
$this->config->save($newConfig); |
| 474 |
} |
| 475 |
|
| 476 |
private function handleAutomations(): void |
| 477 |
{ |
| 478 |
if (in_array(self::STEP_AUTOMATION, $this->skip) || $this->cronTimeLimitReached || !acym_level(ACYM_ENTERPRISE)) { |
| 479 |
return; |
| 480 |
} |
| 481 |
|
| 482 |
$automationClass = new AutomationClass(); |
| 483 |
$automationClass->trigger('classic'); |
| 484 |
|
| 485 |
$userStatusCheckTriggers = []; |
| 486 |
acym_trigger('onAcymDefineUserStatusCheckTriggers', [&$userStatusCheckTriggers]); |
| 487 |
$automationClass->trigger($userStatusCheckTriggers); |
| 488 |
|
| 489 |
if (!empty($automationClass->report)) { |
| 490 |
if ($automationClass->didAnAction) { |
| 491 |
$this->processed = true; |
| 492 |
} |
| 493 |
$this->messages = array_merge($this->messages, $automationClass->report); |
| 494 |
} |
| 495 |
} |
| 496 |
|
| 497 |
private function handleAutomaticCampaigns(): void |
| 498 |
{ |
| 499 |
if (in_array(self::STEP_CAMPAIGN, $this->skip) || $this->cronTimeLimitReached || !acym_level(ACYM_ENTERPRISE)) { |
| 500 |
return; |
| 501 |
} |
| 502 |
|
| 503 |
$campaignClass = new CampaignClass(); |
| 504 |
$campaignClass->triggerAutoCampaign(); |
| 505 |
|
| 506 |
if (!empty($campaignClass->messages)) { |
| 507 |
$this->messages = array_merge($this->messages, $campaignClass->messages); |
| 508 |
$this->processed = true; |
| 509 |
} |
| 510 |
} |
| 511 |
|
| 512 |
private function handleSpecificEmails(): void |
| 513 |
{ |
| 514 |
if (in_array(self::STEP_SPECIFIC, $this->skip) || $this->cronTimeLimitReached) { |
| 515 |
return; |
| 516 |
} |
| 517 |
|
| 518 |
$campaignClass = new CampaignClass(); |
| 519 |
$specialTypes = []; |
| 520 |
|
| 521 |
acym_trigger('getCampaignTypes', [&$specialTypes]); |
| 522 |
|
| 523 |
$specialMail = $campaignClass->getCampaignsByTypes($specialTypes, true); |
| 524 |
$time = time(); |
| 525 |
|
| 526 |
acym_trigger('filterSpecificMailsToSend', [&$specialMail, $time]); |
| 527 |
|
| 528 |
foreach ($specialMail as $onespecialMail) { |
| 529 |
$campaignClass->send($onespecialMail->id); |
| 530 |
} |
| 531 |
} |
| 532 |
|
| 533 |
private function handleFollowups(): void |
| 534 |
{ |
| 535 |
if (in_array(self::STEP_FOLLOWUP, $this->skip) || $this->cronTimeLimitReached) { |
| 536 |
return; |
| 537 |
} |
| 538 |
|
| 539 |
$followupClass = new FollowupClass(); |
| 540 |
$followups = $followupClass->getFollowupDailyBases(); |
| 541 |
|
| 542 |
$dailyHour = $this->config->get('daily_hour', '12'); |
| 543 |
$dailyMinute = $this->config->get('daily_minute', '00'); |
| 544 |
$dayBasedOnCMSTimezone = acym_date('now', 'Y-m-d'); |
| 545 |
$dayBasedOnCMSTimezoneAtSpecifiedHour = acym_getTimeFromCMSDate($dayBasedOnCMSTimezone.' '.$dailyHour.':'.$dailyMinute); |
| 546 |
$time = time(); |
| 547 |
|
| 548 |
foreach ($followups as $followup) { |
| 549 |
$lastTrigger = empty($followup->last_trigger) ? '' : acym_date($followup->last_trigger, 'Y-m-d'); |
| 550 |
if ($time >= $dayBasedOnCMSTimezoneAtSpecifiedHour && $lastTrigger != $dayBasedOnCMSTimezone) { |
| 551 |
if (!empty($followup->condition)) { |
| 552 |
$followup->condition = json_decode($followup->condition, true); |
| 553 |
} |
| 554 |
|
| 555 |
acym_trigger('onAcymFollowupDailyBasesNeedToBeTriggered', [$followup]); |
| 556 |
} |
| 557 |
} |
| 558 |
} |
| 559 |
|
| 560 |
private function handleMailboxActions(): void |
| 561 |
{ |
| 562 |
if (in_array(self::STEP_MAILBOX_ACTION, $this->skip) || $this->cronTimeLimitReached || !acym_level(ACYM_ENTERPRISE)) { |
| 563 |
return; |
| 564 |
} |
| 565 |
|
| 566 |
$mailboxActionClass = new MailboxClass(); |
| 567 |
$mailboxes = $mailboxActionClass->getAllActiveReadyWithActions(); |
| 568 |
|
| 569 |
if (empty($mailboxes)) { |
| 570 |
return; |
| 571 |
} |
| 572 |
|
| 573 |
$mailboxHelper = new MailboxHelper(); |
| 574 |
$mailboxHelper->report = false; |
| 575 |
foreach ($mailboxes as $oneMailboxAction) { |
| 576 |
$this->processed = true; |
| 577 |
|
| 578 |
if (!$mailboxHelper->isConnectionValid($oneMailboxAction, false)) { |
| 579 |
$this->messages[] = acym_translationSprintf('ACYM_CONNECTION_FAILED_MAILBOX_X', $oneMailboxAction->id.'-'.$oneMailboxAction->name); |
| 580 |
$this->errorDetected = true; |
| 581 |
continue; |
| 582 |
} |
| 583 |
|
| 584 |
$nbMessages = $mailboxHelper->getNBMessages(); |
| 585 |
if (!$nbMessages) { |
| 586 |
$this->messages[] = acym_translationSprintf('ACYM_NO_MESSAGE_IN_MAILBOX_X', $oneMailboxAction->id.'-'.$oneMailboxAction->name); |
| 587 |
$mailboxHelper->close(); |
| 588 |
continue; |
| 589 |
} |
| 590 |
|
| 591 |
$mailboxHelper->handleAction(); |
| 592 |
$mailboxHelper->close(); |
| 593 |
|
| 594 |
|
| 595 |
$oneMailboxAction->nextdate = time() + $oneMailboxAction->frequency; |
| 596 |
unset($oneMailboxAction->conditions); |
| 597 |
unset($oneMailboxAction->actions); |
| 598 |
$mailboxActionClass->save($oneMailboxAction); |
| 599 |
} |
| 600 |
} |
| 601 |
|
| 602 |
private function cleanData(): void |
| 603 |
{ |
| 604 |
if (!in_array(self::STEP_DELETE_HISTORY, $this->skip) && $this->isDailyCron()) { |
| 605 |
$userStatClass = new UserStatClass(); |
| 606 |
$userDetailedStatsDeleted = $userStatClass->deleteDetailedStatsPeriod(); |
| 607 |
if (!empty($userDetailedStatsDeleted['message'])) { |
| 608 |
$this->messages[] = $userDetailedStatsDeleted['message']; |
| 609 |
$this->processed = true; |
| 610 |
} |
| 611 |
|
| 612 |
$userClass = new UserClass(); |
| 613 |
$userHistoryDeleted = $userClass->deleteHistoryPeriod(); |
| 614 |
if (!empty($userHistoryDeleted['message'])) { |
| 615 |
$this->messages[] = $userHistoryDeleted['message']; |
| 616 |
$this->processed = true; |
| 617 |
} |
| 618 |
|
| 619 |
$mailArchiveClass = new MailArchiveClass(); |
| 620 |
$archiveDeleted = $mailArchiveClass->deleteArchivePeriod(); |
| 621 |
if (!empty($archiveDeleted['message'])) { |
| 622 |
$this->messages[] = $archiveDeleted['message']; |
| 623 |
$this->processed = true; |
| 624 |
} |
| 625 |
} |
| 626 |
|
| 627 |
if (!in_array(self::STEP_CLEAN_DATA_EXTERNAL_SENDING_METHOD, $this->skip) && $this->isDailyCron()) { |
| 628 |
acym_trigger('onAcymCleanDataExternalSendingMethod'); |
| 629 |
} |
| 630 |
|
| 631 |
if (!in_array(self::STEP_CLEAN_EXPORT_CHANGES, $this->skip) && $this->isDailyCron()) { |
| 632 |
$exportHelper = new ExportHelper(); |
| 633 |
$exportHelper->cleanExportChangesFile(); |
| 634 |
} |
| 635 |
|
| 636 |
if ($this->isDailyCron()) { |
| 637 |
$time = time(); |
| 638 |
$this->config->save(['cron_last_daily' => $time]); |
| 639 |
} |
| 640 |
} |
| 641 |
|
| 642 |
private function isDailyCron(): bool |
| 643 |
{ |
| 644 |
$dailyHour = $this->config->get('daily_hour', '12'); |
| 645 |
$dailyMinute = $this->config->get('daily_minute', '00'); |
| 646 |
$dayBasedOnCMSTimezone = acym_date('now', 'Y-m-d'); |
| 647 |
$dayBasedOnCMSTimezoneAtSpecifiedHour = acym_getTimeFromCMSDate($dayBasedOnCMSTimezone.' '.$dailyHour.':'.$dailyMinute); |
| 648 |
|
| 649 |
$time = time(); |
| 650 |
|
| 651 |
$lastCronDayBasedOnCMSTimezone = acym_date($this->config->get('cron_last_daily', $time - 86400), 'Y-m-d'); |
| 652 |
|
| 653 |
return $time >= $dayBasedOnCMSTimezoneAtSpecifiedHour && $lastCronDayBasedOnCMSTimezone != $dayBasedOnCMSTimezone; |
| 654 |
} |
| 655 |
|
| 656 |
private function handleABTestCampaigns(): void |
| 657 |
{ |
| 658 |
if (in_array(self::STEP_ABTEST, $this->skip) || $this->cronTimeLimitReached) { |
| 659 |
return; |
| 660 |
} |
| 661 |
|
| 662 |
$campaignClass = new CampaignClass(); |
| 663 |
$abTestCampaigns = $campaignClass->getAllAbTestCampaignsToFinishSending(); |
| 664 |
foreach ($abTestCampaigns as $campaign) { |
| 665 |
if ($campaignClass->finishAbTestCampaign($campaign)) { |
| 666 |
$this->messages[] = acym_translationSprintf('ACYM_ABTEST_CAMPAIGN_X_FINAL_VERSION_SENT', $campaign->id); |
| 667 |
$this->processed = true; |
| 668 |
} |
| 669 |
} |
| 670 |
} |
| 671 |
|
| 672 |
private function handleScenario(): void |
| 673 |
{ |
| 674 |
if (in_array(self::STEP_SCENARIO, $this->skip) || $this->cronTimeLimitReached) { |
| 675 |
return; |
| 676 |
} |
| 677 |
|
| 678 |
$scenarioHelper = new ScenarioHelper(); |
| 679 |
$scenarioHelper->executeAvailableSteps(); |
| 680 |
$scenarioHelper->triggerTimeScenarios(); |
| 681 |
} |
| 682 |
|
| 683 |
private function continueCronCall(): void |
| 684 |
{ |
| 685 |
if ($this->externalSendingNotFinished) { |
| 686 |
$cronKey = ''; |
| 687 |
if (!empty($this->config->get('cron_security', 0)) && !empty($this->config->get('cron_key'))) { |
| 688 |
$cronKey = '&cronKey='.$this->config->get('cron_key'); |
| 689 |
} |
| 690 |
acym_makeCurlCall(acym_frontendLink('cron&task=cron&external_sending_repeat=1&t='.time().$cronKey), ['verifySsl' => false]); |
| 691 |
} |
| 692 |
} |
| 693 |
} |
| 694 |
|