TYPO3 CMS  TYPO3_8-7
FileInfo.php
Go to the documentation of this file.
1 <?php
3 
4 /*
5  * This file is part of the TYPO3 CMS project.
6  *
7  * It is free software; you can redistribute it and/or modify it under
8  * the terms of the GNU General Public License, either version 2
9  * of the License, or any later version.
10  *
11  * For the full copyright and license information, please read the
12  * LICENSE.txt file that was distributed with this source code.
13  *
14  * The TYPO3 project - inspiring people to share!
15  */
16 
19 
23 class FileInfo extends \SplFileInfo implements TypeInterface
24 {
33  public function getMimeType()
34  {
35  $mimeType = false;
36  if ($this->isFile()) {
37  $fileExtensionToMimeTypeMapping = $GLOBALS['TYPO3_CONF_VARS']['SYS']['FileInfo']['fileExtensionToMimeType'];
38  $lowercaseFileExtension = strtolower($this->getExtension());
39  if (!empty($fileExtensionToMimeTypeMapping[$lowercaseFileExtension])) {
40  $mimeType = $fileExtensionToMimeTypeMapping[$lowercaseFileExtension];
41  } else {
42  if (function_exists('finfo_file')) {
43  $fileInfo = new \finfo();
44  $mimeType = $fileInfo->file($this->getPathname(), FILEINFO_MIME_TYPE);
45  } elseif (function_exists('mime_content_type')) {
46  $mimeType = mime_content_type($this->getPathname());
47  }
48  }
49  }
50 
51  if (isset($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS'][\TYPO3\CMS\Core\Type\File\FileInfo::class]['mimeTypeGuessers'])
52  && is_array($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS'][\TYPO3\CMS\Core\Type\File\FileInfo::class]['mimeTypeGuessers'])
53  ) {
54  foreach ($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS'][\TYPO3\CMS\Core\Type\File\FileInfo::class]['mimeTypeGuessers'] as $mimeTypeGuesser) {
55  $hookParameters = [
56  'mimeType' => &$mimeType
57  ];
58 
60  $mimeTypeGuesser,
61  $hookParameters,
62  $this
63  );
64  }
65  }
66 
67  return $mimeType;
68  }
69 
81  public function getMimeExtensions(): array
82  {
83  $mimeExtensions = [];
84  if ($this->isFile()) {
85  $fileExtensionToMimeTypeMapping = $GLOBALS['TYPO3_CONF_VARS']['SYS']['FileInfo']['fileExtensionToMimeType'];
86  $mimeType = $this->getMimeType();
87  if (in_array($mimeType, $fileExtensionToMimeTypeMapping, true)) {
88  $mimeExtensions = array_keys($fileExtensionToMimeTypeMapping, $mimeType, true);
89  // extraction using magic.mime database with FILEINFO_EXTENSION was introduced in PHP 7.2.0
90  } elseif (function_exists('finfo_file') && defined('FILEINFO_EXTENSION')) {
91  $fileInfo = new \finfo();
92  $mimeExtensions = array_filter(
94  '/',
95  (string)$fileInfo->file($this->getPathname(), FILEINFO_EXTENSION)
96  ),
97  function ($item) {
98  // filter invalid items ('???' is used if not found in magic.mime database)
99  return $item !== '' && $item !== '???';
100  }
101  );
102  }
103  }
104  return $mimeExtensions;
105  }
106 }
static callUserFunction($funcName, &$params, &$ref, $_='', $errorMode=0)
static trimExplode($delim, $string, $removeEmptyValues=false, $limit=0)
if(TYPO3_MODE==='BE') $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_tsfebeuserauth.php']['frontendEditingController']['default']