Upgrade framework
This commit is contained in:
231
vendor/ramsey/uuid/src/Provider/Dce/SystemDceSecurityProvider.php
vendored
Normal file
231
vendor/ramsey/uuid/src/Provider/Dce/SystemDceSecurityProvider.php
vendored
Normal file
@@ -0,0 +1,231 @@
|
||||
<?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\Dce;
|
||||
|
||||
use Ramsey\Uuid\Exception\DceSecurityException;
|
||||
use Ramsey\Uuid\Provider\DceSecurityProviderInterface;
|
||||
use Ramsey\Uuid\Type\Integer as IntegerObject;
|
||||
|
||||
use function escapeshellarg;
|
||||
use function preg_split;
|
||||
use function str_getcsv;
|
||||
use function strrpos;
|
||||
use function strtolower;
|
||||
use function strtoupper;
|
||||
use function substr;
|
||||
use function trim;
|
||||
|
||||
use const PREG_SPLIT_NO_EMPTY;
|
||||
|
||||
/**
|
||||
* SystemDceSecurityProvider retrieves the user or group identifiers from the system
|
||||
*/
|
||||
class SystemDceSecurityProvider implements DceSecurityProviderInterface
|
||||
{
|
||||
/**
|
||||
* @throws DceSecurityException if unable to get a user identifier
|
||||
*
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function getUid(): IntegerObject
|
||||
{
|
||||
/** @var int|float|string|IntegerObject|null $uid */
|
||||
static $uid = null;
|
||||
|
||||
if ($uid instanceof IntegerObject) {
|
||||
return $uid;
|
||||
}
|
||||
|
||||
if ($uid === null) {
|
||||
$uid = $this->getSystemUid();
|
||||
}
|
||||
|
||||
if ($uid === '') {
|
||||
throw new DceSecurityException(
|
||||
'Unable to get a user identifier using the system DCE '
|
||||
. 'Security provider; please provide a custom identifier or '
|
||||
. 'use a different provider'
|
||||
);
|
||||
}
|
||||
|
||||
$uid = new IntegerObject($uid);
|
||||
|
||||
return $uid;
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws DceSecurityException if unable to get a group identifier
|
||||
*
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function getGid(): IntegerObject
|
||||
{
|
||||
/** @var int|float|string|IntegerObject|null $gid */
|
||||
static $gid = null;
|
||||
|
||||
if ($gid instanceof IntegerObject) {
|
||||
return $gid;
|
||||
}
|
||||
|
||||
if ($gid === null) {
|
||||
$gid = $this->getSystemGid();
|
||||
}
|
||||
|
||||
if ($gid === '') {
|
||||
throw new DceSecurityException(
|
||||
'Unable to get a group identifier using the system DCE '
|
||||
. 'Security provider; please provide a custom identifier or '
|
||||
. 'use a different provider'
|
||||
);
|
||||
}
|
||||
|
||||
$gid = new IntegerObject($gid);
|
||||
|
||||
return $gid;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the UID from the system
|
||||
*/
|
||||
private function getSystemUid(): string
|
||||
{
|
||||
if (!$this->hasShellExec()) {
|
||||
return '';
|
||||
}
|
||||
|
||||
return match ($this->getOs()) {
|
||||
'WIN' => $this->getWindowsUid(),
|
||||
default => trim((string) shell_exec('id -u')),
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the GID from the system
|
||||
*/
|
||||
private function getSystemGid(): string
|
||||
{
|
||||
if (!$this->hasShellExec()) {
|
||||
return '';
|
||||
}
|
||||
|
||||
return match ($this->getOs()) {
|
||||
'WIN' => $this->getWindowsGid(),
|
||||
default => trim((string) shell_exec('id -g')),
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns true if shell_exec() is available for use
|
||||
*/
|
||||
private function hasShellExec(): bool
|
||||
{
|
||||
$disabledFunctions = strtolower((string) ini_get('disable_functions'));
|
||||
|
||||
return !str_contains($disabledFunctions, 'shell_exec');
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the PHP_OS string
|
||||
*/
|
||||
private function getOs(): string
|
||||
{
|
||||
/**
|
||||
* @psalm-suppress UnnecessaryVarAnnotation
|
||||
* @var string $phpOs
|
||||
*/
|
||||
$phpOs = constant('PHP_OS');
|
||||
|
||||
return strtoupper(substr($phpOs, 0, 3));
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the user identifier for a user on a Windows system
|
||||
*
|
||||
* Windows does not have the same concept as an effective POSIX UID for the
|
||||
* running script. Instead, each user is uniquely identified by an SID
|
||||
* (security identifier). The SID includes three 32-bit unsigned integers
|
||||
* that make up a unique domain identifier, followed by an RID (relative
|
||||
* identifier) that we will use as the UID. The primary caveat is that this
|
||||
* UID may not be unique to the system, since it is, instead, unique to the
|
||||
* domain.
|
||||
*
|
||||
* @link https://www.lifewire.com/what-is-an-sid-number-2626005 What Is an SID Number?
|
||||
* @link https://bit.ly/30vE7NM Well-known SID Structures
|
||||
* @link https://bit.ly/2FWcYKJ Well-known security identifiers in Windows operating systems
|
||||
* @link https://www.windows-commandline.com/get-sid-of-user/ Get SID of user
|
||||
*/
|
||||
private function getWindowsUid(): string
|
||||
{
|
||||
$response = shell_exec('whoami /user /fo csv /nh');
|
||||
|
||||
if ($response === null) {
|
||||
return '';
|
||||
}
|
||||
|
||||
$sid = str_getcsv(trim((string) $response))[1] ?? '';
|
||||
|
||||
if (($lastHyphen = strrpos($sid, '-')) === false) {
|
||||
return '';
|
||||
}
|
||||
|
||||
return trim(substr($sid, $lastHyphen + 1));
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a group identifier for a user on a Windows system
|
||||
*
|
||||
* Since Windows does not have the same concept as an effective POSIX GID
|
||||
* for the running script, we will get the local group memberships for the
|
||||
* user running the script. Then, we will get the SID (security identifier)
|
||||
* for the first group that appears in that list. Finally, we will return
|
||||
* the RID (relative identifier) for the group and use that as the GID.
|
||||
*
|
||||
* @link https://www.windows-commandline.com/list-of-user-groups-command-line/ List of user groups command line
|
||||
*/
|
||||
private function getWindowsGid(): string
|
||||
{
|
||||
$response = shell_exec('net user %username% | findstr /b /i "Local Group Memberships"');
|
||||
|
||||
if ($response === null) {
|
||||
return '';
|
||||
}
|
||||
|
||||
/** @var string[] $userGroups */
|
||||
$userGroups = preg_split('/\s{2,}/', (string) $response, -1, PREG_SPLIT_NO_EMPTY);
|
||||
|
||||
$firstGroup = trim($userGroups[1] ?? '', "* \t\n\r\0\x0B");
|
||||
|
||||
if ($firstGroup === '') {
|
||||
return '';
|
||||
}
|
||||
|
||||
$response = shell_exec('wmic group get name,sid | findstr /b /i ' . escapeshellarg($firstGroup));
|
||||
|
||||
if ($response === null) {
|
||||
return '';
|
||||
}
|
||||
|
||||
/** @var string[] $userGroup */
|
||||
$userGroup = preg_split('/\s{2,}/', (string) $response, -1, PREG_SPLIT_NO_EMPTY);
|
||||
|
||||
$sid = $userGroup[1] ?? '';
|
||||
|
||||
if (($lastHyphen = strrpos($sid, '-')) === false) {
|
||||
return '';
|
||||
}
|
||||
|
||||
return trim(substr($sid, $lastHyphen + 1));
|
||||
}
|
||||
}
|
||||
41
vendor/ramsey/uuid/src/Provider/DceSecurityProviderInterface.php
vendored
Normal file
41
vendor/ramsey/uuid/src/Provider/DceSecurityProviderInterface.php
vendored
Normal file
@@ -0,0 +1,41 @@
|
||||
<?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;
|
||||
|
||||
use Ramsey\Uuid\Rfc4122\UuidV2;
|
||||
use Ramsey\Uuid\Type\Integer as IntegerObject;
|
||||
|
||||
/**
|
||||
* A DCE provider provides access to local domain identifiers for version 2,
|
||||
* DCE Security, UUIDs
|
||||
*
|
||||
* @see UuidV2
|
||||
*/
|
||||
interface DceSecurityProviderInterface
|
||||
{
|
||||
/**
|
||||
* Returns a user identifier for the system
|
||||
*
|
||||
* @link https://en.wikipedia.org/wiki/User_identifier User identifier
|
||||
*/
|
||||
public function getUid(): IntegerObject;
|
||||
|
||||
/**
|
||||
* Returns a group identifier for the system
|
||||
*
|
||||
* @link https://en.wikipedia.org/wiki/Group_identifier Group identifier
|
||||
*/
|
||||
public function getGid(): IntegerObject;
|
||||
}
|
||||
@@ -1,4 +1,5 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* This file is part of the ramsey/uuid library
|
||||
*
|
||||
@@ -7,51 +8,47 @@
|
||||
*
|
||||
* @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;
|
||||
|
||||
/**
|
||||
* 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[]
|
||||
* @param iterable<NodeProviderInterface> $providers Array of node providers
|
||||
*/
|
||||
private $nodeProviders;
|
||||
|
||||
/**
|
||||
* Constructs a `FallbackNodeProvider` using an array of node providers
|
||||
*
|
||||
* @param NodeProviderInterface[] $providers Array of node providers
|
||||
*/
|
||||
public function __construct(array $providers)
|
||||
public function __construct(private iterable $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
|
||||
*/
|
||||
public function getNode()
|
||||
public function getNode(): Hexadecimal
|
||||
{
|
||||
foreach ($this->nodeProviders as $provider) {
|
||||
if ($node = $provider->getNode()) {
|
||||
return $node;
|
||||
$lastProviderException = null;
|
||||
|
||||
foreach ($this->providers as $provider) {
|
||||
try {
|
||||
return $provider->getNode();
|
||||
} catch (NodeException $exception) {
|
||||
$lastProviderException = $exception;
|
||||
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
throw new NodeException(
|
||||
'Unable to find a suitable node provider',
|
||||
0,
|
||||
$lastProviderException
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
66
vendor/ramsey/uuid/src/Provider/Node/NodeProviderCollection.php
vendored
Normal file
66
vendor/ramsey/uuid/src/Provider/Node/NodeProviderCollection.php
vendored
Normal file
@@ -0,0 +1,66 @@
|
||||
<?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\Uuid\Provider\NodeProviderInterface;
|
||||
use Ramsey\Uuid\Type\Hexadecimal;
|
||||
|
||||
/**
|
||||
* A collection of NodeProviderInterface objects
|
||||
*
|
||||
* @deprecated this class has been deprecated, and will be removed in 5.0.0. The use-case for this class comes from
|
||||
* a pre-`phpstan/phpstan` and pre-`vimeo/psalm` ecosystem, in which type safety had to be mostly enforced
|
||||
* at runtime: that is no longer necessary, now that you can safely verify your code to be correct, and use
|
||||
* more generic types like `iterable<T>` instead.
|
||||
*
|
||||
* @extends AbstractCollection<NodeProviderInterface>
|
||||
*/
|
||||
class NodeProviderCollection extends AbstractCollection
|
||||
{
|
||||
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
|
||||
* @psalm-suppress RedundantConditionGivenDocblockType
|
||||
*/
|
||||
public function unserialize($serialized): void
|
||||
{
|
||||
/** @var array<array-key, NodeProviderInterface> $data */
|
||||
$data = unserialize($serialized, [
|
||||
'allowed_classes' => [
|
||||
Hexadecimal::class,
|
||||
RandomNodeProvider::class,
|
||||
StaticNodeProvider::class,
|
||||
SystemNodeProvider::class,
|
||||
],
|
||||
]);
|
||||
|
||||
$this->data = array_filter(
|
||||
$data,
|
||||
function ($unserialized): bool {
|
||||
return $unserialized instanceof NodeProviderInterface;
|
||||
}
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,5 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* This file is part of the ramsey/uuid library
|
||||
*
|
||||
@@ -7,30 +8,62 @@
|
||||
*
|
||||
* @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\RandomSourceException;
|
||||
use Ramsey\Uuid\Provider\NodeProviderInterface;
|
||||
use Ramsey\Uuid\Type\Hexadecimal;
|
||||
use Throwable;
|
||||
|
||||
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
|
||||
*/
|
||||
public function getNode()
|
||||
public function getNode(): Hexadecimal
|
||||
{
|
||||
return sprintf('%06x%06x', mt_rand(0, 0xffffff), mt_rand(0, 0xffffff));
|
||||
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);
|
||||
$nodeLsb = substr($nodeBytes, 3);
|
||||
|
||||
// Set the multicast bit; see RFC 4122, section 4.5.
|
||||
$nodeMsb = hex2bin(
|
||||
str_pad(
|
||||
dechex(hexdec(bin2hex($nodeMsb)) | 0x010000),
|
||||
6,
|
||||
'0',
|
||||
STR_PAD_LEFT
|
||||
)
|
||||
);
|
||||
|
||||
// Recombine the node bytes.
|
||||
$node = $nodeMsb . $nodeLsb;
|
||||
|
||||
return new Hexadecimal(str_pad(bin2hex($node), 12, '0', STR_PAD_LEFT));
|
||||
}
|
||||
}
|
||||
|
||||
73
vendor/ramsey/uuid/src/Provider/Node/StaticNodeProvider.php
vendored
Normal file
73
vendor/ramsey/uuid/src/Provider/Node/StaticNodeProvider.php
vendored
Normal file
@@ -0,0 +1,73 @@
|
||||
<?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
|
||||
{
|
||||
private Hexadecimal $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));
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,5 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* This file is part of the ramsey/uuid library
|
||||
*
|
||||
@@ -7,45 +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_contains;
|
||||
use function str_replace;
|
||||
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.
|
||||
*/
|
||||
public function getNode()
|
||||
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
|
||||
*/
|
||||
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 = array();
|
||||
// First, try a Linux-specific approach.
|
||||
$node = $this->getSysfs();
|
||||
|
||||
// Search the ifconfig output for all MAC addresses and return
|
||||
// the first one found
|
||||
$node = false;
|
||||
if (preg_match_all($pattern, $this->getIfconfig(), $matches, PREG_PATTERN_ORDER)) {
|
||||
$node = $matches[1][0];
|
||||
$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;
|
||||
}
|
||||
|
||||
@@ -53,24 +95,99 @@ class SystemNodeProvider implements NodeProviderInterface
|
||||
* Returns the network interface configuration for the system
|
||||
*
|
||||
* @codeCoverageIgnore
|
||||
* @return string
|
||||
*/
|
||||
protected function getIfconfig()
|
||||
protected function getIfconfig(): string
|
||||
{
|
||||
$disabledFunctions = strtolower((string) ini_get('disable_functions'));
|
||||
|
||||
if (str_contains($disabledFunctions, 'passthru')) {
|
||||
return '';
|
||||
}
|
||||
|
||||
/**
|
||||
* @psalm-suppress UnnecessaryVarAnnotation
|
||||
* @var string $phpOs
|
||||
*/
|
||||
$phpOs = constant('PHP_OS');
|
||||
|
||||
ob_start();
|
||||
switch (strtoupper(substr(php_uname('a'), 0, 3))) {
|
||||
switch (strtoupper(substr($phpOs, 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();
|
||||
|
||||
if (preg_match_all(self::IFCONFIG_PATTERN, $ifconfig, $matches, PREG_PATTERN_ORDER)) {
|
||||
foreach ($matches[1] as $iface) {
|
||||
if ($iface !== '00:00:00:00:00:00' && $iface !== '00-00-00-00-00-00') {
|
||||
return $iface;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return '';
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns MAC address from the first system interface via the sysfs interface
|
||||
*/
|
||||
protected function getSysfs(): string
|
||||
{
|
||||
$mac = '';
|
||||
|
||||
/**
|
||||
* @psalm-suppress UnnecessaryVarAnnotation
|
||||
* @var string $phpOs
|
||||
*/
|
||||
$phpOs = constant('PHP_OS');
|
||||
|
||||
if (strtoupper($phpOs) === 'LINUX') {
|
||||
$addressPaths = glob('/sys/class/net/*/address', GLOB_NOSORT);
|
||||
|
||||
if ($addressPaths === false || count($addressPaths) === 0) {
|
||||
return '';
|
||||
}
|
||||
|
||||
/** @var array<array-key, string> $macs */
|
||||
$macs = [];
|
||||
|
||||
array_walk($addressPaths, function (string $addressPath) use (&$macs): void {
|
||||
if (is_readable($addressPath)) {
|
||||
$macs[] = file_get_contents($addressPath);
|
||||
}
|
||||
});
|
||||
|
||||
/** @var callable $trim */
|
||||
$trim = 'trim';
|
||||
|
||||
$macs = array_map($trim, $macs);
|
||||
|
||||
// Remove invalid entries.
|
||||
$macs = array_filter($macs, function (string $address) {
|
||||
return $address !== '00:00:00:00:00:00'
|
||||
&& preg_match(self::SYSFS_PATTERN, $address);
|
||||
});
|
||||
|
||||
/** @var string|bool $mac */
|
||||
$mac = reset($macs);
|
||||
}
|
||||
|
||||
return (string) $mac;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* This file is part of the ramsey/uuid library
|
||||
*
|
||||
@@ -7,23 +8,23 @@
|
||||
*
|
||||
* @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;
|
||||
|
||||
use Ramsey\Uuid\Type\Hexadecimal;
|
||||
|
||||
/**
|
||||
* NodeProviderInterface provides functionality to get the node ID (or host ID
|
||||
* in the form of the system's MAC address) from a specific type of node provider
|
||||
* A node provider retrieves or generates a node ID
|
||||
*/
|
||||
interface NodeProviderInterface
|
||||
{
|
||||
/**
|
||||
* Returns the system node ID
|
||||
* Returns a node ID
|
||||
*
|
||||
* @return string System node ID as a hexadecimal string
|
||||
* @return Hexadecimal The node ID as a hexadecimal string
|
||||
*/
|
||||
public function getNode();
|
||||
public function getNode(): Hexadecimal;
|
||||
}
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* This file is part of the ramsey/uuid library
|
||||
*
|
||||
@@ -7,70 +8,50 @@
|
||||
*
|
||||
* @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\Time;
|
||||
|
||||
use Ramsey\Uuid\Provider\TimeProviderInterface;
|
||||
use Ramsey\Uuid\Type\Integer as IntegerObject;
|
||||
use Ramsey\Uuid\Type\Time;
|
||||
|
||||
/**
|
||||
* FixedTimeProvider uses an previously-generated timestamp to provide the time
|
||||
* FixedTimeProvider uses a known time to provide the time
|
||||
*
|
||||
* This provider allows the use of a previously-generated timestamp, such as one
|
||||
* stored in a database, when creating version 1 UUIDs.
|
||||
* This provider allows the use of a previously-generated, or known, time
|
||||
* when generating time-based UUIDs.
|
||||
*/
|
||||
class FixedTimeProvider implements TimeProviderInterface
|
||||
{
|
||||
/**
|
||||
* @var int[] Array containing `sec` and `usec` components of a timestamp
|
||||
*/
|
||||
private $fixedTime;
|
||||
|
||||
/**
|
||||
* Constructs a `FixedTimeProvider` using the provided `$timestamp`
|
||||
*
|
||||
* @param int[] Array containing `sec` and `usec` components of a timestamp
|
||||
* @throws \InvalidArgumentException if the `$timestamp` does not contain `sec` or `usec` components
|
||||
*/
|
||||
public function __construct(array $timestamp)
|
||||
public function __construct(private Time $time)
|
||||
{
|
||||
if (!array_key_exists('sec', $timestamp) || !array_key_exists('usec', $timestamp)) {
|
||||
throw new \InvalidArgumentException('Array must contain sec and usec keys.');
|
||||
}
|
||||
|
||||
$this->fixedTime = $timestamp;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the `usec` component of the timestamp
|
||||
* Sets the `usec` component of the time
|
||||
*
|
||||
* @param int $value The `usec` value to set
|
||||
* @param int|string|IntegerObject $value The `usec` value to set
|
||||
*/
|
||||
public function setUsec($value)
|
||||
public function setUsec($value): void
|
||||
{
|
||||
$this->fixedTime['usec'] = $value;
|
||||
$this->time = new Time($this->time->getSeconds(), $value);
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the `sec` component of the timestamp
|
||||
* Sets the `sec` component of the time
|
||||
*
|
||||
* @param int $value The `sec` value to set
|
||||
* @param int|string|IntegerObject $value The `sec` value to set
|
||||
*/
|
||||
public function setSec($value)
|
||||
public function setSec($value): void
|
||||
{
|
||||
$this->fixedTime['sec'] = $value;
|
||||
$this->time = new Time($value, $this->time->getMicroseconds());
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a timestamp array
|
||||
*
|
||||
* @return int[] Array containing `sec` and `usec` components of a timestamp
|
||||
*/
|
||||
public function currentTime()
|
||||
public function getTime(): Time
|
||||
{
|
||||
return $this->fixedTime;
|
||||
return $this->time;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* This file is part of the ramsey/uuid library
|
||||
*
|
||||
@@ -7,27 +8,26 @@
|
||||
*
|
||||
* @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\Time;
|
||||
|
||||
use Ramsey\Uuid\Provider\TimeProviderInterface;
|
||||
use Ramsey\Uuid\Type\Time;
|
||||
|
||||
use function gettimeofday;
|
||||
|
||||
/**
|
||||
* SystemTimeProvider uses built-in PHP functions to provide the time
|
||||
* SystemTimeProvider retrieves the current time using built-in PHP functions
|
||||
*/
|
||||
class SystemTimeProvider implements TimeProviderInterface
|
||||
{
|
||||
/**
|
||||
* Returns a timestamp array
|
||||
*
|
||||
* @return int[] Array containing `sec` and `usec` components of a timestamp
|
||||
*/
|
||||
public function currentTime()
|
||||
public function getTime(): Time
|
||||
{
|
||||
return gettimeofday();
|
||||
$time = gettimeofday();
|
||||
|
||||
return new Time($time['sec'], $time['usec']);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* This file is part of the ramsey/uuid library
|
||||
*
|
||||
@@ -7,23 +8,21 @@
|
||||
*
|
||||
* @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;
|
||||
|
||||
use Ramsey\Uuid\Type\Time;
|
||||
|
||||
/**
|
||||
* TimeProviderInterface provides functionality to get the time from a specific
|
||||
* type of time provider
|
||||
* A time provider retrieves the current time
|
||||
*/
|
||||
interface TimeProviderInterface
|
||||
{
|
||||
/**
|
||||
* Returns a timestamp array
|
||||
*
|
||||
* @return int[] Array guaranteed to contain `sec` and `usec` components of a timestamp
|
||||
* Returns a time object
|
||||
*/
|
||||
public function currentTime();
|
||||
public function getTime(): Time;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user