-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPlugin.php
More file actions
86 lines (76 loc) · 2.51 KB
/
Copy pathPlugin.php
File metadata and controls
86 lines (76 loc) · 2.51 KB
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
<?php namespace RainLab\Cloudflare;
use Config;
use Illuminate\Http\Request;
use Illuminate\Http\Middleware\TrustProxies;
use Illuminate\Contracts\Http\Kernel;
use RainLab\Cloudflare\Classes\IgnoreRocketLoader;
use System\Classes\PluginBase;
/**
* Plugin Information File
*
* @package rainlab\cloudflare
* @author Alexey Bobkov, Samuel Georges
*/
class Plugin extends PluginBase
{
/**
* pluginDetails about this plugin.
*/
public function pluginDetails()
{
return [
'name' => 'Cloudflare',
'description' => 'Improves compatibility when running October CMS behind the Cloudflare network.',
'author' => 'Alexey Bobkov, Samuel Georges',
'icon' => 'icon-cloud',
'homepage' => 'https://github.com/rainlab/cloudflare-plugin'
];
}
/**
* boot method, called right before the request route.
*/
public function boot()
{
if ($this->app->runningInConsole()) {
return;
}
$this->registerTrustedProxies();
$this->registerRocketLoaderBypass();
}
/**
* registerTrustedProxies trusts the Cloudflare network so the client address and request scheme resolve correctly.
*/
protected function registerTrustedProxies()
{
if (!Config::get('rainlab.cloudflare::trust_proxies', true)) {
return;
}
$addresses = (array) Config::get('rainlab.cloudflare::proxy_addresses', []);
if (!$addresses) {
return;
}
// Laravel 10 and above resets the trusted state on every request, using the
// TrustProxies middleware, so the address registration must go through it
if (method_exists(TrustProxies::class, 'at')) {
TrustProxies::at($addresses);
}
// Older versions found in October CMS v3 do not boot this middleware, so the
// addresses are registered directly and persist for the request lifecycle
else {
Request::setTrustedProxies(
$addresses,
Request::HEADER_X_FORWARDED_FOR |
Request::HEADER_X_FORWARDED_HOST |
Request::HEADER_X_FORWARDED_PORT |
Request::HEADER_X_FORWARDED_PROTO
);
}
}
/**
* registerRocketLoaderBypass protects rendered script tags from Rocket Loader interference.
*/
protected function registerRocketLoaderBypass()
{
$this->app[Kernel::class]->pushMiddleware(IgnoreRocketLoader::class);
}
}