49e94a33251e1c8c28b4f41d1ac987ce36197b68
[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\DataHandler\DataHandler $tceMain
57 * @return array
58 */
59 public function filterInlineChildren(array $parameters, \TYPO3\CMS\Core\DataHandler\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 foreach ($values as $value) {
69 if (empty($value)) {
70 continue;
71 }
72 $parts = \TYPO3\CMS\Core\Utility\GeneralUtility::revExplode('_', $value, 2);
73 $fileReferenceUid = $parts[count($parts) - 1];
74 $fileReference = \TYPO3\CMS\Core\Resource\ResourceFactory::getInstance()->getFileReferenceObject($fileReferenceUid);
75 $file = $fileReference->getOriginalFile();
76 if ($this->isAllowed($file)) {
77 $cleanValues[] = $value;
78 } else {
79 // Remove the erroneously created reference record again
80 $tceMain->deleteAction('sys_file_reference', $fileReferenceUid);
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\AbstractDriver $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\AbstractDriver $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 $file = $driver->getFile($itemIdentifier);
108 if (!$this->isAllowed($file)) {
109 $returnCode = -1;
110 }
111 }
112 return $returnCode;
113 }
114
115 /**
116 * Checks whether a file is allowed according to the criteria defined in the class variables ($this->allowedFileExtensions etc.)
117 *
118 * @param \TYPO3\CMS\Core\Resource\FileInterface $file
119 * @return boolean
120 */
121 protected function isAllowed(\TYPO3\CMS\Core\Resource\FileInterface $file) {
122 $result = TRUE;
123 $fileExt = $file->getExtension();
124 // Check allowed file extensions
125 if (($this->allowedFileExtensions !== NULL && count($this->allowedFileExtensions) > 0) && !in_array($fileExt, $this->allowedFileExtensions)) {
126 $result = FALSE;
127 }
128 // Check disallowed file extensions
129 if (($this->disallowedFileExtensions !== NULL && count($this->disallowedFileExtensions) > 0) && in_array($fileExt, $this->disallowedFileExtensions)) {
130 $result = FALSE;
131 }
132 return $result;
133 }
134
135 /**
136 * Set allowed file extensions
137 *
138 * @param mixed $allowedFileExtensions Comma-separated list or array, of allowed file extensions
139 */
140 public function setAllowedFileExtensions($allowedFileExtensions) {
141 $this->allowedFileExtensions = $this->convertToArray($allowedFileExtensions);
142 }
143
144 /**
145 * Set disallowed file extensions
146 *
147 * @param mixed $disallowedFileExtensions Comma-separated list or array, of allowed file extensions
148 */
149 public function setDisallowedFileExtensions($disallowedFileExtensions) {
150 $this->disallowedFileExtensions = $this->convertToArray($disallowedFileExtensions);
151 }
152
153 /**
154 * Converts mixed (string or array) input arguments into an array, NULL if empty.
155 *
156 * @param mixed $inputArgument Comma-separated list or array.
157 * @return array
158 */
159 protected function convertToArray($inputArgument) {
160 $returnValue = NULL;
161 if (is_array($inputArgument)) {
162 $returnValue = $inputArgument;
163 } elseif (strlen($inputArgument) > 0) {
164 $returnValue = \TYPO3\CMS\Core\Utility\GeneralUtility::trimExplode(',', $inputArgument);
165 }
166 return $returnValue;
167 }
168
169 }
170
171
172 ?>