Subversion Repositories oidplus

Rev

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