Subversion Repositories oidplus

Rev

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

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