diff options
Diffstat (limited to 'vendor/ramsey/uuid/src/Provider/Node')
5 files changed, 270 insertions, 82 deletions
diff --git a/vendor/ramsey/uuid/src/Provider/Node/FallbackNodeProvider.php b/vendor/ramsey/uuid/src/Provider/Node/FallbackNodeProvider.php index 83488ab96..f6e5e406d 100644 --- a/vendor/ramsey/uuid/src/Provider/Node/FallbackNodeProvider.php +++ b/vendor/ramsey/uuid/src/Provider/Node/FallbackNodeProvider.php @@ -1,4 +1,5 @@ <?php + /** * This file is part of the ramsey/uuid library * @@ -7,53 +8,54 @@ * * @copyright Copyright (c) Ben Ramsey <ben@benramsey.com> * @license http://opensource.org/licenses/MIT MIT - * @link https://benramsey.com/projects/ramsey-uuid/ Documentation - * @link https://packagist.org/packages/ramsey/uuid Packagist - * @link https://github.com/ramsey/uuid GitHub */ +declare(strict_types=1); + namespace Ramsey\Uuid\Provider\Node; -use Exception; +use Ramsey\Uuid\Exception\NodeException; use Ramsey\Uuid\Provider\NodeProviderInterface; +use Ramsey\Uuid\Type\Hexadecimal; /** - * FallbackNodeProvider attempts to gain the system host ID from an array of - * providers, falling back to the next in line in the event a host ID can not be - * obtained + * FallbackNodeProvider retrieves the system node ID by stepping through a list + * of providers until a node ID can be obtained */ class FallbackNodeProvider implements NodeProviderInterface { /** - * @var NodeProviderInterface[] + * @var NodeProviderCollection */ private $nodeProviders; /** - * Constructs a `FallbackNodeProvider` using an array of node providers - * - * @param NodeProviderInterface[] $providers Array of node providers + * @param NodeProviderCollection $providers Array of node providers */ - public function __construct(array $providers) + public function __construct(NodeProviderCollection $providers) { $this->nodeProviders = $providers; } - /** - * Returns the system node ID by iterating over an array of node providers - * and returning the first non-empty value found - * - * @return string System node ID as a hexadecimal string - * @throws Exception - */ - public function getNode() + public function getNode(): Hexadecimal { + $lastProviderException = null; + + /** @var NodeProviderInterface $provider */ foreach ($this->nodeProviders as $provider) { - if ($node = $provider->getNode()) { - return $node; + try { + return $provider->getNode(); + } catch (NodeException $exception) { + $lastProviderException = $exception; + + continue; } } - return null; + throw new NodeException( + 'Unable to find a suitable node provider', + 0, + $lastProviderException + ); } } diff --git a/vendor/ramsey/uuid/src/Provider/Node/NodeProviderCollection.php b/vendor/ramsey/uuid/src/Provider/Node/NodeProviderCollection.php new file mode 100644 index 000000000..89d09178d --- /dev/null +++ b/vendor/ramsey/uuid/src/Provider/Node/NodeProviderCollection.php @@ -0,0 +1,54 @@ +<?php + +/** + * This file is part of the ramsey/uuid library + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + * + * @copyright Copyright (c) Ben Ramsey <ben@benramsey.com> + * @license http://opensource.org/licenses/MIT MIT + */ + +declare(strict_types=1); + +namespace Ramsey\Uuid\Provider\Node; + +use Ramsey\Collection\AbstractCollection; +use Ramsey\Collection\CollectionInterface; +use Ramsey\Uuid\Provider\NodeProviderInterface; +use Ramsey\Uuid\Type\Hexadecimal; + +/** + * A collection of NodeProviderInterface objects + */ +class NodeProviderCollection extends AbstractCollection implements CollectionInterface +{ + public function getType(): string + { + return NodeProviderInterface::class; + } + + /** + * Re-constructs the object from its serialized form + * + * @param string $serialized The serialized PHP string to unserialize into + * a UuidInterface instance + * + * @phpcsSuppress SlevomatCodingStandard.TypeHints.ParameterTypeHint.MissingNativeTypeHint + */ + public function unserialize($serialized): void + { + /** @var mixed[] $data */ + $data = unserialize($serialized, [ + 'allowed_classes' => [ + Hexadecimal::class, + RandomNodeProvider::class, + StaticNodeProvider::class, + SystemNodeProvider::class, + ], + ]); + + $this->data = $data; + } +} diff --git a/vendor/ramsey/uuid/src/Provider/Node/RandomNodeProvider.php b/vendor/ramsey/uuid/src/Provider/Node/RandomNodeProvider.php index 79ec63cb8..266c0b738 100644 --- a/vendor/ramsey/uuid/src/Provider/Node/RandomNodeProvider.php +++ b/vendor/ramsey/uuid/src/Provider/Node/RandomNodeProvider.php @@ -1,4 +1,5 @@ <?php + /** * This file is part of the ramsey/uuid library * @@ -7,33 +8,43 @@ * * @copyright Copyright (c) Ben Ramsey <ben@benramsey.com> * @license http://opensource.org/licenses/MIT MIT - * @link https://benramsey.com/projects/ramsey-uuid/ Documentation - * @link https://packagist.org/packages/ramsey/uuid Packagist - * @link https://github.com/ramsey/uuid GitHub */ +declare(strict_types=1); + namespace Ramsey\Uuid\Provider\Node; -use Exception; +use Ramsey\Uuid\Exception\RandomSourceException; use Ramsey\Uuid\Provider\NodeProviderInterface; +use Ramsey\Uuid\Type\Hexadecimal; + +use function bin2hex; +use function dechex; +use function hex2bin; +use function hexdec; +use function str_pad; +use function substr; + +use const STR_PAD_LEFT; /** - * RandomNodeProvider provides functionality to generate a random node ID, in - * the event that the node ID could not be obtained from the host system + * RandomNodeProvider generates a random node ID * - * @link http://tools.ietf.org/html/rfc4122#section-4.5 + * @link http://tools.ietf.org/html/rfc4122#section-4.5 RFC 4122, § 4.5: Node IDs that Do Not Identify the Host */ class RandomNodeProvider implements NodeProviderInterface { - /** - * Returns the system node ID - * - * @return string System node ID as a hexadecimal string - * @throws Exception if it was not possible to gather sufficient entropy - */ - public function getNode() + public function getNode(): Hexadecimal { - $nodeBytes = random_bytes(6); + try { + $nodeBytes = random_bytes(6); + } catch (\Throwable $exception) { + throw new RandomSourceException( + $exception->getMessage(), + (int) $exception->getCode(), + $exception + ); + } // Split the node bytes for math on 32-bit systems. $nodeMsb = substr($nodeBytes, 0, 3); @@ -52,6 +63,6 @@ class RandomNodeProvider implements NodeProviderInterface // Recombine the node bytes. $node = $nodeMsb . $nodeLsb; - return str_pad(bin2hex($node), 12, '0', STR_PAD_LEFT); + return new Hexadecimal(str_pad(bin2hex($node), 12, '0', STR_PAD_LEFT)); } } diff --git a/vendor/ramsey/uuid/src/Provider/Node/StaticNodeProvider.php b/vendor/ramsey/uuid/src/Provider/Node/StaticNodeProvider.php new file mode 100644 index 000000000..51f1b02ea --- /dev/null +++ b/vendor/ramsey/uuid/src/Provider/Node/StaticNodeProvider.php @@ -0,0 +1,76 @@ +<?php + +/** + * This file is part of the ramsey/uuid library + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + * + * @copyright Copyright (c) Ben Ramsey <ben@benramsey.com> + * @license http://opensource.org/licenses/MIT MIT + */ + +declare(strict_types=1); + +namespace Ramsey\Uuid\Provider\Node; + +use Ramsey\Uuid\Exception\InvalidArgumentException; +use Ramsey\Uuid\Provider\NodeProviderInterface; +use Ramsey\Uuid\Type\Hexadecimal; + +use function dechex; +use function hexdec; +use function str_pad; +use function substr; + +use const STR_PAD_LEFT; + +/** + * StaticNodeProvider provides a static node value with the multicast bit set + * + * @link http://tools.ietf.org/html/rfc4122#section-4.5 RFC 4122, § 4.5: Node IDs that Do Not Identify the Host + */ +class StaticNodeProvider implements NodeProviderInterface +{ + /** + * @var Hexadecimal + */ + private $node; + + /** + * @param Hexadecimal $node The static node value to use + */ + public function __construct(Hexadecimal $node) + { + if (strlen($node->toString()) > 12) { + throw new InvalidArgumentException( + 'Static node value cannot be greater than 12 hexadecimal characters' + ); + } + + $this->node = $this->setMulticastBit($node); + } + + public function getNode(): Hexadecimal + { + return $this->node; + } + + /** + * Set the multicast bit for the static node value + */ + private function setMulticastBit(Hexadecimal $node): Hexadecimal + { + $nodeHex = str_pad($node->toString(), 12, '0', STR_PAD_LEFT); + $firstOctet = substr($nodeHex, 0, 2); + + $firstOctet = str_pad( + dechex(hexdec($firstOctet) | 0x01), + 2, + '0', + STR_PAD_LEFT + ); + + return new Hexadecimal($firstOctet . substr($nodeHex, 2)); + } +} diff --git a/vendor/ramsey/uuid/src/Provider/Node/SystemNodeProvider.php b/vendor/ramsey/uuid/src/Provider/Node/SystemNodeProvider.php index 57015133a..8234abaee 100644 --- a/vendor/ramsey/uuid/src/Provider/Node/SystemNodeProvider.php +++ b/vendor/ramsey/uuid/src/Provider/Node/SystemNodeProvider.php @@ -1,4 +1,5 @@ <?php + /** * This file is part of the ramsey/uuid library * @@ -7,50 +8,86 @@ * * @copyright Copyright (c) Ben Ramsey <ben@benramsey.com> * @license http://opensource.org/licenses/MIT MIT - * @link https://benramsey.com/projects/ramsey-uuid/ Documentation - * @link https://packagist.org/packages/ramsey/uuid Packagist - * @link https://github.com/ramsey/uuid GitHub */ +declare(strict_types=1); + namespace Ramsey\Uuid\Provider\Node; +use Ramsey\Uuid\Exception\NodeException; use Ramsey\Uuid\Provider\NodeProviderInterface; +use Ramsey\Uuid\Type\Hexadecimal; + +use function array_filter; +use function array_map; +use function array_walk; +use function count; +use function ob_get_clean; +use function ob_start; +use function preg_match; +use function preg_match_all; +use function reset; +use function str_replace; +use function strpos; +use function strtolower; +use function strtoupper; +use function substr; + +use const GLOB_NOSORT; +use const PREG_PATTERN_ORDER; /** - * SystemNodeProvider provides functionality to get the system node ID (MAC - * address) using external system calls + * SystemNodeProvider retrieves the system node ID, if possible + * + * The system node ID, or host ID, is often the same as the MAC address for a + * network interface on the host. */ class SystemNodeProvider implements NodeProviderInterface { /** - * Returns the system node ID - * - * @return string|false System node ID as a hexadecimal string, or false if it is not found + * Pattern to match nodes in ifconfig and ipconfig output. + */ + private const IFCONFIG_PATTERN = '/[^:]([0-9a-f]{2}([:-])[0-9a-f]{2}(\2[0-9a-f]{2}){4})[^:]/i'; + + /** + * Pattern to match nodes in sysfs stream output. + */ + private const SYSFS_PATTERN = '/^([0-9a-f]{2}:){5}[0-9a-f]{2}$/i'; + + public function getNode(): Hexadecimal + { + $node = $this->getNodeFromSystem(); + + if ($node === '') { + throw new NodeException( + 'Unable to fetch a node for this system' + ); + } + + return new Hexadecimal($node); + } + + /** + * Returns the system node, if it can find it */ - public function getNode() + protected function getNodeFromSystem(): string { static $node = null; if ($node !== null) { - return $node; + return (string) $node; } - $pattern = '/[^:]([0-9A-Fa-f]{2}([:-])[0-9A-Fa-f]{2}(\2[0-9A-Fa-f]{2}){4})[^:]/'; - $matches = []; - - // first try a linux specific way + // First, try a Linux-specific approach. $node = $this->getSysfs(); - // Search the ifconfig output for all MAC addresses and return - // the first one found - if ($node === false) { - if (preg_match_all($pattern, $this->getIfconfig(), $matches, PREG_PATTERN_ORDER)) { - $node = $matches[1][0]; - } - } - if ($node !== false) { - $node = str_replace([':', '-'], '', $node); + if ($node === '') { + // Search ifconfig output for MAC addresses & return the first one. + $node = $this->getIfconfig(); } + + $node = str_replace([':', '-'], '', $node); + return $node; } @@ -58,11 +95,12 @@ class SystemNodeProvider implements NodeProviderInterface * Returns the network interface configuration for the system * * @codeCoverageIgnore - * @return string */ - protected function getIfconfig() + protected function getIfconfig(): string { - if (strpos(strtolower(ini_get('disable_functions')), 'passthru') !== false) { + $disabledFunctions = strtolower((string) ini_get('disable_functions')); + + if (strpos($disabledFunctions, 'passthru') !== false) { return ''; } @@ -70,40 +108,50 @@ class SystemNodeProvider implements NodeProviderInterface switch (strtoupper(substr(constant('PHP_OS'), 0, 3))) { case 'WIN': passthru('ipconfig /all 2>&1'); + break; case 'DAR': passthru('ifconfig 2>&1'); + break; case 'FRE': passthru('netstat -i -f link 2>&1'); + break; case 'LIN': default: passthru('netstat -ie 2>&1'); + break; } - return ob_get_clean(); + $ifconfig = (string) ob_get_clean(); + + $node = ''; + if (preg_match_all(self::IFCONFIG_PATTERN, $ifconfig, $matches, PREG_PATTERN_ORDER)) { + $node = $matches[1][0] ?? ''; + } + + return (string) $node; } /** - * Returns mac address from the first system interface via the sysfs interface - * - * @return string|bool + * Returns MAC address from the first system interface via the sysfs interface */ - protected function getSysfs() + protected function getSysfs(): string { - $mac = false; + $mac = ''; if (strtoupper(constant('PHP_OS')) === 'LINUX') { $addressPaths = glob('/sys/class/net/*/address', GLOB_NOSORT); - if (empty($addressPaths)) { - return false; + if ($addressPaths === false || count($addressPaths) === 0) { + return ''; } $macs = []; - array_walk($addressPaths, function ($addressPath) use (&$macs) { + + array_walk($addressPaths, function (string $addressPath) use (&$macs): void { if (is_readable($addressPath)) { $macs[] = file_get_contents($addressPath); } @@ -111,18 +159,15 @@ class SystemNodeProvider implements NodeProviderInterface $macs = array_map('trim', $macs); - // remove invalid entries - $macs = array_filter($macs, function ($mac) { - return - // localhost adapter - $mac !== '00:00:00:00:00:00' && - // must match mac adress - preg_match('/^([0-9a-f]{2}:){5}[0-9a-f]{2}$/i', $mac); + // Remove invalid entries. + $macs = array_filter($macs, function (string $address) { + return $address !== '00:00:00:00:00:00' + && preg_match(self::SYSFS_PATTERN, $address); }); $mac = reset($macs); } - return $mac; + return (string) $mac; } } |