Initial Commit
This commit is contained in:
67
dependencies/league/uri-interfaces/IPv4/BCMathCalculator.php
vendored
Normal file
67
dependencies/league/uri-interfaces/IPv4/BCMathCalculator.php
vendored
Normal file
@ -0,0 +1,67 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* League.Uri (https://uri.thephpleague.com)
|
||||
*
|
||||
* (c) Ignace Nyamagana Butera <nyamsprod@gmail.com>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
declare (strict_types=1);
|
||||
namespace WP_Ultimo\Dependencies\League\Uri\IPv4;
|
||||
|
||||
use function bcadd;
|
||||
use function bccomp;
|
||||
use function bcdiv;
|
||||
use function bcmod;
|
||||
use function bcmul;
|
||||
use function bcpow;
|
||||
use function bcsub;
|
||||
use function str_split;
|
||||
final class BCMathCalculator implements Calculator
|
||||
{
|
||||
private const SCALE = 0;
|
||||
private const CONVERSION_TABLE = ['0' => '0', '1' => '1', '2' => '2', '3' => '3', '4' => '4', '5' => '5', '6' => '6', '7' => '7', '8' => '8', '9' => '9', 'a' => '10', 'b' => '11', 'c' => '12', 'd' => '13', 'e' => '14', 'f' => '15'];
|
||||
public function baseConvert(mixed $value, int $base) : string
|
||||
{
|
||||
$value = (string) $value;
|
||||
if (10 === $base) {
|
||||
return $value;
|
||||
}
|
||||
$base = (string) $base;
|
||||
$decimal = '0';
|
||||
foreach (str_split($value) as $char) {
|
||||
$decimal = bcadd($this->multiply($decimal, $base), self::CONVERSION_TABLE[$char], self::SCALE);
|
||||
}
|
||||
return $decimal;
|
||||
}
|
||||
public function pow(mixed $value, int $exponent) : string
|
||||
{
|
||||
return bcpow((string) $value, (string) $exponent, self::SCALE);
|
||||
}
|
||||
public function compare(mixed $value1, $value2) : int
|
||||
{
|
||||
return bccomp((string) $value1, (string) $value2, self::SCALE);
|
||||
}
|
||||
public function multiply(mixed $value1, $value2) : string
|
||||
{
|
||||
return bcmul((string) $value1, (string) $value2, self::SCALE);
|
||||
}
|
||||
public function div(mixed $value, mixed $base) : string
|
||||
{
|
||||
return bcdiv((string) $value, (string) $base, self::SCALE);
|
||||
}
|
||||
public function mod(mixed $value, mixed $base) : string
|
||||
{
|
||||
return bcmod((string) $value, (string) $base, self::SCALE);
|
||||
}
|
||||
public function add(mixed $value1, mixed $value2) : string
|
||||
{
|
||||
return bcadd((string) $value1, (string) $value2, self::SCALE);
|
||||
}
|
||||
public function sub(mixed $value1, mixed $value2) : string
|
||||
{
|
||||
return bcsub((string) $value1, (string) $value2, self::SCALE);
|
||||
}
|
||||
}
|
86
dependencies/league/uri-interfaces/IPv4/Calculator.php
vendored
Normal file
86
dependencies/league/uri-interfaces/IPv4/Calculator.php
vendored
Normal file
@ -0,0 +1,86 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* League.Uri (https://uri.thephpleague.com)
|
||||
*
|
||||
* (c) Ignace Nyamagana Butera <nyamsprod@gmail.com>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
declare (strict_types=1);
|
||||
namespace WP_Ultimo\Dependencies\League\Uri\IPv4;
|
||||
|
||||
interface Calculator
|
||||
{
|
||||
/**
|
||||
* Add numbers.
|
||||
*
|
||||
* @param mixed $value1 a number that will be added to $value2
|
||||
* @param mixed $value2 a number that will be added to $value1
|
||||
*
|
||||
* @return mixed the addition result
|
||||
*/
|
||||
public function add(mixed $value1, mixed $value2);
|
||||
/**
|
||||
* Subtract one number from another.
|
||||
*
|
||||
* @param mixed $value1 a number that will be subtracted of $value2
|
||||
* @param mixed $value2 a number that will be subtracted to $value1
|
||||
*
|
||||
* @return mixed the subtraction result
|
||||
*/
|
||||
public function sub(mixed $value1, mixed $value2);
|
||||
/**
|
||||
* Multiply numbers.
|
||||
*
|
||||
* @param mixed $value1 a number that will be multiplied by $value2
|
||||
* @param mixed $value2 a number that will be multiplied by $value1
|
||||
*
|
||||
* @return mixed the multiplication result
|
||||
*/
|
||||
public function multiply(mixed $value1, mixed $value2);
|
||||
/**
|
||||
* Divide numbers.
|
||||
*
|
||||
* @param mixed $value The number being divided.
|
||||
* @param mixed $base The number that $value is being divided by.
|
||||
*
|
||||
* @return mixed the result of the division
|
||||
*/
|
||||
public function div(mixed $value, mixed $base);
|
||||
/**
|
||||
* Raise an number to the power of exponent.
|
||||
*
|
||||
* @param mixed $value scalar, the base to use
|
||||
*
|
||||
* @return mixed the value raised to the power of exp.
|
||||
*/
|
||||
public function pow(mixed $value, int $exponent);
|
||||
/**
|
||||
* Returns the int point remainder (modulo) of the division of the arguments.
|
||||
*
|
||||
* @param mixed $value The dividend
|
||||
* @param mixed $base The divisor
|
||||
*
|
||||
* @return mixed the remainder
|
||||
*/
|
||||
public function mod(mixed $value, mixed $base);
|
||||
/**
|
||||
* Number comparison.
|
||||
*
|
||||
* @param mixed $value1 the first value
|
||||
* @param mixed $value2 the second value
|
||||
*
|
||||
* @return int Returns < 0 if value1 is less than value2; > 0 if value1 is greater than value2, and 0 if they are equal.
|
||||
*/
|
||||
public function compare(mixed $value1, mixed $value2) : int;
|
||||
/**
|
||||
* Get the decimal integer value of a variable.
|
||||
*
|
||||
* @param mixed $value The scalar value being converted to an integer
|
||||
*
|
||||
* @return mixed the integer value
|
||||
*/
|
||||
public function baseConvert(mixed $value, int $base);
|
||||
}
|
178
dependencies/league/uri-interfaces/IPv4/Converter.php
vendored
Normal file
178
dependencies/league/uri-interfaces/IPv4/Converter.php
vendored
Normal file
@ -0,0 +1,178 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* League.Uri (https://uri.thephpleague.com)
|
||||
*
|
||||
* (c) Ignace Nyamagana Butera <nyamsprod@gmail.com>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
declare (strict_types=1);
|
||||
namespace WP_Ultimo\Dependencies\League\Uri\IPv4;
|
||||
|
||||
use WP_Ultimo\Dependencies\League\Uri\Exceptions\MissingFeature;
|
||||
use WP_Ultimo\Dependencies\League\Uri\FeatureDetection;
|
||||
use Stringable;
|
||||
use function array_pop;
|
||||
use function count;
|
||||
use function explode;
|
||||
use function extension_loaded;
|
||||
use function ltrim;
|
||||
use function preg_match;
|
||||
use function str_ends_with;
|
||||
use function substr;
|
||||
final class Converter
|
||||
{
|
||||
private const REGEXP_IPV4_HOST = '/
|
||||
(?(DEFINE) # . is missing as it is used to separate labels
|
||||
(?<hexadecimal>0x[[:xdigit:]]*)
|
||||
(?<octal>0[0-7]*)
|
||||
(?<decimal>\\d+)
|
||||
(?<ipv4_part>(?:(?&hexadecimal)|(?&octal)|(?&decimal))*)
|
||||
)
|
||||
^(?:(?&ipv4_part)\\.){0,3}(?&ipv4_part)\\.?$
|
||||
/x';
|
||||
private const REGEXP_IPV4_NUMBER_PER_BASE = ['/^0x(?<number>[[:xdigit:]]*)$/' => 16, '/^0(?<number>[0-7]*)$/' => 8, '/^(?<number>\\d+)$/' => 10];
|
||||
private readonly mixed $maxIPv4Number;
|
||||
public function __construct(private readonly Calculator $calculator)
|
||||
{
|
||||
$this->maxIPv4Number = $calculator->sub($calculator->pow(2, 32), 1);
|
||||
}
|
||||
/**
|
||||
* Returns an instance using a GMP calculator.
|
||||
*/
|
||||
public static function fromGMP() : self
|
||||
{
|
||||
return new self(new GMPCalculator());
|
||||
}
|
||||
/**
|
||||
* Returns an instance using a Bcmath calculator.
|
||||
*/
|
||||
public static function fromBCMath() : self
|
||||
{
|
||||
return new self(new BCMathCalculator());
|
||||
}
|
||||
/**
|
||||
* Returns an instance using a PHP native calculator (requires 64bits PHP).
|
||||
*/
|
||||
public static function fromNative() : self
|
||||
{
|
||||
return new self(new NativeCalculator());
|
||||
}
|
||||
/**
|
||||
* Returns an instance using a detected calculator depending on the PHP environment.
|
||||
*
|
||||
* @throws MissingFeature If no Calculator implementing object can be used on the platform
|
||||
*
|
||||
* @codeCoverageIgnore
|
||||
*/
|
||||
public static function fromEnvironment() : self
|
||||
{
|
||||
FeatureDetection::supportsIPv4Conversion();
|
||||
return match (\true) {
|
||||
extension_loaded('gmp') => self::fromGMP(),
|
||||
function_exists('bcadd') => self::fromBCMath(),
|
||||
default => self::fromNative(),
|
||||
};
|
||||
}
|
||||
public function isIpv4(Stringable|string|null $host) : bool
|
||||
{
|
||||
return null !== $this->toDecimal($host);
|
||||
}
|
||||
public function toOctal(Stringable|string|null $host) : ?string
|
||||
{
|
||||
$host = $this->toDecimal($host);
|
||||
return match (null) {
|
||||
$host => null,
|
||||
default => \implode('.', \array_map(fn($value) => \str_pad(\decoct((int) $value), 4, '0', \STR_PAD_LEFT), explode('.', $host))),
|
||||
};
|
||||
}
|
||||
public function toHexadecimal(Stringable|string|null $host) : ?string
|
||||
{
|
||||
$host = $this->toDecimal($host);
|
||||
return match (null) {
|
||||
$host => null,
|
||||
default => '0x' . \implode('', \array_map(fn($value) => \dechex((int) $value), explode('.', $host))),
|
||||
};
|
||||
}
|
||||
/**
|
||||
* Tries to convert a IPv4 hexadecimal or a IPv4 octal notation into a IPv4 dot-decimal notation if possible
|
||||
* otherwise returns null.
|
||||
*
|
||||
* @see https://url.spec.whatwg.org/#concept-ipv4-parser
|
||||
*/
|
||||
public function toDecimal(Stringable|string|null $host) : ?string
|
||||
{
|
||||
$host = (string) $host;
|
||||
if (1 !== preg_match(self::REGEXP_IPV4_HOST, $host)) {
|
||||
return null;
|
||||
}
|
||||
if (str_ends_with($host, '.')) {
|
||||
$host = substr($host, 0, -1);
|
||||
}
|
||||
$numbers = [];
|
||||
foreach (explode('.', $host) as $label) {
|
||||
$number = $this->labelToNumber($label);
|
||||
if (null === $number) {
|
||||
return null;
|
||||
}
|
||||
$numbers[] = $number;
|
||||
}
|
||||
$ipv4 = array_pop($numbers);
|
||||
$max = $this->calculator->pow(256, 6 - count($numbers));
|
||||
if ($this->calculator->compare($ipv4, $max) > 0) {
|
||||
return null;
|
||||
}
|
||||
foreach ($numbers as $offset => $number) {
|
||||
if ($this->calculator->compare($number, 255) > 0) {
|
||||
return null;
|
||||
}
|
||||
$ipv4 = $this->calculator->add($ipv4, $this->calculator->multiply($number, $this->calculator->pow(256, 3 - $offset)));
|
||||
}
|
||||
return $this->long2Ip($ipv4);
|
||||
}
|
||||
/**
|
||||
* Converts a domain label into a IPv4 integer part.
|
||||
*
|
||||
* @see https://url.spec.whatwg.org/#ipv4-number-parser
|
||||
*
|
||||
* @return mixed returns null if it cannot correctly convert the label
|
||||
*/
|
||||
private function labelToNumber(string $label) : mixed
|
||||
{
|
||||
foreach (self::REGEXP_IPV4_NUMBER_PER_BASE as $regexp => $base) {
|
||||
if (1 !== preg_match($regexp, $label, $matches)) {
|
||||
continue;
|
||||
}
|
||||
$number = ltrim($matches['number'], '0');
|
||||
if ('' === $number) {
|
||||
return 0;
|
||||
}
|
||||
$number = $this->calculator->baseConvert($number, $base);
|
||||
if (0 <= $this->calculator->compare($number, 0) && 0 >= $this->calculator->compare($number, $this->maxIPv4Number)) {
|
||||
return $number;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
/**
|
||||
* Generates the dot-decimal notation for IPv4.
|
||||
*
|
||||
* @see https://url.spec.whatwg.org/#concept-ipv4-parser
|
||||
*
|
||||
* @param mixed $ipAddress the number representation of the IPV4address
|
||||
*/
|
||||
private function long2Ip(mixed $ipAddress) : string
|
||||
{
|
||||
$output = '';
|
||||
for ($offset = 0; $offset < 4; $offset++) {
|
||||
$output = $this->calculator->mod($ipAddress, 256) . $output;
|
||||
if ($offset < 3) {
|
||||
$output = '.' . $output;
|
||||
}
|
||||
$ipAddress = $this->calculator->div($ipAddress, 256);
|
||||
}
|
||||
return $output;
|
||||
}
|
||||
}
|
58
dependencies/league/uri-interfaces/IPv4/GMPCalculator.php
vendored
Normal file
58
dependencies/league/uri-interfaces/IPv4/GMPCalculator.php
vendored
Normal file
@ -0,0 +1,58 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* League.Uri (https://uri.thephpleague.com)
|
||||
*
|
||||
* (c) Ignace Nyamagana Butera <nyamsprod@gmail.com>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
declare (strict_types=1);
|
||||
namespace WP_Ultimo\Dependencies\League\Uri\IPv4;
|
||||
|
||||
use GMP;
|
||||
use function gmp_add;
|
||||
use function gmp_cmp;
|
||||
use function gmp_div_q;
|
||||
use function gmp_init;
|
||||
use function gmp_mod;
|
||||
use function gmp_mul;
|
||||
use function gmp_pow;
|
||||
use function gmp_sub;
|
||||
use const GMP_ROUND_MINUSINF;
|
||||
final class GMPCalculator implements Calculator
|
||||
{
|
||||
public function baseConvert(mixed $value, int $base) : GMP
|
||||
{
|
||||
return gmp_init($value, $base);
|
||||
}
|
||||
public function pow(mixed $value, int $exponent) : GMP
|
||||
{
|
||||
return gmp_pow($value, $exponent);
|
||||
}
|
||||
public function compare(mixed $value1, mixed $value2) : int
|
||||
{
|
||||
return gmp_cmp($value1, $value2);
|
||||
}
|
||||
public function multiply(mixed $value1, mixed $value2) : GMP
|
||||
{
|
||||
return gmp_mul($value1, $value2);
|
||||
}
|
||||
public function div(mixed $value, mixed $base) : GMP
|
||||
{
|
||||
return gmp_div_q($value, $base, GMP_ROUND_MINUSINF);
|
||||
}
|
||||
public function mod(mixed $value, mixed $base) : GMP
|
||||
{
|
||||
return gmp_mod($value, $base);
|
||||
}
|
||||
public function add(mixed $value1, mixed $value2) : GMP
|
||||
{
|
||||
return gmp_add($value1, $value2);
|
||||
}
|
||||
public function sub(mixed $value1, mixed $value2) : GMP
|
||||
{
|
||||
return gmp_sub($value1, $value2);
|
||||
}
|
||||
}
|
50
dependencies/league/uri-interfaces/IPv4/NativeCalculator.php
vendored
Normal file
50
dependencies/league/uri-interfaces/IPv4/NativeCalculator.php
vendored
Normal file
@ -0,0 +1,50 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* League.Uri (https://uri.thephpleague.com)
|
||||
*
|
||||
* (c) Ignace Nyamagana Butera <nyamsprod@gmail.com>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
declare (strict_types=1);
|
||||
namespace WP_Ultimo\Dependencies\League\Uri\IPv4;
|
||||
|
||||
use function floor;
|
||||
use function intval;
|
||||
final class NativeCalculator implements Calculator
|
||||
{
|
||||
public function baseConvert(mixed $value, int $base) : int
|
||||
{
|
||||
return intval((string) $value, $base);
|
||||
}
|
||||
public function pow(mixed $value, int $exponent)
|
||||
{
|
||||
return $value ** $exponent;
|
||||
}
|
||||
public function compare(mixed $value1, mixed $value2) : int
|
||||
{
|
||||
return $value1 <=> $value2;
|
||||
}
|
||||
public function multiply(mixed $value1, mixed $value2) : int
|
||||
{
|
||||
return $value1 * $value2;
|
||||
}
|
||||
public function div(mixed $value, mixed $base) : int
|
||||
{
|
||||
return (int) floor($value / $base);
|
||||
}
|
||||
public function mod(mixed $value, mixed $base) : int
|
||||
{
|
||||
return $value % $base;
|
||||
}
|
||||
public function add(mixed $value1, mixed $value2) : int
|
||||
{
|
||||
return $value1 + $value2;
|
||||
}
|
||||
public function sub(mixed $value1, mixed $value2) : int
|
||||
{
|
||||
return $value1 - $value2;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user