Spaces:
No application file
No application file
File size: 27,306 Bytes
d2897cd |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 |
<?php
namespace Mautic\PageBundle\Controller;
use Mautic\CoreBundle\Controller\AbstractFormController;
use Mautic\CoreBundle\Exception\InvalidDecodedStringException;
use Mautic\CoreBundle\Helper\CookieHelper;
use Mautic\CoreBundle\Helper\IpLookupHelper;
use Mautic\CoreBundle\Helper\TrackingPixelHelper;
use Mautic\CoreBundle\Helper\UrlHelper;
use Mautic\CoreBundle\Security\Permissions\CorePermissions;
use Mautic\CoreBundle\Twig\Helper\AnalyticsHelper;
use Mautic\CoreBundle\Twig\Helper\AssetsHelper;
use Mautic\LeadBundle\Entity\Lead;
use Mautic\LeadBundle\Helper\ContactRequestHelper;
use Mautic\LeadBundle\Helper\PrimaryCompanyHelper;
use Mautic\LeadBundle\Helper\TokenHelper;
use Mautic\LeadBundle\Model\LeadModel;
use Mautic\LeadBundle\Tracker\ContactTracker;
use Mautic\LeadBundle\Tracker\Service\DeviceTrackingService\DeviceTrackingServiceInterface;
use Mautic\PageBundle\Entity\Page;
use Mautic\PageBundle\Event\PageDisplayEvent;
use Mautic\PageBundle\Event\TrackingEvent;
use Mautic\PageBundle\Helper\TrackingHelper;
use Mautic\PageBundle\Model\PageModel;
use Mautic\PageBundle\Model\Tracking404Model;
use Mautic\PageBundle\Model\VideoModel;
use Mautic\PageBundle\PageEvents;
use Psr\Log\LoggerInterface;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Symfony\Component\Routing\RouterInterface;
class PublicController extends AbstractFormController
{
/**
* @return Response
*
* @throws \Exception
* @throws \Mautic\CoreBundle\Exception\FileNotFoundException
*/
public function indexAction(
Request $request,
ContactRequestHelper $contactRequestHelper,
CookieHelper $cookieHelper,
AnalyticsHelper $analyticsHelper,
AssetsHelper $assetsHelper,
Tracking404Model $tracking404Model,
RouterInterface $router,
$slug)
{
/** @var PageModel $model */
$model = $this->getModel('page');
$security = $this->security;
/** @var Page|bool $entity */
$entity = $model->getEntityBySlugs($slug);
// Do not hit preference center pages
if (!empty($entity) && !$entity->getIsPreferenceCenter()) {
$userAccess = $security->hasEntityAccess('page:pages:viewown', 'page:pages:viewother', $entity->getCreatedBy());
$published = $entity->isPublished();
// Make sure the page is published or deny access if not
if (!$published && !$userAccess) {
// If the page has a redirect type, handle it
if (null != $entity->getRedirectType()) {
$model->hitPage($entity, $request, $entity->getRedirectType());
return $this->redirect($entity->getRedirectUrl(), (int) $entity->getRedirectType());
} else {
$model->hitPage($entity, $request, 401);
return $this->accessDenied();
}
}
$lead = null;
$query = null;
if (!$userAccess) {
// Extract the lead from the request so it can be used to determine language if applicable
$query = $model->getHitQuery($request, $entity);
$lead = $contactRequestHelper->getContactFromQuery($query);
}
// Correct the URL if it doesn't match up
if (!$request->attributes->get('ignore_mismatch', 0)) {
// Make sure URLs match up
$url = $model->generateUrl($entity, false);
$requestUri = $request->getRequestUri();
// Remove query when comparing
$query = $request->getQueryString();
if (!empty($query)) {
$requestUri = str_replace("?{$query}", '', $url);
}
// Redirect if they don't match
if ($requestUri != $url) {
$model->hitPage($entity, $request, 301, $lead, $query);
return $this->redirect($url, 301);
}
}
// Check for variants
[$parentVariant, $childrenVariants] = $entity->getVariants();
// Is this a variant of another? If so, the parent URL should be used unless a user is logged in and previewing
if ($parentVariant != $entity && !$userAccess) {
$model->hitPage($entity, $request, 301, $lead, $query);
$url = $model->generateUrl($parentVariant, false);
return $this->redirect($url, 301);
}
// First determine the A/B test to display if applicable
if (!$userAccess) {
// Check to see if a variant should be shown versus the parent but ignore if a user is previewing
if (count($childrenVariants)) {
$variants = [];
$variantWeight = 0;
$totalHits = $entity->getVariantHits();
foreach ($childrenVariants as $id => $child) {
if ($child->isPublished()) {
$variantSettings = $child->getVariantSettings();
$variants[$id] = [
'weight' => ($variantSettings['weight'] / 100),
'hits' => $child->getVariantHits(),
];
$variantWeight += $variantSettings['weight'];
// Count translations for this variant as well
$translations = $child->getTranslations(true);
/** @var Page $translation */
foreach ($translations as $translation) {
if ($translation->isPublished()) {
$variants[$id]['hits'] += (int) $translation->getVariantHits();
}
}
$totalHits += $variants[$id]['hits'];
}
}
if (count($variants)) {
// check to see if this user has already been displayed a specific variant
$variantCookie = $request->cookies->get('mautic_page_'.$entity->getId());
if (!empty($variantCookie)) {
if (isset($variants[$variantCookie])) {
// if not the parent, show the specific variant already displayed to the visitor
if ($variantCookie !== $entity->getId()) {
$entity = $childrenVariants[$variantCookie];
} // otherwise proceed with displaying parent
}
} else {
// Add parent weight
$variants[$entity->getId()] = [
'weight' => ((100 - $variantWeight) / 100),
'hits' => $entity->getVariantHits(),
];
// Count translations for the parent as well
$translations = $entity->getTranslations(true);
/** @var Page $translation */
foreach ($translations as $translation) {
if ($translation->isPublished()) {
$variants[$entity->getId()]['hits'] += (int) $translation->getVariantHits();
}
}
$totalHits += $variants[$id]['hits'];
// determine variant to show
foreach ($variants as &$variant) {
$variant['weight_deficit'] = ($totalHits) ? $variant['weight'] - ($variant['hits'] / $totalHits) : $variant['weight'];
}
// Reorder according to send_weight so that campaigns which currently send one at a time alternate
uasort(
$variants,
function ($a, $b): int {
if ($a['weight_deficit'] === $b['weight_deficit']) {
if ($a['hits'] === $b['hits']) {
return 0;
}
// if weight is the same - sort by least number displayed
return ($a['hits'] < $b['hits']) ? -1 : 1;
}
// sort by the one with the greatest deficit first
return ($a['weight_deficit'] > $b['weight_deficit']) ? -1 : 1;
}
);
// find the one with the most difference from weight
$useId = array_key_first($variants);
// set the cookie - 14 days
$cookieHelper->setCookie(
'mautic_page_'.$entity->getId(),
$useId,
3600 * 24 * 14
);
if ($useId != $entity->getId()) {
$entity = $childrenVariants[$useId];
}
}
}
}
// Now show the translation for the page or a/b test - only fetch a translation if a slug was not used
if ($entity->isTranslation() && empty($entity->languageSlug)) {
[$translationParent, $translatedEntity] = $model->getTranslatedEntity(
$entity,
$lead,
$request
);
if ($translationParent && $translatedEntity !== $entity) {
if (!$request->get('ntrd', 0)) {
$url = $model->generateUrl($translatedEntity, false);
$model->hitPage($entity, $request, 302, $lead, $query);
return $this->redirect($url, 302);
}
}
}
}
// Generate contents
$analytics = $analyticsHelper->getCode();
$BCcontent = $entity->getContent();
$content = $entity->getCustomHtml();
// This condition remains so the Mautic v1 themes would display the content
if (empty($content) && !empty($BCcontent)) {
/**
* @deprecated BC support to be removed in 3.0
*/
$template = $entity->getTemplate();
// all the checks pass so display the content
$slots = $this->factory->getTheme($template)->getSlots('page');
$content = $entity->getContent();
$this->processSlots($slots, $entity);
// Add the GA code to the template assets
if (!empty($analytics)) {
$this->factory->getHelper('template.assets')->addCustomDeclaration($analytics);
}
$logicalName = $this->factory->getHelper('theme')->checkForTwigTemplate('@themes/'.$template.'/html/page.html.twig');
$response = $this->render(
$logicalName,
[
'slots' => $slots,
'content' => $content,
'page' => $entity,
'template' => $template,
'public' => true,
]
);
$content = $response->getContent();
} else {
if (!empty($analytics)) {
$content = str_replace('</head>', $analytics."\n</head>", $content);
}
if ($entity->getNoIndex()) {
$content = str_replace('</head>', "<meta name=\"robots\" content=\"noindex\">\n</head>", $content);
}
}
$assetsHelper->addScript($router->generate('mautic_js', [], UrlGeneratorInterface::ABSOLUTE_URL),
'onPageDisplay_headClose',
true,
'mautic_js'
);
$event = new PageDisplayEvent($content, $entity);
$this->dispatcher->dispatch($event, PageEvents::PAGE_ON_DISPLAY);
$content = $event->getContent();
$model->hitPage($entity, $request, 200, $lead, $query);
return new Response($content);
}
if (false !== $entity && $tracking404Model->isTrackable()) {
$tracking404Model->hitPage($entity, $request);
}
return $this->notFound();
}
/**
* @return Response|\Symfony\Component\HttpKernel\Exception\NotFoundHttpException
*
* @throws \Exception
* @throws \Mautic\CoreBundle\Exception\FileNotFoundException
*/
public function previewAction(Request $request, CorePermissions $security, int $id)
{
$contactId = (int) $request->query->get('contactId');
if ($contactId) {
/** @var LeadModel $leadModel */
$leadModel = $this->getModel('lead.lead');
/** @var Lead $contact */
$contact = $leadModel->getEntity($contactId);
}
/** @var PageModel $model */
$model = $this->getModel('page');
/** @var Page $page */
$page = $model->getEntity($id);
if (!$page->getId()) {
return $this->notFound();
}
$analytics = $this->factory->getHelper('twig.analytics')->getCode();
$BCcontent = $page->getContent();
$content = $page->getCustomHtml();
if (!$security->isAdmin()
&& (
(!$page->isPublished())
|| (!$security->hasEntityAccess(
'email:emails:viewown',
'email:emails:viewother',
$page->getCreatedBy()
)))
) {
return $this->accessDenied();
}
if ($contactId && (
!$security->isAdmin()
|| !$security->hasEntityAccess('lead:leads:viewown', 'lead:leads:viewother')
)
) {
return $this->accessDenied();
}
if (empty($content) && !empty($BCcontent)) {
$template = $page->getTemplate();
// all the checks pass so display the content
$slots = $this->factory->getTheme($template)->getSlots('page');
$content = $page->getContent();
$this->processSlots($slots, $page);
// Add the GA code to the template assets
if (!empty($analytics)) {
$this->factory->getHelper('template.assets')->addCustomDeclaration($analytics);
}
$logicalName = $this->factory->getHelper('theme')->checkForTwigTemplate('@themes/'.$template.'/html/page.html.twig');
$response = $this->render(
$logicalName,
[
'slots' => $slots,
'content' => $content,
'page' => $page,
'template' => $template,
'public' => true, // @deprecated Remove in 2.0
]
);
$content = $response->getContent();
} else {
$content = str_replace('</head>', $analytics.$this->renderView('@MauticPage/Page/preview_header.html.twig')."\n</head>", $content);
}
if ($this->dispatcher->hasListeners(PageEvents::PAGE_ON_DISPLAY)) {
$event = new PageDisplayEvent($content, $page, $this->getPreferenceCenterConfig());
if (isset($contact) && $contact instanceof Lead) {
$event->setLead($contact);
}
$this->dispatcher->dispatch($event, PageEvents::PAGE_ON_DISPLAY);
$content = $event->getContent();
}
return new Response($content);
}
/**
* @return Response
*
* @throws \Exception
*/
public function trackingImageAction(Request $request)
{
/** @var PageModel $model */
$model = $this->getModel('page');
$model->hitPage(null, $request);
return TrackingPixelHelper::getResponse($request);
}
/**
* @return JsonResponse
*
* @throws \Exception
*/
public function trackingAction(
Request $request,
DeviceTrackingServiceInterface $deviceTrackingService,
TrackingHelper $trackingHelper,
ContactTracker $contactTracker
) {
$notSuccessResponse = new JsonResponse(
[
'success' => 0,
]
);
if (!$this->security->isAnonymous()) {
return $notSuccessResponse;
}
/** @var PageModel $model */
$model = $this->getModel('page');
try {
$model->hitPage(null, $request);
} catch (InvalidDecodedStringException) {
// do not track invalid ct
return $notSuccessResponse;
}
$lead = $contactTracker->getContact();
$trackedDevice = $deviceTrackingService->getTrackedDevice();
$trackingId = (null === $trackedDevice ? null : $trackedDevice->getTrackingId());
$sessionValue = $trackingHelper->getCacheItem(true);
$event = new TrackingEvent($lead, $request, $sessionValue);
$this->dispatcher->dispatch($event, PageEvents::ON_CONTACT_TRACKED);
return new JsonResponse(
[
'success' => 1,
'id' => ($lead) ? $lead->getId() : null,
'sid' => $trackingId,
'device_id' => $trackingId,
'events' => $event->getResponse()->all(),
]
);
}
/**
* @throws \Exception
*/
public function redirectAction(
Request $request,
ContactRequestHelper $contactRequestHelper,
PrimaryCompanyHelper $primaryCompanyHelper,
IpLookupHelper $ipLookupHelper,
LoggerInterface $logger,
$redirectId
): \Symfony\Component\HttpFoundation\RedirectResponse {
$logger->debug('Attempting to load redirect with tracking_id of: '.$redirectId);
/** @var \Mautic\PageBundle\Model\RedirectModel $redirectModel */
$redirectModel = $this->getModel('page.redirect');
$redirect = $redirectModel->getRedirectById($redirectId);
$logger->debug('Executing Redirect: '.$redirect);
if (null === $redirect || !$redirect->isPublished(false)) {
$logger->debug('Redirect with tracking_id of '.$redirectId.' not found');
$url = ($redirect) ? $redirect->getUrl() : 'n/a';
throw $this->createNotFoundException($this->translator->trans('mautic.core.url.error.404', ['%url%' => $url]));
}
// Ensure the URL does not have encoded ampersands
$url = UrlHelper::decodeAmpersands($redirect->getUrl());
// Get query string
$query = $request->query->all();
$ct = $query['ct'] ?? null;
// Tak on anything left to the URL
if (count($query)) {
$url = UrlHelper::appendQueryToUrl($url, http_build_query($query));
}
// If the IP address is not trackable, it means it came form a configured "do not track" IP or a "do not track" user agent
// This prevents simulated clicks from 3rd party services such as URL shorteners from simulating clicks
$ipAddress = $ipLookupHelper->getIpAddress();
if ($ct) {
if ($ipAddress->isTrackable()) {
// Search replace lead fields in the URL
/** @var PageModel $pageModel */
$pageModel = $this->getModel('page');
try {
$lead = $contactRequestHelper->getContactFromQuery(['ct' => $ct]);
$pageModel->hitPage($redirect, $request, 200, $lead);
} catch (InvalidDecodedStringException $e) {
// Invalid ct value so we must unset it
// and process the request without it
$logger->error(sprintf('Invalid clickthrough value: %s', $ct), ['exception' => $e]);
$request->request->set('ct', '');
$request->query->set('ct', '');
$lead = $contactRequestHelper->getContactFromQuery();
$pageModel->hitPage($redirect, $request, 200, $lead);
}
$leadArray = ($lead) ? $primaryCompanyHelper->getProfileFieldsWithPrimaryCompany($lead) : [];
$url = TokenHelper::findLeadTokens($url, $leadArray, true);
}
if (str_contains($url, $this->generateUrl('mautic_asset_download'))) {
if (strpos($url, '&')) {
$url .= '&ct='.$ct;
} else {
$url .= '?ct='.$ct;
}
}
}
$url = UrlHelper::sanitizeAbsoluteUrl($url);
if (!UrlHelper::isValidUrl($url)) {
throw $this->createNotFoundException($this->translator->trans('mautic.core.url.error.404', ['%url%' => $url]));
}
return $this->redirect($url);
}
/**
* PreProcess page slots for public view.
*
* @deprecated - to be removed in 3.0
*
* @param array $slots
* @param Page $entity
*/
private function processSlots($slots, $entity): void
{
/** @var AssetsHelper $assetsHelper */
$assetsHelper = $this->factory->getHelper('template.assets');
/** @var \Mautic\CoreBundle\Twig\Helper\SlotsHelper $slotsHelper */
$slotsHelper = $this->factory->getHelper('template.slots');
$content = $entity->getContent();
foreach ($slots as $slot => $slotConfig) {
// backward compatibility - if slotConfig array does not exist
if (is_numeric($slot)) {
$slot = $slotConfig;
$slotConfig = [];
}
if (isset($slotConfig['type']) && 'slideshow' == $slotConfig['type']) {
if (isset($content[$slot])) {
$options = json_decode($content[$slot], true);
} else {
$options = [
'width' => '100%',
'height' => '250px',
'background_color' => 'transparent',
'arrow_navigation' => false,
'dot_navigation' => true,
'interval' => 5000,
'pause' => 'hover',
'wrap' => true,
'keyboard' => true,
];
}
// Create sample slides for first time or if all slides were deleted
if (empty($options['slides'])) {
$options['slides'] = [
[
'order' => 0,
'background-image' => $assetsHelper->getOverridableUrl('images/mautic_logo_lb200.png'),
'captionheader' => 'Caption 1',
],
[
'order' => 1,
'background-image' => $assetsHelper->getOverridableUrl('images/mautic_logo_db200.png'),
'captionheader' => 'Caption 2',
],
];
}
// Order slides
usort(
$options['slides'],
fn ($a, $b): int => strcmp($a['order'], $b['order'])
);
$options['slot'] = $slot;
$options['public'] = true;
} elseif (isset($slotConfig['type']) && 'textarea' == $slotConfig['type']) {
$value = isset($content[$slot]) ? nl2br($content[$slot]) : '';
$slotsHelper->set($slot, $value);
} else {
// Fallback for other types like html, text, textarea and all unknown
$value = $content[$slot] ?? '';
$slotsHelper->set($slot, $value);
}
}
$parentVariant = $entity->getVariantParent();
$title = (!empty($parentVariant)) ? $parentVariant->getTitle() : $entity->getTitle();
$slotsHelper->set('pageTitle', $title);
}
/**
* Track video views.
*/
public function hitVideoAction(Request $request)
{
// Only track XMLHttpRequests, because the hit should only come from there
if ($request->isXmlHttpRequest()) {
/** @var VideoModel $model */
$model = $this->getModel('page.video');
try {
$model->hitVideo($request);
} catch (\Exception) {
return new JsonResponse(['success' => false]);
}
return new JsonResponse(['success' => true]);
}
return new Response();
}
/**
* Get the ID of the currently tracked Contact.
*/
public function getContactIdAction(DeviceTrackingServiceInterface $trackedDeviceService, ContactTracker $contactTracker): JsonResponse
{
$data = [];
if ($this->security->isAnonymous()) {
$lead = $contactTracker->getContact();
$trackedDevice = $trackedDeviceService->getTrackedDevice();
$trackingId = (null === $trackedDevice ? null : $trackedDevice->getTrackingId());
$data = [
'id' => ($lead) ? $lead->getId() : null,
'sid' => $trackingId,
'device_id' => $trackingId,
];
}
return new JsonResponse($data);
}
/**
* @return array<string,bool>
*/
private function getPreferenceCenterConfig(): array
{
return [
'showContactFrequency' => $this->coreParametersHelper->get('show_contact_frequency'),
'showContactPauseDates' => $this->coreParametersHelper->get('show_contact_pause_dates'),
'showContactPreferredChannels' => $this->coreParametersHelper->get('show_contact_preferred_channels'),
'showContactCategories' => $this->coreParametersHelper->get('show_contact_categories'),
'showContactSegments' => $this->coreParametersHelper->get('show_contact_segments'),
];
}
}
|