Subversion Repositories oidplus

Rev

Rev 790 | Rev 801 | 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
5
 * Copyright 2019 - 2021 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
if (!defined('INSIDE_OIDPLUS')) die();
21
 
22
class OIDplusGuid extends OIDplusObject {
23
        private $guid;
24
 
25
        public function __construct($guid) {
26
                if (uuid_valid($guid)) {
27
                        $this->guid = uuid_canonize($guid); // It is a real GUID (leaf node)
28
                } else {
29
                        $this->guid = $guid; // It is a category name
30
                }
31
        }
32
 
33
        public static function parse($node_id) {
34
                @list($namespace, $guid) = explode(':', $node_id, 2);
35
                if ($namespace !== 'guid') return false;
36
                return new self($guid);
37
        }
38
 
39
        public static function objectTypeTitle() {
40
                return _L('Globally Unique Identifier (GUID)');
41
        }
42
 
43
        public static function objectTypeTitleShort() {
44
                return _L('GUID');
45
        }
46
 
47
        public static function ns() {
48
                return 'guid';
49
        }
50
 
51
        public static function root() {
52
                return 'guid:';
53
        }
54
 
55
        public function isRoot() {
56
                return $this->guid == '';
57
        }
58
 
59
        public function nodeId($with_ns=true) {
60
                return $with_ns ? 'guid:'.$this->guid : $this->guid;
61
        }
62
 
63
        public function addString($str) {
64
                if (uuid_valid($str)) {
65
                        // real GUID
66
                        return 'guid:'.uuid_canonize($str);
67
                } else {
68
                        // just a category
69
                        return 'guid:'.$this->guid.'/'.$str;
70
                }
71
        }
72
 
73
        public function crudShowId(OIDplusObject $parent) {
74
                $tmp = explode('/',$this->guid);
75
                return end($tmp);
76
        }
77
 
78
        public function jsTreeNodeName(OIDplusObject $parent = null) {
79
                if ($parent == null) return $this->objectTypeTitle();
80
                return $this->crudShowId($parent);
81
        }
82
 
83
        public function defaultTitle() {
84
                return $this->guid;
85
        }
86
 
87
        public function isLeafNode() {
88
                return uuid_valid($this->guid);
89
        }
90
 
753 daniel-mar 91
        private function getTechInfo() {
92
                $tech_info = array();
93
                $tech_info[_L('UUID')] = uuid_canonize($this->guid);
94
                $tech_info[_L('C++ notation')] = uuid_c_syntax($this->guid);
95
 
96
                ob_start();
97
                uuid_info($this->guid);
98
                $info = ob_get_contents();
99
                preg_match_all('@([^:]+):\s*(.+)\n@ismU', $info, $m, PREG_SET_ORDER);
100
                foreach ($m as $m1) {
101
                        $key = $m1[1];
102
                        $value = $m1[2];
103
                        $tech_info[$key] = $value;
104
                }
105
                ob_end_clean();
106
 
107
                return $tech_info;
108
        }
109
 
635 daniel-mar 110
        public function getContentPage(&$title, &$content, &$icon) {
800 daniel-mar 111
                $icon = file_exists(__DIR__.'/img/main_icon.png') ? OIDplus::webPath(__DIR__,true).'img/main_icon.png' : '';
635 daniel-mar 112
 
113
                if ($this->isRoot()) {
114
                        $title = OIDplusGuid::objectTypeTitle();
115
 
116
                        $res = OIDplus::db()->query("select * from ###objects where parent = ?", array(self::root()));
790 daniel-mar 117
                        if ($res->any()) {
635 daniel-mar 118
                                $content  = _L('Please select a GUID in the tree view at the left to show its contents.');
119
                        } else {
120
                                $content  = _L('Currently, no GUID is registered in the system.');
121
                        }
122
 
123
                        if (!$this->isLeafNode()) {
124
                                if (OIDplus::authUtils()->isAdminLoggedIn()) {
125
                                        $content .= '<h2>'._L('Manage root objects / categories').'</h2>';
126
                                } else {
127
                                        $content .= '<h2>'._L('Available objects / categories').'</h2>';
128
                                }
129
                                $content .= '%%CRUD%%';
130
                        }
131
                } else {
132
                        $title = $this->getTitle();
133
 
134
                        if ($this->isLeafNode()) {
753 daniel-mar 135
                                $tech_info = $this->getTechInfo();
136
                                $tech_info_html = '';
137
                                if (count($tech_info) > 0) {
138
                                        $tech_info_html .= '<h2>'._L('Technical information').'</h2>';
139
                                        $tech_info_html .= '<table border="0">';
140
                                        foreach ($tech_info as $key => $value) {
141
                                                $tech_info_html .= '<tr><td>'.$key.': </td><td><code>'.$value.'</code></td></tr>';
142
                                        }
143
                                        $tech_info_html .= '</table>';
144
                                }
635 daniel-mar 145
 
753 daniel-mar 146
                                $content = $tech_info_html;
147
 
148
                                // $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 149
                        } else {
150
                                $content = '';
151
                        }
152
 
153
                        $content .= '<h2>'._L('Description').'</h2>%%DESC%%';
154
 
155
                        if (!$this->isLeafNode()) {
156
                                if ($this->userHasWriteRights()) {
157
                                        $content .= '<h2>'._L('Create or change subsequent objects / categories').'</h2>';
158
                                } else {
159
                                        $content .= '<h2>'._L('Subsequent objects / categories').'</h2>';
160
                                }
161
                                $content .= '%%CRUD%%';
162
                        }
163
                }
164
        }
165
 
166
        // TODO: It would be nice if category and leaf items could have different pictures.
167
        //       But the problem is, that the RA link should have a orange "GUID" icon, not a folder icon
168
        /*
169
        public function getIcon($row) {
170
                if (!$this->isLeafNode()) return null; // foldericon
171
                return parent::getIcon($row);
172
        }
173
        */
174
 
175
        public function one_up() {
176
                // A GUID is a GUID, there is no hierarchy
177
                return false;
178
        }
179
 
180
        public function distance($to) {
181
                // Distance between GUIDs is not possible
182
                return null;
183
        }
184
 
185
        public function getAltIds() {
186
                if ($this->isRoot()) return array();
187
                if (!$this->isLeafNode()) return array();
188
                $ids = parent::getAltIds();
189
                $ids[] = new OIDplusAltId('oid', uuid_to_oid($this->guid), _L('OID representation of UUID'));
190
                return $ids;
191
        }
192
 
193
        public function getDirectoryName() {
194
                if ($this->isLeafNode()) {
195
                        // Leaf (UUID)
196
                        // Example output: "guid_adb0b042_5b57_11eb_b0d9_3c4a92df8582"
197
                        $str = $this->nodeId(false);
198
                        $str = str_replace('-', '_', $str);
199
                        $str = strtolower($str);
200
                        return $this->ns().'_'.$str;
201
                } else {
202
                        // Category
203
                        return parent::getDirectoryName();
204
                }
205
        }
800 daniel-mar 206
 
207
        public function rootIconname($mode) {
208
                return 'img/'.$mode.'_icon16.png';
209
        }
707 daniel-mar 210
}