RequestMatcher.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\HttpFoundation;
  11. /**
  12. * RequestMatcher compares a pre-defined set of checks against a Request instance.
  13. *
  14. * @author Fabien Potencier <fabien@symfony.com>
  15. */
  16. class RequestMatcher implements RequestMatcherInterface
  17. {
  18. protected $path;
  19. protected $host;
  20. protected $methods;
  21. protected $ip;
  22. protected $attributes;
  23. public function __construct($path = null, $host = null, $methods = null, $ip = null, array $attributes = array())
  24. {
  25. $this->path = $path;
  26. $this->host = $host;
  27. $this->methods = $methods;
  28. $this->ip = $ip;
  29. $this->attributes = $attributes;
  30. }
  31. /**
  32. * Adds a check for the URL host name.
  33. *
  34. * @param string $regexp A Regexp
  35. */
  36. public function matchHost($regexp)
  37. {
  38. $this->host = $regexp;
  39. }
  40. /**
  41. * Adds a check for the URL path info.
  42. *
  43. * @param string $regexp A Regexp
  44. */
  45. public function matchPath($regexp)
  46. {
  47. $this->path = $regexp;
  48. }
  49. /**
  50. * Adds a check for the client IP.
  51. *
  52. * @param string $ip A specific IP address or a range specified using IP/netmask like 192.168.1.0/24
  53. */
  54. public function matchIp($ip)
  55. {
  56. $this->ip = $ip;
  57. }
  58. /**
  59. * Adds a check for the HTTP method.
  60. *
  61. * @param string|array An HTTP method or an array of HTTP methods
  62. */
  63. public function matchMethod($method)
  64. {
  65. $this->methods = array_map(
  66. function ($m)
  67. {
  68. return strtolower($m);
  69. },
  70. is_array($method) ? $method : array($method)
  71. );
  72. }
  73. /**
  74. * Adds a check for request attribute.
  75. *
  76. * @param string $key The request attribute name
  77. * @param string $regexp A Regexp
  78. */
  79. public function matchAttribute($key, $regexp)
  80. {
  81. $this->attributes[$key] = $regexp;
  82. }
  83. /**
  84. * {@inheritdoc}
  85. */
  86. public function matches(Request $request)
  87. {
  88. if (null !== $this->methods && !in_array(strtolower($request->getMethod()), $this->methods)) {
  89. return false;
  90. }
  91. foreach ($this->attributes as $key => $pattern) {
  92. if (!preg_match('#'.str_replace('#', '\\#', $pattern).'#', $request->attributes->get($key))) {
  93. return false;
  94. }
  95. }
  96. if (null !== $this->path && !preg_match('#'.str_replace('#', '\\#', $this->path).'#', $request->getPathInfo())) {
  97. return false;
  98. }
  99. if (null !== $this->host && !preg_match('#'.str_replace('#', '\\#', $this->host).'#', $request->getHost())) {
  100. return false;
  101. }
  102. if (null !== $this->ip && !$this->checkIp($request->getClientIp())) {
  103. return false;
  104. }
  105. return true;
  106. }
  107. protected function checkIp($ip)
  108. {
  109. if (false !== strpos($this->ip, '/')) {
  110. list($address, $netmask) = explode('/', $this->ip);
  111. if ($netmask < 1 || $netmask > 32) {
  112. return false;
  113. }
  114. } else {
  115. $address = $this->ip;
  116. $netmask = 32;
  117. }
  118. return 0 === substr_compare(sprintf('%032b', ip2long($ip)), sprintf('%032b', ip2long($address)), 0, $netmask);
  119. }
  120. }