Rev 196 | 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 | |
76 | if (isset($_REQUEST['weid']) && ($_REQUEST['weid'] == "true")) { // <-- TODO: mixing GUI with backend code is not a good idea...! |
||
77 | if (preg_replace('@[A-Za-z0-9]@ismU', '', $str) !== "") throw new Exception("Invalid characters. Can only be A-Z and 0-9"); |
||
78 | $old_oid = $this->oid; |
||
79 | $old_weid = WeidOidConverter::oid2weid($old_oid, ''); |
||
80 | $old_weid = substr($old_weid, 0, strlen($old_weid)-2); // remove checksum |
||
81 | if ($old_weid != '') $old_weid .= '-'; |
||
82 | $old_weid = 'weid:' . $old_weid . $str . '-?'; // add new WEID arc |
||
83 | $new_oid = WeidOidConverter::weid2oid($old_weid); // convert back to OID (since OIDplus can only handle those) |
||
84 | return "oid:$new_oid"; |
||
85 | } else { |
||
86 | return $this->appendArcs($str)->nodeId(); |
||
87 | } |
||
2 | daniel-mar | 88 | } |
89 | |||
90 | public function crudShowId(OIDplusObject $parent) { |
||
91 | return $this->deltaDotNotation($parent); |
||
92 | } |
||
93 | |||
94 | public function crudInsertPrefix() { |
||
95 | return ''; |
||
96 | } |
||
97 | |||
98 | public function jsTreeNodeName(OIDplusObject $parent = null) { |
||
99 | if ($parent == null) return $this->objectTypeTitle(); |
||
100 | return $this->viewGetArcAsn1s($parent); |
||
101 | } |
||
102 | |||
103 | public function defaultTitle() { |
||
104 | return 'OID ' . $this->oid; |
||
105 | } |
||
106 | |||
16 | daniel-mar | 107 | public function isLeafNode() { |
108 | return false; |
||
109 | } |
||
110 | |||
34 | daniel-mar | 111 | public function getContentPage(&$title, &$content, &$icon) { |
55 | daniel-mar | 112 | $icon = file_exists(__DIR__.'/icon_big.png') ? 'plugins/objectTypes/'.basename(__DIR__).'/icon_big.png' : ''; |
113 | |||
2 | daniel-mar | 114 | if ($this->isRoot()) { |
115 | $title = OIDplusOid::objectTypeTitle(); |
||
116 | |||
150 | daniel-mar | 117 | $res = OIDplus::db()->query("select id from ".OIDPLUS_TABLENAME_PREFIX."objects where parent = ?", array(self::root())); |
2 | daniel-mar | 118 | if (OIDplus::db()->num_rows($res) > 0) { |
119 | $content = 'Please select an OID in the tree view at the left to show its contents.'; |
||
120 | } else { |
||
121 | $content = 'Currently, no OID is registered in the system.'; |
||
122 | } |
||
123 | |||
16 | daniel-mar | 124 | if (!$this->isLeafNode()) { |
125 | if (OIDplus::authUtils()::isAdminLoggedIn()) { |
||
126 | $content .= '<h2>Manage your root OIDs</h2>'; |
||
127 | } else { |
||
128 | $content .= '<h2>Root OIDs</h2>'; |
||
129 | } |
||
130 | $content .= '%%CRUD%%'; |
||
2 | daniel-mar | 131 | } |
132 | } else { |
||
192 | daniel-mar | 133 | $title = $this->getTitle(); |
134 | |||
103 | daniel-mar | 135 | $content = "<h2>Technical information</h2>".$this->oidInformation(). |
11 | daniel-mar | 136 | "<h2>Description</h2>%%DESC%%". |
6 | daniel-mar | 137 | "<h2>Registration Authority</h2>%%RA_INFO%%"; |
2 | daniel-mar | 138 | |
16 | daniel-mar | 139 | if (!$this->isLeafNode()) { |
140 | if ($this->userHasWriteRights()) { |
||
141 | $content .= '<h2>Create or change subsequent objects</h2>'; |
||
142 | } else { |
||
143 | $content .= '<h2>Subsequent objects</h2>'; |
||
144 | } |
||
145 | $content .= '%%CRUD%%'; |
||
2 | daniel-mar | 146 | } |
147 | } |
||
148 | } |
||
149 | |||
150 | # --- |
||
151 | |||
197 | daniel-mar | 152 | public function isWeid($allow_root) { |
61 | daniel-mar | 153 | $weid = WeidOidConverter::oid2weid($this->getDotNotation()); |
197 | daniel-mar | 154 | if (!$allow_root && ($weid === 'weid:4')) return false; |
196 | daniel-mar | 155 | return $weid !== false; |
2 | daniel-mar | 156 | } |
157 | |||
196 | daniel-mar | 158 | public function weidArc() { |
159 | $weid = WeidOidConverter::oid2weid($this->getDotNotation()); |
||
160 | if ($weid === false) return false; |
||
197 | daniel-mar | 161 | list($ns,$weid) = explode(':', $weid, 2); |
196 | daniel-mar | 162 | $x = explode('-', $weid); |
197 | daniel-mar | 163 | if (count($x) < 2) return ''; // WEID root arc. Has no name |
196 | daniel-mar | 164 | return $x[count($x)-2]; |
165 | } |
||
166 | |||
167 | public function getWeidNotation($withAbbr=true) { |
||
168 | $weid = WeidOidConverter::oid2weid($this->getDotNotation()); |
||
169 | if ($withAbbr) { |
||
170 | list($ns,$weid) = explode(':', $weid); |
||
171 | $weid_arcs = explode('-', $weid); |
||
172 | foreach ($weid_arcs as $i => &$weid) { |
||
173 | if ($i == count($weid_arcs)-1) { |
||
174 | $weid = '<abbr title="weLuhn check digit">'.$weid.'</abbr>'; |
||
175 | } else { |
||
176 | $oid_arcs = explode('.',$this->oid); |
||
177 | $weid_num = $oid_arcs[(count($oid_arcs)-1)-(count($weid_arcs)-1)+($i+1)]; |
||
178 | if ($weid_num != $weid) { |
||
179 | $weid = '<abbr title="Numeric value: '.$weid_num.'">'.$weid.'</abbr>'; |
||
180 | } |
||
181 | } |
||
182 | } |
||
183 | $weid = '<abbr title="Root arc: 1.3.6.1.4.1.37553.8">' . $ns . '</abbr>:' . implode('-',$weid_arcs); |
||
184 | } |
||
185 | return $weid; |
||
186 | } |
||
187 | |||
188 | private function oidInformation() { |
||
189 | $out = array(); |
||
190 | $out[] = "Dot notation: <code>" . $this->getDotNotation() . "</code>"; |
||
191 | $out[] = "ASN.1 notation: <code>{ " . $this->getAsn1Notation() . " }</code>"; |
||
192 | $out[] = "OID-IRI notation: <code>" . $this->getIriNotation() . "</code>"; |
||
197 | daniel-mar | 193 | if ($this->isWeid(true)) { |
196 | daniel-mar | 194 | $out[] = "WEID notation: <code>" . $this->getWeidNotation() . "</code>"; |
195 | } |
||
196 | return '<p>'.implode('<br>',$out).'</p>'; |
||
197 | } |
||
198 | |||
2 | daniel-mar | 199 | public function __clone() { |
200 | return new self($this->oid); |
||
201 | } |
||
202 | |||
203 | public function appendArcs(String $arcs) { |
||
204 | $out = clone $this; |
||
5 | daniel-mar | 205 | |
2 | daniel-mar | 206 | if ($out->isRoot()) { |
207 | $out->oid .= $arcs; |
||
208 | } else { |
||
209 | $out->oid .= '.' . $arcs; |
||
210 | } |
||
5 | daniel-mar | 211 | |
13 | daniel-mar | 212 | $bak_oid = $out->oid; |
5 | daniel-mar | 213 | $out->oid = sanitizeOID($out->oid); |
13 | daniel-mar | 214 | if ($out->oid === false) throw new Exception("$bak_oid is not a valid OID!"); |
5 | daniel-mar | 215 | |
2 | daniel-mar | 216 | return $out; |
217 | } |
||
218 | |||
219 | public function deltaDotNotation(OIDplusOid $parent) { |
||
220 | if (!$parent->isRoot()) { |
||
221 | if (substr($this->oid, 0, strlen($parent->oid)+1) == $parent->oid.'.') { |
||
222 | return substr($this->oid, strlen($parent->oid)+1); |
||
223 | } else { |
||
224 | return false; |
||
225 | } |
||
226 | } else { |
||
227 | return $this->oid; |
||
228 | } |
||
229 | } |
||
230 | |||
231 | public function viewGetArcAsn1s(OIDplusOid $parent=null, $separator = ' | ') { |
||
232 | $asn_ids = array(); |
||
233 | |||
234 | if (is_null($parent)) $parent = OIDplusOid::parse('oid:'); |
||
235 | |||
236 | $part = $this->deltaDotNotation($parent); |
||
237 | |||
238 | if (strpos($part, '.') === false) { |
||
150 | daniel-mar | 239 | $res2 = OIDplus::db()->query("select name from ".OIDPLUS_TABLENAME_PREFIX."asn1id where oid = ? order by lfd", array("oid:".$this->oid)); |
2 | daniel-mar | 240 | while ($row2 = OIDplus::db()->fetch_array($res2)) { |
241 | $asn_ids[] = $row2['name'].'('.$part.')'; |
||
242 | } |
||
243 | } |
||
244 | |||
245 | if (count($asn_ids) == 0) $asn_ids = array($part); |
||
246 | return implode($asn_ids, $separator); |
||
247 | } |
||
248 | |||
249 | public function getAsn1Notation($withAbbr=true) { |
||
250 | $asn1_notation = ''; |
||
251 | $arcs = explode('.', $this->oid); |
||
252 | |||
253 | foreach ($arcs as $arc) { |
||
150 | daniel-mar | 254 | $res = OIDplus::db()->query("select name, standardized from ".OIDPLUS_TABLENAME_PREFIX."asn1id where oid = ? order by lfd", array('oid:'.implode('.',$arcs))); |
2 | daniel-mar | 255 | |
256 | $names = array(); |
||
257 | while ($row = OIDplus::db()->fetch_array($res)) { |
||
51 | daniel-mar | 258 | $names[] = $row['name']."(".end($arcs).")"; |
259 | if ($row['standardized']) { |
||
260 | $names[] = $row['name']; |
||
261 | } |
||
2 | daniel-mar | 262 | } |
263 | |||
52 | daniel-mar | 264 | $numeric = array_pop($arcs); |
2 | daniel-mar | 265 | if (count($names) > 1) { |
266 | $first_name = array_shift($names); |
||
52 | daniel-mar | 267 | $abbr = 'Other identifiers: '.implode(' ',$names); |
2 | daniel-mar | 268 | if ($withAbbr) { |
51 | daniel-mar | 269 | $asn1_notation = '<abbr title="'.$abbr.'">'.$first_name.'</abbr> '.$asn1_notation; |
2 | daniel-mar | 270 | } else { |
51 | daniel-mar | 271 | $asn1_notation = $first_name.' '.$asn1_notation; |
2 | daniel-mar | 272 | } |
273 | } else if (count($names) == 1) { |
||
51 | daniel-mar | 274 | $asn1_notation = array_shift($names).' '.$asn1_notation; |
2 | daniel-mar | 275 | } else { |
52 | daniel-mar | 276 | $asn1_notation = $numeric.' '.$asn1_notation; |
2 | daniel-mar | 277 | } |
278 | } |
||
279 | |||
280 | return $asn1_notation; |
||
281 | } |
||
282 | |||
283 | public function getIriNotation($withAbbr=true) { |
||
284 | $iri_notation = ''; |
||
285 | $arcs = explode('.', $this->oid); |
||
286 | |||
287 | foreach ($arcs as $arc) { |
||
150 | daniel-mar | 288 | $res = OIDplus::db()->query("select name, longarc from ".OIDPLUS_TABLENAME_PREFIX."iri where oid = ? order by lfd", array('oid:'.implode('.',$arcs))); |
2 | daniel-mar | 289 | |
290 | $is_longarc = false; |
||
291 | $names = array(); |
||
292 | while ($row = OIDplus::db()->fetch_array($res)) { |
||
293 | $is_longarc = $row['longarc']; |
||
294 | $names[] = $row['name']; |
||
51 | daniel-mar | 295 | |
296 | if ($is_longarc) { |
||
297 | $names[] = 'Joint-ISO-ITU-T/'.$row['name']; // Long arcs can only be inside root OID 2 |
||
298 | } |
||
2 | daniel-mar | 299 | } |
300 | |||
301 | $names[] = array_pop($arcs); |
||
302 | if (count($names) > 2) { |
||
303 | $first_name = array_shift($names); |
||
52 | daniel-mar | 304 | $numeric = array_pop($names); |
305 | $abbr = 'Other identifiers: '.implode(' ',$names).' Numeric value: '.$numeric; |
||
2 | daniel-mar | 306 | $iri_notation = $withAbbr ? '<abbr title="'.$abbr.'">'.$first_name.'</abbr>/'.$iri_notation : $first_name.'/'.$iri_notation; |
307 | } else if (count($names) > 1) { |
||
308 | $first_name = array_shift($names); |
||
309 | $abbr = 'Numeric value: '.array_shift($names); |
||
310 | $iri_notation = $withAbbr ? '<abbr title="'.$abbr.'">'.$first_name.'</abbr>/'.$iri_notation : $first_name.'/'.$iri_notation; |
||
311 | } else if (count($names) == 1) { |
||
312 | $iri_notation = array_shift($names) . '/' . $iri_notation; |
||
313 | } |
||
314 | |||
51 | daniel-mar | 315 | if ($is_longarc) break; // we don't write /ITU-T/ at the beginning, when /ITU-T/xxx is a long arc |
2 | daniel-mar | 316 | } |
317 | $iri_notation = '/' . substr($iri_notation, 0, strlen($iri_notation)-1); |
||
318 | |||
319 | return $iri_notation; |
||
320 | } |
||
321 | |||
322 | public function getDotNotation() { |
||
323 | return $this->oid; |
||
324 | } |
||
325 | |||
13 | daniel-mar | 326 | public function isWellKnown() { |
150 | daniel-mar | 327 | $res = OIDplus::db()->query("select oid from ".OIDPLUS_TABLENAME_PREFIX."asn1id where oid = ? and well_known = 1", array("oid:".$this->oid)); |
13 | daniel-mar | 328 | if (OIDplus::db()->num_rows($res) > 0) return true; |
329 | |||
150 | daniel-mar | 330 | $res = OIDplus::db()->query("select oid from ".OIDPLUS_TABLENAME_PREFIX."iri where oid = ? and well_known = 1", array("oid:".$this->oid)); |
13 | daniel-mar | 331 | if (OIDplus::db()->num_rows($res) > 0) return true; |
332 | |||
333 | return false; |
||
334 | } |
||
335 | |||
147 | daniel-mar | 336 | public function replaceAsn1Ids($demandedASN1s=array(), $simulate=false) { |
13 | daniel-mar | 337 | if ($this->isWellKnown()) { |
338 | throw new Exception("OID ".$this->oid." is a 'well-known' OID. Its identifiers cannot be changed."); |
||
339 | } |
||
340 | |||
2 | daniel-mar | 341 | // First do a few checks |
342 | foreach ($demandedASN1s as &$asn1) { |
||
343 | $asn1 = trim($asn1); |
||
344 | |||
13 | daniel-mar | 345 | // Validate identifier |
346 | if (!oid_id_is_valid($asn1)) throw new Exception("'$asn1' is not a valid ASN.1 identifier!"); |
||
347 | |||
2 | daniel-mar | 348 | // Check if the (real) parent has any conflict |
150 | daniel-mar | 349 | $res = OIDplus::db()->query("select oid from ".OIDPLUS_TABLENAME_PREFIX."asn1id where name = ?", array($asn1)); |
2 | daniel-mar | 350 | while ($row = OIDplus::db()->fetch_array($res)) { |
351 | $check_oid = OIDplusOid::parse($row['oid'])->oid; |
||
352 | if ((oid_up($check_oid) === oid_up($this->oid)) && // same parent |
||
353 | ($check_oid !== $this->oid)) // different OID |
||
354 | { |
||
355 | throw new Exception("ASN.1 identifier '$asn1' is already used by another OID ($check_oid)"); |
||
356 | } |
||
357 | } |
||
358 | } |
||
359 | |||
360 | // Now do the real replacement |
||
147 | daniel-mar | 361 | if (!$simulate) { |
150 | daniel-mar | 362 | OIDplus::db()->query("delete from ".OIDPLUS_TABLENAME_PREFIX."asn1id where oid = ?", array("oid:".$this->oid)); |
147 | daniel-mar | 363 | foreach ($demandedASN1s as &$asn1) { |
150 | daniel-mar | 364 | if (!OIDplus::db()->query("insert into ".OIDPLUS_TABLENAME_PREFIX."asn1id (oid, name) values (?, ?)", array("oid:".$this->oid, $asn1))) { |
147 | daniel-mar | 365 | throw new Exception("Insertion of ASN.1 ID $asn1 to OID ".$this->oid." failed!"); |
366 | } |
||
2 | daniel-mar | 367 | } |
368 | } |
||
369 | } |
||
370 | |||
147 | daniel-mar | 371 | public function replaceIris($demandedIris=array(), $simulate=false) { |
13 | daniel-mar | 372 | if ($this->isWellKnown()) { |
373 | throw new Exception("OID ".$this->oid." is a 'well-known' OID. Its identifiers cannot be changed."); |
||
374 | } |
||
375 | |||
2 | daniel-mar | 376 | // First do a few checks |
377 | foreach ($demandedIris as &$iri) { |
||
378 | $iri = trim($iri); |
||
379 | |||
13 | daniel-mar | 380 | // Validate identifier |
381 | if (!iri_arc_valid($iri, false)) throw new Exception("'$iri' is not a valid IRI!"); |
||
382 | |||
2 | daniel-mar | 383 | // Check if the (real) parent has any conflict |
150 | daniel-mar | 384 | $res = OIDplus::db()->query("select oid from ".OIDPLUS_TABLENAME_PREFIX."iri where name = ?", array($iri)); |
2 | daniel-mar | 385 | while ($row = OIDplus::db()->fetch_array($res)) { |
386 | $check_oid = OIDplusOid::parse($row['oid'])->oid; |
||
387 | if ((oid_up($check_oid) === oid_up($this->oid)) && // same parent |
||
388 | ($check_oid !== $this->oid)) // different OID |
||
389 | { |
||
390 | throw new Exception("IRI '$iri' is already used by another OID ($check_oid)"); |
||
391 | } |
||
392 | } |
||
393 | } |
||
394 | |||
395 | // Now do the real replacement |
||
147 | daniel-mar | 396 | if (!$simulate) { |
150 | daniel-mar | 397 | OIDplus::db()->query("delete from ".OIDPLUS_TABLENAME_PREFIX."iri where oid = ?", array("oid:".$this->oid)); |
147 | daniel-mar | 398 | foreach ($demandedIris as &$iri) { |
150 | daniel-mar | 399 | if (!OIDplus::db()->query("insert into ".OIDPLUS_TABLENAME_PREFIX."iri (oid, name) values (?, ?)", array("oid:".$this->oid, $iri))) { |
147 | daniel-mar | 400 | throw new Exception("Insertion of IRI $iri to OID ".$this->oid." failed!"); |
401 | } |
||
2 | daniel-mar | 402 | } |
403 | } |
||
404 | } |
||
12 | daniel-mar | 405 | |
20 | daniel-mar | 406 | public function one_up() { |
407 | return self::parse(self::ns().':'.oid_up($this->oid)); |
||
12 | daniel-mar | 408 | } |
409 | |||
410 | public function distance($to) { |
||
411 | if (!is_object($to)) $to = OIDplusObject::parse($to); |
||
28 | daniel-mar | 412 | if (!($to instanceof $this)) return false; |
12 | daniel-mar | 413 | return oid_distance($to->oid, $this->oid); |
414 | } |
||
192 | daniel-mar | 415 | |
193 | daniel-mar | 416 | public function getAltIds() { |
417 | if ($this->isRoot()) return array(); |
||
418 | $ids = parent::getAltIds(); |
||
419 | if ($uuid = oid_to_uuid($this->oid)) { |
||
420 | $ids[] = array('guid', $uuid, 'GUID representation of this OID'); |
||
421 | } |
||
422 | $ids[] = array('guid', gen_uuid_md5_namebased(UUID_NAMEBASED_NS_OID, $this->oid), 'Namebased version 3 / MD5 UUID with namespace UUID_NAMEBASED_NS_OID'); |
||
423 | $ids[] = array('guid', gen_uuid_sha1_namebased(UUID_NAMEBASED_NS_OID, $this->oid), 'Namebased version 5 / SHA1 UUID with namespace UUID_NAMEBASED_NS_OID'); |
||
424 | return $ids; |
||
192 | daniel-mar | 425 | } |
2 | daniel-mar | 426 | } |
427 | |||
61 | daniel-mar | 428 | OIDplus::registerObjectType('OIDplusOid'); |