PluginProbe ʕ •ᴥ•ʔ
Independent Analytics – WordPress Analytics Plugin / 2.14.1
Independent Analytics – WordPress Analytics Plugin v2.14.1
2.15.5 2.15.4 2.15.3 2.15.2 2.15.1 2.15.0 2.14.10 trunk 1.1 1.10 1.10.1 1.11 1.12 1.13 1.14 1.15 1.16 1.17 1.17.1 1.17.2 1.17.3 1.17.4 1.18 1.18.1 1.19.0 1.19.1 1.2 1.20.0 1.21.0 1.22.0 1.22.1 1.23.0 1.23.1 1.24.0 1.24.1 1.25.0 1.25.1 1.26.0 1.27.0 1.28.0 1.28.1 1.28.2 1.28.3 1.29.0 1.3 1.30.0 1.30.1 1.4 1.5 1.6 1.7 1.8 1.9 2.0.0 2.0.1 2.1.4 2.1.5 2.1.6 2.10.0 2.10.1 2.10.2 2.10.3 2.10.4 2.11.0 2.11.1 2.11.10 2.11.2 2.11.3 2.11.4 2.11.5 2.11.6 2.11.7 2.11.8 2.11.9 2.12.0 2.12.1 2.12.2 2.13.1 2.13.2 2.13.5 2.13.6 2.14.0 2.14.1 2.14.2 2.14.4 2.14.6 2.14.7 2.14.8 2.14.9 2.2.0 2.2.1 2.3.1 2.3.2 2.4.2 2.4.3 2.5.0 2.5.1 2.6.0 2.6.1 2.6.2 2.6.3 2.6.4 2.7.0 2.7.1 2.7.2 2.7.3 2.8.2 2.8.3 2.8.4 2.8.5 2.8.6 2.8.7 2.8.8 2.8.9 2.9.2 2.9.3 2.9.4 2.9.5 2.9.6 2.9.7
independent-analytics / vendor / mlocati / ip-lib / src / Range / Pattern.php
independent-analytics / vendor / mlocati / ip-lib / src / Range Last commit date
AbstractRange.php 1 year ago Pattern.php 1 year ago RangeInterface.php 1 year ago Single.php 1 year ago Subnet.php 1 year ago Type.php 1 year ago
Pattern.php
298 lines
1 <?php
2
3 namespace IAWPSCOPED\IPLib\Range;
4
5 use IAWPSCOPED\IPLib\Address\AddressInterface;
6 use IAWPSCOPED\IPLib\Address\IPv4;
7 use IAWPSCOPED\IPLib\Address\IPv6;
8 use IAWPSCOPED\IPLib\Address\Type as AddressType;
9 use IAWPSCOPED\IPLib\ParseStringFlag;
10 /**
11 * Represents an address range in pattern format (only ending asterisks are supported).
12 *
13 * @example 127.0.*.*
14 * @example ::/8
15 * @internal
16 */
17 class Pattern extends AbstractRange
18 {
19 /**
20 * Starting address of the range.
21 *
22 * @var \IPLib\Address\AddressInterface
23 */
24 protected $fromAddress;
25 /**
26 * Final address of the range.
27 *
28 * @var \IPLib\Address\AddressInterface
29 */
30 protected $toAddress;
31 /**
32 * Number of ending asterisks.
33 *
34 * @var int
35 */
36 protected $asterisksCount;
37 /**
38 * The type of the range of this IP range.
39 *
40 * @var int|false|null false if this range crosses multiple range types, null if yet to be determined
41 *
42 * @since 1.5.0
43 */
44 protected $rangeType;
45 /**
46 * Initializes the instance.
47 *
48 * @param \IPLib\Address\AddressInterface $fromAddress
49 * @param \IPLib\Address\AddressInterface $toAddress
50 * @param int $asterisksCount
51 */
52 public function __construct(AddressInterface $fromAddress, AddressInterface $toAddress, $asterisksCount)
53 {
54 $this->fromAddress = $fromAddress;
55 $this->toAddress = $toAddress;
56 $this->asterisksCount = $asterisksCount;
57 }
58 /**
59 * {@inheritdoc}
60 *
61 * @see \IPLib\Range\RangeInterface::__toString()
62 */
63 public function __toString()
64 {
65 return $this->toString();
66 }
67 /**
68 * @deprecated since 1.17.0: use the parseString() method instead.
69 * For upgrading:
70 * - if $supportNonDecimalIPv4 is true, use the ParseStringFlag::IPV4_MAYBE_NON_DECIMAL flag
71 *
72 * @param string|mixed $range
73 * @param bool $supportNonDecimalIPv4
74 *
75 * @return static|null
76 *
77 * @see \IPLib\Range\Pattern::parseString()
78 * @since 1.10.0 added the $supportNonDecimalIPv4 argument
79 */
80 public static function fromString($range, $supportNonDecimalIPv4 = \false)
81 {
82 return static::parseString($range, ParseStringFlag::MAY_INCLUDE_PORT | ParseStringFlag::MAY_INCLUDE_ZONEID | ($supportNonDecimalIPv4 ? ParseStringFlag::IPV4_MAYBE_NON_DECIMAL : 0));
83 }
84 /**
85 * Try get the range instance starting from its string representation.
86 *
87 * @param string|mixed $range
88 * @param int $flags A combination or zero or more flags
89 *
90 * @return static|null
91 *
92 * @since 1.17.0
93 * @see \IPLib\ParseStringFlag
94 */
95 public static function parseString($range, $flags = 0)
96 {
97 if (!\is_string($range) || \strpos($range, '*') === \false) {
98 return null;
99 }
100 if ($range === '*.*.*.*') {
101 return new static(IPv4::parseString('0.0.0.0'), IPv4::parseString('255.255.255.255'), 4);
102 }
103 if ($range === '*:*:*:*:*:*:*:*') {
104 return new static(IPv6::parseString('::'), IPv6::parseString('ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff'), 8);
105 }
106 $matches = null;
107 if (\strpos($range, '.') !== \false && \preg_match('/^[^*]+((?:\\.\\*)+)$/', $range, $matches)) {
108 $asterisksCount = \strlen($matches[1]) >> 1;
109 if ($asterisksCount > 0) {
110 $missingDots = 3 - \substr_count($range, '.');
111 if ($missingDots > 0) {
112 $range .= \str_repeat('.*', $missingDots);
113 $asterisksCount += $missingDots;
114 }
115 }
116 $fromAddress = IPv4::parseString(\str_replace('*', '0', $range), $flags);
117 if ($fromAddress === null) {
118 return null;
119 }
120 $fixedBytes = \array_slice($fromAddress->getBytes(), 0, -$asterisksCount);
121 $otherBytes = \array_fill(0, $asterisksCount, 255);
122 $toAddress = IPv4::fromBytes(\array_merge($fixedBytes, $otherBytes));
123 return new static($fromAddress, $toAddress, $asterisksCount);
124 }
125 if (\strpos($range, ':') !== \false && \preg_match('/^[^*]+((?::\\*)+)$/', $range, $matches)) {
126 $asterisksCount = \strlen($matches[1]) >> 1;
127 $fromAddress = IPv6::parseString(\str_replace('*', '0', $range));
128 if ($fromAddress === null) {
129 return null;
130 }
131 $fixedWords = \array_slice($fromAddress->getWords(), 0, -$asterisksCount);
132 $otherWords = \array_fill(0, $asterisksCount, 0xffff);
133 $toAddress = IPv6::fromWords(\array_merge($fixedWords, $otherWords));
134 return new static($fromAddress, $toAddress, $asterisksCount);
135 }
136 return null;
137 }
138 /**
139 * {@inheritdoc}
140 *
141 * @see \IPLib\Range\RangeInterface::toString()
142 */
143 public function toString($long = \false)
144 {
145 if ($this->asterisksCount === 0) {
146 return $this->fromAddress->toString($long);
147 }
148 switch (\true) {
149 case $this->fromAddress instanceof \IAWPSCOPED\IPLib\Address\IPv4:
150 $chunks = \explode('.', $this->fromAddress->toString());
151 $chunks = \array_slice($chunks, 0, -$this->asterisksCount);
152 $chunks = \array_pad($chunks, 4, '*');
153 $result = \implode('.', $chunks);
154 break;
155 case $this->fromAddress instanceof \IAWPSCOPED\IPLib\Address\IPv6:
156 if ($long) {
157 $chunks = \explode(':', $this->fromAddress->toString(\true));
158 $chunks = \array_slice($chunks, 0, -$this->asterisksCount);
159 $chunks = \array_pad($chunks, 8, '*');
160 $result = \implode(':', $chunks);
161 } elseif ($this->asterisksCount === 8) {
162 $result = '*:*:*:*:*:*:*:*';
163 } else {
164 $bytes = $this->toAddress->getBytes();
165 $bytes = \array_slice($bytes, 0, -$this->asterisksCount * 2);
166 $bytes = \array_pad($bytes, 16, 1);
167 $address = IPv6::fromBytes($bytes);
168 $before = \substr($address->toString(\false), 0, -\strlen(':101') * $this->asterisksCount);
169 $result = $before . \str_repeat(':*', $this->asterisksCount);
170 }
171 break;
172 default:
173 throw new \Exception('@todo');
174 }
175 return $result;
176 }
177 /**
178 * {@inheritdoc}
179 *
180 * @see \IPLib\Range\RangeInterface::getAddressType()
181 */
182 public function getAddressType()
183 {
184 return $this->fromAddress->getAddressType();
185 }
186 /**
187 * {@inheritdoc}
188 *
189 * @see \IPLib\Range\RangeInterface::getStartAddress()
190 */
191 public function getStartAddress()
192 {
193 return $this->fromAddress;
194 }
195 /**
196 * {@inheritdoc}
197 *
198 * @see \IPLib\Range\RangeInterface::getEndAddress()
199 */
200 public function getEndAddress()
201 {
202 return $this->toAddress;
203 }
204 /**
205 * {@inheritdoc}
206 *
207 * @see \IPLib\Range\RangeInterface::getComparableStartString()
208 */
209 public function getComparableStartString()
210 {
211 return $this->fromAddress->getComparableString();
212 }
213 /**
214 * {@inheritdoc}
215 *
216 * @see \IPLib\Range\RangeInterface::getComparableEndString()
217 */
218 public function getComparableEndString()
219 {
220 return $this->toAddress->getComparableString();
221 }
222 /**
223 * {@inheritdoc}
224 *
225 * @see \IPLib\Range\RangeInterface::asSubnet()
226 * @since 1.8.0
227 */
228 public function asSubnet()
229 {
230 return new Subnet($this->getStartAddress(), $this->getEndAddress(), $this->getNetworkPrefix());
231 }
232 /**
233 * {@inheritdoc}
234 *
235 * @see \IPLib\Range\RangeInterface::asPattern()
236 */
237 public function asPattern()
238 {
239 return $this;
240 }
241 /**
242 * {@inheritdoc}
243 *
244 * @see \IPLib\Range\RangeInterface::getSubnetMask()
245 */
246 public function getSubnetMask()
247 {
248 if ($this->getAddressType() !== AddressType::T_IPv4) {
249 return null;
250 }
251 switch ($this->asterisksCount) {
252 case 0:
253 $bytes = array(255, 255, 255, 255);
254 break;
255 case 4:
256 $bytes = array(0, 0, 0, 0);
257 break;
258 default:
259 $bytes = \array_pad(\array_fill(0, 4 - $this->asterisksCount, 255), 4, 0);
260 break;
261 }
262 return IPv4::fromBytes($bytes);
263 }
264 /**
265 * {@inheritdoc}
266 *
267 * @see \IPLib\Range\RangeInterface::getReverseDNSLookupName()
268 */
269 public function getReverseDNSLookupName()
270 {
271 return $this->asterisksCount === 0 ? array($this->getStartAddress()->getReverseDNSLookupName()) : $this->asSubnet()->getReverseDNSLookupName();
272 }
273 /**
274 * {@inheritdoc}
275 *
276 * @see \IPLib\Range\RangeInterface::getSize()
277 */
278 public function getSize()
279 {
280 $fromAddress = $this->fromAddress;
281 $maxPrefix = $fromAddress::getNumberOfBits();
282 $prefix = $this->getNetworkPrefix();
283 return \pow(2, $maxPrefix - $prefix);
284 }
285 /**
286 * @return float|int
287 */
288 private function getNetworkPrefix()
289 {
290 switch ($this->getAddressType()) {
291 case AddressType::T_IPv4:
292 return 8 * (4 - $this->asterisksCount);
293 case AddressType::T_IPv6:
294 return 16 * (8 - $this->asterisksCount);
295 }
296 }
297 }
298