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