[CLEANUP] FAL code
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Classes / Resource / Filter / FileExtensionFilter.php
1 <?php
2 namespace TYPO3\CMS\Core\Resource\Filter;
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\DataHandling\DataHandler;
18 use TYPO3\CMS\Core\Resource\Driver\DriverInterface;
19 use TYPO3\CMS\Core\Resource\ResourceFactory;
20 use TYPO3\CMS\Core\Utility\GeneralUtility;
21
22 /**
23 * Utility methods for filtering filenames
24 */
25 class FileExtensionFilter
26 {
27 /**
28 * Allowed file extensions. If NULL, all extensions are allowed.
29 *
30 * @var array
31 */
32 protected $allowedFileExtensions = null;
33
34 /**
35 * Disallowed file extensions. If NULL, no extension is disallowed (i.e. all are allowed).
36 *
37 * @var array
38 */
39 protected $disallowedFileExtensions = null;
40
41 /**
42 * Entry method for use as TCEMain "inline" field filter
43 *
44 * @param array $parameters
45 * @param DataHandler $tceMain
46 * @return array
47 */
48 public function filterInlineChildren(array $parameters, DataHandler $tceMain)
49 {
50 $values = $parameters['values'];
51 if ($parameters['allowedFileExtensions']) {
52 $this->setAllowedFileExtensions($parameters['allowedFileExtensions']);
53 }
54 if ($parameters['disallowedFileExtensions']) {
55 $this->setDisallowedFileExtensions($parameters['disallowedFileExtensions']);
56 }
57 $cleanValues = array();
58 if (is_array($values)) {
59 foreach ($values as $value) {
60 if (empty($value)) {
61 continue;
62 }
63 $parts = GeneralUtility::revExplode('_', $value, 2);
64 $fileReferenceUid = $parts[count($parts) - 1];
65 $fileReference = ResourceFactory::getInstance()->getFileReferenceObject($fileReferenceUid);
66 $file = $fileReference->getOriginalFile();
67 if ($this->isAllowed($file->getName())) {
68 $cleanValues[] = $value;
69 } else {
70 // Remove the erroneously created reference record again
71 $tceMain->deleteAction('sys_file_reference', $fileReferenceUid);
72 }
73 }
74 }
75 return $cleanValues;
76 }
77
78 /**
79 * Entry method for use as filelist filter.
80 *
81 * We have to use -1 as the „don't include“ return value, as call_user_func() will return FALSE
82 * if calling the method failed and thus we can't use that as a return value.
83 *
84 * @param string $itemName
85 * @param string $itemIdentifier
86 * @param string $parentIdentifier
87 * @param array $additionalInformation Additional information about the inspected item
88 * @param DriverInterface $driver
89 * @return bool|int -1 if the file should not be included in a listing
90 */
91 public function filterFileList($itemName, $itemIdentifier, $parentIdentifier, array $additionalInformation, DriverInterface $driver)
92 {
93 $returnCode = true;
94 // Early return in case no file filters are set at all
95 if ($this->allowedFileExtensions === null && $this->disallowedFileExtensions === null) {
96 return $returnCode;
97 }
98 // Check that this is a file and not a folder
99 if ($driver->fileExists($itemIdentifier)) {
100 if (!$this->isAllowed($itemName)) {
101 $returnCode = -1;
102 }
103 }
104 return $returnCode;
105 }
106
107 /**
108 * Checks whether a file is allowed according to the criteria defined in the class variables ($this->allowedFileExtensions etc.)
109 *
110 * @param string $fileName
111 * @return bool
112 */
113 protected function isAllowed($fileName)
114 {
115 $result = true;
116 $fileExt = strtolower(pathinfo($fileName, PATHINFO_EXTENSION));
117 // Check allowed file extensions
118 if ($this->allowedFileExtensions !== null && !empty($this->allowedFileExtensions) && !in_array($fileExt, $this->allowedFileExtensions)) {
119 $result = false;
120 }
121 // Check disallowed file extensions
122 if ($this->disallowedFileExtensions !== null && !empty($this->disallowedFileExtensions) && in_array($fileExt, $this->disallowedFileExtensions)) {
123 $result = false;
124 }
125 return $result;
126 }
127
128 /**
129 * Set allowed file extensions
130 *
131 * @param mixed $allowedFileExtensions Comma-separated list or array, of allowed file extensions
132 */
133 public function setAllowedFileExtensions($allowedFileExtensions)
134 {
135 $this->allowedFileExtensions = $this->convertToLowercaseArray($allowedFileExtensions);
136 }
137
138 /**
139 * Set disallowed file extensions
140 *
141 * @param mixed $disallowedFileExtensions Comma-separated list or array, of allowed file extensions
142 */
143 public function setDisallowedFileExtensions($disallowedFileExtensions)
144 {
145 $this->disallowedFileExtensions = $this->convertToLowercaseArray($disallowedFileExtensions);
146 }
147
148 /**
149 * Converts mixed (string or array) input arguments into an array, NULL if empty.
150 *
151 * All array values will be converted to lower case.
152 *
153 * @param mixed $inputArgument Comma-separated list or array.
154 * @return array
155 */
156 protected function convertToLowercaseArray($inputArgument)
157 {
158 $returnValue = null;
159 if (is_array($inputArgument)) {
160 $returnValue = $inputArgument;
161 } elseif ((string)$inputArgument !== '') {
162 $returnValue = GeneralUtility::trimExplode(',', $inputArgument);
163 }
164
165 if (is_array($returnValue)) {
166 $returnValue = array_map('strtolower', $returnValue);
167 }
168
169 return $returnValue;
170 }
171 }