googleanalytics
/
lib
/
analytics-admin
/
vendor
/
ramsey
/
uuid
/
src
/
Provider
/
Node
/
SystemNodeProvider.php
googleanalytics
/
lib
/
analytics-admin
/
vendor
/
ramsey
/
uuid
/
src
/
Provider
/
Node
Last commit date
FallbackNodeProvider.php
3 years ago
NodeProviderCollection.php
3 years ago
RandomNodeProvider.php
3 years ago
StaticNodeProvider.php
3 years ago
SystemNodeProvider.php
3 years ago
SystemNodeProvider.php
174 lines
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * This file is part of the ramsey/uuid library |
| 5 | * |
| 6 | * For the full copyright and license information, please view the LICENSE |
| 7 | * file that was distributed with this source code. |
| 8 | * |
| 9 | * @copyright Copyright (c) Ben Ramsey <ben@benramsey.com> |
| 10 | * @license http://opensource.org/licenses/MIT MIT |
| 11 | */ |
| 12 | |
| 13 | declare(strict_types=1); |
| 14 | |
| 15 | namespace Ramsey\Uuid\Provider\Node; |
| 16 | |
| 17 | use Ramsey\Uuid\Exception\NodeException; |
| 18 | use Ramsey\Uuid\Provider\NodeProviderInterface; |
| 19 | use Ramsey\Uuid\Type\Hexadecimal; |
| 20 | |
| 21 | use function array_filter; |
| 22 | use function array_map; |
| 23 | use function array_walk; |
| 24 | use function count; |
| 25 | use function ob_get_clean; |
| 26 | use function ob_start; |
| 27 | use function preg_match; |
| 28 | use function preg_match_all; |
| 29 | use function reset; |
| 30 | use function str_replace; |
| 31 | use function strpos; |
| 32 | use function strtolower; |
| 33 | use function strtoupper; |
| 34 | use function substr; |
| 35 | |
| 36 | use const GLOB_NOSORT; |
| 37 | use const PREG_PATTERN_ORDER; |
| 38 | |
| 39 | /** |
| 40 | * SystemNodeProvider retrieves the system node ID, if possible |
| 41 | * |
| 42 | * The system node ID, or host ID, is often the same as the MAC address for a |
| 43 | * network interface on the host. |
| 44 | */ |
| 45 | class SystemNodeProvider implements NodeProviderInterface |
| 46 | { |
| 47 | /** |
| 48 | * Pattern to match nodes in ifconfig and ipconfig output. |
| 49 | */ |
| 50 | private const IFCONFIG_PATTERN = '/[^:]([0-9a-f]{2}([:-])[0-9a-f]{2}(\2[0-9a-f]{2}){4})[^:]/i'; |
| 51 | |
| 52 | /** |
| 53 | * Pattern to match nodes in sysfs stream output. |
| 54 | */ |
| 55 | private const SYSFS_PATTERN = '/^([0-9a-f]{2}:){5}[0-9a-f]{2}$/i'; |
| 56 | |
| 57 | public function getNode(): Hexadecimal |
| 58 | { |
| 59 | $node = $this->getNodeFromSystem(); |
| 60 | |
| 61 | if ($node === '') { |
| 62 | throw new NodeException( |
| 63 | 'Unable to fetch a node for this system' |
| 64 | ); |
| 65 | } |
| 66 | |
| 67 | return new Hexadecimal($node); |
| 68 | } |
| 69 | |
| 70 | /** |
| 71 | * Returns the system node, if it can find it |
| 72 | */ |
| 73 | protected function getNodeFromSystem(): string |
| 74 | { |
| 75 | static $node = null; |
| 76 | |
| 77 | if ($node !== null) { |
| 78 | return (string) $node; |
| 79 | } |
| 80 | |
| 81 | // First, try a Linux-specific approach. |
| 82 | $node = $this->getSysfs(); |
| 83 | |
| 84 | if ($node === '') { |
| 85 | // Search ifconfig output for MAC addresses & return the first one. |
| 86 | $node = $this->getIfconfig(); |
| 87 | } |
| 88 | |
| 89 | $node = str_replace([':', '-'], '', $node); |
| 90 | |
| 91 | return $node; |
| 92 | } |
| 93 | |
| 94 | /** |
| 95 | * Returns the network interface configuration for the system |
| 96 | * |
| 97 | * @codeCoverageIgnore |
| 98 | */ |
| 99 | protected function getIfconfig(): string |
| 100 | { |
| 101 | $disabledFunctions = strtolower((string) ini_get('disable_functions')); |
| 102 | |
| 103 | if (strpos($disabledFunctions, 'passthru') !== false) { |
| 104 | return ''; |
| 105 | } |
| 106 | |
| 107 | ob_start(); |
| 108 | switch (strtoupper(substr(constant('PHP_OS'), 0, 3))) { |
| 109 | case 'WIN': |
| 110 | passthru('ipconfig /all 2>&1'); |
| 111 | |
| 112 | break; |
| 113 | case 'DAR': |
| 114 | passthru('ifconfig 2>&1'); |
| 115 | |
| 116 | break; |
| 117 | case 'FRE': |
| 118 | passthru('netstat -i -f link 2>&1'); |
| 119 | |
| 120 | break; |
| 121 | case 'LIN': |
| 122 | default: |
| 123 | passthru('netstat -ie 2>&1'); |
| 124 | |
| 125 | break; |
| 126 | } |
| 127 | |
| 128 | $ifconfig = (string) ob_get_clean(); |
| 129 | |
| 130 | $node = ''; |
| 131 | if (preg_match_all(self::IFCONFIG_PATTERN, $ifconfig, $matches, PREG_PATTERN_ORDER)) { |
| 132 | $node = $matches[1][0] ?? ''; |
| 133 | } |
| 134 | |
| 135 | return $node; |
| 136 | } |
| 137 | |
| 138 | /** |
| 139 | * Returns MAC address from the first system interface via the sysfs interface |
| 140 | */ |
| 141 | protected function getSysfs(): string |
| 142 | { |
| 143 | $mac = ''; |
| 144 | |
| 145 | if (strtoupper(constant('PHP_OS')) === 'LINUX') { |
| 146 | $addressPaths = glob('/sys/class/net/*/address', GLOB_NOSORT); |
| 147 | |
| 148 | if ($addressPaths === false || count($addressPaths) === 0) { |
| 149 | return ''; |
| 150 | } |
| 151 | |
| 152 | $macs = []; |
| 153 | |
| 154 | array_walk($addressPaths, function (string $addressPath) use (&$macs): void { |
| 155 | if (is_readable($addressPath)) { |
| 156 | $macs[] = file_get_contents($addressPath); |
| 157 | } |
| 158 | }); |
| 159 | |
| 160 | $macs = array_map('trim', $macs); |
| 161 | |
| 162 | // Remove invalid entries. |
| 163 | $macs = array_filter($macs, function (string $address) { |
| 164 | return $address !== '00:00:00:00:00:00' |
| 165 | && preg_match(self::SYSFS_PATTERN, $address); |
| 166 | }); |
| 167 | |
| 168 | $mac = reset($macs); |
| 169 | } |
| 170 | |
| 171 | return (string) $mac; |
| 172 | } |
| 173 | } |
| 174 |