‪TYPO3CMS  11.5
BasicFileUtility.php
Go to the documentation of this file.
1 <?php
2 
3 /*
4  * This file is part of the TYPO3 CMS project.
5  *
6  * It is free software; you can redistribute it and/or modify it under
7  * the terms of the GNU General Public License, either version 2
8  * of the License, or any later version.
9  *
10  * For the full copyright and license information, please read the
11  * LICENSE.txt file that was distributed with this source code.
12  *
13  * The TYPO3 project - inspiring people to share!
14  */
15 
17 
22 
35 {
39  public const ‪UNSAFE_FILENAME_CHARACTER_EXPRESSION = '\\x00-\\x2C\\/\\x3A-\\x3F\\x5B-\\x60\\x7B-\\xBF';
40 
46  public ‪$maxNumber = 99;
47 
53  public ‪$uniquePrecision = 6;
54 
62  protected function ‪sanitizeFolderPath($theDir)
63  {
64  if (!GeneralUtility::validPathStr($theDir)) {
65  return false;
66  }
67  $theDir = ‪PathUtility::getCanonicalPath($theDir);
68  if (@is_dir($theDir)) {
69  return $theDir;
70  }
71  return false;
72  }
73 
86  public function ‪getUniqueName($theFile, $theDest, $dontCheckForUnique = false)
87  {
88  // $theDest is cleaned up
89  $theDest = $this->‪sanitizeFolderPath($theDest);
90  if ($theDest) {
91  // Fetches info about path, name, extension of $theFile
92  $origFileInfo = GeneralUtility::split_fileref($theFile);
93  // Check if the file exists and if not - return the filename...
94  $fileInfo = $origFileInfo;
95  $theDestFile = $theDest . '/' . $fileInfo['file'];
96  // The destinations file
97  if (!file_exists($theDestFile) || $dontCheckForUnique) {
98  // If the file does NOT exist we return this filename
99  return $theDestFile;
100  }
101  // Well the filename in its pure form existed. Now we try to append numbers / unique-strings and see if we can find an available filename...
102  $theTempFileBody = preg_replace('/_[0-9][0-9]$/', '', $origFileInfo['filebody']);
103  // This removes _xx if appended to the file
104  $theOrigExt = $origFileInfo['realFileext'] ? '.' . $origFileInfo['realFileext'] : '';
105  for ($a = 1; $a <= $this->maxNumber + 1; $a++) {
106  if ($a <= $this->maxNumber) {
107  // First we try to append numbers
108  $insert = '_' . sprintf('%02d', $a);
109  } else {
110  // .. then we try unique-strings...
111  $insert = '_' . substr(md5(‪StringUtility::getUniqueId()), 0, $this->uniquePrecision);
112  }
113  $theTestFile = $theTempFileBody . $insert . $theOrigExt;
114  $theDestFile = $theDest . '/' . $theTestFile;
115  // The destinations file
116  if (!file_exists($theDestFile)) {
117  // If the file does NOT exist we return this filename
118  return $theDestFile;
119  }
120  }
121  }
122 
123  return null;
124  }
125 
134  public function ‪cleanFileName($fileName)
135  {
136  // Handle UTF-8 characters
137  if (‪$GLOBALS['TYPO3_CONF_VARS']['SYS']['UTF8filesystem']) {
138  // allow ".", "-", 0-9, a-z, A-Z and everything beyond U+C0 (latin capital letter a with grave)
139  $cleanFileName = preg_replace('/[' . self::UNSAFE_FILENAME_CHARACTER_EXPRESSION . ']/u', '_', trim($fileName)) ?? '';
140  } else {
141  $fileName = GeneralUtility::makeInstance(CharsetConverter::class)->utf8_char_mapping($fileName);
142  // Replace unwanted characters by underscores
143  $cleanFileName = preg_replace('/[' . self::UNSAFE_FILENAME_CHARACTER_EXPRESSION . '\\xC0-\\xFF]/', '_', trim($fileName)) ?? '';
144  }
145  // Strip trailing dots and return
146  return rtrim($cleanFileName, '.');
147  }
148 }
‪TYPO3\CMS\Core\Utility\PathUtility
Definition: PathUtility.php:25
‪TYPO3\CMS\Core\Utility\File\BasicFileUtility
Definition: BasicFileUtility.php:35
‪TYPO3\CMS\Core\Charset\CharsetConverter
Definition: CharsetConverter.php:54
‪TYPO3\CMS\Core\Utility\PathUtility\getCanonicalPath
‪static string getCanonicalPath($path)
Definition: PathUtility.php:380
‪TYPO3\CMS\Core\Utility\File\BasicFileUtility\cleanFileName
‪string cleanFileName($fileName)
Definition: BasicFileUtility.php:132
‪TYPO3\CMS\Core\Utility\File\BasicFileUtility\UNSAFE_FILENAME_CHARACTER_EXPRESSION
‪const UNSAFE_FILENAME_CHARACTER_EXPRESSION
Definition: BasicFileUtility.php:39
‪TYPO3\CMS\Core\Utility\File
Definition: BasicFileUtility.php:16
‪TYPO3\CMS\Core\Utility\File\BasicFileUtility\getUniqueName
‪string null getUniqueName($theFile, $theDest, $dontCheckForUnique=false)
Definition: BasicFileUtility.php:84
‪TYPO3\CMS\Core\Utility\StringUtility\getUniqueId
‪static string getUniqueId($prefix='')
Definition: StringUtility.php:128
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:25
‪TYPO3\CMS\Core\Utility\File\BasicFileUtility\$maxNumber
‪int $maxNumber
Definition: BasicFileUtility.php:45
‪TYPO3\CMS\Core\Utility\File\BasicFileUtility\sanitizeFolderPath
‪bool string sanitizeFolderPath($theDir)
Definition: BasicFileUtility.php:60
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:50
‪TYPO3\CMS\Core\Utility\File\BasicFileUtility\$uniquePrecision
‪int $uniquePrecision
Definition: BasicFileUtility.php:51
‪TYPO3\CMS\Core\Utility\StringUtility
Definition: StringUtility.php:22