| 1 |
<?php |
| 2 |
/** |
| 3 |
* This file is part of the ramsey/uuid library |
| 4 |
* |
| 5 |
* For the full copyright and license information, please view the LICENSE |
| 6 |
* file that was distributed with this source code. |
| 7 |
* |
| 8 |
* @copyright Copyright (c) Ben Ramsey <[email protected]> |
| 9 |
* @license http://opensource.org/licenses/MIT MIT |
| 10 |
* @link https://benramsey.com/projects/ramsey-uuid/ Documentation |
| 11 |
* @link https://packagist.org/packages/ramsey/uuid Packagist |
| 12 |
* @link https://github.com/ramsey/uuid GitHub |
| 13 |
*/ |
| 14 |
|
| 15 |
namespace Ramsey\Uuid\Provider\Node; |
| 16 |
|
| 17 |
use Ramsey\Uuid\Provider\NodeProviderInterface; |
| 18 |
|
| 19 |
/** |
| 20 |
* SystemNodeProvider provides functionality to get the system node ID (MAC |
| 21 |
* address) using external system calls |
| 22 |
*/ |
| 23 |
class SystemNodeProvider implements NodeProviderInterface |
| 24 |
{ |
| 25 |
/** |
| 26 |
* Returns the system node ID |
| 27 |
* |
| 28 |
* @return string|false System node ID as a hexadecimal string, or false if it is not found |
| 29 |
*/ |
| 30 |
public function getNode() |
| 31 |
{ |
| 32 |
static $node = null; |
| 33 |
|
| 34 |
if ($node !== null) { |
| 35 |
return $node; |
| 36 |
} |
| 37 |
|
| 38 |
$pattern = '/[^:]([0-9A-Fa-f]{2}([:-])[0-9A-Fa-f]{2}(\2[0-9A-Fa-f]{2}){4})[^:]/'; |
| 39 |
$matches = []; |
| 40 |
|
| 41 |
// first try a linux specific way |
| 42 |
$node = $this->getSysfs(); |
| 43 |
|
| 44 |
// Search the ifconfig output for all MAC addresses and return |
| 45 |
// the first one found |
| 46 |
if ($node === false) { |
| 47 |
if (preg_match_all($pattern, $this->getIfconfig(), $matches, PREG_PATTERN_ORDER)) { |
| 48 |
$node = $matches[1][0]; |
| 49 |
} |
| 50 |
} |
| 51 |
if ($node !== false) { |
| 52 |
$node = str_replace([':', '-'], '', $node); |
| 53 |
} |
| 54 |
return $node; |
| 55 |
} |
| 56 |
|
| 57 |
/** |
| 58 |
* Returns the network interface configuration for the system |
| 59 |
* |
| 60 |
* @codeCoverageIgnore |
| 61 |
* @return string |
| 62 |
*/ |
| 63 |
protected function getIfconfig() |
| 64 |
{ |
| 65 |
if (strpos(strtolower(ini_get('disable_functions')), 'passthru') !== false) { |
| 66 |
return ''; |
| 67 |
} |
| 68 |
|
| 69 |
ob_start(); |
| 70 |
switch (strtoupper(substr(constant('PHP_OS'), 0, 3))) { |
| 71 |
case 'WIN': |
| 72 |
passthru('ipconfig /all 2>&1'); |
| 73 |
break; |
| 74 |
case 'DAR': |
| 75 |
passthru('ifconfig 2>&1'); |
| 76 |
break; |
| 77 |
case 'FRE': |
| 78 |
passthru('netstat -i -f link 2>&1'); |
| 79 |
break; |
| 80 |
case 'LIN': |
| 81 |
default: |
| 82 |
passthru('netstat -ie 2>&1'); |
| 83 |
break; |
| 84 |
} |
| 85 |
|
| 86 |
return ob_get_clean(); |
| 87 |
} |
| 88 |
|
| 89 |
/** |
| 90 |
* Returns mac address from the first system interface via the sysfs interface |
| 91 |
* |
| 92 |
* @return string|bool |
| 93 |
*/ |
| 94 |
protected function getSysfs() |
| 95 |
{ |
| 96 |
$mac = false; |
| 97 |
|
| 98 |
if (strtoupper(constant('PHP_OS')) === 'LINUX') { |
| 99 |
$addressPaths = glob('/sys/class/net/*/address', GLOB_NOSORT); |
| 100 |
|
| 101 |
if (empty($addressPaths)) { |
| 102 |
return false; |
| 103 |
} |
| 104 |
|
| 105 |
$macs = []; |
| 106 |
array_walk($addressPaths, function ($addressPath) use (&$macs) { |
| 107 |
if (is_readable($addressPath)) { |
| 108 |
$macs[] = file_get_contents($addressPath); |
| 109 |
} |
| 110 |
}); |
| 111 |
|
| 112 |
$macs = array_map('trim', $macs); |
| 113 |
|
| 114 |
// remove invalid entries |
| 115 |
$macs = array_filter($macs, function ($mac) { |
| 116 |
return |
| 117 |
// localhost adapter |
| 118 |
$mac !== '00:00:00:00:00:00' && |
| 119 |
// must match mac adress |
| 120 |
preg_match('/^([0-9a-f]{2}:){5}[0-9a-f]{2}$/i', $mac); |
| 121 |
}); |
| 122 |
|
| 123 |
$mac = reset($macs); |
| 124 |
} |
| 125 |
|
| 126 |
return $mac; |
| 127 |
} |
| 128 |
} |