BaseIpHelper.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. <?php
  2. /**
  3. * @link http://www.yiiframework.com/
  4. * @copyright Copyright (c) 2008 Yii Software LLC
  5. * @license http://www.yiiframework.com/license/
  6. */
  7. namespace yii\helpers;
  8. /**
  9. * Class BaseIpHelper provides concrete implementation for [[IpHelper]]
  10. *
  11. * Do not use BaseIpHelper, use [[IpHelper]] instead.
  12. *
  13. * @author Dmytro Naumenko <d.naumenko.a@gmail.com>
  14. * @since 2.0.14
  15. */
  16. class BaseIpHelper
  17. {
  18. const IPV4 = 4;
  19. const IPV6 = 6;
  20. /**
  21. * The length of IPv6 address in bits
  22. */
  23. const IPV6_ADDRESS_LENGTH = 128;
  24. /**
  25. * The length of IPv4 address in bits
  26. */
  27. const IPV4_ADDRESS_LENGTH = 32;
  28. /**
  29. * Gets the IP version. Does not perform IP address validation.
  30. *
  31. * @param string $ip the valid IPv4 or IPv6 address.
  32. * @return int [[IPV4]] or [[IPV6]]
  33. */
  34. public static function getIpVersion($ip)
  35. {
  36. return strpos($ip, ':') === false ? self::IPV4 : self::IPV6;
  37. }
  38. /**
  39. * Checks whether IP address or subnet $subnet is contained by $subnet.
  40. *
  41. * For example, the following code checks whether subnet `192.168.1.0/24` is in subnet `192.168.0.0/22`:
  42. *
  43. * ```php
  44. * IpHelper::inRange('192.168.1.0/24', '192.168.0.0/22'); // true
  45. * ```
  46. *
  47. * In case you need to check whether a single IP address `192.168.1.21` is in the subnet `192.168.1.0/24`,
  48. * you can use any of theses examples:
  49. *
  50. * ```php
  51. * IpHelper::inRange('192.168.1.21', '192.168.1.0/24'); // true
  52. * IpHelper::inRange('192.168.1.21/32', '192.168.1.0/24'); // true
  53. * ```
  54. *
  55. * @param string $subnet the valid IPv4 or IPv6 address or CIDR range, e.g.: `10.0.0.0/8` or `2001:af::/64`
  56. * @param string $range the valid IPv4 or IPv6 CIDR range, e.g. `10.0.0.0/8` or `2001:af::/64`
  57. * @return bool whether $subnet is contained by $range
  58. *
  59. * @see https://en.wikipedia.org/wiki/Classless_Inter-Domain_Routing
  60. */
  61. public static function inRange($subnet, $range)
  62. {
  63. list($ip, $mask) = array_pad(explode('/', $subnet), 2, null);
  64. list($net, $netMask) = array_pad(explode('/', $range), 2, null);
  65. $ipVersion = static::getIpVersion($ip);
  66. $netVersion = static::getIpVersion($net);
  67. if ($ipVersion !== $netVersion) {
  68. return false;
  69. }
  70. $maxMask = $ipVersion === self::IPV4 ? self::IPV4_ADDRESS_LENGTH : self::IPV6_ADDRESS_LENGTH;
  71. $mask = isset($mask) ? $mask : $maxMask;
  72. $netMask = isset($netMask) ? $netMask : $maxMask;
  73. $binIp = static::ip2bin($ip);
  74. $binNet = static::ip2bin($net);
  75. return substr($binIp, 0, $netMask) === substr($binNet, 0, $netMask) && $mask >= $netMask;
  76. }
  77. /**
  78. * Expands an IPv6 address to it's full notation.
  79. *
  80. * For example `2001:db8::1` will be expanded to `2001:0db8:0000:0000:0000:0000:0000:0001`
  81. *
  82. * @param string $ip the original valid IPv6 address
  83. * @return string the expanded IPv6 address
  84. */
  85. public static function expandIPv6($ip)
  86. {
  87. $hex = unpack('H*hex', inet_pton($ip));
  88. return substr(preg_replace('/([a-f0-9]{4})/i', '$1:', $hex['hex']), 0, -1);
  89. }
  90. /**
  91. * Converts IP address to bits representation.
  92. *
  93. * @param string $ip the valid IPv4 or IPv6 address
  94. * @return string bits as a string
  95. */
  96. public static function ip2bin($ip)
  97. {
  98. if (static::getIpVersion($ip) === self::IPV4) {
  99. return str_pad(base_convert(ip2long($ip), 10, 2), self::IPV4_ADDRESS_LENGTH, '0', STR_PAD_LEFT);
  100. }
  101. $unpack = unpack('A16', inet_pton($ip));
  102. $binStr = array_shift($unpack);
  103. $bytes = self::IPV6_ADDRESS_LENGTH / 8; // 128 bit / 8 = 16 bytes
  104. $result = '';
  105. while ($bytes-- > 0) {
  106. $result = sprintf('%08b', isset($binStr[$bytes]) ? ord($binStr[$bytes]) : '0') . $result;
  107. }
  108. return $result;
  109. }
  110. }