Subversion Repositories oidplus

Rev

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