/
DefaultBooter.php
491 lines (433 loc) · 17.3 KB
/
DefaultBooter.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
<?php
namespace Concrete\Core\Foundation\Runtime\Boot;
use Concrete\Core\Application\Application;
use Concrete\Core\Application\ApplicationAwareInterface;
use Concrete\Core\Application\ApplicationAwareTrait;
use Concrete\Core\Asset\AssetList;
use Concrete\Core\File\Type\TypeList;
use Concrete\Core\Foundation\ClassAliasList;
use Concrete\Core\Http\Request;
use Concrete\Core\Page\Theme\ThemeRouteCollection;
use Concrete\Core\Routing\RedirectResponse;
use Concrete\Core\Routing\SystemRouteList;
use Concrete\Core\Support\Facade\Facade;
use Concrete\Core\Support\Facade\Route;
use Illuminate\Config\Repository;
use Symfony\Component\HttpFoundation\Request as SymphonyRequest;
use Symfony\Component\HttpFoundation\Response;
class DefaultBooter implements BootInterface, ApplicationAwareInterface
{
use ApplicationAwareTrait;
/**
* Boot up
* Return a response if we're ready to output.
*
* @return null|Response
*/
public function boot()
{
$app = $this->app;
/*
* ----------------------------------------------------------------------------
* Bind the IOC container to our facades
* Completely indebted to Taylor Otwell & Laravel for this.
* ----------------------------------------------------------------------------
*/
Facade::setFacadeApplication($app);
/**
* ----------------------------------------------------------------------------
* Load path detection for relative assets, URL and path to home.
* ----------------------------------------------------------------------------.
*/
require_once DIR_BASE_CORE . '/bootstrap/paths.php';
/*
* ----------------------------------------------------------------------------
* Add install environment detection
* ----------------------------------------------------------------------------
*/
$this->initializeEnvironmentDetection($app);
/*
* ----------------------------------------------------------------------------
* Enable Configuration
* ----------------------------------------------------------------------------
*/
$config = $this->initializeConfig($app);
/*
* ----------------------------------------------------------------------------
* Set configured error reporting
* ----------------------------------------------------------------------------
*/
$this->setupErrorReporting($config);
/*
* ----------------------------------------------------------------------------
* Enable Localization
* ----------------------------------------------------------------------------
*/
$this->initializeLocalization($app);
/*
* ----------------------------------------------------------------------------
* Finalize paths.
* ----------------------------------------------------------------------------
*/
require DIR_BASE_CORE . '/bootstrap/paths_configured.php';
/*
* ----------------------------------------------------------------------------
* Setup core classes aliases.
* ----------------------------------------------------------------------------
*/
$this->initializeClassAliases($config);
/*
* ----------------------------------------------------------------------------
* Simple legacy constants like APP_CHARSET
* ----------------------------------------------------------------------------
*/
$this->initializeLegacyDefinitions($config, $app);
/*
* ----------------------------------------------------------------------------
* Setup the core service groups.
* ----------------------------------------------------------------------------
*/
$this->initializeServiceProviders($app, $config);
/*
* ----------------------------------------------------------------------------
* Setup file cache directories. Has to come after we define services
* because we use the file service.
* ----------------------------------------------------------------------------
*/
$app->setupFilesystem();
/*
* ----------------------------------------------------------------------------
* Registries for theme paths, assets, routes and file types.
* ----------------------------------------------------------------------------
*/
$this->initializeAssets($config);
$this->initializeRoutes($config);
$this->initializeFileTypes($config);
// If we're not in the CLI SAPI, lets do additional booting for HTTP
if (!$this->app->isRunThroughCommandLineInterface()) {
return $this->bootHttpSapi($config, $app);
}
}
/**
* @param $config
* @param $app
*
* @return null|Response
*/
private function bootHttpSapi($config, $app)
{
/*
* ----------------------------------------------------------------------------
* Initialize the request
* ----------------------------------------------------------------------------
*/
$request = $this->initializeRequest($config);
/*
* ----------------------------------------------------------------------------
* If we haven't installed, then we need to reroute. If we have, and we're
* on the install page, and we haven't installed, then we need to dispatch
* early and exit.
* ----------------------------------------------------------------------------
*/
if ($response = $this->checkInstall($app, $request)) {
return $response;
}
if ($this->app->isInstalled()) {
/*
* ----------------------------------------------------------------------------
* Check the page cache in case we need to return a result early.
* ----------------------------------------------------------------------------
*/
if ($response = $this->checkCache($app, $request)) {
return $response;
}
/*
* ----------------------------------------------------------------------------
* Now we load all installed packages, and register their package autoloaders.
* ----------------------------------------------------------------------------
*/
$this->initializePackages($app);
}
}
/**
* Enable configuration.
*
* @param Application $app
*
* @return Repository
*/
private function initializeConfig(Application $app)
{
$config_provider = $app->make('Concrete\Core\Config\ConfigServiceProvider');
$config_provider->register();
/*
* @var \Concrete\Core\Config\Repository\Repository
*/
$config = $app->make('config');
return $config;
}
/**
* Setup the configured error reporting.
*
* @param Repository $config
*/
private function setupErrorReporting(Repository $config)
{
$error_reporting = $config->get('concrete.debug.error_reporting');
if ((string) $error_reporting !== '') {
error_reporting((int) $error_reporting);
}
}
/**
* Enable localization.
*
* This needs to happen very early in the boot process because the
* application configuration (config/app.php) is already calling the t()
* functions which are initializing the Localization singleton. When the
* singleton is being initialized, these services need to be already
* available.
*
* @param Application $app
*/
private function initializeLocalization(Application $app)
{
$localization_provider = $app->make('Concrete\Core\Localization\LocalizationEssentialServiceProvider');
$localization_provider->register();
}
/**
* @param Application $app
*/
private function initializeEnvironmentDetection(Application $app)
{
$environment = $app->environment();
$app->detectEnvironment(function () use ($environment, $app) {
$forceInstalled = \defined('CONCRETE5_INSTALLED') ? CONCRETE5_INSTALLED : getenv('CONCRETE5_INSTALLED');
// Allow overriding installation detection
if ($forceInstalled && strtolower($forceInstalled) !== 'auto') {
return filter_var($forceInstalled, FILTER_VALIDATE_BOOLEAN) ? $environment : 'install';
}
// Check if config has loaded, if so use that
try {
return $app->isInstalled() ? $environment : 'install';
} catch (\Exception $e) {
}
// If we have well formed database details defined, we're probably installed
if ($this->validateDatabaseDetails($environment)) {
return $environment;
}
return 'install';
});
}
/**
* Check whether an environment has well formed database credentials defined
*
* @param $environment
* @return mixed
*/
private function validateDatabaseDetails($environment)
{
$db_config = [];
$configFile = DIR_CONFIG_SITE . '/database.php';
$environmentConfig = DIR_CONFIG_SITE . "/{$environment}.database.php";
// If the database.php file exists, load it first
if (file_exists($configFile)) {
$db_config = include DIR_CONFIG_SITE . '/database.php';
}
// If there's an environment specific database file, load that too
if (file_exists($environmentConfig)) {
$db_config = array_merge($db_config, include $environmentConfig);
}
// Make sure the default connection is set
$defaultConnection = array_get($db_config, 'default-connection');
// Make sure we have all the stuff we expect
$connection = array_get($db_config, "connections.{$defaultConnection}");
// Make sure we have all the keys we are expecting
return $defaultConnection && $connection &&
array_get($connection, 'database') &&
array_get($connection, 'username') &&
array_get($connection, 'server');
}
/**
* @param Repository $config
*
* @return ClassAliasList
*/
private function initializeClassAliases(Repository $config)
{
$list = ClassAliasList::getInstance();
$list->registerMultipleRequired($config->get('app.aliases'));
$list->registerMultiple($config->get('app.facades'));
// Autoload aliases to prevent typehinting errors
class_exists('\Request');
if (version_compare(PHP_VERSION, '7.2.0alpha1') < 0) {
$list->register('Concrete\Core\Foundation\Object', 'Concrete\Core\Foundation\ConcreteObject');
}
return $list;
}
/**
* @param Application $app
* @param Repository $config
*/
private function initializeServiceProviders(Application $app, Repository $config)
{
$list = $this->app->make('Concrete\Core\Foundation\Service\ProviderList');
// Register events first so that they can be used by other providers.
$list->registerProvider($config->get('app.providers.core_events'));
// Register all other providers
$list->registerProviders($config->get('app.providers'));
}
/**
* @param Repository $config
* @param Application $app
*/
private function initializeLegacyDefinitions(Repository $config, Application $app)
{
define('APP_VERSION', $config->get('concrete.version'));
define('APP_CHARSET', $config->get('concrete.charset'));
define('DIR_REL', $app['app_relative_path']);
}
/**
* @param Repository $config
*/
private function initializeAssets(Repository $config)
{
$asset_list = AssetList::getInstance();
$asset_list->registerMultiple($config->get('app.assets', []));
$asset_list->registerGroupMultiple($config->get('app.asset_groups', []));
}
/**
* @param \Illuminate\Config\Repository $config
*/
private function initializeRoutes(Repository $config)
{
/**
* @var $router Router
*/
$router = Route::getFacadeRoot();
// Legacy route registration.
$router->registerMultiple($config->get('app.routes'));
// New style
$router->loadRouteList(new SystemRouteList());
// theme paths
$this->app->make(ThemeRouteCollection::class)
->setThemesByRoutes($config->get('app.theme_paths', array()));
}
/**
* @param \Illuminate\Config\Repository $config
*/
private function initializeFileTypes(Repository $config)
{
$type_list = TypeList::getInstance();
$type_list->defineMultiple($config->get('app.file_types', []));
$type_list->defineImporterAttributeMultiple($config->get('app.importer_attributes', []));
}
/**
* @param \Illuminate\Config\Repository $config
*
* @return Request
*/
private function initializeRequest(Repository $config)
{
/*
* Patch the request, so that it can be seen as if it was an ajax call.
* This can only be done by patching the superglobals, because we have
* to consider 3rd party libraries (like Symfony for instance) which use
* those superglobals.
*/
if (isset($_POST['__ccm_consider_request_as_xhr']) && $_POST['__ccm_consider_request_as_xhr'] === '1') {
unset($_POST['__ccm_consider_request_as_xhr']);
unset($_REQUEST['__ccm_consider_request_as_xhr']);
$_SERVER['HTTP_X_REQUESTED_WITH'] = 'XMLHttpRequest';
}
/*
* ----------------------------------------------------------------------------
* Set trusted proxies and headers for the request
* ----------------------------------------------------------------------------
*/
$trustedProxiesIps = $config->get('concrete.security.trusted_proxies.ips');
if ($trustedProxiesIps) {
$proxyHeaders = $config->get('concrete.security.trusted_proxies.headers');
if (defined(SymphonyRequest::class . '::HEADER_X_FORWARDED_ALL')) {
// Symphony 3.3+
if (is_array($proxyHeaders)) {
$proxyHeadersBitfield = 0;
$legacyValues = [
'forwarded' => Request::HEADER_FORWARDED,
'client_ip' => Request::HEADER_X_FORWARDED_FOR,
'client_host' => Request::HEADER_X_FORWARDED_HOST,
'client_proto' => Request::HEADER_X_FORWARDED_PROTO,
'client_port' => Request::HEADER_X_FORWARDED_PORT,
];
foreach ($proxyHeaders as $proxyHeader) {
if (isset($legacyValues[$proxyHeader])) {
$proxyHeadersBitfield |= $legacyValues[$proxyHeader];
}
}
} else {
$proxyHeadersBitfield = (int) $proxyHeaders;
}
if ($proxyHeadersBitfield === 0) {
$proxyHeadersBitfield = -1;
}
Request::setTrustedProxies($trustedProxiesIps, $proxyHeadersBitfield);
} else {
// Symphony 3.2-
if (is_array($proxyHeaders)) {
foreach ($proxyHeaders as $key => $value) {
Request::setTrustedHeaderName($key, $value);
}
}
Request::setTrustedProxies($trustedProxiesIps);
}
}
/*
* ----------------------------------------------------------------------------
* Obtain the Request object.
* ----------------------------------------------------------------------------
*/
$request = Request::getInstance();
return $request;
}
/**
* If we haven't installed and we're not looking at the install directory, redirect.
*
* @param Application $app
* @param Request $request
*
* @return null|Response
*/
private function checkInstall(Application $app, Request $request)
{
if (!$app->isInstalled()) {
if (
!$request->matches('/install/*')
&& $request->getPath() != '/install'
&& !$request->matches('/ccm/assets/localization/*')
) {
$manager = $app->make('Concrete\Core\Url\Resolver\Manager\ResolverManager');
$response = new RedirectResponse($manager->resolve(['install']));
return $response;
}
}
}
/**
* @param \Concrete\Core\Application\Application $app
* @param \Concrete\Core\Http\Request $request
*
* @return null|Response
*/
private function checkCache(Application $app, Request $request)
{
$response = $app->checkPageCache($request);
if ($response) {
return $response;
}
}
/**
* @param \Concrete\Core\Application\Application $app
*/
private function initializePackages(Application $app)
{
$app->setupPackageAutoloaders();
}
}