Subversion Repositories oidplus

Rev

Go to most recent revision | Blame | Last modification | View Log | RSS feed

  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.  
  20. class OIDplusOid extends OIDplusObject {
  21.         private $oid;
  22.  
  23.         public function __construct($oid) {
  24.                 // TODO: hier gültigkeitsprüfungen machen
  25.                 $this->oid = $oid;
  26.         }
  27.  
  28.         public static function parse($node_id) {
  29.                 @list($namespace, $oid) = explode(':', $node_id, 2);
  30.                 if ($namespace !== 'oid') return false;
  31.                 return new self($oid);
  32.         }
  33.  
  34.         public static function objectTypeTitle() {
  35.                 return "Object Identifier (OID)";
  36.         }
  37.  
  38.         public static function objectTypeTitleShort() {
  39.                 return "OID";
  40.         }
  41.  
  42.         public static function ns() {
  43.                 return 'oid';
  44.         }
  45.  
  46.         public static function root() {
  47.                 return 'oid:';
  48.         }
  49.  
  50.         public function isRoot() {
  51.                 return $this->oid == '';
  52.         }
  53.  
  54.         public function nodeId() {
  55.                 return 'oid:'.$this->oid;
  56.         }
  57.  
  58.         public function addString($str) {
  59.                 if (!$this->isRoot()) {
  60.                         if (strpos($str,'.') !== false) die("Please only submit one arc (not an absolute OID or multiple arcs).");
  61.                 }
  62.                 return $this->appendArcs($str)->nodeId();
  63.         }
  64.  
  65.         public function crudShowId(OIDplusObject $parent) {
  66.                 return $this->deltaDotNotation($parent);
  67.         }
  68.  
  69.         public function crudInsertPrefix() {
  70.                 return '';
  71.         }
  72.  
  73.         public function jsTreeNodeName(OIDplusObject $parent = null) {
  74.                 if ($parent == null) return $this->objectTypeTitle();
  75.                 return $this->viewGetArcAsn1s($parent);
  76.         }
  77.  
  78.         public function defaultTitle() {
  79.                 return 'OID ' . $this->oid;
  80.         }
  81.  
  82.         public function getContentPage(&$title, &$content) {
  83.                 if ($this->isRoot()) {
  84.                         $title = OIDplusOid::objectTypeTitle();
  85.  
  86.                         $res = OIDplus::db()->query("select * from ".OIDPLUS_TABLENAME_PREFIX."objects where parent = '".OIDplus::db()->real_escape_string(self::root())."'");
  87.                         if (OIDplus::db()->num_rows($res) > 0) {
  88.                                 $content = 'Please select an OID in the tree view at the left to show its contents.';
  89.                         } else {
  90.                                 $content = 'Currently, no OID is registered in the system.';
  91.                         }
  92.  
  93.                         if (OIDplus::authUtils()::isAdminLoggedIn()) {
  94.                                 $content .= '<h2>Manage your root OIDs</h2>';
  95.                         } else {
  96.                                 $content .= '<h2>Root OIDs</h2>';
  97.                         }
  98.                         $content .= '%%CRUD%%';
  99.                 } else {
  100.                         $content = "<h2>Technical information</h2>".$this->oidInformation().
  101.                                        "<h2>Description</h2>%%DESC%%<br>".
  102.                                        "<h2>Registration Authority</h2>%%RA_INFO%%";
  103.  
  104.                         if ($this->userHasWriteRights()) {
  105.                                 $content .= '<h2>Create or change subsequent objects</h2>';
  106.                         } else {
  107.                                 $content .= '<h2>Subsequent objects</h2>';
  108.                         }
  109.                         $content .= '%%CRUD%%';
  110.                 }
  111.         }
  112.  
  113.         # ---
  114.  
  115.         private function oidInformation() {
  116.                 return "Dot Notation: <code>" . $this->getDotNotation() . "</code><br>" .
  117.                        "ASN.1 Notation: <code>{ " . $this->getAsn1Notation() . " }</code><br>" .
  118.                        "IRI Notation: <code>" . $this->getIriNotation() . "</code><br><br>";
  119.         }
  120.  
  121.         public function __clone() {
  122.                 return new self($this->oid);
  123.         }
  124.  
  125.         public function appendArcs(String $arcs) {
  126.                 $out = clone $this;
  127.                 if ($out->isRoot()) {
  128.                         $out->oid .= $arcs;
  129.                 } else {
  130.                         $out->oid .= '.' . $arcs;
  131.                 }
  132.                 return $out;
  133.         }
  134.  
  135.         public function deltaDotNotation(OIDplusOid $parent) {
  136.                 if (!$parent->isRoot()) {
  137.                         if (substr($this->oid, 0, strlen($parent->oid)+1) == $parent->oid.'.') {
  138.                                 return substr($this->oid, strlen($parent->oid)+1);
  139.                         } else {
  140.                                 return false;
  141.                         }
  142.                 } else {
  143.                         return $this->oid;
  144.                 }
  145.         }
  146.  
  147.         public function viewGetArcAsn1s(OIDplusOid $parent=null, $separator = ' | ') {
  148.                 $asn_ids = array();
  149.  
  150.                 if (is_null($parent)) $parent = OIDplusOid::parse('oid:');
  151.  
  152.                 $part = $this->deltaDotNotation($parent);
  153.  
  154.                 if (strpos($part, '.') === false) {
  155.                         $res2 = OIDplus::db()->query("select * from ".OIDPLUS_TABLENAME_PREFIX."asn1id where oid = 'oid:".OIDplus::db()->real_escape_string($this->oid)."' order by lfd");
  156.                         while ($row2 = OIDplus::db()->fetch_array($res2)) {
  157.                                 $asn_ids[] = $row2['name'].'('.$part.')';
  158.                         }
  159.                 }
  160.  
  161.                 if (count($asn_ids) == 0) $asn_ids = array($part);
  162.                 return implode($asn_ids, $separator);
  163.         }
  164.  
  165.         public function getAsn1Notation($withAbbr=true) {
  166.                 $asn1_notation = '';
  167.                 $arcs = explode('.', $this->oid);
  168.  
  169.                 foreach ($arcs as $arc) {
  170.                         $res = OIDplus::db()->query("select name from ".OIDPLUS_TABLENAME_PREFIX."asn1id where oid = '".OIDplus::db()->real_escape_string('oid:'.implode('.',$arcs))."' order by lfd");
  171.  
  172.                         $names = array();
  173.                         while ($row = OIDplus::db()->fetch_array($res)) {
  174.                                 $names[] = $row['name'];
  175.                         }
  176.  
  177.                         if (count($names) > 1) {
  178.                                 $first_name = array_shift($names);
  179.                                 $abbr = 'Other identifiers: '.implode(', ',$names);
  180.                                 if ($withAbbr) {
  181.                                         $asn1_notation = '<abbr title="'.$abbr.'">'.$first_name.'</abbr>'.'('.array_pop($arcs).')'.' '.$asn1_notation;
  182.                                 } else {
  183.                                         $asn1_notation = $first_name.'('.array_pop($arcs).')'.' '.$asn1_notation;
  184.                                 }
  185.                         } else if (count($names) == 1) {
  186.                                 $asn1_notation = array_shift($names).'('.array_pop($arcs).')'.' '.$asn1_notation;
  187.                         } else {
  188.                                 $asn1_notation = array_pop($arcs).' '.$asn1_notation;
  189.                         }
  190.                 }
  191.  
  192.                 return $asn1_notation;
  193.         }
  194.  
  195.         public function getIriNotation($withAbbr=true) {
  196.                 $iri_notation = '';
  197.                 $arcs = explode('.', $this->oid);
  198.  
  199.                 foreach ($arcs as $arc) {
  200.                         $res = OIDplus::db()->query("select name, longarc from ".OIDPLUS_TABLENAME_PREFIX."iri where oid = '".OIDplus::db()->real_escape_string('oid:'.implode('.',$arcs))."' order by lfd");
  201.  
  202.                         $is_longarc = false;
  203.                         $names = array();
  204.                         while ($row = OIDplus::db()->fetch_array($res)) {
  205.                                 $is_longarc = $row['longarc'];
  206.                                 $names[] = $row['name'];
  207.                         }
  208.  
  209.                         $names[] = array_pop($arcs);
  210.                         if (count($names) > 2) {
  211.                                 $first_name = array_shift($names);
  212.                                 $abbr = 'Other identifiers: '.implode(', ',$names);
  213.                                 $iri_notation = $withAbbr ? '<abbr title="'.$abbr.'">'.$first_name.'</abbr>/'.$iri_notation : $first_name.'/'.$iri_notation;
  214.                         } else if (count($names) > 1) {
  215.                                 $first_name = array_shift($names);
  216.                                 $abbr = 'Numeric value: '.array_shift($names);
  217.                                 $iri_notation = $withAbbr ? '<abbr title="'.$abbr.'">'.$first_name.'</abbr>/'.$iri_notation : $first_name.'/'.$iri_notation;
  218.                         } else if (count($names) == 1) {
  219.                                 $iri_notation = array_shift($names) . '/' . $iri_notation;
  220.                         }
  221.  
  222.                         if ($is_longarc) break;
  223.                 }
  224.                 $iri_notation = '/' . substr($iri_notation, 0, strlen($iri_notation)-1);
  225.  
  226.                 return $iri_notation;
  227.         }
  228.  
  229.         public function getDotNotation() {
  230.                 return $this->oid;
  231.         }
  232.  
  233.         public function replaceAsn1Ids($demandedASN1s=array()) {
  234.                 // First do a few checks
  235.                 foreach ($demandedASN1s as &$asn1) {
  236.                         $asn1 = trim($asn1);
  237.                         // TODO: check if $id is valid ASN.1
  238.  
  239.                         // Check if the (real) parent has any conflict
  240.                         $res = OIDplus::db()->query("select * from ".OIDPLUS_TABLENAME_PREFIX."asn1id where name = '".OIDplus::db()->real_escape_string($asn1)."'");
  241.                         while ($row = OIDplus::db()->fetch_array($res)) {
  242.                                 $check_oid = OIDplusOid::parse($row['oid'])->oid;
  243.                                 if ((oid_up($check_oid) === oid_up($this->oid)) && // same parent
  244.                                    ($check_oid !== $this->oid))                    // different OID
  245.                                 {
  246.                                         throw new Exception("ASN.1 identifier '$asn1' is already used by another OID ($check_oid)");
  247.                                 }
  248.                         }
  249.                 }
  250.  
  251.                 // Now do the real replacement
  252.                 OIDplus::db()->query("delete from ".OIDPLUS_TABLENAME_PREFIX."asn1id where oid = 'oid:".OIDplus::db()->real_escape_string($this->oid)."'");
  253.                 foreach ($demandedASN1s as &$asn1) {
  254.                         if (!OIDplus::db()->query("insert into ".OIDPLUS_TABLENAME_PREFIX."asn1id (oid, name) values ('oid:".OIDplus::db()->real_escape_string($this->oid)."', '".OIDplus::db()->real_escape_string($asn1)."')")) {
  255.                                 throw new Exception("Insertion of ASN.1 ID $asn1 to OID ".$this->oid." failed!");
  256.                         }
  257.                 }
  258.         }
  259.  
  260.         public function replaceIris($demandedIris=array()) {
  261.                 // First do a few checks
  262.                 foreach ($demandedIris as &$iri) {
  263.                         $iri = trim($iri);
  264.                         // TODO: check if $id is valid IRI
  265.  
  266.                         // Check if the (real) parent has any conflict
  267.                         $res = OIDplus::db()->query("select * from ".OIDPLUS_TABLENAME_PREFIX."iri where name = '".OIDplus::db()->real_escape_string($iri)."'");
  268.                         while ($row = OIDplus::db()->fetch_array($res)) {
  269.                                 $check_oid = OIDplusOid::parse($row['oid'])->oid;
  270.                                 if ((oid_up($check_oid) === oid_up($this->oid)) && // same parent
  271.                                    ($check_oid !== $this->oid))                    // different OID
  272.                                 {
  273.                                         throw new Exception("IRI '$iri' is already used by another OID ($check_oid)");
  274.                                 }
  275.                         }
  276.                 }
  277.  
  278.                 // Now do the real replacement
  279.                 OIDplus::db()->query("delete from ".OIDPLUS_TABLENAME_PREFIX."iri where oid = 'oid:".OIDplus::db()->real_escape_string($this->oid)."'");
  280.                 foreach ($demandedIris as &$iri) {
  281.                         if (!OIDplus::db()->query("insert into ".OIDPLUS_TABLENAME_PREFIX."iri (oid, name) values ('oid:".OIDplus::db()->real_escape_string($this->oid)."', '".OIDplus::db()->real_escape_string($iri)."')")) {
  282.                                 throw new Exception("Insertion of IRI $iri to OID ".$this->oid." failed!");
  283.                         }
  284.                 }
  285.         }
  286. }
  287.  
  288. OIDplusObject::$registeredObjectTypes[] = 'OIDplusOid';
  289.