Move tracking cookie handling to utility class
This commit is contained in:
@@ -27,10 +27,9 @@ namespace EP\EpTrack\Middleware;
|
||||
* 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 EP\EpProducts\Utility\CookieUtility;
|
||||
use Exception;
|
||||
use Psr\Http\Message\ResponseInterface;
|
||||
use Psr\Http\Message\ServerRequestInterface;
|
||||
@@ -48,14 +47,10 @@ class TrackingMiddleware implements MiddlewareInterface
|
||||
{
|
||||
use DbConnectionTrait;
|
||||
|
||||
const COOKIE_NAME = 'ep_trk';
|
||||
const COOKIE_TTL = 30;
|
||||
const CODE_PATTERN = '([a-z]{4}|PA)\d{3,4}';
|
||||
const CODE_PATTERN = 'PA\d{3,4}';
|
||||
|
||||
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
|
||||
{
|
||||
$this->cleanupCookies();
|
||||
|
||||
if ($redirect = $this->matchRedirect($request)) {
|
||||
[$uri, $responseCode] = $redirect;
|
||||
|
||||
@@ -104,8 +99,9 @@ class TrackingMiddleware implements MiddlewareInterface
|
||||
}
|
||||
|
||||
$trackingCode = $redirect['tracking_code'];
|
||||
|
||||
if (!empty($trackingCode)) {
|
||||
$this->setCookie($trackingCode);
|
||||
CookieUtility::setPaCookie($trackingCode);
|
||||
}
|
||||
|
||||
$targetPage = $redirect['target_page_uid'];
|
||||
@@ -139,13 +135,13 @@ class TrackingMiddleware implements MiddlewareInterface
|
||||
$query = $params->getQueryString();
|
||||
$path = $params->getRequestUri();
|
||||
|
||||
if (!preg_match('/^\/' . static::CODE_PATTERN . '\/?$/i', $path)) {
|
||||
if (!preg_match('/^\/PA\d{3,4}\/?$/i', $path)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$urlItems = GeneralUtility::trimExplode('/', $path, true);
|
||||
$code = array_pop($urlItems);
|
||||
$this->setCookie($code);
|
||||
CookieUtility::setPaCookie($code);
|
||||
$uri = '/';
|
||||
if (count($urlItems) > 0) {
|
||||
$uri = '/' . implode('/', $urlItems) . '/';
|
||||
@@ -157,29 +153,6 @@ class TrackingMiddleware implements MiddlewareInterface
|
||||
return $uri;
|
||||
}
|
||||
|
||||
/**
|
||||
* @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(), '/');
|
||||
}
|
||||
|
||||
protected function cleanupCookies(): void
|
||||
{
|
||||
if (!isset($_COOKIE[static::COOKIE_NAME])) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (!preg_match('/^' . static::CODE_PATTERN . '$/', $_COOKIE[static::COOKIE_NAME])) {
|
||||
setcookie(static::COOKIE_NAME, '', time() - 3600);
|
||||
unset($_COOKIE[static::COOKIE_NAME]);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Borrowed from \TYPO3\CMS\Redirects\Service\RedirectService
|
||||
*
|
||||
|
||||
Reference in New Issue
Block a user