Subversion Repositories oidplus

Rev

Rev 360 | Go to most recent revision | Blame | Last modification | View Log | RSS feed

  1. <?php
  2.  
  3. /*
  4.  * OIDplus 2.0
  5.  * Copyright 2019 - 2021 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. if (!defined('INSIDE_OIDPLUS')) die();
  21.  
  22. class OIDplusIpv6 extends OIDplusObject {
  23.         private $ipv6;
  24.         private $bare;
  25.         private $cidr;
  26.  
  27.         public function __construct($ipv6) {
  28.                 $this->ipv6 = $ipv6;
  29.  
  30.                 if (!empty($ipv6)) {
  31.                         if (strpos($ipv6, '/') === false) $ipv6 .= '/128';
  32.                         list($bare, $cidr) = explode('/', $ipv6);
  33.                         $this->bare = $bare;
  34.                         $this->cidr = $cidr;
  35.                         if (!ipv6_valid($bare)) throw new OIDplusException(_L('Invalid IPv6'));
  36.                         if (!is_numeric($cidr)) throw new OIDplusException(_L('Invalid IPv6'));
  37.                         if ($cidr < 0) throw new OIDplusException(_L('Invalid IPv6'));
  38.                         if ($cidr > 128) throw new OIDplusException(_L('Invalid IPv6'));
  39.                         $this->bare = ipv6_normalize($this->bare);
  40.                 }
  41.         }
  42.  
  43.         public static function parse($node_id) {
  44.                 @list($namespace, $ipv6) = explode(':', $node_id, 2);
  45.                 if ($namespace !== 'ipv6') return false;
  46.                 return new self($ipv6);
  47.         }
  48.  
  49.         public static function objectTypeTitle() {
  50.                 return _L('IPv6 Network Blocks');
  51.         }
  52.  
  53.         public static function objectTypeTitleShort() {
  54.                 return _L('IPv6');
  55.         }
  56.  
  57.         public static function ns() {
  58.                 return 'ipv6';
  59.         }
  60.  
  61.         public static function root() {
  62.                 return 'ipv6:';
  63.         }
  64.  
  65.         public function isRoot() {
  66.                 return $this->ipv6 == '';
  67.         }
  68.  
  69.         public function nodeId($with_ns=true) {
  70.                 return $with_ns ? 'ipv6:'.$this->ipv6 : $this->ipv6;
  71.         }
  72.  
  73.         public function addString($str) {
  74.                 if (strpos($str, '/') === false) $str .= "/128";
  75.  
  76.                 if (!$this->isRoot()) {
  77.                         if (!ipv6_in_cidr($this->bare.'/'.$this->cidr, $str)) {
  78.                                 throw new OIDplusException(_L('Cannot add this address, because it must be inside the address range of the superior range.'));
  79.                         }
  80.                 }
  81.  
  82.                 list($ipv6, $cidr) = explode('/', $str);
  83.                 if ($cidr < 0) throw new OIDplusException(_L('Invalid IPv6 address %1',$str));
  84.                 if ($cidr > 128) throw new OIDplusException(_L('Invalid IPv6 address %1',$str));
  85.                 $ipv6_normalized = ipv6_normalize($ipv6);
  86.                 if (!$ipv6_normalized) throw new OIDplusException(_L('Invalid IPv6 address %1',$str));
  87.                 return 'ipv6:'.$ipv6_normalized.'/'.$cidr; // overwrite; no hierarchical tree
  88.         }
  89.  
  90.         public function crudShowId(OIDplusObject $parent) {
  91.                 return $this->ipv6;
  92.         }
  93.  
  94.         public function crudInsertPrefix() {
  95.                 return '';
  96.         }
  97.  
  98.         public function jsTreeNodeName(OIDplusObject $parent = null) {
  99.                 if ($parent == null) return $this->objectTypeTitle();
  100.                 return $this->ipv6;
  101.         }
  102.  
  103.         public function defaultTitle() {
  104.                 return $this->ipv6;
  105.         }
  106.  
  107.         public function isLeafNode() {
  108.                 return $this->cidr >= 128;
  109.         }
  110.  
  111.         public function getContentPage(&$title, &$content, &$icon) {
  112.                 $icon = file_exists(__DIR__.'/icon_big.png') ? 'plugins/objectTypes/'.basename(__DIR__).'/icon_big.png' : '';
  113.  
  114.                 if ($this->isRoot()) {
  115.                         $title = OIDplusIpv6::objectTypeTitle();
  116.  
  117.                         $res = OIDplus::db()->query("select * from ###objects where parent = ?", array(self::root()));
  118.                         if ($res->num_rows() > 0) {
  119.                                 $content  = _L('Please select a network block in the tree view at the left to show its contents.');
  120.                         } else {
  121.                                 $content  = _L('Currently, no network blocks are registered in the system.');
  122.                         }
  123.  
  124.                         if (!$this->isLeafNode()) {
  125.                                 if (OIDplus::authUtils()::isAdminLoggedIn()) {
  126.                                         $content .= '<h2>'._L('Manage root objects').'</h2>';
  127.                                 } else {
  128.                                         $content .= '<h2>'._L('Available objects').'</h2>';
  129.                                 }
  130.                                 $content .= '%%CRUD%%';
  131.                         }
  132.                 } else {
  133.                         $title = $this->getTitle();
  134.  
  135.                         $content = '<h2>'._L('Technical information').'</h2>';
  136.  
  137.                         $content .= '<p>'._L('IPv6/CIDR').': <code>' . ipv6_normalize($this->bare) . '/' . $this->cidr . '</code><br>';
  138.                         if ($this->cidr < 128) {
  139.                                 $content .= _L('First address').': <code>' . ipv6_cidr_min_ip($this->bare . '/' . $this->cidr) . '</code><br>';
  140.                                 $content .= _L('Last address').': <code>' . ipv6_cidr_max_ip($this->bare . '/' . $this->cidr) . '</code></p>';
  141.                         } else {
  142.                                 $content .= _L('Single host address').'</p>';
  143.                         }
  144.  
  145.                         $content .= '<h2>'._L('Description').'</h2>%%DESC%%';
  146.  
  147.                         if (!$this->isLeafNode()) {
  148.                                 if ($this->userHasWriteRights()) {
  149.                                         $content .= '<h2>'._L('Create or change subsequent objects').'</h2>';
  150.                                 } else {
  151.                                         $content .= '<h2>'._L('Subsequent objects').'</h2>';
  152.                                 }
  153.                                 $content .= '%%CRUD%%';
  154.                         }
  155.                 }
  156.         }
  157.  
  158.         public function one_up() {
  159.                 $cidr = $this->cidr - 1;
  160.                 if ($cidr < 0) return false; // cannot go further up
  161.  
  162.                 $tmp = ipv6_normalize_range($this->bare . '/' . $cidr);
  163.                 return self::parse($this->ns() . ':' . $tmp);
  164.         }
  165.  
  166.         public function distance($to) {
  167.                 if (!is_object($to)) $to = OIDplusObject::parse($to);
  168.                 if (!($to instanceof $this)) return false;
  169.                 return ipv6_distance($to->ipv6, $this->ipv6);
  170.         }
  171. }