1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types = 1); |
4
|
|
|
|
5
|
|
|
namespace Protoqol\Prequel\Http\Middleware; |
6
|
|
|
|
7
|
|
|
use Closure; |
8
|
|
|
use Illuminate\Support\Facades\DB; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Class Authorised |
12
|
|
|
* |
13
|
|
|
* @package Protoqol\Prequel\Http\Middleware |
14
|
|
|
*/ |
15
|
|
|
class Authorised |
16
|
|
|
{ |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Handle an incoming request. |
20
|
|
|
* Checks if Prequel is enabled and has a valid database connection. |
21
|
|
|
* |
22
|
|
|
* @param \Illuminate\Http\Request $request |
23
|
|
|
* @param \Closure $next |
24
|
|
|
* |
25
|
|
|
* @return mixed |
26
|
|
|
*/ |
27
|
|
|
public function handle($request, Closure $next) |
28
|
|
|
{ |
29
|
|
|
if (!$this->configurationCheck()->enabled) { |
30
|
|
|
return view('Prequel::error', [ |
31
|
|
|
'error_detailed' => $this->configurationCheck()->detailed, |
32
|
|
|
'http_code' => 403, |
33
|
|
|
'env' => [ |
34
|
|
|
'connection' => 'protected', |
35
|
|
|
'database' => 'protected', |
36
|
|
|
'host' => 'protected', |
37
|
|
|
'port' => 'protected', |
38
|
|
|
'user' => 'protected', |
39
|
|
|
], |
40
|
|
|
]); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
if (!$this->databaseConnectionCheck()->connected) { |
44
|
|
|
return view('Prequel::error', [ |
45
|
|
|
'error_detailed' => $this->databaseConnectionCheck()->detailed, |
46
|
|
|
'http_code' => 503, |
47
|
|
|
'env' => [ |
48
|
|
|
'connection' => config('app.default'), |
49
|
|
|
'database' => config('app.mysql.database'), |
50
|
|
|
'host' => config('app.mysql.host'), |
51
|
|
|
'port' => config('app.mysql.port'), |
52
|
|
|
'user' => config('app.mysql.user'), |
53
|
|
|
], |
54
|
|
|
]); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
return $next($request); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Check connection with database |
62
|
|
|
* |
63
|
|
|
* @return object |
64
|
|
|
*/ |
65
|
|
|
private function databaseConnectionCheck() |
66
|
|
|
{ |
67
|
|
|
$connection = []; |
|
|
|
|
68
|
|
|
|
69
|
|
|
try { |
70
|
|
|
$connection = [ |
71
|
|
|
'connected' => (bool) DB::connection()->getPdo(), |
72
|
|
|
'detailed' => DB::connection()->getPdo(), |
73
|
|
|
]; |
74
|
|
|
} catch (\Exception $exception) { |
75
|
|
|
$connection = [ |
76
|
|
|
'connected' => false, |
77
|
|
|
'detailed' => 'No valid database connection', |
78
|
|
|
]; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
return (object) $connection; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Check if Prequel is enabled and/or in development |
86
|
|
|
* |
87
|
|
|
* @return object |
88
|
|
|
*/ |
89
|
|
|
private function configurationCheck() |
90
|
|
|
{ |
91
|
|
|
return (object) [ |
92
|
|
|
'enabled' => (config('prequel.enabled') |
93
|
|
|
&& config('app.env') !== 'production'), |
94
|
|
|
'detailed' => 'Prequel has been disabled.', |
95
|
|
|
]; |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
|