Subversion Repositories oidplus

Rev

Rev 1050 | Rev 1116 | 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.         private $guid;
  28.  
  29.         public function __construct($guid) {
  30.                 if (uuid_valid($guid)) {
  31.                         $this->guid = strtolower(uuid_canonize($guid)); // It is a real GUID (leaf node)
  32.                 } else {
  33.                         $this->guid = $guid; // It is a category name
  34.                 }
  35.         }
  36.  
  37.         public static function parse($node_id) {
  38.                 @list($namespace, $guid) = explode(':', $node_id, 2);
  39.                 if ($namespace !== self::ns()) return false;
  40.                 return new self($guid);
  41.         }
  42.  
  43.         public static function objectTypeTitle() {
  44.                 return _L('Globally Unique Identifier (GUID)');
  45.         }
  46.  
  47.         public static function objectTypeTitleShort() {
  48.                 return _L('GUID');
  49.         }
  50.  
  51.         public static function ns() {
  52.                 return 'guid';
  53.         }
  54.  
  55.         public static function root() {
  56.                 return self::ns().':';
  57.         }
  58.  
  59.         public function isRoot() {
  60.                 return $this->guid == '';
  61.         }
  62.  
  63.         public function nodeId($with_ns=true) {
  64.                 return $with_ns ? self::root().$this->guid : $this->guid;
  65.         }
  66.  
  67.         public function addString($str) {
  68.                 if (uuid_valid($str)) {
  69.                         // real GUID
  70.                         return self::root() . strtolower(uuid_canonize($str));
  71.                 } else {
  72.                         // just a category
  73.                         if ($this->isRoot()) {
  74.                                 return self::root() . $str;
  75.                         } else {
  76.                                 return $this->nodeId() . '/' . $str;
  77.                         }
  78.                 }
  79.         }
  80.  
  81.         public function crudShowId(OIDplusObject $parent) {
  82.                 if ($this->isLeafNode()) {
  83.                         // We don't parse '/' in a valid FourCC code (i.e. Leaf node)
  84.                         return $this->nodeId(false);
  85.                 } else {
  86.                         if ($parent->isRoot()) {
  87.                                 return substr($this->nodeId(), strlen($parent->nodeId()));
  88.                         } else {
  89.                                 return substr($this->nodeId(), strlen($parent->nodeId())+1);
  90.                         }
  91.                 }
  92.         }
  93.  
  94.         public function jsTreeNodeName(OIDplusObject $parent = null) {
  95.                 if ($parent == null) return $this->objectTypeTitle();
  96.                 return $this->crudShowId($parent);
  97.         }
  98.  
  99.         public function defaultTitle() {
  100.                 return $this->guid;
  101.         }
  102.  
  103.         public function isLeafNode() {
  104.                 return uuid_valid($this->guid);
  105.         }
  106.  
  107.         private function getTechInfo() {
  108.                 $tech_info = array();
  109.                 $tech_info[_L('UUID')] = strtolower(uuid_canonize($this->guid));
  110.                 $tech_info[_L('C++ notation')] = uuid_c_syntax($this->guid);
  111.  
  112.                 ob_start();
  113.                 uuid_info($this->guid);
  114.                 $info = ob_get_contents();
  115.                 preg_match_all('@([^:]+):\s*(.+)\n@ismU', $info, $m, PREG_SET_ORDER);
  116.                 foreach ($m as $m1) {
  117.                         $key = $m1[1];
  118.                         $value = $m1[2];
  119.                         $tech_info[$key] = $value;
  120.                 }
  121.                 ob_end_clean();
  122.  
  123.                 return $tech_info;
  124.         }
  125.  
  126.         public function getContentPage(&$title, &$content, &$icon) {
  127.                 $icon = file_exists(__DIR__.'/img/main_icon.png') ? OIDplus::webpath(__DIR__,OIDplus::PATH_RELATIVE).'img/main_icon.png' : '';
  128.  
  129.                 if ($this->isRoot()) {
  130.                         $title = OIDplusGuid::objectTypeTitle();
  131.  
  132.                         $res = OIDplus::db()->query("select * from ###objects where parent = ?", array(self::root()));
  133.                         if ($res->any()) {
  134.                                 $content  = '<p>'._L('Please select a GUID in the tree view at the left to show its contents.').'</p>';
  135.                         } else {
  136.                                 $content  = '<p>'._L('Currently, no GUID is registered in the system.').'</p>';
  137.                         }
  138.  
  139.                         if (!$this->isLeafNode()) {
  140.                                 if (OIDplus::authUtils()->isAdminLoggedIn()) {
  141.                                         $content .= '<h2>'._L('Manage root objects / categories').'</h2>';
  142.                                 } else {
  143.                                         $content .= '<h2>'._L('Available objects / categories').'</h2>';
  144.                                 }
  145.                                 $content .= '%%CRUD%%';
  146.                         }
  147.                 } else {
  148.                         $title = $this->getTitle();
  149.  
  150.                         if ($this->isLeafNode()) {
  151.                                 $tech_info = $this->getTechInfo();
  152.                                 $tech_info_html = '';
  153.                                 if (count($tech_info) > 0) {
  154.                                         $tech_info_html .= '<h2>'._L('Technical information').'</h2>';
  155.                                         $tech_info_html .= '<table border="0">';
  156.                                         foreach ($tech_info as $key => $value) {
  157.                                                 $tech_info_html .= '<tr><td>'.$key.': </td><td><code>'.$value.'</code></td></tr>';
  158.                                         }
  159.                                         $tech_info_html .= '</table>';
  160.                                 }
  161.  
  162.                                 $content = $tech_info_html;
  163.  
  164.                                 // $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>";
  165.                         } else {
  166.                                 $content = '';
  167.                         }
  168.  
  169.                         $content .= '<h2>'._L('Description').'</h2>%%DESC%%';
  170.  
  171.                         if (!$this->isLeafNode()) {
  172.                                 if ($this->userHasWriteRights()) {
  173.                                         $content .= '<h2>'._L('Create or change subordinate objects / categories').'</h2>';
  174.                                 } else {
  175.                                         $content .= '<h2>'._L('Subordinate objects / categories').'</h2>';
  176.                                 }
  177.                                 $content .= '%%CRUD%%';
  178.                         }
  179.                 }
  180.         }
  181.  
  182.         public function getIcon($row=null) {
  183.                 $in_login_treenode = false;
  184.                 foreach (debug_backtrace() as $trace) {
  185.                         // If we are inside the "Login" area (i.e. "Root object links"), we want the
  186.                         // correct icon, not a folder icon!
  187.                         if ($trace['class'] === OIDplusPagePublicLogin::class) $in_login_treenode = true;
  188.                 }
  189.  
  190.                 if (!$in_login_treenode && !$this->isLeafNode()) return null; // foldericon
  191.  
  192.                 return parent::getIcon($row);
  193.         }
  194.  
  195.         public function one_up() {
  196.                 // A GUID is a GUID, there is no hierarchy
  197.                 return false;
  198.         }
  199.  
  200.         public function distance($to) {
  201.                 // Distance between GUIDs is not possible
  202.                 return null;
  203.         }
  204.  
  205.         public function getAltIds() {
  206.                 if ($this->isRoot()) return array();
  207.                 if (!$this->isLeafNode()) return array();
  208.                 $ids = parent::getAltIds();
  209.                 $ids[] = new OIDplusAltId('oid', uuid_to_oid($this->guid), _L('OID representation of UUID'));
  210.                 return $ids;
  211.         }
  212.  
  213.         public function getDirectoryName() {
  214.                 if ($this->isLeafNode()) {
  215.                         // Leaf (UUID)
  216.                         // Example output: "guid_adb0b042_5b57_11eb_b0d9_3c4a92df8582"
  217.                         $str = $this->nodeId(false);
  218.                         $str = str_replace('-', '_', $str);
  219.                         $str = strtolower($str);
  220.                         return $this->ns().'_'.$str;
  221.                 } else {
  222.                         // Category
  223.                         return parent::getDirectoryName();
  224.                 }
  225.         }
  226.  
  227.         public static function treeIconFilename($mode) {
  228.                 return 'img/'.$mode.'_icon16.png';
  229.         }
  230. }
  231.