Subversion Repositories oidplus

Rev

Rev 1124 | Rev 1271 | 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 OIDplusGuid extends OIDplusObject {
  27.         /**
  28.          * @var string
  29.          */
  30.         private $guid;
  31.  
  32.         /**
  33.          * @param string $guid
  34.          */
  35.         public function __construct(string $guid) {
  36.                 if (uuid_valid($guid)) {
  37.                         $this->guid = strtolower(uuid_canonize($guid)); // It is a real GUID (leaf node)
  38.                 } else {
  39.                         $this->guid = $guid; // It is a category name
  40.                 }
  41.         }
  42.  
  43.         /**
  44.          * @param string $node_id
  45.          * @return OIDplusGuid|null
  46.          */
  47.         public static function parse(string $node_id)/*: ?OIDplusGuid*/ {
  48.                 @list($namespace, $guid) = explode(':', $node_id, 2);
  49.                 if ($namespace !== self::ns()) return null;
  50.                 return new self($guid);
  51.         }
  52.  
  53.         /**
  54.          * @return string
  55.          */
  56.         public static function objectTypeTitle(): string {
  57.                 return _L('Globally Unique Identifier (GUID)');
  58.         }
  59.  
  60.         /**
  61.          * @return string
  62.          */
  63.         public static function objectTypeTitleShort(): string {
  64.                 return _L('GUID');
  65.         }
  66.  
  67.         /**
  68.          * @return string
  69.          */
  70.         public static function ns(): string {
  71.                 return 'guid';
  72.         }
  73.  
  74.         /**
  75.          * @return string
  76.          */
  77.         public static function root(): string {
  78.                 return self::ns().':';
  79.         }
  80.  
  81.         /**
  82.          * @return bool
  83.          */
  84.         public function isRoot(): bool {
  85.                 return $this->guid == '';
  86.         }
  87.  
  88.         /**
  89.          * @param bool $with_ns
  90.          * @return string
  91.          */
  92.         public function nodeId(bool $with_ns=true): string {
  93.                 return $with_ns ? self::root().$this->guid : $this->guid;
  94.         }
  95.  
  96.         /**
  97.          * @param string $str
  98.          * @return string
  99.          */
  100.         public function addString(string $str): string {
  101.                 if (uuid_valid($str)) {
  102.                         // real GUID
  103.                         return self::root() . strtolower(uuid_canonize($str));
  104.                 } else {
  105.                         // just a category
  106.                         if ($this->isRoot()) {
  107.                                 return self::root() . $str;
  108.                         } else {
  109.                                 return $this->nodeId() . '/' . $str;
  110.                         }
  111.                 }
  112.         }
  113.  
  114.         /**
  115.          * @param OIDplusObject $parent
  116.          * @return string
  117.          */
  118.         public function crudShowId(OIDplusObject $parent): string {
  119.                 if ($this->isLeafNode()) {
  120.                         // We don't parse '/' in a valid FourCC code (i.e. Leaf node)
  121.                         return $this->nodeId(false);
  122.                 } else {
  123.                         if ($parent->isRoot()) {
  124.                                 return substr($this->nodeId(), strlen($parent->nodeId()));
  125.                         } else {
  126.                                 return substr($this->nodeId(), strlen($parent->nodeId())+1);
  127.                         }
  128.                 }
  129.         }
  130.  
  131.         /**
  132.          * @param OIDplusObject|null $parent
  133.          * @return string
  134.          */
  135.         public function jsTreeNodeName(OIDplusObject $parent = null): string {
  136.                 if ($parent == null) return $this->objectTypeTitle();
  137.                 return $this->crudShowId($parent);
  138.         }
  139.  
  140.         /**
  141.          * @return string
  142.          */
  143.         public function defaultTitle(): string {
  144.                 return $this->guid;
  145.         }
  146.  
  147.         /**
  148.          * @return bool
  149.          */
  150.         public function isLeafNode(): bool {
  151.                 return uuid_valid($this->guid);
  152.         }
  153.  
  154.         /**
  155.          * @return array
  156.          */
  157.         private function getTechInfo(): array {
  158.                 $tech_info = array();
  159.                 $tech_info[_L('UUID')] = strtolower(uuid_canonize($this->guid));
  160.                 $tech_info[_L('C++ notation')] = uuid_c_syntax($this->guid);
  161.  
  162.                 ob_start();
  163.                 uuid_info($this->guid);
  164.                 $info = ob_get_contents();
  165.                 preg_match_all('@([^:]+):\s*(.+)\n@ismU', $info, $m, PREG_SET_ORDER);
  166.                 foreach ($m as $m1) {
  167.                         $key = $m1[1];
  168.                         $value = $m1[2];
  169.                         $tech_info[$key] = $value;
  170.                 }
  171.                 ob_end_clean();
  172.  
  173.                 return $tech_info;
  174.         }
  175.  
  176.         /**
  177.          * @param string $title
  178.          * @param string $content
  179.          * @param string $icon
  180.          * @return void
  181.          * @throws OIDplusException
  182.          */
  183.         public function getContentPage(string &$title, string &$content, string &$icon) {
  184.                 $icon = file_exists(__DIR__.'/img/main_icon.png') ? OIDplus::webpath(__DIR__,OIDplus::PATH_RELATIVE).'img/main_icon.png' : '';
  185.  
  186.                 if ($this->isRoot()) {
  187.                         $title = OIDplusGuid::objectTypeTitle();
  188.  
  189.                         $res = OIDplus::db()->query("select * from ###objects where parent = ?", array(self::root()));
  190.                         if ($res->any()) {
  191.                                 $content  = '<p>'._L('Please select a GUID in the tree view at the left to show its contents.').'</p>';
  192.                         } else {
  193.                                 $content  = '<p>'._L('Currently, no GUID is registered in the system.').'</p>';
  194.                         }
  195.  
  196.                         if (!$this->isLeafNode()) {
  197.                                 if (OIDplus::authUtils()->isAdminLoggedIn()) {
  198.                                         $content .= '<h2>'._L('Manage root objects / categories').'</h2>';
  199.                                 } else {
  200.                                         $content .= '<h2>'._L('Available objects / categories').'</h2>';
  201.                                 }
  202.                                 $content .= '%%CRUD%%';
  203.                         }
  204.                 } else {
  205.                         $title = $this->getTitle();
  206.  
  207.                         if ($this->isLeafNode()) {
  208.                                 $tech_info = $this->getTechInfo();
  209.                                 $tech_info_html = '';
  210.                                 if (count($tech_info) > 0) {
  211.                                         $tech_info_html .= '<h2>'._L('Technical information').'</h2>';
  212.                                         $tech_info_html .= '<table border="0">';
  213.                                         foreach ($tech_info as $key => $value) {
  214.                                                 $tech_info_html .= '<tr><td>'.$key.': </td><td><code>'.$value.'</code></td></tr>';
  215.                                         }
  216.                                         $tech_info_html .= '</table>';
  217.                                 }
  218.  
  219.                                 $content = $tech_info_html;
  220.  
  221.                                 // $content .= "<p><a href=\"https://misc.daniel-marschall.de/tools/uuid_mac_decoder/interprete_uuid.php?uuid=".urlencode($this->guid)."\">More technical information</a></p>";
  222.                         } else {
  223.                                 $content = '';
  224.                         }
  225.  
  226.                         $content .= '<h2>'._L('Description').'</h2>%%DESC%%';
  227.  
  228.                         if (!$this->isLeafNode()) {
  229.                                 if ($this->userHasWriteRights()) {
  230.                                         $content .= '<h2>'._L('Create or change subordinate objects / categories').'</h2>';
  231.                                 } else {
  232.                                         $content .= '<h2>'._L('Subordinate objects / categories').'</h2>';
  233.                                 }
  234.                                 $content .= '%%CRUD%%';
  235.                         }
  236.                 }
  237.         }
  238.  
  239.         /**
  240.          * @return OIDplusGuid|null
  241.          */
  242.         public function one_up()/*: ?OIDplusGuid*/ {
  243.                 // A GUID is a GUID, there is no hierarchy
  244.                 return null;
  245.         }
  246.  
  247.         /**
  248.          * @param OIDplusObject|string $to
  249.          * @return int|null
  250.          */
  251.         public function distance($to) {
  252.                 // Distance between GUIDs is not possible
  253.                 return null;
  254.         }
  255.  
  256.         /**
  257.          * @return array|OIDplusAltId[]
  258.          * @throws OIDplusException
  259.          */
  260.         public function getAltIds(): array {
  261.                 if ($this->isRoot()) return array();
  262.                 if (!$this->isLeafNode()) return array();
  263.                 $ids = parent::getAltIds();
  264.                 $ids[] = new OIDplusAltId('oid', uuid_to_oid($this->guid), _L('OID representation of UUID'));
  265.                 return $ids;
  266.         }
  267.  
  268.         /**
  269.          * @return string
  270.          * @throws OIDplusException
  271.          */
  272.         public function getDirectoryName(): string {
  273.                 if ($this->isLeafNode()) {
  274.                         // Leaf (UUID)
  275.                         // Example output: "guid_adb0b042_5b57_11eb_b0d9_3c4a92df8582"
  276.                         $str = $this->nodeId(false);
  277.                         $str = str_replace('-', '_', $str);
  278.                         $str = strtolower($str);
  279.                         return $this->ns().'_'.$str;
  280.                 } else {
  281.                         // Category
  282.                         return parent::getDirectoryName();
  283.                 }
  284.         }
  285.  
  286.         /**
  287.          * @param string $mode
  288.          * @return string
  289.          */
  290.         public static function treeIconFilename(string $mode): string {
  291.                 return 'img/'.$mode.'_icon16.png';
  292.         }
  293. }
  294.