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