|
1
|
|
|
<?php namespace GeneaLabs\LaravelCaffeine\Http\Middleware; |
|
2
|
|
|
|
|
3
|
|
|
use Closure; |
|
4
|
|
|
use GeneaLabs\LaravelCaffeine\Dripper; |
|
5
|
|
|
use Illuminate\Http\Request; |
|
6
|
|
|
|
|
7
|
|
|
class LaravelCaffeineDripMiddleware |
|
8
|
|
|
{ |
|
9
|
|
|
public function handle(Request $request, Closure $next) |
|
10
|
|
|
{ |
|
11
|
|
|
$response = $next($request); |
|
12
|
|
|
|
|
13
|
|
|
$content = $response->getContent(); |
|
14
|
|
|
|
|
15
|
|
|
if (! is_string($content)) { |
|
16
|
|
|
return $response; |
|
17
|
|
|
} |
|
18
|
|
|
|
|
19
|
|
|
$shouldDripRegexp = $this->makeRegex([ |
|
20
|
|
|
'<meta\s+', |
|
21
|
|
|
'(name\s*=\s*[\'"]caffeinated[\'"]\s+content\s*=\s*[\'"]false[\'"]', |
|
22
|
|
|
'|content\s*=\s*[\'"]false[\'"]\s+name\s*=\s*[\'"]caffeinated[\'"])', |
|
23
|
|
|
]); |
|
24
|
|
|
|
|
25
|
|
|
$shouldNotDrip = preg_match($shouldDripRegexp, $content); |
|
26
|
|
|
|
|
27
|
|
|
if ($shouldNotDrip) { |
|
|
|
|
|
|
28
|
|
|
return $response; |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
$formTokenRegexp = $this->makeRegex([ |
|
32
|
|
|
'<input([^>]*?[\n]?)*[^>]*?name\s*=\s*[\'"]_token[\'"]', |
|
33
|
|
|
]); |
|
34
|
|
|
$metaTokenRegexp = $this->makeRegex([ |
|
35
|
|
|
'<meta\s+', |
|
36
|
|
|
'name\s*=\s*[\'"]csrf[_-]token[\'"]', |
|
37
|
|
|
]); |
|
38
|
|
|
$hasNoFormToken = ! preg_match($formTokenRegexp, $content); |
|
39
|
|
|
$hasNoMetaToken = ! preg_match($metaTokenRegexp, $content); |
|
40
|
|
|
|
|
41
|
|
|
if ($hasNoFormToken && $hasNoMetaToken) { |
|
42
|
|
|
return $response; |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
$dripper = (new Dripper); |
|
46
|
|
|
$content = str_replace( |
|
47
|
|
|
'</body>', |
|
48
|
|
|
"{$dripper->html}</body>", |
|
|
|
|
|
|
49
|
|
|
$content |
|
50
|
|
|
); |
|
51
|
|
|
$response->setContent($content); |
|
52
|
|
|
|
|
53
|
|
|
return $response; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
protected function makeRegex(array $regexp) : string |
|
57
|
|
|
{ |
|
58
|
|
|
return '/' . implode('', $regexp) . '/'; |
|
59
|
|
|
} |
|
60
|
|
|
} |
|
61
|
|
|
|
In PHP, under loose comparison (like
==, or!=, orswitchconditions), values of different types might be equal.For
integervalues, zero is a special case, in particular the following results might be unexpected: