-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDynamicDebug.php
199 lines (176 loc) · 6.21 KB
/
DynamicDebug.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
<?php
namespace SquirrelForge\Laravel\CoreSupport\Http\Middleware;
use Closure;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Cookie;
use Illuminate\Support\Facades\Config;
use SquirrelForge\Laravel\CoreSupport\Exceptions\DynamicDebugAlreadyActiveException;
use SquirrelForge\Laravel\CoreSupport\Exceptions\DynamicDebugActivatorOriginInvalidException;
use SquirrelForge\Laravel\CoreSupport\Exceptions\DynamicDebugInvalidActivatorsException;
use SquirrelForge\Laravel\CoreSupport\Exceptions\DynamicDebugUnknownActivatorException;
use Symfony\Component\HttpFoundation\IpUtils;
use function SquirrelForge\Laravel\CoreSupport\clientIp;
/**
* Dynamic debug middleware.
*/
class DynamicDebug {
/** @var bool $activated */
protected bool $activated = false;
/** @var null|string $origin */
protected ?string $origin = null;
/** @var null|array $config */
protected ?array $config;
/**
* Constructor.
*/
public function __construct()
{
$this->config = config('sqf-cs.debug');
}
/**
* Handle an incoming request.
* @param Request $request
* @param Closure $next
* @return mixed
* @throws DynamicDebugUnknownActivatorException
* @throws DynamicDebugInvalidActivatorsException
*/
public function handle(Request $request, Closure $next)
{
// Only check if debug is disabled and config is available
if (!config('app.debug') && !empty($this->config)) {
// Requires configured activators
if (!isset($this->config['use']) || !is_array($this->config['use'])) {
throw new DynamicDebugInvalidActivatorsException('No valid activators defined');
}
// Loop activators
foreach ($this->config['use'] as $name) {
// Run available types
if ($name instanceof Closure || is_string($name) && function_exists($name)) {
call_user_func($name, $request, $this);
} else if (method_exists($this, 'activate_with_' . $name)) {
call_user_func([$this, 'activate_with_' . $name], $request);
} else {
$info = (is_string($name) || is_numeric($name) ? '"' . $name . '"' : 'unknown') .
'[' . gettype($name) . ']';
throw new DynamicDebugUnknownActivatorException( 'Unknown activator: ' . $info );
}
// Until one activates
if ($this->activated) {
Config::set('app.debug', true);
break;
}
}
// Check internal debug logging
if ($this->config['log']) {
Log::info('sqf-cs::dynamic-debug(' . ($this->activated ? 'true' : 'false') . ')' .
($this->activated ? ' origin "' . $this->origin . '"' : ''));
}
}
// Continue
return $next($request);
}
/**
* Is activated status
* @return bool
*/
public function isActivated(): bool
{
return $this->activated;
}
/**
* Activate debug mode
* @param string $origin
* @return void
* @throws DynamicDebugAlreadyActiveException
* @throws DynamicDebugActivatorOriginInvalidException
*/
public function activate(string $origin): void
{
if (empty($origin)) {
throw new DynamicDebugActivatorOriginInvalidException('Failed with invalid activation origin');
}
if ($this->activated) {
throw new DynamicDebugAlreadyActiveException( 'Already activated by: ' . $this->origin );
}
$this->activated = true;
$this->origin = $origin;
}
/**
* Activate by ip match
* @param Request $request
* @return void
* @throws DynamicDebugActivatorOriginInvalidException
* @throws DynamicDebugAlreadyActiveException
*/
protected function activate_with_ip(Request $request): void
{
$ips = $this->config['ips'];
if (!empty($ips) && in_array(clientIp($request), $ips)) {
$this->activate('ip');
}
}
/**
* Activate by ip range match
* @param Request $request
* @return void
* @throws DynamicDebugActivatorOriginInvalidException
* @throws DynamicDebugAlreadyActiveException
*/
protected function activate_with_range(Request $request): void
{
$ranges = $this->config['ranges'];
if (!empty($ranges) && IpUtils::checkIp(clientIp($request), $ranges)) {
$this->activate('range');
}
}
/**
* Activate by key and pass values
* @param Request $request
* @return void
* @throws DynamicDebugActivatorOriginInvalidException
* @throws DynamicDebugAlreadyActiveException
*/
protected function activate_with_keypass(Request $request): void
{
$key = $this->config['key'];
$pass = $this->config['pass'];
$limit = $this->config['limit'];
// If active get input and process
if (!empty($key) && !empty($pass) && !empty($limit)) {
$input_pass = $request->get($key);
// Check if we need to create a debug cookie
// or if the timestamped encoded cookie/value pair is set and valid
if (!empty($input_pass) && $input_pass === $pass) {
$this->activate('keypass');
$this->createDebugCookie();
} else if ($this->hasDebugCookie()) {
$this->activate('cookie');
}
}
}
/**
* Check for a debug cookie
* @return bool
*/
public function hasDebugCookie(): bool
{
$today = date($this->config['lifetime']);
$key = $this->config['key'];
$pass = $this->config['pass'];
return Cookie::get(md5($today . $key)) === md5($today . $pass);
}
/**
* Create a debug cookie
* @return void
*/
public function createDebugCookie(): void
{
$today = date($this->config['lifetime']);
$key = $this->config['key'];
$pass = $this->config['pass'];
$limit = $this->config['limit'];
Cookie::queue(md5($today . $key), md5($today . $pass), $limit);
}
}