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