vendor/oveleon/contao-cookiebar/src/EventListener/FrontendTemplateListener.php line 102

Open in your IDE?
  1. <?php
  2. /**
  3. * This file is part of Oveleon Contao Cookiebar.
  4. *
  5. * @package contao-cookiebar
  6. * @license AGPL-3.0
  7. * @author Daniele Sciannimanica <https://github.com/doishub>
  8. * @copyright Oveleon <https://www.oveleon.de/>
  9. */
  10. namespace Oveleon\ContaoCookiebar\EventListener;
  11. use Contao\StringUtil;
  12. use Contao\System;
  13. use Oveleon\ContaoCookiebar\Cookiebar;
  14. class FrontendTemplateListener
  15. {
  16. /**
  17. * Output front end template
  18. *
  19. * @param string $buffer
  20. * @param string $template
  21. *
  22. * @return string
  23. */
  24. public function onOutputFrontendTemplate(string $buffer, string $template): string
  25. {
  26. $arrPageTemplates = System::getContainer()->getParameter('contao_cookiebar.page_templates') ?? ['fe_page'];
  27. if (!in_array($template, $arrPageTemplates) && 0 !== strpos($template, 'fe_page')) {
  28. return $buffer;
  29. }
  30. global $objPage;
  31. $objConfig = Cookiebar::getConfigByPage($objPage->rootId);
  32. if(null === $objConfig)
  33. {
  34. return $buffer;
  35. }
  36. // If a cookie is still set by an older version, it must be deleted
  37. Cookiebar::checkCookie();
  38. // Parse template
  39. $strHtml = Cookiebar::parseCookiebarTemplate($objConfig);
  40. // Load cookie bar scripts
  41. if($objConfig->scriptPosition === 'body')
  42. {
  43. $strHtml .= '<script src="bundles/contaocookiebar/scripts/cookiebar.min.js"></script>';
  44. }
  45. else
  46. {
  47. $GLOBALS['TL_JAVASCRIPT'][] = 'bundles/contaocookiebar/scripts/cookiebar.min.js|static';
  48. }
  49. // Add cookiebar script initialization
  50. $strHtml .= sprintf("<script>var cookiebar = new ContaoCookiebar({configId:%s,pageId:%s,hideOnInit:%s,blocking:%s,focusTrap:%s,version:%s,lifetime:%s,consentLog:%s,token:'%s',doNotTrack:%s,currentPageId:%s,excludedPageIds:%s,cookies:%s,configs:%s,texts:{acceptAndDisplay:'%s'}});</script>",
  51. $objConfig->id,
  52. $objConfig->pageId,
  53. $objConfig->hideOnInit ? 1 : 0,
  54. $objConfig->blocking ? 1 : 0,
  55. System::getContainer()->getParameter('contao_cookiebar.disable_focustrap') ? 0 : 1,
  56. $objConfig->version,
  57. System::getContainer()->getParameter('contao_cookiebar.lifetime'),
  58. System::getContainer()->getParameter('contao_cookiebar.consent_log') ? 1 : 0,
  59. System::getContainer()->getParameter('contao_cookiebar.storage_key'),
  60. System::getContainer()->getParameter('contao_cookiebar.consider_dnt') ? 1 : 0,
  61. $objPage->id,
  62. json_encode(StringUtil::deserialize($objConfig->excludePages)),
  63. json_encode(Cookiebar::validateCookies($objConfig)),
  64. json_encode(Cookiebar::validateGlobalConfigs($objConfig)),
  65. $GLOBALS['TL_LANG']['tl_cookiebar']['acceptAndDisplayLabel']
  66. );
  67. if(null !== $objConfig)
  68. {
  69. switch($objConfig->position)
  70. {
  71. case 'bodyAboveContent':
  72. $buffer = preg_replace("/<body([^>]*)>(.*?)<\/body>/is", "<body$1>$strHtml$2</body>", $buffer);
  73. break;
  74. default:
  75. $buffer = str_replace("</body>", "$strHtml</body>", $buffer);
  76. }
  77. }
  78. return $buffer;
  79. }
  80. /**
  81. * Parse front end template
  82. *
  83. * @param string $buffer
  84. * @param string $template
  85. *
  86. * @return string
  87. */
  88. public function onParseFrontendTemplate(string $buffer, string $template): string
  89. {
  90. global $objPage;
  91. if (TL_MODE == 'BE' || null === $objPage)
  92. {
  93. return $buffer;
  94. }
  95. $objConfig = Cookiebar::getConfigByPage($objPage->rootId);
  96. if(null === $objConfig)
  97. {
  98. return $buffer;
  99. }
  100. $arrTypes = Cookiebar::getIframeTypes();
  101. $arrCookies = Cookiebar::validateCookies($objConfig);
  102. foreach ($arrTypes as $strType => $arrTemplates)
  103. {
  104. if(in_array($template, $arrTemplates))
  105. {
  106. foreach ($arrCookies as $cookie)
  107. {
  108. if(isset($cookie['iframeType']) && $cookie['iframeType'] === $strType)
  109. {
  110. $strBlockUrl = 'cookiebar/block/'.$objPage->language.'/'.$cookie['id'].'?redirect=';
  111. // Check if the element is delivered with a preview image
  112. if(strpos($buffer, 'id="splashImage') !== false)
  113. {
  114. // Regex: Modify href attribute for splash images
  115. $atagRegex = "/id=\"splashImage_([^>]*)href=\"([^>]*)\"/is";
  116. // Get current href attribute
  117. preg_match($atagRegex, $buffer, $matches);
  118. // Overwrite href attribute
  119. $buffer = preg_replace($atagRegex, 'id="splashImage_$1href="'.$strBlockUrl.urlencode($matches[2]).'"', $buffer);
  120. $buffer = str_replace('iframe.src', 'iframe.setAttribute("data-ccb-id", "'.$cookie['id'].'"); iframe.src', $buffer);
  121. }
  122. else
  123. {
  124. // Regex: Modify src attribute for iframes
  125. $frameRegex = "/<iframe([\s\S]*?)src=([\\\\\"\']+)(.*?)[\\\\\"\']+/i";
  126. // Get current src attribute
  127. preg_match_all($frameRegex, $buffer, $matches);
  128. for ($i=0; $i < count($matches[0]); $i++)
  129. {
  130. $quote = $matches[2][$i];
  131. $search = 'src=' . $quote;
  132. $replace = 'data-ccb-id=' . $quote . $cookie['id'] . $quote . ' src='. $quote . $strBlockUrl . urlencode($matches[3][$i]) . $quote . ' data-src=' . $quote;
  133. $iframe = str_replace($search, $replace, $matches[0][$i]);
  134. $buffer = str_replace($matches[0][$i], $iframe, $buffer);
  135. }
  136. }
  137. }
  138. }
  139. break;
  140. }
  141. }
  142. return $buffer;
  143. }
  144. }