<?php
/*
* UUID utils for PHP
* Copyright 2011-2019 Daniel Marschall, ViaThinkSoft
* Version 2019-10-27
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
if (file_exists(__DIR__
. '/mac_utils.inc.phps')) include_once __DIR__
. '/mac_utils.inc.phps';
define('UUID_NAMEBASED_NS_DNS', '6ba7b810-9dad-11d1-80b4-00c04fd430c8');
define('UUID_NAMEBASED_NS_URL', '6ba7b811-9dad-11d1-80b4-00c04fd430c8');
define('UUID_NAMEBASED_NS_OID', '6ba7b812-9dad-11d1-80b4-00c04fd430c8');
define('UUID_NAMEBASED_NS_X500_DN', '6ba7b814-9dad-11d1-80b4-00c04fd430c8');
function gen_uuid_md5_namespace($namespace_uuid, $name) {
$namespace_uuid = str_replace('-', '', $namespace_uuid);
$namespace_uuid = hex2bin($namespace_uuid);
$hash = md5($namespace_uuid.$name);
$hash[12] = '3'; // Set version: 3 = MD5
$hash[16] = dechex(hexdec($hash[16]) & 0x3 | 0x8); // Set variant to "10xx" (RFC4122)
return substr($hash, 0, 8).'-'.
}
function gen_uuid_sha1_namespace($namespace_uuid, $name) {
$namespace_uuid = str_replace('-', '', $namespace_uuid);
$namespace_uuid = hex2bin($namespace_uuid);
$hash = sha1($namespace_uuid.$name);
$hash[12] = '5'; // Set version: 5 = SHA1
$hash[16] = dechex(hexdec($hash[16]) & 0x3 | 0x8); // Set variant to "10xx" (RFC4122)
return substr($hash, 0, 8).'-'.
}
function uuid_valid($uuid) {
if (strlen($uuid) != 32) return false;
return ($uuid == '');
}
# TODO: Don't echo
function uuid_info($uuid) {
if (!uuid_valid($uuid)) return false;
# $uuid = str_replace(array('-', '{', '}'), '', $uuid);
if ($x >= 14 /* 1110 */) $variant = 3;
else if ($x >= 12 /* 1100 */) $variant = 2;
else if ($x >= 8 /* 1000 */) $variant = 1;
else if ($x >= 0 /* 0000 */) $variant = 0;
switch ($variant) {
case 0:
echo sprintf("%-24s %s\n", "Variant:", "[0xx] NCS (reserved for backward compatibility)");
/*
* Internal structure of variant #0 UUIDs
*
* The first 6 octets are the number of 4 usec units of time that have
* passed since 1/1/80 0000 GMT. The next 2 octets are reserved for
* future use. The next octet is an address family. The next 7 octets
* are a host ID in the form allowed by the specified address family.
*
* Note that while the family field (octet 8) was originally conceived
* of as being able to hold values in the range [0..255], only [0..13]
* were ever used. Thus, the 2 MSB of this field are always 0 and are
* used to distinguish old and current UUID forms.
*
* +--------------------------------------------------------------+
* | high 32 bits of time | 0-3 .time_high
* +-------------------------------+-------------------------------
* | low 16 bits of time | 4-5 .time_low
* +-------+-----------------------+
* | reserved | 6-7 .reserved
* +---------------+---------------+
* | family | 8 .family
* +---------------+----------...-----+
* | node ID | 9-16 .node
* +--------------------------...-----+
*
*/
// Example of an UUID: 333a2276-0000-0000-0d00-00809c000000
# TODO: See https://github.com/cjsv/uuid/blob/master/Doc
# Timestamp: Count of 4us intervals since 01 Jan 1980 00:00:00 GMT
# 1/0,000004 = 250000
# Seconds between 1970 and 1980 : 315532800
# 250000*315532800=78883200000000
$timestamp = substr($uuid, 0, 12);
$ts = gmdate('Y-m-d H:i:s', $ts);
echo sprintf("%-24s %s\n", "Timestamp:", "[0x$timestamp] $ts'".str_pad($ms, 6, '0', STR_PAD_LEFT
).' GMT');
$reserved = substr($uuid, 12, 4);
echo sprintf("%-24s %s\n", "Reserved:", "0x$reserved");
# Family 13 (dds) looks like node is 00 | nnnnnn 000000.
# Family 2 is presumably (ip).
# Not sure if anything else was used.
$family_hex = substr($uuid, 16, 2);
$family_dec = hexdec($family_hex);
if ($family_dec == 2) {
$family_ = 'IP';
} else if ($family_dec == 13) {
$family_ = 'DDS (Data Link)';
} else {
$family_ = "Unknown ($family_dec)"; # TODO: Find out all families [0..13]
}
echo sprintf("%-24s %s\n", "Family:", "[0x$family_hex = $family_dec] $family_");
$nodeid = substr($uuid, 18, 14);
echo sprintf("%-24s %s\n", "Node ID:", "0x$nodeid");
# TODO: interprete node id (the family specifies it)
break;
case 1:
echo sprintf("%-24s %s\n", "Variant:", "[10x] RFC 4122 (Leach-Mealling-Salz)");
switch ($version) {
case 1:
echo sprintf("%-24s %s\n", "Version:", "[1] Time-based with unique random host identifier");
# Timestamp: Count of 100ns intervals since 15 Oct 1582 00:00:00
# 1/0,0000001 = 10000000
$ts = gmdate('Y-m-d H:i:s', $ts);
echo sprintf("%-24s %s\n", "Timestamp:", "[0x$timestamp] $ts'".str_pad($ms, 7, '0', STR_PAD_LEFT
).' GMT');
$x = $x ^ 0x8000;
$nodeid = '';
for ($i=0; $i<6; $i++) {
$nodeid .= substr($x, $i*2, 2);
if ($i != 5) $nodeid .= ':';
}
echo sprintf("%-24s %s\n", "Node ID:", "$nodeid");
echo "\nIn case that this Node ID is a MAC address, here is the interpretation of that MAC address:\n";
echo decode_mac($nodeid);
}
break;
case 2:
echo sprintf("%-24s %s\n", "Version:", "[2] DCE Security version (with POSIX UIDs)");
# TODO: is that correct???
# 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.
echo sprintf("%-24s %s\n", "Local ID:", "0x$x");
# 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".
echo sprintf("%-24s %s\n", "Local Domain:", "0x$x");
# Timestamp: Count of 100ns intervals since 15 Oct 1582 00:00:00
# 1/0,0000001 = 10000000
$timestamp = substr($uuid, 13, 3).substr($uuid, 8, 4).'00000000'; # TODO: is that OK????
$ts = gmdate('Y-m-d H:i:s', $ts);
echo sprintf("%-24s %s\n", "Timestamp:", "[0x$timestamp] $ts'".str_pad($ms, 7, '0', STR_PAD_LEFT
).' GMT');
$x = hexdec(substr($uuid, 16, 2).'00'); # TODO: is that OK????
$x = $x ^ 0x8000;
echo sprintf("%-24s %s\n", "Clock ID:", "0x$y = $x");
$nodeid = '';
for ($i=0; $i<6; $i++) {
$nodeid .= substr($x, $i*2, 2);
if ($i != 5) $nodeid .= ':';
}
echo sprintf("%-24s %s\n", "Node ID:", "$nodeid");
echo "\nIn case that this Node ID is a MAC address, here is the interpretation of that MAC address:\n";
echo decode_mac($nodeid);
}
break;
case 3:
echo sprintf("%-24s %s\n", "Version:", "[3] Name-based (MD5 hash)");
# TODO
break;
case 4:
echo sprintf("%-24s %s\n", "Version:", "[4] Random");
$rand = '';
for ($i=0; $i<16; $i++) {
$bin = str_pad($bin, 8, "0", STR_PAD_LEFT
);
if ($i == 6) {
$bin[0] = 'x';
$bin[1] = 'x';
} else if ($i == 8) {
$bin[0] = 'x';
$bin[1] = 'x';
$bin[2] = 'x';
$bin[3] = 'x';
}
$rand .= "$bin ";
}
echo sprintf("%-24s %s\n", "Random bits:", trim($rand));
break;
case 5:
echo sprintf("%-24s %s\n", "Version:", "[5] Name-based (SHA-1 hash)");
# TODO
break;
default:
echo sprintf("%-24s %s\n", "Version:", "[$version] Unknown");
break;
}
break;
case 2:
echo sprintf("%-24s %s\n", "Variant:", "[110] Reserved for Microsoft Corporation");
# TODO
break;
case 3:
echo sprintf("%-24s %s\n", "Variant:", "[111] Reserved for future use");
break;
}
}
function uuid_canonize($uuid) {
if (!uuid_valid($uuid)) return false;
return oid_to_uuid(uuid_to_oid($uuid));
}
function oid_to_uuid($oid) {
if (!is_uuid_oid($oid)) return false;
if ($oid[0] == '.') {
}
$val = $ary[2];
$y = str_pad($y, 32, "0", STR_PAD_LEFT
);
}
function is_uuid_oid($oid, $only_allow_root=false) {
if ((substr($oid, 0, 5) != '2.25.') && (substr($oid, 0, 6) != '.2.25.')) return false;
if ($oid[0] == '.') $oid = substr($oid, 1);
if ($only_allow_root) {
if (count($ary) != 3) return false;
}
for ($i=2; $i<count($ary); $i++) {
$v = $ary[$i];
if ($v < 0) return false;
}
return true;
}
function uuid_to_oid($uuid) {
if (!uuid_valid($uuid)) return false;
return '2.25.'.gmp_strval($x, 10); # TODO: parameter with or without leading dot
}
function _gen_uuid_v4() {
// http://rogerstringer.com/2013/11/15/generate-uuids-php
return sprintf( '%04x%04x-%04x-%04x-%04x-%04x%04x%04x',
);
}
function gen_uuid($prefer_timebased = true) {
# On Debian: aptitude install php-uuid
# extension_loaded('uuid')
# OSSP uuid extension like seen in php5-uuid at Debian 8
/*
$x = uuid_create($context);
if ($prefer_timebased) {
uuid_make($context, UUID_MAKE_V1);
} else {
uuid_make($context, UUID_MAKE_V4);
}
uuid_export($context, UUID_FMT_STR, $uuid);
return trim($uuid);
*/
# PECL uuid extension like seen in php-uuid at Debian 9
return trim(uuid_create
($prefer_timebased ? UUID_TYPE_TIME
: UUID_TYPE_RANDOM
));
}
# On Debian: aptitude install uuid-runtime
if ($prefer_timebased) {
exec('uuidgen -t', $out, $ec);
} else {
exec('uuidgen -r', $out, $ec);
}
if ($ec == 0) return $out[0];
if (!$prefer_timebased) {
return _gen_uuid_v4();
}
// At this point we cannot fulfil the caller's wish in regards to $prefer_timebased or not
// On Debian Jessie: UUID V4 (Random)
}
return _gen_uuid_v4();
}
function uuid_numeric_value($uuid) {
$oid = uuid_to_oid($uuid);
if (!$oid) return false;
}
function uuid_c_syntax($uuid) {
return '{ 0x' . substr($uuid, 0, 8) .
', 0x' . substr($uuid, 9, 4) .
', 0x' . substr($uuid, 14, 4) .
', { 0x' . substr($uuid, 19, 2).
', 0x' . substr($uuid, 21, 2) .
', 0x' . substr($uuid, 24, 2) .
', 0x' . substr($uuid, 26, 2) .
', 0x' . substr($uuid, 28, 2) .
', 0x' . substr($uuid, 30, 2) .
', 0x' . substr($uuid, 32, 2) .
', 0x' . substr($uuid, 34, 2) . ' } }';
}
# ---
// http://php.net/manual/de/function.hex2bin.php#113057
function hex2bin( $str ) {
$sbin = "";
for ( $i = 0; $i < $len; $i += 2 ) {
}
return $sbin;
}
}