Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: applied code directly from issue #56

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
96 changes: 50 additions & 46 deletions Classes/TypoLink/LinkModifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
*/

use FoT3\Jumpurl\JumpUrlUtility;
use TYPO3\CMS\Core\Http\ApplicationType;
use TYPO3\CMS\Core\Http\ServerRequest;
use TYPO3\CMS\Core\LinkHandling\LinkService;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Core\Utility\StringUtility;
Expand All @@ -40,60 +42,62 @@ class LinkModifier

public function __invoke(AfterLinkIsGeneratedEvent $event): void
{
if ($this->isEnabled($event)) {
$url = $event->getLinkResult()->getUrl();
$context = $event->getLinkResult()->getType();
$configuration = $event->getLinkResult()->getLinkConfiguration();
$this->contentObjectRenderer = $event->getContentObjectRenderer();
$this->frontendController = $this->contentObjectRenderer->getTypoScriptFrontendController();

// Strip the absRefPrefix from the URLs.
$urlPrefix = (string)$this->getTypoScriptFrontendController()->absRefPrefix;
if ($urlPrefix !== '' && str_starts_with($url, $urlPrefix)) {
$url = substr($url, strlen($urlPrefix));
}

// Make sure the slashes in the file URL are not encoded.
if ($context === LinkService::TYPE_FILE) {
$url = str_replace('%2F', '/', rawurlencode(rawurldecode($url)));
}

if ($context === LinkService::TYPE_PAGE && $url === '') {
$url = '/';
}

$urlParameters = ['jumpurl' => $url];
if (($GLOBALS['TYPO3_REQUEST'] ?? null) instanceof ServerRequest && ApplicationType::fromRequest($GLOBALS['TYPO3_REQUEST'])->isFrontend()) {
if ($this->isEnabled($event)) {
$url = $event->getLinkResult()->getUrl();
$context = $event->getLinkResult()->getType();
$configuration = $event->getLinkResult()->getLinkConfiguration();
$this->contentObjectRenderer = $event->getContentObjectRenderer();
$this->frontendController = $this->contentObjectRenderer->getTypoScriptFrontendController();

// Strip the absRefPrefix from the URLs.
$urlPrefix = (string)$this->getTypoScriptFrontendController()->absRefPrefix;
if ($urlPrefix !== '' && str_starts_with($url, $urlPrefix)) {
$url = substr($url, strlen($urlPrefix));
}

$jumpUrlConfig = $configuration['jumpurl.'] ?? [];
// Make sure the slashes in the file URL are not encoded.
if ($context === LinkService::TYPE_FILE) {
$url = str_replace('%2F', '/', rawurlencode(rawurldecode($url)));
}

// see if a secure File URL should be built
if (!empty($jumpUrlConfig['secure'])) {
$secureParameters = $this->getParametersForSecureFile(
$url,
$jumpUrlConfig['secure.'] ?? []
);
$urlParameters = array_merge($urlParameters, $secureParameters);
} else {
$urlParameters['juHash'] = JumpUrlUtility::calculateHash($url);
}
if ($context === LinkService::TYPE_PAGE && $url === '') {
$url = '/';
}

$typoLinkConfiguration = [
'parameter' => $this->getTypoLinkParameter($jumpUrlConfig),
'additionalParams' => GeneralUtility::implodeArrayForUrl('', $urlParameters),
];
$urlParameters = ['jumpurl' => $url];

$jumpurl = $this->getContentObjectRenderer()->typoLink_URL($typoLinkConfiguration);
$jumpUrlConfig = $configuration['jumpurl.'] ?? [];

// Now add the prefix again if it was not added by a typolink call already.
if ($urlPrefix !== '') {
if (!str_starts_with($jumpurl, $urlPrefix)) {
$jumpurl = $urlPrefix . $jumpurl;
// see if a secure File URL should be built
if (!empty($jumpUrlConfig['secure'])) {
$secureParameters = $this->getParametersForSecureFile(
$url,
$jumpUrlConfig['secure.'] ?? []
);
$urlParameters = array_merge($urlParameters, $secureParameters);
} else {
$urlParameters['juHash'] = JumpUrlUtility::calculateHash($url);
}
if (!str_starts_with($url, $urlPrefix)) {
$url = $urlPrefix . $url;

$typoLinkConfiguration = [
'parameter' => $this->getTypoLinkParameter($jumpUrlConfig),
'additionalParams' => GeneralUtility::implodeArrayForUrl('', $urlParameters),
];

$jumpurl = $this->getContentObjectRenderer()->typoLink_URL($typoLinkConfiguration);

// Now add the prefix again if it was not added by a typolink call already.
if ($urlPrefix !== '') {
if (!str_starts_with($jumpurl, $urlPrefix)) {
$jumpurl = $urlPrefix . $jumpurl;
}
if (!str_starts_with($url, $urlPrefix)) {
$url = $urlPrefix . $url;
}
}
$event->setLinkResult($event->getLinkResult()->withAttributes(['href' => $jumpurl, 'jumpurl' => $url]));
}
$event->setLinkResult($event->getLinkResult()->withAttributes(['href' => $jumpurl, 'jumpurl' => $url]));
}
}

Expand Down