Subversion Repositories oidplus

Rev

Rev 36 | 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
 
34 daniel-mar 102
        public function getContentPage(&$title, &$content, &$icon) {
55 daniel-mar 103
                $icon = file_exists(__DIR__.'/icon_big.png') ? 'plugins/objectTypes/'.basename(__DIR__).'/icon_big.png' : '';
104
 
2 daniel-mar 105
                if ($this->isRoot()) {
106
                        $title = OIDplusIpv4::objectTypeTitle();
107
 
108
                        $res = OIDplus::db()->query("select * from ".OIDPLUS_TABLENAME_PREFIX."objects where parent = '".OIDplus::db()->real_escape_string(self::root())."'");
109
                        if (OIDplus::db()->num_rows($res) > 0) {
110
                                $content  = 'Please select a network block in the tree view at the left to show its contents.';
111
                        } else {
112
                                $content  = 'Currently, no network blocks are registered in the system.';
113
                        }
114
 
16 daniel-mar 115
                        if (!$this->isLeafNode()) {
116
                                if (OIDplus::authUtils()::isAdminLoggedIn()) {
117
                                        $content .= '<h2>Manage root objects</h2>';
118
                                } else {
119
                                        $content .= '<h2>Available objects</h2>';
120
                                }
121
                                $content .= '%%CRUD%%';
2 daniel-mar 122
                        }
123
                } else {
17 daniel-mar 124
                        $content = '<h2>Technical information</h2>';
2 daniel-mar 125
 
17 daniel-mar 126
                        $content .= '<p>IPv4/CIDR: <code>' . ipv4_normalize($this->bare) . '/' . $this->cidr . '</code><br>';
127
                        if ($this->cidr < 32) {
128
                                $content .= 'First address: <code>' . ipv4_cidr_min_ip($this->bare . '/' . $this->cidr) . '</code><br>';
129
                                $content .= 'Last address: <code>' . ipv4_cidr_max_ip($this->bare . '/' . $this->cidr) . '</code></p>';
130
                        } else {
131
                                $content .= 'Single host address</p>';
132
                        }
133
 
134
                        $content .= '<h2>Description</h2>%%DESC%%';
135
 
16 daniel-mar 136
                        if (!$this->isLeafNode()) {
137
                                if ($this->userHasWriteRights()) {
138
                                        $content .= '<h2>Create or change subsequent objects</h2>';
139
                                } else {
140
                                        $content .= '<h2>Subsequent objects</h2>';
141
                                }
142
                                $content .= '%%CRUD%%';
2 daniel-mar 143
                        }
21 daniel-mar 144
 
145
                        $content .= '<br>%%WHOIS%%';
2 daniel-mar 146
                }
147
        }
20 daniel-mar 148
 
149
        public function one_up() {
150
                $cidr = $this->cidr - 1;
151
                if ($cidr < 0) return false; // cannot go further up
152
 
36 daniel-mar 153
                $tmp = ipv4_normalize_range($this->bare . '/' . $cidr);
154
                return self::parse($this->ns() . ':' . $tmp);
20 daniel-mar 155
        }
156
 
157
        public function distance($to) {
158
                if (!is_object($to)) $to = OIDplusObject::parse($to);
28 daniel-mar 159
                if (!($to instanceof $this)) return false;
20 daniel-mar 160
                return ipv4_distance($to->ipv4, $this->ipv4);
161
        }
2 daniel-mar 162
}
163
 
164
OIDplusObject::$registeredObjectTypes[] = 'OIDplusIpv4';
165