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