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