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