Subversion Repositories php-qbpwcf

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
3 liveuser 1
<?php
2
 
3
/*
4
 * This file is part of the Symfony package.
5
 *
6
 * (c) Fabien Potencier <fabien@symfony.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 Symfony\Component\HttpFoundation;
13
 
14
/**
15
 * RequestMatcher compares a pre-defined set of checks against a Request instance.
16
 *
17
 * @author Fabien Potencier <fabien@symfony.com>
18
 */
19
class RequestMatcher implements RequestMatcherInterface
20
{
21
    /**
22
     * @var string|null
23
     */
24
    private $path;
25
 
26
    /**
27
     * @var string|null
28
     */
29
    private $host;
30
 
31
    /**
32
     * @var int|null
33
     */
34
    private $port;
35
 
36
    /**
37
     * @var string[]
38
     */
39
    private $methods = [];
40
 
41
    /**
42
     * @var string[]
43
     */
44
    private $ips = [];
45
 
46
    /**
47
     * @var array
48
     */
49
    private $attributes = [];
50
 
51
    /**
52
     * @var string[]
53
     */
54
    private $schemes = [];
55
 
56
    /**
57
     * @param string|string[]|null $methods
58
     * @param string|string[]|null $ips
59
     * @param string|string[]|null $schemes
60
     */
61
    public function __construct(string $path = null, string $host = null, $methods = null, $ips = null, array $attributes = [], $schemes = null, int $port = null)
62
    {
63
        $this->matchPath($path);
64
        $this->matchHost($host);
65
        $this->matchMethod($methods);
66
        $this->matchIps($ips);
67
        $this->matchScheme($schemes);
68
        $this->matchPort($port);
69
 
70
        foreach ($attributes as $k => $v) {
71
            $this->matchAttribute($k, $v);
72
        }
73
    }
74
 
75
    /**
76
     * Adds a check for the HTTP scheme.
77
     *
78
     * @param string|string[]|null $scheme An HTTP scheme or an array of HTTP schemes
79
     */
80
    public function matchScheme($scheme)
81
    {
82
        $this->schemes = null !== $scheme ? array_map('strtolower', (array) $scheme) : [];
83
    }
84
 
85
    /**
86
     * Adds a check for the URL host name.
87
     */
88
    public function matchHost(?string $regexp)
89
    {
90
        $this->host = $regexp;
91
    }
92
 
93
    /**
94
     * Adds a check for the the URL port.
95
     *
96
     * @param int|null $port The port number to connect to
97
     */
98
    public function matchPort(?int $port)
99
    {
100
        $this->port = $port;
101
    }
102
 
103
    /**
104
     * Adds a check for the URL path info.
105
     */
106
    public function matchPath(?string $regexp)
107
    {
108
        $this->path = $regexp;
109
    }
110
 
111
    /**
112
     * Adds a check for the client IP.
113
     *
114
     * @param string $ip A specific IP address or a range specified using IP/netmask like 192.168.1.0/24
115
     */
116
    public function matchIp(string $ip)
117
    {
118
        $this->matchIps($ip);
119
    }
120
 
121
    /**
122
     * Adds a check for the client IP.
123
     *
124
     * @param string|string[]|null $ips A specific IP address or a range specified using IP/netmask like 192.168.1.0/24
125
     */
126
    public function matchIps($ips)
127
    {
128
        $this->ips = null !== $ips ? (array) $ips : [];
129
    }
130
 
131
    /**
132
     * Adds a check for the HTTP method.
133
     *
134
     * @param string|string[]|null $method An HTTP method or an array of HTTP methods
135
     */
136
    public function matchMethod($method)
137
    {
138
        $this->methods = null !== $method ? array_map('strtoupper', (array) $method) : [];
139
    }
140
 
141
    /**
142
     * Adds a check for request attribute.
143
     */
144
    public function matchAttribute(string $key, string $regexp)
145
    {
146
        $this->attributes[$key] = $regexp;
147
    }
148
 
149
    /**
150
     * {@inheritdoc}
151
     */
152
    public function matches(Request $request)
153
    {
154
        if ($this->schemes && !\in_array($request->getScheme(), $this->schemes, true)) {
155
            return false;
156
        }
157
 
158
        if ($this->methods && !\in_array($request->getMethod(), $this->methods, true)) {
159
            return false;
160
        }
161
 
162
        foreach ($this->attributes as $key => $pattern) {
163
            if (!preg_match('{'.$pattern.'}', $request->attributes->get($key))) {
164
                return false;
165
            }
166
        }
167
 
168
        if (null !== $this->path && !preg_match('{'.$this->path.'}', rawurldecode($request->getPathInfo()))) {
169
            return false;
170
        }
171
 
172
        if (null !== $this->host && !preg_match('{'.$this->host.'}i', $request->getHost())) {
173
            return false;
174
        }
175
 
176
        if (null !== $this->port && 0 < $this->port && $request->getPort() !== $this->port) {
177
            return false;
178
        }
179
 
180
        if (IpUtils::checkIp($request->getClientIp(), $this->ips)) {
181
            return true;
182
        }
183
 
184
        // Note to future implementors: add additional checks above the
185
        // foreach above or else your check might not be run!
186
        return 0 === \count($this->ips);
187
    }
188
}