| @@ -1,0 +1,294 @@ | ||
| 1 | +<?php | |
| 2 | + | |
| 3 | +namespace AcyMailing\FrontControllers; | |
| 4 | + | |
| 5 | +use AcyMailing\Classes\CampaignClass; | |
| 6 | +use AcyMailing\Classes\HistoryClass; | |
| 7 | +use AcyMailing\Classes\MailArchiveClass; | |
| 8 | +use AcyMailing\Classes\MailClass; | |
| 9 | +use AcyMailing\Classes\UserClass; | |
| 10 | +use AcyMailing\Classes\UserStatClass; | |
| 11 | +use AcyMailing\Helpers\EditorHelper; | |
| 12 | +use AcyMailing\Helpers\MailerHelper; | |
| 13 | +use AcyMailing\Helpers\PaginationHelper; | |
| 14 | +use AcyMailing\Core\AcymController; | |
| 15 | +use AcyMailing\Core\AcymParameter; | |
| 16 | + | |
| 17 | +class ArchiveController extends AcymController | |
| 18 | +{ | |
| 19 | + public function __construct() | |
| 20 | + { | |
| 21 | + parent::__construct(); | |
| 22 | + $this->setDefaultTask('view'); | |
| 23 | + | |
| 24 | + $this->publicFrontTasks = [ | |
| 25 | + 'view', | |
| 26 | + 'showArchive', | |
| 27 | + 'listing', | |
| 28 | + ]; | |
| 29 | + } | |
| 30 | + | |
| 31 | + public function view(): void | |
| 32 | + { | |
| 33 | + if ($this->config->get('archive_index', 0) == 0) { | |
| 34 | + acym_addMetadata('robots', 'noindex,nofollow'); | |
| 35 | + } | |
| 36 | + | |
| 37 | + acym_addMetadata('UTF-8', '', 'charset'); | |
| 38 | + | |
| 39 | + $mailId = acym_getVar('int', 'id', 0); | |
| 40 | + $isPopup = acym_getVar('int', 'is_popup', 0); | |
| 41 | + | |
| 42 | + $mailArchiveClass = new MailArchiveClass(); | |
| 43 | + $oneMail = $mailArchiveClass->getOneByMailId($mailId); | |
| 44 | + if (empty($oneMail) && $this->canViewMailOnline($mailId)) { | |
| 45 | + $mailerHelper = new MailerHelper(); | |
| 46 | + $oneMail = $mailerHelper->load($mailId); | |
| 47 | + } | |
| 48 | + | |
| 49 | + if (empty($oneMail)) { | |
| 50 | + acym_raiseError(404, acym_translation('ACYM_EMAIL_NOT_FOUND')); | |
| 51 | + | |
| 52 | + return; | |
| 53 | + } | |
| 54 | + | |
| 55 | + acym_addMetadata('og:url', acym_frontendLink('archive&task=view&id='.$mailId)); | |
| 56 | + acym_addMetadata('og:title', $oneMail->subject); | |
| 57 | + acym_setPageTitle($oneMail->subject); | |
| 58 | + | |
| 59 | + // Replace the user Dtexts for the preview if the key/userid here | |
| 60 | + $userKeys = acym_getVar('string', 'userid', 0); | |
| 61 | + if (!empty($userKeys)) { | |
| 62 | + $userId = intval(substr($userKeys, 0, strpos($userKeys, '-'))); | |
| 63 | + $userKey = substr($userKeys, strpos($userKeys, '-') + 1); | |
| 64 | + $receiver = acym_loadObject('SELECT * FROM #__acym_user WHERE `id` = '.intval($userId).' AND `key` = '.acym_escapeDB($userKey)); | |
| 65 | + } | |
| 66 | + | |
| 67 | + $currentEmail = acym_currentUserEmail(); | |
| 68 | + if (empty($receiver) && !empty($currentEmail)) { | |
| 69 | + $userClass = new UserClass(); | |
| 70 | + $receiver = $userClass->getOneByEmail($currentEmail); | |
| 71 | + } | |
| 72 | + | |
| 73 | + if (empty($receiver)) { | |
| 74 | + $receiver = new \stdClass(); | |
| 75 | + $receiver->name = acym_translation('ACYM_VISITOR'); | |
| 76 | + } | |
| 77 | + | |
| 78 | + acym_trigger('replaceUserInformation', [&$oneMail, &$receiver, false]); | |
| 79 | + | |
| 80 | + // If there is the unsubscribe link for elastic email | |
| 81 | + preg_match('@href="{unsubscribe:(.*)}"@', $oneMail->body, $match); | |
| 82 | + if (!empty($match)) { | |
| 83 | + //We replace the tag by the url | |
| 84 | + $oneMail->body = str_replace($match[0], 'href="'.$match[1].'"', $oneMail->body); | |
| 85 | + } | |
| 86 | + | |
| 87 | + // Add foundation for email CSS only for D&D emails | |
| 88 | + if (strpos($oneMail->body, 'acym__wysid__template') !== false) { | |
| 89 | + acym_addStyle(false, ACYM_CSS.'libraries/foundation_email.min.css?v='.filemtime(ACYM_MEDIA.'css'.DS.'libraries'.DS.'foundation_email.min.css')); | |
| 90 | + } | |
| 91 | + acym_addStyle(true, acym_getEmailCssFixes()); | |
| 92 | + if (!empty($oneMail->stylesheet)) { | |
| 93 | + acym_addStyle(true, $oneMail->stylesheet); | |
| 94 | + } | |
| 95 | + $editorHelper = new EditorHelper(); | |
| 96 | + $settings = json_decode($oneMail->settings ?? '[]', true); | |
| 97 | + if (!empty($settings)) { | |
| 98 | + $settings = $editorHelper->getSettingsStyle($settings); | |
| 99 | + | |
| 100 | + if (!empty($settings)) { | |
| 101 | + acym_addStyle(true, $settings); | |
| 102 | + } | |
| 103 | + } | |
| 104 | + | |
| 105 | + // Make sure the background image works on the archive | |
| 106 | + $oneMail->body = preg_replace('#background\-image: url\("([^)]*)"\)#Uis', 'background-image: url($1)', $oneMail->body); | |
| 107 | + | |
| 108 | + $data = [ | |
| 109 | + 'mail' => $oneMail, | |
| 110 | + 'receiver' => $receiver, | |
| 111 | + ]; | |
| 112 | + | |
| 113 | + acym_includeHeaders(); | |
| 114 | + parent::display($data); | |
| 115 | + | |
| 116 | + // We are forced to use exit because of WordPress that displays a 0 if an exit isn't used | |
| 117 | + if ($isPopup || 'wordpress' === ACYM_CMS) exit; | |
| 118 | + } | |
| 119 | + | |
| 120 | + private function canViewMailOnline(int $mailId): bool | |
| 121 | + { | |
| 122 | + if (empty($mailId)) { | |
| 123 | + return false; | |
| 124 | + } | |
| 125 | + | |
| 126 | + $mailClass = new MailClass(); | |
| 127 | + if ($mailClass->hasUserAccess($mailId)) { | |
| 128 | + return true; | |
| 129 | + } | |
| 130 | + | |
| 131 | + $campaignClass = new CampaignClass(); | |
| 132 | + $campaign = $campaignClass->getOneByMailId($mailId); | |
| 133 | + if ( | |
| 134 | + !empty($campaign) | |
| 135 | + && (int)$campaign->visible === 1 | |
| 136 | + && (int)$campaign->active === 1 | |
| 137 | + && (int)$campaign->sent === 1 | |
| 138 | + ) { | |
| 139 | + return true; | |
| 140 | + } | |
| 141 | + | |
| 142 | + $userKeys = acym_getVar('string', 'userid', ''); | |
| 143 | + $separatorPosition = strpos($userKeys, '-'); | |
| 144 | + if ($separatorPosition === false) { | |
| 145 | + return false; | |
| 146 | + } | |
| 147 | + | |
| 148 | + $userId = intval(substr($userKeys, 0, $separatorPosition)); | |
| 149 | + $userKey = substr($userKeys, $separatorPosition + 1); | |
| 150 | + if (empty($userId) || empty($userKey)) { | |
| 151 | + return false; | |
| 152 | + } | |
| 153 | + | |
| 154 | + $historyClass = new HistoryClass(); | |
| 155 | + $userHistory = $historyClass->getHistoryOfOneById($userId); | |
| 156 | + foreach ($userHistory as $userHistoryItem) { | |
| 157 | + if ((int)$userHistoryItem->mail_id === $mailId && $userHistoryItem->action === 'test_sent') { | |
| 158 | + return true; | |
| 159 | + } | |
| 160 | + } | |
| 161 | + | |
| 162 | + acym_setVar('id', $userId); | |
| 163 | + acym_setVar('key', $userKey); | |
| 164 | + $userClass = new UserClass(); | |
| 165 | + $user = $userClass->identify(true); | |
| 166 | + | |
| 167 | + if (empty($user)) { | |
| 168 | + return false; | |
| 169 | + } | |
| 170 | + | |
| 171 | + if (!empty($user->cms_id) && $mailClass->hasUserAccess($mailId, false, $user->cms_id)) { | |
| 172 | + return true; | |
| 173 | + } | |
| 174 | + | |
| 175 | + $userStatClass = new UserStatClass(); | |
| 176 | + | |
| 177 | + return $userStatClass->hasUserReceivedMail($mailId, $userId); | |
| 178 | + } | |
| 179 | + | |
| 180 | + public function listing(): void | |
| 181 | + { | |
| 182 | + acym_setVar('layout', 'listing'); | |
| 183 | + | |
| 184 | + $search = acym_getVar('string', 'acym_search', ''); | |
| 185 | + | |
| 186 | + // Get the Joomla menu parameters | |
| 187 | + $menu = acym_getMenu(); | |
| 188 | + if (!is_object($menu)) { | |
| 189 | + acym_redirect(acym_rootURI()); | |
| 190 | + | |
| 191 | + return; | |
| 192 | + } | |
| 193 | + $params = method_exists($menu, 'getParams') ? $menu->getParams() : $menu->params; | |
| 194 | + $menuParams = new AcymParameter($params); | |
| 195 | + | |
| 196 | + // Handle the core Joomla params | |
| 197 | + $paramsJoomla = []; | |
| 198 | + $paramsJoomla['suffix'] = $menuParams->get('pageclass_sfx', ''); | |
| 199 | + $paramsJoomla['page_heading'] = $menuParams->get('page_heading'); | |
| 200 | + $paramsJoomla['show_page_heading'] = $menuParams->get('show_page_heading', 0); | |
| 201 | + | |
| 202 | + if ($menuParams->get('menu-meta_description')) { | |
| 203 | + acym_addMetadata('description', $menuParams->get('menu-meta_description')); | |
| 204 | + } | |
| 205 | + | |
| 206 | + if ($menuParams->get('menu-meta_keywords')) { | |
| 207 | + acym_addMetadata('keywords', $menuParams->get('menu-meta_keywords')); | |
| 208 | + } | |
| 209 | + | |
| 210 | + if ($menuParams->get('robots')) { | |
| 211 | + acym_addMetadata('robots', $menuParams->get('robots')); | |
| 212 | + } | |
| 213 | + | |
| 214 | + // Initialize our own params then call the generic view | |
| 215 | + $nbNewslettersPerPage = $menuParams->get('archiveNbNewslettersPerPage', 10); | |
| 216 | + $listsSent = $menuParams->get('lists', ''); | |
| 217 | + $popup = $menuParams->get('popup', '1'); | |
| 218 | + $displayUserListOnly = $menuParams->get('displayUserListOnly', '1'); | |
| 219 | + | |
| 220 | + $viewParams = [ | |
| 221 | + 'nbNewslettersPerPage' => $nbNewslettersPerPage, | |
| 222 | + 'listsSent' => $listsSent, | |
| 223 | + 'popup' => $popup, | |
| 224 | + 'paramsCMS' => $paramsJoomla, | |
| 225 | + 'search' => $search, | |
| 226 | + 'displayUserListOnly' => $displayUserListOnly, | |
| 227 | + ]; | |
| 228 | + | |
| 229 | + $this->showArchive($viewParams); | |
| 230 | + } | |
| 231 | + | |
| 232 | + public function showArchive(array $viewParams): void | |
| 233 | + { | |
| 234 | + acym_setVar('layout', 'listing'); | |
| 235 | + | |
| 236 | + $data = $this->getDataForArchive($viewParams); | |
| 237 | + | |
| 238 | + parent::display($data); | |
| 239 | + } | |
| 240 | + | |
| 241 | + private function getDataForArchive(array $viewParams): array | |
| 242 | + { | |
| 243 | + $params = []; | |
| 244 | + | |
| 245 | + $userId = false; | |
| 246 | + $userClass = new UserClass(); | |
| 247 | + $currentUser = $userClass->identify(true); | |
| 248 | + if (!empty($currentUser)) { | |
| 249 | + $params['userId'] = $currentUser->id; | |
| 250 | + $userId = $currentUser->id; | |
| 251 | + } | |
| 252 | + | |
| 253 | + if (acym_isMultilingual()) { | |
| 254 | + $params['language'] = acym_getLanguageTag(); | |
| 255 | + } | |
| 256 | + | |
| 257 | + if (!empty($viewParams['listsSent'])) { | |
| 258 | + $params['lists'] = $viewParams['listsSent']; | |
| 259 | + } | |
| 260 | + | |
| 261 | + if (!empty($viewParams['search'])) { | |
| 262 | + $params['search'] = $viewParams['search']; | |
| 263 | + } | |
| 264 | + | |
| 265 | + if (!empty($viewParams['displayUserListOnly'])) { | |
| 266 | + $params['displayUserListOnly'] = $viewParams['displayUserListOnly']; | |
| 267 | + } | |
| 268 | + | |
| 269 | + $params['page'] = max(1, $this->getVarFiltersListing('int', 'archive_pagination_page', 1)); | |
| 270 | + $campaignClass = new CampaignClass(); | |
| 271 | + $pagination = new PaginationHelper(); | |
| 272 | + $params['numberPerPage'] = (int)$viewParams['nbNewslettersPerPage']; | |
| 273 | + | |
| 274 | + $returnLastNewsletters = $campaignClass->getLastNewsletters($params); | |
| 275 | + $pagination->setStatus($returnLastNewsletters['count'], $params['page'], $params['numberPerPage']); | |
| 276 | + | |
| 277 | + $disableButtons = ''; | |
| 278 | + if (isset($viewParams['disableButtons'])) { | |
| 279 | + $disableButtons = $viewParams['disableButtons']; | |
| 280 | + } | |
| 281 | + | |
| 282 | + return [ | |
| 283 | + 'newsletters' => $returnLastNewsletters['matchingNewsletters'], | |
| 284 | + 'paramsCMS' => $viewParams['paramsCMS'], | |
| 285 | + 'pagination' => $pagination, | |
| 286 | + 'userId' => $userId, | |
| 287 | + 'popup' => '1' === $viewParams['popup'], | |
| 288 | + 'displayUserListOnly' => '1' === $viewParams['displayUserListOnly'], | |
| 289 | + 'search' => $viewParams['search'], | |
| 290 | + 'actionUrl' => acym_currentURL(), | |
| 291 | + 'disableButtons' => $disableButtons, | |
| 292 | + ]; | |
| 293 | + } | |
| 294 | +} | |