Completed
Push — master ( 01048e...77d2e6 )
by
unknown
15:26
created

DefaultFactory::getPublicStructure()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 21
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 14
c 1
b 0
f 0
nc 3
nop 2
dl 0
loc 21
rs 9.7998
1
<?php
2
namespace TYPO3\CMS\Install\FolderStructure;
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 TYPO3\CMS\Core\Core\Environment;
18
19
/**
20
 * Factory returns default folder structure object hierarchy
21
 * @internal This class is only meant to be used within EXT:install and is not part of the TYPO3 Core API.
22
 */
23
class DefaultFactory
24
{
25
    /**
26
     * Get default structure object hierarchy
27
     *
28
     * @return StructureFacadeInterface
29
     */
30
    public function getStructure()
31
    {
32
        $rootNode = new RootNode($this->getDefaultStructureDefinition(), null);
33
        return new StructureFacade($rootNode);
34
    }
35
36
    /**
37
     * Default definition of folder and file structure with dynamic
38
     * permission settings
39
     *
40
     * @return array
41
     */
42
    protected function getDefaultStructureDefinition(): array
43
    {
44
        $filePermission = $GLOBALS['TYPO3_CONF_VARS']['SYS']['fileCreateMask'];
45
        $directoryPermission = $GLOBALS['TYPO3_CONF_VARS']['SYS']['folderCreateMask'];
46
        if (Environment::getPublicPath() === Environment::getProjectPath()) {
47
            $structure = [
48
                // Note that root node has no trailing slash like all others
49
                'name' => Environment::getPublicPath(),
50
                'targetPermission' => $directoryPermission,
51
                'children' => [
52
                    [
53
                        'name' => 'typo3temp',
54
                        'type' => DirectoryNode::class,
55
                        'targetPermission' => $directoryPermission,
56
                        'children' => [
57
                            [
58
                                'name' => 'index.html',
59
                                'type' => FileNode::class,
60
                                'targetPermission' => $filePermission,
61
                                'targetContent' => '',
62
                            ],
63
                            $this->getTemporaryAssetsFolderStructure(),
64
                            [
65
                                'name' => 'var',
66
                                'type' => DirectoryNode::class,
67
                                'targetPermission' => $directoryPermission,
68
                                'children' => [
69
                                    [
70
                                        'name' => '.htaccess',
71
                                        'type' => FileNode::class,
72
                                        'targetPermission' => $filePermission,
73
                                        'targetContentFile' => Environment::getFrameworkBasePath() . '/install/Resources/Private/FolderStructureTemplateFiles/typo3temp-var-htaccess',
74
                                    ],
75
                                    [
76
                                        'name' => 'charset',
77
                                        'type' => DirectoryNode::class,
78
                                        'targetPermission' => $directoryPermission,
79
                                    ],
80
                                    [
81
                                        'name' => 'cache',
82
                                        'type' => DirectoryNode::class,
83
                                        'targetPermission' => $directoryPermission,
84
                                    ],
85
                                    [
86
                                        'name' => 'lock',
87
                                        'type' => DirectoryNode::class,
88
                                        'targetPermission' => $directoryPermission,
89
                                    ]
90
                                ]
91
                            ],
92
                        ],
93
                    ],
94
                    [
95
                        'name' => 'typo3conf',
96
                        'type' => DirectoryNode::class,
97
                        'targetPermission' => $directoryPermission,
98
                        'children' => [
99
                            [
100
                                'name' => 'ext',
101
                                'type' => DirectoryNode::class,
102
                                'targetPermission' => $directoryPermission,
103
                            ],
104
                            [
105
                                'name' => 'l10n',
106
                                'type' => DirectoryNode::class,
107
                                'targetPermission' => $directoryPermission,
108
                            ],
109
                        ],
110
                    ],
111
                    $this->getFileadminStructure(),
112
                ],
113
            ];
114
115
            // Have a default .htaccess if running apache web server or a default web.config if running IIS
116
            if ($this->isApacheServer()) {
117
                $structure['children'][] = [
118
                    'name' => '.htaccess',
119
                    'type' => FileNode::class,
120
                    'targetPermission' => $filePermission,
121
                    'targetContentFile' => Environment::getFrameworkBasePath() . '/install/Resources/Private/FolderStructureTemplateFiles/root-htaccess',
122
                ];
123
            } elseif ($this->isMicrosoftIisServer()) {
124
                $structure['children'][] = [
125
                    'name' => 'web.config',
126
                    'type' => FileNode::class,
127
                    'targetPermission' => $filePermission,
128
                    'targetContentFile' => Environment::getFrameworkBasePath() . '/install/Resources/Private/FolderStructureTemplateFiles/root-web-config',
129
                ];
130
            }
131
        } else {
132
            // This is when the public path is a subfolder (e.g. public/ or web/)
133
            $publicPath = substr(Environment::getPublicPath(), strlen(Environment::getProjectPath())+1);
0 ignored issues
show
Coding Style introduced by
Expected 1 space before "+"; 0 found
Loading history...
Coding Style introduced by
Expected 1 space after "+"; 0 found
Loading history...
134
135
            $publicPathSubStructure = [
136
                [
137
                    'name' => 'typo3temp',
138
                    'type' => DirectoryNode::class,
139
                    'targetPermission' => $directoryPermission,
140
                    'children' => [
141
                        [
142
                            'name' => 'index.html',
143
                            'type' => FileNode::class,
144
                            'targetPermission' => $filePermission,
145
                            'targetContent' => '',
146
                        ],
147
                        $this->getTemporaryAssetsFolderStructure(),
148
                    ],
149
                ],
150
                [
151
                    'name' => 'typo3conf',
152
                    'type' => DirectoryNode::class,
153
                    'targetPermission' => $directoryPermission,
154
                    'children' => [
155
                        [
156
                            'name' => 'ext',
157
                            'type' => DirectoryNode::class,
158
                            'targetPermission' => $directoryPermission,
159
                        ],
160
                    ],
161
                ],
162
                $this->getFileadminStructure(),
163
            ];
164
165
            // Have a default .htaccess if running apache web server or a default web.config if running IIS
166
            if ($this->isApacheServer()) {
167
                $publicPathSubStructure[] = [
168
                    'name' => '.htaccess',
169
                    'type' => FileNode::class,
170
                    'targetPermission' => $filePermission,
171
                    'targetContentFile' => Environment::getFrameworkBasePath() . '/install/Resources/Private/FolderStructureTemplateFiles/root-htaccess',
172
                ];
173
            } elseif ($this->isMicrosoftIisServer()) {
174
                $publicPathSubStructure[] = [
175
                    'name' => 'web.config',
176
                    'type' => FileNode::class,
177
                    'targetPermission' => $filePermission,
178
                    'targetContentFile' => Environment::getFrameworkBasePath() . '/install/Resources/Private/FolderStructureTemplateFiles/root-web-config',
179
                ];
180
            }
181
182
            $structure = [
183
                // Note that root node has no trailing slash like all others
184
                'name' => Environment::getProjectPath(),
185
                'targetPermission' => $directoryPermission,
186
                'children' => [
187
                    $this->getPublicStructure($publicPath, $publicPathSubStructure),
188
                    [
189
                        'name' => 'var',
190
                        'type' => DirectoryNode::class,
191
                        'targetPermission' => $directoryPermission,
192
                        'children' => [
193
                            [
194
                                'name' => '.htaccess',
195
                                'type' => FileNode::class,
196
                                'targetPermission' => $filePermission,
197
                                'targetContentFile' => Environment::getFrameworkBasePath() . '/install/Resources/Private/FolderStructureTemplateFiles/typo3temp-var-htaccess',
198
                            ],
199
                            [
200
                                'name' => 'charset',
201
                                'type' => DirectoryNode::class,
202
                                'targetPermission' => $directoryPermission,
203
                            ],
204
                            [
205
                                'name' => 'cache',
206
                                'type' => DirectoryNode::class,
207
                                'targetPermission' => $directoryPermission,
208
                            ],
209
                            [
210
                                'name' => 'labels',
211
                                'type' => DirectoryNode::class,
212
                                'targetPermission' => $directoryPermission,
213
                            ],
214
                            [
215
                                'name' => 'lock',
216
                                'type' => DirectoryNode::class,
217
                                'targetPermission' => $directoryPermission,
218
                            ],
219
                        ]
220
                    ]
221
                ],
222
            ];
223
        }
224
        return $structure;
225
    }
226
227
    /**
228
     * Get public path structure while resolving nested paths
229
     *
230
     * @param string $publicPath
231
     * @param array $subStructure
232
     * @return array
233
     */
234
    protected function getPublicStructure(string $publicPath, array $subStructure): array
235
    {
236
        $directoryPermission = $GLOBALS['TYPO3_CONF_VARS']['SYS']['folderCreateMask'];
237
        $publicPathParts = array_reverse(mb_split('/', $publicPath));
238
239
        $lastNode = null;
240
        foreach ($publicPathParts as $publicPathPart) {
241
            $node = [
242
                'name' => $publicPathPart,
243
                'type' => DirectoryNode::class,
244
                'targetPermission' => $directoryPermission,
245
            ];
246
            if ($lastNode !== null) {
247
                $node['children'][] = $lastNode;
248
            } else {
249
                $node['children'] = $subStructure;
250
            }
251
            $lastNode = $node;
252
        }
253
254
        return $lastNode;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $lastNode could return the type null which is incompatible with the type-hinted return array. Consider adding an additional type-check to rule them out.
Loading history...
255
    }
256
257
    protected function getFileadminStructure(): array
258
    {
259
        $filePermission = $GLOBALS['TYPO3_CONF_VARS']['SYS']['fileCreateMask'];
260
        $directoryPermission = $GLOBALS['TYPO3_CONF_VARS']['SYS']['folderCreateMask'];
261
        return [
262
            'name' => !empty($GLOBALS['TYPO3_CONF_VARS']['BE']['fileadminDir']) ? rtrim($GLOBALS['TYPO3_CONF_VARS']['BE']['fileadminDir'], '/') : 'fileadmin',
263
            'type' => DirectoryNode::class,
264
            'targetPermission' => $directoryPermission,
265
            'children' => [
266
                [
267
                    'name' => '_temp_',
268
                    'type' => DirectoryNode::class,
269
                    'targetPermission' => $directoryPermission,
270
                    'children' => [
271
                        [
272
                            'name' => '.htaccess',
273
                            'type' => FileNode::class,
274
                            'targetPermission' => $filePermission,
275
                            'targetContentFile' => Environment::getFrameworkBasePath() . '/install/Resources/Private/FolderStructureTemplateFiles/fileadmin-temp-htaccess',
276
                        ],
277
                        [
278
                            'name' => 'index.html',
279
                            'type' => FileNode::class,
280
                            'targetPermission' => $filePermission,
281
                            'targetContentFile' => Environment::getFrameworkBasePath() . '/install/Resources/Private/FolderStructureTemplateFiles/fileadmin-temp-index.html',
282
                        ],
283
                    ],
284
                ],
285
                [
286
                    'name' => 'user_upload',
287
                    'type' => DirectoryNode::class,
288
                    'targetPermission' => $directoryPermission,
289
                    'children' => [
290
                        [
291
                            'name' => '_temp_',
292
                            'type' => DirectoryNode::class,
293
                            'targetPermission' => $directoryPermission,
294
                            'children' => [
295
                                [
296
                                    'name' => 'index.html',
297
                                    'type' => FileNode::class,
298
                                    'targetPermission' => $filePermission,
299
                                    'targetContent' => '',
300
                                ],
301
                                [
302
                                    'name' => 'importexport',
303
                                    'type' => DirectoryNode::class,
304
                                    'targetPermission' => $directoryPermission,
305
                                    'children' => [
306
                                        [
307
                                            'name' => '.htaccess',
308
                                            'type' => FileNode::class,
309
                                            'targetPermission' => $filePermission,
310
                                            'targetContentFile' => Environment::getFrameworkBasePath() . '/install/Resources/Private/FolderStructureTemplateFiles/fileadmin-user_upload-temp-importexport-htaccess',
311
                                        ],
312
                                        [
313
                                            'name' => 'index.html',
314
                                            'type' => FileNode::class,
315
                                            'targetPermission' => $filePermission,
316
                                            'targetContentFile' => Environment::getFrameworkBasePath() . '/install/Resources/Private/FolderStructureTemplateFiles/fileadmin-temp-index.html',
317
                                        ],
318
                                    ],
319
                                ],
320
                            ],
321
                        ],
322
                        [
323
                            'name' => 'index.html',
324
                            'type' => FileNode::class,
325
                            'targetPermission' => $filePermission,
326
                            'targetContent' => '',
327
                        ],
328
                    ],
329
                ],
330
            ],
331
        ];
332
    }
333
334
    /**
335
     * This defines the structure for typo3temp/assets
336
     *
337
     * @return array
338
     */
339
    protected function getTemporaryAssetsFolderStructure(): array
340
    {
341
        $directoryPermission = $GLOBALS['TYPO3_CONF_VARS']['SYS']['folderCreateMask'];
342
        return [
343
            'name' => 'assets',
344
            'type' => DirectoryNode::class,
345
            'targetPermission' => $directoryPermission,
346
            'children' => [
347
                [
348
                    'name' => 'compressed',
349
                    'type' => DirectoryNode::class,
350
                    'targetPermission' => $directoryPermission
351
                ],
352
                [
353
                    'name' => 'css',
354
                    'type' => DirectoryNode::class,
355
                    'targetPermission' => $directoryPermission
356
                ],
357
                [
358
                    'name' => 'js',
359
                    'type' => DirectoryNode::class,
360
                    'targetPermission' => $directoryPermission
361
                ],
362
                [
363
                    'name' => 'images',
364
                    'type' => DirectoryNode::class,
365
                    'targetPermission' => $directoryPermission
366
                ],
367
                [
368
                    'name' => '_processed_',
369
                    'type' => DirectoryNode::class,
370
                    'targetPermission' => $directoryPermission
371
                ]
372
            ]
373
        ];
374
    }
375
376
    protected function isApacheServer(): bool
377
    {
378
        return isset($_SERVER['SERVER_SOFTWARE']) && strpos($_SERVER['SERVER_SOFTWARE'], 'Apache') === 0;
379
    }
380
381
    protected function isMicrosoftIisServer(): bool
382
    {
383
        return isset($_SERVER['SERVER_SOFTWARE']) && strpos($_SERVER['SERVER_SOFTWARE'], 'Microsoft-IIS') === 0;
384
    }
385
}
386