Subversion Repositories oidplus

Rev

Rev 962 | Rev 1086 | 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
 
1050 daniel-mar 20
namespace ViaThinkSoft\OIDplus;
635 daniel-mar 21
 
22
class OIDplusIpv6 extends OIDplusObject {
23
        private $ipv6;
24
        private $bare;
25
        private $cidr;
26
 
27
        public function __construct($ipv6) {
28
                $this->ipv6 = $ipv6;
29
 
30
                if (!empty($ipv6)) {
31
                        if (strpos($ipv6, '/') === false) $ipv6 .= '/128';
32
                        list($bare, $cidr) = explode('/', $ipv6);
33
                        $this->bare = $bare;
34
                        $this->cidr = $cidr;
35
                        if (!ipv6_valid($bare)) throw new OIDplusException(_L('Invalid IPv6'));
36
                        if (!is_numeric($cidr)) throw new OIDplusException(_L('Invalid IPv6'));
37
                        if ($cidr < 0) throw new OIDplusException(_L('Invalid IPv6'));
38
                        if ($cidr > 128) throw new OIDplusException(_L('Invalid IPv6'));
39
                        $this->bare = ipv6_normalize($this->bare);
856 daniel-mar 40
                        $this->ipv6 = $this->bare . '/' . $this->cidr;
635 daniel-mar 41
                }
42
        }
43
 
44
        public static function parse($node_id) {
45
                @list($namespace, $ipv6) = explode(':', $node_id, 2);
860 daniel-mar 46
                if ($namespace !== self::ns()) return false;
635 daniel-mar 47
                return new self($ipv6);
48
        }
49
 
50
        public static function objectTypeTitle() {
51
                return _L('IPv6 Network Blocks');
52
        }
53
 
54
        public static function objectTypeTitleShort() {
55
                return _L('IPv6');
56
        }
57
 
58
        public static function ns() {
59
                return 'ipv6';
60
        }
61
 
62
        public static function root() {
860 daniel-mar 63
                return self::ns().':';
635 daniel-mar 64
        }
65
 
66
        public function isRoot() {
67
                return $this->ipv6 == '';
68
        }
69
 
70
        public function nodeId($with_ns=true) {
859 daniel-mar 71
                return $with_ns ? self::root().$this->ipv6 : $this->ipv6;
635 daniel-mar 72
        }
73
 
74
        public function addString($str) {
75
                if (strpos($str, '/') === false) $str .= "/128";
76
 
77
                if (!$this->isRoot()) {
78
                        if (!ipv6_in_cidr($this->bare.'/'.$this->cidr, $str)) {
79
                                throw new OIDplusException(_L('Cannot add this address, because it must be inside the address range of the superior range.'));
80
                        }
81
                }
82
 
83
                list($ipv6, $cidr) = explode('/', $str);
84
                if ($cidr < 0) throw new OIDplusException(_L('Invalid IPv6 address %1',$str));
85
                if ($cidr > 128) throw new OIDplusException(_L('Invalid IPv6 address %1',$str));
86
                $ipv6_normalized = ipv6_normalize($ipv6);
87
                if (!$ipv6_normalized) throw new OIDplusException(_L('Invalid IPv6 address %1',$str));
859 daniel-mar 88
                return self::root().$ipv6_normalized.'/'.$cidr; // overwrite; no hierarchical tree
635 daniel-mar 89
        }
90
 
91
        public function crudShowId(OIDplusObject $parent) {
92
                return $this->ipv6;
93
        }
94
 
95
        public function jsTreeNodeName(OIDplusObject $parent = null) {
96
                if ($parent == null) return $this->objectTypeTitle();
97
                return $this->ipv6;
98
        }
99
 
100
        public function defaultTitle() {
101
                return $this->ipv6;
102
        }
103
 
104
        public function isLeafNode() {
105
                return $this->cidr >= 128;
106
        }
107
 
753 daniel-mar 108
        private function getTechInfo() {
109
                if ($this->isRoot()) return array();
110
 
111
                $tech_info = array();
112
 
113
                $tech_info[_L('IPv6/CIDR')] = ipv6_normalize($this->bare) . '/' . $this->cidr;
114
                if ($this->cidr < 128) {
115
                        $tech_info[_L('First address')] = ipv6_cidr_min_ip($this->bare . '/' . $this->cidr);
116
                        $tech_info[_L('Last address')]  = ipv6_cidr_max_ip($this->bare . '/' . $this->cidr);
117
                }
118
 
119
                return $tech_info;
120
        }
121
 
635 daniel-mar 122
        public function getContentPage(&$title, &$content, &$icon) {
801 daniel-mar 123
                $icon = file_exists(__DIR__.'/img/main_icon.png') ? OIDplus::webpath(__DIR__,OIDplus::PATH_RELATIVE).'img/main_icon.png' : '';
635 daniel-mar 124
 
125
                if ($this->isRoot()) {
126
                        $title = OIDplusIpv6::objectTypeTitle();
127
 
128
                        $res = OIDplus::db()->query("select * from ###objects where parent = ?", array(self::root()));
790 daniel-mar 129
                        if ($res->any()) {
962 daniel-mar 130
                                $content  = '<p>'._L('Please select a network block in the tree view at the left to show its contents.').'</p>';
635 daniel-mar 131
                        } else {
962 daniel-mar 132
                                $content  = '<p>'._L('Currently, no network blocks are registered in the system.').'</p>';
635 daniel-mar 133
                        }
134
 
135
                        if (!$this->isLeafNode()) {
136
                                if (OIDplus::authUtils()->isAdminLoggedIn()) {
137
                                        $content .= '<h2>'._L('Manage root objects').'</h2>';
138
                                } else {
139
                                        $content .= '<h2>'._L('Available objects').'</h2>';
140
                                }
141
                                $content .= '%%CRUD%%';
142
                        }
143
                } else {
144
                        $title = $this->getTitle();
145
 
753 daniel-mar 146
                        $tech_info = $this->getTechInfo();
147
                        $tech_info_html = '';
148
                        if (count($tech_info) > 0) {
149
                                $tech_info_html .= '<h2>'._L('Technical information').'</h2>';
150
                                $tech_info_html .= '<table border="0">';
151
                                foreach ($tech_info as $key => $value) {
152
                                        $tech_info_html .= '<tr><td>'.$key.': </td><td><code>'.$value.'</code></td></tr>';
153
                                }
154
                                $tech_info_html .= '</table>';
635 daniel-mar 155
                        }
753 daniel-mar 156
                        if ($this->cidr == 128) $tech_info_html .= _L('Single host address');
635 daniel-mar 157
 
753 daniel-mar 158
                        $content = $tech_info_html;
159
 
635 daniel-mar 160
                        $content .= '<h2>'._L('Description').'</h2>%%DESC%%';
161
 
162
                        if (!$this->isLeafNode()) {
163
                                if ($this->userHasWriteRights()) {
928 daniel-mar 164
                                        $content .= '<h2>'._L('Create or change subordinate objects').'</h2>';
635 daniel-mar 165
                                } else {
928 daniel-mar 166
                                        $content .= '<h2>'._L('Subordinate objects').'</h2>';
635 daniel-mar 167
                                }
168
                                $content .= '%%CRUD%%';
169
                        }
170
                }
171
        }
172
 
173
        public function one_up() {
174
                $cidr = $this->cidr - 1;
175
                if ($cidr < 0) return false; // cannot go further up
176
 
177
                $tmp = ipv6_normalize_range($this->bare . '/' . $cidr);
178
                return self::parse($this->ns() . ':' . $tmp);
179
        }
180
 
181
        public function distance($to) {
182
                if (!is_object($to)) $to = OIDplusObject::parse($to);
183
                if (!($to instanceof $this)) return false;
184
                return ipv6_distance($to->ipv6, $this->ipv6);
185
        }
186
 
187
        public function getDirectoryName() {
188
                if ($this->isRoot()) return $this->ns();
189
                $bare = str_replace(':','_',ipv6_normalize($this->bare));
190
                if ($this->isLeafNode()) {
191
                        return $this->ns().'_'.$bare;
192
                } else {
193
                        return $this->ns().'_'.$bare.'__'.$this->cidr;
194
                }
195
        }
800 daniel-mar 196
 
805 daniel-mar 197
        public static function treeIconFilename($mode) {
800 daniel-mar 198
                return 'img/'.$mode.'_icon16.png';
199
        }
707 daniel-mar 200
}