Completed
Push — master ( fcada5...f5d72d )
by
unknown
13:28
created

DefaultFactory::isMicrosoftIisServer()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 1
nc 2
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
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
            $structure = [
135
                // Note that root node has no trailing slash like all others
136
                'name' => Environment::getProjectPath(),
137
                'targetPermission' => $directoryPermission,
138
                'children' => [
139
                    [
140
                        'name' => $publicPath,
141
                        'type' => DirectoryNode::class,
142
                        'targetPermission' => $directoryPermission,
143
                        'children' => [
144
                            [
145
                                'name' => 'typo3temp',
146
                                'type' => DirectoryNode::class,
147
                                'targetPermission' => $directoryPermission,
148
                                'children' => [
149
                                    [
150
                                        'name' => 'index.html',
151
                                        'type' => FileNode::class,
152
                                        'targetPermission' => $filePermission,
153
                                        'targetContent' => '',
154
                                    ],
155
                                    $this->getTemporaryAssetsFolderStructure(),
156
                                ],
157
                            ],
158
                            [
159
                                'name' => 'typo3conf',
160
                                'type' => DirectoryNode::class,
161
                                'targetPermission' => $directoryPermission,
162
                                'children' => [
163
                                    [
164
                                        'name' => 'ext',
165
                                        'type' => DirectoryNode::class,
166
                                        'targetPermission' => $directoryPermission,
167
                                    ],
168
                                ],
169
                            ],
170
                            $this->getFileadminStructure(),
171
                        ]
172
                    ],
173
                    [
174
                        'name' => 'var',
175
                        'type' => DirectoryNode::class,
176
                        'targetPermission' => $directoryPermission,
177
                        'children' => [
178
                            [
179
                                'name' => '.htaccess',
180
                                'type' => FileNode::class,
181
                                'targetPermission' => $filePermission,
182
                                'targetContentFile' => Environment::getFrameworkBasePath() . '/install/Resources/Private/FolderStructureTemplateFiles/typo3temp-var-htaccess',
183
                            ],
184
                            [
185
                                'name' => 'charset',
186
                                'type' => DirectoryNode::class,
187
                                'targetPermission' => $directoryPermission,
188
                            ],
189
                            [
190
                                'name' => 'cache',
191
                                'type' => DirectoryNode::class,
192
                                'targetPermission' => $directoryPermission,
193
                            ],
194
                            [
195
                                'name' => 'labels',
196
                                'type' => DirectoryNode::class,
197
                                'targetPermission' => $directoryPermission,
198
                            ],
199
                            [
200
                                'name' => 'lock',
201
                                'type' => DirectoryNode::class,
202
                                'targetPermission' => $directoryPermission,
203
                            ],
204
                        ]
205
                    ]
206
                ],
207
            ];
208
209
            // Have a default .htaccess if running apache web server or a default web.config if running IIS
210
            if ($this->isApacheServer()) {
211
                $structure['children'][0]['children'][] = [
212
                    'name' => '.htaccess',
213
                    'type' => FileNode::class,
214
                    'targetPermission' => $filePermission,
215
                    'targetContentFile' => Environment::getFrameworkBasePath() . '/install/Resources/Private/FolderStructureTemplateFiles/root-htaccess',
216
                ];
217
            } elseif ($this->isMicrosoftIisServer()) {
218
                $structure['children'][0]['children'][] = [
219
                    'name' => 'web.config',
220
                    'type' => FileNode::class,
221
                    'targetPermission' => $filePermission,
222
                    'targetContentFile' => Environment::getFrameworkBasePath() . '/install/Resources/Private/FolderStructureTemplateFiles/root-web-config',
223
                ];
224
            }
225
        }
226
        return $structure;
227
    }
228
229
    protected function getFileadminStructure(): array
230
    {
231
        $filePermission = $GLOBALS['TYPO3_CONF_VARS']['SYS']['fileCreateMask'];
232
        $directoryPermission = $GLOBALS['TYPO3_CONF_VARS']['SYS']['folderCreateMask'];
233
        return [
234
            'name' => !empty($GLOBALS['TYPO3_CONF_VARS']['BE']['fileadminDir']) ? rtrim($GLOBALS['TYPO3_CONF_VARS']['BE']['fileadminDir'], '/') : 'fileadmin',
235
            'type' => DirectoryNode::class,
236
            'targetPermission' => $directoryPermission,
237
            'children' => [
238
                [
239
                    'name' => '_temp_',
240
                    'type' => DirectoryNode::class,
241
                    'targetPermission' => $directoryPermission,
242
                    'children' => [
243
                        [
244
                            'name' => '.htaccess',
245
                            'type' => FileNode::class,
246
                            'targetPermission' => $filePermission,
247
                            'targetContentFile' => Environment::getFrameworkBasePath() . '/install/Resources/Private/FolderStructureTemplateFiles/fileadmin-temp-htaccess',
248
                        ],
249
                        [
250
                            'name' => 'index.html',
251
                            'type' => FileNode::class,
252
                            'targetPermission' => $filePermission,
253
                            'targetContentFile' => Environment::getFrameworkBasePath() . '/install/Resources/Private/FolderStructureTemplateFiles/fileadmin-temp-index.html',
254
                        ],
255
                    ],
256
                ],
257
                [
258
                    'name' => 'user_upload',
259
                    'type' => DirectoryNode::class,
260
                    'targetPermission' => $directoryPermission,
261
                    'children' => [
262
                        [
263
                            'name' => '_temp_',
264
                            'type' => DirectoryNode::class,
265
                            'targetPermission' => $directoryPermission,
266
                            'children' => [
267
                                [
268
                                    'name' => 'index.html',
269
                                    'type' => FileNode::class,
270
                                    'targetPermission' => $filePermission,
271
                                    'targetContent' => '',
272
                                ],
273
                                [
274
                                    'name' => 'importexport',
275
                                    'type' => DirectoryNode::class,
276
                                    'targetPermission' => $directoryPermission,
277
                                    'children' => [
278
                                        [
279
                                            'name' => '.htaccess',
280
                                            'type' => FileNode::class,
281
                                            'targetPermission' => $filePermission,
282
                                            'targetContentFile' => Environment::getFrameworkBasePath() . '/install/Resources/Private/FolderStructureTemplateFiles/fileadmin-user_upload-temp-importexport-htaccess',
283
                                        ],
284
                                        [
285
                                            'name' => 'index.html',
286
                                            'type' => FileNode::class,
287
                                            'targetPermission' => $filePermission,
288
                                            'targetContentFile' => Environment::getFrameworkBasePath() . '/install/Resources/Private/FolderStructureTemplateFiles/fileadmin-temp-index.html',
289
                                        ],
290
                                    ],
291
                                ],
292
                            ],
293
                        ],
294
                        [
295
                            'name' => 'index.html',
296
                            'type' => FileNode::class,
297
                            'targetPermission' => $filePermission,
298
                            'targetContent' => '',
299
                        ],
300
                    ],
301
                ],
302
            ],
303
        ];
304
    }
305
306
    /**
307
     * This defines the structure for typo3temp/assets
308
     *
309
     * @return array
310
     */
311
    protected function getTemporaryAssetsFolderStructure(): array
312
    {
313
        $directoryPermission = $GLOBALS['TYPO3_CONF_VARS']['SYS']['folderCreateMask'];
314
        return [
315
            'name' => 'assets',
316
            'type' => DirectoryNode::class,
317
            'targetPermission' => $directoryPermission,
318
            'children' => [
319
                [
320
                    'name' => 'compressed',
321
                    'type' => DirectoryNode::class,
322
                    'targetPermission' => $directoryPermission
323
                ],
324
                [
325
                    'name' => 'css',
326
                    'type' => DirectoryNode::class,
327
                    'targetPermission' => $directoryPermission
328
                ],
329
                [
330
                    'name' => 'js',
331
                    'type' => DirectoryNode::class,
332
                    'targetPermission' => $directoryPermission
333
                ],
334
                [
335
                    'name' => 'images',
336
                    'type' => DirectoryNode::class,
337
                    'targetPermission' => $directoryPermission
338
                ],
339
                [
340
                    'name' => '_processed_',
341
                    'type' => DirectoryNode::class,
342
                    'targetPermission' => $directoryPermission
343
                ]
344
            ]
345
        ];
346
    }
347
348
    protected function isApacheServer(): bool
349
    {
350
        return isset($_SERVER['SERVER_SOFTWARE']) && strpos($_SERVER['SERVER_SOFTWARE'], 'Apache') === 0;
351
    }
352
353
    protected function isMicrosoftIisServer(): bool
354
    {
355
        return isset($_SERVER['SERVER_SOFTWARE']) && strpos($_SERVER['SERVER_SOFTWARE'], 'Microsoft-IIS') === 0;
356
    }
357
}
358