Rev 750 | Rev 1050 | Go to most recent revision | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 750 | Rev 771 | ||
---|---|---|---|
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 2021-12-08 |
6 | * Revision 2022-03-06 |
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 59... | Line 59... | ||
59 | $sum += $digit; |
59 | $sum += $digit; |
60 | } |
60 | } |
61 | return ($sum%10) == 0 ? 0 : 10-($sum%10); |
61 | return ($sum%10) == 0 ? 0 : 10-($sum%10); |
62 | } |
62 | } |
63 | 63 | ||
- | 64 | private static function oidSanitize($oid) { |
|
- | 65 | $oid = trim($oid); |
|
- | 66 | ||
- | 67 | if (substr($oid,0,1) == '.') $oid = substr($oid,1); // remove leading dot |
|
- | 68 | ||
- | 69 | if ($oid != '') { |
|
- | 70 | $elements = explode('.', $oid); |
|
- | 71 | foreach ($elements as &$elem) { |
|
- | 72 | if (trim($elem) == '') return false; |
|
- | 73 | ||
- | 74 | if (!preg_match('/^\d+$/', $elem, $m)) return false; |
|
- | 75 | ||
- | 76 | if (preg_match('/^0+$/', $elem, $m)) { |
|
- | 77 | $elem = '0'; |
|
- | 78 | } else { |
|
- | 79 | $elem = preg_replace('/^0+/', '', $elem); |
|
- | 80 | } |
|
- | 81 | }; |
|
- | 82 | $oid = implode('.', $elements); |
|
- | 83 | ||
- | 84 | if ((count($elements) > 0) && ($elements[0] != '0') && ($elements[0] != '1') && ($elements[0] != '2')) return false; |
|
- | 85 | if ((count($elements) > 1) && (($elements[0] == '0') || ($elements[0] == '1')) && ((strlen($elements[1]) > 2) || ($elements[1] > 39))) return false; |
|
- | 86 | } |
|
- | 87 | ||
- | 88 | return $oid; |
|
- | 89 | } |
|
- | 90 | ||
64 | // Translates a weid to an oid |
91 | // Translates a weid to an oid |
65 | // "weid:EXAMPLE-3" becomes "1.3.6.1.4.1.37553.8.32488192274" |
92 | // "weid:EXAMPLE-3" becomes "1.3.6.1.4.1.37553.8.32488192274" |
66 | // If it failed (e.g. wrong namespace, wrong checksum, etc.) then false is returned. |
93 | // If it failed (e.g. wrong namespace, wrong checksum, etc.) then false is returned. |
67 | // If the weid ends with '?', then it will be replaced with the checksum, |
94 | // If the weid ends with '?', then it will be replaced with the checksum, |
68 | // e.g. weid:EXAMPLE-? becomes weid:EXAMPLE-3 |
95 | // e.g. weid:EXAMPLE-? becomes weid:EXAMPLE-3 |
69 | public static function weid2oid(&$weid) { |
96 | public static function weid2oid(&$weid) { |
- | 97 | $weid = trim($weid); |
|
70 | 98 | ||
71 | $p = strrpos($weid,':'); |
99 | $p = strrpos($weid,':'); |
72 | $namespace = substr($weid, 0, $p+1); |
100 | $namespace = substr($weid, 0, $p+1); |
73 | $rest = substr($weid, $p+1); |
101 | $rest = substr($weid, $p+1); |
74 | 102 | ||
Line 88... | Line 116... | ||
88 | } |
116 | } |
89 | 117 | ||
90 | $weid = $rest; |
118 | $weid = $rest; |
91 | 119 | ||
92 | $elements = array_merge(($base != '') ? explode('-', $base) : array(), explode('-', $weid)); |
120 | $elements = array_merge(($base != '') ? explode('-', $base) : array(), explode('-', $weid)); |
- | 121 | ||
- | 122 | foreach ($elements as $elem) { |
|
- | 123 | if ($elem == '') return false; |
|
- | 124 | } |
|
- | 125 | ||
93 | $actual_checksum = array_pop($elements); |
126 | $actual_checksum = array_pop($elements); |
94 | $expected_checksum = self::weLuhnGetCheckDigit(implode('-',$elements)); |
127 | $expected_checksum = self::weLuhnGetCheckDigit(implode('-',$elements)); |
95 | if ($actual_checksum != '?') { |
128 | if ($actual_checksum != '?') { |
96 | if ($actual_checksum != $expected_checksum) return false; // wrong checksum |
129 | if ($actual_checksum != $expected_checksum) { |
- | 130 | return false; // wrong checksum |
|
- | 131 | } |
|
97 | } else { |
132 | } else { |
98 | // If checksum is '?', it will be replaced by the actual checksum, |
133 | // If checksum is '?', it will be replaced by the actual checksum, |
99 | // e.g. weid:EXAMPLE-? becomes weid:EXAMPLE-3 |
134 | // e.g. weid:EXAMPLE-? becomes weid:EXAMPLE-3 |
100 | $weid = str_replace('?', $expected_checksum, $weid); |
135 | $weid = str_replace('?', $expected_checksum, $weid); |
101 | } |
136 | } |
102 | foreach ($elements as &$arc) { |
137 | foreach ($elements as &$arc) { |
103 | //$arc = strtoupper(base_convert($arc, 36, 10)); |
138 | //$arc = strtoupper(base_convert($arc, 36, 10)); |
104 | $arc = strtoupper(self::base_convert_bigint($arc, 36, 10)); |
139 | $arc = strtoupper(self::base_convert_bigint($arc, 36, 10)); |
105 | } |
140 | } |
106 | $oidstr = implode('.', $elements); |
141 | $oid = implode('.', $elements); |
107 | 142 | ||
108 | $weid = strtolower($namespace) . strtoupper($weid); // add namespace again |
143 | $weid = strtolower($namespace) . strtoupper($weid); // add namespace again |
109 | 144 | ||
- | 145 | $oid = self::oidSanitize($oid); |
|
- | 146 | if ($oid === false) return false; |
|
- | 147 | ||
110 | return $oidstr; |
148 | return $oid; |
111 | } |
149 | } |
112 | 150 | ||
113 | // Converts an OID to WEID |
151 | // Converts an OID to WEID |
114 | // "1.3.6.1.4.1.37553.8.32488192274" becomes "weid:EXAMPLE-3" |
152 | // "1.3.6.1.4.1.37553.8.32488192274" becomes "weid:EXAMPLE-3" |
115 | public static function oid2weid($oid) { |
153 | public static function oid2weid($oid) { |
- | 154 | $oid = self::oidSanitize($oid); |
|
116 | if (substr($oid,0,1) === '.') $oid = substr($oid,1); // remove leading dot |
155 | if ($oid === false) return false; |
117 | 156 | ||
118 | if ($oid !== '') { |
157 | if ($oid !== '') { |
119 | $elements = explode('.', $oid); |
158 | $elements = explode('.', $oid); |
120 | foreach ($elements as &$arc) { |
159 | foreach ($elements as &$arc) { |
121 | //$arc = strtoupper(base_convert($arc, 10, 36)); |
160 | //$arc = strtoupper(base_convert($arc, 10, 36)); |
Line 147... | Line 186... | ||
147 | } else { |
186 | } else { |
148 | // should not happen |
187 | // should not happen |
149 | return false; |
188 | return false; |
150 | } |
189 | } |
151 | 190 | ||
152 | return $namespace . ($weidstr == '' ? $checksum : $weidstr . '-' . $checksum); |
191 | $weid = $namespace . ($weidstr == '' ? $checksum : $weidstr . '-' . $checksum); |
- | 192 | ||
- | 193 | return $weid; |
|
153 | } |
194 | } |
154 | 195 | ||
155 | protected static function base_convert_bigint($numstring, $frombase, $tobase) { |
196 | protected static function base_convert_bigint($numstring, $frombase, $tobase) { |
156 | $frombase_str = ''; |
197 | $frombase_str = ''; |
157 | for ($i=0; $i<$frombase; $i++) { |
198 | for ($i=0; $i<$frombase; $i++) { |