| 1 |
<?php |
| 2 |
|
| 3 |
namespace AcyMailing\Helpers; |
| 4 |
|
| 5 |
use AcyMailing\Core\AcymObject; |
| 6 |
|
| 7 |
class HeaderHelper extends AcymObject |
| 8 |
{ |
| 9 |
public function display(array $breadcrumb): string |
| 10 |
{ |
| 11 |
$header = '<div class="cell large-6 xlarge-7 xxlarge-8 grid-x acym_vcenter">'; |
| 12 |
$header .= $this->getBreadcrumb($breadcrumb); |
| 13 |
$header .= '</div>'; |
| 14 |
|
| 15 |
$header .= '<div class="cell large-6 xlarge-5 xxlarge-4 grid-x">'; |
| 16 |
|
| 17 |
$header .= '<div id="checkVersionArea" class="cell auto grid-x align-right acym_vcenter">'; |
| 18 |
$header .= $this->checkVersionArea(); |
| 19 |
$header .= '</div>'; |
| 20 |
|
| 21 |
$header .= '<div class="cell shrink grid-x align-right acym_vcenter">'; |
| 22 |
$header .= $this->getCheckVersionButton(); |
| 23 |
$header .= $this->getHelpWedButton(); |
| 24 |
$header .= $this->getDocumentationButton(); |
| 25 |
$header .= $this->getNotificationCenter(); |
| 26 |
$header .= '</div>'; |
| 27 |
|
| 28 |
$header .= '</div>'; |
| 29 |
|
| 30 |
$header = '<div id="acym_header" class="grid-x margin-bottom-1">'.$header.'</div>'; |
| 31 |
|
| 32 |
return $this->getLastNews().$header; |
| 33 |
} |
| 34 |
|
| 35 |
private function getLastNews(): string |
| 36 |
{ |
| 37 |
$lastNewsCheck = $this->config->get('last_news_check', 0); |
| 38 |
if ($lastNewsCheck < time() - 7200) { |
| 39 |
$context = stream_context_create(['http' => ['timeout' => 1]]); |
| 40 |
$news = @file_get_contents(ACYM_ACYMAILING_WEBSITE.'acymnews.xml', false, $context); |
| 41 |
$this->config->saveConfig( |
| 42 |
[ |
| 43 |
'last_news_check' => time(), |
| 44 |
'last_news' => base64_encode($news), |
| 45 |
], |
| 46 |
false |
| 47 |
); |
| 48 |
} else { |
| 49 |
$news = $this->config->get('last_news', ''); |
| 50 |
if (!empty($news)) $news = base64_decode($news); |
| 51 |
} |
| 52 |
if (empty($news)) { |
| 53 |
return ''; |
| 54 |
} |
| 55 |
|
| 56 |
$news = @simplexml_load_string($news); |
| 57 |
if (empty($news->news)) { |
| 58 |
return ''; |
| 59 |
} |
| 60 |
|
| 61 |
$currentLanguage = acym_getLanguageTag(); |
| 62 |
$latestNews = null; |
| 63 |
$doNotRemind = json_decode($this->config->get('remindme', '[]')); |
| 64 |
|
| 65 |
foreach ($news->news as $oneNews) { |
| 66 |
if (!empty($latestNews) && strtotime($latestNews->date) > strtotime($oneNews->date)) break; |
| 67 |
|
| 68 |
$language = strtolower($oneNews->language); |
| 69 |
if (empty($oneNews->published) || ($language != strtolower($currentLanguage) && ($language != 'default' || !empty($latestNews)))) { |
| 70 |
continue; |
| 71 |
} |
| 72 |
|
| 73 |
if (!empty($oneNews->extension) && strtolower($oneNews->extension) !== 'acymailing') continue; |
| 74 |
|
| 75 |
if (!empty($oneNews->cms) && strtolower($oneNews->cms) != strtolower('WordPress')) continue; |
| 76 |
|
| 77 |
if (!empty($oneNews->level) && strtolower($oneNews->level) != strtolower($this->config->get('level'))) continue; |
| 78 |
|
| 79 |
if (!empty($oneNews->version)) { |
| 80 |
[$version, $operator] = explode('_', $oneNews->version); |
| 81 |
if (!version_compare($this->config->get('version'), $version, $operator)) continue; |
| 82 |
} |
| 83 |
|
| 84 |
if (!empty($oneNews->mailermethod)) { |
| 85 |
$sendMethod = $this->config->get('mailer_method', ''); |
| 86 |
if ($oneNews->mailermethod !== $sendMethod) continue; |
| 87 |
} |
| 88 |
|
| 89 |
if (in_array($oneNews->name, $doNotRemind)) continue; |
| 90 |
|
| 91 |
$latestNews = $oneNews; |
| 92 |
} |
| 93 |
|
| 94 |
if (empty($latestNews)) { |
| 95 |
return ''; |
| 96 |
} |
| 97 |
|
| 98 |
$newsDisplay = '<div id="acym__header__banner__news" data-news="'.acym_escape($latestNews->name).'">'; |
| 99 |
$newsDisplay .= $latestNews->content; |
| 100 |
$newsDisplay .= '</div>'; |
| 101 |
|
| 102 |
return $newsDisplay; |
| 103 |
} |
| 104 |
|
| 105 |
private function getBreadcrumb(array $breadcrumb): string |
| 106 |
{ |
| 107 |
$links = []; |
| 108 |
foreach ($breadcrumb as $oneLevel => $link) { |
| 109 |
if (!empty($link)) { |
| 110 |
$oneLevel = '<a href="'.$link.'">'.$oneLevel.'</a>'; |
| 111 |
} |
| 112 |
$links[] = '<li>'.$oneLevel.'</li>'; |
| 113 |
} |
| 114 |
|
| 115 |
if (count($links) > 1) { |
| 116 |
$links[count($links) - 1] = str_replace('<li>', '<li class="last_link cell shrink"><span class="show-for-sr">Current: </span>', $links[count($links) - 1]); |
| 117 |
} |
| 118 |
|
| 119 |
$header = '<i class="cell shrink acym-logo"></i>'; |
| 120 |
$header .= '<div id="acym_global_navigation" class="cell auto"> |
| 121 |
<nav aria-label="You are here:" role="navigation"> |
| 122 |
<ul class="breadcrumbs grid-x">'.implode('<li class="breadcrumbs__separator"><i class="acymicon-keyboard-arrow-right"></i></li>', $links).'</ul> |
| 123 |
</nav> |
| 124 |
</div>'; |
| 125 |
|
| 126 |
return $header; |
| 127 |
} |
| 128 |
|
| 129 |
public function checkVersionArea(bool $reloading = false): string |
| 130 |
{ |
| 131 |
$currentLevel = $this->config->get('level', ''); |
| 132 |
$currentVersion = $this->config->get('version', ''); |
| 133 |
$latestVersion = $this->config->get('latestversion', ''); |
| 134 |
|
| 135 |
$version = '<div id="acym_level_version_area" class="text-right">'; |
| 136 |
$version .= '<div id="acym_level">'.ACYM_NAME.' '.$currentLevel.' '; |
| 137 |
|
| 138 |
if (version_compare($currentVersion, $latestVersion, '>=')) { |
| 139 |
$version .= acym_tooltip( |
| 140 |
[ |
| 141 |
'hoveredText' => '<span class="acym__color__green">'.$currentVersion.'</span>', |
| 142 |
'textShownInTooltip' => acym_translation('ACYM_UP_TO_DATE'), |
| 143 |
] |
| 144 |
); |
| 145 |
} elseif (!empty($latestVersion)) { |
| 146 |
if ('wordpress' === ACYM_CMS) { |
| 147 |
$downloadLink = admin_url().'update-core.php'; |
| 148 |
} else { |
| 149 |
$downloadLink = ACYM_ACYMAILING_WEBSITE.'account/license/" target="_blank'; |
| 150 |
} |
| 151 |
$version .= acym_tooltip( |
| 152 |
[ |
| 153 |
'hoveredText' => '<span class="acy_updateversion acym__color__red">'.$currentVersion.'</span>', |
| 154 |
'textShownInTooltip' => acym_translationSprintf('ACYM_CLICK_UPDATE', $latestVersion), |
| 155 |
'titleShownInTooltip' => acym_translation('ACYM_OLD_VERSION'), |
| 156 |
'link' => $downloadLink, |
| 157 |
] |
| 158 |
); |
| 159 |
} |
| 160 |
|
| 161 |
$version .= '</div></div>'; |
| 162 |
|
| 163 |
$expirationDate = $this->config->get('expirationdate', 0); |
| 164 |
if ((empty($expirationDate) || $expirationDate == -1) && empty($this->config->get('acymailer_apikey', ''))) { |
| 165 |
return $version; |
| 166 |
} |
| 167 |
|
| 168 |
$version .= '<div id="acym_expiration" class="cell">'; |
| 169 |
if (acym_level(ACYM_ESSENTIAL) && ACYM_PRODUCTION) { |
| 170 |
if ($expirationDate == -2) { |
| 171 |
$version .= '<div class="acylicence_expired"> |
| 172 |
<a class="acy_attachlicence acymbuttons acym__color__red acym_link_license_tab" |
| 173 |
href="'.acym_completeLink('configuration', false, false, true).'">'.acym_translation('ACYM_ATTACH_LICENCE').'</a> |
| 174 |
</div>'; |
| 175 |
} elseif ($expirationDate < time()) { |
| 176 |
$version .= acym_tooltip( |
| 177 |
[ |
| 178 |
'hoveredText' => '<span class="acy_subscriptionexpired acym__color__red">'.acym_translation('ACYM_SUBSCRIPTION_EXPIRED').'</span>', |
| 179 |
'textShownInTooltip' => acym_translation('ACYM_SUBSCRIPTION_EXPIRED_LINK'), |
| 180 |
'link' => ACYM_ACYMAILING_WEBSITE.'account/license/', |
| 181 |
] |
| 182 |
); |
| 183 |
} else { |
| 184 |
$version .= '<div class="acylicence_valid"> |
| 185 |
<span class="acy_subscriptionok acym__color__green">'.acym_translationSprintf( |
| 186 |
'ACYM_VALID_UNTIL', |
| 187 |
acym_getDate($expirationDate, acym_translation('ACYM_DATE_FORMAT_LC4')) |
| 188 |
).'</span> |
| 189 |
</div>'; |
| 190 |
} |
| 191 |
} |
| 192 |
|
| 193 |
$creditRemainingSendingMethod = ''; |
| 194 |
acym_trigger('onAcymGetCreditRemainingSendingMethod', [&$creditRemainingSendingMethod, $reloading]); |
| 195 |
if (!empty($creditRemainingSendingMethod)) { |
| 196 |
$version .= '<div class="acy_sending_method_credits"> |
| 197 |
<span>'.$creditRemainingSendingMethod.'</span> |
| 198 |
</div>'; |
| 199 |
} |
| 200 |
|
| 201 |
$version .= '</div>'; |
| 202 |
|
| 203 |
return $version; |
| 204 |
} |
| 205 |
|
| 206 |
private function getCheckVersionButton(): string |
| 207 |
{ |
| 208 |
if (ACYM_CMS == 'wordpress' && !acym_level(ACYM_ESSENTIAL)) { |
| 209 |
return ''; |
| 210 |
} |
| 211 |
|
| 212 |
$lastLicenseCheck = $this->config->get('lastlicensecheck', 0); |
| 213 |
if (empty($lastLicenseCheck)) { |
| 214 |
$lastLicenseCheck = time(); |
| 215 |
} |
| 216 |
|
| 217 |
$lastDateFormatted = acym_date($lastLicenseCheck, 'ACYM_DATE_FORMAT_LC2'); |
| 218 |
|
| 219 |
return acym_tooltip( |
| 220 |
[ |
| 221 |
'hoveredText' => '<a id="checkVersionButton" type="button" class="grid-x align-center button_header medium-shrink acym_vcenter"><i class="cell shrink acymicon-autorenew"></i></a>', |
| 222 |
'textShownInTooltip' => acym_translation('ACYM_LAST_CHECK').' <span id="acym__check__version__last__check">'.$lastDateFormatted.'</span>', |
| 223 |
] |
| 224 |
); |
| 225 |
} |
| 226 |
|
| 227 |
private function getDocumentationButton(): string |
| 228 |
{ |
| 229 |
return acym_tooltip( |
| 230 |
[ |
| 231 |
'hoveredText' => '<a type="button" class="grid-x align-center button_header medium-shrink acym_vcenter" target="_blank" href="'.ACYM_DOCUMENTATION.'"><i class="cell shrink acymicon-book"></i></a>', |
| 232 |
'textShownInTooltip' => acym_translation('ACYM_DOCUMENTATION'), |
| 233 |
] |
| 234 |
); |
| 235 |
} |
| 236 |
|
| 237 |
private function getHelpWedButton(): string |
| 238 |
{ |
| 239 |
if (ACYM_CMS !== 'wordpress' || acym_level(ACYM_ESSENTIAL)) { |
| 240 |
return ''; |
| 241 |
} |
| 242 |
|
| 243 |
return '<a type="button" class="grid-x align-center button_header medium-shrink acym_vcenter" target="_blank" href="https://wordpress.org/support/plugin/acymailing/"> |
| 244 |
<i class="cell shrink acymicon-wordpress"></i> |
| 245 |
</a>'; |
| 246 |
} |
| 247 |
|
| 248 |
public function getNotificationCenter(): string |
| 249 |
{ |
| 250 |
$notifications = json_decode($this->config->get('notifications', '{}'), true); |
| 251 |
if (!is_array($notifications) || empty($notifications)) { |
| 252 |
$notifications = []; |
| 253 |
} |
| 254 |
$message = ''; |
| 255 |
$notificationLevel = 0; |
| 256 |
if (!empty($_SESSION['acym_success'])) { |
| 257 |
$message = $_SESSION['acym_success']; |
| 258 |
$_SESSION['acym_success'] = ''; |
| 259 |
$notificationLevel = 1; |
| 260 |
} |
| 261 |
|
| 262 |
if (!empty($notifications)) { |
| 263 |
foreach ($notifications as $notification) { |
| 264 |
if ($notification['read']) continue; |
| 265 |
if ($notification['level'] == 'info' && $notificationLevel < 2) $notificationLevel = 2; |
| 266 |
if ($notification['level'] == 'warning' && $notificationLevel < 3) $notificationLevel = 3; |
| 267 |
if ($notification['level'] == 'error' && $notificationLevel < 4) $notificationLevel = 4; |
| 268 |
} |
| 269 |
} |
| 270 |
|
| 271 |
$iconToDisplay = ''; |
| 272 |
$tooltip = ''; |
| 273 |
|
| 274 |
switch ($notificationLevel) { |
| 275 |
case 0: |
| 276 |
$iconToDisplay = 'acymicon-bell'; |
| 277 |
$notificationLevel = ''; |
| 278 |
break; |
| 279 |
case 1: |
| 280 |
$iconToDisplay = 'acymicon-check-circle acym__color__green'; |
| 281 |
$notificationLevel = 'acym__header__notification__button__success acym__header__notification__pulse'; |
| 282 |
$tooltip = 'data-acym-tooltip="'.acym_escape($message).'" data-acym-tooltip-position="left"'; |
| 283 |
break; |
| 284 |
case 2: |
| 285 |
$iconToDisplay = 'acymicon-bell acym__color__blue'; |
| 286 |
$notificationLevel = 'acym__header__notification__button__info'; |
| 287 |
break; |
| 288 |
case 3: |
| 289 |
$iconToDisplay = 'acymicon-exclamation-triangle acym__color__orange'; |
| 290 |
$notificationLevel = 'acym__header__notification__button__warning'; |
| 291 |
break; |
| 292 |
case 4: |
| 293 |
$iconToDisplay = 'acymicon-exclamation-circle acym__color__red'; |
| 294 |
$notificationLevel = 'acym__header__notification__button__error'; |
| 295 |
break; |
| 296 |
} |
| 297 |
|
| 298 |
$notificationCenter = '<div class="cell grid-x align-center acym_vcenter medium-shrink acym__header__notification '.$notificationLevel.' button_header cursor-pointer" '.$tooltip.'>'; |
| 299 |
$notificationCenter .= '<i class="'.$iconToDisplay.'"></i>'; |
| 300 |
$notificationCenter .= '<div class="cell grid-x acym__header__notification__center align-center">'; |
| 301 |
$notificationCenter .= $this->getNotificationCenterInner($notifications); |
| 302 |
$notificationCenter .= '</div>'; |
| 303 |
$notificationCenter .= '</div>'; |
| 304 |
|
| 305 |
return $notificationCenter; |
| 306 |
} |
| 307 |
|
| 308 |
public function getNotificationCenterInner(array $notifications): string |
| 309 |
{ |
| 310 |
$notificationCenter = ''; |
| 311 |
if (empty($notifications)) { |
| 312 |
$notificationCenter .= '<div class="cell grid-x acym__header__notification__one acym__header__notification__one__empty acym_vcenter">'; |
| 313 |
$notificationCenter .= '<h2 class="cell text-center">'.acym_translation('ACYM_YOU_DONT_HAVE_NOTIFICATIONS').'</h2>'; |
| 314 |
$notificationCenter .= '</div>'; |
| 315 |
} else { |
| 316 |
$notificationCenter .= '<div class="cell grid-x acym__header__notification__toolbox"><p class="cell auto">'; |
| 317 |
$notificationCenter .= acym_translation('ACYM_NOTIFICATIONS'); |
| 318 |
$notificationCenter .= '</p><div class="cell shrink cursor-pointer acym__header__notification__toolbox__remove text-right" data-id="all">'; |
| 319 |
$notificationCenter .= acym_translation('ACYM_DELETE_ALL'); |
| 320 |
$notificationCenter .= '</div></div>'; |
| 321 |
foreach ($notifications as $key => $notif) { |
| 322 |
$fullMessageHover = $notif['message']; |
| 323 |
|
| 324 |
if (strlen($notif['message']) > 150) { |
| 325 |
$tag = new \stdClass(); |
| 326 |
$tag->wrap = 150; |
| 327 |
|
| 328 |
$pluginHelper = new PluginHelper(); |
| 329 |
$notif['message'] = $pluginHelper->wrapText($notif['message'], $tag); |
| 330 |
} |
| 331 |
$fullMessageHover = $fullMessageHover != $notif['message'] ? 'data-acym-full="'.acym_escape($fullMessageHover).'"' : ''; |
| 332 |
|
| 333 |
$logo = $notif['level'] === 'info' ? 'acymicon-bell' : ($notif['level'] == 'warning' ? 'acymicon-exclamation-triangle' : 'acymicon-exclamation-circle'); |
| 334 |
$read = $notif['read'] ? 'acym__header__notification__one__read' : ''; |
| 335 |
$notificationCenter .= '<div class="'.$read.' cell grid-x acym__header__notification__one acym_vcenter acym_vcenter acym__header__notification__one__'.$notif['level'].'">'; |
| 336 |
$notificationCenter .= '<div class="cell small-3 align-center grid-x acym__header__notification__one__icon"><i class="cell '.$logo.'"></i></div>'; |
| 337 |
$notificationCenter .= '<div class="cell grid-x small-8"><p class="cell acym__header__notification__message" '.$fullMessageHover.'>'.$notif['message']; |
| 338 |
$notificationCenter .= '<div class="cell acym__header__notification__one__date">'.acym_date($notif['date']).'</div></div>'; |
| 339 |
$notificationCenter .= '<i class="cell small-1 acym__header__notification__one__delete acymicon-close" data-id="'.acym_escape($key).'"></i>'; |
| 340 |
$notificationCenter .= '</div>'; |
| 341 |
} |
| 342 |
} |
| 343 |
|
| 344 |
return $notificationCenter; |
| 345 |
} |
| 346 |
|
| 347 |
public function addNotification(object $notif): string |
| 348 |
{ |
| 349 |
if ($notif->level === 'success') { |
| 350 |
$_SESSION['acym_success'] = $notif->message; |
| 351 |
|
| 352 |
return ''; |
| 353 |
} |
| 354 |
|
| 355 |
$notifications = json_decode($this->config->get('notifications', '[]'), true); |
| 356 |
if (!is_array($notifications)) { |
| 357 |
$notifications = []; |
| 358 |
} |
| 359 |
|
| 360 |
$notif->message = str_replace('<br />', "\r\n", $notif->message); |
| 361 |
$notif->message = strip_tags($notif->message, '<a>'); |
| 362 |
|
| 363 |
foreach ($notifications as $key => $oneNotif) { |
| 364 |
if ($oneNotif['message'] === $notif->message && $oneNotif['level'] === $notif->level) unset($notifications[$key]); |
| 365 |
} |
| 366 |
$notifications = array_values($notifications); |
| 367 |
|
| 368 |
$notif->id = uniqid(); |
| 369 |
array_unshift($notifications, $notif); |
| 370 |
|
| 371 |
if (count($notifications) > 10) unset($notifications[10]); |
| 372 |
|
| 373 |
$this->config->saveConfig(['notifications' => json_encode($notifications)]); |
| 374 |
|
| 375 |
return $notif->id; |
| 376 |
} |
| 377 |
} |
| 378 |
|