TYPO3 CMS  TYPO3_7-6
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 
21 {
25  protected static $storageRowCache = null;
26 
30  protected $objectType = \TYPO3\CMS\Core\Resource\ResourceStorage::class;
31 
35  protected $table = 'sys_file_storage';
36 
40  protected $typeField = 'driver';
41 
45  protected $driverField = 'driver';
46 
50  protected $logger;
51 
55  protected $db;
56 
57  public function __construct()
58  {
59  parent::__construct();
60 
62  $logManager = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(\TYPO3\CMS\Core\Log\LogManager::class);
63  $this->logger = $logManager->getLogger(__CLASS__);
64  $this->db = $GLOBALS['TYPO3_DB'];
65  }
66 
72  public function findByUid($uid)
73  {
74  $this->initializeLocalCache();
75  if (isset(self::$storageRowCache[$uid])) {
76  return $this->factory->getStorageObject($uid, self::$storageRowCache[$uid]);
77  }
78  return null;
79  }
80 
86  protected function initializeLocalCache()
87  {
88  if (static::$storageRowCache === null) {
89  static::$storageRowCache = $this->db->exec_SELECTgetRows(
90  '*',
91  $this->table,
92  '1=1' . $this->getWhereClauseForEnabledFields(),
93  '',
94  'name',
95  '',
96  'uid'
97  );
98  // if no storage is created before or the user has not access to a storage
99  // static::$storageRowCache would have the value array()
100  // so check if there is any record. If no record is found, create the fileadmin/ storage
101  // selecting just one row is enoung
102 
103  if (static::$storageRowCache === []) {
104  $storageObjectsExists = $this->db->exec_SELECTgetSingleRow('uid', $this->table, '');
105  if ($storageObjectsExists !== null) {
106  if ($this->createLocalStorage(
107  'fileadmin/ (auto-created)',
108  $GLOBALS['TYPO3_CONF_VARS']['BE']['fileadminDir'],
109  'relative',
110  'This is the local fileadmin/ directory. This storage mount has been created automatically by TYPO3.',
111  true
112  ) > 0) {
113  // reset to null to force reloading of storages
114  static::$storageRowCache = null;
115  // call self for initialize Cache
116  $this->initializeLocalCache();
117  }
118  }
119  }
120  }
121  }
122 
129  public function findByStorageType($storageType)
130  {
131  $this->initializeLocalCache();
132 
134  $driverRegistry = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(\TYPO3\CMS\Core\Resource\Driver\DriverRegistry::class);
135 
136  $storageObjects = [];
137  foreach (static::$storageRowCache as $storageRow) {
138  if ($storageRow['driver'] !== $storageType) {
139  continue;
140  }
141  if ($driverRegistry->driverExists($storageRow['driver'])) {
142  $storageObjects[] = $this->factory->getStorageObject($storageRow['uid'], $storageRow);
143  } else {
144  $this->logger->warning(
145  sprintf('Could not instantiate storage "%s" because of missing driver.', [$storageRow['name']]),
146  $storageRow
147  );
148  }
149  }
150  return $storageObjects;
151  }
152 
159  public function findAll()
160  {
161  $this->initializeLocalCache();
162 
164  $driverRegistry = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(\TYPO3\CMS\Core\Resource\Driver\DriverRegistry::class);
165 
166  $storageObjects = [];
167  foreach (static::$storageRowCache as $storageRow) {
168  if ($driverRegistry->driverExists($storageRow['driver'])) {
169  $storageObjects[] = $this->factory->getStorageObject($storageRow['uid'], $storageRow);
170  } else {
171  $this->logger->warning(
172  sprintf('Could not instantiate storage "%s" because of missing driver.', [$storageRow['name']]),
173  $storageRow
174  );
175  }
176  }
177  return $storageObjects;
178  }
179 
190  public function createLocalStorage($name, $basePath, $pathType, $description = '', $default = false)
191  {
192  $caseSensitive = $this->testCaseSensitivity($pathType === 'relative' ? PATH_site . $basePath : $basePath);
193  // create the FlexForm for the driver configuration
194  $flexFormData = [
195  'data' => [
196  'sDEF' => [
197  'lDEF' => [
198  'basePath' => ['vDEF' => rtrim($basePath, '/') . '/'],
199  'pathType' => ['vDEF' => $pathType],
200  'caseSensitive' => ['vDEF' => $caseSensitive]
201  ]
202  ]
203  ]
204  ];
205 
207  $flexObj = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(\TYPO3\CMS\Core\Configuration\FlexForm\FlexFormTools::class);
208  $flexFormXml = $flexObj->flexArray2Xml($flexFormData, true);
209 
210  // create the record
211  $field_values = [
212  'pid' => 0,
213  'tstamp' => $GLOBALS['EXEC_TIME'],
214  'crdate' => $GLOBALS['EXEC_TIME'],
215  'name' => $name,
216  'description' => $description,
217  'driver' => 'Local',
218  'configuration' => $flexFormXml,
219  'is_online' => 1,
220  'is_browsable' => 1,
221  'is_public' => 1,
222  'is_writable' => 1,
223  'is_default' => $default ? 1 : 0
224  ];
225  $this->db->exec_INSERTquery('sys_file_storage', $field_values);
226  return (int)$this->db->sql_insert_id();
227  }
228 
235  protected function createDomainObject(array $databaseRow)
236  {
237  return $this->factory->getStorageObject($databaseRow['uid'], $databaseRow);
238  }
239 
246  protected function testCaseSensitivity($absolutePath)
247  {
248  $caseSensitive = true;
249  $path = rtrim($absolutePath, '/') . '/aAbB';
250  $testFileExists = @file_exists($path);
251 
252  // create test file
253  if (!$testFileExists) {
254  touch($path);
255  }
256 
257  // do the actual sensitivity check
258  if (@file_exists(strtoupper($path)) && @file_exists(strtolower($path))) {
259  $caseSensitive = false;
260  }
261 
262  // clean filesystem
263  if (!$testFileExists) {
264  unlink($path);
265  }
266 
267  return $caseSensitive;
268  }
269 }
$uid
Definition: server.php:38
if(TYPO3_MODE==='BE') $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_tsfebeuserauth.php']['frontendEditingController']['default']