Subversion Repositories oidplus

Rev

Rev 1386 | 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
1086 daniel-mar 5
 * Copyright 2019 - 2023 Daniel Marschall, ViaThinkSoft
635 daniel-mar 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
 
1086 daniel-mar 22
// phpcs:disable PSR1.Files.SideEffects
23
\defined('INSIDE_OIDPLUS') or die;
24
// phpcs:enable PSR1.Files.SideEffects
25
 
635 daniel-mar 26
class OIDplusIpv6 extends OIDplusObject {
1130 daniel-mar 27
        /**
28
         * @var string
29
         */
635 daniel-mar 30
        private $ipv6;
1130 daniel-mar 31
 
32
        /**
33
         * @var string
34
         */
635 daniel-mar 35
        private $bare;
1130 daniel-mar 36
 
37
        /**
38
         * @var int
39
         */
635 daniel-mar 40
        private $cidr;
41
 
1116 daniel-mar 42
        /**
1130 daniel-mar 43
         * @param string $ipv6
1116 daniel-mar 44
         * @throws OIDplusException
45
         */
1130 daniel-mar 46
        public function __construct(string $ipv6) {
635 daniel-mar 47
                $this->ipv6 = $ipv6;
48
 
49
                if (!empty($ipv6)) {
50
                        if (strpos($ipv6, '/') === false) $ipv6 .= '/128';
51
                        list($bare, $cidr) = explode('/', $ipv6);
52
                        $this->bare = $bare;
1174 daniel-mar 53
                        if (!is_numeric($cidr)) throw new OIDplusException(_L('Invalid IPv6'));
1130 daniel-mar 54
                        $this->cidr = (int)$cidr;
635 daniel-mar 55
                        if (!ipv6_valid($bare)) throw new OIDplusException(_L('Invalid IPv6'));
56
                        if ($cidr < 0) throw new OIDplusException(_L('Invalid IPv6'));
57
                        if ($cidr > 128) throw new OIDplusException(_L('Invalid IPv6'));
58
                        $this->bare = ipv6_normalize($this->bare);
856 daniel-mar 59
                        $this->ipv6 = $this->bare . '/' . $this->cidr;
635 daniel-mar 60
                }
61
        }
62
 
1116 daniel-mar 63
        /**
64
         * @param string $node_id
65
         * @return OIDplusIpv6|null
1130 daniel-mar 66
         * @throws OIDplusException
1116 daniel-mar 67
         */
68
        public static function parse(string $node_id)/*: ?OIDplusIpv6*/ {
635 daniel-mar 69
                @list($namespace, $ipv6) = explode(':', $node_id, 2);
1116 daniel-mar 70
                if ($namespace !== self::ns()) return null;
635 daniel-mar 71
                return new self($ipv6);
72
        }
73
 
1116 daniel-mar 74
        /**
75
         * @return string
76
         */
77
        public static function objectTypeTitle(): string {
635 daniel-mar 78
                return _L('IPv6 Network Blocks');
79
        }
80
 
1116 daniel-mar 81
        /**
82
         * @return string
83
         */
84
        public static function objectTypeTitleShort(): string {
635 daniel-mar 85
                return _L('IPv6');
86
        }
87
 
1116 daniel-mar 88
        /**
89
         * @return string
90
         */
91
        public static function ns(): string {
635 daniel-mar 92
                return 'ipv6';
93
        }
94
 
1116 daniel-mar 95
        /**
96
         * @return string
97
         */
98
        public static function root(): string {
860 daniel-mar 99
                return self::ns().':';
635 daniel-mar 100
        }
101
 
1116 daniel-mar 102
        /**
103
         * @return bool
104
         */
105
        public function isRoot(): bool {
635 daniel-mar 106
                return $this->ipv6 == '';
107
        }
108
 
1116 daniel-mar 109
        /**
110
         * @param bool $with_ns
111
         * @return string
112
         */
113
        public function nodeId(bool $with_ns=true): string {
859 daniel-mar 114
                return $with_ns ? self::root().$this->ipv6 : $this->ipv6;
635 daniel-mar 115
        }
116
 
1116 daniel-mar 117
        /**
118
         * @param string $str
119
         * @return string
120
         * @throws OIDplusException
121
         */
122
        public function addString(string $str): string {
635 daniel-mar 123
                if (strpos($str, '/') === false) $str .= "/128";
124
 
125
                if (!$this->isRoot()) {
126
                        if (!ipv6_in_cidr($this->bare.'/'.$this->cidr, $str)) {
127
                                throw new OIDplusException(_L('Cannot add this address, because it must be inside the address range of the superior range.'));
128
                        }
129
                }
130
 
131
                list($ipv6, $cidr) = explode('/', $str);
132
                if ($cidr < 0) throw new OIDplusException(_L('Invalid IPv6 address %1',$str));
133
                if ($cidr > 128) throw new OIDplusException(_L('Invalid IPv6 address %1',$str));
134
                $ipv6_normalized = ipv6_normalize($ipv6);
135
                if (!$ipv6_normalized) throw new OIDplusException(_L('Invalid IPv6 address %1',$str));
859 daniel-mar 136
                return self::root().$ipv6_normalized.'/'.$cidr; // overwrite; no hierarchical tree
635 daniel-mar 137
        }
138
 
1116 daniel-mar 139
        /**
140
         * @param OIDplusObject $parent
141
         * @return string
142
         */
143
        public function crudShowId(OIDplusObject $parent): string {
635 daniel-mar 144
                return $this->ipv6;
145
        }
146
 
1116 daniel-mar 147
        /**
148
         * @param OIDplusObject|null $parent
149
         * @return string
150
         */
151
        public function jsTreeNodeName(OIDplusObject $parent = null): string {
635 daniel-mar 152
                if ($parent == null) return $this->objectTypeTitle();
153
                return $this->ipv6;
154
        }
155
 
1116 daniel-mar 156
        /**
157
         * @return string
158
         */
159
        public function defaultTitle(): string {
635 daniel-mar 160
                return $this->ipv6;
161
        }
162
 
1116 daniel-mar 163
        /**
164
         * @return bool
165
         */
166
        public function isLeafNode(): bool {
635 daniel-mar 167
                return $this->cidr >= 128;
168
        }
169
 
1116 daniel-mar 170
        /**
171
         * @return array
172
         */
173
        private function getTechInfo(): array {
753 daniel-mar 174
                if ($this->isRoot()) return array();
175
 
176
                $tech_info = array();
177
 
178
                $tech_info[_L('IPv6/CIDR')] = ipv6_normalize($this->bare) . '/' . $this->cidr;
179
                if ($this->cidr < 128) {
180
                        $tech_info[_L('First address')] = ipv6_cidr_min_ip($this->bare . '/' . $this->cidr);
181
                        $tech_info[_L('Last address')]  = ipv6_cidr_max_ip($this->bare . '/' . $this->cidr);
182
                }
183
 
184
                return $tech_info;
185
        }
186
 
1116 daniel-mar 187
        /**
188
         * @param string $title
189
         * @param string $content
190
         * @param string $icon
191
         * @return void
192
         * @throws OIDplusException
193
         */
194
        public function getContentPage(string &$title, string &$content, string &$icon) {
801 daniel-mar 195
                $icon = file_exists(__DIR__.'/img/main_icon.png') ? OIDplus::webpath(__DIR__,OIDplus::PATH_RELATIVE).'img/main_icon.png' : '';
635 daniel-mar 196
 
197
                if ($this->isRoot()) {
198
                        $title = OIDplusIpv6::objectTypeTitle();
199
 
200
                        $res = OIDplus::db()->query("select * from ###objects where parent = ?", array(self::root()));
790 daniel-mar 201
                        if ($res->any()) {
962 daniel-mar 202
                                $content  = '<p>'._L('Please select a network block in the tree view at the left to show its contents.').'</p>';
635 daniel-mar 203
                        } else {
962 daniel-mar 204
                                $content  = '<p>'._L('Currently, no network blocks are registered in the system.').'</p>';
635 daniel-mar 205
                        }
206
 
207
                        if (!$this->isLeafNode()) {
208
                                if (OIDplus::authUtils()->isAdminLoggedIn()) {
209
                                        $content .= '<h2>'._L('Manage root objects').'</h2>';
210
                                } else {
211
                                        $content .= '<h2>'._L('Available objects').'</h2>';
212
                                }
213
                                $content .= '%%CRUD%%';
214
                        }
215
                } else {
216
                        $title = $this->getTitle();
217
 
753 daniel-mar 218
                        $tech_info = $this->getTechInfo();
219
                        $tech_info_html = '';
220
                        if (count($tech_info) > 0) {
221
                                $tech_info_html .= '<h2>'._L('Technical information').'</h2>';
1386 daniel-mar 222
                                $tech_info_html .= '<div style="overflow:auto"><table border="0">';
753 daniel-mar 223
                                foreach ($tech_info as $key => $value) {
1386 daniel-mar 224
                                        $tech_info_html .= '<tr><td valign="top" style="white-space: nowrap;">'.$key.': </td><td><code>'.$value.'</code></td></tr>';
753 daniel-mar 225
                                }
1387 daniel-mar 226
                                $tech_info_html .= '</table></div>';
635 daniel-mar 227
                        }
753 daniel-mar 228
                        if ($this->cidr == 128) $tech_info_html .= _L('Single host address');
635 daniel-mar 229
 
753 daniel-mar 230
                        $content = $tech_info_html;
231
 
635 daniel-mar 232
                        $content .= '<h2>'._L('Description').'</h2>%%DESC%%';
233
 
234
                        if (!$this->isLeafNode()) {
235
                                if ($this->userHasWriteRights()) {
928 daniel-mar 236
                                        $content .= '<h2>'._L('Create or change subordinate objects').'</h2>';
635 daniel-mar 237
                                } else {
928 daniel-mar 238
                                        $content .= '<h2>'._L('Subordinate objects').'</h2>';
635 daniel-mar 239
                                }
240
                                $content .= '%%CRUD%%';
241
                        }
242
                }
243
        }
244
 
1116 daniel-mar 245
        /**
246
         * @return OIDplusIpv6|null
247
         */
248
        public function one_up()/*: ?OIDplusIpv6*/ {
635 daniel-mar 249
                $cidr = $this->cidr - 1;
1116 daniel-mar 250
                if ($cidr < 0) return null; // cannot go further up
635 daniel-mar 251
 
252
                $tmp = ipv6_normalize_range($this->bare . '/' . $cidr);
253
                return self::parse($this->ns() . ':' . $tmp);
254
        }
255
 
1116 daniel-mar 256
        /**
1130 daniel-mar 257
         * @param OIDplusObject|string $to
1116 daniel-mar 258
         * @return float|int|mixed|string|null
259
         */
635 daniel-mar 260
        public function distance($to) {
261
                if (!is_object($to)) $to = OIDplusObject::parse($to);
1121 daniel-mar 262
                if (!$to) return null;
1116 daniel-mar 263
                if (!($to instanceof $this)) return null;
264
                $res = ipv6_distance($to->ipv6, $this->ipv6);
265
                return $res !== false ? $res : null;
635 daniel-mar 266
        }
267
 
1116 daniel-mar 268
        /**
269
         * @return string
270
         */
271
        public function getDirectoryName(): string {
635 daniel-mar 272
                if ($this->isRoot()) return $this->ns();
273
                $bare = str_replace(':','_',ipv6_normalize($this->bare));
274
                if ($this->isLeafNode()) {
275
                        return $this->ns().'_'.$bare;
276
                } else {
277
                        return $this->ns().'_'.$bare.'__'.$this->cidr;
278
                }
279
        }
800 daniel-mar 280
 
1116 daniel-mar 281
        /**
282
         * @param string $mode
283
         * @return string
284
         */
285
        public static function treeIconFilename(string $mode): string {
800 daniel-mar 286
                return 'img/'.$mode.'_icon16.png';
287
        }
707 daniel-mar 288
}