TYPO3 CMS  TYPO3_8-7
FileExtensionFilter.php
Go to the documentation of this file.
1 <?php
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 
22 
27 {
33  protected $allowedFileExtensions = null;
34 
40  protected $disallowedFileExtensions = null;
41 
49  public function filterInlineChildren(array $parameters, DataHandler $dataHandler)
50  {
51  $values = $parameters['values'];
52  if ($parameters['allowedFileExtensions']) {
53  $this->setAllowedFileExtensions($parameters['allowedFileExtensions']);
54  }
55  if ($parameters['disallowedFileExtensions']) {
56  $this->setDisallowedFileExtensions($parameters['disallowedFileExtensions']);
57  }
58  $cleanValues = [];
59  if (is_array($values)) {
60  foreach ($values as $value) {
61  if (empty($value)) {
62  continue;
63  }
64  $parts = GeneralUtility::revExplode('_', $value, 2);
65  $fileReferenceUid = $parts[count($parts) - 1];
66  $fileReference = ResourceFactory::getInstance()->getFileReferenceObject($fileReferenceUid);
67  $file = $fileReference->getOriginalFile();
68  if ($this->isAllowed($file->getExtension())) {
69  $cleanValues[] = $value;
70  } else {
71  // Remove the erroneously created reference record again
72  $dataHandler->deleteAction('sys_file_reference', $fileReferenceUid);
73  }
74  }
75  }
76  return $cleanValues;
77  }
78 
92  public function filterFileList($itemName, $itemIdentifier, $parentIdentifier, array $additionalInformation, DriverInterface $driver)
93  {
94  $returnCode = true;
95  // Early return in case no file filters are set at all
96  if ($this->allowedFileExtensions === null && $this->disallowedFileExtensions === null) {
97  return $returnCode;
98  }
99  // Check that this is a file and not a folder
100  if ($driver->fileExists($itemIdentifier)) {
101  try {
102  $fileInfo = $driver->getFileInfoByIdentifier($itemIdentifier, ['extension']);
103  } catch (\InvalidArgumentException $e) {
104  $fileInfo = [];
105  }
106  if (!isset($fileInfo['extension'])) {
107  $fileInfo['extension'] = PathUtility::pathinfo($itemIdentifier, PATHINFO_EXTENSION);
108  }
109  if (!$this->isAllowed($fileInfo['extension'])) {
110  $returnCode = -1;
111  }
112  }
113  return $returnCode;
114  }
115 
122  protected function isAllowed($fileExt)
123  {
124  $fileExt = strtolower($fileExt);
125  $result = true;
126  // Check allowed file extensions
127  if ($this->allowedFileExtensions !== null && !empty($this->allowedFileExtensions) && !in_array($fileExt, $this->allowedFileExtensions)) {
128  $result = false;
129  }
130  // Check disallowed file extensions
131  if ($this->disallowedFileExtensions !== null && !empty($this->disallowedFileExtensions) && in_array($fileExt, $this->disallowedFileExtensions)) {
132  $result = false;
133  }
134  return $result;
135  }
136 
143  {
144  $this->allowedFileExtensions = $this->convertToLowercaseArray($allowedFileExtensions);
145  }
146 
153  {
154  $this->disallowedFileExtensions = $this->convertToLowercaseArray($disallowedFileExtensions);
155  }
156 
165  protected function convertToLowercaseArray($inputArgument)
166  {
167  $returnValue = null;
168  if (is_array($inputArgument)) {
169  $returnValue = $inputArgument;
170  } elseif ((string)$inputArgument !== '') {
171  $returnValue = GeneralUtility::trimExplode(',', $inputArgument);
172  }
173 
174  if (is_array($returnValue)) {
175  $returnValue = array_map('strtolower', $returnValue);
176  }
177 
178  return $returnValue;
179  }
180 }
static trimExplode($delim, $string, $removeEmptyValues=false, $limit=0)
getFileInfoByIdentifier($fileIdentifier, array $propertiesToExtract=[])
static pathinfo($path, $options=null)
filterInlineChildren(array $parameters, DataHandler $dataHandler)
filterFileList($itemName, $itemIdentifier, $parentIdentifier, array $additionalInformation, DriverInterface $driver)
static revExplode($delimiter, $string, $count=0)