2,8 → 2,8 |
|
/* |
* OID-Utilities for PHP |
* Copyright 2011-2021 Daniel Marschall, ViaThinkSoft |
* Version 2021-05-21 |
* Copyright 2011-2020 Daniel Marschall, ViaThinkSoft |
* Version 2020-09-12 |
* |
* Licensed under the Apache License, Version 2.0 (the "License"); |
* you may not use this file except in compliance with the License. |
31,13 → 31,13 |
* Checks if an OID has a valid dot notation. |
* @author Daniel Marschall, ViaThinkSoft |
* @version 2014-12-09 |
* @param string $oid<br /> |
* @param $oid (string)<br /> |
* An OID in dot notation. |
* @param boolean $allow_leading_zeroes<br /> |
* @param $allow_leading_zeroes (bool)<br /> |
* true of leading zeroes are allowed or not. |
* @param boolean $allow_leading_dot<br /> |
* @param $allow_leading_dot (bool)<br /> |
* true of leading dots are allowed or not. |
* @return boolean true if the dot notation is valid. |
* @return (bool) true if the dot notation is valid. |
**/ |
function oid_valid_dotnotation($oid, $allow_leading_zeroes=true, $allow_leading_dot=false, $min_len=0) { |
$regex = oid_validation_regex($allow_leading_zeroes, $allow_leading_dot, $min_len); |
50,11 → 50,11 |
* Returns a full regular expression to validate an OID in dot-notation |
* @author Daniel Marschall, ViaThinkSoft |
* @version 2014-12-09 |
* @param boolean $allow_leading_zeroes<br /> |
* @param $allow_leading_zeroes (bool)<br /> |
* true of leading zeroes are allowed or not. |
* @param boolean $allow_leading_dot<br /> |
* @param $allow_leading_dot (bool)<br /> |
* true of leading dots are allowed or not. |
* @return string The regular expression |
* @return (string) The regular expression |
**/ |
function oid_validation_regex($allow_leading_zeroes=true, $allow_leading_dot=false, $min_len=0) { |
$leading_dot_policy = $allow_leading_dot ? OID_DOT_OPTIONAL : OID_DOT_FORBIDDEN; |
69,19 → 69,19 |
* It can be inserted into regular expressions. |
* @author Daniel Marschall, ViaThinkSoft |
* @version 2014-12-09 |
* @param int $min_len<br /> |
* @param $min_len (int)<br /> |
* 0="." and greater will be recognized, but not ""<br /> |
* 1=".2" and greater will be recognized<br /> |
* 2=".2.999" and greater will be recognized (default)<br /> |
* etc. |
* @param boolean $allow_leading_zeroes<br /> |
* @param $allow_leading_zeroes (bool)<br /> |
* true: ".2.0999" will be recognized<br /> |
* false: ".2.0999" won't be recognized (default) |
* @param int $leading_dot_policy<br /> |
* @param $leading_dot_policy (int)<br /> |
* 0 (OID_DOT_FORBIDDEN): forbidden<br /> |
* 1 (OID_DOT_OPTIONAL) : optional (default)<br /> |
* 2 (OID_DOT_REQUIRED) : enforced |
* @return string|false A regular expression which matches OIDs in dot notation |
* @return (string) A regular expression which matches OIDs in dot notation |
**/ |
function oid_part_regex($min_len=2, $allow_leading_zeroes=false, $leading_dot_policy=OID_DOT_OPTIONAL) { |
switch ($leading_dot_policy) { |
96,7 → 96,7 |
break; |
default: |
assert(false); |
return false; |
break; |
} |
|
$lead_zero = $allow_leading_zeroes ? '0*' : ''; |
133,24 → 133,24 |
* Searches all OIDs in $text and outputs them as array. |
* @author Daniel Marschall, ViaThinkSoft |
* @version 2014-12-09 |
* @param string $text<br /> |
* @param $text (string)<br /> |
* The text to be parsed |
* @param int $min_len<br /> |
* @param $min_len (int)<br /> |
* 0="." and greater will be recognized, but not ""<br /> |
* 1=".2" and greater will be recognized<br /> |
* 2=".2.999" and greater will be recognized (default)<br /> |
* etc. |
* @param boolean $allow_leading_zeroes<br /> |
* @param $allow_leading_zeroes (bool)<br /> |
* true: ".2.0999" will be recognized<br /> |
* false: ".2.0999" won't be recognized (default) |
* @param int $leading_dot_policy<br /> |
* @param $leading_dot_policy (int)<br /> |
* 0 (OID_DOT_FORBIDDEN): forbidden<br /> |
* 1 (OID_DOT_OPTIONAL) : optional (default)<br /> |
* 2 (OID_DOT_REQUIRED) : enforced |
* @param boolean $requires_whitespace_delimiters<br /> |
* @param $requires_whitespace_delimiters (bool)<br /> |
* true: "2.999" will be recognized, as well as " 2.999 " (default)<br /> |
* false: "2.999!" will be reconigzed, as well as "2.999.c" (this might be used in in documentations with templates) |
* @return string[] An array of OIDs in dot notation |
* @return (array<string>) An array of OIDs in dot notation |
**/ |
function parse_oids($text, $min_len=2, $allow_leading_zeroes=false, $leading_dot_policy=OID_DOT_OPTIONAL, $requires_whitespace_delimiters=true) { |
$regex = oid_detection_regex($min_len, $allow_leading_zeroes, $leading_dot_policy, $requires_whitespace_delimiters); |
164,22 → 164,22 |
* Returns a full regular expression for detecting OIDs in dot notation inside a text. |
* @author Daniel Marschall, ViaThinkSoft |
* @version 2014-12-09 |
* @param int $min_len<br /> |
* @param $min_len (int)<br /> |
* 0="." and greater will be recognized, but not ""<br /> |
* 1=".2" and greater will be recognized<br /> |
* 2=".2.999" and greater will be recognized (default)<br /> |
* etc. |
* @param boolean $allow_leading_zeroes<br /> |
* @param $allow_leading_zeroes (bool)<br /> |
* true: ".2.0999" will be recognized<br /> |
* false: ".2.0999" won't be recognized (default) |
* @param int $leading_dot_policy<br /> |
* @param $leading_dot_policy (int)<br /> |
* 0 (OID_DOT_FORBIDDEN): forbidden<br /> |
* 1 (OID_DOT_OPTIONAL) : optional (default)<br /> |
* 2 (OID_DOT_REQUIRED) : enforced |
* @param boolean $requires_whitespace_delimiters<br /> |
* @param $requires_whitespace_delimiters (bool)<br /> |
* true: "2.999" will be recognized, as well as " 2.999 " (default)<br /> |
* false: "2.999!" will be reconigzed, as well as "2.999.c" (this might be used in in documentations with templates) |
* @return string The regular expression |
* @return (string) The regular expression |
**/ |
function oid_detection_regex($min_len=2, $allow_leading_zeroes=false, $leading_dot_policy=OID_DOT_OPTIONAL, $requires_whitespace_delimiters=true) { |
if ($requires_whitespace_delimiters) { |
202,9 → 202,9 |
* Leading dots and leading zeroes are tolerated. |
* @author Daniel Marschall, ViaThinkSoft |
* @version 2014-12-16 |
* @param string $oid<br /> |
* @param $oid (string)<br /> |
* An OID in dot notation. |
* @return string|false The parent OID in dot notation. |
* @return (string) The parent OID in dot notation. |
**/ |
function oid_up($oid) { |
$oid = sanitizeOID($oid, 'auto'); |
221,7 → 221,7 |
* Outputs the depth of an OID. |
* @author Daniel Marschall, ViaThinkSoft |
* @version 2014-12-09 |
* @param string $oid An OID in dot notation (with or without leading dot) |
* @param $oid (string) An OID in dot notation (with or without leading dot) |
* @return (int) The depth of the OID, e.g. 2.999 and .2.999 has the length 2. |
**/ |
function oid_len($oid) { |
239,9 → 239,9 |
* The OID will not be checked for validity! |
* @author Daniel Marschall, ViaThinkSoft |
* @version 2014-12-17 |
* @param string $oid<br /> |
* @param $oid (string)<br /> |
* An OID in dot notation. |
* @return string[] An array with all parent OIDs. |
* @return (array<string>) An array with all parent OIDs. |
**/ |
function oid_parents($oid) { |
$parents = array(); |
267,10 → 267,10 |
* Sorts an array containing OIDs in dot notation. |
* @author Daniel Marschall, ViaThinkSoft |
* @version 2014-12-09 |
* @param string[] $ary<br /> |
* @param $ary (array<string>)<br /> |
* An array of OIDs in dot notation.<br /> |
* This array will be changed by this method. |
* @param boolean $output_with_leading_dot<br /> |
* @param $output_with_leading_dot (bool)<br /> |
* true: The array will be normalized to OIDs with a leading dot. |
* false: The array will be normalized to OIDs without a leading dot. (default) |
**/ |
280,7 → 280,6 |
$none = $output_with_leading_dot ? '.' : ''; |
|
$d = array(); |
$oid = null; |
foreach ($ary as &$oid) { |
if (($oid == '') || ($oid == '.')) { |
$out[] = $none; |
295,7 → 294,6 |
unset($oid); |
ksort($d); |
|
$data = null; |
foreach ($d as $firstarc => &$data) { |
oidSort($data); |
foreach ($data as &$rest) { |
311,11 → 309,11 |
* Checks if two OIDs in dot-notation are equal |
* @author Daniel Marschall, ViaThinkSoft |
* @version 2020-05-27 |
* @param string $oidA<br /> |
* @param $oidA (string)<br /> |
* First OID |
* @param string $oidB<br /> |
* @param $oidB (string)<br /> |
* Second OID |
* @return boolean|null True if the OIDs are equal, null if one of the OIDs are invalid |
* @return (bool) True if the OIDs are equal |
**/ |
function oid_dotnotation_equal($oidA, $oidB) { |
$oidA = sanitizeOID($oidA, false); |
331,13 → 329,13 |
* Removes leading zeroes from an OID in dot notation. |
* @author Daniel Marschall, ViaThinkSoft |
* @version 2015-08-17 |
* @param string $oid<br /> |
* @param $oid (string)<br /> |
* An OID in dot notation. |
* @param boolean $leading_dot<br /> |
* @param $leading_dot (bool)<br /> |
* true: The OID is valid, if it contains a leading dot.<br /> |
* false (default): The OID is valid, if it does not contain a leading dot. |
* 'auto: Allow both |
* @return string|false The OID without leading dots, or <code>false</code> if the OID is syntactically wrong. |
* @return (mixed) The OID without leading dots, or <code>false</code> if the OID is syntactically wrong. |
**/ |
$oid_sanitize_cache = array(); |
function sanitizeOID($oid, $leading_dot=false) { |
383,9 → 381,9 |
* This function tolerates leading dots. |
* @author Daniel Marschall, ViaThinkSoft |
* @version 2014-12-16 |
* @param string $oid<br /> |
* @param $oid (string)<br /> |
* An OID in dot notation. |
* @return string|false The top arc of the OID or empty string if it is already the root ('.') |
* @return (mixed) The top arc of the OID or empty string if it is already the root ('.') |
**/ |
function oid_toparc($oid) { |
$leadingdot = substr($oid,0,1) == '.'; |
412,11 → 410,11 |
* This function tolerates leading dots and leading zeroes. |
* @author Daniel Marschall, ViaThinkSoft |
* @version 2014-12-20 |
* @param string $a<br /> |
* @param $a (string)<br /> |
* An OID. |
* @param string $b<br /> |
* @param $b (string)<br /> |
* An OID. |
* @return int|false false if both OIDs do not have a child-parent or parent-child relation, e.g. oid_distance('2.999.1.2.3', '2.999.4.5') = false, or if one of the OIDs is syntactially invalid<br /> |
* @return (string) false if both OIDs do not have a child-parent or parent-child relation, e.g. oid_distance('2.999.1.2.3', '2.999.4.5') = false, or if one of the OIDs is syntactially invalid<br /> |
* >0 if $a is more specific than $b , e.g. oid_distance('2.999.1.2', '2.999') = 2<br /> |
* <0 if $a is more common than $b , e.g. oid_distance('2.999', '2.999.1.2') = -2 |
**/ |
451,9 → 449,9 |
* Leading zeroes are tolerated. |
* @author Daniel Marschall, ViaThinkSoft |
* @version 2014-12-20 |
* @param string $oid<br /> |
* @param $oid (string)<br /> |
* An OID. |
* @return string|false The OID with a leading dot or false if the OID is syntactially wrong. |
* @return (string) The OID with a leading dot or false if the OID is syntactially wrong. |
**/ |
function oid_add_leading_dot($oid) { |
$oid = sanitizeOID($oid, 'auto'); |
468,9 → 466,9 |
* Leading zeroes are tolerated. |
* @author Daniel Marschall, ViaThinkSoft |
* @version 2014-12-20 |
* @param string $oid<br /> |
* @param $oid (string)<br /> |
* An OID. |
* @return string|false The OID without a leading dot or false if the OID is syntactially wrong. |
* @return (string) The OID without a leading dot or false if the OID is syntactially wrong. |
**/ |
function oid_remove_leading_dot($oid) { |
$oid = sanitizeOID($oid, 'auto'); |
484,9 → 482,9 |
* Find the common ancestor of two or more OIDs |
* @author Daniel Marschall, ViaThinkSoft |
* @version 2020-05-27 |
* @param string[] $oids<br /> |
* @param $oids (array)<br /> |
* An array of multiple OIDs, e.g. 2.999.1 and 2.999.2.3.4 |
* @return string|false The common ancestor, e.g. 2.999, or false if there is no common ancestor. |
* @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(); |
500,9 → 498,9 |
$oid = explode('.', $oid); |
} |
|
$max_ok = strlen($oids[0]); |
$max_ok = count($oids[0]); |
for ($i=1; $i<count($oids); $i++) { |
for ($j=0; $j<min(strlen($oids[$i]),strlen($oids[0])); $j++) { |
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; |
601,9 → 599,9 |
* Checks if an IRI identifier is valid or not. |
* @author Daniel Marschall, ViaThinkSoft |
* @version 2014-12-17 |
* @param string $iri<br /> |
* @param $iri (string)<br /> |
* An OID in OID-IRI notation, e.g. /Example/test |
* @return boolean true if the IRI identifier is valid. |
* @return (bool) true if the IRI identifier is valid. |
**/ |
function iri_valid($iri) { |
if ($iri == '/') return true; // OK? |
672,9 → 670,9 |
* 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 |
* @param string $iri<br /> |
* @param $iri (string)<br /> |
* An OID in OID-IRI notation, e.g. /Example/test |
* @return string|false The modified IRI. |
* @return (string) The modified IRI. |
**/ |
function iri_add_longarcs($iri) { |
$iri_long_arcs = iri_get_long_arcs(); |
714,10 → 712,10 |
* Checks if an ASN.1 identifier is valid. |
* @author Daniel Marschall, ViaThinkSoft |
* @version 2020-05-22 |
* @param string $id<br /> |
* @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 boolean true, if the identifier is valid: It begins with an lowercase letter and contains only 0-9, a-z, A-Z and "-" |
* @return (bool) true, if the identifier is valid: It begins with an lowercase letter and contains only 0-9, a-z, A-Z and "-" |
**/ |
function oid_id_is_valid($id) { |
// see Rec. ITU-T X.660 | ISO/IEC 9834-1, clause 7.7 |
733,9 → 731,9 |
* This function will fail (return false) if there are unresolved symbols, e.g. {iso test} is not valid while { iso 123 } is valid. |
* @author Daniel Marschall, ViaThinkSoft |
* @version 2014-12-17 |
* @param string $asn1<br /> |
* @param $asn (string)<br /> |
* An OID in ASN.1 notation. |
* @return boolean true if the identifier is valid. |
* @return (bools) true if the identifier is valid. |
**/ |
function asn1_path_valid($asn1) { |
return asn1_to_dot($asn1) != false; |
805,9 → 803,9 |
* This function will fail (return false) if there are unresolved symbols, e.g. {iso test} will not be resolved to 1.test |
* @author Daniel Marschall, ViaThinkSoft |
* @version 2014-12-17 |
* @param string $asn<br /> |
* @param $asn (string)<br /> |
* An OID in ASN.1 notation. |
* @return string|false An OID in dot notation without leading dot or false if the path is invalid. |
* @return (string) An OID in dot notation without leading dot or false if the path is invalid. |
**/ |
function asn1_to_dot($asn) { |
$standardized = asn1_get_standardized_array(); |
862,9 → 860,9 |
* Gets the last numeric identifier of an ASN.1 notation OID. |
* @author Daniel Marschall, ViaThinkSoft |
* @version 2020-06-11 |
* @param string $asn1id<br /> |
* @param $asn1id (string)<br /> |
* An ASN.1 identifier string, e.g. { 2 example(999) test(1) } |
* @return int|false The last numeric identifier arc, e.g. "1" or false if the ID is invalid |
* @return (int) The last numeric identifier arc, e.g. "1" |
**/ |
function asn1_last_identifier($asn1id) { |
$asn1id = preg_replace('@\(\s*\d+\s*\)@', '', $asn1id); |
871,7 → 869,7 |
$asn1id = trim(str_replace(array('{', '}', "\t"), ' ', $asn1id)); |
$ary = explode(' ', $asn1id); |
$asn1id = $ary[count($ary)-1]; |
return preg_match('#[^0-9]#',$asn1id) ? (int)$asn1id : false; |
return preg_match('#[^0-9]#',$asn1id) ? $asn1id : false; |
} |
|
/** |
879,12 → 877,12 |
* 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 |
* @param string $id<br /> |
* @param $id (string)<br /> |
* An ASN.1 identifier. |
* @param boolean $append_id_prefix<br /> |
* @param $append_id_prefix (bool)<br /> |
* true (default): If the identifier doesn't start with a-Z, the problem will be solved by prepending "id-" to the identifier.<br /> |
* false: If the identifier doesn't start with a-Z, then the problem cannot be solved (method returns empty string). |
* @return string The "soft corrected" ASN.1 identifier.<br /> |
* @return (string) The "soft corrected" ASN.1 identifier.<br /> |
* Invalid characters will be removed.<br /> |
* Uncorrectable start elements (0-9 or "-") will be either removed or solved by prepending "id-" (see <code>$append_id_prefix</code>)<br /> |
* If the identifier begins with an upper case letter, the letter will be converted into lower case. |