1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the FOSHttpCache package. |
5
|
|
|
* |
6
|
|
|
* (c) FriendsOfSymfony <http://friendsofsymfony.github.com/> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace FOS\HttpCache\UserContext; |
13
|
|
|
|
14
|
|
|
use Symfony\Component\HttpFoundation\Request; |
15
|
|
|
use Symfony\Component\HttpFoundation\RequestMatcherInterface; |
16
|
|
|
use Symfony\Component\HttpKernel\Kernel; |
17
|
|
|
use Symfony\Component\OptionsResolver\OptionsResolver; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Matches anonymous requests using a list of identification headers. |
21
|
|
|
*/ |
22
|
|
|
class AnonymousRequestMatcher implements RequestMatcherInterface |
23
|
|
|
{ |
24
|
|
|
/** |
25
|
|
|
* @var array |
26
|
|
|
*/ |
27
|
|
|
private $options; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @param array $options Configuration for the matcher. All options are required because this matcher is usually |
31
|
|
|
* created by the UserContextSubscriber which provides the default values. |
32
|
|
|
* |
33
|
|
|
* @throws \InvalidArgumentException if unknown keys are found in $options |
34
|
|
|
*/ |
35
|
|
|
public function __construct(array $options = array()) |
36
|
|
|
{ |
37
|
|
|
$resolver = new OptionsResolver(); |
38
|
|
|
$resolver->setRequired(array('user_identifier_headers', 'session_name_prefix')); |
39
|
|
|
|
40
|
|
|
// actually string[] but that is not supported by symfony < 3.4 |
41
|
|
|
$resolver->setAllowedTypes('user_identifier_headers', ['array']); |
42
|
|
|
$resolver->setAllowedTypes('session_name_prefix', ['string', 'boolean']); |
43
|
|
|
|
44
|
|
|
$this->options = $resolver->resolve($options); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
public function matches(Request $request) |
48
|
|
|
{ |
49
|
|
|
// You might have to enable rewriting of the Authorization header in your server config or .htaccess: |
50
|
|
|
// RewriteEngine On |
51
|
|
|
// RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}] |
52
|
|
|
foreach ($this->options['user_identifier_headers'] as $header) { |
53
|
|
|
if ($request->headers->has($header)) { |
54
|
|
|
return false; |
55
|
|
|
} |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
if ($this->options['session_name_prefix']) { |
59
|
|
|
foreach ($request->cookies as $name => $value) { |
60
|
|
|
if (0 === strpos($name, $this->options['session_name_prefix'])) { |
61
|
|
|
return false; |
62
|
|
|
} |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
return true; |
67
|
|
|
} |
68
|
|
|
} |
69
|
|
|
|