TYPO3 CMS  TYPO3_6-2
FileExtensionFilter.php
Go to the documentation of this file.
1 <?php
3 
22 
28  protected $allowedFileExtensions = NULL;
29 
35  protected $disallowedFileExtensions = NULL;
36 
44  public function filterInlineChildren(array $parameters, \TYPO3\CMS\Core\DataHandling\DataHandler $tceMain) {
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  }
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  $returnCode = TRUE;
88  // Early return in case no file filters are set at all
89  if ($this->allowedFileExtensions === NULL && $this->disallowedFileExtensions === NULL) {
90  return $returnCode;
91  }
92  // Check that this is a file and not a folder
93  if ($driver->fileExists($itemIdentifier)) {
94  if (!$this->isAllowed($itemName)) {
95  $returnCode = -1;
96  }
97  }
98  return $returnCode;
99  }
100 
107  protected function isAllowed($fileName) {
108  $result = TRUE;
109  $fileExt = strtolower(pathinfo($fileName, PATHINFO_EXTENSION));
110  // Check allowed file extensions
111  if ($this->allowedFileExtensions !== NULL && count($this->allowedFileExtensions) > 0 && !in_array($fileExt, $this->allowedFileExtensions)) {
112  $result = FALSE;
113  }
114  // Check disallowed file extensions
115  if ($this->disallowedFileExtensions !== NULL && count($this->disallowedFileExtensions) > 0 && in_array($fileExt, $this->disallowedFileExtensions)) {
116  $result = FALSE;
117  }
118  return $result;
119  }
120 
127  $this->allowedFileExtensions = $this->convertToLowercaseArray($allowedFileExtensions);
128  }
129 
136  $this->disallowedFileExtensions = $this->convertToLowercaseArray($disallowedFileExtensions);
137  }
138 
147  protected function convertToLowercaseArray($inputArgument) {
148  $returnValue = NULL;
149  if (is_array($inputArgument)) {
150  $returnValue = $inputArgument;
151  } elseif (strlen($inputArgument) > 0) {
152  $returnValue = \TYPO3\CMS\Core\Utility\GeneralUtility::trimExplode(',', $inputArgument);
153  }
154 
155  if (is_array($returnValue)) {
156  $returnValue = array_map('strtolower', $returnValue);
157  }
158 
159  return $returnValue;
160  }
161 
162 }
$driver
Definition: server.php:34
$parameters
Definition: FileDumpEID.php:15
filterInlineChildren(array $parameters, \TYPO3\CMS\Core\DataHandling\DataHandler $tceMain)
filterFileList($itemName, $itemIdentifier, $parentIdentifier, array $additionalInformation, \TYPO3\CMS\Core\Resource\Driver\DriverInterface $driver)
static trimExplode($delim, $string, $removeEmptyValues=FALSE, $limit=0)
if($list_of_literals) if(!empty($literals)) if(!empty($literals)) $result
Analyse literals to prepend the N char to them if their contents aren&#39;t numeric.
static revExplode($delimiter, $string, $count=0)