| @@ -5,9 +5,9 @@ | ||
| 5 | 5 | use AcyMailing\Classes\MailStatClass; |
| 6 | 6 | use AcyMailing\Classes\UserClass; |
| 7 | 7 | use AcyMailing\Classes\UserStatClass; |
| 8 | 8 | use AcyMailing\Core\AcymController; |
| 9 | -use AcyMailing\Libraries\Browser\Browser; | |
| 9 | +use AcyMailing\Libraries\Browser\BrowserDetection; | |
| 10 | 10 | |
| 11 | 11 | class FrontstatsController extends AcymController |
| 12 | 12 | { |
| 13 | 13 | public function __construct() |
| @@ -18,87 +18,88 @@ | ||
| 18 | 18 | 'openStats', |
| 19 | 19 | ]; |
| 20 | 20 | } |
| 21 | 21 | |
| 22 | - public function openStats() | |
| 22 | + public function openStats(): void | |
| 23 | 23 | { |
| 24 | 24 | $mailId = acym_getVar('int', 'id'); |
| 25 | 25 | $userId = acym_getVar('int', 'userid'); |
| 26 | 26 | |
| 27 | + if (!empty($mailId) && !empty($userId) && !acym_isRobot()) { | |
| 28 | + $this->recordOpen($mailId, $userId); | |
| 29 | + } | |
| 27 | 30 | |
| 28 | - if (!empty($mailId) && !empty($userId) && !acym_isRobot()) { | |
| 29 | - $userStatClass = new UserStatClass(); | |
| 30 | - $userStat = $userStatClass->getOneByMailAndUserId($mailId, $userId); | |
| 31 | - if (!empty($userStat)) { | |
| 32 | - $openUnique = 1; | |
| 33 | - if ($userStat->open > 0) { | |
| 34 | - $openUnique = 0; | |
| 35 | - } | |
| 31 | + acym_noCache(); | |
| 32 | + if (ob_get_level() > 0) { | |
| 33 | + ob_end_clean(); | |
| 34 | + } | |
| 36 | 35 | |
| 37 | - $mailStat = [ | |
| 38 | - 'mail_id' => $mailId, | |
| 39 | - 'open_unique' => $openUnique, | |
| 40 | - 'open_total' => 1, | |
| 41 | - ]; | |
| 36 | + $statsPicture = ACYM_MEDIA_RELATIVE.'images/editor/statpicture.png'; | |
| 37 | + $statsPicture = ACYM_ROOT.ltrim(str_replace(['\\', '/'], DS, $statsPicture), DS); | |
| 42 | 38 | |
| 43 | - $mailStatClass = new MailStatClass(); | |
| 44 | - $mailStatClass->save($mailStat); | |
| 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 | + } | |
| 45 | 44 | |
| 46 | - $device = ''; | |
| 47 | - $openedWith = ''; | |
| 48 | - if (isset($_SERVER['HTTP_USER_AGENT'])) { | |
| 49 | - $userAgent = strtolower($_SERVER['HTTP_USER_AGENT']); | |
| 50 | - $allDevices = array_merge(UserStatClass::DESKTOP_DEVICES, UserStatClass::MOBILE_DEVICES); | |
| 45 | + private function recordOpen(int $mailId, int $userId): void | |
| 46 | + { | |
| 47 | + $userStatClass = new UserStatClass(); | |
| 48 | + $userStat = $userStatClass->getOneByMailAndUserId($mailId, $userId); | |
| 51 | 49 | |
| 52 | - foreach ($allDevices as $oneDeviceKey => $oneDevice) { | |
| 53 | - if (preg_match('/'.$oneDeviceKey.'/', $userAgent)) { | |
| 54 | - $device = $oneDeviceKey; | |
| 55 | - break; | |
| 56 | - } | |
| 57 | - } | |
| 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 | + } | |
| 58 | 55 | |
| 59 | - $browser = new Browser($userAgent); | |
| 60 | - $openedWith = $browser->getBrowser(); | |
| 61 | - } | |
| 56 | + $anonymousStats = !empty($this->config->get('anonymous_stats', 0)); | |
| 62 | 57 | |
| 63 | - $userStatToInsert = []; | |
| 64 | - $userStatToInsert['user_id'] = $userId; | |
| 65 | - $userStatToInsert['mail_id'] = $mailId; | |
| 66 | - $userStatToInsert['open'] = 1; | |
| 67 | - $userStatToInsert['open_date'] = acym_date('now', 'Y-m-d H:i:s'); | |
| 68 | - $userStatToInsert['device'] = $device; | |
| 69 | - $userStatToInsert['opened_with'] = $openedWith; | |
| 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); | |
| 70 | 62 | |
| 71 | - $userStatClass->save($userStatToInsert); | |
| 63 | + $mailStatClass = new MailStatClass(); | |
| 64 | + $mailStatClass->save($mailStat); | |
| 72 | 65 | |
| 73 | - $userClass = new UserClass(); | |
| 74 | - $subscriber = $userClass->getOneById($userId); | |
| 75 | - if (!empty($subscriber)) { | |
| 76 | - $subscriber->last_open_date = acym_date('now', 'Y-m-d H:i:s'); | |
| 77 | - $userClass->triggers = false; | |
| 78 | - $userClass->sendConf = false; | |
| 79 | - $userClass->save($subscriber); | |
| 80 | - } | |
| 81 | - } | |
| 82 | - } | |
| 66 | + $openDate = acym_date('now', 'Y-m-d H:i:s', false); | |
| 83 | 67 | |
| 84 | - acym_noCache(); | |
| 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); | |
| 85 | 74 | |
| 86 | - ob_end_clean(); | |
| 75 | + $device = $openingInformation['os_name'] === 'unknown' ? '' : $openingInformation['os_name']; | |
| 76 | + $openedWith = $openingInformation['browser_name'] === 'unknown' ? '' : $openingInformation['browser_name']; | |
| 77 | + } | |
| 87 | 78 | |
| 88 | - $picture = ACYM_MEDIA_RELATIVE.'images/editor/statpicture.png'; | |
| 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 | + } | |
| 89 | 93 | |
| 90 | - $picture = ltrim(str_replace(['\\', '/'], DS, $picture), DS); | |
| 94 | + UserStatClass::triggerActivity('user_open', $userId, $mailId); | |
| 91 | 95 | |
| 92 | - $imagename = ACYM_ROOT.$picture; | |
| 93 | - $handle = fopen($imagename, 'r'); | |
| 94 | - if (!$handle) { | |
| 95 | - exit; | |
| 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); | |
| 96 | 103 | } |
| 97 | - | |
| 98 | - acym_header('Content-type: image/png'); | |
| 99 | - $contents = fread($handle, filesize($imagename)); | |
| 100 | - fclose($handle); | |
| 101 | - echo $contents; | |
| 102 | - exit; | |
| 103 | 104 | } |
| 104 | 105 | } |