Skip to content

Commit

Permalink
Normalize base URL as well; Change html_entity_decode into escaper fo…
Browse files Browse the repository at this point in the history
…r PHPCS
  • Loading branch information
jissereitsma committed Jun 29, 2022
1 parent ef4b943 commit 120572a
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 8 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

## [0.3.9] - 29 June 2022
### Fixed
- Normalize base URL as well
- Change html_entity_decode into escaper for PHPCS

## [0.3.8] - 29 June 2022
### Fixed
- Make sure to keep HTTPS URLs for media intact
Expand Down
28 changes: 21 additions & 7 deletions Util/UrlConvertor.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Yireo\NextGenImages\Util;

use Magento\Framework\App\Filesystem\DirectoryList as FilesystemDirectoryList;
use Magento\Framework\Escaper;
use Magento\Framework\Exception\FileSystemException;
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Framework\Filesystem\DirectoryList;
Expand All @@ -28,20 +29,27 @@ class UrlConvertor
* @var DirectoryList
*/
private $directoryList;
/**
* @var Escaper
*/
private $escaper;

/**
* @param UrlInterface $urlModel
* @param StoreManagerInterface $storeManager
* @param DirectoryList $directoryList
* @param Escaper $escaper
*/
public function __construct(
UrlInterface $urlModel,
StoreManagerInterface $storeManager,
DirectoryList $directoryList
DirectoryList $directoryList,
Escaper $escaper
) {
$this->urlModel = $urlModel;
$this->storeManager = $storeManager;
$this->directoryList = $directoryList;
$this->escaper = $escaper;
}

/**
Expand All @@ -55,7 +63,7 @@ public function isLocal(string $url): bool
return true;
}

$baseUrl = $this->normalizeUrl($this->getBaseUrl());
$baseUrl = $this->getBaseUrl();
if (strpos($url, $baseUrl) !== false) {
return true;
}
Expand Down Expand Up @@ -96,11 +104,11 @@ public function getUrlFromFilename(string $filename): string
}

if (strpos($filename, $this->getBaseFolder()) !== false) {
return str_replace($this->getBaseFolder() . '/', $this->getBaseUrl(), $filename);
return str_replace($this->getBaseFolder() . '/', $this->getBaseUrl(false), $filename);
}

if (!preg_match('/^\//', $filename)) {
return $this->getBaseUrl() . $filename;
return $this->getBaseUrl(false) . $filename;
}

throw new NotFoundException((string)__('Filename "' . $filename . '" is not matched with an URL'));
Expand All @@ -113,7 +121,7 @@ public function getUrlFromFilename(string $filename): string
*/
public function getFilenameFromUrl(string $url): string
{
$url = html_entity_decode($url);
$url = $this->escaper->escapeHtml($url);
$url = preg_replace('/\/static\/version(\d+\/)/', '/static/', $url);
$url = $this->normalizeUrl($url);

Expand Down Expand Up @@ -149,11 +157,17 @@ public function getFilenameFromUrl(string $url): string
}

/**
* @param bool $normalizeUrl
* @return string
*/
private function getBaseUrl(): string
private function getBaseUrl(bool $normalizeUrl = true): string
{
return str_replace('/index.php/', '/', $this->urlModel->getBaseUrl());
$baseUrl = str_replace('/index.php/', '/', $this->urlModel->getBaseUrl());
if ($normalizeUrl === false) {
return $baseUrl;
}

return $this->normalizeUrl($baseUrl);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "yireo/magento2-next-gen-images",
"license": "OSL-3.0",
"version": "0.3.8",
"version": "0.3.9",
"type": "magento2-module",
"homepage": "https://www.yireo.com/software/magento-extensions/next-gen-images",
"description": "Magento 2 module to add NextGen images support to the Magento frontend",
Expand Down

0 comments on commit 120572a

Please sign in to comment.