Subversion Repositories uuid_mac_utils

Rev

Rev 10 | Rev 24 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
2 daniel-mar 1
<?php
2
 
3
/*
4
 * UUID utils for PHP
15 daniel-mar 5
 * Copyright 2011 - 2023 Daniel Marschall, ViaThinkSoft
6
 * Version 2023-04-29
2 daniel-mar 7
 *
8
 * Licensed under the Apache License, Version 2.0 (the "License");
9
 * you may not use this file except in compliance with the License.
10
 * You may obtain a copy of the License at
11
 *
12
 *     http://www.apache.org/licenses/LICENSE-2.0
13
 *
14
 * Unless required by applicable law or agreed to in writing, software
15
 * distributed under the License is distributed on an "AS IS" BASIS,
16
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17
 * See the License for the specific language governing permissions and
18
 * limitations under the License.
19
 */
20
 
21
# This library requires either the GMP extension (or BCMath if gmp_supplement.inc.php is present)
22
 
23
if (file_exists(__DIR__ . '/mac_utils.inc.phps')) include_once __DIR__ . '/mac_utils.inc.phps'; // optionally used for uuid_info()
24
if (file_exists(__DIR__ . '/gmp_supplement.inc.php')) include_once __DIR__ . '/gmp_supplement.inc.php';
25
 
26
define('UUID_NAMEBASED_NS_DNS',     '6ba7b810-9dad-11d1-80b4-00c04fd430c8');
27
define('UUID_NAMEBASED_NS_URL',     '6ba7b811-9dad-11d1-80b4-00c04fd430c8');
28
define('UUID_NAMEBASED_NS_OID',     '6ba7b812-9dad-11d1-80b4-00c04fd430c8');
29
define('UUID_NAMEBASED_NS_X500_DN', '6ba7b814-9dad-11d1-80b4-00c04fd430c8');
30
 
31
function uuid_valid($uuid) {
32
        $uuid = str_replace(array('-', '{', '}'), '', $uuid);
33
        $uuid = strtoupper($uuid);
34
        #$uuid = trim($uuid);
35
 
36
        if (strlen($uuid) != 32) return false;
37
 
38
        $uuid = preg_replace('@[0-9A-F]@', '', $uuid);
39
 
40
        return ($uuid == '');
41
}
42
 
43
# TODO: Don't echo
44
function uuid_info($uuid) {
45
        if (!uuid_valid($uuid)) return false;
46
 
47
        #$uuid = trim($uuid);
48
        # $uuid = str_replace(array('-', '{', '}'), '', $uuid);
49
        $uuid = strtoupper($uuid);
50
        $uuid = preg_replace('@[^0-9A-F]@', '', $uuid);
51
 
52
        $x = hexdec(substr($uuid, 16, 1));
53
             if ($x >= 14 /* 1110 */) $variant = 3;
54
        else if ($x >= 12 /* 1100 */) $variant = 2;
55
        else if ($x >=  8 /* 1000 */) $variant = 1;
56
        else if ($x >=  0 /* 0000 */) $variant = 0;
57
        else $variant = -1; // should not happen
58
 
59
        switch ($variant) {
60
                case 0:
61
                        echo sprintf("%-24s %s\n", "Variant:", "[0xx] NCS (reserved for backward compatibility)");
62
 
63
                        /*
64
                         * Internal structure of variant #0 UUIDs
65
                         *
66
                         * The first 6 octets are the number of 4 usec units of time that have
67
                         * passed since 1/1/80 0000 GMT.  The next 2 octets are reserved for
68
                         * future use.  The next octet is an address family.  The next 7 octets
69
                         * are a host ID in the form allowed by the specified address family.
70
                         *
71
                         * Note that while the family field (octet 8) was originally conceived
72
                         * of as being able to hold values in the range [0..255], only [0..13]
73
                         * were ever used.  Thus, the 2 MSB of this field are always 0 and are
74
                         * used to distinguish old and current UUID forms.
75
                         *
76
                         * +--------------------------------------------------------------+
77
                         * |                    high 32 bits of time                      |  0-3  .time_high
78
                         * +-------------------------------+-------------------------------
79
                         * |     low 16 bits of time       |  4-5               .time_low
80
                         * +-------+-----------------------+
81
                         * |         reserved              |  6-7               .reserved
82
                         * +---------------+---------------+
83
                         * |    family     |   8                                .family
84
                         * +---------------+----------...-----+
85
                         * |            node ID               |  9-16           .node
86
                         * +--------------------------...-----+
87
                         *
88
                         */
89
 
90
                        // Example of an UUID: 333a2276-0000-0000-0d00-00809c000000
91
 
92
                        # TODO: See https://github.com/cjsv/uuid/blob/master/Doc
93
 
94
                        # Timestamp: Count of 4us intervals since 01 Jan 1980 00:00:00 GMT
95
                        # 1/0,000004 = 250000
96
                        # Seconds between 1970 and 1980 : 315532800
97
                        # 250000*315532800=78883200000000
98
                        $timestamp = substr($uuid, 0, 12);
99
                        $ts = gmp_init($timestamp, 16);
100
                        $ts = gmp_add($ts, gmp_init("78883200000000"));
101
                        $ms = gmp_mod($ts, gmp_init("250000"));
102
                        $ts = gmp_div($ts, gmp_init("250000"));
103
                        $ts = gmp_strval($ts);
104
                        $ms = gmp_strval($ms);
105
                        $ts = gmdate('Y-m-d H:i:s', intval($ts))."'".str_pad($ms, 7, '0', STR_PAD_LEFT).' GMT';
106
                        echo sprintf("%-24s %s\n", "Timestamp:", "[0x$timestamp] $ts");
107
 
108
                        $reserved = substr($uuid, 12, 4);
109
                        echo sprintf("%-24s %s\n", "Reserved:", "0x$reserved");
110
 
111
                        # Family 13 (dds) looks like node is 00 | nnnnnn 000000.
112
                        # Family 2 is presumably (ip).
113
                        # Not sure if anything else was used.
114
                        $family_hex = substr($uuid, 16, 2);
115
                        $family_dec = hexdec($family_hex);
116
                        if ($family_dec == 2) {
117
                                $family_ = 'IP';
118
                        } else if ($family_dec == 13) {
119
                                $family_ = 'DDS (Data Link)';
120
                        } else {
121
                                $family_ = "Unknown ($family_dec)"; # There are probably no more families
122
                        }
123
                        echo sprintf("%-24s %s\n", "Family:", "[0x$family_hex = $family_dec] $family_");
124
 
125
                        $nodeid = substr($uuid, 18, 14);
126
                        echo sprintf("%-24s %s\n", "Node ID:", "0x$nodeid");
127
                        # TODO: interprete node id (the family specifies it)
128
 
129
                        break;
130
                case 1:
131
                        echo sprintf("%-24s %s\n", "Variant:", "[10x] RFC 4122 (Leach-Mealling-Salz)");
132
 
133
                        $version = hexdec(substr($uuid, 12, 1));
134
                        switch ($version) {
135
                                case 1:
136
                                        echo sprintf("%-24s %s\n", "Version:", "[1] Time-based with unique random host identifier");
137
 
138
                                        # Timestamp: Count of 100ns intervals since 15 Oct 1582 00:00:00
139
                                        # 1/0,0000001 = 10000000
140
                                        $timestamp = substr($uuid, 13, 3).substr($uuid, 8, 4).substr($uuid, 0, 8);
141
                                        $ts = gmp_init($timestamp, 16);
142
                                        $ts = gmp_sub($ts, gmp_init("122192928000000000"));
143
                                        $ms = gmp_mod($ts, gmp_init("10000000"));
144
                                        $ts = gmp_div($ts, gmp_init("10000000"));
145
                                        $ts = gmp_strval($ts);
146
                                        $ms = gmp_strval($ms);
147
                                        $ts = gmdate('Y-m-d H:i:s', intval($ts))."'".str_pad($ms, 7, '0', STR_PAD_LEFT).' GMT';
148
                                        echo sprintf("%-24s %s\n", "Timestamp:", "[0x$timestamp] $ts");
149
 
150
                                        $x = hexdec(substr($uuid, 16, 4));
151
                                        $dec = $x & 0x3FFF; // The highest 2 bits are used by "variant" (10x)
152
                                        $hex = substr($uuid, 16, 4);
153
                                        echo sprintf("%-24s %s\n", "Clock ID:", "[0x$hex] $dec");
154
 
155
                                        $x = substr($uuid, 20, 12);
156
                                        $nodeid = '';
157
                                        for ($i=0; $i<6; $i++) {
158
                                                $nodeid .= substr($x, $i*2, 2);
159
                                                if ($i != 5) $nodeid .= ':';
160
                                        }
161
                                        echo sprintf("%-24s %s\n", "Node ID:", "$nodeid");
162
 
163
                                        if (function_exists('decode_mac')) {
164
                                                echo "\nIn case that this Node ID is a MAC address, here is the interpretation of that MAC address:\n";
15 daniel-mar 165
                                                decode_mac($nodeid);
2 daniel-mar 166
                                        }
167
 
168
                                        break;
169
                                case 2:
170
                                        echo sprintf("%-24s %s\n", "Version:", "[2] DCE Security version");
171
 
172
                                        # The time_low field (which represents an integer in the range [0, 232-1]) is interpreted as a local-ID; that is, an identifier (within the domain specified by clock_seq_low) meaningful to the local host. In the particular case of a POSIX host, when combined with a POSIX UID or POSIX GID domain in the clock_seq_low field (above), the time_low field represents a POSIX UID or POSIX GID, respectively.
173
                                        $x = substr($uuid, 0, 8);
174
                                        echo sprintf("%-24s %s\n", "Local ID:", "0x$x");
175
 
176
                                        # The clock_seq_low field (which represents an integer in the range [0, 28-1]) is interpreted as a local domain (as represented by sec_rgy_domain_t; see sec_rgy_domain_t ); that is, an identifier domain meaningful to the local host. (Note that the data type sec_rgy_domain_t can potentially hold values outside the range [0, 28-1]; however, the only values currently registered are in the range [0, 2], so this type mismatch is not significant.) In the particular case of a POSIX host, the value sec_rgy_domain_person is to be interpreted as the "POSIX UID domain", and the value sec_rgy_domain_group is to be interpreted as the "POSIX GID domain".
177
                                        $x = substr($uuid, 18, 2);
178
                                        if ($x == '00') $domain_info = 'POSIX: User-ID / Non-POSIX: site-defined';
179
                                        else if ($x == '01') $domain_info = 'POSIX: Group-ID / Non-POSIX: site-defined';
180
                                        else $domain_info = 'site-defined';
181
                                        echo sprintf("%-24s %s\n", "Local Domain:", "0x$x ($domain_info)");
182
 
183
                                        # Timestamp: Count of 100ns intervals since 15 Oct 1582 00:00:00
184
                                        # 1/0,0000001 = 10000000
185
                                        $timestamp = substr($uuid, 13, 3).substr($uuid, 8, 4).'00000000';
186
                                        $ts = gmp_init($timestamp, 16);
187
                                        $ts = gmp_sub($ts, gmp_init("122192928000000000"));
188
                                        $ms = gmp_mod($ts, gmp_init("10000000"));
189
                                        $ts = gmp_div($ts, gmp_init("10000000"));
190
                                        $ts = gmp_strval($ts);
191
                                        $ms = gmp_strval($ms);
192
                                        $ts_min = gmdate('Y-m-d H:i:s', intval($ts))."'".str_pad($ms, 7, '0', STR_PAD_LEFT).' GMT';
193
 
194
                                        $timestamp = substr($uuid, 13, 3).substr($uuid, 8, 4).'FFFFFFFF';
195
                                        $ts = gmp_init($timestamp, 16);
196
                                        $ts = gmp_sub($ts, gmp_init("122192928000000000"));
197
                                        $ms = gmp_mod($ts, gmp_init("10000000"));
198
                                        $ts = gmp_div($ts, gmp_init("10000000"));
199
                                        $ts = gmp_strval($ts);
200
                                        $ms = gmp_strval($ms);
201
                                        $ts_max = gmdate('Y-m-d H:i:s', intval($ts))."'".str_pad($ms, 7, '0', STR_PAD_LEFT).' GMT';
202
 
203
                                        $timestamp = substr($uuid, 13, 3).substr($uuid, 8, 4).'xxxxxxxx';
204
                                        echo sprintf("%-24s %s\n", "Timestamp:", "[0x$timestamp] $ts_min - $ts_max");
205
 
206
                                        $x = hexdec(substr($uuid, 16, 2).'00');
207
                                        $dec_min = $x & 0x3FFF; // The highest 2 bits are used by "variant" (10x)
208
                                        $x = hexdec(substr($uuid, 16, 2).'FF');
209
                                        $dec_max = $x & 0x3FFF; // The highest 2 bits are used by "variant" (10x)
210
                                        $hex = substr($uuid, 16, 2).'xx';
211
                                        echo sprintf("%-24s %s\n", "Clock ID:", "[0x$hex] $dec_min - $dec_max");
212
 
213
                                        $x = substr($uuid, 20, 12);
214
                                        $nodeid = '';
215
                                        for ($i=0; $i<6; $i++) {
216
                                                $nodeid .= substr($x, $i*2, 2);
217
                                                if ($i != 5) $nodeid .= ':';
218
                                        }
219
                                        echo sprintf("%-24s %s\n", "Node ID:", "$nodeid");
220
 
221
                                        if (function_exists('decode_mac')) {
222
                                                echo "\nIn case that this Node ID is a MAC address, here is the interpretation of that MAC address:\n";
15 daniel-mar 223
                                                decode_mac($nodeid);
2 daniel-mar 224
                                        }
225
 
226
                                        break;
227
                                case 3:
228
                                        echo sprintf("%-24s %s\n", "Version:", "[3] Name-based (MD5 hash)");
229
 
230
                                        $hash = str_replace('-', '', strtolower($uuid));
231
                                        $hash[12] = '?'; // was overwritten by version
232
                                        $hash[16] = '?'; // was partially overwritten by variant
233
 
234
                                        echo sprintf("%-24s %s\n", "MD5(Namespace+Subject):", "$hash");
235
 
236
                                        break;
237
                                case 4:
238
                                        echo sprintf("%-24s %s\n", "Version:", "[4] Random");
239
 
240
                                        $rand = '';
241
                                        for ($i=0; $i<16; $i++) {
242
                                                $bin = base_convert(substr($uuid, $i*2, 2), 16, 2);
243
                                                $bin = str_pad($bin, 8, "0", STR_PAD_LEFT);
244
 
245
                                                if ($i == 6) {
246
                                                        $bin[0] = 'x';
247
                                                        $bin[1] = 'x';
248
                                                } else if ($i == 8) {
249
                                                        $bin[0] = 'x';
250
                                                        $bin[1] = 'x';
251
                                                        $bin[2] = 'x';
252
                                                        $bin[3] = 'x';
253
                                                }
254
 
255
                                                $rand .= "$bin ";
256
                                        }
257
 
258
                                        echo sprintf("%-24s %s\n", "Random bits:", trim($rand));
259
 
260
                                        break;
261
                                case 5:
262
                                        echo sprintf("%-24s %s\n", "Version:", "[5] Name-based (SHA-1 hash)");
263
 
264
                                        $hash = str_replace('-', '', strtolower($uuid));
265
                                        $hash[12] = '?'; // was overwritten by version
266
                                        $hash[16] = '?'; // was partially overwritten by variant
267
                                        $hash .= '????????'; // was cut off
268
 
269
                                        echo sprintf("%-24s %s\n", "SHA1(Namespace+Subject):", "$hash");
270
 
271
 
272
                                        break;
273
                                default:
274
                                        echo sprintf("%-24s %s\n", "Version:", "[$version] Unknown");
275
                                        break;
276
                        }
277
 
278
                        break;
279
                case 2:
280
                        echo sprintf("%-24s %s\n", "Variant:", "[110] Reserved for Microsoft Corporation");
281
                        break;
282
                case 3:
283
                        echo sprintf("%-24s %s\n", "Variant:", "[111] Reserved for future use");
284
                        break;
285
        }
286
}
287
 
288
function uuid_canonize($uuid) {
289
        if (!uuid_valid($uuid)) return false;
290
        return oid_to_uuid(uuid_to_oid($uuid));
291
}
292
 
293
function oid_to_uuid($oid) {
294
        if (!is_uuid_oid($oid)) return false;
295
 
8 daniel-mar 296
        if (substr($oid,0,1) == '.') {
2 daniel-mar 297
                $oid = substr($oid, 1);
298
        }
299
        $ary = explode('.', $oid);
300
 
301
        if (!isset($ary[2])) return false;
302
 
303
        $val = $ary[2];
304
 
305
        $x = gmp_init($val, 10);
306
        $y = gmp_strval($x, 16);
307
        $y = str_pad($y, 32, "0", STR_PAD_LEFT);
308
        return substr($y,  0, 8).'-'.
309
               substr($y,  8, 4).'-'.
310
               substr($y, 12, 4).'-'.
311
               substr($y, 16, 4).'-'.
312
               substr($y, 20, 12);
313
}
314
 
315
function is_uuid_oid($oid, $only_allow_root=false) {
9 daniel-mar 316
        if (substr($oid,0,1) == '.') $oid = substr($oid, 1); // remove leading dot
2 daniel-mar 317
 
318
        $ary = explode('.', $oid);
319
 
320
        if ($only_allow_root) {
321
                if (count($ary) != 3) return false;
322
        } else {
323
                if (count($ary) < 3) return false;
324
        }
325
 
326
        if ($ary[0] != '2') return false;
327
        if ($ary[1] != '25') return false;
328
        for ($i=2; $i<count($ary); $i++) {
329
                $v = $ary[$i];
330
                if (!is_numeric($v)) return false;
331
                if ($i == 2) {
332
                        // Must be in the range of 128 bit UUID
333
                        $test = gmp_init($v, 10);
334
                        if (strlen(gmp_strval($test, 16)) > 32) return false;
335
                }
336
                if ($v < 0) return false;
337
        }
338
 
339
        return true;
340
}
341
 
342
function uuid_to_oid($uuid) {
343
        if (!uuid_valid($uuid)) return false;
344
 
345
        $uuid = str_replace(array('-', '{', '}'), '', $uuid);
346
        $x = gmp_init($uuid, 16);
347
        return '2.25.'.gmp_strval($x, 10); # TODO: parameter with or without leading dot
348
}
349
 
350
function gen_uuid($prefer_timebased = true) {
351
        $uuid = $prefer_timebased ? gen_uuid_timebased() : false;
352
        if ($uuid === false) $uuid = gen_uuid_random();
353
        return $uuid;
354
}
355
 
356
// Version 1 (Time based) UUID
357
function gen_uuid_timebased() {
358
        # On Debian: apt-get install php-uuid
359
        # extension_loaded('uuid')
360
        if (function_exists('uuid_create')) {
361
                # OSSP uuid extension like seen in php5-uuid at Debian 8
362
                /*
363
                $x = uuid_create($context);
364
                uuid_make($context, UUID_MAKE_V1);
365
                uuid_export($context, UUID_FMT_STR, $uuid);
366
                return trim($uuid);
367
                */
368
 
369
                # PECL uuid extension like seen in php-uuid at Debian 9
370
                return trim(uuid_create(UUID_TYPE_TIME));
371
        }
372
 
373
        # On Debian: apt-get install uuid-runtime
374
        if (strtoupper(substr(PHP_OS, 0, 3)) !== 'WIN') {
375
                $out = array();
376
                $ec = -1;
377
                exec('uuidgen -t 2>/dev/null', $out, $ec);
378
                if ($ec == 0) return trim($out[0]);
379
        }
380
 
381
        # If we hadn't any success yet, then implement the time based generation routine ourselves!
382
        # Based on https://github.com/fredriklindberg/class.uuid.php/blob/master/class.uuid.php
383
 
384
        $uuid = array(
385
                'time_low' => 0,                /* 32-bit */
386
                'time_mid' => 0,                /* 16-bit */
387
                'time_hi' => 0,                 /* 16-bit */
388
                'clock_seq_hi' => 0,            /*  8-bit */
389
                'clock_seq_low' => 0,           /*  8-bit */
390
                'node' => array()               /* 48-bit */
391
        );
392
 
393
        /*
394
         * Get current time in 100 ns intervals. The magic value
395
         * is the offset between UNIX epoch and the UUID UTC
396
         * time base October 15, 1582.
397
         */
398
        $tp = gettimeofday();
399
        $time = ($tp['sec'] * 10000000) + ($tp['usec'] * 10) + 0x01B21DD213814000;
400
 
401
        $uuid['time_low'] = $time & 0xffffffff;
402
        /* Work around PHP 32-bit bit-operation limits */
403
        $high = intval($time / 0xffffffff);
404
        $uuid['time_mid'] = $high & 0xffff;
405
        $uuid['time_hi'] = (($high >> 16) & 0xfff) | (1/*TimeBased*/ << 12);
406
 
407
        /*
408
         * We don't support saved state information and generate
409
         * a random clock sequence each time.
410
         */
411
        $uuid['clock_seq_hi'] = 0x80 | mt_rand(0, 64);
412
        $uuid['clock_seq_low'] = mt_rand(0, 255);
413
 
414
        /*
415
         * Node should be set to the 48-bit IEEE node identifier
416
         */
417
        $mac = get_mac_address();
418
        if ($mac) {
419
                $node = str_replace(':','',$mac);
420
                for ($i = 0; $i < 6; $i++) {
421
                        $uuid['node'][$i] = hexdec(substr($node, $i*2, 2));
422
                }
423
 
424
                /*
425
                 * Now output the UUID
426
                 */
427
                return sprintf(
428
                        '%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x',
429
                        ($uuid['time_low']), ($uuid['time_mid']), ($uuid['time_hi']),
430
                        $uuid['clock_seq_hi'], $uuid['clock_seq_low'],
431
                        $uuid['node'][0], $uuid['node'][1], $uuid['node'][2],
432
                        $uuid['node'][3], $uuid['node'][4], $uuid['node'][5]);
433
        }
434
 
435
        # We cannot generate the timebased UUID!
436
        return false;
437
}
438
 
439
function get_mac_address() {
440
        static $detected_mac = false;
441
 
442
        if ($detected_mac !== false) { // false NOT null!
443
                return $detected_mac;
444
        }
445
 
446
        // TODO: This should actually be part of mac_utils.inc.php, but we need it
447
        //       here, and mac_utils.inc.php shall only be optional. What to do?
448
        if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {
449
                // Windows
450
                $cmds = array(
451
                        "ipconfig /all", // faster
452
                        "getmac"
453
                );
454
                foreach ($cmds as $cmd) {
455
                        $out = array();
456
                        $ec = -1;
457
                        exec($cmd, $out, $ec);
458
                        if ($ec == 0) {
459
                                $out = implode("\n",$out);
460
                                $m = array();
461
                                if (preg_match("/([0-9a-f]{2}\\-[0-9a-f]{2}\\-[0-9a-f]{2}\\-[0-9a-f]{2}\\-[0-9a-f]{2}\\-[0-9a-f]{2})/ismU", $out, $m)) {
462
                                        $detected_mac = str_replace('-', ':', strtolower($m[1]));
463
                                        return $detected_mac;
464
                                }
465
                        }
466
                }
467
        } else if (strtoupper(PHP_OS) == 'DARWIN') {
468
                // Mac OS X
469
                $cmds = array(
470
                        "networksetup -listallhardwareports 2>/dev/null",
471
                        "netstat -i 2>/dev/null"
472
                );
473
                foreach ($cmds as $cmd) {
474
                        $out = array();
475
                        $ec = -1;
476
                        exec($cmd, $out, $ec);
477
                        if ($ec == 0) {
478
                                $out = implode("\n",$out);
479
                                $m = array();
480
                                if (preg_match("/([0-9a-f]{2}:[0-9a-f]{2}:[0-9a-f]{2}:[0-9a-f]{2}:[0-9a-f]{2}:[0-9a-f]{2})/ismU", $out, $m)) {
481
                                        $detected_mac = $m[1];
482
                                        return $detected_mac;
483
                                }
484
                        }
485
                }
486
        } else {
487
                // Linux
8 daniel-mar 488
                $addresses = @glob('/sys/class/net/'.'*'.'/address');
489
                foreach ($addresses as $x) {
2 daniel-mar 490
                        if (!strstr($x,'/lo/')) {
491
                                $detected_mac = trim(file_get_contents($x));
492
                                return $detected_mac;
493
                        }
494
                }
495
                $cmds = array(
496
                        "netstat -ie 2>/dev/null",
497
                        "ifconfig 2>/dev/null" // only available for root (because it is in sbin)
498
                );
499
                foreach ($cmds as $cmd) {
500
                        $out = array();
501
                        $ec = -1;
502
                        exec($cmd, $out, $ec);
503
                        if ($ec == 0) {
504
                                $out = implode("\n",$out);
505
                                $m = array();
506
                                if (preg_match("/([0-9a-f]{2}:[0-9a-f]{2}:[0-9a-f]{2}:[0-9a-f]{2}:[0-9a-f]{2}:[0-9a-f]{2})/ismU", $out, $m)) {
507
                                        $detected_mac = $m[1];
508
                                        return $detected_mac;
509
                                }
510
                        }
511
                }
512
        }
513
 
514
        $detected_mac = null;
515
        return $detected_mac;
516
}
517
 
518
// Version 2 (DCE Security) UUID
519
function gen_uuid_dce($domain, $id) {
520
        # Start with a version 1 UUID
521
        $uuid = gen_uuid_timebased();
522
 
523
        # Add ID
524
        $uuid = str_pad(dechex($id), 8, '0', STR_PAD_LEFT) . substr($uuid, 8);
525
 
526
        # Add domain
527
        $uuid = substr($uuid,0,21) . str_pad(dechex($domain), 2, '0', STR_PAD_LEFT) . substr($uuid, 23);
528
 
529
        # Change version to 2
530
        $uuid[14] = '2';
531
 
532
        return $uuid;
533
}
534
 
535
// Version 3 (MD5 name based) UUID
536
function gen_uuid_md5_namebased($namespace_uuid, $name) {
537
        if (!uuid_valid($namespace_uuid)) return false;
538
        $namespace_uuid = uuid_canonize($namespace_uuid);
539
        $namespace_uuid = str_replace('-', '', $namespace_uuid);
540
        $namespace_uuid = hex2bin($namespace_uuid);
541
 
542
        $hash = md5($namespace_uuid.$name);
543
        $hash[12] = '3'; // Set version: 3 = MD5
544
        $hash[16] = dechex(hexdec($hash[16]) & 0x3 | 0x8); // Set variant to "10xx" (RFC4122)
545
 
546
        return substr($hash,  0, 8).'-'.
547
               substr($hash,  8, 4).'-'.
548
               substr($hash, 12, 4).'-'.
549
               substr($hash, 16, 4).'-'.
550
               substr($hash, 20, 12);
551
}
552
 
553
// Version 4 (Random) UUID
554
function gen_uuid_random() {
555
        # On Windows: Requires
556
        #    extension_dir = "C:\php-8.0.3-nts-Win32-vs16-x64\ext"
557
        #    extension=com_dotnet
558
        if (function_exists('com_create_guid')) {
559
                return strtolower(trim(com_create_guid(), '{}'));
560
        }
561
 
562
        # On Debian: apt-get install php-uuid
563
        # extension_loaded('uuid')
564
        if (function_exists('uuid_create')) {
565
                # OSSP uuid extension like seen in php5-uuid at Debian 8
566
                /*
567
                $x = uuid_create($context);
568
                uuid_make($context, UUID_MAKE_V4);
569
                uuid_export($context, UUID_FMT_STR, $uuid);
570
                return trim($uuid);
571
                */
572
 
573
                # PECL uuid extension like seen in php-uuid at Debian 9
574
                return trim(uuid_create(UUID_TYPE_RANDOM));
575
        }
576
 
577
        if (strtoupper(substr(PHP_OS, 0, 3)) !== 'WIN') {
578
                # On Debian: apt-get install uuid-runtime
579
                $out = array();
580
                $ec = -1;
581
                exec('uuidgen -r 2>/dev/null', $out, $ec);
582
                if ($ec == 0) return trim($out[0]);
583
 
584
                # On Debian Jessie: UUID V4 (Random)
585
                if (file_exists('/proc/sys/kernel/random/uuid')) {
586
                        return trim(file_get_contents('/proc/sys/kernel/random/uuid'));
587
                }
588
        }
589
 
590
        # Make the UUID by ourselves
591
        # Source: http://rogerstringer.com/2013/11/15/generate-uuids-php
592
        return sprintf( '%04x%04x-%04x-%04x-%04x-%04x%04x%04x',
593
                mt_rand( 0, 0xffff ), mt_rand( 0, 0xffff ),
594
                mt_rand( 0, 0xffff ),
595
                mt_rand( 0, 0x0fff ) | 0x4000,
596
                mt_rand( 0, 0x3fff ) | 0x8000,
597
                mt_rand( 0, 0xffff ), mt_rand( 0, 0xffff ), mt_rand( 0, 0xffff )
598
        );
599
}
600
 
601
// Version 5 (SHA1 name based) UUID
602
function gen_uuid_sha1_namebased($namespace_uuid, $name) {
603
        $namespace_uuid = str_replace('-', '', $namespace_uuid);
604
        $namespace_uuid = hex2bin($namespace_uuid);
605
 
606
        $hash = sha1($namespace_uuid.$name);
607
        $hash[12] = '5'; // Set version: 5 = SHA1
608
        $hash[16] = dechex(hexdec($hash[16]) & 0x3 | 0x8); // Set variant to "10xx" (RFC4122)
609
 
610
        return substr($hash,  0, 8).'-'.
611
               substr($hash,  8, 4).'-'.
612
               substr($hash, 12, 4).'-'.
613
               substr($hash, 16, 4).'-'.
614
               substr($hash, 20, 12);
615
}
616
 
617
function uuid_numeric_value($uuid) {
618
        $oid = uuid_to_oid($uuid);
619
        if (!$oid) return false;
620
        return substr($oid, strlen('2.25.'));
621
}
622
 
623
function uuid_c_syntax($uuid) {
624
        $uuid = str_replace('{', '', $uuid);
625
        return '{ 0x' . substr($uuid, 0, 8) .
626
                ', 0x' . substr($uuid, 9, 4) .
627
                ', 0x' . substr($uuid, 14, 4) .
628
                ', { 0x' . substr($uuid, 19, 2).
629
                ', 0x' . substr($uuid, 21, 2) .
630
                ', 0x' . substr($uuid, 24, 2) .
631
                ', 0x' . substr($uuid, 26, 2) .
632
                ', 0x' . substr($uuid, 28, 2) .
633
                ', 0x' . substr($uuid, 30, 2) .
634
                ', 0x' . substr($uuid, 32, 2) .
635
                ', 0x' . substr($uuid, 34, 2) . ' } }';
636
}
637
 
638
# ---
639
 
640
// http://php.net/manual/de/function.hex2bin.php#113057
641
if ( !function_exists( 'hex2bin' ) ) {
642
    function hex2bin( $str ) {
643
        $sbin = "";
644
        $len = strlen( $str );
645
        for ( $i = 0; $i < $len; $i += 2 ) {
646
            $sbin .= pack( "H*", substr( $str, $i, 2 ) );
647
        }
648
 
649
        return $sbin;
650
    }
651
}