Upgrade to TYPO3 9.5 LTS
This commit is contained in:
@@ -0,0 +1,197 @@
|
||||
<?php
|
||||
|
||||
namespace EP\EpTrack\Middleware;
|
||||
|
||||
/***************************************************************
|
||||
*
|
||||
* Copyright notice
|
||||
*
|
||||
* (c) 2019 Björn Fromme <[email protected]>, dreipunktnull
|
||||
*
|
||||
* All rights reserved
|
||||
*
|
||||
* This script is part of the TYPO3 project. The TYPO3 project is
|
||||
* free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation; either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* The GNU General Public License can be found at
|
||||
* http://www.gnu.org/copyleft/gpl.html.
|
||||
*
|
||||
* This script is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* This copyright notice MUST APPEAR in all copies of the script!
|
||||
***************************************************************/
|
||||
|
||||
use DateInterval;
|
||||
use DateTime;
|
||||
use Doctrine\DBAL\DBALException;
|
||||
use EP\EpProducts\Traits\DbConnectionTrait;
|
||||
use Exception;
|
||||
use Psr\Http\Message\ResponseInterface;
|
||||
use Psr\Http\Message\ServerRequestInterface;
|
||||
use Psr\Http\Server\MiddlewareInterface;
|
||||
use Psr\Http\Server\RequestHandlerInterface;
|
||||
use TYPO3\CMS\Core\Error\Http\ServiceUnavailableException;
|
||||
use TYPO3\CMS\Core\Http\ImmediateResponseException;
|
||||
use TYPO3\CMS\Core\Http\NormalizedParams;
|
||||
use TYPO3\CMS\Core\Http\RedirectResponse;
|
||||
use TYPO3\CMS\Core\Site\Entity\SiteInterface;
|
||||
use TYPO3\CMS\Core\Utility\GeneralUtility;
|
||||
use TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController;
|
||||
|
||||
class TrackingMiddleware implements MiddlewareInterface
|
||||
{
|
||||
use DbConnectionTrait;
|
||||
|
||||
const COOKIE_NAME = 'ep_trk';
|
||||
const COOKIE_TTL = 30;
|
||||
const CODE_PATTERN = '([a-z]{4}|PA)\d{4}.*';
|
||||
|
||||
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
|
||||
{
|
||||
if ($redirect = $this->matchRedirect($request)) {
|
||||
[$uri, $responseCode] = $redirect;
|
||||
|
||||
return new RedirectResponse(
|
||||
$uri,
|
||||
$responseCode
|
||||
);
|
||||
}
|
||||
|
||||
if ($uri = $this->matchTrackingCodeInUri($request)) {
|
||||
return new RedirectResponse($uri);
|
||||
}
|
||||
|
||||
return $handler->handle($request);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param ServerRequestInterface $request
|
||||
* @return array
|
||||
* @throws DBALException
|
||||
*/
|
||||
public function matchRedirect(ServerRequestInterface $request): ?array
|
||||
{
|
||||
/** @var NormalizedParams $params */
|
||||
$params = $request->getAttribute('normalizedParams');
|
||||
$uri = strtolower($params->getRequestUri());
|
||||
|
||||
if (substr($uri, -1) !== '/') {
|
||||
$uri .= '/';
|
||||
}
|
||||
|
||||
$qb = $this->getDbConnection()->createQueryBuilder();
|
||||
|
||||
$redirect = $qb
|
||||
->select('*')
|
||||
->from('tx_eptrack_domain_model_redirect')
|
||||
->where('redirect_from = LOWER(:uri)')
|
||||
->andWhere('deleted = 0')
|
||||
->andWhere('hidden = 0')
|
||||
->setParameter('uri', $uri)
|
||||
->execute()
|
||||
->fetch();
|
||||
|
||||
if ($redirect === false) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$trackingCode = $redirect['tracking_code'];
|
||||
if (!empty($trackingCode)) {
|
||||
$this->setCookie($trackingCode);
|
||||
}
|
||||
|
||||
$targetPage = $redirect['target_page_uid'];
|
||||
$responseCode = $redirect['response_code'];
|
||||
|
||||
$controller = $this->bootFrontendController($request->getAttribute('site'));
|
||||
|
||||
$typolinkConfig = ['parameter' => $targetPage, 'forceAbsoluteUrl' => true];
|
||||
|
||||
if ($redirect['utm_source'] && $redirect['utm_medium'] && $redirect['utm_campaign'] && $redirect['utm_content']) {
|
||||
$typolinkConfig['additionalParams'] = '&' . http_build_query([
|
||||
'utm_source' => $redirect['utm_source'],
|
||||
'utm_medium' => $redirect['utm_medium'],
|
||||
'utm_campaign' => $redirect['utm_campaign'],
|
||||
'utm_content' => $redirect['utm_content'],
|
||||
]);
|
||||
}
|
||||
|
||||
return [$controller->cObj->typolink_URL($typolinkConfig), $responseCode];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param ServerRequestInterface $request
|
||||
* @return string
|
||||
* @throws Exception
|
||||
*/
|
||||
public function matchTrackingCodeInUri(ServerRequestInterface $request): ?string
|
||||
{
|
||||
/** @var NormalizedParams $params */
|
||||
$params = $request->getAttribute('normalizedParams');
|
||||
$query = $params->getQueryString();
|
||||
$path = $params->getRequestUri();
|
||||
if (preg_match('/(' . static::CODE_PATTERN . ')\/?$/i', $path)) {
|
||||
$urlItems = GeneralUtility::trimExplode('/', $path, true);
|
||||
$code = array_pop($urlItems);
|
||||
$this->setCookie($code);
|
||||
$uri = '/';
|
||||
if (count($urlItems) > 0) {
|
||||
$uri = '/' . implode('/', $urlItems) . '/';
|
||||
}
|
||||
if (!empty($query)) {
|
||||
$uri .= '?' . $query;
|
||||
}
|
||||
|
||||
return $uri;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $code
|
||||
* @throws Exception
|
||||
*/
|
||||
protected function setCookie($code): void
|
||||
{
|
||||
$expiratonDate = new DateTime('now');
|
||||
$expiratonDate->add(new DateInterval('P' . static::COOKIE_TTL . 'D'));
|
||||
setcookie(static::COOKIE_NAME, $code, $expiratonDate->getTimestamp(), '/');
|
||||
}
|
||||
|
||||
/**
|
||||
* Borrowed from \TYPO3\CMS\Redirects\Service\RedirectService
|
||||
*
|
||||
* @param SiteInterface|null $site
|
||||
* @param array $queryParams
|
||||
* @return TypoScriptFrontendController
|
||||
* @throws ServiceUnavailableException
|
||||
* @throws ImmediateResponseException
|
||||
*/
|
||||
protected function bootFrontendController(?SiteInterface $site, array $queryParams = []): TypoScriptFrontendController
|
||||
{
|
||||
$GLOBALS['TYPO3_CONF_VARS']['FE']['pageUnavailable_handling'] = false;
|
||||
/** @var TypoScriptFrontendController $controller */
|
||||
$controller = GeneralUtility::makeInstance(
|
||||
TypoScriptFrontendController::class,
|
||||
null,
|
||||
$site ? $site->getRootPageId() : $GLOBALS['TSFE']->id,
|
||||
0
|
||||
);
|
||||
$controller->fe_user = $GLOBALS['TSFE']->fe_user ?? null;
|
||||
$controller->fetch_the_id();
|
||||
$controller->calculateLinkVars($queryParams);
|
||||
$controller->getConfigArray();
|
||||
$controller->settingLanguage();
|
||||
$controller->settingLocale();
|
||||
$controller->newCObj();
|
||||
|
||||
return $controller;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user