‪TYPO3CMS  10.4
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  const ‪UNSAFE_FILENAME_CHARACTER_EXPRESSION = '\\x00-\\x2C\\/\\x3A-\\x3F\\x5B-\\x60\\x7B-\\xBF';
40 
46  public ‪$maxNumber = 99;
47 
53  public ‪$uniquePrecision = 6;
54 
55  /**********************************
56  *
57  * Checking functions
58  *
59  **********************************/
60 
67  public function ‪setFileExtensionPermissions($allowedFilePermissions, $deniedFilePermissions)
68  {
69  trigger_error('BasicFileUtility->setFileExtensionPermissions() serves no purpose anymore. Any usages can be removed, as the FAL API is handling file permissions.', E_USER_DEPRECATED);
70  }
71 
79  protected function ‪sanitizeFolderPath($theDir)
80  {
81  if (!GeneralUtility::validPathStr($theDir)) {
82  return false;
83  }
84  $theDir = ‪PathUtility::getCanonicalPath($theDir);
85  if (@is_dir($theDir)) {
86  return $theDir;
87  }
88  return false;
89  }
90 
103  public function ‪getUniqueName($theFile, $theDest, $dontCheckForUnique = false)
104  {
105  // $theDest is cleaned up
106  $theDest = $this->‪sanitizeFolderPath($theDest);
107  if ($theDest) {
108  // Fetches info about path, name, extension of $theFile
109  $origFileInfo = GeneralUtility::split_fileref($theFile);
110  // Check if the file exists and if not - return the filename...
111  $fileInfo = $origFileInfo;
112  $theDestFile = $theDest . '/' . $fileInfo['file'];
113  // The destinations file
114  if (!file_exists($theDestFile) || $dontCheckForUnique) {
115  // If the file does NOT exist we return this filename
116  return $theDestFile;
117  }
118  // 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...
119  $theTempFileBody = preg_replace('/_[0-9][0-9]$/', '', $origFileInfo['filebody']);
120  // This removes _xx if appended to the file
121  $theOrigExt = $origFileInfo['realFileext'] ? '.' . $origFileInfo['realFileext'] : '';
122  for ($a = 1; $a <= $this->maxNumber + 1; $a++) {
123  if ($a <= $this->maxNumber) {
124  // First we try to append numbers
125  $insert = '_' . sprintf('%02d', $a);
126  } else {
127  // .. then we try unique-strings...
128  $insert = '_' . substr(md5(‪StringUtility::getUniqueId()), 0, $this->uniquePrecision);
129  }
130  $theTestFile = $theTempFileBody . $insert . $theOrigExt;
131  $theDestFile = $theDest . '/' . $theTestFile;
132  // The destinations file
133  if (!file_exists($theDestFile)) {
134  // If the file does NOT exist we return this filename
135  return $theDestFile;
136  }
137  }
138  }
139 
140  return null;
141  }
142 
143  /*********************
144  *
145  * Cleaning functions
146  *
147  *********************/
148 
157  public function ‪cleanFileName($fileName)
158  {
159  // Handle UTF-8 characters
160  if (‪$GLOBALS['TYPO3_CONF_VARS']['SYS']['UTF8filesystem']) {
161  // allow ".", "-", 0-9, a-z, A-Z and everything beyond U+C0 (latin capital letter a with grave)
162  $cleanFileName = preg_replace('/[' . self::UNSAFE_FILENAME_CHARACTER_EXPRESSION . ']/u', '_', trim($fileName)) ?? '';
163  } else {
164  $fileName = GeneralUtility::makeInstance(CharsetConverter::class)->utf8_char_mapping($fileName);
165  // Replace unwanted characters by underscores
166  $cleanFileName = preg_replace('/[' . self::UNSAFE_FILENAME_CHARACTER_EXPRESSION . '\\xC0-\\xFF]/', '_', trim($fileName)) ?? '';
167  }
168  // Strip trailing dots and return
169  return rtrim($cleanFileName, '.');
170  }
171 }
‪TYPO3\CMS\Core\Utility\PathUtility
Definition: PathUtility.php:24
‪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:307
‪TYPO3\CMS\Core\Utility\File\BasicFileUtility\setFileExtensionPermissions
‪setFileExtensionPermissions($allowedFilePermissions, $deniedFilePermissions)
Definition: BasicFileUtility.php:65
‪TYPO3\CMS\Core\Utility\File\BasicFileUtility\cleanFileName
‪string cleanFileName($fileName)
Definition: BasicFileUtility.php:155
‪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:101
‪TYPO3\CMS\Core\Utility\StringUtility\getUniqueId
‪static string getUniqueId($prefix='')
Definition: StringUtility.php:92
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:5
‪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:77
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:46
‪TYPO3\CMS\Core\Utility\File\BasicFileUtility\$uniquePrecision
‪int $uniquePrecision
Definition: BasicFileUtility.php:51
‪TYPO3\CMS\Core\Utility\StringUtility
Definition: StringUtility.php:22