Subversion Repositories oidplus

Rev

Rev 1386 | 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 OIDplusIpv6 extends OIDplusObject {
  27.         /**
  28.          * @var string
  29.          */
  30.         private $ipv6;
  31.  
  32.         /**
  33.          * @var string
  34.          */
  35.         private $bare;
  36.  
  37.         /**
  38.          * @var int
  39.          */
  40.         private $cidr;
  41.  
  42.         /**
  43.          * @param string $ipv6
  44.          * @throws OIDplusException
  45.          */
  46.         public function __construct(string $ipv6) {
  47.                 $this->ipv6 = $ipv6;
  48.  
  49.                 if (!empty($ipv6)) {
  50.                         if (strpos($ipv6, '/') === false) $ipv6 .= '/128';
  51.                         list($bare, $cidr) = explode('/', $ipv6);
  52.                         $this->bare = $bare;
  53.                         if (!is_numeric($cidr)) throw new OIDplusException(_L('Invalid IPv6'));
  54.                         $this->cidr = (int)$cidr;
  55.                         if (!ipv6_valid($bare)) throw new OIDplusException(_L('Invalid IPv6'));
  56.                         if ($cidr < 0) throw new OIDplusException(_L('Invalid IPv6'));
  57.                         if ($cidr > 128) throw new OIDplusException(_L('Invalid IPv6'));
  58.                         $this->bare = ipv6_normalize($this->bare);
  59.                         $this->ipv6 = $this->bare . '/' . $this->cidr;
  60.                 }
  61.         }
  62.  
  63.         /**
  64.          * @param string $node_id
  65.          * @return OIDplusIpv6|null
  66.          * @throws OIDplusException
  67.          */
  68.         public static function parse(string $node_id)/*: ?OIDplusIpv6*/ {
  69.                 @list($namespace, $ipv6) = explode(':', $node_id, 2);
  70.                 if ($namespace !== self::ns()) return null;
  71.                 return new self($ipv6);
  72.         }
  73.  
  74.         /**
  75.          * @return string
  76.          */
  77.         public static function objectTypeTitle(): string {
  78.                 return _L('IPv6 Network Blocks');
  79.         }
  80.  
  81.         /**
  82.          * @return string
  83.          */
  84.         public static function objectTypeTitleShort(): string {
  85.                 return _L('IPv6');
  86.         }
  87.  
  88.         /**
  89.          * @return string
  90.          */
  91.         public static function ns(): string {
  92.                 return 'ipv6';
  93.         }
  94.  
  95.         /**
  96.          * @return string
  97.          */
  98.         public static function root(): string {
  99.                 return self::ns().':';
  100.         }
  101.  
  102.         /**
  103.          * @return bool
  104.          */
  105.         public function isRoot(): bool {
  106.                 return $this->ipv6 == '';
  107.         }
  108.  
  109.         /**
  110.          * @param bool $with_ns
  111.          * @return string
  112.          */
  113.         public function nodeId(bool $with_ns=true): string {
  114.                 return $with_ns ? self::root().$this->ipv6 : $this->ipv6;
  115.         }
  116.  
  117.         /**
  118.          * @param string $str
  119.          * @return string
  120.          * @throws OIDplusException
  121.          */
  122.         public function addString(string $str): string {
  123.                 if (strpos($str, '/') === false) $str .= "/128";
  124.  
  125.                 if (!$this->isRoot()) {
  126.                         if (!ipv6_in_cidr($this->bare.'/'.$this->cidr, $str)) {
  127.                                 throw new OIDplusException(_L('Cannot add this address, because it must be inside the address range of the superior range.'));
  128.                         }
  129.                 }
  130.  
  131.                 list($ipv6, $cidr) = explode('/', $str);
  132.                 if ($cidr < 0) throw new OIDplusException(_L('Invalid IPv6 address %1',$str));
  133.                 if ($cidr > 128) throw new OIDplusException(_L('Invalid IPv6 address %1',$str));
  134.                 $ipv6_normalized = ipv6_normalize($ipv6);
  135.                 if (!$ipv6_normalized) throw new OIDplusException(_L('Invalid IPv6 address %1',$str));
  136.                 return self::root().$ipv6_normalized.'/'.$cidr; // overwrite; no hierarchical tree
  137.         }
  138.  
  139.         /**
  140.          * @param OIDplusObject $parent
  141.          * @return string
  142.          */
  143.         public function crudShowId(OIDplusObject $parent): string {
  144.                 return $this->ipv6;
  145.         }
  146.  
  147.         /**
  148.          * @param OIDplusObject|null $parent
  149.          * @return string
  150.          */
  151.         public function jsTreeNodeName(OIDplusObject $parent = null): string {
  152.                 if ($parent == null) return $this->objectTypeTitle();
  153.                 return $this->ipv6;
  154.         }
  155.  
  156.         /**
  157.          * @return string
  158.          */
  159.         public function defaultTitle(): string {
  160.                 return $this->ipv6;
  161.         }
  162.  
  163.         /**
  164.          * @return bool
  165.          */
  166.         public function isLeafNode(): bool {
  167.                 return $this->cidr >= 128;
  168.         }
  169.  
  170.         /**
  171.          * @return array
  172.          */
  173.         private function getTechInfo(): array {
  174.                 if ($this->isRoot()) return array();
  175.  
  176.                 $tech_info = array();
  177.  
  178.                 $tech_info[_L('IPv6/CIDR')] = ipv6_normalize($this->bare) . '/' . $this->cidr;
  179.                 if ($this->cidr < 128) {
  180.                         $tech_info[_L('First address')] = ipv6_cidr_min_ip($this->bare . '/' . $this->cidr);
  181.                         $tech_info[_L('Last address')]  = ipv6_cidr_max_ip($this->bare . '/' . $this->cidr);
  182.                 }
  183.  
  184.                 return $tech_info;
  185.         }
  186.  
  187.         /**
  188.          * @param string $title
  189.          * @param string $content
  190.          * @param string $icon
  191.          * @return void
  192.          * @throws OIDplusException
  193.          */
  194.         public function getContentPage(string &$title, string &$content, string &$icon) {
  195.                 $icon = file_exists(__DIR__.'/img/main_icon.png') ? OIDplus::webpath(__DIR__,OIDplus::PATH_RELATIVE).'img/main_icon.png' : '';
  196.  
  197.                 if ($this->isRoot()) {
  198.                         $title = OIDplusIpv6::objectTypeTitle();
  199.  
  200.                         $res = OIDplus::db()->query("select * from ###objects where parent = ?", array(self::root()));
  201.                         if ($res->any()) {
  202.                                 $content  = '<p>'._L('Please select a network block in the tree view at the left to show its contents.').'</p>';
  203.                         } else {
  204.                                 $content  = '<p>'._L('Currently, no network blocks are registered in the system.').'</p>';
  205.                         }
  206.  
  207.                         if (!$this->isLeafNode()) {
  208.                                 if (OIDplus::authUtils()->isAdminLoggedIn()) {
  209.                                         $content .= '<h2>'._L('Manage root objects').'</h2>';
  210.                                 } else {
  211.                                         $content .= '<h2>'._L('Available objects').'</h2>';
  212.                                 }
  213.                                 $content .= '%%CRUD%%';
  214.                         }
  215.                 } else {
  216.                         $title = $this->getTitle();
  217.  
  218.                         $tech_info = $this->getTechInfo();
  219.                         $tech_info_html = '';
  220.                         if (count($tech_info) > 0) {
  221.                                 $tech_info_html .= '<h2>'._L('Technical information').'</h2>';
  222.                                 $tech_info_html .= '<div style="overflow:auto"><table border="0">';
  223.                                 foreach ($tech_info as $key => $value) {
  224.                                         $tech_info_html .= '<tr><td valign="top" style="white-space: nowrap;">'.$key.': </td><td><code>'.$value.'</code></td></tr>';
  225.                                 }
  226.                                 $tech_info_html .= '</table></div>';
  227.                         }
  228.                         if ($this->cidr == 128) $tech_info_html .= _L('Single host address');
  229.  
  230.                         $content = $tech_info_html;
  231.  
  232.                         $content .= '<h2>'._L('Description').'</h2>%%DESC%%';
  233.  
  234.                         if (!$this->isLeafNode()) {
  235.                                 if ($this->userHasWriteRights()) {
  236.                                         $content .= '<h2>'._L('Create or change subordinate objects').'</h2>';
  237.                                 } else {
  238.                                         $content .= '<h2>'._L('Subordinate objects').'</h2>';
  239.                                 }
  240.                                 $content .= '%%CRUD%%';
  241.                         }
  242.                 }
  243.         }
  244.  
  245.         /**
  246.          * @return OIDplusIpv6|null
  247.          */
  248.         public function one_up()/*: ?OIDplusIpv6*/ {
  249.                 $cidr = $this->cidr - 1;
  250.                 if ($cidr < 0) return null; // cannot go further up
  251.  
  252.                 $tmp = ipv6_normalize_range($this->bare . '/' . $cidr);
  253.                 return self::parse($this->ns() . ':' . $tmp);
  254.         }
  255.  
  256.         /**
  257.          * @param OIDplusObject|string $to
  258.          * @return float|int|mixed|string|null
  259.          */
  260.         public function distance($to) {
  261.                 if (!is_object($to)) $to = OIDplusObject::parse($to);
  262.                 if (!$to) return null;
  263.                 if (!($to instanceof $this)) return null;
  264.                 $res = ipv6_distance($to->ipv6, $this->ipv6);
  265.                 return $res !== false ? $res : null;
  266.         }
  267.  
  268.         /**
  269.          * @return string
  270.          */
  271.         public function getDirectoryName(): string {
  272.                 if ($this->isRoot()) return $this->ns();
  273.                 $bare = str_replace(':','_',ipv6_normalize($this->bare));
  274.                 if ($this->isLeafNode()) {
  275.                         return $this->ns().'_'.$bare;
  276.                 } else {
  277.                         return $this->ns().'_'.$bare.'__'.$this->cidr;
  278.                 }
  279.         }
  280.  
  281.         /**
  282.          * @param string $mode
  283.          * @return string
  284.          */
  285.         public static function treeIconFilename(string $mode): string {
  286.                 return 'img/'.$mode.'_icon16.png';
  287.         }
  288. }
  289.