Rev 1086 | Rev 1121 | 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 OIDplusIpv4 extends OIDplusObject { |
27 | private $ipv4; |
||
28 | private $bare; |
||
29 | private $cidr; |
||
30 | |||
1116 | daniel-mar | 31 | /** |
32 | * @param $ipv4 |
||
33 | * @throws OIDplusException |
||
34 | */ |
||
635 | daniel-mar | 35 | public function __construct($ipv4) { |
36 | $this->ipv4 = $ipv4; |
||
37 | |||
38 | if (!empty($ipv4)) { |
||
39 | if (strpos($ipv4, '/') === false) $ipv4 .= '/32'; |
||
40 | list($bare, $cidr) = explode('/', $ipv4); |
||
41 | $this->bare = $bare; |
||
42 | $this->cidr = $cidr; |
||
43 | if (!ipv4_valid($bare)) throw new OIDplusException(_L('Invalid IPv4')); |
||
44 | if (!is_numeric($cidr)) throw new OIDplusException(_L('Invalid IPv4')); |
||
45 | if ($cidr < 0) throw new OIDplusException(_L('Invalid IPv4')); |
||
46 | if ($cidr > 32) throw new OIDplusException(_L('Invalid IPv4')); |
||
47 | $this->bare = ipv4_normalize($this->bare); |
||
856 | daniel-mar | 48 | $this->ipv4 = $this->bare . '/' . $this->cidr; |
635 | daniel-mar | 49 | } |
50 | } |
||
51 | |||
1116 | daniel-mar | 52 | /** |
53 | * @param string $node_id |
||
54 | * @return OIDplusIpv4|null |
||
55 | */ |
||
56 | public static function parse(string $node_id)/*: ?OIDplusIpv4*/ { |
||
635 | daniel-mar | 57 | @list($namespace, $ipv4) = explode(':', $node_id, 2); |
1116 | daniel-mar | 58 | if ($namespace !== self::ns()) return null; |
635 | daniel-mar | 59 | return new self($ipv4); |
60 | } |
||
61 | |||
1116 | daniel-mar | 62 | /** |
63 | * @return string |
||
64 | */ |
||
65 | public static function objectTypeTitle(): string { |
||
635 | daniel-mar | 66 | return _L('IPv4 Network Blocks'); |
67 | } |
||
68 | |||
1116 | daniel-mar | 69 | /** |
70 | * @return string |
||
71 | */ |
||
72 | public static function objectTypeTitleShort(): string { |
||
635 | daniel-mar | 73 | return _L('IPv4'); |
74 | } |
||
75 | |||
1116 | daniel-mar | 76 | /** |
77 | * @return string |
||
78 | */ |
||
79 | public static function ns(): string { |
||
635 | daniel-mar | 80 | return 'ipv4'; |
81 | } |
||
82 | |||
1116 | daniel-mar | 83 | /** |
84 | * @return string |
||
85 | */ |
||
86 | public static function root(): string { |
||
860 | daniel-mar | 87 | return self::ns().':'; |
635 | daniel-mar | 88 | } |
89 | |||
1116 | daniel-mar | 90 | /** |
91 | * @return bool |
||
92 | */ |
||
93 | public function isRoot(): bool { |
||
635 | daniel-mar | 94 | return $this->ipv4 == ''; |
95 | } |
||
96 | |||
1116 | daniel-mar | 97 | /** |
98 | * @param bool $with_ns |
||
99 | * @return string |
||
100 | */ |
||
101 | public function nodeId(bool $with_ns=true): string { |
||
859 | daniel-mar | 102 | return $with_ns ? self::root().$this->ipv4 : $this->ipv4; |
635 | daniel-mar | 103 | } |
104 | |||
1116 | daniel-mar | 105 | /** |
106 | * @param string $str |
||
107 | * @return string |
||
108 | * @throws OIDplusException |
||
109 | */ |
||
110 | public function addString(string $str): string { |
||
635 | daniel-mar | 111 | if (strpos($str, '/') === false) $str .= "/32"; |
112 | |||
113 | if (!$this->isRoot()) { |
||
114 | if (!ipv4_in_cidr($this->bare.'/'.$this->cidr, $str)) { |
||
115 | throw new OIDplusException(_L('Cannot add this address, because it must be inside the address range of the superior range.')); |
||
116 | } |
||
117 | } |
||
118 | |||
119 | list($ipv4, $cidr) = explode('/', $str); |
||
120 | if ($cidr < 0) throw new OIDplusException(_L('Invalid IPv4 address %1',$str)); |
||
121 | if ($cidr > 32) throw new OIDplusException(_L('Invalid IPv4 address %1',$str)); |
||
122 | $ipv4_normalized = ipv4_normalize($ipv4); |
||
123 | if (!$ipv4_normalized) throw new OIDplusException(_L('Invalid IPv4 address %1',$str)); |
||
859 | daniel-mar | 124 | return self::root().$ipv4_normalized.'/'.$cidr; // overwrite; no hierarchical tree |
635 | daniel-mar | 125 | } |
126 | |||
1116 | daniel-mar | 127 | /** |
128 | * @param OIDplusObject $parent |
||
129 | * @return string |
||
130 | */ |
||
131 | public function crudShowId(OIDplusObject $parent): string { |
||
635 | daniel-mar | 132 | return $this->ipv4; |
133 | } |
||
134 | |||
1116 | daniel-mar | 135 | /** |
136 | * @param OIDplusObject|null $parent |
||
137 | * @return string |
||
138 | */ |
||
139 | public function jsTreeNodeName(OIDplusObject $parent = null): string { |
||
635 | daniel-mar | 140 | if ($parent == null) return $this->objectTypeTitle(); |
141 | return $this->ipv4; |
||
142 | } |
||
143 | |||
1116 | daniel-mar | 144 | /** |
145 | * @return string |
||
146 | */ |
||
147 | public function defaultTitle(): string { |
||
635 | daniel-mar | 148 | return $this->ipv4; |
149 | } |
||
150 | |||
1116 | daniel-mar | 151 | /** |
152 | * @return bool |
||
153 | */ |
||
154 | public function isLeafNode(): bool { |
||
635 | daniel-mar | 155 | return $this->cidr >= 32; |
156 | } |
||
157 | |||
1116 | daniel-mar | 158 | /** |
159 | * @return array |
||
160 | */ |
||
161 | private function getTechInfo(): array { |
||
753 | daniel-mar | 162 | if ($this->isRoot()) return array(); |
163 | |||
164 | $tech_info = array(); |
||
165 | |||
166 | $tech_info[_L('IPv4/CIDR')] = ipv4_normalize($this->bare) . '/' . $this->cidr; |
||
167 | if ($this->cidr < 32) { |
||
168 | $tech_info[_L('First address')] = ipv4_cidr_min_ip($this->bare . '/' . $this->cidr); |
||
169 | $tech_info[_L('Last address')] = ipv4_cidr_max_ip($this->bare . '/' . $this->cidr); |
||
170 | } |
||
171 | |||
172 | return $tech_info; |
||
173 | } |
||
174 | |||
1116 | daniel-mar | 175 | /** |
176 | * @param string $title |
||
177 | * @param string $content |
||
178 | * @param string $icon |
||
179 | * @return void |
||
180 | * @throws OIDplusException |
||
181 | */ |
||
182 | public function getContentPage(string &$title, string &$content, string &$icon) { |
||
801 | daniel-mar | 183 | $icon = file_exists(__DIR__.'/img/main_icon.png') ? OIDplus::webpath(__DIR__,OIDplus::PATH_RELATIVE).'img/main_icon.png' : ''; |
635 | daniel-mar | 184 | |
185 | if ($this->isRoot()) { |
||
186 | $title = OIDplusIpv4::objectTypeTitle(); |
||
187 | |||
188 | $res = OIDplus::db()->query("select * from ###objects where parent = ?", array(self::root())); |
||
790 | daniel-mar | 189 | if ($res->any()) { |
962 | daniel-mar | 190 | $content = '<p>'._L('Please select a network block in the tree view at the left to show its contents.').'</p>'; |
635 | daniel-mar | 191 | } else { |
962 | daniel-mar | 192 | $content = '<p>'._L('Currently, no network blocks are registered in the system.').'</p>'; |
635 | daniel-mar | 193 | } |
194 | |||
195 | if (!$this->isLeafNode()) { |
||
196 | if (OIDplus::authUtils()->isAdminLoggedIn()) { |
||
197 | $content .= '<h2>'._L('Manage root objects').'</h2>'; |
||
198 | } else { |
||
199 | $content .= '<h2>'._L('Available objects').'</h2>'; |
||
200 | } |
||
201 | $content .= '%%CRUD%%'; |
||
202 | } |
||
203 | } else { |
||
204 | $title = $this->getTitle(); |
||
205 | |||
753 | daniel-mar | 206 | $tech_info = $this->getTechInfo(); |
207 | $tech_info_html = ''; |
||
208 | if (count($tech_info) > 0) { |
||
209 | $tech_info_html .= '<h2>'._L('Technical information').'</h2>'; |
||
210 | $tech_info_html .= '<table border="0">'; |
||
211 | foreach ($tech_info as $key => $value) { |
||
212 | $tech_info_html .= '<tr><td>'.$key.': </td><td><code>'.$value.'</code></td></tr>'; |
||
213 | } |
||
214 | $tech_info_html .= '</table>'; |
||
635 | daniel-mar | 215 | } |
753 | daniel-mar | 216 | if ($this->cidr == 32) $tech_info_html .= _L('Single host address'); |
635 | daniel-mar | 217 | |
753 | daniel-mar | 218 | $content = $tech_info_html; |
219 | |||
635 | daniel-mar | 220 | $content .= '<h2>'._L('Description').'</h2>%%DESC%%'; |
221 | |||
222 | if (!$this->isLeafNode()) { |
||
223 | if ($this->userHasWriteRights()) { |
||
928 | daniel-mar | 224 | $content .= '<h2>'._L('Create or change subordinate objects').'</h2>'; |
635 | daniel-mar | 225 | } else { |
928 | daniel-mar | 226 | $content .= '<h2>'._L('Subordinate objects').'</h2>'; |
635 | daniel-mar | 227 | } |
228 | $content .= '%%CRUD%%'; |
||
229 | } |
||
230 | } |
||
231 | } |
||
232 | |||
1116 | daniel-mar | 233 | /** |
234 | * @return OIDplusIpv4|null |
||
235 | */ |
||
236 | public function one_up()/*: ?OIDplusIpv4*/ { |
||
635 | daniel-mar | 237 | $cidr = $this->cidr - 1; |
1116 | daniel-mar | 238 | if ($cidr < 0) return null; // cannot go further up |
635 | daniel-mar | 239 | |
240 | $tmp = ipv4_normalize_range($this->bare . '/' . $cidr); |
||
241 | return self::parse($this->ns() . ':' . $tmp); |
||
242 | } |
||
243 | |||
1116 | daniel-mar | 244 | /** |
245 | * @param $to |
||
246 | * @return float|int|mixed|string|null |
||
247 | */ |
||
635 | daniel-mar | 248 | public function distance($to) { |
249 | if (!is_object($to)) $to = OIDplusObject::parse($to); |
||
1116 | daniel-mar | 250 | if (!($to instanceof $this)) return null; |
251 | $res = ipv4_distance($to->ipv4, $this->ipv4); |
||
252 | return $res !== false ? $res : null; |
||
635 | daniel-mar | 253 | } |
254 | |||
1116 | daniel-mar | 255 | /** |
256 | * @return string |
||
257 | */ |
||
258 | public function getDirectoryName(): string { |
||
635 | daniel-mar | 259 | if ($this->isRoot()) return $this->ns(); |
260 | $bare = str_replace('.','_',ipv4_normalize($this->bare)); |
||
261 | if ($this->isLeafNode()) { |
||
262 | return $this->ns().'_'.$bare; |
||
263 | } else { |
||
264 | return $this->ns().'_'.$bare.'__'.$this->cidr; |
||
265 | } |
||
266 | } |
||
800 | daniel-mar | 267 | |
1116 | daniel-mar | 268 | /** |
269 | * @param string $mode |
||
270 | * @return string |
||
271 | */ |
||
272 | public static function treeIconFilename(string $mode): string { |
||
800 | daniel-mar | 273 | return 'img/'.$mode.'_icon16.png'; |
274 | } |
||
707 | daniel-mar | 275 | } |