Subversion Repositories oidplus

Rev

Rev 1130 | Rev 1324 | 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
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();
165
                preg_match_all('@([^:]+):\s*(.+)\n@ismU', $info, $m, PREG_SET_ORDER);
166
                foreach ($m as $m1) {
167
                        $key = $m1[1];
168
                        $value = $m1[2];
169
                        $tech_info[$key] = $value;
170
                }
171
                ob_end_clean();
172
 
173
                return $tech_info;
174
        }
175
 
1116 daniel-mar 176
        /**
177
         * @param string $title
178
         * @param string $content
179
         * @param string $icon
180
         * @return void
181
         * @throws OIDplusException
182
         */
183
        public function getContentPage(string &$title, string &$content, string &$icon) {
801 daniel-mar 184
                $icon = file_exists(__DIR__.'/img/main_icon.png') ? OIDplus::webpath(__DIR__,OIDplus::PATH_RELATIVE).'img/main_icon.png' : '';
635 daniel-mar 185
 
186
                if ($this->isRoot()) {
187
                        $title = OIDplusGuid::objectTypeTitle();
188
 
189
                        $res = OIDplus::db()->query("select * from ###objects where parent = ?", array(self::root()));
790 daniel-mar 190
                        if ($res->any()) {
962 daniel-mar 191
                                $content  = '<p>'._L('Please select a GUID in the tree view at the left to show its contents.').'</p>';
635 daniel-mar 192
                        } else {
962 daniel-mar 193
                                $content  = '<p>'._L('Currently, no GUID is registered in the system.').'</p>';
635 daniel-mar 194
                        }
195
 
196
                        if (!$this->isLeafNode()) {
197
                                if (OIDplus::authUtils()->isAdminLoggedIn()) {
198
                                        $content .= '<h2>'._L('Manage root objects / categories').'</h2>';
199
                                } else {
200
                                        $content .= '<h2>'._L('Available objects / categories').'</h2>';
201
                                }
202
                                $content .= '%%CRUD%%';
203
                        }
204
                } else {
205
                        $title = $this->getTitle();
206
 
207
                        if ($this->isLeafNode()) {
753 daniel-mar 208
                                $tech_info = $this->getTechInfo();
209
                                $tech_info_html = '';
210
                                if (count($tech_info) > 0) {
211
                                        $tech_info_html .= '<h2>'._L('Technical information').'</h2>';
212
                                        $tech_info_html .= '<table border="0">';
213
                                        foreach ($tech_info as $key => $value) {
214
                                                $tech_info_html .= '<tr><td>'.$key.': </td><td><code>'.$value.'</code></td></tr>';
215
                                        }
216
                                        $tech_info_html .= '</table>';
217
                                }
635 daniel-mar 218
 
753 daniel-mar 219
                                $content = $tech_info_html;
220
 
221
                                // $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 222
                        } else {
223
                                $content = '';
224
                        }
225
 
226
                        $content .= '<h2>'._L('Description').'</h2>%%DESC%%';
227
 
228
                        if (!$this->isLeafNode()) {
229
                                if ($this->userHasWriteRights()) {
928 daniel-mar 230
                                        $content .= '<h2>'._L('Create or change subordinate objects / categories').'</h2>';
635 daniel-mar 231
                                } else {
928 daniel-mar 232
                                        $content .= '<h2>'._L('Subordinate objects / categories').'</h2>';
635 daniel-mar 233
                                }
234
                                $content .= '%%CRUD%%';
235
                        }
236
                }
237
        }
238
 
1116 daniel-mar 239
        /**
240
         * @return OIDplusGuid|null
241
         */
242
        public function one_up()/*: ?OIDplusGuid*/ {
635 daniel-mar 243
                // A GUID is a GUID, there is no hierarchy
1271 daniel-mar 244
                return self::parse(self::ns().':');
635 daniel-mar 245
        }
246
 
1116 daniel-mar 247
        /**
1130 daniel-mar 248
         * @param OIDplusObject|string $to
249
         * @return int|null
1116 daniel-mar 250
         */
635 daniel-mar 251
        public function distance($to) {
252
                // Distance between GUIDs is not possible
253
                return null;
254
        }
255
 
1116 daniel-mar 256
        /**
257
         * @return array|OIDplusAltId[]
258
         * @throws OIDplusException
259
         */
260
        public function getAltIds(): array {
635 daniel-mar 261
                if ($this->isRoot()) return array();
262
                if (!$this->isLeafNode()) return array();
263
                $ids = parent::getAltIds();
264
                $ids[] = new OIDplusAltId('oid', uuid_to_oid($this->guid), _L('OID representation of UUID'));
265
                return $ids;
266
        }
267
 
1116 daniel-mar 268
        /**
269
         * @return string
270
         * @throws OIDplusException
271
         */
272
        public function getDirectoryName(): string {
635 daniel-mar 273
                if ($this->isLeafNode()) {
274
                        // Leaf (UUID)
275
                        // Example output: "guid_adb0b042_5b57_11eb_b0d9_3c4a92df8582"
276
                        $str = $this->nodeId(false);
277
                        $str = str_replace('-', '_', $str);
278
                        $str = strtolower($str);
279
                        return $this->ns().'_'.$str;
280
                } else {
281
                        // Category
282
                        return parent::getDirectoryName();
283
                }
284
        }
800 daniel-mar 285
 
1116 daniel-mar 286
        /**
287
         * @param string $mode
288
         * @return string
289
         */
290
        public static function treeIconFilename(string $mode): string {
800 daniel-mar 291
                return 'img/'.$mode.'_icon16.png';
292
        }
707 daniel-mar 293
}