42 lines
1.4 KiB
PHP
42 lines
1.4 KiB
PHP
<?php
|
|
declare(strict_types=1);
|
|
|
|
namespace Esun\FileManager\Model\Config\Backend;
|
|
|
|
use Magento\Framework\App\Config\Value;
|
|
use Magento\Framework\Exception\LocalizedException;
|
|
use Esun\FileManager\Model\Security\FilePolicy;
|
|
|
|
class AllowedExtensions extends Value
|
|
{
|
|
/**
|
|
* @throws LocalizedException
|
|
*/
|
|
public function beforeSave(): self
|
|
{
|
|
$value = strtolower((string) $this->getValue());
|
|
$extensions = preg_split('/[\s,]+/', $value, -1, PREG_SPLIT_NO_EMPTY) ?: [];
|
|
if (!$extensions) {
|
|
throw new LocalizedException(__('At least one allowed extension is required.'));
|
|
}
|
|
|
|
foreach ($extensions as $extension) {
|
|
if (!preg_match('/^[a-z0-9]+$/', $extension)) {
|
|
throw new LocalizedException(__('Invalid extension: %1', $extension));
|
|
}
|
|
if (in_array($extension, FilePolicy::BLOCKED_EXTENSIONS, true)) {
|
|
throw new LocalizedException(__('The extension %1 is always forbidden.', $extension));
|
|
}
|
|
if (!in_array($extension, FilePolicy::getSupportedExtensions(), true)) {
|
|
throw new LocalizedException(
|
|
__('The extension %1 does not have a secure MIME policy.', $extension)
|
|
);
|
|
}
|
|
}
|
|
|
|
$this->setValue(implode(',', array_values(array_unique($extensions))));
|
|
|
|
return parent::beforeSave();
|
|
}
|
|
}
|