Skip to content
Snippets Groups Projects
Commit 8f34effd authored by Georgi Mateev's avatar Georgi Mateev
Browse files

[BUGFIX] #133 Invalid Resources-Path when TYPO3s Base-Url is a sub directory

parent 4aab3803
No related branches found
No related tags found
1 merge request!6Release 3.3.0
......@@ -26,6 +26,7 @@ namespace SGalinski\SgCookieOptin\Hook;
* This copyright notice MUST APPEAR in all copies of the script!
***************************************************************/
use SGalinski\SgCookieOptin\Service\BaseUrlService;
use SGalinski\SgCookieOptin\Service\ExtensionSettingsService;
use SGalinski\SgCookieOptin\Service\LanguageService;
use SGalinski\SgCookieOptin\Service\DemoModeService;
......@@ -64,6 +65,9 @@ class GenerateFilesAfterTcaSave {
const TEMPLATE_STYLE_SHEET_PATH = 'typo3conf/ext/sg_cookie_optin/Resources/Public/StyleSheets/';
const TEMPLATE_STYLE_SHEET_NAME = 'cookieOptin.css';
/** @var int */
protected $siteRoot = NULL;
/**
* Generates the files out of the TCA data.
*
......@@ -110,8 +114,8 @@ class GenerateFilesAfterTcaSave {
}
}
$siteRoot = (int) $dataHandler->getPID(self::TABLE_NAME, $originalRecord['uid']);
if ($siteRoot <= 0) {
$this->siteRoot = (int) $dataHandler->getPID(self::TABLE_NAME, $originalRecord['uid']);
if ($this->siteRoot <= 0) {
return;
}
......@@ -120,7 +124,7 @@ class GenerateFilesAfterTcaSave {
return;
}
$folderName = str_replace('#PID#', $siteRoot, $folder . self::FOLDER_SITEROOT);
$folderName = str_replace('#PID#', $this->siteRoot, $folder . self::FOLDER_SITEROOT);
$sitePath = defined('PATH_site') ? PATH_site : Environment::getPublicPath() . '/';
// First remove the folder with all files and then create it again. So no data artifacts are kept.
GeneralUtility::rmdir($sitePath . $folderName, TRUE);
......@@ -133,7 +137,7 @@ class GenerateFilesAfterTcaSave {
$originalTSFE = $typoScriptFrontendController = $GLOBALS['TSFE'];
if (!($typoScriptFrontendController instanceof TypoScriptFrontendController)) {
$typoScriptFrontendController = $GLOBALS['TSFE'] = new TypoScriptFrontendController(
$GLOBALS['TYPO3_CONF_VARS'], $siteRoot, 0
$GLOBALS['TYPO3_CONF_VARS'], $this->siteRoot, 0
);
}
......@@ -219,7 +223,7 @@ class GenerateFilesAfterTcaSave {
];
$this->createCSSFile($fullData, $folderName, $cssData, $minifyFiles);
$languages = LanguageService::getLanguages($siteRoot);
$languages = LanguageService::getLanguages($this->siteRoot);
foreach ($languages as $language) {
$languageUid = (int) $language['uid'];
if ($languageUid < 0) {
......@@ -519,7 +523,7 @@ class GenerateFilesAfterTcaSave {
}
GeneralUtility::fixPermissions($groupFile);
return '/' . $file;
return BaseUrlService::getSiteBaseUrl($this->siteRoot) . $file;
}
/**
......
<?php
namespace SGalinski\SgCookieOptin\Service;
use TYPO3\CMS\Core\Exception\SiteNotFoundException;
use TYPO3\CMS\Core\Site\SiteFinder;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Core\Utility\VersionNumberUtility;
class BaseUrlService {
/** @var SiteFinder|null */
private static $siteFinder;
/**
* Gets the base Url for this site root
*
* @param $rootPid
* @return string
*/
public static function getSiteBaseUrl($rootPid) {
if (VersionNumberUtility::convertVersionNumberToInteger(TYPO3_version) <= 9000000) {
return '/';
}
if (self::$siteFinder === null) {
self::$siteFinder = GeneralUtility::makeInstance( SiteFinder::class );
}
try {
$site = self::$siteFinder->getSiteByPageId( $rootPid );
$basePath = (string) $site->getBase();
} catch(SiteNotFoundException $e) {
$basePath = '/';
}
if ($basePath[strlen($basePath) - 1] !== '/') {
$basePath .= '/';
}
return $basePath;
}
}
......@@ -26,6 +26,7 @@ namespace SGalinski\SgCookieOptin\UserFunction;
* This copyright notice MUST APPEAR in all copies of the script!
***************************************************************/
use SGalinski\SgCookieOptin\Service\BaseUrlService;
use SGalinski\SgCookieOptin\Service\ExtensionSettingsService;
use SGalinski\SgCookieOptin\Service\DemoModeService;
use TYPO3\CMS\Core\Context\Context;
......@@ -70,6 +71,8 @@ class AddCookieOptinJsAndCss implements SingletonInterface {
if (!$folder) {
return '';
}
$siteBaseUrl = BaseUrlService::getSiteBaseUrl($this->rootpage);
$file = $folder . 'siteroot-' . $rootPageId . '/' . 'cookieOptin.js';
$sitePath = defined('PATH_site') ? PATH_site : Environment::getPublicPath() . '/';
......@@ -92,7 +95,7 @@ class AddCookieOptinJsAndCss implements SingletonInterface {
$jsonData = json_decode(file_get_contents($sitePath . $jsonFile), TRUE);
if (isset($jsonData['settings']['disable_for_this_language']) && !$jsonData['settings']['disable_for_this_language']) {
return '<script id="cookieOptinData" type="application/json">' . json_encode($jsonData) .
'</script><script src="/' . $file . '" type="text/javascript" data-ignore="1"></script>';
'</script><script src="' . $siteBaseUrl . $file . '" type="text/javascript" data-ignore="1"></script>';
}
} else {
// Old including from version 2.X.X @todo remove in version 4.X.X
......@@ -110,7 +113,7 @@ class AddCookieOptinJsAndCss implements SingletonInterface {
$cacheBuster = '';
}
return '<script src="/' . $file . '?' . $cacheBuster . '" type="text/javascript" data-ignore="1"></script>';
return '<script src="' . $siteBaseUrl . $file . '?' . $cacheBuster . '" type="text/javascript" data-ignore="1"></script>';
}
}
......@@ -144,8 +147,10 @@ class AddCookieOptinJsAndCss implements SingletonInterface {
if (!$cacheBuster) {
$cacheBuster = '';
}
$siteBaseUrl = BaseUrlService::getSiteBaseUrl($this->rootpage);
return '<link rel="stylesheet" type="text/css" href="/' . $file . '?' . $cacheBuster . '" media="all">';
return '<link rel="stylesheet" type="text/css" href="' . $siteBaseUrl . $file . '?' . $cacheBuster . '" media="all">';
}
/**
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment