| @@ -1,0 +1,124 @@ | ||
| 1 | +<?php | |
| 2 | + | |
| 3 | +namespace AcyMailing\Classes; | |
| 4 | + | |
| 5 | +use AcyMailing\Core\AcymClass; | |
| 6 | + | |
| 7 | +class HistoryClass extends AcymClass | |
| 8 | +{ | |
| 9 | + public function __construct() | |
| 10 | + { | |
| 11 | + parent::__construct(); | |
| 12 | + | |
| 13 | + $this->table = 'history'; | |
| 14 | + } | |
| 15 | + | |
| 16 | + /** | |
| 17 | + * Function insert to insert a line into the history... | |
| 18 | + * User modification, user update, bounces... etc. | |
| 19 | + */ | |
| 20 | + public function insert(int $userId, string $action, array $data = [], int $mailid = 0, $unsubscribe_reason = null): void | |
| 21 | + { | |
| 22 | + $currentUserid = acym_currentUserId(); | |
| 23 | + if (!empty($currentUserid)) { | |
| 24 | + $data[] = acym_translation('EXECUTED_BY').'::'.$currentUserid.' ( '.acym_currentUserName().' )'; | |
| 25 | + } | |
| 26 | + $history = new \stdClass(); | |
| 27 | + $history->user_id = intval($userId); | |
| 28 | + $history->action = acym_stripTags($action); | |
| 29 | + $history->data = implode("\n", $data); | |
| 30 | + $history->unsubscribe_reason = $unsubscribe_reason; | |
| 31 | + //Avoid a memory issue when the data is way too big. | |
| 32 | + if (strlen($history->data) > 100000) { | |
| 33 | + $history->data = substr($history->data, 0, 10000); | |
| 34 | + } | |
| 35 | + | |
| 36 | + static $date = null; | |
| 37 | + if (empty($date)) { | |
| 38 | + $date = time(); | |
| 39 | + } | |
| 40 | + | |
| 41 | + $history->date = ++$date; | |
| 42 | + while ($this->alreadyExists($history->user_id, $history->date)) { | |
| 43 | + $history->date++; | |
| 44 | + } | |
| 45 | + | |
| 46 | + $date = $history->date; | |
| 47 | + | |
| 48 | + $history->mail_id = $mailid; | |
| 49 | + $history->ip = acym_getIP(); | |
| 50 | + | |
| 51 | + if (!empty($_SERVER) && empty($this->config->get('anonymous_stats', 0))) { | |
| 52 | + $source = []; | |
| 53 | + $vars = [ | |
| 54 | + 'HTTP_REFERER', | |
| 55 | + 'HTTP_USER_AGENT', | |
| 56 | + 'HTTP_HOST', | |
| 57 | + 'SERVER_ADDR', | |
| 58 | + 'REMOTE_ADDR', | |
| 59 | + 'REQUEST_URI', | |
| 60 | + 'QUERY_STRING', | |
| 61 | + ]; | |
| 62 | + | |
| 63 | + foreach ($vars as $oneVar) { | |
| 64 | + $remoteIp = acym_getVar('string', $oneVar, '', 'SERVER'); | |
| 65 | + if (!empty($remoteIp)) { | |
| 66 | + $source[] = $oneVar.'::'.acym_stripTags($remoteIp); | |
| 67 | + } | |
| 68 | + } | |
| 69 | + $history->source = implode("\n", $source); | |
| 70 | + } | |
| 71 | + | |
| 72 | + try { | |
| 73 | + acym_insertObject('#__acym_history', $history); | |
| 74 | + } catch (\Exception $e) { | |
| 75 | + } | |
| 76 | + } | |
| 77 | + | |
| 78 | + private function alreadyExists(int $userId, int $time): bool | |
| 79 | + { | |
| 80 | + $result = acym_loadResult('SELECT user_id FROM #__acym_history WHERE user_id = '.intval($userId).' AND date = '.intval($time)); | |
| 81 | + | |
| 82 | + return !empty($result); | |
| 83 | + } | |
| 84 | + | |
| 85 | + /** | |
| 86 | + * Get all history lines for one user order by date descending | |
| 87 | + */ | |
| 88 | + public function getHistoryOfOneById(int $userId): array | |
| 89 | + { | |
| 90 | + return acym_loadObjectList( | |
| 91 | + 'SELECT h.*, m.id, m.subject FROM #__acym_history AS h | |
| 92 | + LEFT JOIN #__acym_mail AS m ON h.mail_id = m.id | |
| 93 | + WHERE h.user_id = '.intval($userId).' | |
| 94 | + ORDER BY h.date DESC' | |
| 95 | + ); | |
| 96 | + } | |
| 97 | + | |
| 98 | + public function getAllUnsubReasons(): array | |
| 99 | + { | |
| 100 | + $query = 'SELECT `action`, `data`, `unsubscribe_reason` FROM #__acym_history WHERE `action` = "unsubscribed" AND `unsubscribe_reason` != ""'; | |
| 101 | + | |
| 102 | + return acym_loadObjectList($query); | |
| 103 | + } | |
| 104 | + | |
| 105 | + public function getAllMainLanguageUnsubReasons(): array | |
| 106 | + { | |
| 107 | + $allUnsubReasons = $this->config->get('unsub_survey', '{}'); | |
| 108 | + | |
| 109 | + return json_decode($allUnsubReasons, true); | |
| 110 | + } | |
| 111 | + | |
| 112 | + public function getUnsubscribeReasonText($index) | |
| 113 | + { | |
| 114 | + $surveyAnswers = json_decode($this->config->get('unsub_survey'), true); | |
| 115 | + if (is_numeric($index)) { | |
| 116 | + $index = $index - 1; | |
| 117 | + $reason = $surveyAnswers[$index] ?? $index; | |
| 118 | + } else { | |
| 119 | + $reason = $index; | |
| 120 | + } | |
| 121 | + | |
| 122 | + return $reason; | |
| 123 | + } | |
| 124 | +} | |