Subversion Repositories oidplus

Rev

Rev 644 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

  1. <?php
  2.  
  3. /*
  4.  * OIDplus 2.0
  5.  * Copyright 2019 - 2021 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.  
  20. if (!defined('INSIDE_OIDPLUS')) die();
  21.  
  22. class OIDplusOid extends OIDplusObject {
  23.         private $oid;
  24.  
  25.         public function __construct($oid) {
  26.                 $bak_oid = $oid;
  27.  
  28.                 $oid = sanitizeOID($oid, 'auto');
  29.                 if ($oid === false) {
  30.                         throw new OIDplusException(_L('Invalid OID %1',$bak_oid));
  31.                 }
  32.  
  33.                 if (($oid != '') && (!oid_valid_dotnotation($oid, false, true, 0))) {
  34.                         // avoid OIDs like 3.0
  35.                         throw new OIDplusException(_L('Invalid OID %1',$bak_oid));
  36.                 }
  37.  
  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 _L('Object Identifier (OID)');
  49.         }
  50.  
  51.         public static function objectTypeTitleShort() {
  52.                 return _L('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($with_ns=true) {
  68.                 return $with_ns ? 'oid:'.$this->oid : $this->oid;
  69.         }
  70.  
  71.         public function addString($str) {
  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).'));
  74.                 }
  75.  
  76.                 return $this->appendArcs($str)->nodeId();
  77.         }
  78.  
  79.         public function crudShowId(OIDplusObject $parent) {
  80.                 if ($parent instanceof OIDplusOid) {
  81.                         return $this->deltaDotNotation($parent);
  82.                 }
  83.         }
  84.  
  85.         public function crudInsertPrefix() {
  86.                 return '';
  87.         }
  88.  
  89.         public function jsTreeNodeName(OIDplusObject $parent = null) {
  90.                 if ($parent == null) return $this->objectTypeTitle();
  91.                 if ($parent instanceof OIDplusOid) {
  92.                         return $this->viewGetArcAsn1s($parent);
  93.                 } else {
  94.                         return '';
  95.                 }
  96.         }
  97.  
  98.         public function defaultTitle() {
  99.                 return _L('OID %1',$this->oid);
  100.         }
  101.  
  102.         public function isLeafNode() {
  103.                 return false;
  104.         }
  105.  
  106.         public function getContentPage(&$title, &$content, &$icon) {
  107.                 $icon = file_exists(__DIR__.'/icon_big.png') ? OIDplus::webPath(__DIR__,true).'/icon_big.png' : '';
  108.  
  109.                 if ($this->isRoot()) {
  110.                         $title = OIDplusOid::objectTypeTitle();
  111.  
  112.                         $res = OIDplus::db()->query("select id from ###objects where parent = ?", array(self::root()));
  113.                         if ($res->num_rows() > 0) {
  114.                                 $content = _L('Please select an OID in the tree view at the left to show its contents.');
  115.                         } else {
  116.                                 $content = _L('Currently, no OID is registered in the system.');
  117.                         }
  118.  
  119.                         if (!$this->isLeafNode()) {
  120.                                 if (OIDplus::authUtils()->isAdminLoggedIn()) {
  121.                                         $content .= '<h2>'._L('Manage your root OIDs').'</h2>';
  122.                                 } else {
  123.                                         $content .= '<h2>'._L('Root OIDs').'</h2>';
  124.                                 }
  125.                                 $content .= '%%CRUD%%';
  126.                         }
  127.                 } else {
  128.                         $title = $this->getTitle();
  129.  
  130.                         $content = '<h2>'._L('Technical information').'</h2>'.$this->oidInformation().
  131.                                    '<h2>'._L('Description').'</h2>%%DESC%%'.
  132.                                    '<h2>'._L('Registration Authority').'</h2>%%RA_INFO%%';
  133.  
  134.                         if (!$this->isLeafNode()) {
  135.                                 if ($this->userHasWriteRights()) {
  136.                                         $content .= '<h2>'._L('Create or change subsequent objects').'</h2>';
  137.                                 } else {
  138.                                         $content .= '<h2>'._L('Subsequent objects').'</h2>';
  139.                                 }
  140.                                 $content .= '%%CRUD%%';
  141.                         }
  142.                 }
  143.         }
  144.  
  145.         # ---
  146.  
  147.         public function isWeid($allow_root) {
  148.                 $weid = WeidOidConverter::oid2weid($this->getDotNotation());
  149.                 if (!$allow_root && ($weid === 'weid:4')) return false;
  150.                 return $weid !== false;
  151.         }
  152.  
  153.         public function weidArc() {
  154.                 $weid = WeidOidConverter::oid2weid($this->getDotNotation());
  155.                 if ($weid === false) return false;
  156.                 list($ns,$weid) = explode(':', $weid, 2);
  157.                 $x = explode('-', $weid);
  158.                 if (count($x) < 2) return ''; // WEID root arc. Has no name
  159.                 return $x[count($x)-2];
  160.         }
  161.  
  162.         public function getWeidNotation($withAbbr=true) {
  163.                 $weid = WeidOidConverter::oid2weid($this->getDotNotation());
  164.                 if ($withAbbr) {
  165.                         list($ns,$weid) = explode(':', $weid);
  166.                         $weid_arcs = explode('-', $weid);
  167.                         foreach ($weid_arcs as $i => &$weid) {
  168.                                 if ($i == count($weid_arcs)-1) {
  169.                                         $weid = '<abbr title="'._L('weLuhn check digit').'">'.$weid.'</abbr>';
  170.                                 } else {
  171.                                         $oid_arcs = explode('.',$this->oid);
  172.                                         $weid_num = $oid_arcs[(count($oid_arcs)-1)-(count($weid_arcs)-1)+($i+1)];
  173.                                         if ($weid_num != $weid) {
  174.                                                 $weid = '<abbr title="'._L('Numeric value').': '.$weid_num.'">'.$weid.'</abbr>';
  175.                                         }
  176.                                 }
  177.                         }
  178.                         $weid = '<abbr title="'._L('Root arc').': 1.3.6.1.4.1.37553.8">' . $ns . '</abbr>:' . implode('-',$weid_arcs);
  179.                 }
  180.                 return $weid;
  181.         }
  182.  
  183.         private function oidInformation() {
  184.                 $out = array();
  185.                 $out[] = _L('Dot notation').': <code>' . $this->getDotNotation() . '</code>';
  186.                 $out[] = _L('ASN.1 notation').': <code>' . $this->getAsn1Notation(true) . '</code>';
  187.                 $out[] = _L('OID-IRI notation').': <code>' . $this->getIriNotation(true) . '</code>';
  188.                 if ($this->isWeid(true)) {
  189.                         $out[] = _L('WEID notation').': <code>' . $this->getWeidNotation() . '</code>';
  190.                 }
  191.                 return '<p>'.implode('<br>',$out).'</p>';
  192.         }
  193.  
  194.         public function appendArcs(String $arcs) {
  195.                 $out = new self($this->oid);
  196.  
  197.                 if ($out->isRoot()) {
  198.                         $out->oid .= $arcs;
  199.                 } else {
  200.                         $out->oid .= '.' . $arcs;
  201.                 }
  202.  
  203.                 $bak_oid = $out->oid;
  204.                 $out->oid = sanitizeOID($out->oid);
  205.                 if ($out->oid === false) throw new OIDplusException(_L('%1 is not a valid OID!',$bak_oid));
  206.  
  207.                 $maxlen = OIDplus::baseConfig()->getValue('LIMITS_MAX_ID_LENGTH')-strlen('oid:');
  208.                 if (strlen($out->oid) > $maxlen) {
  209.                         throw new OIDplusException(_L('The resulting OID "%1" is too long (max allowed length: %2).',$out->oid,$maxlen));
  210.                 }
  211.  
  212.                 $depth = 0;
  213.                 foreach (explode('.',$out->oid) as $arc) {
  214.                         if (strlen($arc) > OIDplus::baseConfig()->getValue('LIMITS_MAX_OID_ARC_SIZE')) {
  215.                                 $maxlen = OIDplus::baseConfig()->getValue('LIMITS_MAX_OID_ARC_SIZE');
  216.                                 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));
  217.                         }
  218.                         $depth++;
  219.                 }
  220.                 if ($depth > OIDplus::baseConfig()->getValue('LIMITS_MAX_OID_DEPTH')) {
  221.                         $maxdepth = OIDplus::baseConfig()->getValue('LIMITS_MAX_OID_DEPTH');
  222.                         throw new OIDplusException(_L('OID %1 has too many arcs (current depth %2, max depth %3)',$out->oid,$depth,$maxdepth));
  223.                 }
  224.  
  225.                 return $out;
  226.         }
  227.  
  228.         public function deltaDotNotation(OIDplusOid $parent) {
  229.                 if (!$parent->isRoot()) {
  230.                         if (substr($this->oid, 0, strlen($parent->oid)+1) == $parent->oid.'.') {
  231.                                 return substr($this->oid, strlen($parent->oid)+1);
  232.                         } else {
  233.                                 return false;
  234.                         }
  235.                 } else {
  236.                         return $this->oid;
  237.                 }
  238.         }
  239.  
  240.         public function viewGetArcAsn1s(OIDplusOid $parent=null, $separator = ' | ') {
  241.                 $asn_ids = array();
  242.  
  243.                 if (is_null($parent)) $parent = OIDplusOid::parse('oid:');
  244.  
  245.                 $part = $this->deltaDotNotation($parent);
  246.  
  247.                 if (strpos($part, '.') === false) {
  248.                         $res2 = OIDplus::db()->query("select name from ###asn1id where oid = ? order by lfd", array("oid:".$this->oid));
  249.                         while ($row2 = $res2->fetch_array()) {
  250.                                 $asn_ids[] = $row2['name'].'('.$part.')';
  251.                         }
  252.                 }
  253.  
  254.                 if (count($asn_ids) == 0) $asn_ids = array($part);
  255.                 return implode($separator, $asn_ids);
  256.         }
  257.  
  258.         public function getAsn1Notation($withAbbr=true) {
  259.                 $asn1_notation = '';
  260.                 $arcs = explode('.', $this->oid);
  261.  
  262.                 foreach ($arcs as $arc) {
  263.                         $res = OIDplus::db()->query("select name, standardized from ###asn1id where oid = ? order by lfd", array('oid:'.implode('.',$arcs)));
  264.  
  265.                         $names = array();
  266.                         while ($row = $res->fetch_array()) {
  267.                                 $names[] = $row['name']."(".end($arcs).")";
  268.                                 if ($row['standardized']) {
  269.                                         $names[] = $row['name'];
  270.                                 }
  271.                         }
  272.  
  273.                         $numeric = array_pop($arcs);
  274.                         if (count($names) > 1) {
  275.                                 $first_name = array_shift($names);
  276.                                 $abbr = _L('Other identifiers').':&#10;      '.implode('&#10;      ',$names);
  277.                                 if ($withAbbr) {
  278.                                         $asn1_notation = '<abbr title="'.$abbr.'">'.$first_name.'</abbr> '.$asn1_notation;
  279.                                 } else {
  280.                                         $asn1_notation = $first_name.' '.$asn1_notation;
  281.                                 }
  282.                         } else if (count($names) == 1) {
  283.                                 $asn1_notation = array_shift($names).' '.$asn1_notation;
  284.                         } else {
  285.                                 $asn1_notation = $numeric.' '.$asn1_notation;
  286.                         }
  287.                 }
  288.  
  289.                 return "{ $asn1_notation }";
  290.         }
  291.  
  292.         public function getIriNotation($withAbbr=true) {
  293.                 $iri_notation = '';
  294.                 $arcs = explode('.', $this->oid);
  295.  
  296.                 foreach ($arcs as $arc) {
  297.                         $res = OIDplus::db()->query("select name, longarc from ###iri where oid = ? order by lfd", array('oid:'.implode('.',$arcs)));
  298.  
  299.                         $is_longarc = false;
  300.                         $names = array();
  301.                         while ($row = $res->fetch_array()) {
  302.                                 $is_longarc = $row['longarc'];
  303.                                 $names[] = $row['name'];
  304.  
  305.                                 if ($is_longarc) {
  306.                                         $names[] = 'Joint-ISO-ITU-T/'.$row['name']; // Long arcs can only be inside root OID 2
  307.                                 }
  308.                         }
  309.  
  310.                         $names[] = array_pop($arcs);
  311.                         if (count($names) > 2) {
  312.                                 $first_name = array_shift($names);
  313.                                 $numeric = array_pop($names);
  314.                                 $abbr = _L('Other identifiers').':&#10;      '.implode('&#10;      ',$names).'&#10;'._L('Numeric value').': '.$numeric;
  315.                                 $iri_notation = $withAbbr ? '<abbr title="'.$abbr.'">'.$first_name.'</abbr>/'.$iri_notation : $first_name.'/'.$iri_notation;
  316.                         } else if (count($names) > 1) {
  317.                                 $first_name = array_shift($names);
  318.                                 $abbr = _L('Numeric value').': '.array_shift($names);
  319.                                 $iri_notation = $withAbbr ? '<abbr title="'.$abbr.'">'.$first_name.'</abbr>/'.$iri_notation : $first_name.'/'.$iri_notation;
  320.                         } else if (count($names) == 1) {
  321.                                 $iri_notation = array_shift($names) . '/' . $iri_notation;
  322.                         }
  323.  
  324.                         if ($is_longarc) break; // we don't write /ITU-T/ at the beginning, when /ITU-T/xyz is a long arc
  325.                 }
  326.                 $iri_notation = '/' . substr($iri_notation, 0, strlen($iri_notation)-1);
  327.  
  328.                 return $iri_notation;
  329.         }
  330.  
  331.         public function getDotNotation() {
  332.                 return $this->oid;
  333.         }
  334.  
  335.         public function isWellKnown() {
  336.                 $res = OIDplus::db()->query("select oid from ###asn1id where oid = ? and well_known = ?", array("oid:".$this->oid,true));
  337.                 if ($res->num_rows() > 0) return true;
  338.  
  339.                 $res = OIDplus::db()->query("select oid from ###iri where oid = ? and well_known = ?", array("oid:".$this->oid,true));
  340.                 if ($res->num_rows() > 0) return true;
  341.  
  342.                 return false;
  343.         }
  344.  
  345.         public function replaceAsn1Ids($demandedASN1s=array(), $simulate=false) {
  346.                 if ($this->isWellKnown()) {
  347.                         throw new OIDplusException(_L('OID "%1" is a "well-known" OID. Its identifiers cannot be changed.',$this->oid));
  348.                 }
  349.  
  350.                 // First do a few checks
  351.                 foreach ($demandedASN1s as &$asn1) {
  352.                         $asn1 = trim($asn1);
  353.  
  354.                         if (strlen($asn1) > OIDplus::baseConfig()->getValue('LIMITS_MAX_OID_ASN1_ID_LEN')) {
  355.                                 $maxlen = OIDplus::baseConfig()->getValue('LIMITS_MAX_OID_ASN1_ID_LEN');
  356.                                 throw new OIDplusException(_L('ASN.1 alphanumeric identifier "%1" is too long (max allowed length %2)',$asn1,$maxlen));
  357.                         }
  358.  
  359.                         // Validate identifier
  360.                         if (!oid_id_is_valid($asn1)) throw new OIDplusException(_L('"%1" is not a valid ASN.1 identifier!',$asn1));
  361.  
  362.                         // Check if the (real) parent has any conflict
  363.                         // Unlike IRI identifiers, ASN.1 identifiers may be used multiple times (not recommended), except if one of them is standardized
  364.                         $res = OIDplus::db()->query("select oid from ###asn1id where name = ? and standardized = ?", array($asn1,true));
  365.                         while ($row = $res->fetch_array()) {
  366.                                 $check_oid = OIDplusOid::parse($row['oid'])->oid;
  367.                                 if ((oid_up($check_oid) === oid_up($this->oid)) && // same parent
  368.                                    ($check_oid !== $this->oid))                    // different OID
  369.                                 {
  370.                                         throw new OIDplusException(_L('ASN.1 identifier "%1" is a standardized identifier belonging to OID %2',$asn1,$check_oid));
  371.                                 }
  372.                         }
  373.                 }
  374.  
  375.                 // Now do the real replacement
  376.                 if (!$simulate) {
  377.                         OIDplus::db()->query("delete from ###asn1id where oid = ?", array("oid:".$this->oid));
  378.                         foreach ($demandedASN1s as &$asn1) {
  379.                                 OIDplus::db()->query("insert into ###asn1id (oid, name) values (?, ?)", array("oid:".$this->oid, $asn1));
  380.                         }
  381.                 }
  382.         }
  383.  
  384.         public function replaceIris($demandedIris=array(), $simulate=false) {
  385.                 if ($this->isWellKnown()) {
  386.                         throw new OIDplusException(_L('OID "%1" is a "well-known" OID. Its identifiers cannot be changed.',$this->oid));
  387.                 }
  388.  
  389.                 // First do a few checks
  390.                 foreach ($demandedIris as &$iri) {
  391.                         $iri = trim($iri);
  392.  
  393.                         if (strlen($iri) > OIDplus::baseConfig()->getValue('LIMITS_MAX_OID_UNICODE_LABEL_LEN')) {
  394.                                 $maxlen = OIDplus::baseConfig()->getValue('LIMITS_MAX_OID_UNICODE_LABEL_LEN');
  395.                                 throw new OIDplusException(_L('Unicode label "%1" is too long (max allowed length %2)',$iri,$maxlen));
  396.                         }
  397.  
  398.                         // Validate identifier
  399.                         if (!iri_arc_valid($iri, false)) throw new OIDplusException(_L('"%1" is not a valid IRI!',$iri));
  400.  
  401.                         // Check if the (real) parent has any conflict
  402.                         $res = OIDplus::db()->query("select oid from ###iri where name = ?", array($iri));
  403.                         while ($row = $res->fetch_array()) {
  404.                                 $check_oid = OIDplusOid::parse($row['oid'])->oid;
  405.                                 if ((oid_up($check_oid) === oid_up($this->oid)) && // same parent
  406.                                    ($check_oid !== $this->oid))                    // different OID
  407.                                 {
  408.                                         throw new OIDplusException(_L('IRI "%1" is already used by another OID (%2)',$iri,$check_oid));
  409.                                 }
  410.                         }
  411.                 }
  412.  
  413.                 // Now do the real replacement
  414.                 if (!$simulate) {
  415.                         OIDplus::db()->query("delete from ###iri where oid = ?", array("oid:".$this->oid));
  416.                         foreach ($demandedIris as &$iri) {
  417.                                 OIDplus::db()->query("insert into ###iri (oid, name) values (?, ?)", array("oid:".$this->oid, $iri));
  418.                         }
  419.                 }
  420.         }
  421.  
  422.         public function one_up() {
  423.                 return self::parse(self::ns().':'.oid_up($this->oid));
  424.         }
  425.  
  426.         public function distance($to) {
  427.                 if (!is_object($to)) $to = OIDplusObject::parse($to);
  428.                 if (!($to instanceof $this)) return false;
  429.                 return oid_distance($to->oid, $this->oid);
  430.         }
  431.  
  432.         public function getAltIds() {
  433.                 if ($this->isRoot()) return array();
  434.                 $ids = parent::getAltIds();
  435.                 if ($uuid = oid_to_uuid($this->oid)) {
  436.                         $ids[] = new OIDplusAltId('guid', $uuid, _L('GUID representation of this OID'));
  437.                 }
  438.                 $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'));
  439.                 $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'));
  440.                 return $ids;
  441.         }
  442.  
  443.         public function getDirectoryName() {
  444.                 if ($this->isRoot()) return $this->ns();
  445.                 $oid = $this->nodeId(false);
  446.                 return $this->ns().'_'.str_replace('.', '_', $oid);
  447.         }
  448. }
  449.