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