Rev 644 | Go to most recent revision | Details | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
635 | daniel-mar | 1 | <?php |
2 | |||
3 | /* |
||
4 | * OIDplus 2.0 |
||
5 | * Copyright 2019 - 2021 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('INSIDE_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 OIDplusException(_L('Invalid IPv4')); |
||
36 | if (!is_numeric($cidr)) throw new OIDplusException(_L('Invalid IPv4')); |
||
37 | if ($cidr < 0) throw new OIDplusException(_L('Invalid IPv4')); |
||
38 | if ($cidr > 32) throw new OIDplusException(_L('Invalid IPv4')); |
||
39 | $this->bare = ipv4_normalize($this->bare); |
||
40 | } |
||
41 | } |
||
42 | |||
43 | public static function parse($node_id) { |
||
44 | @list($namespace, $ipv4) = explode(':', $node_id, 2); |
||
45 | if ($namespace !== 'ipv4') return false; |
||
46 | return new self($ipv4); |
||
47 | } |
||
48 | |||
49 | public static function objectTypeTitle() { |
||
50 | return _L('IPv4 Network Blocks'); |
||
51 | } |
||
52 | |||
53 | public static function objectTypeTitleShort() { |
||
54 | return _L('IPv4'); |
||
55 | } |
||
56 | |||
57 | public static function ns() { |
||
58 | return 'ipv4'; |
||
59 | } |
||
60 | |||
61 | public static function root() { |
||
62 | return 'ipv4:'; |
||
63 | } |
||
64 | |||
65 | public function isRoot() { |
||
66 | return $this->ipv4 == ''; |
||
67 | } |
||
68 | |||
69 | public function nodeId($with_ns=true) { |
||
70 | return $with_ns ? 'ipv4:'.$this->ipv4 : $this->ipv4; |
||
71 | } |
||
72 | |||
73 | public function addString($str) { |
||
74 | if (strpos($str, '/') === false) $str .= "/32"; |
||
75 | |||
76 | if (!$this->isRoot()) { |
||
77 | if (!ipv4_in_cidr($this->bare.'/'.$this->cidr, $str)) { |
||
78 | throw new OIDplusException(_L('Cannot add this address, because it must be inside the address range of the superior range.')); |
||
79 | } |
||
80 | } |
||
81 | |||
82 | list($ipv4, $cidr) = explode('/', $str); |
||
83 | if ($cidr < 0) throw new OIDplusException(_L('Invalid IPv4 address %1',$str)); |
||
84 | if ($cidr > 32) throw new OIDplusException(_L('Invalid IPv4 address %1',$str)); |
||
85 | $ipv4_normalized = ipv4_normalize($ipv4); |
||
86 | if (!$ipv4_normalized) throw new OIDplusException(_L('Invalid IPv4 address %1',$str)); |
||
87 | return 'ipv4:'.$ipv4_normalized.'/'.$cidr; // overwrite; no hierarchical tree |
||
88 | } |
||
89 | |||
90 | public function crudShowId(OIDplusObject $parent) { |
||
91 | return $this->ipv4; |
||
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->ipv4; |
||
101 | } |
||
102 | |||
103 | public function defaultTitle() { |
||
104 | return $this->ipv4; |
||
105 | } |
||
106 | |||
107 | public function isLeafNode() { |
||
108 | return $this->cidr >= 32; |
||
109 | } |
||
110 | |||
111 | public function getContentPage(&$title, &$content, &$icon) { |
||
112 | $icon = file_exists(__DIR__.'/icon_big.png') ? OIDplus::webPath(__DIR__,true).'/icon_big.png' : ''; |
||
113 | |||
114 | if ($this->isRoot()) { |
||
115 | $title = OIDplusIpv4::objectTypeTitle(); |
||
116 | |||
117 | $res = OIDplus::db()->query("select * from ###objects where parent = ?", array(self::root())); |
||
118 | if ($res->num_rows() > 0) { |
||
119 | $content = _L('Please select a network block in the tree view at the left to show its contents.'); |
||
120 | } else { |
||
121 | $content = _L('Currently, no network blocks are registered in the system.'); |
||
122 | } |
||
123 | |||
124 | if (!$this->isLeafNode()) { |
||
125 | if (OIDplus::authUtils()->isAdminLoggedIn()) { |
||
126 | $content .= '<h2>'._L('Manage root objects').'</h2>'; |
||
127 | } else { |
||
128 | $content .= '<h2>'._L('Available objects').'</h2>'; |
||
129 | } |
||
130 | $content .= '%%CRUD%%'; |
||
131 | } |
||
132 | } else { |
||
133 | $title = $this->getTitle(); |
||
134 | |||
135 | $content = '<h2>'._L('Technical information').'</h2>'; |
||
136 | |||
137 | $content .= '<p>'._L('IPv4/CIDR').': <code>' . ipv4_normalize($this->bare) . '/' . $this->cidr . '</code><br>'; |
||
138 | if ($this->cidr < 32) { |
||
139 | $content .= _L('First address').': <code>' . ipv4_cidr_min_ip($this->bare . '/' . $this->cidr) . '</code><br>'; |
||
140 | $content .= _L('Last address').': <code>' . ipv4_cidr_max_ip($this->bare . '/' . $this->cidr) . '</code></p>'; |
||
141 | } else { |
||
142 | $content .= _L('Single host address').'</p>'; |
||
143 | } |
||
144 | |||
145 | $content .= '<h2>'._L('Description').'</h2>%%DESC%%'; |
||
146 | |||
147 | if (!$this->isLeafNode()) { |
||
148 | if ($this->userHasWriteRights()) { |
||
149 | $content .= '<h2>'._L('Create or change subsequent objects').'</h2>'; |
||
150 | } else { |
||
151 | $content .= '<h2>'._L('Subsequent objects').'</h2>'; |
||
152 | } |
||
153 | $content .= '%%CRUD%%'; |
||
154 | } |
||
155 | } |
||
156 | } |
||
157 | |||
158 | public function one_up() { |
||
159 | $cidr = $this->cidr - 1; |
||
160 | if ($cidr < 0) return false; // cannot go further up |
||
161 | |||
162 | $tmp = ipv4_normalize_range($this->bare . '/' . $cidr); |
||
163 | return self::parse($this->ns() . ':' . $tmp); |
||
164 | } |
||
165 | |||
166 | public function distance($to) { |
||
167 | if (!is_object($to)) $to = OIDplusObject::parse($to); |
||
168 | if (!($to instanceof $this)) return false; |
||
169 | return ipv4_distance($to->ipv4, $this->ipv4); |
||
170 | } |
||
171 | |||
172 | public function getDirectoryName() { |
||
173 | if ($this->isRoot()) return $this->ns(); |
||
174 | $bare = str_replace('.','_',ipv4_normalize($this->bare)); |
||
175 | if ($this->isLeafNode()) { |
||
176 | return $this->ns().'_'.$bare; |
||
177 | } else { |
||
178 | return $this->ns().'_'.$bare.'__'.$this->cidr; |
||
179 | } |
||
180 | } |
||
181 | } |