Subversion Repositories oidinfo_api

Rev

Rev 15 | Rev 17 | Go to most recent revision | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 15 Rev 16
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-2020 Daniel Marschall, ViaThinkSoft
5
 * Copyright 2011-2021 Daniel Marschall, ViaThinkSoft
6
 * Version 2020-09-12
6
 * Version 2021-05-21
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   $oid (string)<br />
34
 * @param   string $oid<br />
35
 *              An OID in dot notation.
35
 *              An OID in dot notation.
36
 * @param   $allow_leading_zeroes (bool)<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   $allow_leading_dot (bool)<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  (bool) 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);
44
 
44
 
45
        $m = array();
45
        $m = array();
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   $allow_leading_zeroes (bool)<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   $allow_leading_dot (bool)<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;
61
 
61
 
62
        $part_regex = oid_part_regex($min_len, $allow_leading_zeroes, $leading_dot_policy);
62
        $part_regex = oid_part_regex($min_len, $allow_leading_zeroes, $leading_dot_policy);
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   $min_len (int)<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   $allow_leading_zeroes (bool)<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   $leading_dot_policy (int)<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) 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) {
88
                case 0: // forbidden
88
                case 0: // forbidden
89
                        $lead_dot = '';
89
                        $lead_dot = '';
Line 94... Line 94...
94
                case 2: // enforced
94
                case 2: // enforced
95
                        $lead_dot = '\\.';
95
                        $lead_dot = '\\.';
96
                        break;
96
                        break;
97
                default:
97
                default:
98
                        assert(false);
98
                        assert(false);
99
                        break;
99
                        return false;
100
        }
100
        }
101
 
101
 
102
        $lead_zero            = $allow_leading_zeroes ? '0*' : '';
102
        $lead_zero            = $allow_leading_zeroes ? '0*' : '';
103
        $zero_till_thirtynine = '(([0-9])|([1-3][0-9]))'; // second arc is limited to 0..39 if root arc is 0..1
103
        $zero_till_thirtynine = '(([0-9])|([1-3][0-9]))'; // second arc is limited to 0..39 if root arc is 0..1
104
        $singledot_option     = ($min_len == 0) && ($leading_dot_policy != OID_DOT_FORBIDDEN) ? '|\\.' : '';
104
        $singledot_option     = ($min_len == 0) && ($leading_dot_policy != OID_DOT_FORBIDDEN) ? '|\\.' : '';
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   $text (string)<br />
136
 * @param   string $text<br />
137
 *              The text to be parsed
137
 *              The text to be parsed
138
 * @param   $min_len (int)<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   $allow_leading_zeroes (bool)<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   $leading_dot_policy (int)<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   $requires_whitespace_delimiters (bool)<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  (array<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);
157
 
157
 
158
        $matches = array();
158
        $matches = array();
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   $min_len (int)<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   $allow_leading_zeroes (bool)<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   $leading_dot_policy (int)<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   $requires_whitespace_delimiters (bool)<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) {
186
                // A fully qualified regular expression which can be used by preg_match()
186
                // A fully qualified regular expression which can be used by preg_match()
187
                $begin_condition = '(?<=^|\\s)';
187
                $begin_condition = '(?<=^|\\s)';
Line 200... Line 200...
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   $oid (string)<br />
205
 * @param   string $oid<br />
206
 *              An OID in dot notation.
206
 *              An OID in dot notation.
207
 * @return  (string) 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');
211
        if ($oid === false) return false;
211
        if ($oid === false) return false;
212
 
212
 
Line 219... Line 219...
219
 
219
 
220
/**
220
/**
221
 * Outputs the depth of an OID.
221
 * Outputs the depth of an OID.
222
 * @author  Daniel Marschall, ViaThinkSoft
222
 * @author  Daniel Marschall, ViaThinkSoft
223
 * @version 2014-12-09
223
 * @version 2014-12-09
224
 * @param   $oid (string) An OID in dot notation (with or without leading dot)
224
 * @param   string $oid An OID in dot notation (with or without leading dot)
225
 * @return  (int) The depth of the OID, e.g. 2.999 and .2.999 has the length 2.
225
 * @return  (int) The depth of the OID, e.g. 2.999 and .2.999 has the length 2.
226
 **/
226
 **/
227
function oid_len($oid) {
227
function oid_len($oid) {
228
        if ($oid == '') return 0;
228
        if ($oid == '') return 0;
229
        if ($oid[0] == '.') $oid = substr($oid, 1);
229
        if ($oid[0] == '.') $oid = substr($oid, 1);
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   $oid (string)<br />
242
 * @param   string $oid<br />
243
 *              An OID in dot notation.
243
 *              An OID in dot notation.
244
 * @return  (array<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();
248
 
248
 
249
        while (oid_len($oid) > 1) {
249
        while (oid_len($oid) > 1) {
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   $ary (array<string>)<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   $output_with_leading_dot (bool)<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();
279
 
279
 
280
        $none = $output_with_leading_dot ? '.' : '';
280
        $none = $output_with_leading_dot ? '.' : '';
281
 
281
 
282
        $d = array();
282
        $d = array();
-
 
283
        $oid = null;
283
        foreach ($ary as &$oid) {
284
        foreach ($ary as &$oid) {
284
                if (($oid == '') || ($oid == '.')) {
285
                if (($oid == '') || ($oid == '.')) {
285
                        $out[] = $none;
286
                        $out[] = $none;
286
                } else {
287
                } else {
287
                        $oid = sanitizeOID($oid, 'auto'); // strike leading zeroes
288
                        $oid = sanitizeOID($oid, 'auto'); // strike leading zeroes
Line 292... Line 293...
292
                }
293
                }
293
        }
294
        }
294
        unset($oid);
295
        unset($oid);
295
        ksort($d);
296
        ksort($d);
296
 
297
 
-
 
298
        $data = null;
297
        foreach ($d as $firstarc => &$data) {
299
        foreach ($d as $firstarc => &$data) {
298
                oidSort($data);
300
                oidSort($data);
299
                foreach ($data as &$rest) {
301
                foreach ($data as &$rest) {
300
                        $out[] = ($output_with_leading_dot ? '.' : '')."$firstarc" . (($rest != $none) ? ".$rest" : '');
302
                        $out[] = ($output_with_leading_dot ? '.' : '')."$firstarc" . (($rest != $none) ? ".$rest" : '');
301
                }
303
                }
Line 307... Line 309...
307
 
309
 
308
/**
310
/**
309
 * Checks if two OIDs in dot-notation are equal
311
 * Checks if two OIDs in dot-notation are equal
310
 * @author  Daniel Marschall, ViaThinkSoft
312
 * @author  Daniel Marschall, ViaThinkSoft
311
 * @version 2020-05-27
313
 * @version 2020-05-27
312
 * @param   $oidA (string)<br />
314
 * @param   string $oidA<br />
313
 *              First OID
315
 *              First OID
314
 * @param   $oidB (string)<br />
316
 * @param   string $oidB<br />
315
 *              Second OID
317
 *              Second OID
316
 * @return  (bool) True if the OIDs are equal
318
 * @return  boolean|null True if the OIDs are equal, null if one of the OIDs are invalid
317
 **/
319
 **/
318
function oid_dotnotation_equal($oidA, $oidB) {
320
function oid_dotnotation_equal($oidA, $oidB) {
319
        $oidA = sanitizeOID($oidA, false);
321
        $oidA = sanitizeOID($oidA, false);
320
        if ($oidA === false) return null;
322
        if ($oidA === false) return null;
321
 
323
 
Line 327... Line 329...
327
 
329
 
328
/**
330
/**
329
 * Removes leading zeroes from an OID in dot notation.
331
 * Removes leading zeroes from an OID in dot notation.
330
 * @author  Daniel Marschall, ViaThinkSoft
332
 * @author  Daniel Marschall, ViaThinkSoft
331
 * @version 2015-08-17
333
 * @version 2015-08-17
332
 * @param   $oid (string)<br />
334
 * @param   string $oid<br />
333
 *              An OID in dot notation.
335
 *              An OID in dot notation.
334
 * @param   $leading_dot (bool)<br />
336
 * @param   boolean $leading_dot<br />
335
 *              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 />
336
 *              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.
337
 *              'auto: Allow both
339
 *              'auto: Allow both
338
 * @return  (mixed) 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.
339
 **/
341
 **/
340
$oid_sanitize_cache = array();
342
$oid_sanitize_cache = array();
341
function sanitizeOID($oid, $leading_dot=false) {
343
function sanitizeOID($oid, $leading_dot=false) {
342
        if ($leading_dot) $leading_dot = substr($oid,0,1) == '.';
344
        if ($leading_dot) $leading_dot = substr($oid,0,1) == '.';
343
 
345
 
Line 379... Line 381...
379
/**
381
/**
380
 * Shows the top arc of an OID.
382
 * Shows the top arc of an OID.
381
 * This function tolerates leading dots.
383
 * This function tolerates leading dots.
382
 * @author  Daniel Marschall, ViaThinkSoft
384
 * @author  Daniel Marschall, ViaThinkSoft
383
 * @version 2014-12-16
385
 * @version 2014-12-16
384
 * @param   $oid (string)<br />
386
 * @param   string $oid<br />
385
 *              An OID in dot notation.
387
 *              An OID in dot notation.
386
 * @return  (mixed) 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 ('.')
387
 **/
389
 **/
388
function oid_toparc($oid) {
390
function oid_toparc($oid) {
389
        $leadingdot = substr($oid,0,1) == '.';
391
        $leadingdot = substr($oid,0,1) == '.';
390
 
392
 
391
        $oid = sanitizeOID($oid, $leadingdot);
393
        $oid = sanitizeOID($oid, $leadingdot);
Line 408... Line 410...
408
/**
410
/**
409
 * Calculates the distance between two OIDs.
411
 * Calculates the distance between two OIDs.
410
 * This function tolerates leading dots and leading zeroes.
412
 * This function tolerates leading dots and leading zeroes.
411
 * @author  Daniel Marschall, ViaThinkSoft
413
 * @author  Daniel Marschall, ViaThinkSoft
412
 * @version 2014-12-20
414
 * @version 2014-12-20
413
 * @param   $a (string)<br />
415
 * @param   string $a<br />
414
 *              An OID.
416
 *              An OID.
415
 * @param   $b (string)<br />
417
 * @param   string $b<br />
416
 *              An OID.
418
 *              An OID.
417
 * @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 />
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 />
418
 *              >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 />
419
 *              <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
420
 **/
422
 **/
421
function oid_distance($a, $b) {
423
function oid_distance($a, $b) {
422
        if (substr($a,0,1) == '.') $a = substr($a,1);
424
        if (substr($a,0,1) == '.') $a = substr($a,1);
Line 447... Line 449...
447
/**
449
/**
448
 * Adds a leading dot to an OID.
450
 * Adds a leading dot to an OID.
449
 * Leading zeroes are tolerated.
451
 * Leading zeroes are tolerated.
450
 * @author  Daniel Marschall, ViaThinkSoft
452
 * @author  Daniel Marschall, ViaThinkSoft
451
 * @version 2014-12-20
453
 * @version 2014-12-20
452
 * @param   $oid (string)<br />
454
 * @param   string $oid<br />
453
 *              An OID.
455
 *              An OID.
454
 * @return  (string) 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.
455
 **/
457
 **/
456
function oid_add_leading_dot($oid) {
458
function oid_add_leading_dot($oid) {
457
        $oid = sanitizeOID($oid, 'auto');
459
        $oid = sanitizeOID($oid, 'auto');
458
        if ($oid === false) return false;
460
        if ($oid === false) return false;
459
 
461
 
Line 464... Line 466...
464
/**
466
/**
465
 * Removes a leading dot to an OID.
467
 * Removes a leading dot to an OID.
466
 * Leading zeroes are tolerated.
468
 * Leading zeroes are tolerated.
467
 * @author  Daniel Marschall, ViaThinkSoft
469
 * @author  Daniel Marschall, ViaThinkSoft
468
 * @version 2014-12-20
470
 * @version 2014-12-20
469
 * @param   $oid (string)<br />
471
 * @param   string $oid<br />
470
 *              An OID.
472
 *              An OID.
471
 * @return  (string) 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.
472
 **/
474
 **/
473
function oid_remove_leading_dot($oid) {
475
function oid_remove_leading_dot($oid) {
474
        $oid = sanitizeOID($oid, 'auto');
476
        $oid = sanitizeOID($oid, 'auto');
475
        if ($oid === false) return false;
477
        if ($oid === false) return false;
476
 
478
 
Line 480... Line 482...
480
 
482
 
481
/**
483
/**
482
 * Find the common ancestor of two or more OIDs
484
 * Find the common ancestor of two or more OIDs
483
 * @author  Daniel Marschall, ViaThinkSoft
485
 * @author  Daniel Marschall, ViaThinkSoft
484
 * @version 2020-05-27
486
 * @version 2020-05-27
485
 * @param   $oids (array)<br />
487
 * @param   string[] $oids<br />
486
 *              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
487
 * @return  (mixed) 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.
488
 **/
490
 **/
489
function oid_common_ancestor(array $oids) {
491
function oid_common_ancestor(array $oids) {
490
        $shared = array();
492
        $shared = array();
491
 
493
 
492
        if (!is_array($oids)) return false;
494
        if (!is_array($oids)) return false;
Line 496... Line 498...
496
                $oid = sanitizeOID($oid, false);
498
                $oid = sanitizeOID($oid, false);
497
                if ($oid === false) return false;
499
                if ($oid === false) return false;
498
                $oid = explode('.', $oid);
500
                $oid = explode('.', $oid);
499
        }
501
        }
500
 
502
 
501
        $max_ok = count($oids[0]);
503
        $max_ok = strlen($oids[0]);
502
        for ($i=1; $i<count($oids); $i++) {
504
        for ($i=1; $i<count($oids); $i++) {
503
                for ($j=0; $j<min(count($oids[$i]),count($oids[0])); $j++) {
505
                for ($j=0; $j<min(strlen($oids[$i]),strlen($oids[0])); $j++) {
504
                        if ($oids[$i][$j] != $oids[0][$j]) {
506
                        if ($oids[$i][$j] != $oids[0][$j]) {
505
                                if ($j < $max_ok) $max_ok = $j;
507
                                if ($j < $max_ok) $max_ok = $j;
506
                                break;
508
                                break;
507
                        }
509
                        }
508
                }
510
                }
Line 597... Line 599...
597
 
599
 
598
/**
600
/**
599
 * Checks if an IRI identifier is valid or not.
601
 * Checks if an IRI identifier is valid or not.
600
 * @author  Daniel Marschall, ViaThinkSoft
602
 * @author  Daniel Marschall, ViaThinkSoft
601
 * @version 2014-12-17
603
 * @version 2014-12-17
602
 * @param   $iri (string)<br />
604
 * @param   string $iri<br />
603
 *              An OID in OID-IRI notation, e.g. /Example/test
605
 *              An OID in OID-IRI notation, e.g. /Example/test
604
 * @return  (bool) true if the IRI identifier is valid.
606
 * @return  boolean true if the IRI identifier is valid.
605
 **/
607
 **/
606
function iri_valid($iri) {
608
function iri_valid($iri) {
607
        if ($iri == '/') return true; // OK?
609
        if ($iri == '/') return true; // OK?
608
 
610
 
609
        if (substr($iri, 0, 1) != '/') return false;
611
        if (substr($iri, 0, 1) != '/') return false;
Line 668... Line 670...
668
 
670
 
669
/**
671
/**
670
 * 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 .
671
 * @author  Daniel Marschall, ViaThinkSoft
673
 * @author  Daniel Marschall, ViaThinkSoft
672
 * @version 2020-05-22
674
 * @version 2020-05-22
673
 * @param   $iri (string)<br />
675
 * @param   string $iri<br />
674
 *              An OID in OID-IRI notation, e.g. /Example/test
676
 *              An OID in OID-IRI notation, e.g. /Example/test
675
 * @return  (string) The modified IRI.
677
 * @return  string|false The modified IRI.
676
 **/
678
 **/
677
function iri_add_longarcs($iri) {
679
function iri_add_longarcs($iri) {
678
        $iri_long_arcs = iri_get_long_arcs();
680
        $iri_long_arcs = iri_get_long_arcs();
679
 
681
 
680
        if (!iri_valid($iri)) return false;
682
        if (!iri_valid($iri)) return false;
Line 710... Line 712...
710
 
712
 
711
/**
713
/**
712
 * Checks if an ASN.1 identifier is valid.
714
 * Checks if an ASN.1 identifier is valid.
713
 * @author  Daniel Marschall, ViaThinkSoft
715
 * @author  Daniel Marschall, ViaThinkSoft
714
 * @version 2020-05-22
716
 * @version 2020-05-22
715
 * @param   $id (string)<br />
717
 * @param   string $id<br />
716
 *              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 }"
717
 *              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.
718
 * @return  (bool) 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 "-"
719
 **/
721
 **/
720
function oid_id_is_valid($id) {
722
function oid_id_is_valid($id) {
721
        // see Rec. ITU-T X.660 | ISO/IEC 9834-1, clause 7.7
723
        // see Rec. ITU-T X.660 | ISO/IEC 9834-1, clause 7.7
722
        // and Rec. ITU-T X.680 | ISO/IEC 8824-1, clause 12.3
724
        // and Rec. ITU-T X.680 | ISO/IEC 8824-1, clause 12.3
723
        if (substr($id,-1,1) == '-') return false;
725
        if (substr($id,-1,1) == '-') return false;
Line 729... Line 731...
729
 * Checks if the ASN.1 notation of an OID is valid.
731
 * Checks if the ASN.1 notation of an OID is valid.
730
 * This function does not tolerate leading zeros.
732
 * This function does not tolerate leading zeros.
731
 * 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.
732
 * @author  Daniel Marschall, ViaThinkSoft
734
 * @author  Daniel Marschall, ViaThinkSoft
733
 * @version 2014-12-17
735
 * @version 2014-12-17
734
 * @param   $asn (string)<br />
736
 * @param   string $asn1<br />
735
 *              An OID in ASN.1 notation.
737
 *              An OID in ASN.1 notation.
736
 * @return  (bools) true if the identifier is valid.
738
 * @return  boolean true if the identifier is valid.
737
 **/
739
 **/
738
function asn1_path_valid($asn1) {
740
function asn1_path_valid($asn1) {
739
        return asn1_to_dot($asn1) != false;
741
        return asn1_to_dot($asn1) != false;
740
}
742
}
741
 
743
 
Line 801... Line 803...
801
 * e.g. {iso 3} --> 1.3
803
 * e.g. {iso 3} --> 1.3
802
 * This function does not tolerate leading zeros.
804
 * This function does not tolerate leading zeros.
803
 * 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
804
 * @author  Daniel Marschall, ViaThinkSoft
806
 * @author  Daniel Marschall, ViaThinkSoft
805
 * @version 2014-12-17
807
 * @version 2014-12-17
806
 * @param   $asn (string)<br />
808
 * @param   string $asn<br />
807
 *              An OID in ASN.1 notation.
809
 *              An OID in ASN.1 notation.
808
 * @return  (string) 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.
809
 **/
811
 **/
810
function asn1_to_dot($asn) {
812
function asn1_to_dot($asn) {
811
        $standardized = asn1_get_standardized_array();
813
        $standardized = asn1_get_standardized_array();
812
 
814
 
813
        // Clean up
815
        // Clean up
Line 858... Line 860...
858
 
860
 
859
/**
861
/**
860
 * Gets the last numeric identifier of an ASN.1 notation OID.
862
 * Gets the last numeric identifier of an ASN.1 notation OID.
861
 * @author  Daniel Marschall, ViaThinkSoft
863
 * @author  Daniel Marschall, ViaThinkSoft
862
 * @version 2020-06-11
864
 * @version 2020-06-11
863
 * @param   $asn1id (string)<br />
865
 * @param   string $asn1id<br />
864
 *              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) }
865
 * @return  (int) The last numeric identifier arc, e.g. "1"
867
 * @return  int|false The last numeric identifier arc, e.g. "1" or false if the ID is invalid
866
 **/
868
 **/
867
function asn1_last_identifier($asn1id) {
869
function asn1_last_identifier($asn1id) {
868
        $asn1id = preg_replace('@\(\s*\d+\s*\)@', '', $asn1id);
870
        $asn1id = preg_replace('@\(\s*\d+\s*\)@', '', $asn1id);
869
        $asn1id = trim(str_replace(array('{', '}', "\t"), ' ', $asn1id));
871
        $asn1id = trim(str_replace(array('{', '}', "\t"), ' ', $asn1id));
870
        $ary = explode(' ', $asn1id);
872
        $ary = explode(' ', $asn1id);
871
        $asn1id = $ary[count($ary)-1];
873
        $asn1id = $ary[count($ary)-1];
872
        return preg_match('#[^0-9]#',$asn1id) ? $asn1id : false;
874
        return preg_match('#[^0-9]#',$asn1id) ? (int)$asn1id : false;
873
}
875
}
874
 
876
 
875
/**
877
/**
876
 * "Soft corrects" an invalid ASN.1 identifier.<br />
878
 * "Soft corrects" an invalid ASN.1 identifier.<br />
877
 * 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.
878
 * @author  Daniel Marschall, ViaThinkSoft
880
 * @author  Daniel Marschall, ViaThinkSoft
879
 * @version 2020-05-22
881
 * @version 2020-05-22
880
 * @param   $id (string)<br />
882
 * @param   string $id<br />
881
 *              An ASN.1 identifier.
883
 *              An ASN.1 identifier.
882
 * @param   $append_id_prefix (bool)<br />
884
 * @param   boolean $append_id_prefix<br />
883
 *              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 />
884
 *              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).
885
 * @return  (string) The "soft corrected" ASN.1 identifier.<br />
887
 * @return  string The "soft corrected" ASN.1 identifier.<br />
886
 *              Invalid characters will be removed.<br />
888
 *              Invalid characters will be removed.<br />
887
 *              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 />
888
 *              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.
889
 **/
891
 **/
890
function oid_soft_correct_id($id, $append_id_prefix = true) {
892
function oid_soft_correct_id($id, $append_id_prefix = true) {