[TASK] Removes extra empty lines
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Classes / Resource / StorageRepository.php
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
17 /**
18 * Repository for accessing the file mounts
19 */
20 class StorageRepository extends AbstractRepository
21 {
22 /**
23 * @var NULL|array‚
24 */
25 protected static $storageRowCache = null;
26
27 /**
28 * @var string
29 */
30 protected $objectType = \TYPO3\CMS\Core\Resource\ResourceStorage::class;
31
32 /**
33 * @var string
34 */
35 protected $table = 'sys_file_storage';
36
37 /**
38 * @var string
39 */
40 protected $typeField = 'driver';
41
42 /**
43 * @var string
44 */
45 protected $driverField = 'driver';
46
47 /**
48 * @var \TYPO3\CMS\Core\Log\Logger
49 */
50 protected $logger;
51
52 /**
53 * @var \TYPO3\CMS\Core\Database\DatabaseConnection
54 */
55 protected $db;
56
57 public function __construct()
58 {
59 parent::__construct();
60
61 /** @var $logManager \TYPO3\CMS\Core\Log\LogManager */
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
67 /**
68 * @param int $uid
69 *
70 * @return NULL|ResourceStorage
71 */
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 /**
82 * Initializes the Storage
83 *
84 * @return void
85 */
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 === array()) {
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
123 /**
124 * Finds storages by type, i.e. the driver used
125 *
126 * @param string $storageType
127 * @return ResourceStorage[]
128 */
129 public function findByStorageType($storageType)
130 {
131 $this->initializeLocalCache();
132
133 /** @var $driverRegistry Driver\DriverRegistry */
134 $driverRegistry = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(\TYPO3\CMS\Core\Resource\Driver\DriverRegistry::class);
135
136 $storageObjects = array();
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.', array($storageRow['name'])),
146 $storageRow
147 );
148 }
149 }
150 return $storageObjects;
151 }
152
153 /**
154 * Returns a list of mountpoints that are available in the VFS.
155 * In case no storage exists this automatically created a storage for fileadmin/
156 *
157 * @return ResourceStorage[]
158 */
159 public function findAll()
160 {
161 $this->initializeLocalCache();
162
163 /** @var $driverRegistry Driver\DriverRegistry */
164 $driverRegistry = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(\TYPO3\CMS\Core\Resource\Driver\DriverRegistry::class);
165
166 $storageObjects = array();
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.', array($storageRow['name'])),
173 $storageRow
174 );
175 }
176 }
177 return $storageObjects;
178 }
179
180 /**
181 * Create the initial local storage base e.g. for the fileadmin/ directory.
182 *
183 * @param string $name
184 * @param string $basePath
185 * @param string $pathType
186 * @param string $description
187 * @param bool $default set to default storage
188 * @return int uid of the inserted record
189 */
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 = array(
195 'data' => array(
196 'sDEF' => array(
197 'lDEF' => array(
198 'basePath' => array('vDEF' => rtrim($basePath, '/') . '/'),
199 'pathType' => array('vDEF' => $pathType),
200 'caseSensitive' => array('vDEF' => $caseSensitive)
201 )
202 )
203 )
204 );
205
206 /** @var $flexObj \TYPO3\CMS\Core\Configuration\FlexForm\FlexFormTools */
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 = array(
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
229 /**
230 * Creates an object managed by this repository.
231 *
232 * @param array $databaseRow
233 * @return ResourceStorage
234 */
235 protected function createDomainObject(array $databaseRow)
236 {
237 return $this->factory->getStorageObject($databaseRow['uid'], $databaseRow);
238 }
239
240 /**
241 * Test if the local filesystem is case sensitive
242 *
243 * @param string $absolutePath
244 * @return bool
245 */
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 }