Subversion Repositories oidplus

Rev

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