<?php
declare(strict_types=1);
namespace FoxHabbit\BasisBundle\EventListener\Frontend;
use Pimcore\Bundle\CoreBundle\EventListener\Traits\EnabledTrait;
use Pimcore\Bundle\CoreBundle\EventListener\Traits\PimcoreContextAwareTrait;
use Pimcore\Bundle\CoreBundle\EventListener\Traits\PreviewRequestTrait;
use Pimcore\Bundle\CoreBundle\EventListener\Traits\ResponseInjectionTrait;
use Pimcore\Http\Request\Resolver\PimcoreContextResolver;
use Pimcore\Tool;
use Symfony\Component\HttpKernel\Event\ResponseEvent;
class CookiebotListener
{
use EnabledTrait;
use ResponseInjectionTrait;
use PimcoreContextAwareTrait;
use PreviewRequestTrait;
protected $cookiebotId = null;
protected $cookiebotManual = null;
public function onKernelResponse(ResponseEvent $event)
{
$code = null;
$request = $event->getRequest();
$response = $event->getResponse();
if (!$this->isHtmlResponse($response)) {
return;
} else if (!$this->isEnabled()) {
$code = '<!-- cookiebot listener not enabled -->';
} else if (!$event->isMasterRequest()) {
$code = '<!-- cookiebot listener: not in master request -->';
} else if (!$this->matchesPimcoreContext($request, PimcoreContextResolver::CONTEXT_DEFAULT)) {
// only inject tag manager code on non-admin requests
$code = '<!-- cookiebot listener: wrong request context -->';
} else if (!Tool::useFrontendOutputFilters()) {
$code = '<!-- cookiebot listener: output filters disabled -->';
} else if ($this->isPreviewRequest($request)) {
$code = '<!-- cookiebot listener: no output in preview -->';
} else if (!$this->isHtmlResponse($response)) {
return;
}
$siteId = null;
if(\Pimcore\Model\Site::isSiteRequest()) {
$site = \Pimcore\Model\Site::getCurrentSite();
$siteId = $site->getId();
}
if( $this->cookiebotId === null) {
$websiteSetting = \Pimcore\Model\WebsiteSetting::getByName('CookiebotId', $siteId);
if( $websiteSetting && $websiteSetting->getData()) {
$this->cookiebotId = $websiteSetting->getData();
}
}
if( ! $this->cookiebotId) {
$code = '<!-- cookiebot listener: no id set -->';
}
$locale = $event->getRequest()->getLocale();
$useIAB = $websiteSetting = \Pimcore\Model\WebsiteSetting::getByName('CookiebotUseIAB', $siteId);
$useIABCode = '';
if($useIAB && $useIAB->getData()) {
$useIABCode = 'data-framework="IAB"';
}
if( empty($code)) {
if( $this->cookiebotManual) {
$code = '<!-- script id="Cookiebot" src="https://consent.cookiebot.com/uc.js" '.$useIABCode.' data-cbid="'.$this->cookiebotId.'" data-culture="'.$locale.'" type="text/javascript" async></script> MANUAL -->';
} else {
$code = '<script id="Cookiebot" src="https://consent.cookiebot.com/uc.js" '.$useIABCode.' data-cbid="'.$this->cookiebotId.'" data-culture="'.$locale.'" data-blockingmode="auto" type="text/javascript"></script><!-- AUTO -->';
}
}
$content = $response->getContent();
if (!empty($code)) {
// search for the start <head> tag, and insert the code
$headStartPosition = stripos($content, "<head>");
if ($headStartPosition !== false) {
$content = substr_replace($content, "<head>".$code, $headStartPosition, 6);
}
}
$response->setContent($content);
}
}