Rev 1050 | Rev 1116 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
635 | daniel-mar | 1 | <?php |
2 | |||
3 | /* |
||
4 | * OIDplus 2.0 |
||
1086 | daniel-mar | 5 | * Copyright 2019 - 2023 Daniel Marschall, ViaThinkSoft |
635 | 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 | |||
1050 | daniel-mar | 20 | namespace ViaThinkSoft\OIDplus; |
635 | daniel-mar | 21 | |
1086 | daniel-mar | 22 | // phpcs:disable PSR1.Files.SideEffects |
23 | \defined('INSIDE_OIDPLUS') or die; |
||
24 | // phpcs:enable PSR1.Files.SideEffects |
||
25 | |||
635 | daniel-mar | 26 | class OIDplusIpv6 extends OIDplusObject { |
27 | private $ipv6; |
||
28 | private $bare; |
||
29 | private $cidr; |
||
30 | |||
31 | public function __construct($ipv6) { |
||
32 | $this->ipv6 = $ipv6; |
||
33 | |||
34 | if (!empty($ipv6)) { |
||
35 | if (strpos($ipv6, '/') === false) $ipv6 .= '/128'; |
||
36 | list($bare, $cidr) = explode('/', $ipv6); |
||
37 | $this->bare = $bare; |
||
38 | $this->cidr = $cidr; |
||
39 | if (!ipv6_valid($bare)) throw new OIDplusException(_L('Invalid IPv6')); |
||
40 | if (!is_numeric($cidr)) throw new OIDplusException(_L('Invalid IPv6')); |
||
41 | if ($cidr < 0) throw new OIDplusException(_L('Invalid IPv6')); |
||
42 | if ($cidr > 128) throw new OIDplusException(_L('Invalid IPv6')); |
||
43 | $this->bare = ipv6_normalize($this->bare); |
||
856 | daniel-mar | 44 | $this->ipv6 = $this->bare . '/' . $this->cidr; |
635 | daniel-mar | 45 | } |
46 | } |
||
47 | |||
48 | public static function parse($node_id) { |
||
49 | @list($namespace, $ipv6) = explode(':', $node_id, 2); |
||
860 | daniel-mar | 50 | if ($namespace !== self::ns()) return false; |
635 | daniel-mar | 51 | return new self($ipv6); |
52 | } |
||
53 | |||
54 | public static function objectTypeTitle() { |
||
55 | return _L('IPv6 Network Blocks'); |
||
56 | } |
||
57 | |||
58 | public static function objectTypeTitleShort() { |
||
59 | return _L('IPv6'); |
||
60 | } |
||
61 | |||
62 | public static function ns() { |
||
63 | return 'ipv6'; |
||
64 | } |
||
65 | |||
66 | public static function root() { |
||
860 | daniel-mar | 67 | return self::ns().':'; |
635 | daniel-mar | 68 | } |
69 | |||
70 | public function isRoot() { |
||
71 | return $this->ipv6 == ''; |
||
72 | } |
||
73 | |||
74 | public function nodeId($with_ns=true) { |
||
859 | daniel-mar | 75 | return $with_ns ? self::root().$this->ipv6 : $this->ipv6; |
635 | daniel-mar | 76 | } |
77 | |||
78 | public function addString($str) { |
||
79 | if (strpos($str, '/') === false) $str .= "/128"; |
||
80 | |||
81 | if (!$this->isRoot()) { |
||
82 | if (!ipv6_in_cidr($this->bare.'/'.$this->cidr, $str)) { |
||
83 | throw new OIDplusException(_L('Cannot add this address, because it must be inside the address range of the superior range.')); |
||
84 | } |
||
85 | } |
||
86 | |||
87 | list($ipv6, $cidr) = explode('/', $str); |
||
88 | if ($cidr < 0) throw new OIDplusException(_L('Invalid IPv6 address %1',$str)); |
||
89 | if ($cidr > 128) throw new OIDplusException(_L('Invalid IPv6 address %1',$str)); |
||
90 | $ipv6_normalized = ipv6_normalize($ipv6); |
||
91 | if (!$ipv6_normalized) throw new OIDplusException(_L('Invalid IPv6 address %1',$str)); |
||
859 | daniel-mar | 92 | return self::root().$ipv6_normalized.'/'.$cidr; // overwrite; no hierarchical tree |
635 | daniel-mar | 93 | } |
94 | |||
95 | public function crudShowId(OIDplusObject $parent) { |
||
96 | return $this->ipv6; |
||
97 | } |
||
98 | |||
99 | public function jsTreeNodeName(OIDplusObject $parent = null) { |
||
100 | if ($parent == null) return $this->objectTypeTitle(); |
||
101 | return $this->ipv6; |
||
102 | } |
||
103 | |||
104 | public function defaultTitle() { |
||
105 | return $this->ipv6; |
||
106 | } |
||
107 | |||
108 | public function isLeafNode() { |
||
109 | return $this->cidr >= 128; |
||
110 | } |
||
111 | |||
753 | daniel-mar | 112 | private function getTechInfo() { |
113 | if ($this->isRoot()) return array(); |
||
114 | |||
115 | $tech_info = array(); |
||
116 | |||
117 | $tech_info[_L('IPv6/CIDR')] = ipv6_normalize($this->bare) . '/' . $this->cidr; |
||
118 | if ($this->cidr < 128) { |
||
119 | $tech_info[_L('First address')] = ipv6_cidr_min_ip($this->bare . '/' . $this->cidr); |
||
120 | $tech_info[_L('Last address')] = ipv6_cidr_max_ip($this->bare . '/' . $this->cidr); |
||
121 | } |
||
122 | |||
123 | return $tech_info; |
||
124 | } |
||
125 | |||
635 | daniel-mar | 126 | public function getContentPage(&$title, &$content, &$icon) { |
801 | daniel-mar | 127 | $icon = file_exists(__DIR__.'/img/main_icon.png') ? OIDplus::webpath(__DIR__,OIDplus::PATH_RELATIVE).'img/main_icon.png' : ''; |
635 | daniel-mar | 128 | |
129 | if ($this->isRoot()) { |
||
130 | $title = OIDplusIpv6::objectTypeTitle(); |
||
131 | |||
132 | $res = OIDplus::db()->query("select * from ###objects where parent = ?", array(self::root())); |
||
790 | daniel-mar | 133 | if ($res->any()) { |
962 | daniel-mar | 134 | $content = '<p>'._L('Please select a network block in the tree view at the left to show its contents.').'</p>'; |
635 | daniel-mar | 135 | } else { |
962 | daniel-mar | 136 | $content = '<p>'._L('Currently, no network blocks are registered in the system.').'</p>'; |
635 | daniel-mar | 137 | } |
138 | |||
139 | if (!$this->isLeafNode()) { |
||
140 | if (OIDplus::authUtils()->isAdminLoggedIn()) { |
||
141 | $content .= '<h2>'._L('Manage root objects').'</h2>'; |
||
142 | } else { |
||
143 | $content .= '<h2>'._L('Available objects').'</h2>'; |
||
144 | } |
||
145 | $content .= '%%CRUD%%'; |
||
146 | } |
||
147 | } else { |
||
148 | $title = $this->getTitle(); |
||
149 | |||
753 | daniel-mar | 150 | $tech_info = $this->getTechInfo(); |
151 | $tech_info_html = ''; |
||
152 | if (count($tech_info) > 0) { |
||
153 | $tech_info_html .= '<h2>'._L('Technical information').'</h2>'; |
||
154 | $tech_info_html .= '<table border="0">'; |
||
155 | foreach ($tech_info as $key => $value) { |
||
156 | $tech_info_html .= '<tr><td>'.$key.': </td><td><code>'.$value.'</code></td></tr>'; |
||
157 | } |
||
158 | $tech_info_html .= '</table>'; |
||
635 | daniel-mar | 159 | } |
753 | daniel-mar | 160 | if ($this->cidr == 128) $tech_info_html .= _L('Single host address'); |
635 | daniel-mar | 161 | |
753 | daniel-mar | 162 | $content = $tech_info_html; |
163 | |||
635 | daniel-mar | 164 | $content .= '<h2>'._L('Description').'</h2>%%DESC%%'; |
165 | |||
166 | if (!$this->isLeafNode()) { |
||
167 | if ($this->userHasWriteRights()) { |
||
928 | daniel-mar | 168 | $content .= '<h2>'._L('Create or change subordinate objects').'</h2>'; |
635 | daniel-mar | 169 | } else { |
928 | daniel-mar | 170 | $content .= '<h2>'._L('Subordinate objects').'</h2>'; |
635 | daniel-mar | 171 | } |
172 | $content .= '%%CRUD%%'; |
||
173 | } |
||
174 | } |
||
175 | } |
||
176 | |||
177 | public function one_up() { |
||
178 | $cidr = $this->cidr - 1; |
||
179 | if ($cidr < 0) return false; // cannot go further up |
||
180 | |||
181 | $tmp = ipv6_normalize_range($this->bare . '/' . $cidr); |
||
182 | return self::parse($this->ns() . ':' . $tmp); |
||
183 | } |
||
184 | |||
185 | public function distance($to) { |
||
186 | if (!is_object($to)) $to = OIDplusObject::parse($to); |
||
187 | if (!($to instanceof $this)) return false; |
||
188 | return ipv6_distance($to->ipv6, $this->ipv6); |
||
189 | } |
||
190 | |||
191 | public function getDirectoryName() { |
||
192 | if ($this->isRoot()) return $this->ns(); |
||
193 | $bare = str_replace(':','_',ipv6_normalize($this->bare)); |
||
194 | if ($this->isLeafNode()) { |
||
195 | return $this->ns().'_'.$bare; |
||
196 | } else { |
||
197 | return $this->ns().'_'.$bare.'__'.$this->cidr; |
||
198 | } |
||
199 | } |
||
800 | daniel-mar | 200 | |
805 | daniel-mar | 201 | public static function treeIconFilename($mode) { |
800 | daniel-mar | 202 | return 'img/'.$mode.'_icon16.png'; |
203 | } |
||
707 | daniel-mar | 204 | } |