TYPO3  7.6
FileExtensionFilter.php
Go to the documentation of this file.
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 
21 {
27  protected $allowedFileExtensions = null;
28 
34  protected $disallowedFileExtensions = null;
35 
43  public function filterInlineChildren(array $parameters, \TYPO3\CMS\Core\DataHandling\DataHandler $tceMain)
44  {
45  $values = $parameters['values'];
46  if ($parameters['allowedFileExtensions']) {
47  $this->setAllowedFileExtensions($parameters['allowedFileExtensions']);
48  }
49  if ($parameters['disallowedFileExtensions']) {
50  $this->setDisallowedFileExtensions($parameters['disallowedFileExtensions']);
51  }
52  $cleanValues = array();
53  if (is_array($values)) {
54  foreach ($values as $value) {
55  if (empty($value)) {
56  continue;
57  }
58  $parts = \TYPO3\CMS\Core\Utility\GeneralUtility::revExplode('_', $value, 2);
59  $fileReferenceUid = $parts[count($parts) - 1];
60  $fileReference = \TYPO3\CMS\Core\Resource\ResourceFactory::getInstance()->getFileReferenceObject($fileReferenceUid);
61  $file = $fileReference->getOriginalFile();
62  if ($this->isAllowed($file->getName())) {
63  $cleanValues[] = $value;
64  } else {
65  // Remove the erroneously created reference record again
66  $tceMain->deleteAction('sys_file_reference', $fileReferenceUid);
67  }
68  }
69  }
70  return $cleanValues;
71  }
72 
86  public function filterFileList($itemName, $itemIdentifier, $parentIdentifier, array $additionalInformation, \TYPO3\CMS\Core\Resource\Driver\DriverInterface $driver)
87  {
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 
108  protected function isAllowed($fileName)
109  {
110  $result = true;
111  $fileExt = strtolower(pathinfo($fileName, PATHINFO_EXTENSION));
112  // Check allowed file extensions
113  if ($this->allowedFileExtensions !== null && !empty($this->allowedFileExtensions) && !in_array($fileExt, $this->allowedFileExtensions)) {
114  $result = false;
115  }
116  // Check disallowed file extensions
117  if ($this->disallowedFileExtensions !== null && !empty($this->disallowedFileExtensions) && in_array($fileExt, $this->disallowedFileExtensions)) {
118  $result = false;
119  }
120  return $result;
121  }
122 
129  {
130  $this->allowedFileExtensions = $this->convertToLowercaseArray($allowedFileExtensions);
131  }
132 
139  {
140  $this->disallowedFileExtensions = $this->convertToLowercaseArray($disallowedFileExtensions);
141  }
142 
151  protected function convertToLowercaseArray($inputArgument)
152  {
153  $returnValue = null;
154  if (is_array($inputArgument)) {
155  $returnValue = $inputArgument;
156  } elseif ((string)$inputArgument !== '') {
157  $returnValue = \TYPO3\CMS\Core\Utility\GeneralUtility::trimExplode(',', $inputArgument);
158  }
159 
160  if (is_array($returnValue)) {
161  $returnValue = array_map('strtolower', $returnValue);
162  }
163 
164  return $returnValue;
165  }
166 }