Subversion Repositories oidplus

Rev

Rev 1387 | 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 OIDplusGuid extends OIDplusObject {
1130 daniel-mar 27
        /**
28
         * @var string
29
         */
635 daniel-mar 30
        private $guid;
31
 
1116 daniel-mar 32
        /**
1130 daniel-mar 33
         * @param string $guid
1116 daniel-mar 34
         */
1130 daniel-mar 35
        public function __construct(string $guid) {
635 daniel-mar 36
                if (uuid_valid($guid)) {
856 daniel-mar 37
                        $this->guid = strtolower(uuid_canonize($guid)); // It is a real GUID (leaf node)
635 daniel-mar 38
                } else {
39
                        $this->guid = $guid; // It is a category name
40
                }
41
        }
42
 
1116 daniel-mar 43
        /**
44
         * @param string $node_id
45
         * @return OIDplusGuid|null
46
         */
47
        public static function parse(string $node_id)/*: ?OIDplusGuid*/ {
635 daniel-mar 48
                @list($namespace, $guid) = explode(':', $node_id, 2);
1116 daniel-mar 49
                if ($namespace !== self::ns()) return null;
635 daniel-mar 50
                return new self($guid);
51
        }
52
 
1116 daniel-mar 53
        /**
54
         * @return string
55
         */
56
        public static function objectTypeTitle(): string {
635 daniel-mar 57
                return _L('Globally Unique Identifier (GUID)');
58
        }
59
 
1116 daniel-mar 60
        /**
61
         * @return string
62
         */
63
        public static function objectTypeTitleShort(): string {
635 daniel-mar 64
                return _L('GUID');
65
        }
66
 
1116 daniel-mar 67
        /**
68
         * @return string
69
         */
70
        public static function ns(): string {
635 daniel-mar 71
                return 'guid';
72
        }
73
 
1116 daniel-mar 74
        /**
75
         * @return string
76
         */
77
        public static function root(): string {
860 daniel-mar 78
                return self::ns().':';
635 daniel-mar 79
        }
80
 
1116 daniel-mar 81
        /**
82
         * @return bool
83
         */
84
        public function isRoot(): bool {
635 daniel-mar 85
                return $this->guid == '';
86
        }
87
 
1116 daniel-mar 88
        /**
89
         * @param bool $with_ns
90
         * @return string
91
         */
92
        public function nodeId(bool $with_ns=true): string {
859 daniel-mar 93
                return $with_ns ? self::root().$this->guid : $this->guid;
635 daniel-mar 94
        }
95
 
1116 daniel-mar 96
        /**
97
         * @param string $str
98
         * @return string
99
         */
100
        public function addString(string $str): string {
635 daniel-mar 101
                if (uuid_valid($str)) {
102
                        // real GUID
858 daniel-mar 103
                        return self::root() . strtolower(uuid_canonize($str));
635 daniel-mar 104
                } else {
105
                        // just a category
858 daniel-mar 106
                        if ($this->isRoot()) {
107
                                return self::root() . $str;
108
                        } else {
109
                                return $this->nodeId() . '/' . $str;
110
                        }
635 daniel-mar 111
                }
112
        }
113
 
1116 daniel-mar 114
        /**
115
         * @param OIDplusObject $parent
116
         * @return string
117
         */
118
        public function crudShowId(OIDplusObject $parent): string {
850 daniel-mar 119
                if ($this->isLeafNode()) {
120
                        // We don't parse '/' in a valid FourCC code (i.e. Leaf node)
121
                        return $this->nodeId(false);
122
                } else {
123
                        if ($parent->isRoot()) {
124
                                return substr($this->nodeId(), strlen($parent->nodeId()));
125
                        } else {
126
                                return substr($this->nodeId(), strlen($parent->nodeId())+1);
127
                        }
128
                }
635 daniel-mar 129
        }
130
 
1116 daniel-mar 131
        /**
132
         * @param OIDplusObject|null $parent
133
         * @return string
134
         */
135
        public function jsTreeNodeName(OIDplusObject $parent = null): string {
635 daniel-mar 136
                if ($parent == null) return $this->objectTypeTitle();
137
                return $this->crudShowId($parent);
138
        }
139
 
1116 daniel-mar 140
        /**
141
         * @return string
142
         */
143
        public function defaultTitle(): string {
635 daniel-mar 144
                return $this->guid;
145
        }
146
 
1116 daniel-mar 147
        /**
148
         * @return bool
149
         */
150
        public function isLeafNode(): bool {
635 daniel-mar 151
                return uuid_valid($this->guid);
152
        }
153
 
1116 daniel-mar 154
        /**
155
         * @return array
156
         */
157
        private function getTechInfo(): array {
753 daniel-mar 158
                $tech_info = array();
856 daniel-mar 159
                $tech_info[_L('UUID')] = strtolower(uuid_canonize($this->guid));
753 daniel-mar 160
                $tech_info[_L('C++ notation')] = uuid_c_syntax($this->guid);
161
 
162
                ob_start();
163
                uuid_info($this->guid);
164
                $info = ob_get_contents();
1324 daniel-mar 165
                ob_end_clean();
166
 
1325 daniel-mar 167
                $p = strpos($info, "<u>Interpretation");
168
                if ($p !== false) $info = substr($info, $p);
169
 
1324 daniel-mar 170
                $lines = explode("\n", $info);
171
                $key = '';
172
                foreach ($lines as $line) {
1325 daniel-mar 173
                        $m1 = explode(':', $line, 2);
1324 daniel-mar 174
                        if (!isset($m1[1])) $m1 = array($key, $m1[0]);
175
                        $key = $m1[0];
176
                        if (str_starts_with($key, '<u>')) continue;
177
                        if (isset($tech_info[$key])) {
178
                                $value = $tech_info[$key].'<br>'.$m1[1];
179
                        } else {
180
                                $value = $m1[1];
181
                        }
753 daniel-mar 182
                        $tech_info[$key] = $value;
183
                }
184
 
185
                return $tech_info;
186
        }
187
 
1116 daniel-mar 188
        /**
189
         * @param string $title
190
         * @param string $content
191
         * @param string $icon
192
         * @return void
193
         * @throws OIDplusException
194
         */
195
        public function getContentPage(string &$title, string &$content, string &$icon) {
801 daniel-mar 196
                $icon = file_exists(__DIR__.'/img/main_icon.png') ? OIDplus::webpath(__DIR__,OIDplus::PATH_RELATIVE).'img/main_icon.png' : '';
635 daniel-mar 197
 
198
                if ($this->isRoot()) {
199
                        $title = OIDplusGuid::objectTypeTitle();
200
 
201
                        $res = OIDplus::db()->query("select * from ###objects where parent = ?", array(self::root()));
790 daniel-mar 202
                        if ($res->any()) {
962 daniel-mar 203
                                $content  = '<p>'._L('Please select a GUID in the tree view at the left to show its contents.').'</p>';
635 daniel-mar 204
                        } else {
962 daniel-mar 205
                                $content  = '<p>'._L('Currently, no GUID is registered in the system.').'</p>';
635 daniel-mar 206
                        }
207
 
208
                        if (!$this->isLeafNode()) {
209
                                if (OIDplus::authUtils()->isAdminLoggedIn()) {
210
                                        $content .= '<h2>'._L('Manage root objects / categories').'</h2>';
211
                                } else {
212
                                        $content .= '<h2>'._L('Available objects / categories').'</h2>';
213
                                }
214
                                $content .= '%%CRUD%%';
215
                        }
216
                } else {
217
                        $title = $this->getTitle();
218
 
219
                        if ($this->isLeafNode()) {
753 daniel-mar 220
                                $tech_info = $this->getTechInfo();
221
                                $tech_info_html = '';
222
                                if (count($tech_info) > 0) {
223
                                        $tech_info_html .= '<h2>'._L('Technical information').'</h2>';
1386 daniel-mar 224
                                        $tech_info_html .= '<div style="overflow:auto"><table border="0">';
753 daniel-mar 225
                                        foreach ($tech_info as $key => $value) {
1386 daniel-mar 226
                                                $tech_info_html .= '<tr><td valign="top" style="white-space: nowrap;">'.$key.': </td><td><code>'.$value.'</code></td></tr>';
753 daniel-mar 227
                                        }
1387 daniel-mar 228
                                        $tech_info_html .= '</table></div>';
753 daniel-mar 229
                                }
635 daniel-mar 230
 
753 daniel-mar 231
                                $content = $tech_info_html;
232
 
233
                                // $content .= "<p><a href=\"https://misc.daniel-marschall.de/tools/uuid_mac_decoder/interprete_uuid.php?uuid=".urlencode($this->guid)."\">More technical information</a></p>";
635 daniel-mar 234
                        } else {
235
                                $content = '';
236
                        }
237
 
238
                        $content .= '<h2>'._L('Description').'</h2>%%DESC%%';
239
 
240
                        if (!$this->isLeafNode()) {
241
                                if ($this->userHasWriteRights()) {
928 daniel-mar 242
                                        $content .= '<h2>'._L('Create or change subordinate objects / categories').'</h2>';
635 daniel-mar 243
                                } else {
928 daniel-mar 244
                                        $content .= '<h2>'._L('Subordinate objects / categories').'</h2>';
635 daniel-mar 245
                                }
246
                                $content .= '%%CRUD%%';
247
                        }
248
                }
249
        }
250
 
1116 daniel-mar 251
        /**
252
         * @return OIDplusGuid|null
253
         */
254
        public function one_up()/*: ?OIDplusGuid*/ {
635 daniel-mar 255
                // A GUID is a GUID, there is no hierarchy
1271 daniel-mar 256
                return self::parse(self::ns().':');
635 daniel-mar 257
        }
258
 
1116 daniel-mar 259
        /**
1130 daniel-mar 260
         * @param OIDplusObject|string $to
261
         * @return int|null
1116 daniel-mar 262
         */
635 daniel-mar 263
        public function distance($to) {
264
                // Distance between GUIDs is not possible
265
                return null;
266
        }
267
 
1116 daniel-mar 268
        /**
269
         * @return array|OIDplusAltId[]
270
         * @throws OIDplusException
271
         */
272
        public function getAltIds(): array {
635 daniel-mar 273
                if ($this->isRoot()) return array();
274
                if (!$this->isLeafNode()) return array();
275
                $ids = parent::getAltIds();
1424 daniel-mar 276
 
277
                // TODO: this should be included to uuid_utils.inc.php
278
                $uuid_to_oid_bases = array(
279
                        '2.25' => 'ISO/ITU-T 128 bits',
280
                        '1.2.840.113556.1.8000.2554' => 'Microsoft',
281
                        '1.3.6.1.4.1.54392.1' => 'Waterjuice 2x64 bits',
282
                        '1.3.6.1.4.1.54392.2' => 'Waterjuice 4x32 bits',
283
                        '1.3.6.1.4.1.54392.3' => 'Waterjuice 8x16 bits'
284
                );
285
                foreach ($uuid_to_oid_bases as $base_oid => $base_desc) {
286
                        $ids[] = new OIDplusAltId('oid', uuid_to_oid($this->guid, $base_oid), _L('OID representation of UUID').' ('.$base_desc.')');
287
                }
288
 
635 daniel-mar 289
                return $ids;
290
        }
291
 
1116 daniel-mar 292
        /**
293
         * @return string
294
         * @throws OIDplusException
295
         */
296
        public function getDirectoryName(): string {
635 daniel-mar 297
                if ($this->isLeafNode()) {
298
                        // Leaf (UUID)
299
                        // Example output: "guid_adb0b042_5b57_11eb_b0d9_3c4a92df8582"
300
                        $str = $this->nodeId(false);
301
                        $str = str_replace('-', '_', $str);
302
                        $str = strtolower($str);
303
                        return $this->ns().'_'.$str;
304
                } else {
305
                        // Category
306
                        return parent::getDirectoryName();
307
                }
308
        }
800 daniel-mar 309
 
1116 daniel-mar 310
        /**
311
         * @param string $mode
312
         * @return string
313
         */
314
        public static function treeIconFilename(string $mode): string {
800 daniel-mar 315
                return 'img/'.$mode.'_icon16.png';
316
        }
707 daniel-mar 317
}