Subversion Repositories oidplus

Rev

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