Subversion Repositories oidplus

Rev

Rev 644 | Rev 753 | 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
 
107
        public function getContentPage(&$title, &$content, &$icon) {
644 daniel-mar 108
                $icon = file_exists(__DIR__.'/icon_big.png') ? OIDplus::webPath(__DIR__,true).'icon_big.png' : '';
635 daniel-mar 109
 
110
                if ($this->isRoot()) {
111
                        $title = OIDplusIpv4::objectTypeTitle();
112
 
113
                        $res = OIDplus::db()->query("select * from ###objects where parent = ?", array(self::root()));
114
                        if ($res->num_rows() > 0) {
115
                                $content  = _L('Please select a network block in the tree view at the left to show its contents.');
116
                        } else {
117
                                $content  = _L('Currently, no network blocks are registered in the system.');
118
                        }
119
 
120
                        if (!$this->isLeafNode()) {
121
                                if (OIDplus::authUtils()->isAdminLoggedIn()) {
122
                                        $content .= '<h2>'._L('Manage root objects').'</h2>';
123
                                } else {
124
                                        $content .= '<h2>'._L('Available objects').'</h2>';
125
                                }
126
                                $content .= '%%CRUD%%';
127
                        }
128
                } else {
129
                        $title = $this->getTitle();
130
 
131
                        $content = '<h2>'._L('Technical information').'</h2>';
132
 
133
                        $content .= '<p>'._L('IPv4/CIDR').': <code>' . ipv4_normalize($this->bare) . '/' . $this->cidr . '</code><br>';
134
                        if ($this->cidr < 32) {
135
                                $content .= _L('First address').': <code>' . ipv4_cidr_min_ip($this->bare . '/' . $this->cidr) . '</code><br>';
136
                                $content .= _L('Last address').': <code>' . ipv4_cidr_max_ip($this->bare . '/' . $this->cidr) . '</code></p>';
137
                        } else {
138
                                $content .= _L('Single host address').'</p>';
139
                        }
140
 
141
                        $content .= '<h2>'._L('Description').'</h2>%%DESC%%';
142
 
143
                        if (!$this->isLeafNode()) {
144
                                if ($this->userHasWriteRights()) {
145
                                        $content .= '<h2>'._L('Create or change subsequent objects').'</h2>';
146
                                } else {
147
                                        $content .= '<h2>'._L('Subsequent objects').'</h2>';
148
                                }
149
                                $content .= '%%CRUD%%';
150
                        }
151
                }
152
        }
153
 
154
        public function one_up() {
155
                $cidr = $this->cidr - 1;
156
                if ($cidr < 0) return false; // cannot go further up
157
 
158
                $tmp = ipv4_normalize_range($this->bare . '/' . $cidr);
159
                return self::parse($this->ns() . ':' . $tmp);
160
        }
161
 
162
        public function distance($to) {
163
                if (!is_object($to)) $to = OIDplusObject::parse($to);
164
                if (!($to instanceof $this)) return false;
165
                return ipv4_distance($to->ipv4, $this->ipv4);
166
        }
167
 
168
        public function getDirectoryName() {
169
                if ($this->isRoot()) return $this->ns();
170
                $bare = str_replace('.','_',ipv4_normalize($this->bare));
171
                if ($this->isLeafNode()) {
172
                        return $this->ns().'_'.$bare;
173
                } else {
174
                        return $this->ns().'_'.$bare.'__'.$this->cidr;
175
                }
176
        }
707 daniel-mar 177
}