2,8 → 2,8 |
|
/* |
* OID-Utilities for PHP |
* Copyright 2011-2020 Daniel Marschall, ViaThinkSoft |
* Version 2020-06-11 |
* Copyright 2011-2019 Daniel Marschall, ViaThinkSoft |
* Version 2019-03-25 |
* |
* Licensed under the Apache License, Version 2.0 (the "License"); |
* you may not use this file except in compliance with the License. |
20,8 → 20,8 |
|
// All functions in this library are compatible with leading zeroes (not recommended) and leading dots |
|
// TODO: change some function names, so that they have a uniform naming schema, and rename "oid identifier" into "asn.1 alphanumeric identifier" |
// oid_id_is_valid() => asn1_alpha_id_valid() |
// TODO: change the function names, so that they have a uniform naming schema, and rename "oid identifier" into "asn.1 alphanumeric identifier" |
// TODO: Function for finding a shared ancestor, e.g. oid_shared_ancestor('2.999.1.2.3', '2.999.4.5') == '2.999' |
|
define('OID_DOT_FORBIDDEN', 0); |
define('OID_DOT_OPTIONAL', 1); |
305,26 → 305,6 |
} |
|
/** |
* Checks if two OIDs in dot-notation are equal |
* @author Daniel Marschall, ViaThinkSoft |
* @version 2020-05-27 |
* @param $oidA (string)<br /> |
* First OID |
* @param $oidB (string)<br /> |
* Second OID |
* @return (bool) True if the OIDs are equal |
**/ |
function oid_dotnotation_equal($oidA, $oidB) { |
$oidA = sanitizeOID($oidA, false); |
if ($oidA === false) return null; |
|
$oidB = sanitizeOID($oidB, false); |
if ($oidB === false) return null; |
|
return $oidA === $oidB; |
} |
|
/** |
* Removes leading zeroes from an OID in dot notation. |
* @author Daniel Marschall, ViaThinkSoft |
* @version 2015-08-17 |
437,6 → 417,7 |
|
return count($ary) - count($bry); |
} |
|
/* |
assert(oid_distance('2.999.1.2.3', '2.999.4.5') === false); |
assert(oid_distance('2.999.1.2', '2.999') === 2); |
477,50 → 458,7 |
return $oid; |
} |
|
/** |
* Find the common ancestor of two or more OIDs |
* @author Daniel Marschall, ViaThinkSoft |
* @version 2020-05-27 |
* @param $oids (array)<br /> |
* An array of multiple OIDs, e.g. 2.999.1 and 2.999.2.3.4 |
* @return (mixed) The common ancestor, e.g. 2.999, or false if there is no common ancestor. |
**/ |
function oid_common_ancestor(array $oids) { |
$shared = array(); |
|
if (!is_array($oids)) return false; |
if (count($oids) === 0) return false; |
|
foreach ($oids as &$oid) { |
$oid = sanitizeOID($oid, false); |
if ($oid === false) return false; |
$oid = explode('.', $oid); |
} |
|
$max_ok = count($oids[0]); |
for ($i=1; $i<count($oids); $i++) { |
for ($j=0; $j<min(count($oids[$i]),count($oids[0])); $j++) { |
if ($oids[$i][$j] != $oids[0][$j]) { |
if ($j < $max_ok) $max_ok = $j; |
break; |
} |
} |
if ($j < $max_ok) $max_ok = $j; |
} |
|
$out = array(); |
for ($i=0; $i<$max_ok; $i++) { |
$out[] = $oids[0][$i]; |
} |
return implode('.', $out); |
} |
/* |
assert(oid_shared_ancestor(array('2.999.4.5.3', '2.999.4.5')) === "2.999.4.5"); |
assert(oid_shared_ancestor(array('2.999.4.5', '2.999.4.5.3')) === "2.999.4.5"); |
assert(oid_shared_ancestor(array('2.999.1.2.3', '2.999.4.5')) === "2.999"); |
*/ |
|
|
# === OID-IRI NOTATION FUNCTIONS === |
|
if (!function_exists('mb_ord')) { |
667,7 → 605,7 |
/** |
* Tries to shorten/simplify an IRI by applying "long arcs", e.g. /2/999/123 -> /Example/123 . |
* @author Daniel Marschall, ViaThinkSoft |
* @version 2020-05-22 |
* @version 2014-12-28 |
* @param $iri (string)<br /> |
* An OID in OID-IRI notation, e.g. /Example/test |
* @return (string) The modified IRI. |
675,17 → 613,25 |
function iri_add_longarcs($iri) { |
$iri_long_arcs = iri_get_long_arcs(); |
|
if (!iri_valid($iri)) return false; |
// TODO: $iri valid? |
|
$ary = explode('/', $iri); |
|
$ary_number_iri = $ary; |
if ($ary_number_iri[1] == 'Joint-ISO-ITU-T') $ary_number_iri[1] = '2'; |
|
/* |
if ($ary_number_iri[1] == '2') { |
// TODO: /2/Example -> /2/999 -> /Example |
} else { |
// Currently, only long arcs inside .2 are defined |
// return $iri; |
} |
*/ |
$number_iri = implode('/', $ary_number_iri); |
|
foreach ($iri_long_arcs as $cur_longarc => $cur_iri) { |
assert(iri_valid($cur_iri)); |
// TODO: $cur_iri valid? |
|
if (strpos($number_iri.'/', $cur_iri.'/') === 0) { |
$cnt = substr_count($cur_iri, '/'); |
for ($i=1; $i<$cnt; $i++) { |
700,9 → 646,6 |
|
return $iri; |
} |
/* |
assert(iri_add_longarcs('/2/999/123') === '/Example/123'); |
*/ |
|
# === FUNCTIONS FOR OIDS IN ASN.1 NOTATION === |
|
709,18 → 652,15 |
/** |
* Checks if an ASN.1 identifier is valid. |
* @author Daniel Marschall, ViaThinkSoft |
* @version 2020-05-22 |
* @version 2014-12-09 |
* @param $id (string)<br /> |
* An ASN.1 identifier, e.g. "example". Not "example(99)" or "99" and not a path like "{ 2 999 }" |
* Note: Use asn1_path_valid() for validating a whole ASN.1 notation path. |
* @return (bool) true, if the identifier is valid: It begins with an lowercase letter and contains only 0-9, a-z, A-Z and "-" |
**/ |
# TODO: umbenennen in asn1_alpha_id_valid |
function oid_id_is_valid($id) { |
// see Rec. ITU-T X.660 | ISO/IEC 9834-1, clause 7.7 |
// and Rec. ITU-T X.680 | ISO/IEC 8824-1, clause 12.3 |
if (substr($id,-1,1) == '-') return false; |
if (strstr($id,'--')) return false; |
return preg_match('/^([a-z][a-zA-Z0-9-]*)$/', $id) != 0; |
return preg_match('/^([a-z][a-zA-Z0-9-]*)$/', $id); |
} |
|
/** |
853,26 → 793,10 |
*/ |
|
/** |
* Gets the last numeric identifier of an ASN.1 notation OID. |
* @author Daniel Marschall, ViaThinkSoft |
* @version 2020-06-11 |
* @param $asn1id (string)<br /> |
* An ASN.1 identifier string, e.g. { 2 example(999) test(1) } |
* @return (int) The last numeric identifier arc, e.g. "1" |
**/ |
function asn1_last_identifier($asn1id) { |
$asn1id = preg_replace('@\(\s*\d+\s*\)@', '', $asn1id); |
$asn1id = trim(str_replace(array('{', '}', "\t"), ' ', $asn1id)); |
$ary = explode(' ', $asn1id); |
$asn1id = $ary[count($ary)-1]; |
return preg_match('#[^0-9]#',$asn1id) ? $asn1id : false; |
} |
|
/** |
* "Soft corrects" an invalid ASN.1 identifier.<br /> |
* Attention, by "soft correcting" the ID, it is not authoritative anymore, and might not be able to be resolved by ORS. |
* @author Daniel Marschall, ViaThinkSoft |
* @version 2020-05-22 |
* @version 2014-12-09 |
* @param $id (string)<br /> |
* An ASN.1 identifier. |
* @param $append_id_prefix (bool)<br /> |
887,9 → 811,6 |
// Convert "_" to "-" |
$id = str_replace('_', '-', $id); |
|
// Convert "--" to "-" |
$id = str_replace('--', '-', $id); |
|
// Remove invalid characters |
$id = preg_replace('/[^a-zA-Z0-9-]+/', '', $id); |
|
907,3 → 828,4 |
|
return $id; |
} |
|