Subversion Repositories php_utils

Rev

Rev 5 | Rev 23 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 5 Rev 13
Line 1... Line 1...
1
<?php
1
<?php
2
 
2
 
3
/*
3
/*
4
 * OID-Utilities for PHP
4
 * OID-Utilities for PHP
5
 * Copyright 2011-2021 Daniel Marschall, ViaThinkSoft
5
 * Copyright 2011-2021 Daniel Marschall, ViaThinkSoft
6
 * Version 2021-05-21
6
 * Version 2021-09-23
7
 *
7
 *
8
 * Licensed under the Apache License, Version 2.0 (the "License");
8
 * Licensed under the Apache License, Version 2.0 (the "License");
9
 * you may not use this file except in compliance with 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
10
 * You may obtain a copy of the License at
11
 *
11
 *
Line 29... Line 29...
29
 
29
 
30
/**
30
/**
31
 * Checks if an OID has a valid dot notation.
31
 * Checks if an OID has a valid dot notation.
32
 * @author  Daniel Marschall, ViaThinkSoft
32
 * @author  Daniel Marschall, ViaThinkSoft
33
 * @version 2014-12-09
33
 * @version 2014-12-09
34
 * @param   string $oid<br />
34
 * @param   string $oid<br/>
35
 *              An OID in dot notation.
35
 *              An OID in dot notation.
36
 * @param   boolean $allow_leading_zeroes<br />
36
 * @param   boolean $allow_leading_zeroes<br/>
37
 *              true of leading zeroes are allowed or not.
37
 *              true of leading zeroes are allowed or not.
38
 * @param   boolean $allow_leading_dot<br />
38
 * @param   boolean $allow_leading_dot<br/>
39
 *              true of leading dots are allowed or not.
39
 *              true of leading dots are allowed or not.
40
 * @return  boolean true if the dot notation is valid.
40
 * @return  boolean true if the dot notation is valid.
41
 **/
41
 **/
42
function oid_valid_dotnotation($oid, $allow_leading_zeroes=true, $allow_leading_dot=false, $min_len=0) {
42
function oid_valid_dotnotation($oid, $allow_leading_zeroes=true, $allow_leading_dot=false, $min_len=0) {
43
        $regex = oid_validation_regex($allow_leading_zeroes, $allow_leading_dot, $min_len);
43
        $regex = oid_validation_regex($allow_leading_zeroes, $allow_leading_dot, $min_len);
Line 48... Line 48...
48
 
48
 
49
/**
49
/**
50
 * Returns a full regular expression to validate an OID in dot-notation
50
 * Returns a full regular expression to validate an OID in dot-notation
51
 * @author  Daniel Marschall, ViaThinkSoft
51
 * @author  Daniel Marschall, ViaThinkSoft
52
 * @version 2014-12-09
52
 * @version 2014-12-09
53
 * @param   boolean $allow_leading_zeroes<br />
53
 * @param   boolean $allow_leading_zeroes<br/>
54
 *              true of leading zeroes are allowed or not.
54
 *              true of leading zeroes are allowed or not.
55
 * @param   boolean $allow_leading_dot<br />
55
 * @param   boolean $allow_leading_dot<br/>
56
 *              true of leading dots are allowed or not.
56
 *              true of leading dots are allowed or not.
57
 * @return  string The regular expression
57
 * @return  string The regular expression
58
 **/
58
 **/
59
function oid_validation_regex($allow_leading_zeroes=true, $allow_leading_dot=false, $min_len=0) {
59
function oid_validation_regex($allow_leading_zeroes=true, $allow_leading_dot=false, $min_len=0) {
60
        $leading_dot_policy = $allow_leading_dot ? OID_DOT_OPTIONAL : OID_DOT_FORBIDDEN;
60
        $leading_dot_policy = $allow_leading_dot ? OID_DOT_OPTIONAL : OID_DOT_FORBIDDEN;
Line 67... Line 67...
67
/**
67
/**
68
 * Returns a partial regular expression which matches valid OIDs in dot notation.
68
 * Returns a partial regular expression which matches valid OIDs in dot notation.
69
 * It can be inserted into regular expressions.
69
 * It can be inserted into regular expressions.
70
 * @author  Daniel Marschall, ViaThinkSoft
70
 * @author  Daniel Marschall, ViaThinkSoft
71
 * @version 2014-12-09
71
 * @version 2014-12-09
72
 * @param   int $min_len<br />
72
 * @param   int $min_len<br/>
73
 *              0="." and greater will be recognized, but not ""<br />
73
 *              0="." and greater will be recognized, but not ""<br/>
74
 *              1=".2" and greater will be recognized<br />
74
 *              1=".2" and greater will be recognized<br/>
75
 *              2=".2.999" and greater will be recognized (default)<br />
75
 *              2=".2.999" and greater will be recognized (default)<br/>
76
 *              etc.
76
 *              etc.
77
 * @param   boolean $allow_leading_zeroes<br />
77
 * @param   boolean $allow_leading_zeroes<br/>
78
 *              true:  ".2.0999" will be recognized<br />
78
 *              true:  ".2.0999" will be recognized<br/>
79
 *              false: ".2.0999" won't be recognized (default)
79
 *              false: ".2.0999" won't be recognized (default)
80
 * @param   int $leading_dot_policy<br />
80
 * @param   int $leading_dot_policy<br/>
81
 *              0 (OID_DOT_FORBIDDEN): forbidden<br />
81
 *              0 (OID_DOT_FORBIDDEN): forbidden<br/>
82
 *              1 (OID_DOT_OPTIONAL) : optional (default)<br />
82
 *              1 (OID_DOT_OPTIONAL) : optional (default)<br/>
83
 *              2 (OID_DOT_REQUIRED) : enforced
83
 *              2 (OID_DOT_REQUIRED) : enforced
84
 * @return  string|false A regular expression which matches OIDs in dot notation
84
 * @return  string|false A regular expression which matches OIDs in dot notation
85
 **/
85
 **/
86
function oid_part_regex($min_len=2, $allow_leading_zeroes=false, $leading_dot_policy=OID_DOT_OPTIONAL) {
86
function oid_part_regex($min_len=2, $allow_leading_zeroes=false, $leading_dot_policy=OID_DOT_OPTIONAL) {
87
        switch ($leading_dot_policy) {
87
        switch ($leading_dot_policy) {
Line 131... Line 131...
131
 
131
 
132
/**
132
/**
133
 * Searches all OIDs in $text and outputs them as array.
133
 * Searches all OIDs in $text and outputs them as array.
134
 * @author  Daniel Marschall, ViaThinkSoft
134
 * @author  Daniel Marschall, ViaThinkSoft
135
 * @version 2014-12-09
135
 * @version 2014-12-09
136
 * @param   string $text<br />
136
 * @param   string $text<br/>
137
 *              The text to be parsed
137
 *              The text to be parsed
138
 * @param   int $min_len<br />
138
 * @param   int $min_len<br/>
139
 *              0="." and greater will be recognized, but not ""<br />
139
 *              0="." and greater will be recognized, but not ""<br/>
140
 *              1=".2" and greater will be recognized<br />
140
 *              1=".2" and greater will be recognized<br/>
141
 *              2=".2.999" and greater will be recognized (default)<br />
141
 *              2=".2.999" and greater will be recognized (default)<br/>
142
 *              etc.
142
 *              etc.
143
 * @param   boolean $allow_leading_zeroes<br />
143
 * @param   boolean $allow_leading_zeroes<br/>
144
 *              true:  ".2.0999" will be recognized<br />
144
 *              true:  ".2.0999" will be recognized<br/>
145
 *              false: ".2.0999" won't be recognized (default)
145
 *              false: ".2.0999" won't be recognized (default)
146
 * @param   int $leading_dot_policy<br />
146
 * @param   int $leading_dot_policy<br/>
147
 *              0 (OID_DOT_FORBIDDEN): forbidden<br />
147
 *              0 (OID_DOT_FORBIDDEN): forbidden<br/>
148
 *              1 (OID_DOT_OPTIONAL) : optional (default)<br />
148
 *              1 (OID_DOT_OPTIONAL) : optional (default)<br/>
149
 *              2 (OID_DOT_REQUIRED) : enforced
149
 *              2 (OID_DOT_REQUIRED) : enforced
150
 * @param   boolean $requires_whitespace_delimiters<br />
150
 * @param   boolean $requires_whitespace_delimiters<br/>
151
 *              true:  "2.999" will be recognized, as well as " 2.999 " (default)<br />
151
 *              true:  "2.999" will be recognized, as well as " 2.999 " (default)<br/>
152
 *              false: "2.999!" will be reconigzed, as well as "2.999.c" (this might be used in in documentations with templates)
152
 *              false: "2.999!" will be reconigzed, as well as "2.999.c" (this might be used in in documentations with templates)
153
 * @return  string[] An array of OIDs in dot notation
153
 * @return  string[] An array of OIDs in dot notation
154
 **/
154
 **/
155
function parse_oids($text, $min_len=2, $allow_leading_zeroes=false, $leading_dot_policy=OID_DOT_OPTIONAL, $requires_whitespace_delimiters=true) {
155
function parse_oids($text, $min_len=2, $allow_leading_zeroes=false, $leading_dot_policy=OID_DOT_OPTIONAL, $requires_whitespace_delimiters=true) {
156
        $regex = oid_detection_regex($min_len, $allow_leading_zeroes, $leading_dot_policy, $requires_whitespace_delimiters);
156
        $regex = oid_detection_regex($min_len, $allow_leading_zeroes, $leading_dot_policy, $requires_whitespace_delimiters);
Line 162... Line 162...
162
 
162
 
163
/**
163
/**
164
 * Returns a full regular expression for detecting OIDs in dot notation inside a text.
164
 * Returns a full regular expression for detecting OIDs in dot notation inside a text.
165
 * @author  Daniel Marschall, ViaThinkSoft
165
 * @author  Daniel Marschall, ViaThinkSoft
166
 * @version 2014-12-09
166
 * @version 2014-12-09
167
 * @param   int $min_len<br />
167
 * @param   int $min_len<br/>
168
 *              0="." and greater will be recognized, but not ""<br />
168
 *              0="." and greater will be recognized, but not ""<br/>
169
 *              1=".2" and greater will be recognized<br />
169
 *              1=".2" and greater will be recognized<br/>
170
 *              2=".2.999" and greater will be recognized (default)<br />
170
 *              2=".2.999" and greater will be recognized (default)<br/>
171
 *              etc.
171
 *              etc.
172
 * @param   boolean $allow_leading_zeroes<br />
172
 * @param   boolean $allow_leading_zeroes<br/>
173
 *              true:  ".2.0999" will be recognized<br />
173
 *              true:  ".2.0999" will be recognized<br/>
174
 *              false: ".2.0999" won't be recognized (default)
174
 *              false: ".2.0999" won't be recognized (default)
175
 * @param   int $leading_dot_policy<br />
175
 * @param   int $leading_dot_policy<br/>
176
 *              0 (OID_DOT_FORBIDDEN): forbidden<br />
176
 *              0 (OID_DOT_FORBIDDEN): forbidden<br/>
177
 *              1 (OID_DOT_OPTIONAL) : optional (default)<br />
177
 *              1 (OID_DOT_OPTIONAL) : optional (default)<br/>
178
 *              2 (OID_DOT_REQUIRED) : enforced
178
 *              2 (OID_DOT_REQUIRED) : enforced
179
 * @param   boolean $requires_whitespace_delimiters<br />
179
 * @param   boolean $requires_whitespace_delimiters<br/>
180
 *              true:  "2.999" will be recognized, as well as " 2.999 " (default)<br />
180
 *              true:  "2.999" will be recognized, as well as " 2.999 " (default)<br/>
181
 *              false: "2.999!" will be reconigzed, as well as "2.999.c" (this might be used in in documentations with templates)
181
 *              false: "2.999!" will be reconigzed, as well as "2.999.c" (this might be used in in documentations with templates)
182
 * @return  string The regular expression
182
 * @return  string The regular expression
183
 **/
183
 **/
184
function oid_detection_regex($min_len=2, $allow_leading_zeroes=false, $leading_dot_policy=OID_DOT_OPTIONAL, $requires_whitespace_delimiters=true) {
184
function oid_detection_regex($min_len=2, $allow_leading_zeroes=false, $leading_dot_policy=OID_DOT_OPTIONAL, $requires_whitespace_delimiters=true) {
185
        if ($requires_whitespace_delimiters) {
185
        if ($requires_whitespace_delimiters) {
Line 196... Line 196...
196
 
196
 
197
        return '@'.$begin_condition.$part_regex.$end_condition.'@';
197
        return '@'.$begin_condition.$part_regex.$end_condition.'@';
198
}
198
}
199
 
199
 
200
/**
200
/**
201
 * Returns the parent of an OID in dot notation or the OID itself, if it is the root.<br />
201
 * Returns the parent of an OID in dot notation or the OID itself, if it is the root.<br/>
202
 * Leading dots and leading zeroes are tolerated.
202
 * Leading dots and leading zeroes are tolerated.
203
 * @author  Daniel Marschall, ViaThinkSoft
203
 * @author  Daniel Marschall, ViaThinkSoft
204
 * @version 2014-12-16
204
 * @version 2014-12-16
205
 * @param   string $oid<br />
205
 * @param   string $oid<br/>
206
 *              An OID in dot notation.
206
 *              An OID in dot notation.
207
 * @return  string|false The parent OID in dot notation.
207
 * @return  string|false The parent OID in dot notation.
208
 **/
208
 **/
209
function oid_up($oid) {
209
function oid_up($oid) {
210
        $oid = sanitizeOID($oid, 'auto');
210
        $oid = sanitizeOID($oid, 'auto');
Line 237... Line 237...
237
 * Lists all parents of an OID.
237
 * Lists all parents of an OID.
238
 * This function tolerates leading dots. The parent of '.' stays '.'.
238
 * This function tolerates leading dots. The parent of '.' stays '.'.
239
 * The OID will not be checked for validity!
239
 * The OID will not be checked for validity!
240
 * @author  Daniel Marschall, ViaThinkSoft
240
 * @author  Daniel Marschall, ViaThinkSoft
241
 * @version 2014-12-17
241
 * @version 2014-12-17
242
 * @param   string $oid<br />
242
 * @param   string $oid<br/>
243
 *              An OID in dot notation.
243
 *              An OID in dot notation.
244
 * @return  string[] An array with all parent OIDs.
244
 * @return  string[] An array with all parent OIDs.
245
 **/
245
 **/
246
function oid_parents($oid) {
246
function oid_parents($oid) {
247
        $parents = array();
247
        $parents = array();
Line 265... Line 265...
265
 
265
 
266
/**
266
/**
267
 * Sorts an array containing OIDs in dot notation.
267
 * Sorts an array containing OIDs in dot notation.
268
 * @author  Daniel Marschall, ViaThinkSoft
268
 * @author  Daniel Marschall, ViaThinkSoft
269
 * @version 2014-12-09
269
 * @version 2014-12-09
270
 * @param   string[] $ary<br />
270
 * @param   string[] $ary<br/>
271
 *              An array of OIDs in dot notation.<br />
271
 *              An array of OIDs in dot notation.<br/>
272
 *              This array will be changed by this method.
272
 *              This array will be changed by this method.
273
 * @param   boolean $output_with_leading_dot<br />
273
 * @param   boolean $output_with_leading_dot<br/>
274
 *              true: The array will be normalized to OIDs with a leading dot.
274
 *              true: The array will be normalized to OIDs with a leading dot.
275
 *              false: The array will be normalized to OIDs without a leading dot. (default)
275
 *              false: The array will be normalized to OIDs without a leading dot. (default)
276
 **/
276
 **/
277
function oidSort(&$ary, $output_with_leading_dot=false) {
277
function oidSort(&$ary, $output_with_leading_dot=false) {
278
        $out = array();
278
        $out = array();
Line 309... Line 309...
309
 
309
 
310
/**
310
/**
311
 * Checks if two OIDs in dot-notation are equal
311
 * Checks if two OIDs in dot-notation are equal
312
 * @author  Daniel Marschall, ViaThinkSoft
312
 * @author  Daniel Marschall, ViaThinkSoft
313
 * @version 2020-05-27
313
 * @version 2020-05-27
314
 * @param   string $oidA<br />
314
 * @param   string $oidA<br/>
315
 *              First OID
315
 *              First OID
316
 * @param   string $oidB<br />
316
 * @param   string $oidB<br/>
317
 *              Second OID
317
 *              Second OID
318
 * @return  boolean|null True if the OIDs are equal, null if one of the OIDs are invalid
318
 * @return  boolean|null True if the OIDs are equal, null if one of the OIDs are invalid
319
 **/
319
 **/
320
function oid_dotnotation_equal($oidA, $oidB) {
320
function oid_dotnotation_equal($oidA, $oidB) {
321
        $oidA = sanitizeOID($oidA, false);
321
        $oidA = sanitizeOID($oidA, false);
Line 329... Line 329...
329
 
329
 
330
/**
330
/**
331
 * Removes leading zeroes from an OID in dot notation.
331
 * Removes leading zeroes from an OID in dot notation.
332
 * @author  Daniel Marschall, ViaThinkSoft
332
 * @author  Daniel Marschall, ViaThinkSoft
333
 * @version 2015-08-17
333
 * @version 2015-08-17
334
 * @param   string $oid<br />
334
 * @param   string $oid<br/>
335
 *              An OID in dot notation.
335
 *              An OID in dot notation.
336
 * @param   boolean $leading_dot<br />
336
 * @param   boolean $leading_dot<br/>
337
 *              true: The OID is valid, if it contains a leading dot.<br />
337
 *              true: The OID is valid, if it contains a leading dot.<br/>
338
 *              false (default): The OID is valid, if it does not contain a leading dot.
338
 *              false (default): The OID is valid, if it does not contain a leading dot.
339
 *              'auto: Allow both
339
 *              'auto: Allow both
340
 * @return  string|false The OID without leading dots, or <code>false</code> if the OID is syntactically wrong.
340
 * @return  string|false The OID without leading dots, or <code>false</code> if the OID is syntactically wrong.
341
 **/
341
 **/
342
$oid_sanitize_cache = array();
342
$oid_sanitize_cache = array();
Line 381... Line 381...
381
/**
381
/**
382
 * Shows the top arc of an OID.
382
 * Shows the top arc of an OID.
383
 * This function tolerates leading dots.
383
 * This function tolerates leading dots.
384
 * @author  Daniel Marschall, ViaThinkSoft
384
 * @author  Daniel Marschall, ViaThinkSoft
385
 * @version 2014-12-16
385
 * @version 2014-12-16
386
 * @param   string $oid<br />
386
 * @param   string $oid<br/>
387
 *              An OID in dot notation.
387
 *              An OID in dot notation.
388
 * @return  string|false The top arc of the OID or empty string if it is already the root ('.')
388
 * @return  string|false The top arc of the OID or empty string if it is already the root ('.')
389
 **/
389
 **/
390
function oid_toparc($oid) {
390
function oid_toparc($oid) {
391
        $leadingdot = substr($oid,0,1) == '.';
391
        $leadingdot = substr($oid,0,1) == '.';
Line 410... Line 410...
410
/**
410
/**
411
 * Calculates the distance between two OIDs.
411
 * Calculates the distance between two OIDs.
412
 * This function tolerates leading dots and leading zeroes.
412
 * This function tolerates leading dots and leading zeroes.
413
 * @author  Daniel Marschall, ViaThinkSoft
413
 * @author  Daniel Marschall, ViaThinkSoft
414
 * @version 2014-12-20
414
 * @version 2014-12-20
415
 * @param   string $a<br />
415
 * @param   string $a<br/>
416
 *              An OID.
416
 *              An OID.
417
 * @param   string $b<br />
417
 * @param   string $b<br/>
418
 *              An OID.
418
 *              An OID.
419
 * @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 />
419
 * @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/>
420
 *              >0 if $a is more specific than $b , e.g. oid_distance('2.999.1.2', '2.999') = 2<br />
420
 *              >0 if $a is more specific than $b , e.g. oid_distance('2.999.1.2', '2.999') = 2<br/>
421
 *              <0 if $a is more common than $b , e.g. oid_distance('2.999', '2.999.1.2') = -2
421
 *              <0 if $a is more common than $b , e.g. oid_distance('2.999', '2.999.1.2') = -2
422
 **/
422
 **/
423
function oid_distance($a, $b) {
423
function oid_distance($a, $b) {
424
        if (substr($a,0,1) == '.') $a = substr($a,1);
424
        if (substr($a,0,1) == '.') $a = substr($a,1);
425
        if (substr($b,0,1) == '.') $b = substr($b,1);
425
        if (substr($b,0,1) == '.') $b = substr($b,1);
Line 449... Line 449...
449
/**
449
/**
450
 * Adds a leading dot to an OID.
450
 * Adds a leading dot to an OID.
451
 * Leading zeroes are tolerated.
451
 * Leading zeroes are tolerated.
452
 * @author  Daniel Marschall, ViaThinkSoft
452
 * @author  Daniel Marschall, ViaThinkSoft
453
 * @version 2014-12-20
453
 * @version 2014-12-20
454
 * @param   string $oid<br />
454
 * @param   string $oid<br/>
455
 *              An OID.
455
 *              An OID.
456
 * @return  string|false The OID with a leading dot or false if the OID is syntactially wrong.
456
 * @return  string|false The OID with a leading dot or false if the OID is syntactially wrong.
457
 **/
457
 **/
458
function oid_add_leading_dot($oid) {
458
function oid_add_leading_dot($oid) {
459
        $oid = sanitizeOID($oid, 'auto');
459
        $oid = sanitizeOID($oid, 'auto');
Line 466... Line 466...
466
/**
466
/**
467
 * Removes a leading dot to an OID.
467
 * Removes a leading dot to an OID.
468
 * Leading zeroes are tolerated.
468
 * Leading zeroes are tolerated.
469
 * @author  Daniel Marschall, ViaThinkSoft
469
 * @author  Daniel Marschall, ViaThinkSoft
470
 * @version 2014-12-20
470
 * @version 2014-12-20
471
 * @param   string $oid<br />
471
 * @param   string $oid<br/>
472
 *              An OID.
472
 *              An OID.
473
 * @return  string|false The OID without a leading dot or false if the OID is syntactially wrong.
473
 * @return  string|false The OID without a leading dot or false if the OID is syntactially wrong.
474
 **/
474
 **/
475
function oid_remove_leading_dot($oid) {
475
function oid_remove_leading_dot($oid) {
476
        $oid = sanitizeOID($oid, 'auto');
476
        $oid = sanitizeOID($oid, 'auto');
Line 482... Line 482...
482
 
482
 
483
/**
483
/**
484
 * Find the common ancestor of two or more OIDs
484
 * Find the common ancestor of two or more OIDs
485
 * @author  Daniel Marschall, ViaThinkSoft
485
 * @author  Daniel Marschall, ViaThinkSoft
486
 * @version 2020-05-27
486
 * @version 2020-05-27
487
 * @param   string[] $oids<br />
487
 * @param   string[] $oids<br/>
488
 *              An array of multiple OIDs, e.g. 2.999.1 and 2.999.2.3.4
488
 *              An array of multiple OIDs, e.g. 2.999.1 and 2.999.2.3.4
489
 * @return  string|false The common ancestor, e.g. 2.999, or false if there is no common ancestor.
489
 * @return  string|false The common ancestor, e.g. 2.999, or false if there is no common ancestor.
490
 **/
490
 **/
491
function oid_common_ancestor(array $oids) {
491
function oid_common_ancestor(array $oids) {
492
        $shared = array();
492
        $shared = array();
Line 599... Line 599...
599
 
599
 
600
/**
600
/**
601
 * Checks if an IRI identifier is valid or not.
601
 * Checks if an IRI identifier is valid or not.
602
 * @author  Daniel Marschall, ViaThinkSoft
602
 * @author  Daniel Marschall, ViaThinkSoft
603
 * @version 2014-12-17
603
 * @version 2014-12-17
604
 * @param   string $iri<br />
604
 * @param   string $iri<br/>
605
 *              An OID in OID-IRI notation, e.g. /Example/test
605
 *              An OID in OID-IRI notation, e.g. /Example/test
606
 * @return  boolean true if the IRI identifier is valid.
606
 * @return  boolean true if the IRI identifier is valid.
607
 **/
607
 **/
608
function iri_valid($iri) {
608
function iri_valid($iri) {
609
        if ($iri == '/') return true; // OK?
609
        if ($iri == '/') return true; // OK?
Line 670... Line 670...
670
 
670
 
671
/**
671
/**
672
 * Tries to shorten/simplify an IRI by applying "long arcs", e.g. /2/999/123 -> /Example/123 .
672
 * Tries to shorten/simplify an IRI by applying "long arcs", e.g. /2/999/123 -> /Example/123 .
673
 * @author  Daniel Marschall, ViaThinkSoft
673
 * @author  Daniel Marschall, ViaThinkSoft
674
 * @version 2020-05-22
674
 * @version 2020-05-22
675
 * @param   string $iri<br />
675
 * @param   string $iri<br/>
676
 *              An OID in OID-IRI notation, e.g. /Example/test
676
 *              An OID in OID-IRI notation, e.g. /Example/test
677
 * @return  string|false The modified IRI.
677
 * @return  string|false The modified IRI.
678
 **/
678
 **/
679
function iri_add_longarcs($iri) {
679
function iri_add_longarcs($iri) {
680
        $iri_long_arcs = iri_get_long_arcs();
680
        $iri_long_arcs = iri_get_long_arcs();
Line 712... Line 712...
712
 
712
 
713
/**
713
/**
714
 * Checks if an ASN.1 identifier is valid.
714
 * Checks if an ASN.1 identifier is valid.
715
 * @author  Daniel Marschall, ViaThinkSoft
715
 * @author  Daniel Marschall, ViaThinkSoft
716
 * @version 2020-05-22
716
 * @version 2020-05-22
717
 * @param   string $id<br />
717
 * @param   string $id<br/>
718
 *              An ASN.1 identifier, e.g. "example". Not "example(99)" or "99" and not a path like "{ 2 999 }"
718
 *              An ASN.1 identifier, e.g. "example". Not "example(99)" or "99" and not a path like "{ 2 999 }"
719
 *              Note: Use asn1_path_valid() for validating a whole ASN.1 notation path.
719
 *              Note: Use asn1_path_valid() for validating a whole ASN.1 notation path.
720
 * @return  boolean true, if the identifier is valid: It begins with an lowercase letter and contains only 0-9, a-z, A-Z and "-"
720
 * @return  boolean true, if the identifier is valid: It begins with an lowercase letter and contains only 0-9, a-z, A-Z and "-"
721
 **/
721
 **/
722
function oid_id_is_valid($id) {
722
function oid_id_is_valid($id) {
Line 731... Line 731...
731
 * Checks if the ASN.1 notation of an OID is valid.
731
 * Checks if the ASN.1 notation of an OID is valid.
732
 * This function does not tolerate leading zeros.
732
 * This function does not tolerate leading zeros.
733
 * This function will fail (return false) if there are unresolved symbols, e.g. {iso test} is not valid while { iso 123 } is valid.
733
 * This function will fail (return false) if there are unresolved symbols, e.g. {iso test} is not valid while { iso 123 } is valid.
734
 * @author  Daniel Marschall, ViaThinkSoft
734
 * @author  Daniel Marschall, ViaThinkSoft
735
 * @version 2014-12-17
735
 * @version 2014-12-17
736
 * @param   string $asn1<br />
736
 * @param   string $asn1<br/>
737
 *              An OID in ASN.1 notation.
737
 *              An OID in ASN.1 notation.
738
 * @return  boolean true if the identifier is valid.
738
 * @return  boolean true if the identifier is valid.
739
 **/
739
 **/
740
function asn1_path_valid($asn1) {
740
function asn1_path_valid($asn1) {
741
        return asn1_to_dot($asn1) != false;
741
        return asn1_to_dot($asn1) != false;
Line 796... Line 796...
796
        $standardized['1.identified-organization'] = '1.3';
796
        $standardized['1.identified-organization'] = '1.3';
797
        return $standardized;
797
        return $standardized;
798
}
798
}
799
 
799
 
800
/**
800
/**
801
 * Converts an OID in ASN.1 notation into an OID in dot notation and tries to resolve well-known identifiers.<br />
801
 * Converts an OID in ASN.1 notation into an OID in dot notation and tries to resolve well-known identifiers.<br/>
802
 * e.g. {joint-iso-itu-t(2) example(999) 1 2 3} --> 2.999.1.2.3<br />
802
 * e.g. {joint-iso-itu-t(2) example(999) 1 2 3} --> 2.999.1.2.3<br/>
803
 * e.g. {iso 3} --> 1.3
803
 * e.g. {iso 3} --> 1.3
804
 * This function does not tolerate leading zeros.
804
 * This function does not tolerate leading zeros.
805
 * This function will fail (return false) if there are unresolved symbols, e.g. {iso test} will not be resolved to 1.test
805
 * This function will fail (return false) if there are unresolved symbols, e.g. {iso test} will not be resolved to 1.test
806
 * @author  Daniel Marschall, ViaThinkSoft
806
 * @author  Daniel Marschall, ViaThinkSoft
807
 * @version 2014-12-17
807
 * @version 2014-12-17
808
 * @param   string $asn<br />
808
 * @param   string $asn<br/>
809
 *              An OID in ASN.1 notation.
809
 *              An OID in ASN.1 notation.
810
 * @return  string|false An OID in dot notation without leading dot or false if the path is invalid.
810
 * @return  string|false An OID in dot notation without leading dot or false if the path is invalid.
811
 **/
811
 **/
812
function asn1_to_dot($asn) {
812
function asn1_to_dot($asn) {
813
        $standardized = asn1_get_standardized_array();
813
        $standardized = asn1_get_standardized_array();
Line 860... Line 860...
860
 
860
 
861
/**
861
/**
862
 * Gets the last numeric identifier of an ASN.1 notation OID.
862
 * Gets the last numeric identifier of an ASN.1 notation OID.
863
 * @author  Daniel Marschall, ViaThinkSoft
863
 * @author  Daniel Marschall, ViaThinkSoft
864
 * @version 2020-06-11
864
 * @version 2020-06-11
865
 * @param   string $asn1id<br />
865
 * @param   string $asn1id<br/>
866
 *              An ASN.1 identifier string, e.g. { 2 example(999) test(1) }
866
 *              An ASN.1 identifier string, e.g. { 2 example(999) test(1) }
867
 * @return  int|false The last numeric identifier arc, e.g. "1" or false if the ID is invalid
867
 * @return  int|false The last numeric identifier arc, e.g. "1" or false if the ID is invalid
868
 **/
868
 **/
869
function asn1_last_identifier($asn1id) {
869
function asn1_last_identifier($asn1id) {
870
        $asn1id = preg_replace('@\(\s*\d+\s*\)@', '', $asn1id);
870
        $asn1id = preg_replace('@\(\s*\d+\s*\)@', '', $asn1id);
Line 873... Line 873...
873
        $asn1id = $ary[count($ary)-1];
873
        $asn1id = $ary[count($ary)-1];
874
        return preg_match('#[^0-9]#',$asn1id) ? (int)$asn1id : false;
874
        return preg_match('#[^0-9]#',$asn1id) ? (int)$asn1id : false;
875
}
875
}
876
 
876
 
877
/**
877
/**
878
 * "Soft corrects" an invalid ASN.1 identifier.<br />
878
 * "Soft corrects" an invalid ASN.1 identifier.<br/>
879
 * Attention, by "soft correcting" the ID, it is not authoritative anymore, and might not be able to be resolved by ORS.
879
 * Attention, by "soft correcting" the ID, it is not authoritative anymore, and might not be able to be resolved by ORS.
880
 * @author  Daniel Marschall, ViaThinkSoft
880
 * @author  Daniel Marschall, ViaThinkSoft
881
 * @version 2020-05-22
881
 * @version 2020-05-22
882
 * @param   string $id<br />
882
 * @param   string $id<br/>
883
 *              An ASN.1 identifier.
883
 *              An ASN.1 identifier.
884
 * @param   boolean $append_id_prefix<br />
884
 * @param   boolean $append_id_prefix<br/>
885
 *              true (default): If the identifier doesn't start with a-Z, the problem will be solved by prepending "id-" to the identifier.<br />
885
 *              true (default): If the identifier doesn't start with a-Z, the problem will be solved by prepending "id-" to the identifier.<br/>
886
 *              false: If the identifier doesn't start with a-Z, then the problem cannot be solved (method returns empty string).
886
 *              false: If the identifier doesn't start with a-Z, then the problem cannot be solved (method returns empty string).
887
 * @return  string The "soft corrected" ASN.1 identifier.<br />
887
 * @return  string The "soft corrected" ASN.1 identifier.<br/>
888
 *              Invalid characters will be removed.<br />
888
 *              Invalid characters will be removed.<br/>
889
 *              Uncorrectable start elements (0-9 or "-") will be either removed or solved by prepending "id-" (see <code>$append_id_prefix</code>)<br />
889
 *              Uncorrectable start elements (0-9 or "-") will be either removed or solved by prepending "id-" (see <code>$append_id_prefix</code>)<br/>
890
 *              If the identifier begins with an upper case letter, the letter will be converted into lower case.
890
 *              If the identifier begins with an upper case letter, the letter will be converted into lower case.
891
 **/
891
 **/
892
function oid_soft_correct_id($id, $append_id_prefix = true) {
892
function oid_soft_correct_id($id, $append_id_prefix = true) {
893
        // Convert "_" to "-"
893
        // Convert "_" to "-"
894
        $id = str_replace('_', '-', $id);
894
        $id = str_replace('_', '-', $id);