Rev 1035 | Rev 1086 | 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 | |||
1050 | daniel-mar | 20 | namespace ViaThinkSoft\OIDplus; |
635 | daniel-mar | 21 | |
22 | class OIDplusGuid extends OIDplusObject { |
||
23 | private $guid; |
||
24 | |||
25 | public function __construct($guid) { |
||
26 | if (uuid_valid($guid)) { |
||
856 | daniel-mar | 27 | $this->guid = strtolower(uuid_canonize($guid)); // It is a real GUID (leaf node) |
635 | daniel-mar | 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); |
||
860 | daniel-mar | 35 | if ($namespace !== self::ns()) return false; |
635 | daniel-mar | 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() { |
||
860 | daniel-mar | 52 | return self::ns().':'; |
635 | daniel-mar | 53 | } |
54 | |||
55 | public function isRoot() { |
||
56 | return $this->guid == ''; |
||
57 | } |
||
58 | |||
59 | public function nodeId($with_ns=true) { |
||
859 | daniel-mar | 60 | return $with_ns ? self::root().$this->guid : $this->guid; |
635 | daniel-mar | 61 | } |
62 | |||
63 | public function addString($str) { |
||
64 | if (uuid_valid($str)) { |
||
65 | // real GUID |
||
858 | daniel-mar | 66 | return self::root() . strtolower(uuid_canonize($str)); |
635 | daniel-mar | 67 | } else { |
68 | // just a category |
||
858 | daniel-mar | 69 | if ($this->isRoot()) { |
70 | return self::root() . $str; |
||
71 | } else { |
||
72 | return $this->nodeId() . '/' . $str; |
||
73 | } |
||
635 | daniel-mar | 74 | } |
75 | } |
||
76 | |||
77 | public function crudShowId(OIDplusObject $parent) { |
||
850 | daniel-mar | 78 | if ($this->isLeafNode()) { |
79 | // We don't parse '/' in a valid FourCC code (i.e. Leaf node) |
||
80 | return $this->nodeId(false); |
||
81 | } else { |
||
82 | if ($parent->isRoot()) { |
||
83 | return substr($this->nodeId(), strlen($parent->nodeId())); |
||
84 | } else { |
||
85 | return substr($this->nodeId(), strlen($parent->nodeId())+1); |
||
86 | } |
||
87 | } |
||
635 | daniel-mar | 88 | } |
89 | |||
90 | public function jsTreeNodeName(OIDplusObject $parent = null) { |
||
91 | if ($parent == null) return $this->objectTypeTitle(); |
||
92 | return $this->crudShowId($parent); |
||
93 | } |
||
94 | |||
95 | public function defaultTitle() { |
||
96 | return $this->guid; |
||
97 | } |
||
98 | |||
99 | public function isLeafNode() { |
||
100 | return uuid_valid($this->guid); |
||
101 | } |
||
102 | |||
753 | daniel-mar | 103 | private function getTechInfo() { |
104 | $tech_info = array(); |
||
856 | daniel-mar | 105 | $tech_info[_L('UUID')] = strtolower(uuid_canonize($this->guid)); |
753 | daniel-mar | 106 | $tech_info[_L('C++ notation')] = uuid_c_syntax($this->guid); |
107 | |||
108 | ob_start(); |
||
109 | uuid_info($this->guid); |
||
110 | $info = ob_get_contents(); |
||
111 | preg_match_all('@([^:]+):\s*(.+)\n@ismU', $info, $m, PREG_SET_ORDER); |
||
112 | foreach ($m as $m1) { |
||
113 | $key = $m1[1]; |
||
114 | $value = $m1[2]; |
||
115 | $tech_info[$key] = $value; |
||
116 | } |
||
117 | ob_end_clean(); |
||
118 | |||
119 | return $tech_info; |
||
120 | } |
||
121 | |||
635 | daniel-mar | 122 | public function getContentPage(&$title, &$content, &$icon) { |
801 | daniel-mar | 123 | $icon = file_exists(__DIR__.'/img/main_icon.png') ? OIDplus::webpath(__DIR__,OIDplus::PATH_RELATIVE).'img/main_icon.png' : ''; |
635 | daniel-mar | 124 | |
125 | if ($this->isRoot()) { |
||
126 | $title = OIDplusGuid::objectTypeTitle(); |
||
127 | |||
128 | $res = OIDplus::db()->query("select * from ###objects where parent = ?", array(self::root())); |
||
790 | daniel-mar | 129 | if ($res->any()) { |
962 | daniel-mar | 130 | $content = '<p>'._L('Please select a GUID in the tree view at the left to show its contents.').'</p>'; |
635 | daniel-mar | 131 | } else { |
962 | daniel-mar | 132 | $content = '<p>'._L('Currently, no GUID is registered in the system.').'</p>'; |
635 | daniel-mar | 133 | } |
134 | |||
135 | if (!$this->isLeafNode()) { |
||
136 | if (OIDplus::authUtils()->isAdminLoggedIn()) { |
||
137 | $content .= '<h2>'._L('Manage root objects / categories').'</h2>'; |
||
138 | } else { |
||
139 | $content .= '<h2>'._L('Available objects / categories').'</h2>'; |
||
140 | } |
||
141 | $content .= '%%CRUD%%'; |
||
142 | } |
||
143 | } else { |
||
144 | $title = $this->getTitle(); |
||
145 | |||
146 | if ($this->isLeafNode()) { |
||
753 | daniel-mar | 147 | $tech_info = $this->getTechInfo(); |
148 | $tech_info_html = ''; |
||
149 | if (count($tech_info) > 0) { |
||
150 | $tech_info_html .= '<h2>'._L('Technical information').'</h2>'; |
||
151 | $tech_info_html .= '<table border="0">'; |
||
152 | foreach ($tech_info as $key => $value) { |
||
153 | $tech_info_html .= '<tr><td>'.$key.': </td><td><code>'.$value.'</code></td></tr>'; |
||
154 | } |
||
155 | $tech_info_html .= '</table>'; |
||
156 | } |
||
635 | daniel-mar | 157 | |
753 | daniel-mar | 158 | $content = $tech_info_html; |
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>"; |
||
635 | daniel-mar | 161 | } else { |
162 | $content = ''; |
||
163 | } |
||
164 | |||
165 | $content .= '<h2>'._L('Description').'</h2>%%DESC%%'; |
||
166 | |||
167 | if (!$this->isLeafNode()) { |
||
168 | if ($this->userHasWriteRights()) { |
||
928 | daniel-mar | 169 | $content .= '<h2>'._L('Create or change subordinate objects / categories').'</h2>'; |
635 | daniel-mar | 170 | } else { |
928 | daniel-mar | 171 | $content .= '<h2>'._L('Subordinate objects / categories').'</h2>'; |
635 | daniel-mar | 172 | } |
173 | $content .= '%%CRUD%%'; |
||
174 | } |
||
175 | } |
||
176 | } |
||
177 | |||
1035 | daniel-mar | 178 | public function getIcon($row=null) { |
179 | $in_login_treenode = false; |
||
180 | foreach (debug_backtrace() as $trace) { |
||
181 | // If we are inside the "Login" area (i.e. "Root object links"), we want the |
||
182 | // correct icon, not a folder icon! |
||
1050 | daniel-mar | 183 | if ($trace['class'] === OIDplusPagePublicLogin::class) $in_login_treenode = true; |
1035 | daniel-mar | 184 | } |
185 | |||
186 | if (!$in_login_treenode && !$this->isLeafNode()) return null; // foldericon |
||
187 | |||
635 | daniel-mar | 188 | return parent::getIcon($row); |
189 | } |
||
190 | |||
191 | public function one_up() { |
||
192 | // A GUID is a GUID, there is no hierarchy |
||
193 | return false; |
||
194 | } |
||
195 | |||
196 | public function distance($to) { |
||
197 | // Distance between GUIDs is not possible |
||
198 | return null; |
||
199 | } |
||
200 | |||
201 | public function getAltIds() { |
||
202 | if ($this->isRoot()) return array(); |
||
203 | if (!$this->isLeafNode()) return array(); |
||
204 | $ids = parent::getAltIds(); |
||
205 | $ids[] = new OIDplusAltId('oid', uuid_to_oid($this->guid), _L('OID representation of UUID')); |
||
206 | return $ids; |
||
207 | } |
||
208 | |||
209 | public function getDirectoryName() { |
||
210 | if ($this->isLeafNode()) { |
||
211 | // Leaf (UUID) |
||
212 | // Example output: "guid_adb0b042_5b57_11eb_b0d9_3c4a92df8582" |
||
213 | $str = $this->nodeId(false); |
||
214 | $str = str_replace('-', '_', $str); |
||
215 | $str = strtolower($str); |
||
216 | return $this->ns().'_'.$str; |
||
217 | } else { |
||
218 | // Category |
||
219 | return parent::getDirectoryName(); |
||
220 | } |
||
221 | } |
||
800 | daniel-mar | 222 | |
805 | daniel-mar | 223 | public static function treeIconFilename($mode) { |
800 | daniel-mar | 224 | return 'img/'.$mode.'_icon16.png'; |
225 | } |
||
707 | daniel-mar | 226 | } |