Subversion Repositories oidplus

Rev

Rev 1391 | Rev 1420 | 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 - 2023 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. namespace ViaThinkSoft\OIDplus;
  21.  
  22. // phpcs:disable PSR1.Files.SideEffects
  23. \defined('INSIDE_OIDPLUS') or die;
  24. // phpcs:enable PSR1.Files.SideEffects
  25.  
  26. class OIDplusAid extends OIDplusObject {
  27.         /**
  28.          * @var string
  29.          */
  30.         private $aid;
  31.  
  32.         /**
  33.          * @param string $aid
  34.          */
  35.         public function __construct(string $aid) {
  36.                 // TODO: syntax checks
  37.                 $this->aid = $aid;
  38.         }
  39.  
  40.         /**
  41.          * @param string $node_id
  42.          * @return OIDplusAid|null
  43.          */
  44.         public static function parse(string $node_id)/*: ?OIDplusAid*/ {
  45.                 @list($namespace, $aid) = explode(':', $node_id, 2);
  46.                 if ($namespace !== self::ns()) return null;
  47.                 return new self($aid);
  48.         }
  49.  
  50.         /**
  51.          * @return string
  52.          */
  53.         public static function objectTypeTitle(): string {
  54.                 return _L('Application Identifier (ISO/IEC 7816)');
  55.         }
  56.  
  57.         /**
  58.          * @return string
  59.          */
  60.         public static function objectTypeTitleShort(): string {
  61.                 return _L('AID');
  62.         }
  63.  
  64.         /**
  65.          * @return string
  66.          */
  67.         public static function ns(): string {
  68.                 return 'aid';
  69.         }
  70.  
  71.         /**
  72.          * @return string
  73.          */
  74.         public static function root(): string {
  75.                 return self::ns().':';
  76.         }
  77.  
  78.         /**
  79.          * @return bool
  80.          */
  81.         public function isRoot(): bool {
  82.                 return $this->aid == '';
  83.         }
  84.  
  85.         /**
  86.          * @param bool $with_ns
  87.          * @return string
  88.          */
  89.         public function nodeId(bool $with_ns=true): string {
  90.                 return $with_ns ? self::root().$this->aid : $this->aid;
  91.         }
  92.  
  93.         /**
  94.          * @param string $str
  95.          * @return string
  96.          * @throws OIDplusException
  97.          */
  98.         public function addString(string $str): string {
  99.                 $m = array();
  100.  
  101.                 $str = str_replace(' ','',$str);
  102.                 $str = str_replace(':','',$str);
  103.  
  104.                 if (!preg_match('@^[0-9a-fA-F]+$@', $str, $m)) {
  105.                         throw new OIDplusException(_L('AID part needs to be hexadecimal'));
  106.                 }
  107.  
  108.                 if (strlen($this->nodeId(false).$str) > 32) {
  109.                         throw new OIDplusException(_L('An AID has a maximum length of 16 bytes'));
  110.                 }
  111.  
  112.                 // removed, because for D2 76 00 01 86 F... it makes sense to have your root (which is inside a foreign RID) being your OIDplus root
  113.                 /*
  114.                 $pre   = $this->nodeId(false);
  115.                 $add   = strtoupper($str);
  116.                 $after = $pre.$add;
  117.                 $rid = '?';
  118.                 $pix = '?';
  119.                 $p = aid_split_rid_pix($after, $rid, $pix);
  120.                 if ($p > 1) { // Why $p>1? For "F", there is no RID. We allow that somebody include "F" in the first node
  121.                         if ((strlen($pre)<$p) && (strlen($after)>$p)) {
  122.                                 $rid = substr($rid,strlen($pre));
  123.                                 throw new OIDplusException(_L('This node would mix RID (registry ID) and PIX (application specific). Please split it into two nodes "%1" and "%2".',$rid,$pix));
  124.                         }
  125.                 }
  126.                 */
  127.  
  128.                 return $this->nodeId(true).strtoupper($str);
  129.         }
  130.  
  131.         /**
  132.          * @param OIDplusObject $parent
  133.          * @return string
  134.          * @throws OIDplusException
  135.          */
  136.         public function crudShowId(OIDplusObject $parent): string {
  137.                 return $this->chunkedNotation(false);
  138.         }
  139.  
  140.         /**
  141.          * @return string
  142.          * @throws OIDplusException
  143.          */
  144.         public function crudInsertPrefix(): string {
  145.                 return $this->isRoot() ? '' : $this->chunkedNotation(false);
  146.         }
  147.  
  148.         /**
  149.          * @param OIDplusObject|null $parent
  150.          * @return string
  151.          */
  152.         public function jsTreeNodeName(OIDplusObject $parent = null): string {
  153.                 if ($parent == null) return $this->objectTypeTitle();
  154.                 return substr($this->nodeId(), strlen($parent->nodeId()));
  155.         }
  156.  
  157.         /**
  158.          * @return string
  159.          */
  160.         public function defaultTitle(): string {
  161.                 //return $this->aid;
  162.                 return rtrim(chunk_split($this->aid, 2, ' '), ' ');
  163.         }
  164.  
  165.         /**
  166.          * @return bool
  167.          */
  168.         public function isLeafNode(): bool {
  169.                 // We don't know when an AID is "leaf", because an AID can have an arbitary length <= 16 Bytes.
  170.                 // But if it is 16 bytes long (32 nibbles), then we are 100% certain that it is a leaf node.
  171.                 return (strlen($this->nodeId(false)) == 32);
  172.         }
  173.  
  174.         /**
  175.          * @param string $title
  176.          * @param string $content
  177.          * @param string $icon
  178.          * @return void
  179.          * @throws OIDplusException
  180.          */
  181.         public function getContentPage(string &$title, string &$content, string &$icon) {
  182.                 $icon = file_exists(__DIR__.'/img/main_icon.png') ? OIDplus::webpath(__DIR__,OIDplus::PATH_RELATIVE).'img/main_icon.png' : '';
  183.  
  184.                 if ($this->isRoot()) {
  185.                         $title = OIDplusAid::objectTypeTitle();
  186.  
  187.                         $res = OIDplus::db()->query("select * from ###objects where parent = ?", array(self::root()));
  188.                         if ($res->any()) {
  189.                                 $content  = '<p>'._L('Please select an item in the tree view at the left to show its contents.').'</p>';
  190.                         } else {
  191.                                 $content  = '<p>'._L('Currently, no Application Identifiers are registered in the system.').'</p>';
  192.                         }
  193.  
  194.                         if (!$this->isLeafNode()) {
  195.                                 if (OIDplus::authUtils()->isAdminLoggedIn()) {
  196.                                         $content .= '<h2>'._L('Manage root objects').'</h2>';
  197.                                 } else {
  198.                                         $content .= '<h2>'._L('Available objects').'</h2>';
  199.                                 }
  200.                                 $content .= '%%CRUD%%';
  201.                         }
  202.                 } else {
  203.                         $title = $this->getTitle();
  204.  
  205.                         $chunked = $this->chunkedNotation(true);
  206.                         $content = '<h2>'.$chunked.'</h2>';
  207.  
  208.                         $tmp = decode_aid($this->aid,true);
  209.                         $tmp = htmlentities($tmp);
  210.                         $tmp = str_replace(' ','&nbsp;',$tmp);
  211.                         $tmp = nl2br($tmp);
  212.                         $tmp = preg_replace('@(warning|invalid|error|illegal(&nbsp;usage){0,1})@i', '<span class="aid_decoder_errortext">\\1</span>', $tmp);
  213.                         $tmp = preg_replace('@(\\\\\\d{3})@i', '<span class="aid_decoder_specialhexchar">\\1</span>', $tmp);
  214.  
  215.                         $content .= '<h2>'._L('Decoding').'</h2>';
  216.                         $content .= '<table border="0">';
  217.                         $content .= '<div style="overflow:auto;white-space:nowrap"><code>'.$tmp.'</code></div>';
  218.                         $content .= '</table>';
  219.  
  220.                         $content .= '<h2>'._L('Description').'</h2>%%DESC%%';
  221.                         if ($this->userHasWriteRights()) {
  222.                                 $content .= '<h2>'._L('Create or change subordinate objects').'</h2>';
  223.                         } else {
  224.                                 $content .= '<h2>'._L('Subordinate objects').'</h2>';
  225.                         }
  226.                         $content .= '%%CRUD%%';
  227.                 }
  228.         }
  229.  
  230.         # ---
  231.  
  232.         /**
  233.          * @param bool $withAbbr
  234.          * @return string
  235.          * @throws OIDplusException
  236.          */
  237.         public function chunkedNotation(bool $withAbbr=true): string {
  238.                 $curid = self::root().$this->aid;
  239.  
  240.                 $obj = OIDplusObject::findFitting($curid);
  241.                 if (!$obj) return $this->aid;
  242.  
  243.                 $hints = array();
  244.                 $lengths = array(strlen($curid));
  245.                 while ($obj = OIDplusObject::findFitting($curid)) {
  246.                         $objParent = $obj->getParent();
  247.                         if (!$objParent) break;
  248.                         $curid = $objParent->nodeId();
  249.                         $hints[] = $obj->getTitle();
  250.                         $lengths[] = strlen($curid);
  251.                 }
  252.  
  253.                 array_shift($lengths);
  254.                 $chunks = array();
  255.  
  256.                 $full = self::root().$this->aid;
  257.                 foreach ($lengths as $len) {
  258.                         $chunks[] = substr($full, $len);
  259.                         $full = substr($full, 0, $len);
  260.                 }
  261.  
  262.                 $hints = array_reverse($hints);
  263.                 $chunks = array_reverse($chunks);
  264.  
  265.                 $full = array();
  266.                 foreach ($chunks as $c) {
  267.                         $hint = array_shift($hints);
  268.                         $full[] = $withAbbr && ($hint !== '') ? '<abbr title="'.htmlentities($hint).'">'.$c.'</abbr>' : $c;
  269.                 }
  270.                 return implode(' ', $full);
  271.         }
  272.  
  273.         /**
  274.          * @return OIDplusAid|null
  275.          */
  276.         public function one_up()/*: ?OIDplusAid*/ {
  277.                 return self::parse($this->ns().':'.substr($this->aid,0,strlen($this->aid)-1));
  278.         }
  279.  
  280.         /**
  281.          * @param OIDplusObject|string $to
  282.          * @return int|null
  283.          */
  284.         public function distance($to) {
  285.                 if (!is_object($to)) $to = OIDplusObject::parse($to);
  286.                 if (!$to) return null;
  287.                 if (!($to instanceof $this)) return null;
  288.  
  289.                 $a = $to->aid;
  290.                 $b = $this->aid;
  291.  
  292.                 $ary = $a;
  293.                 $bry = $b;
  294.  
  295.                 $min_len = min(strlen($ary), strlen($bry));
  296.  
  297.                 for ($i=0; $i<$min_len; $i++) {
  298.                         if ($ary[$i] != $bry[$i]) return null;
  299.                 }
  300.  
  301.                 return strlen($ary) - strlen($bry);
  302.         }
  303.  
  304.         /**
  305.          * @return array|OIDplusAltId[]
  306.          * @throws OIDplusException
  307.          */
  308.         public function getAltIds(): array {
  309.                 if ($this->isRoot()) return array();
  310.                 $ids = parent::getAltIds();
  311.  
  312.                 $aid = $this->nodeId(false);
  313.                 $aid = strtoupper($aid);
  314.  
  315.                 // ViaThinkSoft proprietary AIDs
  316.  
  317.                 // (VTS B1) Members
  318.                 if ($aid == 'D276000186B1') {
  319.                         $oid = '1.3.6.1.4.1.37476.1';
  320.                         $ids[] = new OIDplusAltId('oid', $oid, _L('Object Identifier (OID)'));
  321.                 }
  322.  
  323.                 if (preg_match('@^D276000186B1(....)$@', $aid, $m)) {
  324.                         $oid = '1.3.6.1.4.1.37476.1.'.ltrim($m[1],'0');
  325.                         $ids[] = new OIDplusAltId('oid', $oid, _L('Object Identifier (OID)'));
  326.                 }
  327.  
  328.                 // (VTS B2) Products
  329.                 if ($aid == 'D276000186B2') {
  330.                         $oid = '1.3.6.1.4.1.37476.2';
  331.                         $ids[] = new OIDplusAltId('oid', $oid, _L('Object Identifier (OID)'));
  332.                 }
  333.  
  334.                 if (preg_match('@^D276000186B2(....)$@', $aid, $m)) {
  335.                         $oid = '1.3.6.1.4.1.37476.2.'.ltrim($m[1],'0');
  336.                         $ids[] = new OIDplusAltId('oid', $oid, _L('Object Identifier (OID)'));
  337.                 }
  338.  
  339.                 // (VTS B2 00 05) OIDplus Information Objects AID
  340.                 // Attention: D276000186B20005 does NOT represent 1.3.6.1.4.1.37476.30.9
  341.                 //            because the mapping to OIDplus systems only applies for 00......-7F...... (31 bit hash)
  342.  
  343.                 if (preg_match('@^D276000186B20005([0-7].......)$@', $aid, $m)) {
  344.                         $oid = '1.3.6.1.4.1.37476.30.9.'.hexdec($m[1]);
  345.                         $ids[] = new OIDplusAltId('oid', $oid, _L('Object Identifier (OID)'));
  346.                 }
  347.  
  348.                 if (preg_match('@^D276000186B20005([0-7].......)([0-7].......)$@', $aid, $m)) {
  349.                         $oid = '1.3.6.1.4.1.37476.30.9.'.hexdec($m[1]).'.'.hexdec($m[2]);
  350.                         $ids[] = new OIDplusAltId('oid', $oid, _L('Object Identifier (OID)'));
  351.                 }
  352.  
  353.                 // ViaThinkSoft "Example" AID
  354.  
  355.                 if ($aid == 'D276000186E0') {
  356.                         // Note that the OID object type plugin also maps children of 2.999 to AID,
  357.                         // using a hash. But since this is not unique and cannot be reverted,
  358.                         // we cannot have an reverse lookup/map.
  359.                         $ids[] = new OIDplusAltId('oid', '2.999', _L('Object Identifier (OID)'), ' ('._L('Optional PIX allowed, without prefix').')');
  360.                 }
  361.  
  362.                 // ViaThinkSoft "Foreign" AIDs
  363.  
  364.                 // (VTS F0) IANA PEN + PIX
  365.                 // Resolve only if there is no PIX
  366.                 if (str_starts_with($aid,'D276000186F0')) {
  367.                         $rest = substr($aid,strlen('D276000186F0'));
  368.                         $p = strpos($rest,'F');
  369.                         if ($p !== false) {
  370.                                 $pen = substr($rest,0,$p);
  371.                                 $pix = substr($rest,$p+1);
  372.                         } else {
  373.                                 $pen = $rest;
  374.                                 $pix = '';
  375.                         }
  376.                         if (($pix === '') && preg_match('/^[0-9]+$/',$pen,$m)) {
  377.                                 $oid = '1.3.6.1.4.1.'.$pen;
  378.                                 $ids[] = new OIDplusAltId('oid', $oid, _L('Object Identifier (OID)'));
  379.                                 $ids[] = new OIDplusAltId('iana-pen', $pen, _L('IANA Private Enterprise Number (PEN)'));
  380.                         }
  381.                 }
  382.  
  383.                 // (VTS F1) ViaThinkSoft FreeOID + PIX
  384.                 // Resolve only if there is no PIX
  385.                 if (str_starts_with($aid,'D276000186F1')) {
  386.                         $rest = substr($aid,strlen('D276000186F1'));
  387.                         $p = strpos($rest,'F');
  388.                         if ($p !== false) {
  389.                                 $number = substr($rest,0,$p);
  390.                                 $pix = substr($rest,$p+1);
  391.                         } else {
  392.                                 $number = $rest;
  393.                                 $pix = '';
  394.                         }
  395.                         if (($pix === '') && preg_match('/^[0-9]+$/',$number,$m)) {
  396.                                 $oid = '1.3.6.1.4.1.37476.9000.'.$number;
  397.                                 $ids[] = new OIDplusAltId('oid', $oid, _L('Object Identifier (OID)'));
  398.                         }
  399.                 }
  400.  
  401.                 // (VTS F2) MAC address (EUI/ELI/...) + PIX
  402.                 // Resolve only if there is no PIX
  403.                 if (str_starts_with($aid,'D276000186F2')) {
  404.                         $size_nibble = substr($aid,strlen('D276000186F2'),1);
  405.                         if ($size_nibble != '') {
  406.                                 $mac = substr($aid, strlen('D276000186F2X'), hexdec($size_nibble) + 1);
  407.                                 if (strlen($aid) <= strlen('D276000186F2X') + hexdec($size_nibble) + 1) {
  408.                                         $mac_type = mac_type(str_pad($mac, 12, '0', STR_PAD_RIGHT));
  409.                                         $ids[] = new OIDplusAltId('mac', $mac, $mac_type);
  410.                                 }
  411.                         }
  412.                 }
  413.  
  414.                 // (VTS F3) USB-IF VendorID + PIX
  415.                 // Resolve only if there is no PIX
  416.                 if (str_starts_with($aid,'D276000186F3')) {
  417.                         $rest = substr($aid,strlen('D276000186F3'));
  418.                         if (strlen($rest) == 4) {
  419.                                 $vid = $rest;
  420.                                 $ids[] = new OIDplusAltId('usb-vendor-id', $vid, _L('USB-IF (usb.org) VendorID'));
  421.                         }
  422.                 }
  423.  
  424.                 // (VTS F4) D-U-N-S number + PIX
  425.                 // Resolve only if there is no PIX
  426.                 if (str_starts_with($aid,'D276000186F4')) {
  427.                         $rest = substr($aid,strlen('D276000186F4'));
  428.                         $p = strpos($rest,'F');
  429.                         if ($p !== false) {
  430.                                 $duns = substr($rest,0,$p);
  431.                                 $pix = substr($rest,$p+1);
  432.                         } else {
  433.                                 $duns = $rest;
  434.                                 $pix = '';
  435.                         }
  436.                         if (($pix === '') && preg_match('/^[0-9]+$/',$duns,$m)) {
  437.                                 $ids[] = new OIDplusAltId('duns', $duns, _L('Data Universal Numbering System (D-U-N-S)'));
  438.                         }
  439.                 }
  440.  
  441.                 // (VTS F5) GS1 number + PIX
  442.                 // Resolve only if there is no PIX
  443.                 if (str_starts_with($aid,'D276000186F5')) {
  444.                         $rest = substr($aid,strlen('D276000186F5'));
  445.                         $p = strpos($rest,'F');
  446.                         if ($p !== false) {
  447.                                 $gs1 = substr($rest,0,$p);
  448.                                 $pix = substr($rest,$p+1);
  449.                         } else {
  450.                                 $gs1 = $rest;
  451.                                 $pix = '';
  452.                         }
  453.                         if (($pix === '') && preg_match('/^[0-9]+$/',$gs1,$m)) {
  454.                                 $ids[] = new OIDplusAltId('gs1', $gs1, _L('GS1 Based IDs (GLN/GTIN/SSCC/...)'), ' ('._L('without check-digit').')');
  455.                         }
  456.                 }
  457.  
  458.                 // (VTS F6) OID<->AID, no PIX
  459.                 if (str_starts_with($aid,'D276000186F6')) {
  460.                         $der = substr($aid,strlen('D276000186F6'));
  461.                         $len = strlen($der);
  462.                         if ($len%2 == 0) {
  463.                                 $len /= 2;
  464.                                 $len = str_pad("$len", 2, '0', STR_PAD_LEFT);
  465.                                 $type = '06'; // absolute OID
  466.                                 $der = "$type $len $der";
  467.                                 $oid = \OidDerConverter::derToOID(\OidDerConverter::hexStrToArray($der));
  468.                                 if ($oid) {
  469.                                         $oid = ltrim($oid,'.');
  470.                                         $ids[] = new OIDplusAltId('oid', $oid, _L('Object Identifier (OID)'));
  471.                                 }
  472.                         }
  473.                 }
  474.  
  475.                 // The case E8... (Standard OID 1.0) doesn't need to be addressed here, because it is already shown in the AID decoder (and it is ambiguous since DER and PIX are mixed)
  476.                 // TODO: If it has no pix, then resolve it !!! but how do we know if there is a PIX or a part ID ?
  477.  
  478.                 return $ids;
  479.         }
  480.  
  481.         /**
  482.          * @return string
  483.          */
  484.         public function getDirectoryName(): string {
  485.                 if ($this->isRoot()) return $this->ns();
  486.                 return $this->ns().'_'.$this->nodeId(false); // safe, because there are only AIDs
  487.         }
  488.  
  489.         /**
  490.          * @param string $mode
  491.          * @return string
  492.          */
  493.         public static function treeIconFilename(string $mode): string {
  494.                 return 'img/'.$mode.'_icon16.png';
  495.         }
  496. }
  497.