Subversion Repositories oidplus

Rev

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