[BUGFIX] Fix several typos in php comments
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Classes / Type / File / FileInfo.php
1 <?php
2 namespace TYPO3\CMS\Core\Type\File;
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
17 use TYPO3\CMS\Core\Type\TypeInterface;
18 use TYPO3\CMS\Core\Utility\GeneralUtility;
19
20 /**
21 * A SPL FileInfo class providing general information related to a file.
22 */
23 class FileInfo extends \SplFileInfo implements TypeInterface
24 {
25 /**
26 * Return the mime type of a file.
27 *
28 * TYPO3 specific settings in $GLOBALS['TYPO3_CONF_VARS']['SYS']['FileInfo']['fileExtensionToMimeType'] take
29 * precedence over native resolving.
30 *
31 * @return string|bool Returns the mime type or FALSE if the mime type could not be discovered
32 */
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 foreach ($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS'][\TYPO3\CMS\Core\Type\File\FileInfo::class]['mimeTypeGuessers'] ?? [] as $mimeTypeGuesser) {
52 $hookParameters = [
53 'mimeType' => &$mimeType
54 ];
55
56 GeneralUtility::callUserFunction(
57 $mimeTypeGuesser,
58 $hookParameters,
59 $this
60 );
61 }
62
63 return $mimeType;
64 }
65
66 /**
67 * Returns the file extensions appropriate for a the MIME type detected in the file. For types that commonly have
68 * multiple file extensions, such as JPEG images, then the return value is multiple extensions, for instance that
69 * could be ['jpeg', 'jpg', 'jpe', 'jiff']. For unknown types not available in the magic.mime database
70 * (/etc/magic.mime, /etc/mime.types, ...), then return value is an empty array.
71 *
72 * TYPO3 specific settings in $GLOBALS['TYPO3_CONF_VARS']['SYS']['FileInfo']['fileExtensionToMimeType'] take
73 * precedence over native resolving.
74 *
75 * @return string[]
76 */
77 public function getMimeExtensions(): array
78 {
79 $mimeExtensions = [];
80 if ($this->isFile()) {
81 $fileExtensionToMimeTypeMapping = $GLOBALS['TYPO3_CONF_VARS']['SYS']['FileInfo']['fileExtensionToMimeType'];
82 $mimeType = $this->getMimeType();
83 if (in_array($mimeType, $fileExtensionToMimeTypeMapping, true)) {
84 $mimeExtensions = array_keys($fileExtensionToMimeTypeMapping, $mimeType, true);
85 } elseif (function_exists('finfo_file')) {
86 $fileInfo = new \finfo();
87 $mimeExtensions = array_filter(
88 GeneralUtility::trimExplode(
89 '/',
90 (string)$fileInfo->file($this->getPathname(), FILEINFO_EXTENSION)
91 ),
92 function ($item) {
93 // filter invalid items ('???' is used if not found in magic.mime database)
94 return $item !== '' && $item !== '???';
95 }
96 );
97 }
98 }
99 return $mimeExtensions;
100 }
101 }