1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* This file is part of the Shieldon package. |
4
|
|
|
* |
5
|
|
|
* (c) Terry L. <[email protected]> |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
8
|
|
|
* file that was distributed with this source code. |
9
|
|
|
* |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
declare(strict_types=1); |
13
|
|
|
|
14
|
|
|
namespace Shieldon\Firewall\Integration; |
15
|
|
|
|
16
|
|
|
use Psr\Http\Message\ServerRequestInterface as Request; |
17
|
|
|
use Shieldon\Firewall\Firewall; |
18
|
|
|
use Shieldon\Firewall\HttpResolver; |
19
|
|
|
use function storage_path; |
|
|
|
|
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Middleware for Laravel framework (5.x - 6.x) |
23
|
|
|
*/ |
24
|
|
|
class Laravel |
25
|
|
|
{ |
26
|
|
|
/** |
27
|
|
|
* The absolute path of the storage where stores Shieldon generated data. |
28
|
|
|
* |
29
|
|
|
* @var string |
30
|
|
|
*/ |
31
|
|
|
protected $storage; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* The entry point of Shieldon Firewall's control panel. |
35
|
|
|
* |
36
|
|
|
* For example: https://yoursite.com/firewall/panel/ |
37
|
|
|
* Just use the path component of a URI. |
38
|
|
|
* |
39
|
|
|
* @var string |
40
|
|
|
*/ |
41
|
|
|
protected $panelUri; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Constructor. |
45
|
|
|
* |
46
|
|
|
* @param string $storage See property `storage` explanation. |
47
|
|
|
* @param string $panelUri See property `panelUri` explanation. |
48
|
|
|
* |
49
|
|
|
* @return void |
50
|
|
|
*/ |
51
|
|
|
public function __construct(string $storage = '', string $panelUri = '') |
52
|
|
|
{ |
53
|
|
|
// The Shieldon generated data is stored at that place. |
54
|
|
|
$this->storage = storage_path('shieldon'); |
|
|
|
|
55
|
|
|
$this->panelUri = '/firewall/panel/'; |
56
|
|
|
|
57
|
|
|
if ('' !== $storage) { |
58
|
|
|
$this->storage = $storage; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
if ('' !== $panelUri) { |
62
|
|
|
$this->panelUri = $panelUri; |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Handle an incoming request. |
68
|
|
|
* |
69
|
|
|
* @param Request $request |
70
|
|
|
* @param Closure $next |
71
|
|
|
* @return mixed |
72
|
|
|
*/ |
73
|
|
|
public function handle(Request $request, Closure $next) |
|
|
|
|
74
|
|
|
{ |
75
|
|
|
$firewall = new Firewall($request); |
76
|
|
|
$firewall->configure($this->storage); |
77
|
|
|
$firewall->controlPanel($this->panelUri); |
78
|
|
|
|
79
|
|
|
// Pass Laravel CSRF Token to Captcha form. |
80
|
|
|
$firewall->getKernel()->setCaptcha( |
81
|
|
|
new \Shieldon\Captcha\Csrf([ |
|
|
|
|
82
|
|
|
'name' => '_token', |
83
|
|
|
'value' => csrf_token(), |
|
|
|
|
84
|
|
|
]) |
85
|
|
|
); |
86
|
|
|
|
87
|
|
|
$response = $firewall->run(); |
88
|
|
|
|
89
|
|
|
if ($response->getStatusCode() !== 200) { |
90
|
|
|
$httpResolver = new HttpResolver(); |
91
|
|
|
$httpResolver($response); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
return $next($request); |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
|