Subversion Repositories oidplus

Rev

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