‪TYPO3CMS  10.4
StorageRepository.php
Go to the documentation of this file.
1 <?php
2 
3 /*
4  * This file is part of the TYPO3 CMS project.
5  *
6  * It is free software; you can redistribute it and/or modify it under
7  * the terms of the GNU General Public License, either version 2
8  * of the License, or any later version.
9  *
10  * For the full copyright and license information, please read the
11  * LICENSE.txt file that was distributed with this source code.
12  *
13  * The TYPO3 project - inspiring people to share!
14  */
15 
17 
18 use Psr\Log\LoggerAwareInterface;
19 use Psr\Log\LoggerAwareTrait;
26 
30 class ‪StorageRepository extends ‪AbstractRepository implements LoggerAwareInterface
31 {
32  use LoggerAwareTrait;
33 
37  protected ‪$storageRowCache;
38 
42  protected ‪$objectType = ResourceStorage::class;
43 
47  protected ‪$table = 'sys_file_storage';
48 
52  protected ‪$typeField = 'driver';
53 
57  protected ‪$driverField = 'driver';
58 
64  public function ‪findByUid($uid)
65  {
66  $this->‪initializeLocalCache();
67  if (isset($this->storageRowCache[$uid])) {
68  return $this->factory->getStorageObject($uid, $this->storageRowCache[$uid]);
69  }
70  return null;
71  }
72 
81  public function ‪fetchRowByUid(int $uid): array
82  {
83  $this->‪initializeLocalCache();
84  if (!isset($this->storageRowCache[$uid])) {
85  throw new \InvalidArgumentException(sprintf('No storage found with uid "%d".', $uid), 1599235454);
86  }
87 
88  return $this->storageRowCache[$uid];
89  }
90 
94  protected function ‪initializeLocalCache()
95  {
96  if ($this->storageRowCache === null) {
97  $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)
98  ->getQueryBuilderForTable($this->table);
99 
100  if ($this->‪getEnvironmentMode() === 'FE') {
101  $queryBuilder->setRestrictions(GeneralUtility::makeInstance(FrontendRestrictionContainer::class));
102  }
103 
104  $result = $queryBuilder
105  ->select('*')
106  ->from($this->table)
107  ->orderBy('name')
108  ->execute();
109 
110  $this->storageRowCache = [];
111  while ($row = $result->fetch()) {
112  if (!empty($row['uid'])) {
113  $this->storageRowCache[$row['uid']] = $row;
114  }
115  }
116 
117  // if no storage is created before or the user has not access to a storage
118  // $this->storageRowCache would have the value array()
119  // so check if there is any record. If no record is found, create the fileadmin/ storage
120  // selecting just one row is enough
121 
122  if ($this->storageRowCache === []) {
123  $connection = GeneralUtility::makeInstance(ConnectionPool::class)
124  ->getConnectionForTable($this->table);
125 
126  $storageObjectsCount = $connection->count('uid', $this->table, []);
127 
128  if ($storageObjectsCount === 0) {
129  if ($this->‪createLocalStorage(
130  'fileadmin/ (auto-created)',
131  ‪$GLOBALS['TYPO3_CONF_VARS']['BE']['fileadminDir'],
132  'relative',
133  'This is the local fileadmin/ directory. This storage mount has been created automatically by TYPO3.',
134  true
135  ) > 0) {
136  // reset to null to force reloading of storages
137  $this->storageRowCache = null;
138  // call self for initialize Cache
139  $this->‪initializeLocalCache();
140  }
141  }
142  }
143  }
144  }
145 
152  public function ‪findByStorageType($storageType)
153  {
154  $this->‪initializeLocalCache();
155 
157  $driverRegistry = GeneralUtility::makeInstance(DriverRegistry::class);
158 
159  $storageObjects = [];
160  foreach ($this->storageRowCache as $storageRow) {
161  if ($storageRow['driver'] !== $storageType) {
162  continue;
163  }
164  if ($driverRegistry->driverExists($storageRow['driver'])) {
165  $storageObjects[] = $this->factory->getStorageObject($storageRow['uid'], $storageRow);
166  } else {
167  $this->logger->warning(
168  sprintf('Could not instantiate storage "%s" because of missing driver.', $storageRow['name']),
169  $storageRow
170  );
171  }
172  }
173  return $storageObjects;
174  }
175 
182  public function ‪findAll()
183  {
184  $this->‪initializeLocalCache();
185 
187  $driverRegistry = GeneralUtility::makeInstance(DriverRegistry::class);
188 
189  $storageObjects = [];
190  foreach ($this->storageRowCache as $storageRow) {
191  if ($driverRegistry->driverExists($storageRow['driver'])) {
192  $storageObjects[] = $this->factory->getStorageObject($storageRow['uid'], $storageRow);
193  } else {
194  $this->logger->warning(
195  sprintf('Could not instantiate storage "%s" because of missing driver.', $storageRow['name']),
196  $storageRow
197  );
198  }
199  }
200  return $storageObjects;
201  }
202 
213  public function ‪createLocalStorage($name, $basePath, $pathType, $description = '', $default = false)
214  {
215  $caseSensitive = $this->‪testCaseSensitivity($pathType === 'relative' ? ‪Environment::getPublicPath() . '/' . $basePath : $basePath);
216  // create the FlexForm for the driver configuration
217  $flexFormData = [
218  'data' => [
219  'sDEF' => [
220  'lDEF' => [
221  'basePath' => ['vDEF' => rtrim($basePath, '/') . '/'],
222  'pathType' => ['vDEF' => $pathType],
223  'caseSensitive' => ['vDEF' => $caseSensitive]
224  ]
225  ]
226  ]
227  ];
228 
230  $flexObj = GeneralUtility::makeInstance(FlexFormTools::class);
231  $flexFormXml = $flexObj->flexArray2Xml($flexFormData, true);
232 
233  // create the record
234  $field_values = [
235  'pid' => 0,
236  'tstamp' => ‪$GLOBALS['EXEC_TIME'],
237  'crdate' => ‪$GLOBALS['EXEC_TIME'],
238  'name' => $name,
239  'description' => $description,
240  'driver' => 'Local',
241  'configuration' => $flexFormXml,
242  'is_online' => 1,
243  'is_browsable' => 1,
244  'is_public' => 1,
245  'is_writable' => 1,
246  'is_default' => $default ? 1 : 0
247  ];
248 
249  $dbConnection = GeneralUtility::makeInstance(ConnectionPool::class)
250  ->getConnectionForTable($this->table);
251  $dbConnection->insert($this->table, $field_values);
252 
253  // Flush local resourceStorage cache so the storage can be accessed during the same request right away
254  $this->storageRowCache = null;
255 
256  return (int)$dbConnection->lastInsertId($this->table);
257  }
258 
265  protected function ‪createDomainObject(array $databaseRow)
266  {
267  return $this->factory->getStorageObject($databaseRow['uid'], $databaseRow);
268  }
269 
276  protected function ‪testCaseSensitivity($absolutePath)
277  {
278  $caseSensitive = true;
279  $path = rtrim($absolutePath, '/') . '/aAbB';
280  $testFileExists = @file_exists($path);
281 
282  // create test file
283  if (!$testFileExists) {
284  touch($path);
285  }
286 
287  // do the actual sensitivity check
288  if (@file_exists(strtoupper($path)) && @file_exists(strtolower($path))) {
289  $caseSensitive = false;
290  }
291 
292  // clean filesystem
293  if (!$testFileExists) {
294  unlink($path);
295  }
296 
297  return $caseSensitive;
298  }
299 }
‪TYPO3\CMS\Core\Core\Environment\getPublicPath
‪static string getPublicPath()
Definition: Environment.php:180
‪TYPO3\CMS\Core\Resource\StorageRepository\fetchRowByUid
‪array fetchRowByUid(int $uid)
Definition: StorageRepository.php:76
‪TYPO3\CMS\Core\Resource\StorageRepository\$table
‪string $table
Definition: StorageRepository.php:44
‪TYPO3\CMS\Core\Resource\AbstractRepository
Definition: AbstractRepository.php:31
‪TYPO3\CMS\Core\Resource\StorageRepository\testCaseSensitivity
‪bool testCaseSensitivity($absolutePath)
Definition: StorageRepository.php:271
‪TYPO3\CMS\Core\Resource\StorageRepository\$driverField
‪string $driverField
Definition: StorageRepository.php:52
‪TYPO3\CMS\Core\Resource\StorageRepository\findByStorageType
‪ResourceStorage[] findByStorageType($storageType)
Definition: StorageRepository.php:147
‪TYPO3\CMS\Core\Resource\StorageRepository\$typeField
‪string $typeField
Definition: StorageRepository.php:48
‪TYPO3\CMS\Core\Resource\StorageRepository\createDomainObject
‪ResourceStorage createDomainObject(array $databaseRow)
Definition: StorageRepository.php:260
‪TYPO3\CMS\Core\Resource\StorageRepository\$storageRowCache
‪array null $storageRowCache
Definition: StorageRepository.php:36
‪TYPO3\CMS\Core\Configuration\FlexForm\FlexFormTools
Definition: FlexFormTools.php:38
‪TYPO3\CMS\Core\Resource\StorageRepository
Definition: StorageRepository.php:31
‪TYPO3\CMS\Core\Resource\StorageRepository\createLocalStorage
‪int createLocalStorage($name, $basePath, $pathType, $description='', $default=false)
Definition: StorageRepository.php:208
‪TYPO3\CMS\Core\Resource\StorageRepository\initializeLocalCache
‪initializeLocalCache()
Definition: StorageRepository.php:89
‪TYPO3\CMS\Core\Resource
Definition: generateMimeTypes.php:52
‪TYPO3\CMS\Core\Resource\Driver\DriverRegistry
Definition: DriverRegistry.php:24
‪TYPO3\CMS\Core\Resource\StorageRepository\$objectType
‪string $objectType
Definition: StorageRepository.php:40
‪TYPO3\CMS\Core\Resource\ResourceStorage
Definition: ResourceStorage.php:122
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:5
‪TYPO3\CMS\Core\Core\Environment
Definition: Environment.php:40
‪TYPO3\CMS\Core\Database\ConnectionPool
Definition: ConnectionPool.php:46
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:46
‪TYPO3\CMS\Core\Database\Query\Restriction\FrontendRestrictionContainer
Definition: FrontendRestrictionContainer.php:31
‪TYPO3\CMS\Core\Resource\StorageRepository\findByUid
‪ResourceStorage null findByUid($uid)
Definition: StorageRepository.php:59
‪TYPO3\CMS\Core\Resource\AbstractRepository\getEnvironmentMode
‪string getEnvironmentMode()
Definition: AbstractRepository.php:296
‪TYPO3\CMS\Core\Resource\StorageRepository\findAll
‪ResourceStorage[] findAll()
Definition: StorageRepository.php:177