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