|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Spatie\CookieConsent; |
|
4
|
|
|
|
|
5
|
|
|
use Closure; |
|
6
|
|
|
use Illuminate\Http\Response; |
|
7
|
|
|
|
|
8
|
|
|
class CookieConsentMiddleware |
|
9
|
|
|
{ |
|
10
|
|
|
public function handle($request, Closure $next) |
|
11
|
|
|
{ |
|
12
|
|
|
$response = $next($request); |
|
13
|
|
|
|
|
14
|
|
|
if (! config('cookie-consent.enabled')) { |
|
15
|
|
|
return $response; |
|
16
|
|
|
} |
|
17
|
|
|
|
|
18
|
|
|
if (! $response instanceof Response) { |
|
19
|
|
|
return $response; |
|
20
|
|
|
} |
|
21
|
|
|
|
|
22
|
|
|
if ($this->excluded($request)) { |
|
23
|
|
|
return $response; |
|
24
|
|
|
} |
|
25
|
|
|
|
|
26
|
|
|
if (! $this->containsBodyTag($response)) { |
|
27
|
|
|
return $response; |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
return $this->addCookieConsentScriptToResponse($response); |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* Excluded routes. |
|
35
|
|
|
* |
|
36
|
|
|
* @param $request |
|
37
|
|
|
* @return bool |
|
38
|
|
|
*/ |
|
39
|
|
|
protected function excluded($request): bool |
|
40
|
|
|
{ |
|
41
|
|
|
return in_array($request->route()->getName(), config('cookie-consent.excluded') ?: []); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
protected function containsBodyTag(Response $response): bool |
|
45
|
|
|
{ |
|
46
|
|
|
return $this->getLastClosingBodyTagPosition($response->getContent()) !== false; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* @param \Illuminate\Http\Response $response |
|
51
|
|
|
* |
|
52
|
|
|
* @return $this |
|
53
|
|
|
*/ |
|
54
|
|
|
protected function addCookieConsentScriptToResponse(Response $response) |
|
55
|
|
|
{ |
|
56
|
|
|
$content = $response->getContent(); |
|
57
|
|
|
|
|
58
|
|
|
$closingBodyTagPosition = $this->getLastClosingBodyTagPosition($content); |
|
59
|
|
|
|
|
60
|
|
|
$content = '' |
|
61
|
|
|
.substr($content, 0, $closingBodyTagPosition) |
|
62
|
|
|
.view('cookieConsent::index')->render() |
|
|
|
|
|
|
63
|
|
|
.substr($content, $closingBodyTagPosition); |
|
64
|
|
|
|
|
65
|
|
|
return $response->setContent($content); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
protected function getLastClosingBodyTagPosition(string $content = '') |
|
69
|
|
|
{ |
|
70
|
|
|
return strripos($content, '</body>'); |
|
71
|
|
|
} |
|
72
|
|
|
} |
|
73
|
|
|
|
It seems like the method you are trying to call exists only in some of the possible types.
Let’s take a look at an example:
Available Fixes
Add an additional type-check:
Only allow a single type to be passed if the variable comes from a parameter: