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 OIDplusIpv6 extends OIDplusObject {
  27.         private $ipv6;
  28.         private $bare;
  29.         private $cidr;
  30.  
  31.         public function __construct($ipv6) {
  32.                 $this->ipv6 = $ipv6;
  33.  
  34.                 if (!empty($ipv6)) {
  35.                         if (strpos($ipv6, '/') === false) $ipv6 .= '/128';
  36.                         list($bare, $cidr) = explode('/', $ipv6);
  37.                         $this->bare = $bare;
  38.                         $this->cidr = $cidr;
  39.                         if (!ipv6_valid($bare)) throw new OIDplusException(_L('Invalid IPv6'));
  40.                         if (!is_numeric($cidr)) throw new OIDplusException(_L('Invalid IPv6'));
  41.                         if ($cidr < 0) throw new OIDplusException(_L('Invalid IPv6'));
  42.                         if ($cidr > 128) throw new OIDplusException(_L('Invalid IPv6'));
  43.                         $this->bare = ipv6_normalize($this->bare);
  44.                         $this->ipv6 = $this->bare . '/' . $this->cidr;
  45.                 }
  46.         }
  47.  
  48.         public static function parse($node_id) {
  49.                 @list($namespace, $ipv6) = explode(':', $node_id, 2);
  50.                 if ($namespace !== self::ns()) return false;
  51.                 return new self($ipv6);
  52.         }
  53.  
  54.         public static function objectTypeTitle() {
  55.                 return _L('IPv6 Network Blocks');
  56.         }
  57.  
  58.         public static function objectTypeTitleShort() {
  59.                 return _L('IPv6');
  60.         }
  61.  
  62.         public static function ns() {
  63.                 return 'ipv6';
  64.         }
  65.  
  66.         public static function root() {
  67.                 return self::ns().':';
  68.         }
  69.  
  70.         public function isRoot() {
  71.                 return $this->ipv6 == '';
  72.         }
  73.  
  74.         public function nodeId($with_ns=true) {
  75.                 return $with_ns ? self::root().$this->ipv6 : $this->ipv6;
  76.         }
  77.  
  78.         public function addString($str) {
  79.                 if (strpos($str, '/') === false) $str .= "/128";
  80.  
  81.                 if (!$this->isRoot()) {
  82.                         if (!ipv6_in_cidr($this->bare.'/'.$this->cidr, $str)) {
  83.                                 throw new OIDplusException(_L('Cannot add this address, because it must be inside the address range of the superior range.'));
  84.                         }
  85.                 }
  86.  
  87.                 list($ipv6, $cidr) = explode('/', $str);
  88.                 if ($cidr < 0) throw new OIDplusException(_L('Invalid IPv6 address %1',$str));
  89.                 if ($cidr > 128) throw new OIDplusException(_L('Invalid IPv6 address %1',$str));
  90.                 $ipv6_normalized = ipv6_normalize($ipv6);
  91.                 if (!$ipv6_normalized) throw new OIDplusException(_L('Invalid IPv6 address %1',$str));
  92.                 return self::root().$ipv6_normalized.'/'.$cidr; // overwrite; no hierarchical tree
  93.         }
  94.  
  95.         public function crudShowId(OIDplusObject $parent) {
  96.                 return $this->ipv6;
  97.         }
  98.  
  99.         public function jsTreeNodeName(OIDplusObject $parent = null) {
  100.                 if ($parent == null) return $this->objectTypeTitle();
  101.                 return $this->ipv6;
  102.         }
  103.  
  104.         public function defaultTitle() {
  105.                 return $this->ipv6;
  106.         }
  107.  
  108.         public function isLeafNode() {
  109.                 return $this->cidr >= 128;
  110.         }
  111.  
  112.         private function getTechInfo() {
  113.                 if ($this->isRoot()) return array();
  114.  
  115.                 $tech_info = array();
  116.  
  117.                 $tech_info[_L('IPv6/CIDR')] = ipv6_normalize($this->bare) . '/' . $this->cidr;
  118.                 if ($this->cidr < 128) {
  119.                         $tech_info[_L('First address')] = ipv6_cidr_min_ip($this->bare . '/' . $this->cidr);
  120.                         $tech_info[_L('Last address')]  = ipv6_cidr_max_ip($this->bare . '/' . $this->cidr);
  121.                 }
  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 = OIDplusIpv6::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 network block in the tree view at the left to show its contents.').'</p>';
  135.                         } else {
  136.                                 $content  = '<p>'._L('Currently, no network blocks are registered in the system.').'</p>';
  137.                         }
  138.  
  139.                         if (!$this->isLeafNode()) {
  140.                                 if (OIDplus::authUtils()->isAdminLoggedIn()) {
  141.                                         $content .= '<h2>'._L('Manage root objects').'</h2>';
  142.                                 } else {
  143.                                         $content .= '<h2>'._L('Available objects').'</h2>';
  144.                                 }
  145.                                 $content .= '%%CRUD%%';
  146.                         }
  147.                 } else {
  148.                         $title = $this->getTitle();
  149.  
  150.                         $tech_info = $this->getTechInfo();
  151.                         $tech_info_html = '';
  152.                         if (count($tech_info) > 0) {
  153.                                 $tech_info_html .= '<h2>'._L('Technical information').'</h2>';
  154.                                 $tech_info_html .= '<table border="0">';
  155.                                 foreach ($tech_info as $key => $value) {
  156.                                         $tech_info_html .= '<tr><td>'.$key.': </td><td><code>'.$value.'</code></td></tr>';
  157.                                 }
  158.                                 $tech_info_html .= '</table>';
  159.                         }
  160.                         if ($this->cidr == 128) $tech_info_html .= _L('Single host address');
  161.  
  162.                         $content = $tech_info_html;
  163.  
  164.                         $content .= '<h2>'._L('Description').'</h2>%%DESC%%';
  165.  
  166.                         if (!$this->isLeafNode()) {
  167.                                 if ($this->userHasWriteRights()) {
  168.                                         $content .= '<h2>'._L('Create or change subordinate objects').'</h2>';
  169.                                 } else {
  170.                                         $content .= '<h2>'._L('Subordinate objects').'</h2>';
  171.                                 }
  172.                                 $content .= '%%CRUD%%';
  173.                         }
  174.                 }
  175.         }
  176.  
  177.         public function one_up() {
  178.                 $cidr = $this->cidr - 1;
  179.                 if ($cidr < 0) return false; // cannot go further up
  180.  
  181.                 $tmp = ipv6_normalize_range($this->bare . '/' . $cidr);
  182.                 return self::parse($this->ns() . ':' . $tmp);
  183.         }
  184.  
  185.         public function distance($to) {
  186.                 if (!is_object($to)) $to = OIDplusObject::parse($to);
  187.                 if (!($to instanceof $this)) return false;
  188.                 return ipv6_distance($to->ipv6, $this->ipv6);
  189.         }
  190.  
  191.         public function getDirectoryName() {
  192.                 if ($this->isRoot()) return $this->ns();
  193.                 $bare = str_replace(':','_',ipv6_normalize($this->bare));
  194.                 if ($this->isLeafNode()) {
  195.                         return $this->ns().'_'.$bare;
  196.                 } else {
  197.                         return $this->ns().'_'.$bare.'__'.$this->cidr;
  198.                 }
  199.         }
  200.  
  201.         public static function treeIconFilename($mode) {
  202.                 return 'img/'.$mode.'_icon16.png';
  203.         }
  204. }
  205.