Rev 753 | Rev 772 | Go to most recent revision | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 753 | Rev 758 | ||
---|---|---|---|
1 | <?php |
1 | <?php |
2 | 2 | ||
3 | /* |
3 | /* |
4 | * OIDplus 2.0 |
4 | * OIDplus 2.0 |
5 | * Copyright 2019 - 2021 Daniel Marschall, ViaThinkSoft |
5 | * Copyright 2019 - 2021 Daniel Marschall, ViaThinkSoft |
6 | * |
6 | * |
7 | * Licensed under the Apache License, Version 2.0 (the "License"); |
7 | * Licensed under the Apache License, Version 2.0 (the "License"); |
8 | * you may not use this file except in compliance with the License. |
8 | * you may not use this file except in compliance with the License. |
9 | * You may obtain a copy of the License at |
9 | * You may obtain a copy of the License at |
10 | * |
10 | * |
11 | * http://www.apache.org/licenses/LICENSE-2.0 |
11 | * http://www.apache.org/licenses/LICENSE-2.0 |
12 | * |
12 | * |
13 | * Unless required by applicable law or agreed to in writing, software |
13 | * Unless required by applicable law or agreed to in writing, software |
14 | * distributed under the License is distributed on an "AS IS" BASIS, |
14 | * distributed under the License is distributed on an "AS IS" BASIS, |
15 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
15 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
16 | * See the License for the specific language governing permissions and |
16 | * See the License for the specific language governing permissions and |
17 | * limitations under the License. |
17 | * limitations under the License. |
18 | */ |
18 | */ |
19 | 19 | ||
20 | if (!defined('INSIDE_OIDPLUS')) die(); |
20 | if (!defined('INSIDE_OIDPLUS')) die(); |
21 | 21 | ||
22 | class OIDplusOid extends OIDplusObject { |
22 | class OIDplusOid extends OIDplusObject { |
23 | private $oid; |
23 | private $oid; |
24 | 24 | ||
25 | public function __construct($oid) { |
25 | public function __construct($oid) { |
26 | $bak_oid = $oid; |
26 | $bak_oid = $oid; |
27 | 27 | ||
28 | $oid = sanitizeOID($oid, 'auto'); |
28 | $oid = sanitizeOID($oid, 'auto'); |
29 | if ($oid === false) { |
29 | if ($oid === false) { |
30 | throw new OIDplusException(_L('Invalid OID %1',$bak_oid)); |
30 | throw new OIDplusException(_L('Invalid OID %1',$bak_oid)); |
31 | } |
31 | } |
32 | 32 | ||
33 | if (($oid != '') && (!oid_valid_dotnotation($oid, false, true, 0))) { |
33 | if (($oid != '') && (!oid_valid_dotnotation($oid, false, true, 0))) { |
34 | // avoid OIDs like 3.0 |
34 | // avoid OIDs like 3.0 |
35 | throw new OIDplusException(_L('Invalid OID %1',$bak_oid)); |
35 | throw new OIDplusException(_L('Invalid OID %1',$bak_oid)); |
36 | } |
36 | } |
37 | 37 | ||
38 | $this->oid = $oid; |
38 | $this->oid = $oid; |
39 | } |
39 | } |
40 | 40 | ||
41 | public static function parse($node_id) { |
41 | public static function parse($node_id) { |
42 | @list($namespace, $oid) = explode(':', $node_id, 2); |
42 | @list($namespace, $oid) = explode(':', $node_id, 2); |
43 | if ($namespace !== 'oid') return false; |
43 | if ($namespace !== 'oid') return false; |
44 | return new self($oid); |
44 | return new self($oid); |
45 | } |
45 | } |
46 | 46 | ||
47 | public static function objectTypeTitle() { |
47 | public static function objectTypeTitle() { |
48 | return _L('Object Identifier (OID)'); |
48 | return _L('Object Identifier (OID)'); |
49 | } |
49 | } |
50 | 50 | ||
51 | public static function objectTypeTitleShort() { |
51 | public static function objectTypeTitleShort() { |
52 | return _L('OID'); |
52 | return _L('OID'); |
53 | } |
53 | } |
54 | 54 | ||
55 | public static function ns() { |
55 | public static function ns() { |
56 | return 'oid'; |
56 | return 'oid'; |
57 | } |
57 | } |
58 | 58 | ||
59 | public static function root() { |
59 | public static function root() { |
60 | return 'oid:'; |
60 | return 'oid:'; |
61 | } |
61 | } |
62 | 62 | ||
63 | public function isRoot() { |
63 | public function isRoot() { |
64 | return $this->oid == ''; |
64 | return $this->oid == ''; |
65 | } |
65 | } |
66 | 66 | ||
67 | public function nodeId($with_ns=true) { |
67 | public function nodeId($with_ns=true) { |
68 | return $with_ns ? 'oid:'.$this->oid : $this->oid; |
68 | return $with_ns ? 'oid:'.$this->oid : $this->oid; |
69 | } |
69 | } |
70 | 70 | ||
71 | public function addString($str) { |
71 | public function addString($str) { |
72 | if (!$this->isRoot()) { |
72 | if (!$this->isRoot()) { |
73 | if (strpos($str,'.') !== false) throw new OIDplusException(_L('Please only submit one arc (not an absolute OID or multiple arcs).')); |
73 | if (strpos($str,'.') !== false) throw new OIDplusException(_L('Please only submit one arc (not an absolute OID or multiple arcs).')); |
74 | } |
74 | } |
75 | 75 | ||
76 | return $this->appendArcs($str)->nodeId(); |
76 | return $this->appendArcs($str)->nodeId(); |
77 | } |
77 | } |
78 | 78 | ||
79 | public function crudShowId(OIDplusObject $parent) { |
79 | public function crudShowId(OIDplusObject $parent) { |
80 | if ($parent instanceof OIDplusOid) { |
80 | if ($parent instanceof OIDplusOid) { |
81 | return $this->deltaDotNotation($parent); |
81 | return $this->deltaDotNotation($parent); |
82 | } |
82 | } |
83 | } |
83 | } |
84 | 84 | ||
85 | public function jsTreeNodeName(OIDplusObject $parent = null) { |
85 | public function jsTreeNodeName(OIDplusObject $parent = null) { |
86 | if ($parent == null) return $this->objectTypeTitle(); |
86 | if ($parent == null) return $this->objectTypeTitle(); |
87 | if ($parent instanceof OIDplusOid) { |
87 | if ($parent instanceof OIDplusOid) { |
88 | return $this->viewGetArcAsn1s($parent); |
88 | return $this->viewGetArcAsn1s($parent); |
89 | } else { |
89 | } else { |
90 | return ''; |
90 | return ''; |
91 | } |
91 | } |
92 | } |
92 | } |
93 | 93 | ||
94 | public function defaultTitle() { |
94 | public function defaultTitle() { |
95 | return _L('OID %1',$this->oid); |
95 | return _L('OID %1',$this->oid); |
96 | } |
96 | } |
97 | 97 | ||
98 | public function isLeafNode() { |
98 | public function isLeafNode() { |
99 | return false; |
99 | return false; |
100 | } |
100 | } |
101 | 101 | ||
102 | private function getTechInfo() { |
102 | private function getTechInfo() { |
103 | $tech_info = array(); |
103 | $tech_info = array(); |
104 | 104 | ||
105 | $tmp = _L('Dot notation'); |
105 | $tmp = _L('Dot notation'); |
106 | $tmp = str_replace(explode(' ', $tmp, 2)[0], '<a href="http://oid-info.com/faq.htm#14" target="_blank">'.explode(' ', $tmp, 2)[0].'</a>', $tmp); |
106 | $tmp = str_replace(explode(' ', $tmp, 2)[0], '<a href="http://oid-info.com/faq.htm#14" target="_blank">'.explode(' ', $tmp, 2)[0].'</a>', $tmp); |
107 | $tech_info[$tmp] = $this->getDotNotation(); |
107 | $tech_info[$tmp] = $this->getDotNotation(); |
108 | 108 | ||
109 | $tmp = _L('ASN.1 notation'); |
109 | $tmp = _L('ASN.1 notation'); |
110 | $tmp = str_replace(explode(' ', $tmp, 2)[0], '<a href="http://oid-info.com/faq.htm#17" target="_blank">'.explode(' ', $tmp, 2)[0].'</a>', $tmp); |
110 | $tmp = str_replace(explode(' ', $tmp, 2)[0], '<a href="http://oid-info.com/faq.htm#17" target="_blank">'.explode(' ', $tmp, 2)[0].'</a>', $tmp); |
111 | $tech_info[$tmp] = $this->getAsn1Notation(); |
111 | $tech_info[$tmp] = $this->getAsn1Notation(); |
112 | 112 | ||
113 | $tmp = _L('OID-IRI notation'); |
113 | $tmp = _L('OID-IRI notation'); |
114 | $tmp = str_replace(explode(' ', $tmp, 2)[0], '<a href="http://oid-info.com/faq.htm#iri" target="_blank">'.explode(' ', $tmp, 2)[0].'</a>', $tmp); |
114 | $tmp = str_replace(explode(' ', $tmp, 2)[0], '<a href="http://oid-info.com/faq.htm#iri" target="_blank">'.explode(' ', $tmp, 2)[0].'</a>', $tmp); |
115 | $tech_info[$tmp] = $this->getIriNotation(); |
115 | $tech_info[$tmp] = $this->getIriNotation(); |
116 | 116 | ||
117 | $tmp = _L('WEID notation'); |
117 | $tmp = _L('WEID notation'); |
118 | $tmp = str_replace(explode(' ', $tmp, 2)[0], '<a href="https://weid.info/" target="_blank">'.explode(' ', $tmp, 2)[0].'</a>', $tmp); |
118 | $tmp = str_replace(explode(' ', $tmp, 2)[0], '<a href="https://weid.info/" target="_blank">'.explode(' ', $tmp, 2)[0].'</a>', $tmp); |
119 | $tech_info[$tmp] = $this->getWeidNotation(); |
119 | $tech_info[$tmp] = $this->getWeidNotation(); |
120 | 120 | ||
121 | return $tech_info; |
121 | return $tech_info; |
122 | } |
122 | } |
123 | 123 | ||
124 | public function getContentPage(&$title, &$content, &$icon) { |
124 | public function getContentPage(&$title, &$content, &$icon) { |
125 | $icon = file_exists(__DIR__.'/icon_big.png') ? OIDplus::webPath(__DIR__,true).'icon_big.png' : ''; |
125 | $icon = file_exists(__DIR__.'/icon_big.png') ? OIDplus::webPath(__DIR__,true).'icon_big.png' : ''; |
126 | 126 | ||
127 | if ($this->isRoot()) { |
127 | if ($this->isRoot()) { |
128 | $title = OIDplusOid::objectTypeTitle(); |
128 | $title = OIDplusOid::objectTypeTitle(); |
129 | 129 | ||
130 | $res = OIDplus::db()->query("select id from ###objects where parent = ?", array(self::root())); |
130 | $res = OIDplus::db()->query("select id from ###objects where parent = ?", array(self::root())); |
131 | if ($res->num_rows() > 0) { |
131 | if ($res->num_rows() > 0) { |
132 | $content = _L('Please select an OID in the tree view at the left to show its contents.'); |
132 | $content = _L('Please select an OID in the tree view at the left to show its contents.'); |
133 | } else { |
133 | } else { |
134 | $content = _L('Currently, no OID is registered in the system.'); |
134 | $content = _L('Currently, no OID is registered in the system.'); |
135 | } |
135 | } |
136 | 136 | ||
137 | if (!$this->isLeafNode()) { |
137 | if (!$this->isLeafNode()) { |
138 | if (OIDplus::authUtils()->isAdminLoggedIn()) { |
138 | if (OIDplus::authUtils()->isAdminLoggedIn()) { |
139 | $content .= '<h2>'._L('Manage your root OIDs').'</h2>'; |
139 | $content .= '<h2>'._L('Manage your root OIDs').'</h2>'; |
140 | } else { |
140 | } else { |
141 | $content .= '<h2>'._L('Root OIDs').'</h2>'; |
141 | $content .= '<h2>'._L('Root OIDs').'</h2>'; |
142 | } |
142 | } |
143 | $content .= '%%CRUD%%'; |
143 | $content .= '%%CRUD%%'; |
144 | } |
144 | } |
145 | } else { |
145 | } else { |
146 | $title = $this->getTitle(); |
146 | $title = $this->getTitle(); |
147 | 147 | ||
148 | $tech_info = $this->getTechInfo(); |
148 | $tech_info = $this->getTechInfo(); |
149 | $tech_info_html = ''; |
149 | $tech_info_html = ''; |
150 | if (count($tech_info) > 0) { |
150 | if (count($tech_info) > 0) { |
151 | $tech_info_html .= '<h2>'._L('Technical information').'</h2>'; |
151 | $tech_info_html .= '<h2>'._L('Technical information').'</h2>'; |
152 | $tech_info_html .= '<table border="0">'; |
152 | $tech_info_html .= '<table border="0">'; |
153 | foreach ($tech_info as $key => $value) { |
153 | foreach ($tech_info as $key => $value) { |
154 | $tech_info_html .= '<tr><td>'.$key.': </td><td><code>'.$value.'</code></td></tr>'; |
154 | $tech_info_html .= '<tr><td>'.$key.': </td><td><code>'.$value.'</code></td></tr>'; |
155 | } |
155 | } |
156 | $tech_info_html .= '</table>'; |
156 | $tech_info_html .= '</table>'; |
157 | } |
157 | } |
158 | 158 | ||
159 | $content = $tech_info_html; |
159 | $content = $tech_info_html; |
160 | 160 | ||
161 | $content .= '<h2>'._L('Description').'</h2>%%DESC%%'. |
161 | $content .= '<h2>'._L('Description').'</h2>%%DESC%%'. |
162 | '<h2>'._L('Registration Authority').'</h2>%%RA_INFO%%'; |
162 | '<h2>'._L('Registration Authority').'</h2>%%RA_INFO%%'; |
163 | 163 | ||
164 | if (!$this->isLeafNode()) { |
164 | if (!$this->isLeafNode()) { |
165 | if ($this->userHasWriteRights()) { |
165 | if ($this->userHasWriteRights()) { |
166 | $content .= '<h2>'._L('Create or change subsequent objects').'</h2>'; |
166 | $content .= '<h2>'._L('Create or change subsequent objects').'</h2>'; |
167 | } else { |
167 | } else { |
168 | $content .= '<h2>'._L('Subsequent objects').'</h2>'; |
168 | $content .= '<h2>'._L('Subsequent objects').'</h2>'; |
169 | } |
169 | } |
170 | $content .= '%%CRUD%%'; |
170 | $content .= '%%CRUD%%'; |
171 | } |
171 | } |
172 | } |
172 | } |
173 | } |
173 | } |
174 | 174 | ||
175 | # --- |
175 | # --- |
176 | 176 | ||
177 | // Gets the last arc of an WEID |
177 | // Gets the last arc of an WEID |
178 | public function weidArc() { |
178 | public function weidArc() { |
179 | // Dirty hack: We prepend '0.' in front of the OID to enforce the |
179 | // Dirty hack: We prepend '0.' in front of the OID to enforce the |
180 | // creation of a Class A weid (weid:root:) . Otherwise we could not |
180 | // creation of a Class A weid (weid:root:) . Otherwise we could not |
181 | // get the hidden arc value "8" from "weid:4" (which is actually "weid:pen:SZ5-8-?" |
181 | // get the hidden arc value "8" from "weid:4" (which is actually "weid:pen:SZ5-8-?" |
182 | $weid = WeidOidConverter::oid2weid('0.'.$this->getDotNotation()); |
182 | $weid = WeidOidConverter::oid2weid('0.'.$this->getDotNotation()); |
183 | if ($weid === false) return false; |
183 | if ($weid === false) return false; |
184 | $ary = explode(':', $weid); |
184 | $ary = explode(':', $weid); |
185 | $weid = array_pop($ary); // remove namespace and sub-namespace if existing |
185 | $weid = array_pop($ary); // remove namespace and sub-namespace if existing |
186 | $x = explode('-', $weid); |
186 | $x = explode('-', $weid); |
187 | if (count($x) < 2) return ''; // WEID root arc. Has no name |
187 | if (count($x) < 2) return ''; // WEID root arc. Has no name |
188 | return $x[count($x)-2]; |
188 | return $x[count($x)-2]; |
189 | } |
189 | } |
190 | 190 | ||
191 | public function getWeidNotation($withAbbr=true) { |
191 | public function getWeidNotation($withAbbr=true) { |
192 | $weid = WeidOidConverter::oid2weid($this->getDotNotation()); |
192 | $weid = WeidOidConverter::oid2weid($this->getDotNotation()); |
193 | if ($withAbbr) { |
193 | if ($withAbbr) { |
194 | $ary = explode(':', $weid); |
194 | $ary = explode(':', $weid); |
195 | $weid = array_pop($ary); // remove namespace and sub-namespace if existing |
195 | $weid = array_pop($ary); // remove namespace and sub-namespace if existing |
196 | $ns = implode(':', $ary).':'; |
196 | $ns = implode(':', $ary).':'; |
197 | 197 | ||
198 | $weid_arcs = explode('-', $weid); |
198 | $weid_arcs = explode('-', $weid); |
199 | foreach ($weid_arcs as $i => &$weid) { |
199 | foreach ($weid_arcs as $i => &$weid) { |
200 | if ($i == count($weid_arcs)-1) { |
200 | if ($i == count($weid_arcs)-1) { |
201 | $weid = '<abbr title="'._L('weLuhn check digit').'">'.$weid.'</abbr>'; |
201 | $weid = '<abbr title="'._L('weLuhn check digit').'">'.$weid.'</abbr>'; |
202 | } else { |
202 | } else { |
203 | $oid_arcs = explode('.',$this->oid); |
203 | $oid_arcs = explode('.',$this->oid); |
204 | $weid_num = $oid_arcs[(count($oid_arcs)-1)-(count($weid_arcs)-1)+($i+1)]; |
204 | $weid_num = $oid_arcs[(count($oid_arcs)-1)-(count($weid_arcs)-1)+($i+1)]; |
205 | if ($weid_num != $weid) { |
205 | if ($weid_num != $weid) { |
206 | $weid = '<abbr title="'._L('Numeric value').': '.$weid_num.'">'.$weid.'</abbr>'; |
206 | $weid = '<abbr title="'._L('Numeric value').': '.$weid_num.'">'.$weid.'</abbr>'; |
207 | } |
207 | } |
208 | } |
208 | } |
209 | } |
209 | } |
210 | $base_arc = '???'; |
210 | $base_arc = '???'; |
211 | if ($ns === 'weid:') $base_arc = '1.3.6.1.4.1.37553.8'; |
211 | if ($ns === 'weid:') $base_arc = '1.3.6.1.4.1.37553.8'; |
212 | if ($ns === 'weid:pen:') $base_arc = '1.3.6.1.4.1'; |
212 | if ($ns === 'weid:pen:') $base_arc = '1.3.6.1.4.1'; |
213 | if ($ns === 'weid:root:') $base_arc = _L('OID tree root'); |
213 | if ($ns === 'weid:root:') $base_arc = _L('OID tree root'); |
214 | 214 | ||
215 | $weid = '<abbr title="'._L('Base OID').': '.$base_arc.'">' . rtrim($ns,':') . '</abbr>:' . implode('-',$weid_arcs); |
215 | $weid = '<abbr title="'._L('Base OID').': '.$base_arc.'">' . rtrim($ns,':') . '</abbr>:' . implode('-',$weid_arcs); |
216 | } |
216 | } |
217 | return $weid; |
217 | return $weid; |
218 | } |
218 | } |
219 | 219 | ||
220 | public function appendArcs(String $arcs) { |
220 | public function appendArcs(String $arcs) { |
221 | $out = new self($this->oid); |
221 | $out = new self($this->oid); |
222 | 222 | ||
223 | if ($out->isRoot()) { |
223 | if ($out->isRoot()) { |
224 | $out->oid .= $arcs; |
224 | $out->oid .= $arcs; |
225 | } else { |
225 | } else { |
226 | $out->oid .= '.' . $arcs; |
226 | $out->oid .= '.' . $arcs; |
227 | } |
227 | } |
228 | 228 | ||
229 | $bak_oid = $out->oid; |
229 | $bak_oid = $out->oid; |
230 | $out->oid = sanitizeOID($out->oid); |
230 | $out->oid = sanitizeOID($out->oid); |
231 | if ($out->oid === false) throw new OIDplusException(_L('%1 is not a valid OID!',$bak_oid)); |
231 | if ($out->oid === false) throw new OIDplusException(_L('%1 is not a valid OID!',$bak_oid)); |
232 | 232 | ||
233 | $maxlen = OIDplus::baseConfig()->getValue('LIMITS_MAX_ID_LENGTH')-strlen('oid:'); |
233 | $maxlen = OIDplus::baseConfig()->getValue('LIMITS_MAX_ID_LENGTH')-strlen('oid:'); |
234 | if (strlen($out->oid) > $maxlen) { |
234 | if (strlen($out->oid) > $maxlen) { |
235 | throw new OIDplusException(_L('The resulting OID "%1" is too long (max allowed length: %2).',$out->oid,$maxlen)); |
235 | throw new OIDplusException(_L('The resulting OID "%1" is too long (max allowed length: %2).',$out->oid,$maxlen)); |
236 | } |
236 | } |
237 | 237 | ||
238 | $depth = 0; |
238 | $depth = 0; |
239 | foreach (explode('.',$out->oid) as $arc) { |
239 | foreach (explode('.',$out->oid) as $arc) { |
240 | if (strlen($arc) > OIDplus::baseConfig()->getValue('LIMITS_MAX_OID_ARC_SIZE')) { |
240 | if (strlen($arc) > OIDplus::baseConfig()->getValue('LIMITS_MAX_OID_ARC_SIZE')) { |
241 | $maxlen = OIDplus::baseConfig()->getValue('LIMITS_MAX_OID_ARC_SIZE'); |
241 | $maxlen = OIDplus::baseConfig()->getValue('LIMITS_MAX_OID_ARC_SIZE'); |
242 | throw new OIDplusException(_L('Arc "%1" is too long and therefore cannot be appended to the OID "%2" (max allowed arc size is "%3")',$arc,$this->oid,$maxlen)); |
242 | throw new OIDplusException(_L('Arc "%1" is too long and therefore cannot be appended to the OID "%2" (max allowed arc size is "%3")',$arc,$this->oid,$maxlen)); |
243 | } |
243 | } |
244 | $depth++; |
244 | $depth++; |
245 | } |
245 | } |
246 | if ($depth > OIDplus::baseConfig()->getValue('LIMITS_MAX_OID_DEPTH')) { |
246 | if ($depth > OIDplus::baseConfig()->getValue('LIMITS_MAX_OID_DEPTH')) { |
247 | $maxdepth = OIDplus::baseConfig()->getValue('LIMITS_MAX_OID_DEPTH'); |
247 | $maxdepth = OIDplus::baseConfig()->getValue('LIMITS_MAX_OID_DEPTH'); |
248 | throw new OIDplusException(_L('OID %1 has too many arcs (current depth %2, max depth %3)',$out->oid,$depth,$maxdepth)); |
248 | throw new OIDplusException(_L('OID %1 has too many arcs (current depth %2, max depth %3)',$out->oid,$depth,$maxdepth)); |
249 | } |
249 | } |
250 | 250 | ||
251 | return $out; |
251 | return $out; |
252 | } |
252 | } |
253 | 253 | ||
254 | public function deltaDotNotation(OIDplusOid $parent) { |
254 | public function deltaDotNotation(OIDplusOid $parent) { |
255 | if (!$parent->isRoot()) { |
255 | if (!$parent->isRoot()) { |
256 | if (substr($this->oid, 0, strlen($parent->oid)+1) == $parent->oid.'.') { |
256 | if (substr($this->oid, 0, strlen($parent->oid)+1) == $parent->oid.'.') { |
257 | return substr($this->oid, strlen($parent->oid)+1); |
257 | return substr($this->oid, strlen($parent->oid)+1); |
258 | } else { |
258 | } else { |
259 | return false; |
259 | return false; |
260 | } |
260 | } |
261 | } else { |
261 | } else { |
262 | return $this->oid; |
262 | return $this->oid; |
263 | } |
263 | } |
264 | } |
264 | } |
265 | 265 | ||
266 | public function viewGetArcAsn1s(OIDplusOid $parent=null, $separator = ' | ') { |
266 | public function viewGetArcAsn1s(OIDplusOid $parent=null, $separator = ' | ') { |
267 | $asn_ids = array(); |
267 | $asn_ids = array(); |
268 | 268 | ||
269 | if (is_null($parent)) $parent = OIDplusOid::parse('oid:'); |
269 | if (is_null($parent)) $parent = OIDplusOid::parse('oid:'); |
270 | 270 | ||
271 | $part = $this->deltaDotNotation($parent); |
271 | $part = $this->deltaDotNotation($parent); |
272 | 272 | ||
273 | if (strpos($part, '.') === false) { |
273 | if (strpos($part, '.') === false) { |
274 | $res2 = OIDplus::db()->query("select name from ###asn1id where oid = ? order by lfd", array("oid:".$this->oid)); |
274 | $res2 = OIDplus::db()->query("select name from ###asn1id where oid = ? order by lfd", array("oid:".$this->oid)); |
275 | while ($row2 = $res2->fetch_array()) { |
275 | while ($row2 = $res2->fetch_array()) { |
276 | $asn_ids[] = $row2['name'].'('.$part.')'; |
276 | $asn_ids[] = $row2['name'].'('.$part.')'; |
277 | } |
277 | } |
278 | } |
278 | } |
279 | 279 | ||
280 | if (count($asn_ids) == 0) $asn_ids = array($part); |
280 | if (count($asn_ids) == 0) $asn_ids = array($part); |
281 | return implode($separator, $asn_ids); |
281 | return implode($separator, $asn_ids); |
282 | } |
282 | } |
283 | 283 | ||
284 | public function getAsn1Notation($withAbbr=true) { |
284 | public function getAsn1Notation($withAbbr=true) { |
285 | $asn1_notation = ''; |
285 | $asn1_notation = ''; |
286 | $arcs = explode('.', $this->oid); |
286 | $arcs = explode('.', $this->oid); |
287 | 287 | ||
288 | foreach ($arcs as $arc) { |
288 | foreach ($arcs as $arc) { |
289 | $res = OIDplus::db()->query("select name, standardized from ###asn1id where oid = ? order by lfd", array('oid:'.implode('.',$arcs))); |
289 | $res = OIDplus::db()->query("select name, standardized from ###asn1id where oid = ? order by lfd", array('oid:'.implode('.',$arcs))); |
290 | 290 | ||
291 | $names = array(); |
291 | $names = array(); |
292 | while ($row = $res->fetch_array()) { |
292 | while ($row = $res->fetch_array()) { |
293 | $names[] = $row['name']."(".end($arcs).")"; |
293 | $names[] = $row['name']."(".end($arcs).")"; |
294 | if ($row['standardized']) { |
294 | if ($row['standardized']) { |
295 | $names[] = $row['name']; |
295 | $names[] = $row['name']; |
296 | } |
296 | } |
297 | } |
297 | } |
298 | 298 | ||
299 | $numeric = array_pop($arcs); |
299 | $numeric = array_pop($arcs); |
300 | if (count($names) > 1) { |
300 | if (count($names) > 1) { |
301 | $first_name = array_shift($names); |
301 | $first_name = array_shift($names); |
302 | $abbr = _L('Other identifiers').': '.implode(' ',$names); |
302 | $abbr = _L('Other identifiers').': '.implode(' ',$names); |
303 | if ($withAbbr) { |
303 | if ($withAbbr) { |
304 | $asn1_notation = '<abbr title="'.$abbr.'">'.$first_name.'</abbr> '.$asn1_notation; |
304 | $asn1_notation = '<abbr title="'.$abbr.'">'.$first_name.'</abbr> '.$asn1_notation; |
305 | } else { |
305 | } else { |
306 | $asn1_notation = $first_name.' '.$asn1_notation; |
306 | $asn1_notation = $first_name.' '.$asn1_notation; |
307 | } |
307 | } |
308 | } else if (count($names) == 1) { |
308 | } else if (count($names) == 1) { |
309 | $asn1_notation = array_shift($names).' '.$asn1_notation; |
309 | $asn1_notation = array_shift($names).' '.$asn1_notation; |
310 | } else { |
310 | } else { |
311 | $asn1_notation = $numeric.' '.$asn1_notation; |
311 | $asn1_notation = $numeric.' '.$asn1_notation; |
312 | } |
312 | } |
313 | } |
313 | } |
314 | 314 | ||
315 | return "{ $asn1_notation }"; |
315 | return "{ ".trim($asn1_notation)." }"; |
316 | } |
316 | } |
317 | 317 | ||
318 | public function getIriNotation($withAbbr=true) { |
318 | public function getIriNotation($withAbbr=true) { |
319 | $iri_notation = ''; |
319 | $iri_notation = ''; |
320 | $arcs = explode('.', $this->oid); |
320 | $arcs = explode('.', $this->oid); |
321 | 321 | ||
322 | foreach ($arcs as $arc) { |
322 | foreach ($arcs as $arc) { |
323 | $res = OIDplus::db()->query("select name, longarc from ###iri where oid = ? order by lfd", array('oid:'.implode('.',$arcs))); |
323 | $res = OIDplus::db()->query("select name, longarc from ###iri where oid = ? order by lfd", array('oid:'.implode('.',$arcs))); |
324 | 324 | ||
325 | $is_longarc = false; |
325 | $is_longarc = false; |
326 | $names = array(); |
326 | $names = array(); |
327 | while ($row = $res->fetch_array()) { |
327 | while ($row = $res->fetch_array()) { |
328 | $is_longarc = $row['longarc']; |
328 | $is_longarc = $row['longarc']; |
329 | $names[] = $row['name']; |
329 | $names[] = $row['name']; |
330 | 330 | ||
331 | if ($is_longarc) { |
331 | if ($is_longarc) { |
332 | $names[] = 'Joint-ISO-ITU-T/'.$row['name']; // Long arcs can only be inside root OID 2 |
332 | $names[] = 'Joint-ISO-ITU-T/'.$row['name']; // Long arcs can only be inside root OID 2 |
333 | } |
333 | } |
334 | } |
334 | } |
335 | 335 | ||
336 | $names[] = array_pop($arcs); |
336 | $names[] = array_pop($arcs); |
337 | if (count($names) > 2) { |
337 | if (count($names) > 2) { |
338 | $first_name = array_shift($names); |
338 | $first_name = array_shift($names); |
339 | $numeric = array_pop($names); |
339 | $numeric = array_pop($names); |
340 | $abbr = _L('Other identifiers').': '.implode(' ',$names).' '._L('Numeric value').': '.$numeric; |
340 | $abbr = _L('Other identifiers').': '.implode(' ',$names).' '._L('Numeric value').': '.$numeric; |
341 | $iri_notation = $withAbbr ? '<abbr title="'.$abbr.'">'.$first_name.'</abbr>/'.$iri_notation : $first_name.'/'.$iri_notation; |
341 | $iri_notation = $withAbbr ? '<abbr title="'.$abbr.'">'.$first_name.'</abbr>/'.$iri_notation : $first_name.'/'.$iri_notation; |
342 | } else if (count($names) > 1) { |
342 | } else if (count($names) > 1) { |
343 | $first_name = array_shift($names); |
343 | $first_name = array_shift($names); |
344 | $abbr = _L('Numeric value').': '.array_shift($names); |
344 | $abbr = _L('Numeric value').': '.array_shift($names); |
345 | $iri_notation = $withAbbr ? '<abbr title="'.$abbr.'">'.$first_name.'</abbr>/'.$iri_notation : $first_name.'/'.$iri_notation; |
345 | $iri_notation = $withAbbr ? '<abbr title="'.$abbr.'">'.$first_name.'</abbr>/'.$iri_notation : $first_name.'/'.$iri_notation; |
346 | } else if (count($names) == 1) { |
346 | } else if (count($names) == 1) { |
347 | $iri_notation = array_shift($names) . '/' . $iri_notation; |
347 | $iri_notation = array_shift($names) . '/' . $iri_notation; |
348 | } |
348 | } |
349 | 349 | ||
350 | if ($is_longarc) break; // we don't write /ITU-T/ at the beginning, when /ITU-T/xyz is a long arc |
350 | if ($is_longarc) break; // we don't write /ITU-T/ at the beginning, when /ITU-T/xyz is a long arc |
351 | } |
351 | } |
352 | $iri_notation = '/' . substr($iri_notation, 0, strlen($iri_notation)-1); |
352 | $iri_notation = '/' . substr($iri_notation, 0, strlen($iri_notation)-1); |
353 | 353 | ||
354 | return $iri_notation; |
354 | return $iri_notation; |
355 | } |
355 | } |
356 | 356 | ||
357 | public function getDotNotation() { |
357 | public function getDotNotation() { |
358 | return $this->oid; |
358 | return $this->oid; |
359 | } |
359 | } |
360 | 360 | ||
361 | public function isWellKnown() { |
361 | public function isWellKnown() { |
362 | $res = OIDplus::db()->query("select oid from ###asn1id where oid = ? and well_known = ?", array("oid:".$this->oid,true)); |
362 | $res = OIDplus::db()->query("select oid from ###asn1id where oid = ? and well_known = ?", array("oid:".$this->oid,true)); |
363 | if ($res->num_rows() > 0) return true; |
363 | if ($res->num_rows() > 0) return true; |
364 | 364 | ||
365 | $res = OIDplus::db()->query("select oid from ###iri where oid = ? and well_known = ?", array("oid:".$this->oid,true)); |
365 | $res = OIDplus::db()->query("select oid from ###iri where oid = ? and well_known = ?", array("oid:".$this->oid,true)); |
366 | if ($res->num_rows() > 0) return true; |
366 | if ($res->num_rows() > 0) return true; |
367 | 367 | ||
368 | return false; |
368 | return false; |
369 | } |
369 | } |
370 | 370 | ||
371 | public function replaceAsn1Ids($demandedASN1s=array(), $simulate=false) { |
371 | public function replaceAsn1Ids($demandedASN1s=array(), $simulate=false) { |
372 | if ($this->isWellKnown()) { |
372 | if ($this->isWellKnown()) { |
373 | throw new OIDplusException(_L('OID "%1" is a "well-known" OID. Its identifiers cannot be changed.',$this->oid)); |
373 | throw new OIDplusException(_L('OID "%1" is a "well-known" OID. Its identifiers cannot be changed.',$this->oid)); |
374 | } |
374 | } |
375 | 375 | ||
376 | // First do a few checks |
376 | // First do a few checks |
377 | foreach ($demandedASN1s as &$asn1) { |
377 | foreach ($demandedASN1s as &$asn1) { |
378 | $asn1 = trim($asn1); |
378 | $asn1 = trim($asn1); |
379 | 379 | ||
380 | if (strlen($asn1) > OIDplus::baseConfig()->getValue('LIMITS_MAX_OID_ASN1_ID_LEN')) { |
380 | if (strlen($asn1) > OIDplus::baseConfig()->getValue('LIMITS_MAX_OID_ASN1_ID_LEN')) { |
381 | $maxlen = OIDplus::baseConfig()->getValue('LIMITS_MAX_OID_ASN1_ID_LEN'); |
381 | $maxlen = OIDplus::baseConfig()->getValue('LIMITS_MAX_OID_ASN1_ID_LEN'); |
382 | throw new OIDplusException(_L('ASN.1 alphanumeric identifier "%1" is too long (max allowed length %2)',$asn1,$maxlen)); |
382 | throw new OIDplusException(_L('ASN.1 alphanumeric identifier "%1" is too long (max allowed length %2)',$asn1,$maxlen)); |
383 | } |
383 | } |
384 | 384 | ||
385 | // Validate identifier |
385 | // Validate identifier |
386 | if (!oid_id_is_valid($asn1)) throw new OIDplusException(_L('"%1" is not a valid ASN.1 identifier!',$asn1)); |
386 | if (!oid_id_is_valid($asn1)) throw new OIDplusException(_L('"%1" is not a valid ASN.1 identifier!',$asn1)); |
387 | 387 | ||
388 | // Check if the (real) parent has any conflict |
388 | // Check if the (real) parent has any conflict |
389 | // Unlike IRI identifiers, ASN.1 identifiers may be used multiple times (not recommended), except if one of them is standardized |
389 | // Unlike IRI identifiers, ASN.1 identifiers may be used multiple times (not recommended), except if one of them is standardized |
390 | $res = OIDplus::db()->query("select oid from ###asn1id where name = ? and standardized = ?", array($asn1,true)); |
390 | $res = OIDplus::db()->query("select oid from ###asn1id where name = ? and standardized = ?", array($asn1,true)); |
391 | while ($row = $res->fetch_array()) { |
391 | while ($row = $res->fetch_array()) { |
392 | $check_oid = OIDplusOid::parse($row['oid'])->oid; |
392 | $check_oid = OIDplusOid::parse($row['oid'])->oid; |
393 | if ((oid_up($check_oid) === oid_up($this->oid)) && // same parent |
393 | if ((oid_up($check_oid) === oid_up($this->oid)) && // same parent |
394 | ($check_oid !== $this->oid)) // different OID |
394 | ($check_oid !== $this->oid)) // different OID |
395 | { |
395 | { |
396 | throw new OIDplusException(_L('ASN.1 identifier "%1" is a standardized identifier belonging to OID %2',$asn1,$check_oid)); |
396 | throw new OIDplusException(_L('ASN.1 identifier "%1" is a standardized identifier belonging to OID %2',$asn1,$check_oid)); |
397 | } |
397 | } |
398 | } |
398 | } |
399 | } |
399 | } |
400 | 400 | ||
401 | // Now do the real replacement |
401 | // Now do the real replacement |
402 | if (!$simulate) { |
402 | if (!$simulate) { |
403 | OIDplus::db()->query("delete from ###asn1id where oid = ?", array("oid:".$this->oid)); |
403 | OIDplus::db()->query("delete from ###asn1id where oid = ?", array("oid:".$this->oid)); |
404 | foreach ($demandedASN1s as &$asn1) { |
404 | foreach ($demandedASN1s as &$asn1) { |
405 | OIDplus::db()->query("insert into ###asn1id (oid, name) values (?, ?)", array("oid:".$this->oid, $asn1)); |
405 | OIDplus::db()->query("insert into ###asn1id (oid, name) values (?, ?)", array("oid:".$this->oid, $asn1)); |
406 | } |
406 | } |
407 | } |
407 | } |
408 | } |
408 | } |
409 | 409 | ||
410 | public function replaceIris($demandedIris=array(), $simulate=false) { |
410 | public function replaceIris($demandedIris=array(), $simulate=false) { |
411 | if ($this->isWellKnown()) { |
411 | if ($this->isWellKnown()) { |
412 | throw new OIDplusException(_L('OID "%1" is a "well-known" OID. Its identifiers cannot be changed.',$this->oid)); |
412 | throw new OIDplusException(_L('OID "%1" is a "well-known" OID. Its identifiers cannot be changed.',$this->oid)); |
413 | } |
413 | } |
414 | 414 | ||
415 | // First do a few checks |
415 | // First do a few checks |
416 | foreach ($demandedIris as &$iri) { |
416 | foreach ($demandedIris as &$iri) { |
417 | $iri = trim($iri); |
417 | $iri = trim($iri); |
418 | 418 | ||
419 | if (strlen($iri) > OIDplus::baseConfig()->getValue('LIMITS_MAX_OID_UNICODE_LABEL_LEN')) { |
419 | if (strlen($iri) > OIDplus::baseConfig()->getValue('LIMITS_MAX_OID_UNICODE_LABEL_LEN')) { |
420 | $maxlen = OIDplus::baseConfig()->getValue('LIMITS_MAX_OID_UNICODE_LABEL_LEN'); |
420 | $maxlen = OIDplus::baseConfig()->getValue('LIMITS_MAX_OID_UNICODE_LABEL_LEN'); |
421 | throw new OIDplusException(_L('Unicode label "%1" is too long (max allowed length %2)',$iri,$maxlen)); |
421 | throw new OIDplusException(_L('Unicode label "%1" is too long (max allowed length %2)',$iri,$maxlen)); |
422 | } |
422 | } |
423 | 423 | ||
424 | // Validate identifier |
424 | // Validate identifier |
425 | if (!iri_arc_valid($iri, false)) throw new OIDplusException(_L('"%1" is not a valid IRI!',$iri)); |
425 | if (!iri_arc_valid($iri, false)) throw new OIDplusException(_L('"%1" is not a valid IRI!',$iri)); |
426 | 426 | ||
427 | // Check if the (real) parent has any conflict |
427 | // Check if the (real) parent has any conflict |
428 | $res = OIDplus::db()->query("select oid from ###iri where name = ?", array($iri)); |
428 | $res = OIDplus::db()->query("select oid from ###iri where name = ?", array($iri)); |
429 | while ($row = $res->fetch_array()) { |
429 | while ($row = $res->fetch_array()) { |
430 | $check_oid = OIDplusOid::parse($row['oid'])->oid; |
430 | $check_oid = OIDplusOid::parse($row['oid'])->oid; |
431 | if ((oid_up($check_oid) === oid_up($this->oid)) && // same parent |
431 | if ((oid_up($check_oid) === oid_up($this->oid)) && // same parent |
432 | ($check_oid !== $this->oid)) // different OID |
432 | ($check_oid !== $this->oid)) // different OID |
433 | { |
433 | { |
434 | throw new OIDplusException(_L('IRI "%1" is already used by another OID (%2)',$iri,$check_oid)); |
434 | throw new OIDplusException(_L('IRI "%1" is already used by another OID (%2)',$iri,$check_oid)); |
435 | } |
435 | } |
436 | } |
436 | } |
437 | } |
437 | } |
438 | 438 | ||
439 | // Now do the real replacement |
439 | // Now do the real replacement |
440 | if (!$simulate) { |
440 | if (!$simulate) { |
441 | OIDplus::db()->query("delete from ###iri where oid = ?", array("oid:".$this->oid)); |
441 | OIDplus::db()->query("delete from ###iri where oid = ?", array("oid:".$this->oid)); |
442 | foreach ($demandedIris as &$iri) { |
442 | foreach ($demandedIris as &$iri) { |
443 | OIDplus::db()->query("insert into ###iri (oid, name) values (?, ?)", array("oid:".$this->oid, $iri)); |
443 | OIDplus::db()->query("insert into ###iri (oid, name) values (?, ?)", array("oid:".$this->oid, $iri)); |
444 | } |
444 | } |
445 | } |
445 | } |
446 | } |
446 | } |
447 | 447 | ||
448 | public function one_up() { |
448 | public function one_up() { |
449 | return self::parse(self::ns().':'.oid_up($this->oid)); |
449 | return self::parse(self::ns().':'.oid_up($this->oid)); |
450 | } |
450 | } |
451 | 451 | ||
452 | public function distance($to) { |
452 | public function distance($to) { |
453 | if (!is_object($to)) $to = OIDplusObject::parse($to); |
453 | if (!is_object($to)) $to = OIDplusObject::parse($to); |
454 | if (!($to instanceof $this)) return false; |
454 | if (!($to instanceof $this)) return false; |
455 | return oid_distance($to->oid, $this->oid); |
455 | return oid_distance($to->oid, $this->oid); |
456 | } |
456 | } |
457 | 457 | ||
458 | public function getAltIds() { |
458 | public function getAltIds() { |
459 | if ($this->isRoot()) return array(); |
459 | if ($this->isRoot()) return array(); |
460 | $ids = parent::getAltIds(); |
460 | $ids = parent::getAltIds(); |
461 | if ($uuid = oid_to_uuid($this->oid)) { |
461 | if ($uuid = oid_to_uuid($this->oid)) { |
462 | $ids[] = new OIDplusAltId('guid', $uuid, _L('GUID representation of this OID')); |
462 | $ids[] = new OIDplusAltId('guid', $uuid, _L('GUID representation of this OID')); |
463 | } |
463 | } |
464 | $ids[] = new OIDplusAltId('guid', gen_uuid_md5_namebased(UUID_NAMEBASED_NS_OID, $this->oid), _L('Name based version 3 / MD5 UUID with namespace %1','UUID_NAMEBASED_NS_OID')); |
464 | $ids[] = new OIDplusAltId('guid', gen_uuid_md5_namebased(UUID_NAMEBASED_NS_OID, $this->oid), _L('Name based version 3 / MD5 UUID with namespace %1','UUID_NAMEBASED_NS_OID')); |
465 | $ids[] = new OIDplusAltId('guid', gen_uuid_sha1_namebased(UUID_NAMEBASED_NS_OID, $this->oid), _L('Name based version 5 / SHA1 UUID with namespace %1','UUID_NAMEBASED_NS_OID')); |
465 | $ids[] = new OIDplusAltId('guid', gen_uuid_sha1_namebased(UUID_NAMEBASED_NS_OID, $this->oid), _L('Name based version 5 / SHA1 UUID with namespace %1','UUID_NAMEBASED_NS_OID')); |
466 | return $ids; |
466 | return $ids; |
467 | } |
467 | } |
468 | 468 | ||
469 | public function getDirectoryName() { |
469 | public function getDirectoryName() { |
470 | if ($this->isRoot()) return $this->ns(); |
470 | if ($this->isRoot()) return $this->ns(); |
471 | $oid = $this->nodeId(false); |
471 | $oid = $this->nodeId(false); |
472 | return $this->ns().'_'.str_replace('.', '_', $oid); |
472 | return $this->ns().'_'.str_replace('.', '_', $oid); |
473 | } |
473 | } |
474 | } |
474 | } |
475 | 475 |