Subversion Repositories oidplus

Rev

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