‪TYPO3CMS  9.5
StorageRepository.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 
17 use Psr\Log\LoggerAwareInterface;
18 use Psr\Log\LoggerAwareTrait;
24 
28 class ‪StorageRepository extends ‪AbstractRepository implements LoggerAwareInterface
29 {
30  use LoggerAwareTrait;
31 
35  protected ‪$storageRowCache;
36 
40  protected ‪$objectType = ResourceStorage::class;
41 
45  protected ‪$table = 'sys_file_storage';
46 
50  protected ‪$typeField = 'driver';
51 
55  protected ‪$driverField = 'driver';
56 
62  public function ‪findByUid($uid)
63  {
64  $this->‪initializeLocalCache();
65  if (isset($this->storageRowCache[$uid])) {
66  return $this->factory->getStorageObject($uid, $this->storageRowCache[$uid]);
67  }
68  return null;
69  }
70 
79  public function ‪fetchRowByUid(int $uid): array
80  {
81  $this->‪initializeLocalCache();
82  if (!isset($this->storageRowCache[$uid])) {
83  throw new \InvalidArgumentException(sprintf('No storage found with uid "%d".', $uid), 1599235454);
84  }
85 
86  return $this->storageRowCache[$uid];
87  }
88 
92  protected function ‪initializeLocalCache()
93  {
94  if ($this->storageRowCache === null) {
95  $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)
96  ->getQueryBuilderForTable($this->table);
97 
98  if ($this->‪getEnvironmentMode() === 'FE') {
99  $queryBuilder->setRestrictions(GeneralUtility::makeInstance(FrontendRestrictionContainer::class));
100  }
101 
102  $result = $queryBuilder
103  ->select('*')
104  ->from($this->table)
105  ->orderBy('name')
106  ->execute();
107 
108  $this->storageRowCache = [];
109  while ($row = $result->fetch()) {
110  if (!empty($row['uid'])) {
111  $this->storageRowCache[$row['uid']] = $row;
112  }
113  }
114 
115  // if no storage is created before or the user has not access to a storage
116  // $this->storageRowCache would have the value array()
117  // so check if there is any record. If no record is found, create the fileadmin/ storage
118  // selecting just one row is enough
119 
120  if ($this->storageRowCache === []) {
121  $connection = GeneralUtility::makeInstance(ConnectionPool::class)
122  ->getConnectionForTable($this->table);
123 
124  $storageObjectsCount = $connection->count('uid', $this->table, []);
125 
126  if ($storageObjectsCount === 0) {
127  if ($this->‪createLocalStorage(
128  'fileadmin/ (auto-created)',
129  ‪$GLOBALS['TYPO3_CONF_VARS']['BE']['fileadminDir'],
130  'relative',
131  'This is the local fileadmin/ directory. This storage mount has been created automatically by TYPO3.',
132  true
133  ) > 0) {
134  // reset to null to force reloading of storages
135  $this->storageRowCache = null;
136  // call self for initialize Cache
137  $this->‪initializeLocalCache();
138  }
139  }
140  }
141  }
142  }
143 
150  public function ‪findByStorageType($storageType)
151  {
152  $this->‪initializeLocalCache();
153 
155  $driverRegistry = GeneralUtility::makeInstance(Driver\DriverRegistry::class);
156 
157  $storageObjects = [];
158  foreach ($this->storageRowCache as $storageRow) {
159  if ($storageRow['driver'] !== $storageType) {
160  continue;
161  }
162  if ($driverRegistry->driverExists($storageRow['driver'])) {
163  $storageObjects[] = $this->factory->getStorageObject($storageRow['uid'], $storageRow);
164  } else {
165  $this->logger->warning(
166  sprintf('Could not instantiate storage "%s" because of missing driver.', [$storageRow['name']]),
167  $storageRow
168  );
169  }
170  }
171  return $storageObjects;
172  }
173 
180  public function ‪findAll()
181  {
182  $this->‪initializeLocalCache();
183 
185  $driverRegistry = GeneralUtility::makeInstance(Driver\DriverRegistry::class);
186 
187  $storageObjects = [];
188  foreach ($this->storageRowCache as $storageRow) {
189  if ($driverRegistry->driverExists($storageRow['driver'])) {
190  $storageObjects[] = $this->factory->getStorageObject($storageRow['uid'], $storageRow);
191  } else {
192  $this->logger->warning(
193  sprintf('Could not instantiate storage "%s" because of missing driver.', [$storageRow['name']]),
194  $storageRow
195  );
196  }
197  }
198  return $storageObjects;
199  }
200 
211  public function ‪createLocalStorage($name, $basePath, $pathType, $description = '', $default = false)
212  {
213  $caseSensitive = $this->‪testCaseSensitivity($pathType === 'relative' ? ‪Environment::getPublicPath() . '/' . $basePath : $basePath);
214  // create the FlexForm for the driver configuration
215  $flexFormData = [
216  'data' => [
217  'sDEF' => [
218  'lDEF' => [
219  'basePath' => ['vDEF' => rtrim($basePath, '/') . '/'],
220  'pathType' => ['vDEF' => $pathType],
221  'caseSensitive' => ['vDEF' => $caseSensitive]
222  ]
223  ]
224  ]
225  ];
226 
228  $flexObj = GeneralUtility::makeInstance(FlexFormTools::class);
229  $flexFormXml = $flexObj->flexArray2Xml($flexFormData, true);
230 
231  // create the record
232  $field_values = [
233  'pid' => 0,
234  'tstamp' => ‪$GLOBALS['EXEC_TIME'],
235  'crdate' => ‪$GLOBALS['EXEC_TIME'],
236  'name' => $name,
237  'description' => $description,
238  'driver' => 'Local',
239  'configuration' => $flexFormXml,
240  'is_online' => 1,
241  'is_browsable' => 1,
242  'is_public' => 1,
243  'is_writable' => 1,
244  'is_default' => $default ? 1 : 0
245  ];
246 
247  $dbConnection = GeneralUtility::makeInstance(ConnectionPool::class)
248  ->getConnectionForTable($this->table);
249  $dbConnection->insert($this->table, $field_values);
250 
251  // Flush local resourceStorage cache so the storage can be accessed during the same request right away
252  $this->storageRowCache = null;
253 
254  return (int)$dbConnection->lastInsertId($this->table);
255  }
256 
263  protected function ‪createDomainObject(array $databaseRow)
264  {
265  return $this->factory->getStorageObject($databaseRow['uid'], $databaseRow);
266  }
267 
274  protected function ‪testCaseSensitivity($absolutePath)
275  {
276  $caseSensitive = true;
277  $path = rtrim($absolutePath, '/') . '/aAbB';
278  $testFileExists = @file_exists($path);
279 
280  // create test file
281  if (!$testFileExists) {
282  touch($path);
283  }
284 
285  // do the actual sensitivity check
286  if (@file_exists(strtoupper($path)) && @file_exists(strtolower($path))) {
287  $caseSensitive = false;
288  }
289 
290  // clean filesystem
291  if (!$testFileExists) {
292  unlink($path);
293  }
294 
295  return $caseSensitive;
296  }
297 }
‪TYPO3\CMS\Core\Core\Environment\getPublicPath
‪static string getPublicPath()
Definition: Environment.php:153
‪TYPO3\CMS\Core\Resource\StorageRepository\fetchRowByUid
‪array fetchRowByUid(int $uid)
Definition: StorageRepository.php:74
‪TYPO3\CMS\Core\Resource\StorageRepository\$table
‪string $table
Definition: StorageRepository.php:42
‪TYPO3\CMS\Core\Resource\AbstractRepository
Definition: AbstractRepository.php:27
‪TYPO3\CMS\Core\Resource\StorageRepository\testCaseSensitivity
‪bool testCaseSensitivity($absolutePath)
Definition: StorageRepository.php:269
‪TYPO3\CMS\Core\Resource\StorageRepository\$driverField
‪string $driverField
Definition: StorageRepository.php:50
‪TYPO3\CMS\Core\Resource\StorageRepository\findByStorageType
‪ResourceStorage[] findByStorageType($storageType)
Definition: StorageRepository.php:145
‪TYPO3\CMS\Core\Resource\StorageRepository\$typeField
‪string $typeField
Definition: StorageRepository.php:46
‪TYPO3\CMS\Core\Resource\StorageRepository\createDomainObject
‪ResourceStorage createDomainObject(array $databaseRow)
Definition: StorageRepository.php:258
‪TYPO3\CMS\Core\Resource\StorageRepository\$storageRowCache
‪array null $storageRowCache
Definition: StorageRepository.php:34
‪TYPO3\CMS\Core\Configuration\FlexForm\FlexFormTools
Definition: FlexFormTools.php:36
‪TYPO3\CMS\Core\Resource\StorageRepository
Definition: StorageRepository.php:29
‪TYPO3\CMS\Core\Resource\StorageRepository\createLocalStorage
‪int createLocalStorage($name, $basePath, $pathType, $description='', $default=false)
Definition: StorageRepository.php:206
‪TYPO3\CMS\Core\Resource\StorageRepository\initializeLocalCache
‪initializeLocalCache()
Definition: StorageRepository.php:87
‪TYPO3\CMS\Core\Resource
Definition: generateMimeTypes.php:37
‪TYPO3\CMS\Core\Resource\StorageRepository\$objectType
‪string $objectType
Definition: StorageRepository.php:38
‪TYPO3\CMS\Core\Resource\ResourceStorage
Definition: ResourceStorage.php:74
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:5
‪TYPO3\CMS\Core\Core\Environment
Definition: Environment.php:39
‪TYPO3\CMS\Core\Database\ConnectionPool
Definition: ConnectionPool.php:44
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:45
‪TYPO3\CMS\Core\Database\Query\Restriction\FrontendRestrictionContainer
Definition: FrontendRestrictionContainer.php:29
‪TYPO3\CMS\Core\Resource\StorageRepository\findByUid
‪ResourceStorage null findByUid($uid)
Definition: StorageRepository.php:57
‪TYPO3\CMS\Core\Resource\AbstractRepository\getEnvironmentMode
‪string getEnvironmentMode()
Definition: AbstractRepository.php:289
‪TYPO3\CMS\Core\Resource\StorageRepository\findAll
‪ResourceStorage[] findAll()
Definition: StorageRepository.php:175