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