Pattern.php 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. <?php
  2. namespace IPLib\Range;
  3. use IPLib\Address\AddressInterface;
  4. use IPLib\Address\IPv4;
  5. use IPLib\Address\IPv6;
  6. use IPLib\Address\Type as AddressType;
  7. use IPLib\ParseStringFlag;
  8. /**
  9. * Represents an address range in pattern format (only ending asterisks are supported).
  10. *
  11. * @example 127.0.*.*
  12. * @example ::/8
  13. */
  14. class Pattern extends AbstractRange
  15. {
  16. /**
  17. * Starting address of the range.
  18. *
  19. * @var \IPLib\Address\AddressInterface
  20. */
  21. protected $fromAddress;
  22. /**
  23. * Final address of the range.
  24. *
  25. * @var \IPLib\Address\AddressInterface
  26. */
  27. protected $toAddress;
  28. /**
  29. * Number of ending asterisks.
  30. *
  31. * @var int
  32. */
  33. protected $asterisksCount;
  34. /**
  35. * The type of the range of this IP range.
  36. *
  37. * @var int|false|null false if this range crosses multiple range types, null if yet to be determined
  38. *
  39. * @since 1.5.0
  40. */
  41. protected $rangeType;
  42. /**
  43. * Initializes the instance.
  44. *
  45. * @param \IPLib\Address\AddressInterface $fromAddress
  46. * @param \IPLib\Address\AddressInterface $toAddress
  47. * @param int $asterisksCount
  48. */
  49. public function __construct(AddressInterface $fromAddress, AddressInterface $toAddress, $asterisksCount)
  50. {
  51. $this->fromAddress = $fromAddress;
  52. $this->toAddress = $toAddress;
  53. $this->asterisksCount = $asterisksCount;
  54. }
  55. /**
  56. * {@inheritdoc}
  57. *
  58. * @see \IPLib\Range\RangeInterface::__toString()
  59. */
  60. public function __toString()
  61. {
  62. return $this->toString();
  63. }
  64. /**
  65. * @deprecated since 1.17.0: use the parseString() method instead.
  66. * For upgrading:
  67. * - if $supportNonDecimalIPv4 is true, use the ParseStringFlag::IPV4_MAYBE_NON_DECIMAL flag
  68. *
  69. * @param string|mixed $range
  70. * @param bool $supportNonDecimalIPv4
  71. *
  72. * @return static|null
  73. *
  74. * @see \IPLib\Range\Pattern::parseString()
  75. * @since 1.10.0 added the $supportNonDecimalIPv4 argument
  76. */
  77. public static function fromString($range, $supportNonDecimalIPv4 = false)
  78. {
  79. return static::parseString($range, ParseStringFlag::MAY_INCLUDE_PORT | ParseStringFlag::MAY_INCLUDE_ZONEID | ($supportNonDecimalIPv4 ? ParseStringFlag::IPV4_MAYBE_NON_DECIMAL : 0));
  80. }
  81. /**
  82. * Try get the range instance starting from its string representation.
  83. *
  84. * @param string|mixed $range
  85. * @param int $flags A combination or zero or more flags
  86. *
  87. * @return static|null
  88. *
  89. * @since 1.17.0
  90. * @see \IPLib\ParseStringFlag
  91. */
  92. public static function parseString($range, $flags = 0)
  93. {
  94. if (!is_string($range) || strpos($range, '*') === false) {
  95. return null;
  96. }
  97. if ($range === '*.*.*.*') {
  98. return new static(IPv4::parseString('0.0.0.0'), IPv4::parseString('255.255.255.255'), 4);
  99. }
  100. if ($range === '*:*:*:*:*:*:*:*') {
  101. return new static(IPv6::parseString('::'), IPv6::parseString('ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff'), 8);
  102. }
  103. $matches = null;
  104. if (strpos($range, '.') !== false && preg_match('/^[^*]+((?:\.\*)+)$/', $range, $matches)) {
  105. $asterisksCount = strlen($matches[1]) >> 1;
  106. if ($asterisksCount > 0) {
  107. $missingDots = 3 - substr_count($range, '.');
  108. if ($missingDots > 0) {
  109. $range .= str_repeat('.*', $missingDots);
  110. $asterisksCount += $missingDots;
  111. }
  112. }
  113. $fromAddress = IPv4::parseString(str_replace('*', '0', $range), $flags);
  114. if ($fromAddress === null) {
  115. return null;
  116. }
  117. $fixedBytes = array_slice($fromAddress->getBytes(), 0, -$asterisksCount);
  118. $otherBytes = array_fill(0, $asterisksCount, 255);
  119. $toAddress = IPv4::fromBytes(array_merge($fixedBytes, $otherBytes));
  120. return new static($fromAddress, $toAddress, $asterisksCount);
  121. }
  122. if (strpos($range, ':') !== false && preg_match('/^[^*]+((?::\*)+)$/', $range, $matches)) {
  123. $asterisksCount = strlen($matches[1]) >> 1;
  124. $fromAddress = IPv6::parseString(str_replace('*', '0', $range));
  125. if ($fromAddress === null) {
  126. return null;
  127. }
  128. $fixedWords = array_slice($fromAddress->getWords(), 0, -$asterisksCount);
  129. $otherWords = array_fill(0, $asterisksCount, 0xffff);
  130. $toAddress = IPv6::fromWords(array_merge($fixedWords, $otherWords));
  131. return new static($fromAddress, $toAddress, $asterisksCount);
  132. }
  133. return null;
  134. }
  135. /**
  136. * {@inheritdoc}
  137. *
  138. * @see \IPLib\Range\RangeInterface::toString()
  139. */
  140. public function toString($long = false)
  141. {
  142. if ($this->asterisksCount === 0) {
  143. return $this->fromAddress->toString($long);
  144. }
  145. switch (true) {
  146. case $this->fromAddress instanceof \IPLib\Address\IPv4:
  147. $chunks = explode('.', $this->fromAddress->toString());
  148. $chunks = array_slice($chunks, 0, -$this->asterisksCount);
  149. $chunks = array_pad($chunks, 4, '*');
  150. $result = implode('.', $chunks);
  151. break;
  152. case $this->fromAddress instanceof \IPLib\Address\IPv6:
  153. if ($long) {
  154. $chunks = explode(':', $this->fromAddress->toString(true));
  155. $chunks = array_slice($chunks, 0, -$this->asterisksCount);
  156. $chunks = array_pad($chunks, 8, '*');
  157. $result = implode(':', $chunks);
  158. } elseif ($this->asterisksCount === 8) {
  159. $result = '*:*:*:*:*:*:*:*';
  160. } else {
  161. $bytes = $this->toAddress->getBytes();
  162. $bytes = array_slice($bytes, 0, -$this->asterisksCount * 2);
  163. $bytes = array_pad($bytes, 16, 1);
  164. $address = IPv6::fromBytes($bytes);
  165. $before = substr($address->toString(false), 0, -strlen(':101') * $this->asterisksCount);
  166. $result = $before . str_repeat(':*', $this->asterisksCount);
  167. }
  168. break;
  169. default:
  170. throw new \Exception('@todo'); // @codeCoverageIgnore
  171. }
  172. return $result;
  173. }
  174. /**
  175. * {@inheritdoc}
  176. *
  177. * @see \IPLib\Range\RangeInterface::getAddressType()
  178. */
  179. public function getAddressType()
  180. {
  181. return $this->fromAddress->getAddressType();
  182. }
  183. /**
  184. * {@inheritdoc}
  185. *
  186. * @see \IPLib\Range\RangeInterface::getStartAddress()
  187. */
  188. public function getStartAddress()
  189. {
  190. return $this->fromAddress;
  191. }
  192. /**
  193. * {@inheritdoc}
  194. *
  195. * @see \IPLib\Range\RangeInterface::getEndAddress()
  196. */
  197. public function getEndAddress()
  198. {
  199. return $this->toAddress;
  200. }
  201. /**
  202. * {@inheritdoc}
  203. *
  204. * @see \IPLib\Range\RangeInterface::getComparableStartString()
  205. */
  206. public function getComparableStartString()
  207. {
  208. return $this->fromAddress->getComparableString();
  209. }
  210. /**
  211. * {@inheritdoc}
  212. *
  213. * @see \IPLib\Range\RangeInterface::getComparableEndString()
  214. */
  215. public function getComparableEndString()
  216. {
  217. return $this->toAddress->getComparableString();
  218. }
  219. /**
  220. * {@inheritdoc}
  221. *
  222. * @see \IPLib\Range\RangeInterface::asSubnet()
  223. * @since 1.8.0
  224. */
  225. public function asSubnet()
  226. {
  227. return new Subnet($this->getStartAddress(), $this->getEndAddress(), $this->getNetworkPrefix());
  228. }
  229. /**
  230. * {@inheritdoc}
  231. *
  232. * @see \IPLib\Range\RangeInterface::asPattern()
  233. */
  234. public function asPattern()
  235. {
  236. return $this;
  237. }
  238. /**
  239. * {@inheritdoc}
  240. *
  241. * @see \IPLib\Range\RangeInterface::getSubnetMask()
  242. */
  243. public function getSubnetMask()
  244. {
  245. if ($this->getAddressType() !== AddressType::T_IPv4) {
  246. return null;
  247. }
  248. switch ($this->asterisksCount) {
  249. case 0:
  250. $bytes = array(255, 255, 255, 255);
  251. break;
  252. case 4:
  253. $bytes = array(0, 0, 0, 0);
  254. break;
  255. default:
  256. $bytes = array_pad(array_fill(0, 4 - $this->asterisksCount, 255), 4, 0);
  257. break;
  258. }
  259. return IPv4::fromBytes($bytes);
  260. }
  261. /**
  262. * {@inheritdoc}
  263. *
  264. * @see \IPLib\Range\RangeInterface::getReverseDNSLookupName()
  265. */
  266. public function getReverseDNSLookupName()
  267. {
  268. return $this->asterisksCount === 0 ? array($this->getStartAddress()->getReverseDNSLookupName()) : $this->asSubnet()->getReverseDNSLookupName();
  269. }
  270. /**
  271. * {@inheritdoc}
  272. *
  273. * @see \IPLib\Range\RangeInterface::getSize()
  274. */
  275. public function getSize()
  276. {
  277. $fromAddress = $this->fromAddress;
  278. $maxPrefix = $fromAddress::getNumberOfBits();
  279. $prefix = $this->getNetworkPrefix();
  280. return pow(2, ($maxPrefix - $prefix));
  281. }
  282. /**
  283. * @return float|int
  284. */
  285. private function getNetworkPrefix()
  286. {
  287. switch ($this->getAddressType()) {
  288. case AddressType::T_IPv4:
  289. return 8 * (4 - $this->asterisksCount);
  290. case AddressType::T_IPv6:
  291. return 16 * (8 - $this->asterisksCount);
  292. }
  293. }
  294. }