Subversion Repositories oidplus

Rev

Rev 790 | Rev 801 | 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
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 jsTreeNodeName(OIDplusObject $parent = null) {
95
                if ($parent == null) return $this->objectTypeTitle();
96
                return $this->ipv4;
97
        }
98
 
99
        public function defaultTitle() {
100
                return $this->ipv4;
101
        }
102
 
103
        public function isLeafNode() {
104
                return $this->cidr >= 32;
105
        }
106
 
753 daniel-mar 107
        private function getTechInfo() {
108
                if ($this->isRoot()) return array();
109
 
110
                $tech_info = array();
111
 
112
                $tech_info[_L('IPv4/CIDR')] = ipv4_normalize($this->bare) . '/' . $this->cidr;
113
                if ($this->cidr < 32) {
114
                        $tech_info[_L('First address')] = ipv4_cidr_min_ip($this->bare . '/' . $this->cidr);
115
                        $tech_info[_L('Last address')]  = ipv4_cidr_max_ip($this->bare . '/' . $this->cidr);
116
                }
117
 
118
                return $tech_info;
119
        }
120
 
635 daniel-mar 121
        public function getContentPage(&$title, &$content, &$icon) {
800 daniel-mar 122
                $icon = file_exists(__DIR__.'/img/main_icon.png') ? OIDplus::webPath(__DIR__,true).'img/main_icon.png' : '';
635 daniel-mar 123
 
124
                if ($this->isRoot()) {
125
                        $title = OIDplusIpv4::objectTypeTitle();
126
 
127
                        $res = OIDplus::db()->query("select * from ###objects where parent = ?", array(self::root()));
790 daniel-mar 128
                        if ($res->any()) {
635 daniel-mar 129
                                $content  = _L('Please select a network block in the tree view at the left to show its contents.');
130
                        } else {
131
                                $content  = _L('Currently, no network blocks are registered in the system.');
132
                        }
133
 
134
                        if (!$this->isLeafNode()) {
135
                                if (OIDplus::authUtils()->isAdminLoggedIn()) {
136
                                        $content .= '<h2>'._L('Manage root objects').'</h2>';
137
                                } else {
138
                                        $content .= '<h2>'._L('Available objects').'</h2>';
139
                                }
140
                                $content .= '%%CRUD%%';
141
                        }
142
                } else {
143
                        $title = $this->getTitle();
144
 
753 daniel-mar 145
                        $tech_info = $this->getTechInfo();
146
                        $tech_info_html = '';
147
                        if (count($tech_info) > 0) {
148
                                $tech_info_html .= '<h2>'._L('Technical information').'</h2>';
149
                                $tech_info_html .= '<table border="0">';
150
                                foreach ($tech_info as $key => $value) {
151
                                        $tech_info_html .= '<tr><td>'.$key.': </td><td><code>'.$value.'</code></td></tr>';
152
                                }
153
                                $tech_info_html .= '</table>';
635 daniel-mar 154
                        }
753 daniel-mar 155
                        if ($this->cidr == 32) $tech_info_html .= _L('Single host address');
635 daniel-mar 156
 
753 daniel-mar 157
                        $content = $tech_info_html;
158
 
635 daniel-mar 159
                        $content .= '<h2>'._L('Description').'</h2>%%DESC%%';
160
 
161
                        if (!$this->isLeafNode()) {
162
                                if ($this->userHasWriteRights()) {
163
                                        $content .= '<h2>'._L('Create or change subsequent objects').'</h2>';
164
                                } else {
165
                                        $content .= '<h2>'._L('Subsequent objects').'</h2>';
166
                                }
167
                                $content .= '%%CRUD%%';
168
                        }
169
                }
170
        }
171
 
172
        public function one_up() {
173
                $cidr = $this->cidr - 1;
174
                if ($cidr < 0) return false; // cannot go further up
175
 
176
                $tmp = ipv4_normalize_range($this->bare . '/' . $cidr);
177
                return self::parse($this->ns() . ':' . $tmp);
178
        }
179
 
180
        public function distance($to) {
181
                if (!is_object($to)) $to = OIDplusObject::parse($to);
182
                if (!($to instanceof $this)) return false;
183
                return ipv4_distance($to->ipv4, $this->ipv4);
184
        }
185
 
186
        public function getDirectoryName() {
187
                if ($this->isRoot()) return $this->ns();
188
                $bare = str_replace('.','_',ipv4_normalize($this->bare));
189
                if ($this->isLeafNode()) {
190
                        return $this->ns().'_'.$bare;
191
                } else {
192
                        return $this->ns().'_'.$bare.'__'.$this->cidr;
193
                }
194
        }
800 daniel-mar 195
 
196
        public function rootIconname($mode) {
197
                return 'img/'.$mode.'_icon16.png';
198
        }
707 daniel-mar 199
}