Skip to content

Commit 6b4aedb

Browse files
committed
[!!!][TASK] Avoid obsolete $charset in sanitizeFileName()
DriverInterface: public function sanitizeFileName(string $fileName, string $charset = ''): string; LocalDriver implementation: public function sanitizeFileName(string $fileName, string $charset = 'utf-8'): string All consumers (mainly ResourceStorage) call the method without handing over the second argument. The uselessness of the second argument has been stated in an @todo for a while already. The patch drops the second argument in interface and implementation. Resolves: #105686 Releases: main Change-Id: I1585792f69e05e75c4f22f4e67599b2d322bb288 Reviewed-on: https://review.typo3.org/c/Packages/TYPO3.CMS/+/87212 Tested-by: Christian Kuhn <[email protected]> Reviewed-by: Simon Praetorius <[email protected]> Reviewed-by: Christian Kuhn <[email protected]> Tested-by: core-ci <[email protected]> Tested-by: Simon Praetorius <[email protected]> Tested-by: Andreas Nedbal <[email protected]> Reviewed-by: Andreas Nedbal <[email protected]>
1 parent 9abb07f commit 6b4aedb

File tree

6 files changed

+62
-61
lines changed

6 files changed

+62
-61
lines changed

Classes/Resource/Driver/DriverInterface.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,11 +73,9 @@ public function isCaseSensitiveFileSystem(): bool;
7373
* Cleans a fileName from not allowed characters
7474
*
7575
* @param non-empty-string $fileName
76-
* @param string $charset Charset of the a fileName
77-
* (defaults to current charset; depending on context)
7876
* @return non-empty-string the sanitized filename
7977
*/
80-
public function sanitizeFileName(string $fileName, string $charset = ''): string;
78+
public function sanitizeFileName(string $fileName): string;
8179

8280
/**
8381
* Hashes a file identifier, taking the case sensitivity of the file system

Classes/Resource/Driver/LocalDriver.php

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -306,32 +306,24 @@ public function getFolderInfoByIdentifier(string $folderIdentifier): array
306306
* Previously in \TYPO3\CMS\Core\Utility\File\BasicFileUtility::cleanFileName()
307307
*
308308
* @param string $fileName Input string, typically the body of a fileName
309-
* @param non-empty-string $charset Charset of the a fileName (defaults to utf-8)
310309
* @return non-empty-string Output string with any characters not matching [.a-zA-Z0-9_-] is substituted by '_' and trailing dots removed
311-
* @todo: at some point it is safe to drop the second argument $charset
312310
*/
313-
public function sanitizeFileName(string $fileName, string $charset = 'utf-8'): string
311+
public function sanitizeFileName(string $fileName): string
314312
{
315-
if ($charset === 'utf-8') {
316-
$fileName = \Normalizer::normalize($fileName) ?: $fileName;
317-
}
318-
313+
$fileName = \Normalizer::normalize($fileName) ?: $fileName;
319314
// Handle UTF-8 characters
320315
if ($GLOBALS['TYPO3_CONF_VARS']['SYS']['UTF8filesystem']) {
321316
// Allow ".", "-", 0-9, a-z, A-Z and everything beyond U+C0 (latin capital letter a with grave)
322317
$cleanFileName = (string)preg_replace('/[' . self::UNSAFE_FILENAME_CHARACTER_EXPRESSION . ']/u', '_', trim($fileName));
323318
} else {
324-
$fileName = GeneralUtility::makeInstance(CharsetConverter::class)->specCharsToASCII($charset, $fileName);
319+
$fileName = GeneralUtility::makeInstance(CharsetConverter::class)->specCharsToASCII('utf-8', $fileName);
325320
// Replace unwanted characters with underscores
326321
$cleanFileName = (string)preg_replace('/[' . self::UNSAFE_FILENAME_CHARACTER_EXPRESSION . '\\xC0-\\xFF]/', '_', trim($fileName));
327322
}
328323
// Strip trailing dots and return
329324
$cleanFileName = rtrim($cleanFileName, '.');
330325
if ($cleanFileName === '') {
331-
throw new InvalidFileNameException(
332-
'File name ' . $fileName . ' is invalid.',
333-
1320288991
334-
);
326+
throw new InvalidFileNameException('File name ' . $fileName . ' is invalid.', 1320288991);
335327
}
336328
return $cleanFileName;
337329
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
.. include:: /Includes.rst.txt
2+
3+
.. _breaking-105686-1732289792:
4+
5+
=================================================================
6+
Breaking: #105686 - Avoid obsolete $charset in sanitizeFileName()
7+
=================================================================
8+
9+
See :issue:`105686`
10+
11+
Description
12+
===========
13+
14+
Class :php:`TYPO3\CMS\Core\Resource\Driver\DriverInterface`:
15+
16+
.. code-block:: php
17+
18+
public function sanitizeFileName(string $fileName, string $charset = ''): string
19+
20+
has been simplified to:
21+
22+
.. code-block:: php
23+
24+
public function sanitizeFileName(string $fileName): string
25+
26+
Classes implementing the interface no longer need to take care of
27+
a second argument.
28+
29+
Impact
30+
======
31+
32+
This most likely has little to no impact since the main API caller,
33+
the core class :php:`ResourceStorage` never hands over the second
34+
argument. Default implementing class :php:`TYPO3\CMS\Core\Resource\Driver\LocalDriver`
35+
thus always fell back as if handling utf-8 strings.
36+
37+
38+
Affected installations
39+
======================
40+
41+
Projects with instances implementing own FAL drivers using :php:`DriverInterface`
42+
may be affected.
43+
44+
45+
Migration
46+
=========
47+
48+
Implementing classes should drop support for the second argument. It does
49+
not collide with the interface if the second argument is kept, but core
50+
code will never call method :php:`sanitizeFileName()` with handing over
51+
a value for a second argument.
52+
53+
.. index:: FAL, PHP-API, NotScanned, ext:core

Tests/Functional/Resource/Driver/LocalDriverTest.php

Lines changed: 2 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1104,16 +1104,10 @@ public function sanitizeFileNameUTF8Filesystem(string $fileName, string $expecte
11041104
/**
11051105
* Every array splits into:
11061106
* - String value fileName
1107-
* - String value charset (none = '', utf-8, latin1, etc.)
11081107
* - Expected result (cleaned fileName)
11091108
*/
11101109
public static function sanitizeFileNameNonUTF8FilesystemDataProvider(): array
11111110
{
1112-
// Generate string containing all characters for the iso8859-1 charset, charcode greater than 127
1113-
$iso88591GreaterThan127 = '';
1114-
for ($i = 0xA0; $i <= 0xFF; $i++) {
1115-
$iso88591GreaterThan127 .= chr($i);
1116-
}
11171111
// Generate string containing all characters for the utf-8 Latin-1 Supplement (U+0080 to U+00FF)
11181112
// without U+0080 to U+009F: control characters
11191113
// Based on http://www.utf8-chartable.de/unicode-utf8-table.pl
@@ -1134,83 +1128,47 @@ public static function sanitizeFileNameNonUTF8FilesystemDataProvider(): array
11341128
}
11351129

11361130
return [
1137-
// Characters ordered by ASCII table
1138-
'allowed characters iso-8859-1' => [
1139-
'-.0123456789@ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz',
1140-
'iso-8859-1',
1141-
'-.0123456789@ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz',
1142-
],
11431131
// Characters ordered by ASCII table
11441132
'allowed characters utf-8' => [
11451133
'-.0123456789@ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz',
1146-
'utf-8',
11471134
'-.0123456789@ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz',
11481135
],
11491136
// Characters ordered by ASCII table (except for space-character, because space-character ist trimmed)
1150-
'replace special characters with _ (not allowed characters) iso-8859-1' => [
1151-
'! "#$%&\'()*+,/:;<=>?[\\]^`{|}~',
1152-
'iso-8859-1',
1153-
'_____________________________',
1154-
],
1155-
// Characters ordered by ASCII table (except for space-character, because space-character ist trimmed)
11561137
'replace special characters with _ (not allowed characters) utf-8' => [
11571138
'! "#$%&\'()*+,/:;<=>?[\\]^`{|}~',
1158-
'utf-8',
11591139
'_____________________________',
11601140
],
1161-
'iso-8859-1 (code > 127)' => [
1162-
// http://de.wikipedia.org/wiki/ISO_8859-1
1163-
// chr(0xA0) = NBSP (no-break space) => gets trimmed
1164-
$iso88591GreaterThan127,
1165-
'iso-8859-1',
1166-
'_centpound_yen____c_a_____R_____-23_u___1o__1_41_23_4_AAAAAEAAAECEEEEIIIIDNOOOOOExOEUUUUEYTHssaaaaaeaaaeceeeeiiiidnoooooe_oeuuuueythy',
1167-
],
11681141
'utf-8 (Latin-1 Supplement)' => [
11691142
// chr(0xC2) . chr(0x0A) = NBSP (no-break space) => gets trimmed
11701143
$utf8Latin1Supplement,
1171-
'utf-8',
11721144
'_centpound__yen______c_a_______R_______-23__u_____1o__1_41_23_4_AAAAAEAAAECEEEEIIIIDNOOOOOExOEUUUUEYTHssaaaaaeaaaeceeeeiiiidnoooooe_oeuuuueythy',
11731145
],
11741146
'utf-8 (Latin-1 Extended A)' => [
11751147
$utf8Latin1ExtendedA,
1176-
'utf-8',
11771148
'AaAaAaCcCcCcCcDdDdEeEeEeEeEeGgGgGgGgHhHhIiIiIiIiIiIJijJjKk__LlLlLlL_l_LlNnNnNn_n____OOooOoOoOEoeRrRrRrSsSsSsSsTtTtTtUuUuUuUuUuUuWwYyYZzZzZzs',
11781149
],
11791150
'utf-8 but not in NFC (Canonical Composition)' => [
11801151
hex2bin('667275cc88686e65757a6569746c696368656e'),
1181-
'utf-8',
11821152
'fruehneuzeitlichen',
11831153
],
1184-
'trim leading and tailing spaces iso-8859-1' => [
1185-
' test.txt ',
1186-
'iso-8859-1',
1187-
'test.txt',
1188-
],
11891154
'trim leading and tailing spaces utf-8' => [
11901155
' test.txt ',
1191-
'utf-8',
1192-
'test.txt',
1193-
],
1194-
'remove tailing dot iso-8859-1' => [
1195-
'test.txt.',
1196-
'iso-8859-1',
11971156
'test.txt',
11981157
],
11991158
'remove tailing dot utf-8' => [
12001159
'test.txt.',
1201-
'utf-8',
12021160
'test.txt',
12031161
],
12041162
];
12051163
}
12061164

12071165
#[DataProvider('sanitizeFileNameNonUTF8FilesystemDataProvider')]
12081166
#[Test]
1209-
public function sanitizeFileNameNonUTF8Filesystem(string $fileName, string $charset, string $expectedResult): void
1167+
public function sanitizeFileNameNonUTF8Filesystem(string $fileName, string $expectedResult): void
12101168
{
12111169
$GLOBALS['TYPO3_CONF_VARS']['SYS']['UTF8filesystem'] = 0;
12121170
$subject = $this->getDefaultInitializedSubject();
1213-
self::assertEquals($expectedResult, $subject->sanitizeFileName($fileName, $charset));
1171+
self::assertEquals($expectedResult, $subject->sanitizeFileName($fileName));
12141172
}
12151173

12161174
#[Test]

Tests/Unit/Resource/Driver/Fixtures/TestingDriver.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ public function mergeConfigurationCapabilities(Capabilities $capabilities): Capa
5555
throw new \BadMethodCallException('Not implemented', 1691577300);
5656
}
5757

58-
public function sanitizeFileName(string $fileName, string $charset = ''): string
58+
public function sanitizeFileName(string $fileName): string
5959
{
6060
throw new \BadMethodCallException('Not implemented', 1691577304);
6161
}

Tests/Unit/Resource/Driver/Fixtures/TestingHierarchicalFilesystemDriver.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public function mergeConfigurationCapabilities(Capabilities $capabilities): Capa
5454
throw new \BadMethodCallException('Not implemented', 1694348376);
5555
}
5656

57-
public function sanitizeFileName(string $fileName, string $charset = ''): string
57+
public function sanitizeFileName(string $fileName): string
5858
{
5959
throw new \BadMethodCallException('Not implemented', 1694348363);
6060
}

0 commit comments

Comments
 (0)