Skip to content
Merged
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
40 changes: 0 additions & 40 deletions .tools/phpstan/baseline/missingType.iterableValue.php
Original file line number Diff line number Diff line change
Expand Up @@ -218,11 +218,6 @@
'count' => 1,
'path' => __DIR__ . '/../../../src/Mailer/Mailer.php',
];
$ignoreErrors[] = [
'rawMessage' => 'Method Redaxo\\Core\\MediaPool\\MediaHandler::addMedia() has parameter $allowedExtensions with no value type specified in iterable type array.',
'count' => 1,
'path' => __DIR__ . '/../../../src/MediaPool/MediaHandler.php',
];
$ignoreErrors[] = [
'rawMessage' => 'Method Redaxo\\Core\\MediaPool\\MediaHandler::addMedia() return type has no value type specified in iterable type array.',
'count' => 1,
Expand All @@ -233,36 +228,6 @@
'count' => 1,
'path' => __DIR__ . '/../../../src/MediaPool/MediaHandler.php',
];
$ignoreErrors[] = [
'rawMessage' => 'Method Redaxo\\Core\\MediaPool\\MediaPool::getAllowedExtensions() has parameter $args with no value type specified in iterable type array.',
'count' => 1,
'path' => __DIR__ . '/../../../src/MediaPool/MediaPool.php',
];
$ignoreErrors[] = [
'rawMessage' => 'Method Redaxo\\Core\\MediaPool\\MediaPool::isAllowedExtension() has parameter $args with no value type specified in iterable type array.',
'count' => 1,
'path' => __DIR__ . '/../../../src/MediaPool/MediaPool.php',
];
$ignoreErrors[] = [
'rawMessage' => 'Method Redaxo\\Core\\RexVar\\LinkListVar::getWidget() has parameter $args with no value type specified in iterable type array.',
'count' => 1,
'path' => __DIR__ . '/../../../src/RexVar/LinkListVar.php',
];
$ignoreErrors[] = [
'rawMessage' => 'Method Redaxo\\Core\\RexVar\\LinkVar::getWidget() has parameter $args with no value type specified in iterable type array.',
'count' => 1,
'path' => __DIR__ . '/../../../src/RexVar/LinkVar.php',
];
$ignoreErrors[] = [
'rawMessage' => 'Method Redaxo\\Core\\RexVar\\MediaListVar::getWidget() has parameter $args with no value type specified in iterable type array.',
'count' => 1,
'path' => __DIR__ . '/../../../src/RexVar/MediaListVar.php',
];
$ignoreErrors[] = [
'rawMessage' => 'Method Redaxo\\Core\\RexVar\\MediaVar::getWidget() has parameter $args with no value type specified in iterable type array.',
'count' => 1,
'path' => __DIR__ . '/../../../src/RexVar/MediaVar.php',
];
$ignoreErrors[] = [
'rawMessage' => 'Method Redaxo\\Core\\Security\\Login::setSessionVar() has parameter $value with no value type specified in iterable type array.',
'count' => 1,
Expand Down Expand Up @@ -398,11 +363,6 @@
'count' => 1,
'path' => __DIR__ . '/../../../src/View/Fragment.php',
];
$ignoreErrors[] = [
'rawMessage' => 'Method Redaxo\\Core\\Tests\\MediaPool\\MediaPoolTest::testIsAllowedExtension() has parameter $args with no value type specified in iterable type array.',
'count' => 1,
'path' => __DIR__ . '/../../../tests/MediaPool/MediaPoolTest.php',
];
$ignoreErrors[] = [
'rawMessage' => 'Method Redaxo\\Core\\Tests\\Security\\PasswordPolicyTest::testCheck() has parameter $options with no value type specified in iterable type array.',
'count' => 1,
Expand Down
37 changes: 0 additions & 37 deletions .tools/psalm/baseline.xml
Original file line number Diff line number Diff line change
Expand Up @@ -844,15 +844,6 @@
</PossiblyNullReference>
</file>
<file src="pages/mediapool/index.php">
<MixedArgumentTypeCoercion>
<code><![CDATA[$argUrl]]></code>
</MixedArgumentTypeCoercion>
<MixedAssignment>
<code><![CDATA[$argValue]]></code>
</MixedAssignment>
<MixedOperand>
<code><![CDATA[escape($argValue)]]></code>
</MixedOperand>
<PossiblyNullArgument>
<code><![CDATA[$error]]></code>
<code><![CDATA[$success]]></code>
Expand Down Expand Up @@ -2475,9 +2466,6 @@
</PossiblyFalseArgument>
</file>
<file src="src/MediaPool/MediaPool.php">
<MixedArgument>
<code><![CDATA[$args['types']]]></code>
</MixedArgument>
<MixedReturnStatement>
<code><![CDATA[Core::getProperty('allowed_mime_types', [])]]></code>
<code><![CDATA[Core::getProperty('allowed_mime_types', [])]]></code>
Expand All @@ -2500,25 +2488,6 @@
<code><![CDATA[['filename' => $params['filename']]]]></code>
</MixedArgumentTypeCoercion>
</file>
<file src="src/RexVar/MediaListVar.php">
<MixedArgument>
<code><![CDATA[$avalue]]></code>
</MixedArgument>
<MixedAssignment>
<code><![CDATA[$avalue]]></code>
</MixedAssignment>
</file>
<file src="src/RexVar/MediaVar.php">
<MixedArgument>
<code><![CDATA[$avalue]]></code>
</MixedArgument>
<MixedArgumentTypeCoercion>
<code><![CDATA[$aname]]></code>
</MixedArgumentTypeCoercion>
<MixedAssignment>
<code><![CDATA[$avalue]]></code>
</MixedAssignment>
</file>
<file src="src/Security/ApiFunction/UserHasSession.php">
<MixedArgument>
<code><![CDATA[$perm]]></code>
Expand Down Expand Up @@ -2929,12 +2898,6 @@
</MixedMethodCall>
</file>
<file src="src/View/View.php">
<MixedAssignment>
<code><![CDATA[$argValue]]></code>
</MixedAssignment>
<MixedOperand>
<code><![CDATA[escape($argValue)]]></code>
</MixedOperand>
<PossiblyFalseArgument>
<code><![CDATA[ini_get('upload_max_filesize')]]></code>
</PossiblyFalseArgument>
Expand Down
11 changes: 6 additions & 5 deletions pages/mediapool/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,18 @@
$func = Request::request('func', 'string');
$success = escape(Request::request('info', 'string'));
$error = escape(Request::request('warning', 'string'));
$args = Request::request('args', 'array');
$types = Request::request('types', 'string');

$regex = '@&lt;(/?(?:b|i|code)|br ?/?)&gt;@i';
$success = preg_replace($regex, '<$1>', $success);
$error = preg_replace($regex, '<$1>', $error);

// -------------- Additional Args
$argUrl = ['args' => $args];
$argUrl = [];
$argFields = '';
foreach ($args as $argName => $argValue) {
$argFields .= '<input type="hidden" name="args[' . escape($argName) . ']" value="' . escape($argValue) . '" />' . "\n";
if ('' !== $types) {
$argUrl['types'] = $types;
$argFields .= '<input type="hidden" name="types" value="' . escape($types) . '" />' . "\n";
}

// ----- opener_input_field setzen
Expand Down Expand Up @@ -113,4 +114,4 @@
}

// -------------- Include Page
Controller::includeCurrentPageSubPath(compact('openerInputField', 'openerLink', 'argUrl', 'args', 'argFields', 'rexFileCategory', 'rexFileCategoryName', 'PERMALL', 'fileId', 'error', 'success'));
Controller::includeCurrentPageSubPath(compact('openerInputField', 'openerLink', 'argUrl', 'argFields', 'rexFileCategory', 'rexFileCategoryName', 'PERMALL', 'fileId', 'error', 'success'));
11 changes: 5 additions & 6 deletions pages/mediapool/media.list.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
$error = '';
}

/** @var array{args?: array{types: string}, opener_input_field?: string} $argUrl */
/** @var array{types?: string, opener_input_field?: string} $argUrl */
$argUrl ??= [];

$mediaMethod = Request::request('media_method', 'string');
Expand Down Expand Up @@ -144,8 +144,8 @@
$success = '';
}

if (!empty($argUrl['args']['types'])) {
echo Message::info(I18n::msg('pool_file_filter') . ' <code>' . escape($argUrl['args']['types']) . '</code>');
if (!empty($argUrl['types'])) {
echo Message::info(I18n::msg('pool_file_filter') . ' <code>' . escape($argUrl['types']) . '</code>');
}

// Add Filter to the Query
Expand All @@ -162,9 +162,8 @@
$filter['category_id'] = $rexFileCategory;
}

if (isset($argUrl['args']['types']) && is_string($argUrl['args']['types'])) {
$types = explode(',', $argUrl['args']['types']);
$filter['types'] = $types;
if (isset($argUrl['types'])) {
$filter['types'] = explode(',', $argUrl['types']);
}

$context = new Context([
Expand Down
4 changes: 3 additions & 1 deletion pages/mediapool/upload.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,10 @@
['error', 'int'],
]);

$types = Request::post('types', 'string');

try {
$data = MediaHandler::addMedia($data, true, Request::post('args', 'array'));
$data = MediaHandler::addMedia($data, true, '' === $types ? [] : explode(',', $types));
$info = I18n::msg('pool_file_added');
if (Request::post('saveandexit', 'boolean')) {
if ('' != $openerInputField) {
Expand Down
4 changes: 2 additions & 2 deletions src/Form/Field/ArticleField.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,9 @@ public function formatElement()
static $widgetCounter = 1;

if ($this->multiple) {
$html = LinkListVar::getWidget($widgetCounter, $this->getAttribute('name'), $this->getValue(), ['category' => $this->categoryId]);
$html = LinkListVar::getWidget($widgetCounter, $this->getAttribute('name'), $this->getValue(), $this->categoryId);
} else {
$html = LinkVar::getWidget($widgetCounter, $this->getAttribute('name'), $this->getValue(), ['category' => $this->categoryId]);
$html = LinkVar::getWidget($widgetCounter, $this->getAttribute('name'), $this->getValue(), $this->categoryId);
}

++$widgetCounter;
Expand Down
25 changes: 13 additions & 12 deletions src/Form/Field/MediaField.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,12 @@

class MediaField extends BaseField
{
/** @var array{category?: int, types?: string, preview?: bool} */
private array $args = [];
private ?int $categoryId = null;

/** @var list<string> */
private array $types = [];

private bool $preview = false;

private bool $multiple = false;

Expand All @@ -35,16 +39,13 @@ public function __construct($tag = '', ?AbstractForm $form = null, array $attrib
*/
public function setCategoryId($categoryId)
{
$this->args['category'] = $categoryId;
$this->categoryId = $categoryId;
}

/**
* @param string $types
* @return void
*/
public function setTypes($types)
/** @param list<string> $types file extensions */
public function setTypes(array $types): void
{
$this->args['types'] = $types;
$this->types = $types;
}

/**
Expand All @@ -53,7 +54,7 @@ public function setTypes($types)
*/
public function setPreview($preview = true)
{
$this->args['preview'] = $preview;
$this->preview = $preview;
}

public function setMultiple(bool $multiple = true): void
Expand All @@ -67,9 +68,9 @@ public function formatElement()
static $widgetCounter = 1;

if ($this->multiple) {
$html = MediaListVar::getWidget($widgetCounter, $this->getAttribute('name'), $this->getValue(), $this->args);
$html = MediaListVar::getWidget($widgetCounter, $this->getAttribute('name'), $this->getValue(), $this->categoryId, $this->types, $this->preview);
} else {
$html = MediaVar::getWidget($widgetCounter, $this->getAttribute('name'), $this->getValue(), $this->args);
$html = MediaVar::getWidget($widgetCounter, $this->getAttribute('name'), $this->getValue(), $this->categoryId, $this->types, $this->preview);
}

++$widgetCounter;
Expand Down
7 changes: 4 additions & 3 deletions src/MediaPool/MediaHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,9 @@ private function __construct() {}
*
* @param array{category_id: int, title: string, file: array{name: string, path?: string, tmp_name?: string, error?: int}} $data
* @param bool $doSubindexing // echte Dateinamen anpassen, falls schon vorhanden
* @param list<string> $types Restrict the allowed file extensions to these types
*/
public static function addMedia(array $data, bool $doSubindexing = true, array $allowedExtensions = []): array
public static function addMedia(array $data, bool $doSubindexing = true, array $types = []): array
{
$error = $data['file']['error'] ?? null;

Expand All @@ -64,9 +65,9 @@ public static function addMedia(array $data, bool $doSubindexing = true, array $
throw new ApiFunctionException(I18n::msg('pool_file_not_found'));
}

if (!MediaPool::isAllowedExtension($data['file']['name'], $allowedExtensions)) {
if (!MediaPool::isAllowedExtension($data['file']['name'], $types)) {
$warning = I18n::msg('pool_file_mediatype_not_allowed') . ' <code>' . escape(File::extension($data['file']['name'])) . '</code>';
$allowedExtensions = MediaPool::getAllowedExtensions($allowedExtensions);
$allowedExtensions = MediaPool::getAllowedExtensions($types);
$warning .= count($allowedExtensions) > 0
? '<br />' . I18n::msg('pool_file_allowed_mediatypes') . ' <code>' . rtrim(implode('</code>, <code>', escape($allowedExtensions)), ', ') . '</code>'
: '<br />' . I18n::msg('pool_file_banned_mediatypes') . ' <code>' . rtrim(implode('</code>, <code>', escape(MediaPool::getBlockedExtensions())), ', ') . '</code>';
Expand Down
28 changes: 15 additions & 13 deletions src/MediaPool/MediaPool.php
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,12 @@ public static function mediaIsInUse(string $filename): string|false
return false;
}

/** check if mediatpye(extension) is allowed for upload. */
public static function isAllowedExtension(string $filename, array $args = []): bool
/**
* Check if the media type (extension) is allowed for upload.
*
* @param list<string> $types Restrict to these file extensions (in addition to the global block list)
*/
public static function isAllowedExtension(string $filename, array $types = []): bool
{
$fileExt = mb_strtolower(File::extension($filename));

Expand All @@ -135,7 +139,7 @@ public static function isAllowedExtension(string $filename, array $args = []): b
}
}

$allowedExtensions = self::getAllowedExtensions($args);
$allowedExtensions = self::getAllowedExtensions($types);
return !count($allowedExtensions) || in_array($fileExt, $allowedExtensions);
}

Expand Down Expand Up @@ -166,23 +170,21 @@ public static function isAllowedMimeType(string $path, ?string $filename = null)
}

/**
* Get allowed mediatype extensions given via media widget "types" param.
* Get allowed media type extensions given via media widget "types" param.
*
* @param array $args widget params
* @param list<string> $types
* @return list<string> allowed extensions
*/
public static function getAllowedExtensions(array $args = []): array
public static function getAllowedExtensions(array $types = []): array
{
$blockedExtensions = self::getBlockedExtensions();

$allowedExtensions = [];
if (isset($args['types'])) {
foreach (explode(',', $args['types']) as $ext) {
$ext = ltrim($ext, '.');
$ext = mb_strtolower($ext);
if (!in_array($ext, $blockedExtensions)) { // allowedExtensions cannot override any blockedExtensions entry from master
$allowedExtensions[] = $ext;
}
foreach ($types as $ext) {
$ext = ltrim($ext, '.');
$ext = mb_strtolower($ext);
if (!in_array($ext, $blockedExtensions)) { // allowedExtensions cannot override any blockedExtensions entry from master
$allowedExtensions[] = $ext;
}
}
return $allowedExtensions;
Expand Down
9 changes: 2 additions & 7 deletions src/MetaInfo/Field/ArticleField.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,19 +72,14 @@ public function renderInput(MetaContext $context): string
{
$category = $this->category ?? $context->category?->id;

$args = [];
if (null !== $category) {
$args['category'] = $category;
}

$name = $this->columnName($context->entity);
$id = ++self::$widgetCounter;
$value = (string) $context->value($this);

if ($this->multiple) {
return LinkListVar::getWidget($id, $name, $value, $args);
return LinkListVar::getWidget($id, $name, $value, $category);
}

return LinkVar::getWidget($id, $name, '' === $value ? null : (int) $value, $args);
return LinkVar::getWidget($id, $name, '' === $value ? null : (int) $value, $category);
}
}
Loading
Loading