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