- Correct getChoices() docblock to reflect translated label => enum backing value order instead of value => label - Re-indent Smart Links menu config to match actual array nesting - Align assignment operators in RefreshStatsCommand - Add missing newline at end of RefreshStatsCommand.php
96 lines
3.6 KiB
PHP
96 lines
3.6 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace MauticPlugin\MT3SMarketingProgramBundle\Controller;
|
|
|
|
use Doctrine\Persistence\ManagerRegistry;
|
|
use Mautic\CoreBundle\Controller\CommonController;
|
|
use Mautic\CoreBundle\Factory\ModelFactory;
|
|
use Mautic\CoreBundle\Helper\CoreParametersHelper;
|
|
use Mautic\CoreBundle\Helper\UserHelper;
|
|
use Mautic\CoreBundle\Security\Permissions\CorePermissions;
|
|
use Mautic\CoreBundle\Service\FlashBag;
|
|
use Mautic\CoreBundle\Translation\Translator;
|
|
use MauticPlugin\MT3SMarketingProgramBundle\Model\SmartLinkModel;
|
|
use Psr\Log\LoggerInterface;
|
|
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
|
|
use Symfony\Component\HttpFoundation\RedirectResponse;
|
|
use Symfony\Component\HttpFoundation\Request;
|
|
use Symfony\Component\HttpFoundation\RequestStack;
|
|
use Symfony\Component\HttpFoundation\Response;
|
|
|
|
/**
|
|
* The public side of the Smart Links: https://{short_link_domain}/l/{slug}.
|
|
*
|
|
* The short link only works when Mautic is entered through the configured short link
|
|
* domain (Settings > Configuration > Smart Links Settings): the call is redirected to
|
|
* the link's destination — its external URL, or its landing page / standalone form for
|
|
* Mautic redirects — with its UTM parameters, and one hit is recorded for the statistics.
|
|
* On any other host, unknown slug, unpublished link or link without destination the
|
|
* short link doesn't exist: 404.
|
|
*/
|
|
class PublicController extends CommonController
|
|
{
|
|
public function __construct(
|
|
private readonly SmartLinkModel $smartLinkModel,
|
|
private readonly LoggerInterface $logger,
|
|
ManagerRegistry $doctrine,
|
|
ModelFactory $modelFactory,
|
|
UserHelper $userHelper,
|
|
CoreParametersHelper $coreParametersHelper,
|
|
EventDispatcherInterface $dispatcher,
|
|
Translator $translator,
|
|
FlashBag $flashBag,
|
|
RequestStack $requestStack,
|
|
CorePermissions $security,
|
|
) {
|
|
parent::__construct($doctrine, $modelFactory, $userHelper, $coreParametersHelper, $dispatcher, $translator, $flashBag, $requestStack, $security);
|
|
}
|
|
|
|
public function goAction(Request $request, string $slug): Response
|
|
{
|
|
// The "if and only if" rule: only the short link domain is an entry point for /l/.
|
|
$domain = $this->smartLinkModel->getShortLinkDomain();
|
|
if ('' === $domain || 0 !== strcasecmp(trim($request->getHost()), $domain)) {
|
|
return $this->notFound();
|
|
}
|
|
|
|
$slug = trim($slug);
|
|
if ('' === $slug) {
|
|
return $this->notFound();
|
|
}
|
|
|
|
$link = $this->smartLinkModel->getRepository()->findOneBy(['slug' => $slug]);
|
|
|
|
if (null === $link || !$link->isPublished()) {
|
|
return $this->notFound();
|
|
}
|
|
|
|
// A link without destination goes nowhere, whatever its type.
|
|
if ($link->getRedirectType()->isMautic()) {
|
|
if (null === $link->getPage() && null === $link->getForm()) {
|
|
return $this->notFound();
|
|
}
|
|
} elseif ('' === trim((string) $link->getDestinationUrl())) {
|
|
return $this->notFound();
|
|
}
|
|
|
|
try {
|
|
$this->smartLinkModel->recordHit($link, $request);
|
|
} catch (\Throwable $e) {
|
|
// Statistics must never block the redirect.
|
|
$this->logger->error(
|
|
'Smart Link hit could not be recorded for slug "{slug}": {message}',
|
|
[
|
|
'slug' => $slug,
|
|
'message' => $e->getMessage(),
|
|
'exception' => $e,
|
|
]
|
|
);
|
|
}
|
|
|
|
return new RedirectResponse($this->smartLinkModel->buildRedirectUrl($link), $link->getRedirectCode()->value);
|
|
}
|
|
}
|