2010-09-01 12:56:06 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This file is part of the Symfony package.
|
|
|
|
*
|
2011-03-06 11:40:06 +00:00
|
|
|
* (c) Fabien Potencier <fabien@symfony.com>
|
2010-09-01 12:56:06 +01:00
|
|
|
*
|
|
|
|
* For the full copyright and license information, please view the LICENSE
|
|
|
|
* file that was distributed with this source code.
|
|
|
|
*/
|
|
|
|
|
2011-01-15 13:29:43 +00:00
|
|
|
namespace Symfony\Component\HttpFoundation;
|
|
|
|
|
2010-09-01 12:56:06 +01:00
|
|
|
/**
|
|
|
|
* RequestMatcher compares a pre-defined set of checks against a Request instance.
|
|
|
|
*
|
2011-03-06 11:40:06 +00:00
|
|
|
* @author Fabien Potencier <fabien@symfony.com>
|
2011-07-20 09:06:02 +01:00
|
|
|
*
|
|
|
|
* @api
|
2010-09-01 12:56:06 +01:00
|
|
|
*/
|
|
|
|
class RequestMatcher implements RequestMatcherInterface
|
|
|
|
{
|
2011-11-02 15:42:51 +00:00
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
*/
|
2011-05-28 21:37:43 +01:00
|
|
|
private $path;
|
2011-12-18 13:33:54 +00:00
|
|
|
|
2011-11-02 15:42:51 +00:00
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
*/
|
2011-05-28 21:37:43 +01:00
|
|
|
private $host;
|
2011-12-18 13:33:54 +00:00
|
|
|
|
2011-11-02 15:42:51 +00:00
|
|
|
/**
|
2012-06-25 17:21:41 +01:00
|
|
|
* @var array
|
2011-11-02 15:42:51 +00:00
|
|
|
*/
|
2012-11-12 12:39:12 +00:00
|
|
|
private $methods = array();
|
2011-12-18 13:33:54 +00:00
|
|
|
|
2011-11-02 15:42:51 +00:00
|
|
|
/**
|
2011-12-18 13:33:54 +00:00
|
|
|
* @var string
|
2011-11-02 15:42:51 +00:00
|
|
|
*/
|
2011-05-28 21:37:43 +01:00
|
|
|
private $ip;
|
2011-12-18 13:33:54 +00:00
|
|
|
|
2011-11-02 15:42:51 +00:00
|
|
|
/**
|
2011-12-18 13:33:54 +00:00
|
|
|
* @var array
|
2011-11-02 15:42:51 +00:00
|
|
|
*/
|
2012-11-12 12:39:12 +00:00
|
|
|
private $attributes = array();
|
2011-01-21 10:00:52 +00:00
|
|
|
|
2012-11-12 12:39:12 +00:00
|
|
|
/**
|
|
|
|
* @param string|null $path
|
|
|
|
* @param string|null $host
|
|
|
|
* @param string|string[]|null $methods
|
|
|
|
* @param string|null $ip
|
|
|
|
* @param array $attributes
|
|
|
|
*/
|
2011-01-21 10:00:52 +00:00
|
|
|
public function __construct($path = null, $host = null, $methods = null, $ip = null, array $attributes = array())
|
|
|
|
{
|
2012-11-12 12:39:12 +00:00
|
|
|
$this->matchPath($path);
|
|
|
|
$this->matchHost($host);
|
|
|
|
$this->matchMethod($methods);
|
|
|
|
$this->matchIp($ip);
|
|
|
|
foreach ($attributes as $k => $v) {
|
|
|
|
$this->matchAttribute($k, $v);
|
|
|
|
}
|
2011-01-21 10:00:52 +00:00
|
|
|
}
|
2010-09-01 12:56:06 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Adds a check for the URL host name.
|
|
|
|
*
|
|
|
|
* @param string $regexp A Regexp
|
|
|
|
*/
|
|
|
|
public function matchHost($regexp)
|
|
|
|
{
|
|
|
|
$this->host = $regexp;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Adds a check for the URL path info.
|
|
|
|
*
|
|
|
|
* @param string $regexp A Regexp
|
|
|
|
*/
|
|
|
|
public function matchPath($regexp)
|
|
|
|
{
|
|
|
|
$this->path = $regexp;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Adds a check for the client IP.
|
|
|
|
*
|
|
|
|
* @param string $ip A specific IP address or a range specified using IP/netmask like 192.168.1.0/24
|
|
|
|
*/
|
|
|
|
public function matchIp($ip)
|
|
|
|
{
|
|
|
|
$this->ip = $ip;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Adds a check for the HTTP method.
|
|
|
|
*
|
2012-11-12 12:39:12 +00:00
|
|
|
* @param string|string[]|null $method An HTTP method or an array of HTTP methods
|
2010-09-01 12:56:06 +01:00
|
|
|
*/
|
|
|
|
public function matchMethod($method)
|
|
|
|
{
|
2012-11-12 12:39:12 +00:00
|
|
|
$this->methods = array_map('strtoupper', (array) $method);
|
2010-09-01 12:56:06 +01:00
|
|
|
}
|
|
|
|
|
2010-10-05 19:20:28 +01:00
|
|
|
/**
|
|
|
|
* Adds a check for request attribute.
|
|
|
|
*
|
|
|
|
* @param string $key The request attribute name
|
|
|
|
* @param string $regexp A Regexp
|
|
|
|
*/
|
|
|
|
public function matchAttribute($key, $regexp)
|
|
|
|
{
|
|
|
|
$this->attributes[$key] = $regexp;
|
|
|
|
}
|
|
|
|
|
2010-09-01 12:56:06 +01:00
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
2011-07-20 09:06:02 +01:00
|
|
|
*
|
|
|
|
* @api
|
2010-09-01 12:56:06 +01:00
|
|
|
*/
|
|
|
|
public function matches(Request $request)
|
|
|
|
{
|
2012-11-12 12:39:12 +00:00
|
|
|
if ($this->methods && !in_array($request->getMethod(), $this->methods)) {
|
2010-09-01 12:56:06 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2010-10-05 19:20:28 +01:00
|
|
|
foreach ($this->attributes as $key => $pattern) {
|
2011-03-23 20:27:04 +00:00
|
|
|
if (!preg_match('#'.str_replace('#', '\\#', $pattern).'#', $request->attributes->get($key))) {
|
2010-10-05 19:20:28 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-05-28 21:37:43 +01:00
|
|
|
if (null !== $this->path) {
|
2011-07-19 15:21:58 +01:00
|
|
|
$path = str_replace('#', '\\#', $this->path);
|
2011-05-28 21:37:43 +01:00
|
|
|
|
2012-03-19 14:59:52 +00:00
|
|
|
if (!preg_match('#'.$path.'#', rawurldecode($request->getPathInfo()))) {
|
2011-05-28 21:37:43 +01:00
|
|
|
return false;
|
|
|
|
}
|
2010-09-01 12:56:06 +01:00
|
|
|
}
|
|
|
|
|
2012-11-12 12:39:12 +00:00
|
|
|
if (null !== $this->host && !preg_match('#'.str_replace('#', '\\#', $this->host).'#i', $request->getHost())) {
|
2010-09-01 12:56:06 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-11-13 16:48:13 +00:00
|
|
|
if (null !== $this->ip && !IpUtils::checkIp($request->getClientIp(), $this->ip)) {
|
2010-09-01 12:56:06 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|