Subversion Repositories oidplus

Rev

Rev 1130 | 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. use SpomkyLabs\Punycode;
  23.  
  24. // phpcs:disable PSR1.Files.SideEffects
  25. \defined('INSIDE_OIDPLUS') or die;
  26. // phpcs:enable PSR1.Files.SideEffects
  27.  
  28. class OIDplusDomain extends OIDplusObject {
  29.         /**
  30.          * @var string
  31.          */
  32.         private $domain;
  33.  
  34.         /**
  35.          * @param string $domain
  36.          */
  37.         public function __construct(string $domain) {
  38.                 // TODO: syntax checks
  39.                 $this->domain = $domain;
  40.         }
  41.  
  42.         /**
  43.          * @param string $node_id
  44.          * @return OIDplusDomain|null
  45.          */
  46.         public static function parse(string $node_id)/*: ?OIDplusDomain*/ {
  47.                 @list($namespace, $domain) = explode(':', $node_id, 2);
  48.                 if ($namespace !== self::ns()) return null;
  49.                 return new self($domain);
  50.         }
  51.  
  52.         /**
  53.          * @return string
  54.          */
  55.         public static function objectTypeTitle(): string {
  56.                 return _L('Domain Names');
  57.         }
  58.  
  59.         /**
  60.          * @return string
  61.          */
  62.         public static function objectTypeTitleShort(): string {
  63.                 return _L('Domain');
  64.         }
  65.  
  66.         /**
  67.          * @return string
  68.          */
  69.         public static function ns(): string {
  70.                 return 'domain';
  71.         }
  72.  
  73.         /**
  74.          * @return string
  75.          */
  76.         public static function root(): string {
  77.                 return self::ns().':';
  78.         }
  79.  
  80.         /**
  81.          * @return bool
  82.          */
  83.         public function isRoot(): bool {
  84.                 return $this->domain == '';
  85.         }
  86.  
  87.         /**
  88.          * @param bool $with_ns
  89.          * @return string
  90.          */
  91.         public function nodeId(bool $with_ns=true): string {
  92.                 return $with_ns ? self::root().$this->domain : $this->domain;
  93.         }
  94.  
  95.         /**
  96.          * @param string $str
  97.          * @return string
  98.          * @throws OIDplusException
  99.          */
  100.         public function addString(string $str): string {
  101.                 if ($this->isRoot()) {
  102.                         return self::root().$str;
  103.                 } else {
  104.                         if (strpos($str,'.') !== false) throw new OIDplusException(_L('Please only submit one arc.'));
  105.                         return self::root().$str.'.'.$this->nodeId(false);
  106.                 }
  107.         }
  108.  
  109.         /**
  110.          * @param OIDplusObject $parent
  111.          * @return string
  112.          */
  113.         public function crudShowId(OIDplusObject $parent): string {
  114.                 return $this->domain;
  115.         }
  116.  
  117.         /**
  118.          * @return string
  119.          * @throws OIDplusException
  120.          */
  121.         public function crudInsertSuffix(): string {
  122.                 return $this->isRoot() ? '' : substr($this->addString(''), strlen(self::ns())+1);
  123.         }
  124.  
  125.         /**
  126.          * @param OIDplusObject|null $parent
  127.          * @return string
  128.          */
  129.         public function jsTreeNodeName(OIDplusObject $parent = null): string {
  130.                 if ($parent == null) return $this->objectTypeTitle();
  131.                 return $this->domain;
  132.         }
  133.  
  134.         /**
  135.          * @return string
  136.          */
  137.         public function defaultTitle(): string {
  138.                 return $this->domain;
  139.         }
  140.  
  141.         /**
  142.          * @return bool
  143.          */
  144.         public function isLeafNode(): bool {
  145.                 return false;
  146.         }
  147.  
  148.         /**
  149.          * @return string[]
  150.          * @throws OIDplusException
  151.          */
  152.         private function getTechInfo(): array {
  153.                 $tech_info = array();
  154.  
  155.                 $dns = $this->nodeId(false);
  156.                 $punycode = Punycode::encode($dns);
  157.  
  158.                 // - common (www.example.com) <== we have this nateively
  159.                 if ($dns != $punycode) {
  160.                         $tmp = _L('Common notation (IDN)');
  161.                         $tech_info[$tmp] = $dns;
  162.                         $tmp = _L('Common notation (Punycode)');
  163.                         $tech_info[$tmp] = $punycode;
  164.                 } else {
  165.                         $tmp = _L('Common notation');
  166.                         $tech_info[$tmp] = $punycode;
  167.                 }
  168.  
  169.                 // - presentation (www.example.com.)
  170.                 $tmp = _L('Presentation notation');
  171.                 $tech_info[$tmp] = $punycode.'.';
  172.  
  173.                 // - wire format (3www7example3com0)
  174.                 $wireformat = '';
  175.                 $ary = explode('.', $punycode.'.');
  176.                 foreach ($ary as $a) {
  177.                         $wireformat .= strlen($a).$a;
  178.                 }
  179.                 $tmp = _L('Wire format');
  180.                 $tech_info[$tmp] = $wireformat;
  181.  
  182.                 return $tech_info;
  183.         }
  184.  
  185.         /**
  186.          * @param string $title
  187.          * @param string $content
  188.          * @param string $icon
  189.          * @return void
  190.          * @throws OIDplusException
  191.          */
  192.         public function getContentPage(string &$title, string &$content, string &$icon) {
  193.                 $icon = file_exists(__DIR__.'/img/main_icon.png') ? OIDplus::webpath(__DIR__,OIDplus::PATH_RELATIVE).'img/main_icon.png' : '';
  194.  
  195.                 if ($this->isRoot()) {
  196.                         $title = OIDplusDomain::objectTypeTitle();
  197.  
  198.                         $res = OIDplus::db()->query("select * from ###objects where parent = ?", array(self::root()));
  199.                         if ($res->any()) {
  200.                                 $content  = '<p>'._L('Please select a Domain Name in the tree view at the left to show its contents.').'</p>';
  201.                         } else {
  202.                                 $content  = '<p>'._L('Currently, no Domain Name is registered in the system.').'</p>';
  203.                         }
  204.  
  205.                         if (!$this->isLeafNode()) {
  206.                                 if (OIDplus::authUtils()->isAdminLoggedIn()) {
  207.                                         $content .= '<h2>'._L('Manage root objects').'</h2>';
  208.                                 } else {
  209.                                         $content .= '<h2>'._L('Available objects').'</h2>';
  210.                                 }
  211.                                 $content .= '%%CRUD%%';
  212.                         }
  213.                 } else {
  214.                         $title = $this->getTitle();
  215.  
  216.                         $tech_info = $this->getTechInfo();
  217.                         $tech_info_html = '';
  218.                         if (count($tech_info) > 0) {
  219.                                 $tech_info_html .= '<h2>'._L('Technical information').'</h2>';
  220.                                 $tech_info_html .= '<div style="overflow:auto"><table border="0">';
  221.                                 foreach ($tech_info as $key => $value) {
  222.                                         $tech_info_html .= '<tr><td valign="top" style="white-space: nowrap;">'.$key.': </td><td><code>'.$value.'</code></td></tr>';
  223.                                 }
  224.                                 $tech_info_html .= '</table></div>';
  225.                         }
  226.  
  227.                         // $content = '<h3>'.explode(':',$this->nodeId())[1].'</h3>';
  228.                         $content = $tech_info_html;
  229.  
  230.                         $content .= '<h2>'._L('Description').'</h2>%%DESC%%'; // TODO: add more meta information about the object type
  231.  
  232.                         if (!$this->isLeafNode()) {
  233.                                 if ($this->userHasWriteRights()) {
  234.                                         $content .= '<h2>'._L('Create or change subordinate objects').'</h2>';
  235.                                 } else {
  236.                                         $content .= '<h2>'._L('Subordinate objects').'</h2>';
  237.                                 }
  238.                                 $content .= '%%CRUD%%';
  239.                         }
  240.                 }
  241.         }
  242.  
  243.         /**
  244.          * @return OIDplusDomain|null
  245.          */
  246.         public function one_up()/*: ?OIDplusDomain*/ {
  247.                 $oid = $this->domain;
  248.  
  249.                 $p = strpos($oid, '.');
  250.                 if ($p === false) return self::parse('');
  251.  
  252.                 $oid_up = substr($oid, $p+1);
  253.  
  254.                 return self::parse(self::ns().':'.$oid_up);
  255.         }
  256.  
  257.         /**
  258.          * @param OIDplusObject|string $to
  259.          * @return int|null
  260.          */
  261.         public function distance($to) {
  262.                 if (!is_object($to)) $to = OIDplusObject::parse($to);
  263.                 if (!$to) return null;
  264.                 if (!($to instanceof $this)) return null;
  265.  
  266.                 $a = $to->domain;
  267.                 $b = $this->domain;
  268.  
  269.                 if (substr($a,-1) == '.') $a = substr($a,0,strlen($a)-1);
  270.                 if (substr($b,-1) == '.') $b = substr($b,0,strlen($b)-1);
  271.  
  272.                 $ary = explode('.', $a);
  273.                 $bry = explode('.', $b);
  274.  
  275.                 $ary = array_reverse($ary);
  276.                 $bry = array_reverse($bry);
  277.  
  278.                 $min_len = min(count($ary), count($bry));
  279.  
  280.                 for ($i=0; $i<$min_len; $i++) {
  281.                         if ($ary[$i] != $bry[$i]) return null;
  282.                 }
  283.  
  284.                 return count($ary) - count($bry);
  285.         }
  286.  
  287.         /**
  288.          * @return OIDplusAltId[]
  289.          * @throws OIDplusException
  290.          */
  291.         public function getAltIds(): array {
  292.                 if ($this->isRoot()) return array();
  293.                 $ids = parent::getAltIds();
  294.  
  295.                 // Note: The payload for the namebased UUID can be any notation:
  296.                 // - common (www.example.com) <== we have this
  297.                 // - presentation (www.example.com.)
  298.                 // - wire format (3www7example3com0)
  299.                 $ids[] = new OIDplusAltId('guid', gen_uuid_md5_namebased(UUID_NAMEBASED_NS_DNS, $this->nodeId(false)), _L('Name based version 3 / MD5 UUID with namespace %1','UUID_NAMEBASED_NS_DNS'));
  300.                 $ids[] = new OIDplusAltId('guid', gen_uuid_sha1_namebased(UUID_NAMEBASED_NS_DNS, $this->nodeId(false)), _L('Name based version 5 / SHA1 UUID with namespace %1','UUID_NAMEBASED_NS_DNS'));
  301.  
  302.                 return $ids;
  303.         }
  304.  
  305.         /**
  306.          * @return string
  307.          */
  308.         public function getDirectoryName(): string {
  309.                 if ($this->isRoot()) return $this->ns();
  310.                 return $this->ns().'_'.md5($this->nodeId(false));
  311.         }
  312.  
  313.         /**
  314.          * @param string $mode
  315.          * @return string
  316.          */
  317.         public static function treeIconFilename(string $mode): string {
  318.                 return 'img/'.$mode.'_icon16.png';
  319.         }
  320. }
  321.