Subversion Repositories oidplus

Rev

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

Rev 1050 Rev 1116
Line 1... Line 1...
1
<?php
1
<?php
2
 
2
 
3
/**
3
/**
4
 * WEID<=>OID Converter
4
 * WEID<=>OID Converter
5
 * (c) Webfan.de, ViaThinkSoft
5
 * (c) Webfan.de, ViaThinkSoft
6
 * Revision 2022-03-06
6
 * Revision 2023-03-22
7
 **/
7
 **/
8
 
8
 
9
// What is a WEID?
9
// What is a WEID?
10
//     A WEID (WEhowski IDentifier) is an alternative representation of an
10
//     A WEID (WEhowski IDentifier) is an alternative representation of an
11
//     OID (Object IDentifier) defined by Till Wehowski.
11
//     OID (Object IDentifier) defined by Till Wehowski.
Line 22... Line 22...
22
//                            oid:2.999
22
//                            oid:2.999
23
//     - The namespace (weid:, weid:pen:, weid:root:) is now case insensitive.
23
//     - The namespace (weid:, weid:pen:, weid:root:) is now case insensitive.
24
//     - Padding with '0' characters is valid (e.g. weid:000EXAMPLE-3)
24
//     - Padding with '0' characters is valid (e.g. weid:000EXAMPLE-3)
25
//       The paddings do not count into the WeLuhn check-digit.
25
//       The paddings do not count into the WeLuhn check-digit.
26
 
26
 
27
namespace Frdl\Weid; // TODO: Namespace mit Till abklären
27
namespace Frdl\Weid;
28
 
28
 
29
class WeidOidConverter {
29
class WeidOidConverter {
30
 
30
 
-
 
31
        /**
-
 
32
         * @param string $str
-
 
33
         * @return false|int
-
 
34
         */
31
        protected static function weLuhnGetCheckDigit($str) {
35
        protected static function weLuhnGetCheckDigit(string $str) {
32
                // Padding zeros don't count to the check digit (December 2021)
36
                // Padding zeros don't count to the check digit (December 2021)
33
                $ary = explode('-', $str);
37
                $ary = explode('-', $str);
34
                foreach ($ary as &$a) {
38
                foreach ($ary as &$a) {
35
                        $a = ltrim($a, '0');
39
                        $a = ltrim($a, '0');
36
                        if ($a === '') $a = '0';
40
                        if ($a === '') $a = '0';
Line 61... Line 65...
61
                        $sum += $digit;
65
                        $sum += $digit;
62
                }
66
                }
63
                return ($sum%10) == 0 ? 0 : 10-($sum%10);
67
                return ($sum%10) == 0 ? 0 : 10-($sum%10);
64
        }
68
        }
65
 
69
 
-
 
70
        /**
-
 
71
         * @param string $oid
-
 
72
         * @return false|string
-
 
73
         */
66
        private static function oidSanitize($oid) {
74
        private static function oidSanitize(string $oid) {
67
                $oid = trim($oid);
75
                $oid = trim($oid);
68
 
76
 
69
                if (substr($oid,0,1) == '.') $oid = substr($oid,1); // remove leading dot
77
                if (substr($oid,0,1) == '.') $oid = substr($oid,1); // remove leading dot
70
 
78
 
71
                if ($oid != '') {
79
                if ($oid != '') {
Line 76... Line 84...
76
                                if (!preg_match('/^\d+$/', $elem, $m)) return false;
84
                                if (!preg_match('/^\d+$/', $elem, $m)) return false;
77
 
85
 
78
                                if (preg_match('/^0+$/', $elem, $m)) {
86
                                if (preg_match('/^0+$/', $elem, $m)) {
79
                                        $elem = '0';
87
                                        $elem = '0';
80
                                } else {
88
                                } else {
81
                                        $elem = preg_replace('/^0+/', '', $elem);
89
                                        $elem = ltrim($elem, "0");
-
 
90
                                }
82
                                }
91
                        }
83
                        };
-
 
84
                        $oid = implode('.', $elements);
92
                        $oid = implode('.', $elements);
85
 
93
 
86
                        if ((count($elements) > 0) && ($elements[0] != '0') && ($elements[0] != '1') && ($elements[0] != '2')) return false;
94
                        if ((count($elements) > 0) && ($elements[0] != '0') && ($elements[0] != '1') && ($elements[0] != '2')) return false;
87
                        if ((count($elements) > 1) && (($elements[0] == '0') || ($elements[0] == '1')) && ((strlen($elements[1]) > 2) || ($elements[1] > 39))) return false;
95
                        if ((count($elements) > 1) && (($elements[0] == '0') || ($elements[0] == '1')) && ((strlen($elements[1]) > 2) || ($elements[1] > 39))) return false;
88
                }
96
                }
89
 
97
 
90
                return $oid;
98
                return $oid;
91
        }
99
        }
92
 
100
 
-
 
101
        /**
93
        // Translates a weid to an oid
102
         * Translates a weid to an oid
94
        // "weid:EXAMPLE-3" becomes "1.3.6.1.4.1.37553.8.32488192274"
103
         * "weid:EXAMPLE-3" becomes "1.3.6.1.4.1.37553.8.32488192274"
95
        // If it failed (e.g. wrong namespace, wrong checksum, etc.) then false is returned.
104
         * If it failed (e.g. wrong namespace, wrong checksum, etc.) then false is returned.
96
        // If the weid ends with '?', then it will be replaced with the checksum,
105
         * If the weid ends with '?', then it will be replaced with the checksum,
97
        // e.g. weid:EXAMPLE-? becomes weid:EXAMPLE-3
106
         * e.g. weid:EXAMPLE-? becomes weid:EXAMPLE-3
-
 
107
         * @param string $weid
-
 
108
         * @return false|string
-
 
109
         */
98
        public static function weid2oid(&$weid) {
110
        public static function weid2oid(string &$weid) {
99
                $weid = trim($weid);
111
                $weid = trim($weid);
100
 
112
 
101
                $p = strrpos($weid,':');
113
                $p = strrpos($weid,':');
102
                $namespace = substr($weid, 0, $p+1);
114
                $namespace = substr($weid, 0, $p+1);
103
                $rest = substr($weid, $p+1);
115
                $rest = substr($weid, $p+1);
Line 132... Line 144...
132
                                return false; // wrong checksum
144
                                return false; // wrong checksum
133
                        }
145
                        }
134
                } else {
146
                } else {
135
                        // If checksum is '?', it will be replaced by the actual checksum,
147
                        // If checksum is '?', it will be replaced by the actual checksum,
136
                        // e.g. weid:EXAMPLE-? becomes weid:EXAMPLE-3
148
                        // e.g. weid:EXAMPLE-? becomes weid:EXAMPLE-3
137
                        $weid = str_replace('?', $expected_checksum, $weid);
149
                        $weid = str_replace('?', "$expected_checksum", $weid);
138
                }
150
                }
139
                foreach ($elements as &$arc) {
151
                foreach ($elements as &$arc) {
140
                        //$arc = strtoupper(base_convert($arc, 36, 10));
152
                        //$arc = strtoupper(base_convert($arc, 36, 10));
141
                        $arc = strtoupper(self::base_convert_bigint($arc, 36, 10));
153
                        $arc = strtoupper(self::base_convert_bigint($arc, 36, 10));
142
                }
154
                }
Line 148... Line 160...
148
                if ($oid === false) return false;
160
                if ($oid === false) return false;
149
 
161
 
150
                return $oid;
162
                return $oid;
151
        }
163
        }
152
 
164
 
-
 
165
        /**
153
        // Converts an OID to WEID
166
         * Converts an OID to WEID
154
        // "1.3.6.1.4.1.37553.8.32488192274" becomes "weid:EXAMPLE-3"
167
         * "1.3.6.1.4.1.37553.8.32488192274" becomes "weid:EXAMPLE-3"
-
 
168
         * @param string $oid
-
 
169
         * @return false|string
-
 
170
         */
155
        public static function oid2weid($oid) {
171
        public static function oid2weid(string $oid) {
156
                $oid = self::oidSanitize($oid);
172
                $oid = self::oidSanitize($oid);
157
                if ($oid === false) return false;
173
                if ($oid === false) return false;
158
 
174
 
159
                if ($oid !== '') {
175
                if ($oid !== '') {
160
                        $elements = explode('.', $oid);
176
                        $elements = explode('.', $oid);
Line 188... Line 204...
188
                } else {
204
                } else {
189
                        // should not happen
205
                        // should not happen
190
                        return false;
206
                        return false;
191
                }
207
                }
192
 
208
 
193
                $weid = $namespace . ($weidstr == '' ? $checksum : $weidstr . '-' . $checksum);
209
                return $namespace . ($weidstr == '' ? $checksum : $weidstr . '-' . $checksum);
194
 
-
 
195
                return $weid;
-
 
196
        }
210
        }
197
 
211
 
-
 
212
        /**
-
 
213
         * @param string $numstring
-
 
214
         * @param int $frombase
-
 
215
         * @param int $tobase
-
 
216
         * @return string
-
 
217
         */
198
        protected static function base_convert_bigint($numstring, $frombase, $tobase) {
218
        protected static function base_convert_bigint(string $numstring, int $frombase, int $tobase): string {
199
                $frombase_str = '';
219
                $frombase_str = '';
200
                for ($i=0; $i<$frombase; $i++) {
220
                for ($i=0; $i<$frombase; $i++) {
201
                        $frombase_str .= strtoupper(base_convert((string)$i, 10, 36));
221
                        $frombase_str .= strtoupper(base_convert((string)$i, 10, 36));
202
                }
222
                }
203
 
223