1 | <?php |
||
11 | abstract class Policy |
||
12 | { |
||
13 | protected $directives = []; |
||
14 | |||
15 | protected $reportOnly = false; |
||
16 | |||
17 | abstract public function configure(); |
||
18 | |||
19 | /** |
||
20 | * @param string $directive |
||
21 | * @param string|array $values |
||
22 | * |
||
23 | * @return \Spatie\Csp\Policies\Policy |
||
24 | * |
||
25 | * @throws \Spatie\Csp\Exceptions\InvalidDirective |
||
26 | */ |
||
27 | public function addDirective(string $directive, $values): self |
||
28 | { |
||
29 | $this->guardAgainstInvalidDirectives($directive); |
||
30 | |||
31 | $rules = array_flatten(array_map(function ($values) { |
||
32 | return empty($values) ? $values : array_filter(explode(' ', $values)); |
||
33 | }, $this->arr_wrap($values))); |
||
34 | |||
35 | foreach ($rules as $rule) { |
||
36 | $sanitizedValue = $this->sanitizeValue($rule); |
||
37 | |||
38 | if (! in_array($sanitizedValue, $this->directives[$directive] ?? [])) { |
||
39 | $this->directives[$directive][] = $sanitizedValue; |
||
40 | } |
||
41 | } |
||
42 | |||
43 | return $this; |
||
44 | } |
||
45 | |||
46 | public function reportOnly(): self |
||
52 | |||
53 | public function enforce(): self |
||
59 | |||
60 | public function reportTo(string $uri): self |
||
66 | |||
67 | public function shouldBeApplied(Request $request, Response $response): bool |
||
71 | |||
72 | public function addNonceForDirective(string $directive): self |
||
76 | |||
77 | public function applyTo(Response $response) |
||
91 | |||
92 | public function __toString() |
||
93 | { |
||
94 | return collect($this->directives) |
||
95 | ->map(function (array $values, string $directive) { |
||
96 | $valueString = implode(' ', $values); |
||
97 | |||
98 | return empty($valueString) ? "{$directive}" : "{$directive} {$valueString}"; |
||
99 | }) |
||
100 | ->implode(';'); |
||
101 | } |
||
102 | |||
103 | protected function guardAgainstInvalidDirectives(string $directive) |
||
109 | |||
110 | protected function isHash(string $value): bool |
||
120 | |||
121 | protected function isSpecialDirective(string $value): bool |
||
134 | |||
135 | protected function sanitizeValue(string $value): string |
||
143 | |||
144 | protected function arr_wrap($value) { |
||
145 | return ! is_array($value) ? [$value] : $value; |
||
147 | } |
||
148 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.