Subversion Repositories oidinfo_api

Rev

Rev 14 | Rev 27 | Go to most recent revision | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 14 Rev 16
Line 1... Line 1...
1
<?php
1
<?php
2
 
2
 
3
/*
3
/*
4
 * UUID utils for PHP
4
 * UUID utils for PHP
5
 * Copyright 2011 - 2020 Daniel Marschall, ViaThinkSoft
5
 * Copyright 2011 - 2021 Daniel Marschall, ViaThinkSoft
6
 * Version 2020-11-14
6
 * Version 2021-05-21
7
 *
7
 *
8
 * Licensed under the Apache License, Version 2.0 (the "License");
8
 * Licensed under the Apache License, Version 2.0 (the "License");
9
 * you may not use this file except in compliance with 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
10
 * You may obtain a copy of the License at
11
 *
11
 *
Line 29... Line 29...
29
define('UUID_NAMEBASED_NS_X500_DN', '6ba7b814-9dad-11d1-80b4-00c04fd430c8');
29
define('UUID_NAMEBASED_NS_X500_DN', '6ba7b814-9dad-11d1-80b4-00c04fd430c8');
30
 
30
 
31
function uuid_valid($uuid) {
31
function uuid_valid($uuid) {
32
        $uuid = str_replace(array('-', '{', '}'), '', $uuid);
32
        $uuid = str_replace(array('-', '{', '}'), '', $uuid);
33
        $uuid = strtoupper($uuid);
33
        $uuid = strtoupper($uuid);
-
 
34
        #$uuid = trim($uuid);
34
 
35
 
35
        if (strlen($uuid) != 32) return false;
36
        if (strlen($uuid) != 32) return false;
36
 
37
 
37
        $uuid = preg_replace('@[0-9A-F]@', '', $uuid);
38
        $uuid = preg_replace('@[0-9A-F]@', '', $uuid);
38
 
39
 
Line 41... Line 42...
41
 
42
 
42
# TODO: Don't echo
43
# TODO: Don't echo
43
function uuid_info($uuid) {
44
function uuid_info($uuid) {
44
        if (!uuid_valid($uuid)) return false;
45
        if (!uuid_valid($uuid)) return false;
45
 
46
 
-
 
47
        #$uuid = trim($uuid);
46
        # $uuid = str_replace(array('-', '{', '}'), '', $uuid);
48
        # $uuid = str_replace(array('-', '{', '}'), '', $uuid);
47
        $uuid = strtoupper($uuid);
49
        $uuid = strtoupper($uuid);
48
        $uuid = preg_replace('@[^0-9A-F]@', '', $uuid);
50
        $uuid = preg_replace('@[^0-9A-F]@', '', $uuid);
49
 
51
 
50
        $x = hexdec(substr($uuid, 16, 1));
52
        $x = hexdec(substr($uuid, 16, 1));
51
             if ($x >= 14 /* 1110 */) $variant = 3;
53
             if ($x >= 14 /* 1110 */) $variant = 3;
52
        else if ($x >= 12 /* 1100 */) $variant = 2;
54
        else if ($x >= 12 /* 1100 */) $variant = 2;
53
        else if ($x >=  8 /* 1000 */) $variant = 1;
55
        else if ($x >=  8 /* 1000 */) $variant = 1;
54
        else if ($x >=  0 /* 0000 */) $variant = 0;
56
        else if ($x >=  0 /* 0000 */) $variant = 0;
55
 
-
 
-
 
57
        else $variant = -1; // should not happen
56
 
58
 
57
        switch ($variant) {
59
        switch ($variant) {
58
                case 0:
60
                case 0:
59
                        echo sprintf("%-24s %s\n", "Variant:", "[0xx] NCS (reserved for backward compatibility)");
61
                        echo sprintf("%-24s %s\n", "Variant:", "[0xx] NCS (reserved for backward compatibility)");
60
 
62
 
Line 98... Line 100...
98
                        $ts = gmp_add($ts, gmp_init("78883200000000"));
100
                        $ts = gmp_add($ts, gmp_init("78883200000000"));
99
                        $ms = gmp_mod($ts, gmp_init("250000"));
101
                        $ms = gmp_mod($ts, gmp_init("250000"));
100
                        $ts = gmp_div($ts, gmp_init("250000"));
102
                        $ts = gmp_div($ts, gmp_init("250000"));
101
                        $ts = gmp_strval($ts);
103
                        $ts = gmp_strval($ts);
102
                        $ms = gmp_strval($ms);
104
                        $ms = gmp_strval($ms);
103
                        $ts = gmdate('Y-m-d H:i:s', $ts)."'".str_pad($ms, 7, '0', STR_PAD_LEFT).' GMT';
105
                        $ts = gmdate('Y-m-d H:i:s', intval($ts))."'".str_pad($ms, 7, '0', STR_PAD_LEFT).' GMT';
104
                        echo sprintf("%-24s %s\n", "Timestamp:", "[0x$timestamp] $ts");
106
                        echo sprintf("%-24s %s\n", "Timestamp:", "[0x$timestamp] $ts");
105
 
107
 
106
                        $reserved = substr($uuid, 12, 4);
108
                        $reserved = substr($uuid, 12, 4);
107
                        echo sprintf("%-24s %s\n", "Reserved:", "0x$reserved");
109
                        echo sprintf("%-24s %s\n", "Reserved:", "0x$reserved");
108
 
110
 
Line 140... Line 142...
140
                                        $ts = gmp_sub($ts, gmp_init("122192928000000000"));
142
                                        $ts = gmp_sub($ts, gmp_init("122192928000000000"));
141
                                        $ms = gmp_mod($ts, gmp_init("10000000"));
143
                                        $ms = gmp_mod($ts, gmp_init("10000000"));
142
                                        $ts = gmp_div($ts, gmp_init("10000000"));
144
                                        $ts = gmp_div($ts, gmp_init("10000000"));
143
                                        $ts = gmp_strval($ts);
145
                                        $ts = gmp_strval($ts);
144
                                        $ms = gmp_strval($ms);
146
                                        $ms = gmp_strval($ms);
145
                                        $ts = gmdate('Y-m-d H:i:s', $ts)."'".str_pad($ms, 7, '0', STR_PAD_LEFT).' GMT';
147
                                        $ts = gmdate('Y-m-d H:i:s', intval($ts))."'".str_pad($ms, 7, '0', STR_PAD_LEFT).' GMT';
146
                                        echo sprintf("%-24s %s\n", "Timestamp:", "[0x$timestamp] $ts");
148
                                        echo sprintf("%-24s %s\n", "Timestamp:", "[0x$timestamp] $ts");
147
 
149
 
148
                                        $x = hexdec(substr($uuid, 16, 4));
150
                                        $x = hexdec(substr($uuid, 16, 4));
149
                                        $dec = $x & 0x3FFF; // The highest 2 bits are used by "variant" (10x)
151
                                        $dec = $x & 0x3FFF; // The highest 2 bits are used by "variant" (10x)
150
                                        $hex = substr($uuid, 16, 4);
152
                                        $hex = substr($uuid, 16, 4);
Line 158... Line 160...
158
                                        }
160
                                        }
159
                                        echo sprintf("%-24s %s\n", "Node ID:", "$nodeid");
161
                                        echo sprintf("%-24s %s\n", "Node ID:", "$nodeid");
160
 
162
 
161
                                        if (function_exists('decode_mac')) {
163
                                        if (function_exists('decode_mac')) {
162
                                                echo "\nIn case that this Node ID is a MAC address, here is the interpretation of that MAC address:\n";
164
                                                echo "\nIn case that this Node ID is a MAC address, here is the interpretation of that MAC address:\n";
163
                                                echo decode_mac($nodeid);
165
                                                echo decode_mac($nodeid); /** @phpstan-ignore-line */
164
                                        }
166
                                        }
165
 
167
 
166
                                        break;
168
                                        break;
167
                                case 2:
169
                                case 2:
168
                                        echo sprintf("%-24s %s\n", "Version:", "[2] DCE Security version");
170
                                        echo sprintf("%-24s %s\n", "Version:", "[2] DCE Security version");
Line 185... Line 187...
185
                                        $ts = gmp_sub($ts, gmp_init("122192928000000000"));
187
                                        $ts = gmp_sub($ts, gmp_init("122192928000000000"));
186
                                        $ms = gmp_mod($ts, gmp_init("10000000"));
188
                                        $ms = gmp_mod($ts, gmp_init("10000000"));
187
                                        $ts = gmp_div($ts, gmp_init("10000000"));
189
                                        $ts = gmp_div($ts, gmp_init("10000000"));
188
                                        $ts = gmp_strval($ts);
190
                                        $ts = gmp_strval($ts);
189
                                        $ms = gmp_strval($ms);
191
                                        $ms = gmp_strval($ms);
190
                                        $ts_min = gmdate('Y-m-d H:i:s', $ts)."'".str_pad($ms, 7, '0', STR_PAD_LEFT).' GMT';
192
                                        $ts_min = gmdate('Y-m-d H:i:s', intval($ts))."'".str_pad($ms, 7, '0', STR_PAD_LEFT).' GMT';
191
 
193
 
192
                                        $timestamp = substr($uuid, 13, 3).substr($uuid, 8, 4).'FFFFFFFF';
194
                                        $timestamp = substr($uuid, 13, 3).substr($uuid, 8, 4).'FFFFFFFF';
193
                                        $ts = gmp_init($timestamp, 16);
195
                                        $ts = gmp_init($timestamp, 16);
194
                                        $ts = gmp_sub($ts, gmp_init("122192928000000000"));
196
                                        $ts = gmp_sub($ts, gmp_init("122192928000000000"));
195
                                        $ms = gmp_mod($ts, gmp_init("10000000"));
197
                                        $ms = gmp_mod($ts, gmp_init("10000000"));
196
                                        $ts = gmp_div($ts, gmp_init("10000000"));
198
                                        $ts = gmp_div($ts, gmp_init("10000000"));
197
                                        $ts = gmp_strval($ts);
199
                                        $ts = gmp_strval($ts);
198
                                        $ms = gmp_strval($ms);
200
                                        $ms = gmp_strval($ms);
199
                                        $ts_max = gmdate('Y-m-d H:i:s', $ts)."'".str_pad($ms, 7, '0', STR_PAD_LEFT).' GMT';
201
                                        $ts_max = gmdate('Y-m-d H:i:s', intval($ts))."'".str_pad($ms, 7, '0', STR_PAD_LEFT).' GMT';
200
 
202
 
201
                                        $timestamp = substr($uuid, 13, 3).substr($uuid, 8, 4).'xxxxxxxx';
203
                                        $timestamp = substr($uuid, 13, 3).substr($uuid, 8, 4).'xxxxxxxx';
202
                                        echo sprintf("%-24s %s\n", "Timestamp:", "[0x$timestamp] $ts_min - $ts_max");
204
                                        echo sprintf("%-24s %s\n", "Timestamp:", "[0x$timestamp] $ts_min - $ts_max");
203
 
205
 
204
                                        $x = hexdec(substr($uuid, 16, 2).'00');
206
                                        $x = hexdec(substr($uuid, 16, 2).'00');
Line 216... Line 218...
216
                                        }
218
                                        }
217
                                        echo sprintf("%-24s %s\n", "Node ID:", "$nodeid");
219
                                        echo sprintf("%-24s %s\n", "Node ID:", "$nodeid");
218
 
220
 
219
                                        if (function_exists('decode_mac')) {
221
                                        if (function_exists('decode_mac')) {
220
                                                echo "\nIn case that this Node ID is a MAC address, here is the interpretation of that MAC address:\n";
222
                                                echo "\nIn case that this Node ID is a MAC address, here is the interpretation of that MAC address:\n";
221
                                                echo decode_mac($nodeid);
223
                                                echo decode_mac($nodeid); /** @phpstan-ignore-line */
222
                                        }
224
                                        }
223
 
225
 
224
                                        break;
226
                                        break;
225
                                case 3:
227
                                case 3:
226
                                        echo sprintf("%-24s %s\n", "Version:", "[3] Name-based (MD5 hash)");
228
                                        echo sprintf("%-24s %s\n", "Version:", "[3] Name-based (MD5 hash)");
Line 293... Line 295...
293
 
295
 
294
        if ($oid[0] == '.') {
296
        if ($oid[0] == '.') {
295
                $oid = substr($oid, 1);
297
                $oid = substr($oid, 1);
296
        }
298
        }
297
        $ary = explode('.', $oid);
299
        $ary = explode('.', $oid);
-
 
300
 
-
 
301
        if (!isset($ary[2])) return false;
-
 
302
 
298
        $val = $ary[2];
303
        $val = $ary[2];
299
 
304
 
300
        $x = gmp_init($val, 10);
305
        $x = gmp_init($val, 10);
301
        $y = gmp_strval($x, 16);
306
        $y = gmp_strval($x, 16);
302
        $y = str_pad($y, 32, "0", STR_PAD_LEFT);
307
        $y = str_pad($y, 32, "0", STR_PAD_LEFT);
Line 312... Line 317...
312
 
317
 
313
        $ary = explode('.', $oid);
318
        $ary = explode('.', $oid);
314
 
319
 
315
        if ($only_allow_root) {
320
        if ($only_allow_root) {
316
                if (count($ary) != 3) return false;
321
                if (count($ary) != 3) return false;
-
 
322
        } else {
-
 
323
                if (count($ary) < 3) return false;
317
        }
324
        }
318
 
325
 
319
        if ($ary[0] != '2') return false;
326
        if ($ary[0] != '2') return false;
320
        if ($ary[1] != '25') return false;
327
        if ($ary[1] != '25') return false;
321
        for ($i=2; $i<count($ary); $i++) {
328
        for ($i=2; $i<count($ary); $i++) {
Line 366... Line 373...
366
        # On Debian: apt-get install uuid-runtime
373
        # On Debian: apt-get install uuid-runtime
367
        if (strtoupper(substr(PHP_OS, 0, 3)) !== 'WIN') {
374
        if (strtoupper(substr(PHP_OS, 0, 3)) !== 'WIN') {
368
                $out = array();
375
                $out = array();
369
                $ec = -1;
376
                $ec = -1;
370
                exec('uuidgen -t 2>/dev/null', $out, $ec);
377
                exec('uuidgen -t 2>/dev/null', $out, $ec);
371
                if ($ec == 0) return $out[0];
378
                if ($ec == 0) return trim($out[0]);
372
        }
379
        }
373
 
380
 
374
        # If we hadn't any success yet, then implement the time based generation routine ourselves!
381
        # If we hadn't any success yet, then implement the time based generation routine ourselves!
375
        # Based on https://github.com/fredriklindberg/class.uuid.php/blob/master/class.uuid.php
382
        # Based on https://github.com/fredriklindberg/class.uuid.php/blob/master/class.uuid.php
376
 
383
 
Line 542... Line 549...
542
               substr($hash, 20, 12);
549
               substr($hash, 20, 12);
543
}
550
}
544
 
551
 
545
// Version 4 (Random) UUID
552
// Version 4 (Random) UUID
546
function gen_uuid_random() {
553
function gen_uuid_random() {
-
 
554
        # On Windows: Requires
-
 
555
        #    extension_dir = "C:\php-8.0.3-nts-Win32-vs16-x64\ext"
-
 
556
        #    extension=com_dotnet
-
 
557
        if (function_exists('com_create_guid')) {
-
 
558
                return strtolower(trim(com_create_guid(), '{}'));
-
 
559
        }
-
 
560
 
547
        # On Debian: apt-get install php-uuid
561
        # On Debian: apt-get install php-uuid
548
        # extension_loaded('uuid')
562
        # extension_loaded('uuid')
549
        if (function_exists('uuid_create')) {
563
        if (function_exists('uuid_create')) {
550
                # OSSP uuid extension like seen in php5-uuid at Debian 8
564
                # OSSP uuid extension like seen in php5-uuid at Debian 8
551
                /*
565
                /*
Line 562... Line 576...
562
        if (strtoupper(substr(PHP_OS, 0, 3)) !== 'WIN') {
576
        if (strtoupper(substr(PHP_OS, 0, 3)) !== 'WIN') {
563
                # On Debian: apt-get install uuid-runtime
577
                # On Debian: apt-get install uuid-runtime
564
                $out = array();
578
                $out = array();
565
                $ec = -1;
579
                $ec = -1;
566
                exec('uuidgen -r 2>/dev/null', $out, $ec);
580
                exec('uuidgen -r 2>/dev/null', $out, $ec);
567
                if ($ec == 0) return $out[0];
581
                if ($ec == 0) return trim($out[0]);
568
 
582
 
569
                # On Debian Jessie: UUID V4 (Random)
583
                # On Debian Jessie: UUID V4 (Random)
570
                if (file_exists('/proc/sys/kernel/random/uuid')) {
584
                if (file_exists('/proc/sys/kernel/random/uuid')) {
571
                        return file_get_contents('/proc/sys/kernel/random/uuid');
585
                        return trim(file_get_contents('/proc/sys/kernel/random/uuid'));
572
                }
586
                }
573
        }
587
        }
574
 
588
 
575
        # Make the UUID by ourselves
589
        # Make the UUID by ourselves
576
        # Source: http://rogerstringer.com/2013/11/15/generate-uuids-php
590
        # Source: http://rogerstringer.com/2013/11/15/generate-uuids-php