NoProxyPattern.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. <?php
  2. /*
  3. * This file is part of Composer.
  4. *
  5. * (c) Nils Adermann <naderman@naderman.de>
  6. * Jordi Boggiano <j.boggiano@seld.be>
  7. *
  8. * For the full copyright and license information, please view the LICENSE
  9. * file that was distributed with this source code.
  10. */
  11. namespace Composer\Util;
  12. /**
  13. * Tests URLs against no_proxy patterns.
  14. */
  15. class NoProxyPattern
  16. {
  17. /**
  18. * @var string[]
  19. */
  20. protected $rules = array();
  21. /**
  22. * @param string $pattern no_proxy pattern
  23. */
  24. public function __construct($pattern)
  25. {
  26. $this->rules = preg_split("/[\s,]+/", $pattern);
  27. }
  28. /**
  29. * Test a URL against the stored pattern.
  30. *
  31. * @param string $url
  32. *
  33. * @return true if the URL matches one of the rules.
  34. */
  35. public function test($url)
  36. {
  37. $host = parse_url($url, PHP_URL_HOST);
  38. $port = parse_url($url, PHP_URL_PORT);
  39. if (empty($port)) {
  40. switch (parse_url($url, PHP_URL_SCHEME)) {
  41. case 'http':
  42. $port = 80;
  43. break;
  44. case 'https':
  45. $port = 443;
  46. break;
  47. }
  48. }
  49. foreach ($this->rules as $rule) {
  50. $match = false;
  51. if ($rule == '*') {
  52. $match - true;
  53. } else {
  54. list($ruleHost) = explode(':', $rule);
  55. list($base) = explode('/', $ruleHost);
  56. if (filter_var($base, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4)) {
  57. // ip or cidr match
  58. if (!isset($ip)) {
  59. $ip = gethostbyname($host);
  60. }
  61. if (strpos($ruleHost, '/') === false) {
  62. $match = $ip === $ruleHost;
  63. } else {
  64. $match = self::inCIDRBlock($ruleHost, $ip);
  65. }
  66. } else {
  67. // match end of domain
  68. $haystack = '.' . trim($host, '.') . '.';
  69. $needle = '.'. trim($ruleHost, '.') .'.';
  70. $match = stripos(strrev($haystack), strrev($needle)) === 0;
  71. }
  72. // final port check
  73. if ($match && strpos($rule, ':') !== false) {
  74. list(, $rulePort) = explode(':', $rule);
  75. if (!empty($rulePort) && $port != $rulePort) {
  76. $match = false;
  77. }
  78. }
  79. }
  80. if ($match) {
  81. return true;
  82. }
  83. }
  84. return false;
  85. }
  86. /**
  87. * Check an IP adress against a CIDR
  88. *
  89. * http://framework.zend.com/svn/framework/extras/incubator/library/ZendX/Whois/Adapter/Cidr.php
  90. *
  91. * @param string $cidr IPv4 block in CIDR notation
  92. * @param string $ip IPv4 address
  93. *
  94. * @return boolean
  95. */
  96. private static function inCIDRBlock($cidr, $ip)
  97. {
  98. // Get the base and the bits from the CIDR
  99. list($base, $bits) = explode('/', $cidr);
  100. // Now split it up into it's classes
  101. list($a, $b, $c, $d) = explode('.', $base);
  102. // Now do some bit shifting/switching to convert to ints
  103. $i = ($a << 24) + ($b << 16) + ($c << 8) + $d;
  104. $mask = $bits == 0 ? 0: (~0 << (32 - $bits));
  105. // Here's our lowest int
  106. $low = $i & $mask;
  107. // Here's our highest int
  108. $high = $i | (~$mask & 0xFFFFFFFF);
  109. // Now split the ip we're checking against up into classes
  110. list($a, $b, $c, $d) = explode('.', $ip);
  111. // Now convert the ip we're checking against to an int
  112. $check = ($a << 24) + ($b << 16) + ($c << 8) + $d;
  113. // If the ip is within the range, including highest/lowest values,
  114. // then it's witin the CIDR range
  115. if ($check >= $low && $check <= $high) {
  116. return true;
  117. } else {
  118. return false;
  119. }
  120. }
  121. }