Subversion Repositories oidplus

Rev

Rev 1321 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
1248 daniel-mar 1
<?php
2
 
3
/*
4
 * OIDplus 2.0
5
 * Copyright 2019 - 2023 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
namespace ViaThinkSoft\OIDplus;
21
 
22
// phpcs:disable PSR1.Files.SideEffects
23
\defined('INSIDE_OIDPLUS') or die;
24
// phpcs:enable PSR1.Files.SideEffects
25
 
1259 daniel-mar 26
class OIDplusObjectTypePluginMac extends OIDplusObjectTypePlugin
27
        implements INTF_OID_1_3_6_1_4_1_37476_2_5_2_3_6 /* gridGeneratorLinks */
28
{
1248 daniel-mar 29
 
30
        /**
31
         * @return string
32
         */
33
        public static function getObjectTypeClassName(): string {
34
                return OIDplusMac::class;
35
        }
36
 
37
        /**
1259 daniel-mar 38
         * @param array $params
39
         * @return array
40
         * @throws OIDplusException
41
         */
1293 daniel-mar 42
        private function action_GenerateAAI(array $params): array {
43
                _CheckParamExists($params, 'aai_bits');
44
                _CheckParamExists($params, 'aai_multicast');
1259 daniel-mar 45
 
1293 daniel-mar 46
                if (($params['aai_bits'] != '48') && ($params['aai_bits'] != '64')) {
47
                        throw new OIDplusException(_L("Invalid bit amount"));
48
                }
1259 daniel-mar 49
 
1324 daniel-mar 50
                return array("status" => 0, "aai" => gen_aai((int)$params['aai_bits'], oidplus_is_true($params['aai_multicast'] ?? false)));
1293 daniel-mar 51
        }
52
 
53
        /**
54
         * @param string $actionID
55
         * @param array $params
56
         * @return array
57
         * @throws OIDplusException
58
         */
59
        public function action(string $actionID, array $params): array {
60
                if ($actionID == 'generate_aai') {
61
                        return $this->action_GenerateAAI($params);
1259 daniel-mar 62
                } else {
63
                        return parent::action($actionID, $params);
64
                }
65
        }
66
 
67
        /**
68
         * Implements interface INTF_OID_1_3_6_1_4_1_37476_2_5_2_3_6
69
         * @param OIDplusObject $objParent
70
         * @return string
71
         */
72
        public function gridGeneratorLinks(OIDplusObject $objParent): string {
1261 daniel-mar 73
                if (!$objParent->isRoot()) return '';
1259 daniel-mar 74
                return
1321 daniel-mar 75
                        '<br>'._L('Generate a random <abbr title="Administratively Assigned Identifier (not world-wide unique!)">AAI</abbr>').':'.
1261 daniel-mar 76
                        '<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>'.
77
                        '<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>'.
1259 daniel-mar 78
                        '<br><a href="https://standards.ieee.org/products-programs/regauth/" target="_blank">('._L('Buy an OUI or CID from IEEE').')</a>';
79
        }
80
 
81
        /**
1248 daniel-mar 82
         * @param string $static_node_id
83
         * @param bool $throw_exception
84
         * @return string
85
         */
86
        public static function prefilterQuery(string $static_node_id, bool $throw_exception): string {
1261 daniel-mar 87
                $static_node_id = trim($static_node_id);
88
 
1248 daniel-mar 89
                $static_node_id = preg_replace('@^eui:@', 'mac:', $static_node_id);
1254 daniel-mar 90
                $static_node_id = preg_replace('@^eli:@', 'mac:', $static_node_id);
1248 daniel-mar 91
 
1261 daniel-mar 92
                // Special treatment for MACs: if someone enters a valid MAC address in the goto box, prepend "mac:"
93
                if (((strpos($static_node_id, ':') !== false) ||
94
                     (strpos($static_node_id, '-') !== false) ||
95
                     (strpos($static_node_id, ' ') !== false)) && mac_valid($static_node_id)) {
96
                        $static_node_id = 'mac:'.$static_node_id;
1248 daniel-mar 97
                }
98
 
1261 daniel-mar 99
                if (str_starts_with($static_node_id,'mac:')) {
100
                        $static_node_id = 'mac:'.str_replace(array('-',':',' '), '', substr($static_node_id,4));
101
                        $static_node_id = 'mac:'.strtoupper(substr($static_node_id,4));
1248 daniel-mar 102
                }
103
 
104
                return $static_node_id;
105
        }
106
 
107
}