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