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