|
1
|
|
|
<?php namespace GeneaLabs\LaravelCaffeine\Http\Middleware; |
|
2
|
|
|
|
|
3
|
|
|
use Closure; |
|
4
|
|
|
use GeneaLabs\LaravelCaffeine\Dripper; |
|
5
|
|
|
use GeneaLabs\LaravelCaffeine\Http\Response; |
|
|
|
|
|
|
6
|
|
|
use Illuminate\Http\Request; |
|
7
|
|
|
|
|
8
|
|
|
class LaravelCaffeineDripMiddleware |
|
9
|
|
|
{ |
|
10
|
|
|
public function handle(Request $request, Closure $next) |
|
11
|
|
|
{ |
|
12
|
|
|
$response = $next($request); |
|
13
|
|
|
|
|
14
|
|
|
$content = $response->getContent(); |
|
15
|
|
|
|
|
16
|
|
|
if (! is_string($content) || strlen(trim($content)) === 0) { |
|
17
|
|
|
return $response; |
|
18
|
|
|
} |
|
19
|
|
|
|
|
20
|
|
|
$shouldDripRegexp = $this->makeRegex([ |
|
21
|
|
|
'<meta\s+', |
|
22
|
|
|
'(name\s*=\s*[\'"]caffeinated[\'"]\s+content\s*=\s*[\'"]false[\'"]', |
|
23
|
|
|
'|content\s*=\s*[\'"]false[\'"]\s+name\s*=\s*[\'"]caffeinated[\'"])', |
|
24
|
|
|
]); |
|
25
|
|
|
|
|
26
|
|
|
$shouldNotDrip = preg_match($shouldDripRegexp, $content); |
|
27
|
|
|
|
|
28
|
|
|
if ($shouldNotDrip) { |
|
29
|
|
|
return $response; |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
$formTokenRegexp = $this->makeRegex([ |
|
33
|
|
|
"<input.*?name\s*=\s*[\'\"]_token[\'\"]", |
|
34
|
|
|
]); |
|
35
|
|
|
$metaTokenRegexp = $this->makeRegex([ |
|
36
|
|
|
"<meta.*?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
|
|
|
|
|
52
|
|
|
$this->setContents($response, $content); |
|
53
|
|
|
|
|
54
|
|
|
return $response; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
protected function makeRegex(array $regexp) : string |
|
58
|
|
|
{ |
|
59
|
|
|
return '/' . implode('', $regexp) . '/'; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* @param $response |
|
64
|
|
|
* @param $content |
|
65
|
|
|
*/ |
|
66
|
|
|
protected function setContents($response, $content): void |
|
67
|
|
|
{ |
|
68
|
|
|
$original = $response->original; |
|
69
|
|
|
|
|
70
|
|
|
$response->setContent($content); |
|
71
|
|
|
|
|
72
|
|
|
$response->original = $original; |
|
73
|
|
|
} |
|
74
|
|
|
} |
|
75
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths