[BUGFIX] Make FileExtensionFilter case insensitive
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Classes / Resource / Filter / FileExtensionFilter.php
1 <?php
2 namespace TYPO3\CMS\Core\Resource\Filter;
3
4 /***************************************************************
5 * Copyright notice
6 *
7 * (c) 2012-2013 Ingmar Schlecht <ingmar.schlecht@typo3.org>
8 * All rights reserved
9 *
10 * This script is part of the TYPO3 project. The TYPO3 project is
11 * free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * The GNU General Public License can be found at
17 * http://www.gnu.org/copyleft/gpl.html.
18 * A copy is found in the text file GPL.txt and important notices to the license
19 * from the author is found in LICENSE.txt distributed with these scripts.
20 *
21 *
22 * This script is distributed in the hope that it will be useful,
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 * GNU General Public License for more details.
26 *
27 * This copyright notice MUST APPEAR in all copies of the script!
28 ***************************************************************/
29 /**
30 * Utility methods for filtering filenames
31 *
32 * @author Ingmar Schlecht <ingmar.schlecht@typo3.org>
33 */
34 class FileExtensionFilter {
35
36 /**
37 * Allowed file extensions. If NULL, all extensions are allowed.
38 *
39 * @var array
40 */
41 protected $allowedFileExtensions = NULL;
42
43 /**
44 * Disallowed file extensions. If NULL, no extension is disallowed (i.e. all are allowed).
45 *
46 * @var array
47 */
48 protected $disallowedFileExtensions = NULL;
49
50 /**
51 * Entry method for use as TCEMain "inline" field filter
52 *
53 * @param array $parameters
54 * @param \TYPO3\CMS\Core\DataHandling\DataHandler $tceMain
55 * @return array
56 */
57 public function filterInlineChildren(array $parameters, \TYPO3\CMS\Core\DataHandling\DataHandler $tceMain) {
58 $values = $parameters['values'];
59 if ($parameters['allowedFileExtensions']) {
60 $this->setAllowedFileExtensions($parameters['allowedFileExtensions']);
61 }
62 if ($parameters['disallowedFileExtensions']) {
63 $this->setDisallowedFileExtensions($parameters['disallowedFileExtensions']);
64 }
65 $cleanValues = array();
66 if (is_array($values)) {
67 foreach ($values as $value) {
68 if (empty($value)) {
69 continue;
70 }
71 $parts = \TYPO3\CMS\Core\Utility\GeneralUtility::revExplode('_', $value, 2);
72 $fileReferenceUid = $parts[count($parts) - 1];
73 $fileReference = \TYPO3\CMS\Core\Resource\ResourceFactory::getInstance()->getFileReferenceObject($fileReferenceUid);
74 $file = $fileReference->getOriginalFile();
75 if ($this->isAllowed($file->getIdentifier())) {
76 $cleanValues[] = $value;
77 } else {
78 // Remove the erroneously created reference record again
79 $tceMain->deleteAction('sys_file_reference', $fileReferenceUid);
80 }
81 }
82 }
83 return $cleanValues;
84 }
85
86 /**
87 * Entry method for use as file list filter.
88 *
89 * We have to use -1 as the „don't include“ return value, as call_user_func() will return FALSE
90 * if calling the method failed and thus we can't use that as a return value.
91 *
92 * @param string $itemName
93 * @param string $itemIdentifier
94 * @param string $parentIdentifier
95 * @param array $additionalInformation Additional information about the inspected item
96 * @param \TYPO3\CMS\Core\Resource\Driver\DriverInterface $driver
97 * @return boolean|integer -1 if the file should not be included in a listing
98 */
99 public function filterFileList($itemName, $itemIdentifier, $parentIdentifier, array $additionalInformation, \TYPO3\CMS\Core\Resource\Driver\DriverInterface $driver) {
100 $returnCode = TRUE;
101 // Early return in case no file filters are set at all
102 if ($this->allowedFileExtensions === NULL && $this->disallowedFileExtensions === NULL) {
103 return $returnCode;
104 }
105 // Check that this is a file and not a folder
106 if ($driver->fileExists($itemIdentifier)) {
107 if (!$this->isAllowed($itemName)) {
108 $returnCode = -1;
109 }
110 }
111 return $returnCode;
112 }
113
114 /**
115 * Checks whether a file is allowed according to the criteria defined in the class variables ($this->allowedFileExtensions etc.)
116 *
117 * @param \TYPO3\CMS\Core\Resource\FileInterface $file
118 * @return boolean
119 */
120 protected function isAllowed($fileName) {
121 $result = TRUE;
122 $fileExt = strtolower(pathinfo($fileName, PATHINFO_EXTENSION));
123 // Check allowed file extensions
124 if ($this->allowedFileExtensions !== NULL && count($this->allowedFileExtensions) > 0 && !in_array($fileExt, $this->allowedFileExtensions)) {
125 $result = FALSE;
126 }
127 // Check disallowed file extensions
128 if ($this->disallowedFileExtensions !== NULL && count($this->disallowedFileExtensions) > 0 && in_array($fileExt, $this->disallowedFileExtensions)) {
129 $result = FALSE;
130 }
131 return $result;
132 }
133
134 /**
135 * Set allowed file extensions
136 *
137 * @param mixed $allowedFileExtensions Comma-separated list or array, of allowed file extensions
138 */
139 public function setAllowedFileExtensions($allowedFileExtensions) {
140 $this->allowedFileExtensions = $this->convertToLowercaseArray($allowedFileExtensions);
141 }
142
143 /**
144 * Set disallowed file extensions
145 *
146 * @param mixed $disallowedFileExtensions Comma-separated list or array, of allowed file extensions
147 */
148 public function setDisallowedFileExtensions($disallowedFileExtensions) {
149 $this->disallowedFileExtensions = $this->convertToLowercaseArray($disallowedFileExtensions);
150 }
151
152 /**
153 * Converts mixed (string or array) input arguments into an array, NULL if empty.
154 *
155 * All array values will be converted to lower case.
156 *
157 * @param mixed $inputArgument Comma-separated list or array.
158 * @return array
159 */
160 protected function convertToLowercaseArray($inputArgument) {
161 $returnValue = NULL;
162 if (is_array($inputArgument)) {
163 $returnValue = $inputArgument;
164 } elseif (strlen($inputArgument) > 0) {
165 $returnValue = \TYPO3\CMS\Core\Utility\GeneralUtility::trimExplode(',', $inputArgument);
166 }
167
168 if (is_array($returnValue)) {
169 $returnValue = array_map('strtolower', $returnValue);
170 }
171
172 return $returnValue;
173 }
174
175 }