media-cloud-sync
/
includes
/
sdk
/
google
/
ramsey
/
uuid
/
src
/
Provider
/
Node
/
SystemNodeProvider.php
SystemNodeProvider.php in Media Cloud Sync 1.2.12, at includes/sdk/google/ramsey/uuid/src/Provider/Node/SystemNodeProvider.php
| 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 | declare (strict_types=1); |
| 13 | namespace Dudlewebs\WPMCS\Ramsey\Uuid\Provider\Node; |
| 14 | |
| 15 | use Dudlewebs\WPMCS\Ramsey\Uuid\Exception\NodeException; |
| 16 | use Dudlewebs\WPMCS\Ramsey\Uuid\Provider\NodeProviderInterface; |
| 17 | use Dudlewebs\WPMCS\Ramsey\Uuid\Type\Hexadecimal; |
| 18 | use function array_filter; |
| 19 | use function array_map; |
| 20 | use function array_walk; |
| 21 | use function count; |
| 22 | use function ob_get_clean; |
| 23 | use function ob_start; |
| 24 | use function preg_match; |
| 25 | use function preg_match_all; |
| 26 | use function reset; |
| 27 | use function str_replace; |
| 28 | use function strpos; |
| 29 | use function strtolower; |
| 30 | use function strtoupper; |
| 31 | use function substr; |
| 32 | use const GLOB_NOSORT; |
| 33 | use const PREG_PATTERN_ORDER; |
| 34 | /** |
| 35 | * SystemNodeProvider retrieves the system node ID, if possible |
| 36 | * |
| 37 | * The system node ID, or host ID, is often the same as the MAC address for a |
| 38 | * network interface on the host. |
| 39 | */ |
| 40 | class SystemNodeProvider implements NodeProviderInterface |
| 41 | { |
| 42 | /** |
| 43 | * Pattern to match nodes in ifconfig and ipconfig output. |
| 44 | */ |
| 45 | private const IFCONFIG_PATTERN = '/[^:]([0-9a-f]{2}([:-])[0-9a-f]{2}(\2[0-9a-f]{2}){4})[^:]/i'; |
| 46 | /** |
| 47 | * Pattern to match nodes in sysfs stream output. |
| 48 | */ |
| 49 | private const SYSFS_PATTERN = '/^([0-9a-f]{2}:){5}[0-9a-f]{2}$/i'; |
| 50 | public function getNode(): Hexadecimal |
| 51 | { |
| 52 | $node = $this->getNodeFromSystem(); |
| 53 | if ($node === '') { |
| 54 | throw new NodeException('Unable to fetch a node for this system'); |
| 55 | } |
| 56 | return new Hexadecimal($node); |
| 57 | } |
| 58 | /** |
| 59 | * Returns the system node, if it can find it |
| 60 | */ |
| 61 | protected function getNodeFromSystem(): string |
| 62 | { |
| 63 | static $node = null; |
| 64 | if ($node !== null) { |
| 65 | return (string) $node; |
| 66 | } |
| 67 | // First, try a Linux-specific approach. |
| 68 | $node = $this->getSysfs(); |
| 69 | if ($node === '') { |
| 70 | // Search ifconfig output for MAC addresses & return the first one. |
| 71 | $node = $this->getIfconfig(); |
| 72 | } |
| 73 | $node = str_replace([':', '-'], '', $node); |
| 74 | return $node; |
| 75 | } |
| 76 | /** |
| 77 | * Returns the network interface configuration for the system |
| 78 | * |
| 79 | * @codeCoverageIgnore |
| 80 | */ |
| 81 | protected function getIfconfig(): string |
| 82 | { |
| 83 | $disabledFunctions = strtolower((string) ini_get('disable_functions')); |
| 84 | if (strpos($disabledFunctions, 'passthru') !== \false) { |
| 85 | return ''; |
| 86 | } |
| 87 | ob_start(); |
| 88 | switch (strtoupper(substr(constant('PHP_OS'), 0, 3))) { |
| 89 | case 'WIN': |
| 90 | passthru('ipconfig /all 2>&1'); |
| 91 | break; |
| 92 | case 'DAR': |
| 93 | passthru('ifconfig 2>&1'); |
| 94 | break; |
| 95 | case 'FRE': |
| 96 | passthru('netstat -i -f link 2>&1'); |
| 97 | break; |
| 98 | case 'LIN': |
| 99 | default: |
| 100 | passthru('netstat -ie 2>&1'); |
| 101 | break; |
| 102 | } |
| 103 | $ifconfig = (string) ob_get_clean(); |
| 104 | $node = ''; |
| 105 | if (preg_match_all(self::IFCONFIG_PATTERN, $ifconfig, $matches, PREG_PATTERN_ORDER)) { |
| 106 | $node = $matches[1][0] ?? ''; |
| 107 | } |
| 108 | return $node; |
| 109 | } |
| 110 | /** |
| 111 | * Returns MAC address from the first system interface via the sysfs interface |
| 112 | */ |
| 113 | protected function getSysfs(): string |
| 114 | { |
| 115 | $mac = ''; |
| 116 | if (strtoupper(constant('PHP_OS')) === 'LINUX') { |
| 117 | $addressPaths = glob('/sys/class/net/*/address', GLOB_NOSORT); |
| 118 | if ($addressPaths === \false || count($addressPaths) === 0) { |
| 119 | return ''; |
| 120 | } |
| 121 | $macs = []; |
| 122 | array_walk($addressPaths, function (string $addressPath) use (&$macs): void { |
| 123 | if (is_readable($addressPath)) { |
| 124 | $macs[] = file_get_contents($addressPath); |
| 125 | } |
| 126 | }); |
| 127 | $macs = array_map('trim', $macs); |
| 128 | // Remove invalid entries. |
| 129 | $macs = array_filter($macs, function (string $address) { |
| 130 | return $address !== '00:00:00:00:00:00' && preg_match(self::SYSFS_PATTERN, $address); |
| 131 | }); |
| 132 | $mac = reset($macs); |
| 133 | } |
| 134 | return (string) $mac; |
| 135 | } |
| 136 | } |
| 137 |