Rev 360 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
2 | daniel-mar | 1 | <?php |
2 | |||
3 | /* |
||
4 | * OIDplus 2.0 |
||
511 | daniel-mar | 5 | * Copyright 2019 - 2021 Daniel Marschall, ViaThinkSoft |
2 | daniel-mar | 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 | |||
511 | daniel-mar | 20 | if (!defined('INSIDE_OIDPLUS')) die(); |
21 | |||
227 | daniel-mar | 22 | class OIDplusIpv6 extends OIDplusObject { |
23 | private $ipv6; |
||
24 | private $bare; |
||
25 | private $cidr; |
||
26 | |||
2 | daniel-mar | 27 | public function __construct($ipv6) { |
28 | $this->ipv6 = $ipv6; |
||
17 | daniel-mar | 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; |
||
360 | daniel-mar | 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')); |
||
184 | daniel-mar | 39 | $this->bare = ipv6_normalize($this->bare); |
17 | daniel-mar | 40 | } |
2 | daniel-mar | 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() { |
||
360 | daniel-mar | 50 | return _L('IPv6 Network Blocks'); |
2 | daniel-mar | 51 | } |
52 | |||
53 | public static function objectTypeTitleShort() { |
||
360 | daniel-mar | 54 | return _L('IPv6'); |
2 | daniel-mar | 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 | |||
247 | daniel-mar | 69 | public function nodeId($with_ns=true) { |
70 | return $with_ns ? 'ipv6:'.$this->ipv6 : $this->ipv6; |
||
2 | daniel-mar | 71 | } |
72 | |||
73 | public function addString($str) { |
||
188 | daniel-mar | 74 | if (strpos($str, '/') === false) $str .= "/128"; |
75 | |||
18 | daniel-mar | 76 | if (!$this->isRoot()) { |
189 | daniel-mar | 77 | if (!ipv6_in_cidr($this->bare.'/'.$this->cidr, $str)) { |
360 | daniel-mar | 78 | throw new OIDplusException(_L('Cannot add this address, because it must be inside the address range of the superior range.')); |
18 | daniel-mar | 79 | } |
80 | } |
||
81 | |||
188 | daniel-mar | 82 | list($ipv6, $cidr) = explode('/', $str); |
360 | daniel-mar | 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)); |
||
189 | daniel-mar | 85 | $ipv6_normalized = ipv6_normalize($ipv6); |
360 | daniel-mar | 86 | if (!$ipv6_normalized) throw new OIDplusException(_L('Invalid IPv6 address %1',$str)); |
189 | daniel-mar | 87 | return 'ipv6:'.$ipv6_normalized.'/'.$cidr; // overwrite; no hierarchical tree |
2 | daniel-mar | 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 | |||
16 | daniel-mar | 107 | public function isLeafNode() { |
17 | daniel-mar | 108 | return $this->cidr >= 128; |
16 | daniel-mar | 109 | } |
110 | |||
34 | daniel-mar | 111 | public function getContentPage(&$title, &$content, &$icon) { |
55 | daniel-mar | 112 | $icon = file_exists(__DIR__.'/icon_big.png') ? 'plugins/objectTypes/'.basename(__DIR__).'/icon_big.png' : ''; |
113 | |||
2 | daniel-mar | 114 | if ($this->isRoot()) { |
115 | $title = OIDplusIpv6::objectTypeTitle(); |
||
116 | |||
261 | daniel-mar | 117 | $res = OIDplus::db()->query("select * from ###objects where parent = ?", array(self::root())); |
236 | daniel-mar | 118 | if ($res->num_rows() > 0) { |
360 | daniel-mar | 119 | $content = _L('Please select a network block in the tree view at the left to show its contents.'); |
2 | daniel-mar | 120 | } else { |
360 | daniel-mar | 121 | $content = _L('Currently, no network blocks are registered in the system.'); |
2 | daniel-mar | 122 | } |
123 | |||
16 | daniel-mar | 124 | if (!$this->isLeafNode()) { |
125 | if (OIDplus::authUtils()::isAdminLoggedIn()) { |
||
360 | daniel-mar | 126 | $content .= '<h2>'._L('Manage root objects').'</h2>'; |
16 | daniel-mar | 127 | } else { |
360 | daniel-mar | 128 | $content .= '<h2>'._L('Available objects').'</h2>'; |
16 | daniel-mar | 129 | } |
130 | $content .= '%%CRUD%%'; |
||
2 | daniel-mar | 131 | } |
132 | } else { |
||
192 | daniel-mar | 133 | $title = $this->getTitle(); |
134 | |||
360 | daniel-mar | 135 | $content = '<h2>'._L('Technical information').'</h2>'; |
2 | daniel-mar | 136 | |
360 | daniel-mar | 137 | $content .= '<p>'._L('IPv6/CIDR').': <code>' . ipv6_normalize($this->bare) . '/' . $this->cidr . '</code><br>'; |
17 | daniel-mar | 138 | if ($this->cidr < 128) { |
360 | daniel-mar | 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>'; |
||
17 | daniel-mar | 141 | } else { |
360 | daniel-mar | 142 | $content .= _L('Single host address').'</p>'; |
17 | daniel-mar | 143 | } |
144 | |||
360 | daniel-mar | 145 | $content .= '<h2>'._L('Description').'</h2>%%DESC%%'; |
17 | daniel-mar | 146 | |
16 | daniel-mar | 147 | if (!$this->isLeafNode()) { |
148 | if ($this->userHasWriteRights()) { |
||
360 | daniel-mar | 149 | $content .= '<h2>'._L('Create or change subsequent objects').'</h2>'; |
16 | daniel-mar | 150 | } else { |
360 | daniel-mar | 151 | $content .= '<h2>'._L('Subsequent objects').'</h2>'; |
16 | daniel-mar | 152 | } |
153 | $content .= '%%CRUD%%'; |
||
2 | daniel-mar | 154 | } |
155 | } |
||
156 | } |
||
20 | daniel-mar | 157 | |
158 | public function one_up() { |
||
159 | $cidr = $this->cidr - 1; |
||
160 | if ($cidr < 0) return false; // cannot go further up |
||
161 | |||
36 | daniel-mar | 162 | $tmp = ipv6_normalize_range($this->bare . '/' . $cidr); |
163 | return self::parse($this->ns() . ':' . $tmp); |
||
20 | daniel-mar | 164 | } |
165 | |||
166 | public function distance($to) { |
||
167 | if (!is_object($to)) $to = OIDplusObject::parse($to); |
||
28 | daniel-mar | 168 | if (!($to instanceof $this)) return false; |
20 | daniel-mar | 169 | return ipv6_distance($to->ipv6, $this->ipv6); |
170 | } |
||
360 | daniel-mar | 171 | } |