Subversion Repositories oidplus

Rev

Rev 1035 | Rev 1086 | Go to most recent revision | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 1035 Rev 1050
1
<?php
1
<?php
2
 
2
 
3
/*
3
/*
4
 * OIDplus 2.0
4
 * OIDplus 2.0
5
 * Copyright 2019 - 2021 Daniel Marschall, ViaThinkSoft
5
 * Copyright 2019 - 2021 Daniel Marschall, ViaThinkSoft
6
 *
6
 *
7
 * Licensed under the Apache License, Version 2.0 (the "License");
7
 * Licensed under the Apache License, Version 2.0 (the "License");
8
 * you may not use this file except in compliance with 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
9
 * You may obtain a copy of the License at
10
 *
10
 *
11
 *     http://www.apache.org/licenses/LICENSE-2.0
11
 *     http://www.apache.org/licenses/LICENSE-2.0
12
 *
12
 *
13
 * Unless required by applicable law or agreed to in writing, software
13
 * Unless required by applicable law or agreed to in writing, software
14
 * distributed under the License is distributed on an "AS IS" BASIS,
14
 * distributed under the License is distributed on an "AS IS" BASIS,
15
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16
 * See the License for the specific language governing permissions and
16
 * See the License for the specific language governing permissions and
17
 * limitations under the License.
17
 * limitations under the License.
18
 */
18
 */
19
 
19
 
20
if (!defined('INSIDE_OIDPLUS')) die();
20
namespace ViaThinkSoft\OIDplus;
21
 
21
 
22
class OIDplusGuid extends OIDplusObject {
22
class OIDplusGuid extends OIDplusObject {
23
        private $guid;
23
        private $guid;
24
 
24
 
25
        public function __construct($guid) {
25
        public function __construct($guid) {
26
                if (uuid_valid($guid)) {
26
                if (uuid_valid($guid)) {
27
                        $this->guid = strtolower(uuid_canonize($guid)); // It is a real GUID (leaf node)
27
                        $this->guid = strtolower(uuid_canonize($guid)); // It is a real GUID (leaf node)
28
                } else {
28
                } else {
29
                        $this->guid = $guid; // It is a category name
29
                        $this->guid = $guid; // It is a category name
30
                }
30
                }
31
        }
31
        }
32
 
32
 
33
        public static function parse($node_id) {
33
        public static function parse($node_id) {
34
                @list($namespace, $guid) = explode(':', $node_id, 2);
34
                @list($namespace, $guid) = explode(':', $node_id, 2);
35
                if ($namespace !== self::ns()) return false;
35
                if ($namespace !== self::ns()) return false;
36
                return new self($guid);
36
                return new self($guid);
37
        }
37
        }
38
 
38
 
39
        public static function objectTypeTitle() {
39
        public static function objectTypeTitle() {
40
                return _L('Globally Unique Identifier (GUID)');
40
                return _L('Globally Unique Identifier (GUID)');
41
        }
41
        }
42
 
42
 
43
        public static function objectTypeTitleShort() {
43
        public static function objectTypeTitleShort() {
44
                return _L('GUID');
44
                return _L('GUID');
45
        }
45
        }
46
 
46
 
47
        public static function ns() {
47
        public static function ns() {
48
                return 'guid';
48
                return 'guid';
49
        }
49
        }
50
 
50
 
51
        public static function root() {
51
        public static function root() {
52
                return self::ns().':';
52
                return self::ns().':';
53
        }
53
        }
54
 
54
 
55
        public function isRoot() {
55
        public function isRoot() {
56
                return $this->guid == '';
56
                return $this->guid == '';
57
        }
57
        }
58
 
58
 
59
        public function nodeId($with_ns=true) {
59
        public function nodeId($with_ns=true) {
60
                return $with_ns ? self::root().$this->guid : $this->guid;
60
                return $with_ns ? self::root().$this->guid : $this->guid;
61
        }
61
        }
62
 
62
 
63
        public function addString($str) {
63
        public function addString($str) {
64
                if (uuid_valid($str)) {
64
                if (uuid_valid($str)) {
65
                        // real GUID
65
                        // real GUID
66
                        return self::root() . strtolower(uuid_canonize($str));
66
                        return self::root() . strtolower(uuid_canonize($str));
67
                } else {
67
                } else {
68
                        // just a category
68
                        // just a category
69
                        if ($this->isRoot()) {
69
                        if ($this->isRoot()) {
70
                                return self::root() . $str;
70
                                return self::root() . $str;
71
                        } else {
71
                        } else {
72
                                return $this->nodeId() . '/' . $str;
72
                                return $this->nodeId() . '/' . $str;
73
                        }
73
                        }
74
                }
74
                }
75
        }
75
        }
76
 
76
 
77
        public function crudShowId(OIDplusObject $parent) {
77
        public function crudShowId(OIDplusObject $parent) {
78
                if ($this->isLeafNode()) {
78
                if ($this->isLeafNode()) {
79
                        // We don't parse '/' in a valid FourCC code (i.e. Leaf node)
79
                        // We don't parse '/' in a valid FourCC code (i.e. Leaf node)
80
                        return $this->nodeId(false);
80
                        return $this->nodeId(false);
81
                } else {
81
                } else {
82
                        if ($parent->isRoot()) {
82
                        if ($parent->isRoot()) {
83
                                return substr($this->nodeId(), strlen($parent->nodeId()));
83
                                return substr($this->nodeId(), strlen($parent->nodeId()));
84
                        } else {
84
                        } else {
85
                                return substr($this->nodeId(), strlen($parent->nodeId())+1);
85
                                return substr($this->nodeId(), strlen($parent->nodeId())+1);
86
                        }
86
                        }
87
                }
87
                }
88
        }
88
        }
89
 
89
 
90
        public function jsTreeNodeName(OIDplusObject $parent = null) {
90
        public function jsTreeNodeName(OIDplusObject $parent = null) {
91
                if ($parent == null) return $this->objectTypeTitle();
91
                if ($parent == null) return $this->objectTypeTitle();
92
                return $this->crudShowId($parent);
92
                return $this->crudShowId($parent);
93
        }
93
        }
94
 
94
 
95
        public function defaultTitle() {
95
        public function defaultTitle() {
96
                return $this->guid;
96
                return $this->guid;
97
        }
97
        }
98
 
98
 
99
        public function isLeafNode() {
99
        public function isLeafNode() {
100
                return uuid_valid($this->guid);
100
                return uuid_valid($this->guid);
101
        }
101
        }
102
 
102
 
103
        private function getTechInfo() {
103
        private function getTechInfo() {
104
                $tech_info = array();
104
                $tech_info = array();
105
                $tech_info[_L('UUID')] = strtolower(uuid_canonize($this->guid));
105
                $tech_info[_L('UUID')] = strtolower(uuid_canonize($this->guid));
106
                $tech_info[_L('C++ notation')] = uuid_c_syntax($this->guid);
106
                $tech_info[_L('C++ notation')] = uuid_c_syntax($this->guid);
107
 
107
 
108
                ob_start();
108
                ob_start();
109
                uuid_info($this->guid);
109
                uuid_info($this->guid);
110
                $info = ob_get_contents();
110
                $info = ob_get_contents();
111
                preg_match_all('@([^:]+):\s*(.+)\n@ismU', $info, $m, PREG_SET_ORDER);
111
                preg_match_all('@([^:]+):\s*(.+)\n@ismU', $info, $m, PREG_SET_ORDER);
112
                foreach ($m as $m1) {
112
                foreach ($m as $m1) {
113
                        $key = $m1[1];
113
                        $key = $m1[1];
114
                        $value = $m1[2];
114
                        $value = $m1[2];
115
                        $tech_info[$key] = $value;
115
                        $tech_info[$key] = $value;
116
                }
116
                }
117
                ob_end_clean();
117
                ob_end_clean();
118
 
118
 
119
                return $tech_info;
119
                return $tech_info;
120
        }
120
        }
121
 
121
 
122
        public function getContentPage(&$title, &$content, &$icon) {
122
        public function getContentPage(&$title, &$content, &$icon) {
123
                $icon = file_exists(__DIR__.'/img/main_icon.png') ? OIDplus::webpath(__DIR__,OIDplus::PATH_RELATIVE).'img/main_icon.png' : '';
123
                $icon = file_exists(__DIR__.'/img/main_icon.png') ? OIDplus::webpath(__DIR__,OIDplus::PATH_RELATIVE).'img/main_icon.png' : '';
124
 
124
 
125
                if ($this->isRoot()) {
125
                if ($this->isRoot()) {
126
                        $title = OIDplusGuid::objectTypeTitle();
126
                        $title = OIDplusGuid::objectTypeTitle();
127
 
127
 
128
                        $res = OIDplus::db()->query("select * from ###objects where parent = ?", array(self::root()));
128
                        $res = OIDplus::db()->query("select * from ###objects where parent = ?", array(self::root()));
129
                        if ($res->any()) {
129
                        if ($res->any()) {
130
                                $content  = '<p>'._L('Please select a GUID in the tree view at the left to show its contents.').'</p>';
130
                                $content  = '<p>'._L('Please select a GUID in the tree view at the left to show its contents.').'</p>';
131
                        } else {
131
                        } else {
132
                                $content  = '<p>'._L('Currently, no GUID is registered in the system.').'</p>';
132
                                $content  = '<p>'._L('Currently, no GUID is registered in the system.').'</p>';
133
                        }
133
                        }
134
 
134
 
135
                        if (!$this->isLeafNode()) {
135
                        if (!$this->isLeafNode()) {
136
                                if (OIDplus::authUtils()->isAdminLoggedIn()) {
136
                                if (OIDplus::authUtils()->isAdminLoggedIn()) {
137
                                        $content .= '<h2>'._L('Manage root objects / categories').'</h2>';
137
                                        $content .= '<h2>'._L('Manage root objects / categories').'</h2>';
138
                                } else {
138
                                } else {
139
                                        $content .= '<h2>'._L('Available objects / categories').'</h2>';
139
                                        $content .= '<h2>'._L('Available objects / categories').'</h2>';
140
                                }
140
                                }
141
                                $content .= '%%CRUD%%';
141
                                $content .= '%%CRUD%%';
142
                        }
142
                        }
143
                } else {
143
                } else {
144
                        $title = $this->getTitle();
144
                        $title = $this->getTitle();
145
 
145
 
146
                        if ($this->isLeafNode()) {
146
                        if ($this->isLeafNode()) {
147
                                $tech_info = $this->getTechInfo();
147
                                $tech_info = $this->getTechInfo();
148
                                $tech_info_html = '';
148
                                $tech_info_html = '';
149
                                if (count($tech_info) > 0) {
149
                                if (count($tech_info) > 0) {
150
                                        $tech_info_html .= '<h2>'._L('Technical information').'</h2>';
150
                                        $tech_info_html .= '<h2>'._L('Technical information').'</h2>';
151
                                        $tech_info_html .= '<table border="0">';
151
                                        $tech_info_html .= '<table border="0">';
152
                                        foreach ($tech_info as $key => $value) {
152
                                        foreach ($tech_info as $key => $value) {
153
                                                $tech_info_html .= '<tr><td>'.$key.': </td><td><code>'.$value.'</code></td></tr>';
153
                                                $tech_info_html .= '<tr><td>'.$key.': </td><td><code>'.$value.'</code></td></tr>';
154
                                        }
154
                                        }
155
                                        $tech_info_html .= '</table>';
155
                                        $tech_info_html .= '</table>';
156
                                }
156
                                }
157
 
157
 
158
                                $content = $tech_info_html;
158
                                $content = $tech_info_html;
159
 
159
 
160
                                // $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>";
160
                                // $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>";
161
                        } else {
161
                        } else {
162
                                $content = '';
162
                                $content = '';
163
                        }
163
                        }
164
 
164
 
165
                        $content .= '<h2>'._L('Description').'</h2>%%DESC%%';
165
                        $content .= '<h2>'._L('Description').'</h2>%%DESC%%';
166
 
166
 
167
                        if (!$this->isLeafNode()) {
167
                        if (!$this->isLeafNode()) {
168
                                if ($this->userHasWriteRights()) {
168
                                if ($this->userHasWriteRights()) {
169
                                        $content .= '<h2>'._L('Create or change subordinate objects / categories').'</h2>';
169
                                        $content .= '<h2>'._L('Create or change subordinate objects / categories').'</h2>';
170
                                } else {
170
                                } else {
171
                                        $content .= '<h2>'._L('Subordinate objects / categories').'</h2>';
171
                                        $content .= '<h2>'._L('Subordinate objects / categories').'</h2>';
172
                                }
172
                                }
173
                                $content .= '%%CRUD%%';
173
                                $content .= '%%CRUD%%';
174
                        }
174
                        }
175
                }
175
                }
176
        }
176
        }
177
 
177
 
178
        public function getIcon($row=null) {
178
        public function getIcon($row=null) {
179
                $in_login_treenode = false;
179
                $in_login_treenode = false;
180
                foreach (debug_backtrace() as $trace) {
180
                foreach (debug_backtrace() as $trace) {
181
                        // If we are inside the "Login" area (i.e. "Root object links"), we want the
181
                        // If we are inside the "Login" area (i.e. "Root object links"), we want the
182
                        // correct icon, not a folder icon!
182
                        // correct icon, not a folder icon!
183
                        if ($trace['class'] === 'OIDplusPagePublicLogin') $in_login_treenode = true;
183
                        if ($trace['class'] === OIDplusPagePublicLogin::class) $in_login_treenode = true;
184
                }
184
                }
185
 
185
 
186
                if (!$in_login_treenode && !$this->isLeafNode()) return null; // foldericon
186
                if (!$in_login_treenode && !$this->isLeafNode()) return null; // foldericon
187
 
187
 
188
                return parent::getIcon($row);
188
                return parent::getIcon($row);
189
        }
189
        }
190
 
190
 
191
        public function one_up() {
191
        public function one_up() {
192
                // A GUID is a GUID, there is no hierarchy
192
                // A GUID is a GUID, there is no hierarchy
193
                return false;
193
                return false;
194
        }
194
        }
195
 
195
 
196
        public function distance($to) {
196
        public function distance($to) {
197
                // Distance between GUIDs is not possible
197
                // Distance between GUIDs is not possible
198
                return null;
198
                return null;
199
        }
199
        }
200
 
200
 
201
        public function getAltIds() {
201
        public function getAltIds() {
202
                if ($this->isRoot()) return array();
202
                if ($this->isRoot()) return array();
203
                if (!$this->isLeafNode()) return array();
203
                if (!$this->isLeafNode()) return array();
204
                $ids = parent::getAltIds();
204
                $ids = parent::getAltIds();
205
                $ids[] = new OIDplusAltId('oid', uuid_to_oid($this->guid), _L('OID representation of UUID'));
205
                $ids[] = new OIDplusAltId('oid', uuid_to_oid($this->guid), _L('OID representation of UUID'));
206
                return $ids;
206
                return $ids;
207
        }
207
        }
208
 
208
 
209
        public function getDirectoryName() {
209
        public function getDirectoryName() {
210
                if ($this->isLeafNode()) {
210
                if ($this->isLeafNode()) {
211
                        // Leaf (UUID)
211
                        // Leaf (UUID)
212
                        // Example output: "guid_adb0b042_5b57_11eb_b0d9_3c4a92df8582"
212
                        // Example output: "guid_adb0b042_5b57_11eb_b0d9_3c4a92df8582"
213
                        $str = $this->nodeId(false);
213
                        $str = $this->nodeId(false);
214
                        $str = str_replace('-', '_', $str);
214
                        $str = str_replace('-', '_', $str);
215
                        $str = strtolower($str);
215
                        $str = strtolower($str);
216
                        return $this->ns().'_'.$str;
216
                        return $this->ns().'_'.$str;
217
                } else {
217
                } else {
218
                        // Category
218
                        // Category
219
                        return parent::getDirectoryName();
219
                        return parent::getDirectoryName();
220
                }
220
                }
221
        }
221
        }
222
 
222
 
223
        public static function treeIconFilename($mode) {
223
        public static function treeIconFilename($mode) {
224
                return 'img/'.$mode.'_icon16.png';
224
                return 'img/'.$mode.'_icon16.png';
225
        }
225
        }
226
}
226
}
227
 
227