Subversion Repositories oidplus

Rev

Rev 1293 | Rev 1321 | Go to most recent revision | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 1293 Rev 1294
1
<?php
1
<?php
2
 
2
 
3
/*
3
/*
4
 * OIDplus 2.0
4
 * OIDplus 2.0
5
 * Copyright 2019 - 2023 Daniel Marschall, ViaThinkSoft
5
 * Copyright 2019 - 2023 Daniel Marschall, ViaThinkSoft
6
 *
6
 *
7
 * Licensed under the Apache License, Version 2.0 (the "License");
7
 * Licensed under the Apache License, Version 2.0 (the "License");
8
 * you may not use this file except in compliance with 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
9
 * You may obtain a copy of the License at
10
 *
10
 *
11
 *     http://www.apache.org/licenses/LICENSE-2.0
11
 *     http://www.apache.org/licenses/LICENSE-2.0
12
 *
12
 *
13
 * Unless required by applicable law or agreed to in writing, software
13
 * Unless required by applicable law or agreed to in writing, software
14
 * distributed under the License is distributed on an "AS IS" BASIS,
14
 * distributed under the License is distributed on an "AS IS" BASIS,
15
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16
 * See the License for the specific language governing permissions and
16
 * See the License for the specific language governing permissions and
17
 * limitations under the License.
17
 * limitations under the License.
18
 */
18
 */
19
 
19
 
20
namespace ViaThinkSoft\OIDplus;
20
namespace ViaThinkSoft\OIDplus;
21
 
21
 
22
// phpcs:disable PSR1.Files.SideEffects
22
// phpcs:disable PSR1.Files.SideEffects
23
\defined('INSIDE_OIDPLUS') or die;
23
\defined('INSIDE_OIDPLUS') or die;
24
// phpcs:enable PSR1.Files.SideEffects
24
// phpcs:enable PSR1.Files.SideEffects
25
 
25
 
26
class OIDplusObjectTypePluginMac extends OIDplusObjectTypePlugin
26
class OIDplusObjectTypePluginMac extends OIDplusObjectTypePlugin
27
        implements INTF_OID_1_3_6_1_4_1_37476_2_5_2_3_6 /* gridGeneratorLinks */
27
        implements INTF_OID_1_3_6_1_4_1_37476_2_5_2_3_6 /* gridGeneratorLinks */
28
{
28
{
29
 
29
 
30
        /**
30
        /**
31
         * @return string
31
         * @return string
32
         */
32
         */
33
        public static function getObjectTypeClassName(): string {
33
        public static function getObjectTypeClassName(): string {
34
                return OIDplusMac::class;
34
                return OIDplusMac::class;
35
        }
35
        }
36
 
36
 
37
        /**
37
        /**
38
         * @param array $params
38
         * @param array $params
39
         * @return array
39
         * @return array
40
         * @throws OIDplusException
40
         * @throws OIDplusException
41
         */
41
         */
42
        private function action_GenerateAAI(array $params): array {
42
        private function action_GenerateAAI(array $params): array {
43
                _CheckParamExists($params, 'aai_bits');
43
                _CheckParamExists($params, 'aai_bits');
44
                _CheckParamExists($params, 'aai_multicast');
44
                _CheckParamExists($params, 'aai_multicast');
45
 
45
 
46
                if (($params['aai_bits'] != '48') && ($params['aai_bits'] != '64')) {
46
                if (($params['aai_bits'] != '48') && ($params['aai_bits'] != '64')) {
47
                        throw new OIDplusException(_L("Invalid bit amount"));
47
                        throw new OIDplusException(_L("Invalid bit amount"));
48
                }
48
                }
49
 
49
 
50
                $aai = '';
50
                $aai = '';
51
                for ($i=0; $i<$params['aai_bits']/4; $i++) {
51
                for ($i=0; $i<$params['aai_bits']/4; $i++) {
52
                        try {
52
                        try {
53
                                $aai .= dechex(random_int(0, 15));
53
                                $aai .= dechex(random_int(0, 15));
54
                        } catch (\Exception $e) {
54
                        } catch (\Exception $e) {
55
                                $aai .= dechex(mt_rand(0, 15));
55
                                $aai .= dechex(mt_rand(0, 15));
56
                        }
56
                        }
57
                }
57
                }
58
 
58
 
59
                if ($params['aai_multicast'] == 'true') {
59
                if (oidplus_is_true($params['aai_multicast'] ?? false)) {
60
                        $aai[1] = '3';
60
                        $aai[1] = '3';
61
                } else {
61
                } else {
62
                        $aai[1] = '2';
62
                        $aai[1] = '2';
63
                }
63
                }
64
 
64
 
65
                $aai = strtoupper($aai);
65
                $aai = strtoupper($aai);
66
                $aai = rtrim(chunk_split($aai, 2, '-'), '-');
66
                $aai = rtrim(chunk_split($aai, 2, '-'), '-');
67
 
67
 
68
                return array("status" => 0, "aai" => $aai);
68
                return array("status" => 0, "aai" => $aai);
69
        }
69
        }
70
 
70
 
71
        /**
71
        /**
72
         * @param string $actionID
72
         * @param string $actionID
73
         * @param array $params
73
         * @param array $params
74
         * @return array
74
         * @return array
75
         * @throws OIDplusException
75
         * @throws OIDplusException
76
         */
76
         */
77
        public function action(string $actionID, array $params): array {
77
        public function action(string $actionID, array $params): array {
78
                if ($actionID == 'generate_aai') {
78
                if ($actionID == 'generate_aai') {
79
                        return $this->action_GenerateAAI($params);
79
                        return $this->action_GenerateAAI($params);
80
                } else {
80
                } else {
81
                        return parent::action($actionID, $params);
81
                        return parent::action($actionID, $params);
82
                }
82
                }
83
        }
83
        }
84
 
84
 
85
        /**
85
        /**
86
         * Implements interface INTF_OID_1_3_6_1_4_1_37476_2_5_2_3_6
86
         * Implements interface INTF_OID_1_3_6_1_4_1_37476_2_5_2_3_6
87
         * @param OIDplusObject $objParent
87
         * @param OIDplusObject $objParent
88
         * @return string
88
         * @return string
89
         */
89
         */
90
        public function gridGeneratorLinks(OIDplusObject $objParent): string {
90
        public function gridGeneratorLinks(OIDplusObject $objParent): string {
91
                if (!$objParent->isRoot()) return '';
91
                if (!$objParent->isRoot()) return '';
92
                return
92
                return
93
                        '<br>'._L('Generate a random AAI:').
93
                        '<br>'._L('Generate a random AAI:').
94
                        '<br>- <abbr title="'._L('Random hexadecimal string, but second nibble must be %1','2').'">Unicast</abbr> <a href="javascript:OIDplusObjectTypePluginMac.generateRandomAAI(48, false)">(AAI-48)</a> | <a href="javascript:OIDplusObjectTypePluginMac.generateRandomAAI(64, false)">(AAI-64)</a>'.
94
                        '<br>- <abbr title="'._L('Random hexadecimal string, but second nibble must be %1','2').'">Unicast</abbr> <a href="javascript:OIDplusObjectTypePluginMac.generateRandomAAI(48, false)">(AAI-48)</a> | <a href="javascript:OIDplusObjectTypePluginMac.generateRandomAAI(64, false)">(AAI-64)</a>'.
95
                        '<br>- <abbr title="'._L('Random hexadecimal string, but second nibble must be %1','3').'">Multicast</abbr> <a href="javascript:OIDplusObjectTypePluginMac.generateRandomAAI(48, true)">(AAI-48)</a> | <a href="javascript:OIDplusObjectTypePluginMac.generateRandomAAI(64, true)">(AAI-64)</a>'.
95
                        '<br>- <abbr title="'._L('Random hexadecimal string, but second nibble must be %1','3').'">Multicast</abbr> <a href="javascript:OIDplusObjectTypePluginMac.generateRandomAAI(48, true)">(AAI-48)</a> | <a href="javascript:OIDplusObjectTypePluginMac.generateRandomAAI(64, true)">(AAI-64)</a>'.
96
                        '<br><a href="https://standards.ieee.org/products-programs/regauth/" target="_blank">('._L('Buy an OUI or CID from IEEE').')</a>';
96
                        '<br><a href="https://standards.ieee.org/products-programs/regauth/" target="_blank">('._L('Buy an OUI or CID from IEEE').')</a>';
97
        }
97
        }
98
 
98
 
99
        /**
99
        /**
100
         * @param string $static_node_id
100
         * @param string $static_node_id
101
         * @param bool $throw_exception
101
         * @param bool $throw_exception
102
         * @return string
102
         * @return string
103
         */
103
         */
104
        public static function prefilterQuery(string $static_node_id, bool $throw_exception): string {
104
        public static function prefilterQuery(string $static_node_id, bool $throw_exception): string {
105
                $static_node_id = trim($static_node_id);
105
                $static_node_id = trim($static_node_id);
106
 
106
 
107
                $static_node_id = preg_replace('@^eui:@', 'mac:', $static_node_id);
107
                $static_node_id = preg_replace('@^eui:@', 'mac:', $static_node_id);
108
                $static_node_id = preg_replace('@^eli:@', 'mac:', $static_node_id);
108
                $static_node_id = preg_replace('@^eli:@', 'mac:', $static_node_id);
109
 
109
 
110
                // Special treatment for MACs: if someone enters a valid MAC address in the goto box, prepend "mac:"
110
                // Special treatment for MACs: if someone enters a valid MAC address in the goto box, prepend "mac:"
111
                if (((strpos($static_node_id, ':') !== false) ||
111
                if (((strpos($static_node_id, ':') !== false) ||
112
                     (strpos($static_node_id, '-') !== false) ||
112
                     (strpos($static_node_id, '-') !== false) ||
113
                     (strpos($static_node_id, ' ') !== false)) && mac_valid($static_node_id)) {
113
                     (strpos($static_node_id, ' ') !== false)) && mac_valid($static_node_id)) {
114
                        $static_node_id = 'mac:'.$static_node_id;
114
                        $static_node_id = 'mac:'.$static_node_id;
115
                }
115
                }
116
 
116
 
117
                if (str_starts_with($static_node_id,'mac:')) {
117
                if (str_starts_with($static_node_id,'mac:')) {
118
                        $static_node_id = 'mac:'.str_replace(array('-',':',' '), '', substr($static_node_id,4));
118
                        $static_node_id = 'mac:'.str_replace(array('-',':',' '), '', substr($static_node_id,4));
119
                        $static_node_id = 'mac:'.strtoupper(substr($static_node_id,4));
119
                        $static_node_id = 'mac:'.strtoupper(substr($static_node_id,4));
120
                }
120
                }
121
 
121
 
122
                return $static_node_id;
122
                return $static_node_id;
123
        }
123
        }
124
 
124
 
125
}
125
}
126
 
126