Skip to content
Open
Show file tree
Hide file tree
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
20 changes: 18 additions & 2 deletions Plugin/ImageContentValidatorExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@
use Magento\Framework\Api\Data\ImageContentInterface;
use Magento\Framework\Api\ImageContentValidator;
use Magento\Framework\Exception\InputException;
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Framework\Filesystem\Io\File as IoFile;
use Magento\Framework\Phrase;
use MarkShust\PolyshellPatch\Utility\Configurations;

/**
* Validate that the uploaded filename has a safe image extension.
Expand All @@ -26,12 +28,20 @@ class ImageContentValidatorExtension
*/
private IoFile $ioFile;

/**
* @var Configurations
*/
private Configurations $configurations;

/**
* @param IoFile $ioFile
*/
public function __construct(IoFile $ioFile)
{
public function __construct(
IoFile $ioFile,
Configurations $configurations
) {
$this->ioFile = $ioFile;
$this->configurations = $configurations;
}

/**
Expand All @@ -40,8 +50,10 @@ public function __construct(IoFile $ioFile)
* @param ImageContentValidator $subject
* @param bool $result
* @param ImageContentInterface $imageContent
*
* @return bool
* @throws InputException
* *@throws NoSuchEntityException
*
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
*/
Expand All @@ -50,6 +62,10 @@ public function afterIsValid(
bool $result,
ImageContentInterface $imageContent
): bool {
if (!$this->configurations->isModuleEnabled()) {
return $result;
}

$fileName = $imageContent->getName();
$pathInfo = $this->ioFile->getPathInfo($fileName);
$extension = strtolower($pathInfo['extension'] ?? '');
Expand Down
21 changes: 19 additions & 2 deletions Plugin/ImageProcessorRestrictExtensions.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
use Magento\Framework\Api\Data\ImageContentInterface;
use Magento\Framework\Api\ImageProcessor;
use Magento\Framework\Api\Uploader;
use Magento\Framework\Exception\NoSuchEntityException;
use MarkShust\PolyshellPatch\Utility\Configurations;

/**
* Enforce an allowlist of file extensions before ImageProcessor saves uploaded files.
Expand All @@ -23,12 +25,21 @@ class ImageProcessorRestrictExtensions
*/
private Uploader $uploader;

/**
* @var Configurations
*/
private Configurations $configurations;

/**
* @param Uploader $uploader
* @param Configurations $configurations
*/
public function __construct(Uploader $uploader)
{
public function __construct(
Uploader $uploader,
Configurations $configurations
) {
$this->uploader = $uploader;
$this->configurations = $configurations;
}

/**
Expand All @@ -37,15 +48,21 @@ public function __construct(Uploader $uploader)
* @param ImageProcessor $subject
* @param string $entityType
* @param ImageContentInterface $imageContent
*
* @return null
*
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
* @throws NoSuchEntityException
*/
public function beforeProcessImageContent(
ImageProcessor $subject,
$entityType,
$imageContent
) {
if (!$this->configurations->isModuleEnabled()) {
return null;
}

$this->uploader->setAllowedExtensions(self::ALLOWED_EXTENSIONS);
return null;
}
Expand Down
56 changes: 56 additions & 0 deletions Utility/Configurations.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php

declare(strict_types=1);

namespace MarkShust\PolyshellPatch\Utility;

use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Store\Model\ScopeInterface;
use Magento\Store\Model\StoreManagerInterface;

/**
* Class Configurations
*/
class Configurations
{
/**
* Configurations constructor.
*
* @param ScopeConfigInterface $scopeConfig
* @param StoreManagerInterface $storeManager
*/
public function __construct(
private readonly ScopeConfigInterface $scopeConfig,
private readonly StoreManagerInterface $storeManager
) {
}

/**
* Get boolean configuration value
*
* @param $path
*
* @return bool
* @throws NoSuchEntityException
*/
private function _isSetFlag($path): bool
{
return $this->scopeConfig->isSetFlag(
$path,
ScopeInterface::SCOPE_STORE,
$this->storeManager->getStore()->getStoreId()
);
}

/**
* Check if the module is enabled
*
* @return bool
* @throws NoSuchEntityException
*/
public function isModuleEnabled(): bool
{
return $this->_isSetFlag(Constants::POLYSHELL_ENABLED_PATH);
}
}
14 changes: 14 additions & 0 deletions Utility/Constants.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

declare(strict_types=1);

namespace MarkShust\PolyshellPatch\Utility;

/**
* Class Constants
*/
class Constants
{
// Configurations paths
public const POLYSHELL_ENABLED_PATH = 'polyshellpatch/general/enabled';
}
18 changes: 18 additions & 0 deletions etc/acl.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:framework:Acl/etc/acl.xsd">
<acl>
<resources>
<resource id="Magento_Backend::admin">
<resource id="Magento_Backend::stores">
<resource id="Magento_Backend::stores_settings">
<resource id="Magento_Config::config">
<resource id="MarkShust_PolyshellPatch::polyshellpatch_config"
title="PolyShell Patch Section" sortOrder="30"/>
</resource>
</resource>
</resource>
</resource>
</resources>
</acl>
</config>
27 changes: 27 additions & 0 deletions etc/adminhtml/system.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Config:etc/system_file.xsd">
<system>
<tab id="mark_shust" translate="label" sortOrder="130">
<label>Mark Shust</label>
</tab>
<section id="polyshellpatch" translate="label" sortOrder="1"
showInDefault="1" showInWebsite="1" showInStore="1">
<class>separator-top</class>
<label>PolyShell Patch</label>
<tab>mark_shust</tab>
<resource>MarkShust_PolyshellPatch::polyshellpatch_config</resource>
<!-- General -->
<group id="general" translate="label" type="text" sortOrder="10"
showInDefault="1" showInWebsite="1" showInStore="1">
<label>General</label>
<field id="enabled" translate="label comment" type="select" sortOrder="10"
showInDefault="1" showInWebsite="1" showInStore="1">
<label>Enable</label>
<source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
<comment>Enable Polyshell Patch</comment>
</field>
</group>
</section>
</system>
</config>
11 changes: 11 additions & 0 deletions etc/config.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Store:etc/config.xsd">
<default>
<polyshellpatch>
<general>
<enabled>1</enabled>
</general>
</polyshellpatch>
</default>
</config>