| 1 |
<?php |
| 2 |
|
| 3 |
namespace AcyMailing\FrontControllers; |
| 4 |
|
| 5 |
use AcyMailing\Classes\MailStatClass; |
| 6 |
use AcyMailing\Classes\UserClass; |
| 7 |
use AcyMailing\Classes\UserStatClass; |
| 8 |
use AcyMailing\Core\AcymController; |
| 9 |
use AcyMailing\Libraries\Browser\BrowserDetection; |
| 10 |
|
| 11 |
class FrontstatsController extends AcymController |
| 12 |
{ |
| 13 |
public function __construct() |
| 14 |
{ |
| 15 |
parent::__construct(); |
| 16 |
|
| 17 |
$this->publicFrontTasks = [ |
| 18 |
'openStats', |
| 19 |
]; |
| 20 |
} |
| 21 |
|
| 22 |
public function openStats(): void |
| 23 |
{ |
| 24 |
$mailId = acym_getVar('int', 'id'); |
| 25 |
$userId = acym_getVar('int', 'userid'); |
| 26 |
|
| 27 |
if (!empty($mailId) && !empty($userId) && !acym_isRobot()) { |
| 28 |
$this->recordOpen($mailId, $userId); |
| 29 |
} |
| 30 |
|
| 31 |
acym_noCache(); |
| 32 |
if (ob_get_level() > 0) { |
| 33 |
ob_end_clean(); |
| 34 |
} |
| 35 |
|
| 36 |
$statsPicture = ACYM_MEDIA_RELATIVE.'images/editor/statpicture.png'; |
| 37 |
$statsPicture = ACYM_ROOT.ltrim(str_replace(['\\', '/'], DS, $statsPicture), DS); |
| 38 |
|
| 39 |
acym_header('Content-type: image/png'); |
| 40 |
// phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_readfile -- Efficient and less memory usage. |
| 41 |
readfile($statsPicture); |
| 42 |
exit; |
| 43 |
} |
| 44 |
|
| 45 |
private function recordOpen(int $mailId, int $userId): void |
| 46 |
{ |
| 47 |
$userStatClass = new UserStatClass(); |
| 48 |
$userStat = $userStatClass->getOneByMailAndUserId($mailId, $userId); |
| 49 |
|
| 50 |
// Ignore opens within X seconds following the email sending to prevent bots from impacting statistics |
| 51 |
$delay = $this->config->get('tracking_delay', 0); |
| 52 |
if (empty($userStat) || acym_isRobot() || (!empty($delay) && acym_getTimeFromUTCDate($userStat->send_date) > time() - $delay)) { |
| 53 |
return; |
| 54 |
} |
| 55 |
|
| 56 |
$anonymousStats = !empty($this->config->get('anonymous_stats', 0)); |
| 57 |
|
| 58 |
$mailStat = new \stdClass(); |
| 59 |
$mailStat->mail_id = $mailId; |
| 60 |
$mailStat->open_total = 1; |
| 61 |
$mailStat->open_unique = $anonymousStats ? 1 : ($userStat->open > 0 ? 0 : 1); |
| 62 |
|
| 63 |
$mailStatClass = new MailStatClass(); |
| 64 |
$mailStatClass->save($mailStat); |
| 65 |
|
| 66 |
$openDate = acym_date('now', 'Y-m-d H:i:s', false); |
| 67 |
|
| 68 |
$device = ''; |
| 69 |
$openedWith = ''; |
| 70 |
$userAgent = acym_getVar('string', 'HTTP_USER_AGENT', null, 'SERVER'); |
| 71 |
if (!empty($userAgent)) { |
| 72 |
$browserDetection = new BrowserDetection(); |
| 73 |
$openingInformation = $browserDetection->getAll($userAgent); |
| 74 |
|
| 75 |
$device = $openingInformation['os_name'] === 'unknown' ? '' : $openingInformation['os_name']; |
| 76 |
$openedWith = $openingInformation['browser_name'] === 'unknown' ? '' : $openingInformation['browser_name']; |
| 77 |
} |
| 78 |
|
| 79 |
if ($anonymousStats) { |
| 80 |
$mailStatClass->incrementDetail($mailId, 'device', $device); |
| 81 |
$mailStatClass->incrementDetail($mailId, 'client', $openedWith); |
| 82 |
$mailStatClass->incrementDetail($mailId, 'open_time', acym_date('now', 'Y-m-d H', false).':00:00'); |
| 83 |
} else { |
| 84 |
$userStatToInsert = new \stdClass(); |
| 85 |
$userStatToInsert->user_id = $userId; |
| 86 |
$userStatToInsert->mail_id = $mailId; |
| 87 |
$userStatToInsert->open = 1; |
| 88 |
$userStatToInsert->open_date = $openDate; |
| 89 |
$userStatToInsert->device = $device; |
| 90 |
$userStatToInsert->opened_with = $openedWith; |
| 91 |
$userStatClass->save($userStatToInsert); |
| 92 |
} |
| 93 |
|
| 94 |
UserStatClass::triggerActivity('user_open', $userId, $mailId); |
| 95 |
|
| 96 |
$userClass = new UserClass(); |
| 97 |
$subscriber = $userClass->getOneById($userId); |
| 98 |
if (!empty($subscriber)) { |
| 99 |
$subscriber->last_open_date = $openDate; |
| 100 |
$userClass->triggers = false; |
| 101 |
$userClass->sendConf = false; |
| 102 |
$userClass->save($subscriber); |
| 103 |
} |
| 104 |
} |
| 105 |
} |
| 106 |
|