Rev 1329 | Rev 1350 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
2 | daniel-mar | 1 | <?php |
2 | |||
3 | /* |
||
4 | * OIDplus 2.0 |
||
1086 | daniel-mar | 5 | * Copyright 2019 - 2023 Daniel Marschall, ViaThinkSoft |
2 | 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; |
511 | 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 | |||
730 | daniel-mar | 26 | abstract class OIDplusObject extends OIDplusBaseClass { |
1130 | daniel-mar | 27 | |
28 | /** |
||
29 | * |
||
30 | */ |
||
1323 | daniel-mar | 31 | //const UUID_NAMEBASED_NS_OidPlusMisc = 'ad1654e6-7e15-11e4-9ef6-78e3b5fc7f22'; |
150 | daniel-mar | 32 | |
1116 | daniel-mar | 33 | /** |
34 | * Please overwrite this function! |
||
35 | * @param string $node_id |
||
36 | * @return OIDplusObject|null |
||
37 | */ |
||
38 | public static function parse(string $node_id)/*: ?OIDplusObject*/ { |
||
227 | daniel-mar | 39 | foreach (OIDplus::getEnabledObjectTypes() as $ot) { |
954 | daniel-mar | 40 | try { |
956 | daniel-mar | 41 | $good = false; |
1050 | daniel-mar | 42 | if (get_parent_class($ot) == OIDplusObject::class) { |
956 | daniel-mar | 43 | $reflector = new \ReflectionMethod($ot, 'parse'); |
44 | $isImplemented = ($reflector->getDeclaringClass()->getName() === $ot); |
||
45 | if ($isImplemented) { // avoid endless loop if parse is not overriden |
||
46 | $good = true; |
||
47 | } |
||
48 | } |
||
49 | // We need to do the workaround with "$good", otherwise PHPstan shows |
||
50 | // "Call to an undefined static method object::parse()" |
||
51 | if ($good && $obj = $ot::parse($node_id)) return $obj; |
||
1050 | daniel-mar | 52 | } catch (\Exception $e) {} |
2 | daniel-mar | 53 | } |
54 | return null; |
||
55 | } |
||
56 | |||
1116 | daniel-mar | 57 | /** |
58 | * @return OIDplusAltId[] |
||
59 | * @throws OIDplusException |
||
60 | */ |
||
61 | public function getAltIds(): array { |
||
193 | daniel-mar | 62 | if ($this->isRoot()) return array(); |
63 | |||
64 | $ids = array(); |
||
1078 | daniel-mar | 65 | |
1327 | daniel-mar | 66 | // Create Information Object OID/AID/UUID |
67 | // ... but not for OIDs below oid:1.3.6.1.4.1.37476.30.9, because these are the definition of these Information Object AID/OID/UUID (which will be decoded in the OID object type plugin) |
||
68 | if (!str_starts_with($this->nodeId(true), 'oid:1.3.6.1.4.1.37476.30.9.')) { |
||
69 | // Creates an OIDplus-Hash-OID |
||
70 | // ... exclude OIDs, because an OID is already an OID |
||
71 | if ($this->ns() != 'oid') { |
||
72 | $sid = OIDplus::getSystemId(true); |
||
73 | if (!empty($sid)) { |
||
74 | $ns_oid = $this->getPlugin()->getManifest()->getOid(); |
||
75 | $hash_payload = $ns_oid.':'.$this->nodeId(false); |
||
76 | $oid = $sid . '.' . smallhash($hash_payload); |
||
77 | $ids[] = new OIDplusAltId('oid', $oid, _L('OIDplus Information Object OID')); |
||
78 | } |
||
193 | daniel-mar | 79 | } |
929 | daniel-mar | 80 | |
1327 | daniel-mar | 81 | // Make a OIDplus-UUID, but... |
82 | // ... exclude GUID, because a GUID is already a GUID |
||
83 | // ... exclude OIDs which are 2.25, because these are basically GUIDs (but 2nd, 3rd, 4th, ... level is OK) |
||
84 | // Previously, we excluded OID, because an OID already has a record UUID_NAMEBASED_NS_OID (defined by IETF) set by class OIDplusOid |
||
85 | if (($this->ns() != 'guid') && ($this->ns() != 'oid' || $this->one_up()->nodeId(true) != 'oid:2.25') /*&& ($this->ns() != 'oid')*/) { |
||
86 | // Obsolete custom namespace for UUIDv3 and UUIDv5: |
||
87 | //$ids[] = new OIDplusAltId('guid', gen_uuid_md5_namebased(self::UUID_NAMEBASED_NS_OidPlusMisc, $this->nodeId()), _L('Name based version 3 / MD5 UUID with namespace %1','UUID_NAMEBASED_NS_OidPlusMisc')); |
||
88 | //$ids[] = new OIDplusAltId('guid', gen_uuid_sha1_namebased(self::UUID_NAMEBASED_NS_OidPlusMisc, $this->nodeId()), _L('Name based version 5 / SHA1 UUID with namespace %1','UUID_NAMEBASED_NS_OidPlusMisc')); |
||
89 | // New custom UUIDv8: |
||
90 | $sysid = OIDplus::getSystemId(false); |
||
91 | $sysid_int = $sysid ? $sysid : 0; |
||
92 | $unix_ts = $this->getCreatedTime() ? strtotime($this->getCreatedTime()) : 0; |
||
1078 | daniel-mar | 93 | $ns_oid = $this->getPlugin()->getManifest()->getOid(); |
1327 | daniel-mar | 94 | $obj_name = $this->nodeId(false); |
95 | $ids[] = new OIDplusAltId('guid', |
||
96 | gen_uuid_v8( |
||
97 | dechex($sysid_int), |
||
98 | dechex($unix_ts/60/60/24), |
||
99 | dechex(0), |
||
100 | sha1($ns_oid), // Note: No 14bit collission between 1.3.6.1.4.1.37476.2.5.2.4.8.[0-185] |
||
101 | sha1($obj_name) |
||
102 | ), |
||
1329 | daniel-mar | 103 | _L('OIDplus Information Object Custom UUID (RFC4122bis)'), |
104 | '', |
||
105 | 'https://github.com/danielmarschall/oidplus/blob/master/doc/oidplus_custom_guid.md' |
||
1327 | daniel-mar | 106 | ); |
929 | daniel-mar | 107 | } |
1327 | daniel-mar | 108 | |
109 | // Make a AID based on ViaThinkSoft schema |
||
110 | // ... exclude AIDs, because an AID is already an AID |
||
111 | if ($this->ns() != 'aid') { |
||
112 | $sid = OIDplus::getSystemId(false); |
||
113 | if ($sid !== false) { |
||
114 | $ns_oid = $this->getPlugin()->getManifest()->getOid(); |
||
115 | $hash_payload = $ns_oid.':'.$this->nodeId(false); |
||
116 | $sid_hex = strtoupper(str_pad(dechex((int)$sid),8,'0',STR_PAD_LEFT)); |
||
117 | $obj_hex = strtoupper(str_pad(dechex(smallhash($hash_payload)),8,'0',STR_PAD_LEFT)); |
||
118 | $aid = 'D276000186B20005'.$sid_hex.$obj_hex; |
||
1329 | daniel-mar | 119 | $ids[] = new OIDplusAltId('aid', $aid, |
1330 | daniel-mar | 120 | _L('OIDplus Information Object Application Identifier (ISO/IEC 7816)'), |
121 | ' ('._L('No PIX allowed').')', |
||
122 | 'https://oidplus.viathinksoft.com/oidplus/?goto=aid%3AD276000186B20005'); |
||
1327 | daniel-mar | 123 | } |
124 | } |
||
1330 | daniel-mar | 125 | |
126 | // Make a MAC based on AAI (not 100% worldwide unique!) |
||
127 | // ... exclude MACs, because an MAC is already a MAC |
||
128 | if ($this->ns() != 'mac') { |
||
129 | $ns_oid = $this->getPlugin()->getManifest()->getOid(); |
||
130 | $obj_name = $this->nodeId(false); |
||
131 | $mac = strtoupper(substr(sha1($ns_oid.':'.$obj_name),-12)); |
||
132 | $mac = rtrim(chunk_split($mac, 2, '-'),'-'); |
||
133 | |||
134 | $mac[1] = '2'; // 2=AAI Unicast |
||
135 | $ids[] = new OIDplusAltId('mac', $mac, _L('OIDplus Information Object MAC address, Unicast (AAI)')); |
||
136 | |||
137 | $mac[1] = '3'; // 3=AAI Multicast |
||
138 | $ids[] = new OIDplusAltId('mac', $mac, _L('OIDplus Information Object MAC address, Multicast (AAI)')); |
||
139 | } |
||
193 | daniel-mar | 140 | } |
1078 | daniel-mar | 141 | |
193 | daniel-mar | 142 | return $ids; |
83 | daniel-mar | 143 | } |
144 | |||
1116 | daniel-mar | 145 | /** |
146 | * @return string |
||
147 | */ |
||
148 | public abstract static function objectTypeTitle(): string; |
||
2 | daniel-mar | 149 | |
1116 | daniel-mar | 150 | /** |
151 | * @return string |
||
152 | */ |
||
153 | public abstract static function objectTypeTitleShort(): string; |
||
2 | daniel-mar | 154 | |
1116 | daniel-mar | 155 | /** |
156 | * @return OIDplusObjectTypePlugin|null |
||
157 | */ |
||
817 | daniel-mar | 158 | public function getPlugin()/*: ?OIDplusObjectTypePlugin */ { |
159 | $plugins = OIDplus::getObjectTypePlugins(); |
||
160 | foreach ($plugins as $plugin) { |
||
1116 | daniel-mar | 161 | if (get_class($this) == $plugin::getObjectTypeClassName()) { |
817 | daniel-mar | 162 | return $plugin; |
163 | } |
||
164 | } |
||
1116 | daniel-mar | 165 | return null; |
817 | daniel-mar | 166 | } |
167 | |||
1116 | daniel-mar | 168 | /** |
169 | * @return string |
||
170 | */ |
||
171 | public abstract static function ns(): string; |
||
2 | daniel-mar | 172 | |
1116 | daniel-mar | 173 | /** |
174 | * @return string |
||
175 | */ |
||
176 | public abstract static function root(): string; |
||
2 | daniel-mar | 177 | |
1116 | daniel-mar | 178 | /** |
179 | * @return bool |
||
180 | */ |
||
181 | public abstract function isRoot(): bool; |
||
2 | daniel-mar | 182 | |
1116 | daniel-mar | 183 | /** |
184 | * @param bool $with_ns |
||
185 | * @return string |
||
186 | */ |
||
187 | public abstract function nodeId(bool $with_ns=true): string; |
||
2 | daniel-mar | 188 | |
1116 | daniel-mar | 189 | /** |
190 | * @param string $str |
||
191 | * @return string mixed |
||
192 | * @throws OIDplusException |
||
193 | */ |
||
194 | public abstract function addString(string $str): string; |
||
2 | daniel-mar | 195 | |
1116 | daniel-mar | 196 | /** |
197 | * @param OIDplusObject $parent |
||
198 | * @return string |
||
199 | */ |
||
200 | public abstract function crudShowId(OIDplusObject $parent): string; |
||
2 | daniel-mar | 201 | |
1116 | daniel-mar | 202 | /** |
203 | * @return string |
||
204 | */ |
||
205 | public function crudInsertPrefix(): string { |
||
707 | daniel-mar | 206 | return ''; |
207 | } |
||
2 | daniel-mar | 208 | |
1116 | daniel-mar | 209 | /** |
210 | * @return string |
||
211 | */ |
||
212 | public function crudInsertSuffix(): string { |
||
707 | daniel-mar | 213 | return ''; |
214 | } |
||
215 | |||
1116 | daniel-mar | 216 | /** |
217 | * @param OIDplusObject|null $parent |
||
218 | * @return string |
||
219 | */ |
||
220 | public abstract function jsTreeNodeName(OIDplusObject $parent = null): string; |
||
2 | daniel-mar | 221 | |
1116 | daniel-mar | 222 | /** |
223 | * @return string |
||
224 | */ |
||
225 | public abstract function defaultTitle(): string; |
||
2 | daniel-mar | 226 | |
1116 | daniel-mar | 227 | /** |
228 | * @return bool |
||
229 | */ |
||
230 | public abstract function isLeafNode(): bool; |
||
16 | daniel-mar | 231 | |
1116 | daniel-mar | 232 | /** |
233 | * @param string $title |
||
234 | * @param string $content |
||
235 | * @param string $icon |
||
236 | * @return void |
||
237 | */ |
||
238 | public abstract function getContentPage(string &$title, string &$content, string &$icon); |
||
2 | daniel-mar | 239 | |
1116 | daniel-mar | 240 | /** |
241 | * @param OIDplusRA|string|null $ra |
||
242 | * @return array |
||
243 | * @throws OIDplusConfigInitializationException |
||
244 | * @throws OIDplusException |
||
245 | */ |
||
246 | public static function getRaRoots($ra=null) : array{ |
||
247 | if ($ra instanceof OIDplusRA) $ra = $ra->raEmail(); |
||
115 | daniel-mar | 248 | |
27 | daniel-mar | 249 | $out = array(); |
150 | daniel-mar | 250 | |
261 | daniel-mar | 251 | if (!OIDplus::baseConfig()->getValue('OBJECT_CACHING', true)) { |
1116 | daniel-mar | 252 | if (!$ra) { |
1171 | daniel-mar | 253 | $res = OIDplus::db()->query("select oChild.id as child_id, oChild.ra_email as child_mail, oParent.ra_email as parent_mail from ###objects as oChild ". |
1148 | daniel-mar | 254 | "left join ###objects as oParent on oChild.parent = oParent.id"); |
1156 | daniel-mar | 255 | $res->naturalSortByField('oChild.id'); |
236 | daniel-mar | 256 | while ($row = $res->fetch_array()) { |
549 | daniel-mar | 257 | if (!OIDplus::authUtils()->isRaLoggedIn($row['parent_mail']) && OIDplus::authUtils()->isRaLoggedIn($row['child_mail'])) { |
1171 | daniel-mar | 258 | $x = self::parse($row['child_id']); // can be NULL if namespace was disabled |
150 | daniel-mar | 259 | if ($x) $out[] = $x; |
260 | } |
||
261 | } |
||
262 | } else { |
||
1171 | daniel-mar | 263 | $res = OIDplus::db()->query("select oChild.id as child_id from ###objects as oChild ". |
261 | daniel-mar | 264 | "left join ###objects as oParent on oChild.parent = oParent.id ". |
433 | daniel-mar | 265 | "where (".OIDplus::db()->getSlang()->isNullFunction('oParent.ra_email',"''")." <> ? and ". |
266 | OIDplus::db()->getSlang()->isNullFunction('oChild.ra_email',"''")." = ?) or ". |
||
1148 | daniel-mar | 267 | " (oParent.ra_email is null and ".OIDplus::db()->getSlang()->isNullFunction('oChild.ra_email',"''")." = ?) ", |
268 | array($ra, $ra, $ra)); |
||
1156 | daniel-mar | 269 | $res->naturalSortByField('oChild.id'); |
236 | daniel-mar | 270 | while ($row = $res->fetch_array()) { |
1171 | daniel-mar | 271 | $x = self::parse($row['child_id']); // can be NULL if namespace was disabled |
1028 | daniel-mar | 272 | if ($x) $out[] = $x; |
27 | daniel-mar | 273 | } |
2 | daniel-mar | 274 | } |
275 | } else { |
||
1116 | daniel-mar | 276 | if (!$ra) { |
415 | daniel-mar | 277 | $ra_mails_to_check = OIDplus::authUtils()->loggedInRaList(); |
150 | daniel-mar | 278 | if (count($ra_mails_to_check) == 0) return $out; |
279 | } else { |
||
1116 | daniel-mar | 280 | $ra_mails_to_check = array($ra); |
2 | daniel-mar | 281 | } |
150 | daniel-mar | 282 | |
283 | self::buildObjectInformationCache(); |
||
284 | |||
285 | foreach ($ra_mails_to_check as $check_ra_mail) { |
||
193 | daniel-mar | 286 | $out_part = array(); |
150 | daniel-mar | 287 | |
975 | daniel-mar | 288 | foreach (self::$object_info_cache as $id => $cacheitem) { |
1028 | daniel-mar | 289 | if ($cacheitem[self::CACHE_RA_EMAIL] == $check_ra_mail) { |
290 | $parent = $cacheitem[self::CACHE_PARENT]; |
||
291 | if (!isset(self::$object_info_cache[$parent]) || (self::$object_info_cache[$parent][self::CACHE_RA_EMAIL] != $check_ra_mail)) { |
||
292 | $out_part[] = $id; |
||
150 | daniel-mar | 293 | } |
294 | } |
||
295 | } |
||
296 | |||
297 | natsort($out_part); |
||
298 | |||
299 | foreach ($out_part as $id) { |
||
300 | $obj = self::parse($id); |
||
301 | if ($obj) $out[] = $obj; |
||
302 | } |
||
303 | } |
||
2 | daniel-mar | 304 | } |
150 | daniel-mar | 305 | |
2 | daniel-mar | 306 | return $out; |
307 | } |
||
308 | |||
1116 | daniel-mar | 309 | /** |
310 | * @return array |
||
311 | * @throws OIDplusException |
||
312 | */ |
||
313 | public static function getAllNonConfidential(): array { |
||
150 | daniel-mar | 314 | $out = array(); |
315 | |||
261 | daniel-mar | 316 | if (!OIDplus::baseConfig()->getValue('OBJECT_CACHING', true)) { |
1148 | daniel-mar | 317 | $res = OIDplus::db()->query("select id from ###objects where confidential = ?", array(false)); |
1156 | daniel-mar | 318 | $res->naturalSortByField('id'); |
236 | daniel-mar | 319 | while ($row = $res->fetch_array()) { |
150 | daniel-mar | 320 | $obj = self::parse($row['id']); // will be NULL if the object type is not registered |
169 | daniel-mar | 321 | if ($obj && (!$obj->isConfidential())) { |
150 | daniel-mar | 322 | $out[] = $row['id']; |
323 | } |
||
2 | daniel-mar | 324 | } |
325 | } else { |
||
150 | daniel-mar | 326 | self::buildObjectInformationCache(); |
2 | daniel-mar | 327 | |
975 | daniel-mar | 328 | foreach (self::$object_info_cache as $id => $cacheitem) { |
329 | $confidential = $cacheitem[self::CACHE_CONFIDENTIAL]; |
||
150 | daniel-mar | 330 | if (!$confidential) { |
331 | $obj = self::parse($id); // will be NULL if the object type is not registered |
||
169 | daniel-mar | 332 | if ($obj && (!$obj->isConfidential())) { |
150 | daniel-mar | 333 | $out[] = $id; |
334 | } |
||
335 | } |
||
2 | daniel-mar | 336 | } |
337 | } |
||
338 | |||
339 | return $out; |
||
340 | } |
||
341 | |||
1116 | daniel-mar | 342 | /** |
343 | * @return bool |
||
344 | * @throws OIDplusException |
||
345 | */ |
||
346 | public function isConfidential(): bool { |
||
261 | daniel-mar | 347 | if (!OIDplus::baseConfig()->getValue('OBJECT_CACHING', true)) { |
426 | daniel-mar | 348 | //static $confidential_cache = array(); |
150 | daniel-mar | 349 | $curid = $this->nodeId(); |
426 | daniel-mar | 350 | //$orig_curid = $curid; |
351 | //if (isset($confidential_cache[$curid])) return $confidential_cache[$curid]; |
||
150 | daniel-mar | 352 | // Recursively search for the confidential flag in the parents |
790 | daniel-mar | 353 | while (($res = OIDplus::db()->query("select parent, confidential from ###objects where id = ?", array($curid)))->any()) { |
236 | daniel-mar | 354 | $row = $res->fetch_array(); |
150 | daniel-mar | 355 | if ($row['confidential']) { |
426 | daniel-mar | 356 | //$confidential_cache[$curid] = true; |
357 | //$confidential_cache[$orig_curid] = true; |
||
150 | daniel-mar | 358 | return true; |
359 | } else { |
||
426 | daniel-mar | 360 | //$confidential_cache[$curid] = false; |
150 | daniel-mar | 361 | } |
362 | $curid = $row['parent']; |
||
426 | daniel-mar | 363 | //if (isset($confidential_cache[$curid])) { |
364 | //$confidential_cache[$orig_curid] = $confidential_cache[$curid]; |
||
365 | //return $confidential_cache[$curid]; |
||
366 | //} |
||
150 | daniel-mar | 367 | } |
368 | |||
426 | daniel-mar | 369 | //$confidential_cache[$orig_curid] = false; |
150 | daniel-mar | 370 | return false; |
371 | } else { |
||
372 | self::buildObjectInformationCache(); |
||
373 | |||
374 | $curid = $this->nodeId(); |
||
375 | // Recursively search for the confidential flag in the parents |
||
169 | daniel-mar | 376 | while (isset(self::$object_info_cache[$curid])) { |
150 | daniel-mar | 377 | if (self::$object_info_cache[$curid][self::CACHE_CONFIDENTIAL]) return true; |
378 | $curid = self::$object_info_cache[$curid][self::CACHE_PARENT]; |
||
379 | } |
||
380 | return false; |
||
2 | daniel-mar | 381 | } |
382 | } |
||
383 | |||
1116 | daniel-mar | 384 | /** |
385 | * @param OIDplusObject $obj |
||
386 | * @return bool |
||
387 | * @throws OIDplusException |
||
388 | */ |
||
389 | public function isChildOf(OIDplusObject $obj): bool { |
||
261 | daniel-mar | 390 | if (!OIDplus::baseConfig()->getValue('OBJECT_CACHING', true)) { |
150 | daniel-mar | 391 | $curid = $this->nodeId(); |
790 | daniel-mar | 392 | while (($res = OIDplus::db()->query("select parent from ###objects where id = ?", array($curid)))->any()) { |
236 | daniel-mar | 393 | $row = $res->fetch_array(); |
150 | daniel-mar | 394 | if ($curid == $obj->nodeId()) return true; |
395 | $curid = $row['parent']; |
||
396 | } |
||
397 | return false; |
||
398 | } else { |
||
399 | self::buildObjectInformationCache(); |
||
400 | |||
401 | $curid = $this->nodeId(); |
||
169 | daniel-mar | 402 | while (isset(self::$object_info_cache[$curid])) { |
150 | daniel-mar | 403 | if ($curid == $obj->nodeId()) return true; |
404 | $curid = self::$object_info_cache[$curid][self::CACHE_PARENT]; |
||
405 | } |
||
406 | return false; |
||
2 | daniel-mar | 407 | } |
150 | daniel-mar | 408 | } |
2 | daniel-mar | 409 | |
1116 | daniel-mar | 410 | /** |
411 | * @return array |
||
412 | * @throws OIDplusException |
||
413 | */ |
||
414 | public function getChildren(): array { |
||
150 | daniel-mar | 415 | $out = array(); |
261 | daniel-mar | 416 | if (!OIDplus::baseConfig()->getValue('OBJECT_CACHING', true)) { |
417 | $res = OIDplus::db()->query("select id from ###objects where parent = ?", array($this->nodeId())); |
||
236 | daniel-mar | 418 | while ($row = $res->fetch_array()) { |
150 | daniel-mar | 419 | $obj = self::parse($row['id']); |
420 | if (!$obj) continue; |
||
421 | $out[] = $obj; |
||
422 | } |
||
423 | } else { |
||
424 | self::buildObjectInformationCache(); |
||
425 | |||
975 | daniel-mar | 426 | foreach (self::$object_info_cache as $id => $cacheitem) { |
427 | $parent = $cacheitem[self::CACHE_PARENT]; |
||
150 | daniel-mar | 428 | if ($parent == $this->nodeId()) { |
429 | $obj = self::parse($id); |
||
430 | if (!$obj) continue; |
||
431 | $out[] = $obj; |
||
432 | } |
||
433 | } |
||
434 | } |
||
435 | return $out; |
||
2 | daniel-mar | 436 | } |
437 | |||
1116 | daniel-mar | 438 | /** |
1137 | daniel-mar | 439 | * @return OIDplusRA|null |
1116 | daniel-mar | 440 | * @throws OIDplusException |
441 | */ |
||
1137 | daniel-mar | 442 | public function getRa()/*: ?OIDplusRA*/ { |
443 | $ra = $this->getRaMail(); |
||
444 | return $ra ? new OIDplusRA($ra) : null; |
||
115 | daniel-mar | 445 | } |
446 | |||
1116 | daniel-mar | 447 | /** |
448 | * @param OIDplusRA|string|null $ra |
||
449 | * @return bool |
||
450 | * @throws OIDplusConfigInitializationException |
||
451 | * @throws OIDplusException |
||
452 | */ |
||
453 | public function userHasReadRights($ra=null): bool { |
||
454 | if ($ra instanceof OIDplusRA) $ra = $ra->raEmail(); |
||
115 | daniel-mar | 455 | |
2 | daniel-mar | 456 | // If it is not confidential, everybody can read/see it. |
416 | daniel-mar | 457 | // Note: This also checks if superior OIDs are confidential. |
2 | daniel-mar | 458 | if (!$this->isConfidential()) return true; |
459 | |||
1116 | daniel-mar | 460 | if (!$ra) { |
416 | daniel-mar | 461 | // Admin may do everything |
549 | daniel-mar | 462 | if (OIDplus::authUtils()->isAdminLoggedIn()) return true; |
416 | daniel-mar | 463 | |
464 | // If the RA is logged in, then they can see the OID. |
||
1137 | daniel-mar | 465 | $ownRa = $this->getRaMail(); |
466 | if ($ownRa && OIDplus::authUtils()->isRaLoggedIn($ownRa)) return true; |
||
2 | daniel-mar | 467 | } else { |
416 | daniel-mar | 468 | // If this OID belongs to the requested RA, then they may see it. |
1116 | daniel-mar | 469 | if ($this->getRaMail() == $ra) return true; |
2 | daniel-mar | 470 | } |
471 | |||
472 | // If someone has rights to an object below our confidential node, |
||
473 | // we let him see the confidential node, |
||
474 | // Otherwise he could not browse through to his own node. |
||
1116 | daniel-mar | 475 | $roots = $this->getRaRoots($ra); |
2 | daniel-mar | 476 | foreach ($roots as $root) { |
477 | if ($root->isChildOf($this)) return true; |
||
478 | } |
||
479 | |||
480 | return false; |
||
481 | } |
||
482 | |||
1116 | daniel-mar | 483 | /** |
484 | * @param array|null $row |
||
485 | * @return string|null |
||
486 | * @throws OIDplusException |
||
487 | */ |
||
488 | public function getIcon(array $row=null) { |
||
20 | daniel-mar | 489 | $namespace = $this->ns(); // must use $this, not self::, otherwise the virtual method will not be called |
2 | daniel-mar | 490 | |
491 | if (is_null($row)) { |
||
150 | daniel-mar | 492 | $ra_email = $this->getRaMail(); |
493 | } else { |
||
494 | $ra_email = $row['ra_email']; |
||
2 | daniel-mar | 495 | } |
632 | daniel-mar | 496 | |
1127 | daniel-mar | 497 | // $dirs = glob(OIDplus::localpath().'plugins/'.'*'.'/objectTypes/'.$namespace.'/'); |
498 | // if (count($dirs) == 0) return null; // default icon (folder) |
||
499 | // $dir = substr($dirs[0], strlen(OIDplus::localpath())); |
||
500 | $reflection = new \ReflectionClass($this); |
||
501 | $dir = dirname($reflection->getFilename()); |
||
502 | $dir = substr($dir, strlen(OIDplus::localpath())); |
||
1130 | daniel-mar | 503 | $dir = str_replace('\\', '/', $dir); |
632 | daniel-mar | 504 | |
1124 | daniel-mar | 505 | if ($this->isRoot()) { |
1127 | daniel-mar | 506 | $icon = $dir . '/' . $this::treeIconFilename('root'); |
2 | daniel-mar | 507 | } else { |
1124 | daniel-mar | 508 | // We use $this:: instead of self:: , because we want to call the overridden methods |
1126 | daniel-mar | 509 | if ($ra_email && OIDplus::authUtils()->isRaLoggedIn($ra_email)) { |
1124 | daniel-mar | 510 | if ($this->isLeafNode()) { |
511 | $icon = $dir . '/' . $this::treeIconFilename('own_leaf'); |
||
512 | if (!file_exists($icon)) $icon = $dir . '/' . $this::treeIconFilename('own'); |
||
513 | } else { |
||
514 | $icon = $dir . '/' . $this::treeIconFilename('own'); |
||
515 | } |
||
516 | } else { |
||
517 | if ($this->isLeafNode()) { |
||
518 | $icon = $dir . '/' . $this::treeIconFilename('general_leaf'); |
||
519 | if (!file_exists($icon)) $icon = $dir . '/' . $this::treeIconFilename('general'); |
||
520 | } else { |
||
521 | $icon = $dir . '/' . $this::treeIconFilename('general'); |
||
522 | } |
||
523 | } |
||
2 | daniel-mar | 524 | } |
632 | daniel-mar | 525 | |
526 | if (!file_exists($icon)) return null; // default icon (folder) |
||
527 | |||
2 | daniel-mar | 528 | return $icon; |
529 | } |
||
530 | |||
1116 | daniel-mar | 531 | /** |
532 | * @param string $id |
||
533 | * @return bool |
||
534 | * @throws OIDplusException |
||
535 | */ |
||
536 | public static function exists(string $id): bool { |
||
261 | daniel-mar | 537 | if (!OIDplus::baseConfig()->getValue('OBJECT_CACHING', true)) { |
538 | $res = OIDplus::db()->query("select id from ###objects where id = ?", array($id)); |
||
790 | daniel-mar | 539 | return $res->any(); |
150 | daniel-mar | 540 | } else { |
541 | self::buildObjectInformationCache(); |
||
542 | return isset(self::$object_info_cache[$id]); |
||
543 | } |
||
12 | daniel-mar | 544 | } |
545 | |||
1116 | daniel-mar | 546 | /** |
547 | * Get parent gives the next possible parent which is EXISTING in OIDplus |
||
548 | * It does not give the immediate parent |
||
549 | * @return OIDplusObject|null |
||
550 | * @throws OIDplusException |
||
551 | */ |
||
979 | daniel-mar | 552 | public function getParent()/*: ?OIDplusObject*/ { |
261 | daniel-mar | 553 | if (!OIDplus::baseConfig()->getValue('OBJECT_CACHING', true)) { |
554 | $res = OIDplus::db()->query("select parent from ###objects where id = ?", array($this->nodeId())); |
||
1271 | daniel-mar | 555 | if ($res->any()) { |
556 | $row = $res->fetch_array(); |
||
557 | $parent = $row['parent']; |
||
558 | $obj = OIDplusObject::parse($parent); |
||
559 | if ($obj) return $obj; |
||
560 | } |
||
150 | daniel-mar | 561 | } else { |
562 | self::buildObjectInformationCache(); |
||
563 | if (isset(self::$object_info_cache[$this->nodeId()])) { |
||
564 | $parent = self::$object_info_cache[$this->nodeId()][self::CACHE_PARENT]; |
||
565 | $obj = OIDplusObject::parse($parent); |
||
566 | if ($obj) return $obj; |
||
567 | } |
||
1271 | daniel-mar | 568 | } |
20 | daniel-mar | 569 | |
1271 | daniel-mar | 570 | // If this OID does not exist, the SQL query "select parent from ..." does not work. So we try to find the next possible parent using one_up() |
571 | $cur = $this->one_up(); |
||
572 | if (!$cur) return null; |
||
573 | do { |
||
574 | // findFitting() checks if that OID exists |
||
575 | if ($fitting = self::findFitting($cur->nodeId())) return $fitting; |
||
576 | |||
577 | $prev = $cur; |
||
578 | $cur = $cur->one_up(); |
||
418 | daniel-mar | 579 | if (!$cur) return null; |
1271 | daniel-mar | 580 | } while ($prev->nodeId() !== $cur->nodeId()); |
20 | daniel-mar | 581 | |
979 | daniel-mar | 582 | return null; |
2 | daniel-mar | 583 | } |
584 | |||
1116 | daniel-mar | 585 | /** |
1137 | daniel-mar | 586 | * @return string|null |
1116 | daniel-mar | 587 | * @throws OIDplusException |
588 | */ |
||
2 | daniel-mar | 589 | public function getRaMail() { |
261 | daniel-mar | 590 | if (!OIDplus::baseConfig()->getValue('OBJECT_CACHING', true)) { |
591 | $res = OIDplus::db()->query("select ra_email from ###objects where id = ?", array($this->nodeId())); |
||
790 | daniel-mar | 592 | if (!$res->any()) return null; |
236 | daniel-mar | 593 | $row = $res->fetch_array(); |
150 | daniel-mar | 594 | return $row['ra_email']; |
595 | } else { |
||
596 | self::buildObjectInformationCache(); |
||
597 | if (isset(self::$object_info_cache[$this->nodeId()])) { |
||
598 | return self::$object_info_cache[$this->nodeId()][self::CACHE_RA_EMAIL]; |
||
599 | } |
||
1137 | daniel-mar | 600 | return null; |
150 | daniel-mar | 601 | } |
2 | daniel-mar | 602 | } |
603 | |||
1116 | daniel-mar | 604 | /** |
1142 | daniel-mar | 605 | * @return string|null |
1116 | daniel-mar | 606 | * @throws OIDplusException |
607 | */ |
||
192 | daniel-mar | 608 | public function getTitle() { |
261 | daniel-mar | 609 | if (!OIDplus::baseConfig()->getValue('OBJECT_CACHING', true)) { |
610 | $res = OIDplus::db()->query("select title from ###objects where id = ?", array($this->nodeId())); |
||
790 | daniel-mar | 611 | if (!$res->any()) return null; |
236 | daniel-mar | 612 | $row = $res->fetch_array(); |
192 | daniel-mar | 613 | return $row['title']; |
614 | } else { |
||
615 | self::buildObjectInformationCache(); |
||
616 | if (isset(self::$object_info_cache[$this->nodeId()])) { |
||
617 | return self::$object_info_cache[$this->nodeId()][self::CACHE_TITLE]; |
||
618 | } |
||
1142 | daniel-mar | 619 | return null; |
192 | daniel-mar | 620 | } |
621 | } |
||
622 | |||
1116 | daniel-mar | 623 | /** |
1142 | daniel-mar | 624 | * @return string|null |
1116 | daniel-mar | 625 | * @throws OIDplusException |
626 | */ |
||
975 | daniel-mar | 627 | public function getDescription() { |
628 | if (!OIDplus::baseConfig()->getValue('OBJECT_CACHING', true)) { |
||
629 | $res = OIDplus::db()->query("select description from ###objects where id = ?", array($this->nodeId())); |
||
630 | if (!$res->any()) return null; |
||
631 | $row = $res->fetch_array(); |
||
632 | return $row['description']; |
||
633 | } else { |
||
634 | self::buildObjectInformationCache(); |
||
635 | if (isset(self::$object_info_cache[$this->nodeId()])) { |
||
636 | return self::$object_info_cache[$this->nodeId()][self::CACHE_DESCRIPTION]; |
||
637 | } |
||
1142 | daniel-mar | 638 | return null; |
975 | daniel-mar | 639 | } |
640 | } |
||
641 | |||
1116 | daniel-mar | 642 | /** |
1142 | daniel-mar | 643 | * @return string|null |
1116 | daniel-mar | 644 | * @throws OIDplusException |
645 | */ |
||
975 | daniel-mar | 646 | public function getComment() { |
647 | if (!OIDplus::baseConfig()->getValue('OBJECT_CACHING', true)) { |
||
648 | $res = OIDplus::db()->query("select comment from ###objects where id = ?", array($this->nodeId())); |
||
649 | if (!$res->any()) return null; |
||
650 | $row = $res->fetch_array(); |
||
651 | return $row['comment']; |
||
652 | } else { |
||
653 | self::buildObjectInformationCache(); |
||
654 | if (isset(self::$object_info_cache[$this->nodeId()])) { |
||
655 | return self::$object_info_cache[$this->nodeId()][self::CACHE_COMMENT]; |
||
656 | } |
||
1142 | daniel-mar | 657 | return null; |
975 | daniel-mar | 658 | } |
659 | } |
||
660 | |||
1116 | daniel-mar | 661 | /** |
1142 | daniel-mar | 662 | * @return string|null |
1116 | daniel-mar | 663 | * @throws OIDplusException |
664 | */ |
||
975 | daniel-mar | 665 | public function getCreatedTime() { |
666 | if (!OIDplus::baseConfig()->getValue('OBJECT_CACHING', true)) { |
||
667 | $res = OIDplus::db()->query("select created from ###objects where id = ?", array($this->nodeId())); |
||
668 | if (!$res->any()) return null; |
||
669 | $row = $res->fetch_array(); |
||
670 | return $row['created']; |
||
671 | } else { |
||
672 | self::buildObjectInformationCache(); |
||
673 | if (isset(self::$object_info_cache[$this->nodeId()])) { |
||
674 | return self::$object_info_cache[$this->nodeId()][self::CACHE_CREATED]; |
||
675 | } |
||
1142 | daniel-mar | 676 | return null; |
975 | daniel-mar | 677 | } |
678 | } |
||
679 | |||
1116 | daniel-mar | 680 | /** |
1142 | daniel-mar | 681 | * @return string|null |
1116 | daniel-mar | 682 | * @throws OIDplusException |
683 | */ |
||
975 | daniel-mar | 684 | public function getUpdatedTime() { |
685 | if (!OIDplus::baseConfig()->getValue('OBJECT_CACHING', true)) { |
||
686 | $res = OIDplus::db()->query("select updated from ###objects where id = ?", array($this->nodeId())); |
||
687 | if (!$res->any()) return null; |
||
688 | $row = $res->fetch_array(); |
||
689 | return $row['updated']; |
||
690 | } else { |
||
691 | self::buildObjectInformationCache(); |
||
692 | if (isset(self::$object_info_cache[$this->nodeId()])) { |
||
693 | return self::$object_info_cache[$this->nodeId()][self::CACHE_UPDATED]; |
||
694 | } |
||
1142 | daniel-mar | 695 | return null; |
975 | daniel-mar | 696 | } |
697 | } |
||
698 | |||
1116 | daniel-mar | 699 | /** |
700 | * @param OIDplusRA|string|null $ra |
||
701 | * @return bool |
||
702 | * @throws OIDplusException |
||
703 | */ |
||
1130 | daniel-mar | 704 | public function userHasParentalWriteRights($ra=null): bool { |
1116 | daniel-mar | 705 | if ($ra instanceof OIDplusRA) $ra = $ra->raEmail(); |
115 | daniel-mar | 706 | |
1116 | daniel-mar | 707 | if (!$ra) { |
549 | daniel-mar | 708 | if (OIDplus::authUtils()->isAdminLoggedIn()) return true; |
2 | daniel-mar | 709 | } |
710 | |||
711 | $objParent = $this->getParent(); |
||
419 | daniel-mar | 712 | if (!$objParent) return false; |
1116 | daniel-mar | 713 | return $objParent->userHasWriteRights($ra); |
2 | daniel-mar | 714 | } |
715 | |||
1116 | daniel-mar | 716 | /** |
717 | * @param OIDplusRA|string|null $ra |
||
718 | * @return bool |
||
719 | * @throws OIDplusException |
||
720 | */ |
||
721 | public function userHasWriteRights($ra=null): bool { |
||
722 | if ($ra instanceof OIDplusRA) $ra = $ra->raEmail(); |
||
115 | daniel-mar | 723 | |
1116 | daniel-mar | 724 | if (!$ra) { |
549 | daniel-mar | 725 | if (OIDplus::authUtils()->isAdminLoggedIn()) return true; |
1269 | daniel-mar | 726 | // TODO: should we allow that the parent RA also may update title/description about this OID (since they delegated it?) |
1137 | daniel-mar | 727 | $ownRa = $this->getRaMail(); |
728 | return $ownRa && OIDplus::authUtils()->isRaLoggedIn($ownRa); |
||
2 | daniel-mar | 729 | } else { |
1116 | daniel-mar | 730 | return $this->getRaMail() == $ra; |
2 | daniel-mar | 731 | } |
732 | } |
||
12 | daniel-mar | 733 | |
1116 | daniel-mar | 734 | /** |
735 | * @param string|OIDplusObject $to |
||
736 | * @return int|null |
||
737 | */ |
||
738 | public function distance($to)/*: ?int*/ { |
||
12 | daniel-mar | 739 | return null; // not implemented |
740 | } |
||
20 | daniel-mar | 741 | |
1116 | daniel-mar | 742 | /** |
1130 | daniel-mar | 743 | * @param OIDplusObject|string $obj |
1116 | daniel-mar | 744 | * @return bool |
745 | */ |
||
1130 | daniel-mar | 746 | public function equals($obj): bool { |
1121 | daniel-mar | 747 | if (!$obj) return false; |
20 | daniel-mar | 748 | if (!is_object($obj)) $obj = OIDplusObject::parse($obj); |
1121 | daniel-mar | 749 | if (!$obj) return false; |
28 | daniel-mar | 750 | if (!($obj instanceof $this)) return false; |
751 | |||
20 | daniel-mar | 752 | $distance = $this->distance($obj); |
753 | if (is_numeric($distance)) return $distance === 0; // if the distance function is implemented, use it |
||
754 | |||
755 | return $this->nodeId() == $obj->nodeId(); // otherwise compare the node id case-sensitive |
||
756 | } |
||
757 | |||
1116 | daniel-mar | 758 | /** |
759 | * @param string $id |
||
760 | * @return OIDplusObject|false |
||
761 | * @throws OIDplusException |
||
762 | */ |
||
977 | daniel-mar | 763 | public static function findFitting(string $id) { |
20 | daniel-mar | 764 | $obj = OIDplusObject::parse($id); |
969 | daniel-mar | 765 | if (!$obj) return false; // e.g. if ObjectType plugin is disabled |
20 | daniel-mar | 766 | |
261 | daniel-mar | 767 | if (!OIDplus::baseConfig()->getValue('OBJECT_CACHING', true)) { |
768 | $res = OIDplus::db()->query("select id from ###objects where id like ?", array($obj->ns().':%')); |
||
236 | daniel-mar | 769 | while ($row = $res->fetch_object()) { |
150 | daniel-mar | 770 | $test = OIDplusObject::parse($row->id); |
771 | if ($obj->equals($test)) return $test; |
||
772 | } |
||
773 | return false; |
||
774 | } else { |
||
775 | self::buildObjectInformationCache(); |
||
975 | daniel-mar | 776 | foreach (self::$object_info_cache as $id => $cacheitem) { |
150 | daniel-mar | 777 | if (strpos($id, $obj->ns().':') === 0) { |
778 | $test = OIDplusObject::parse($id); |
||
779 | if ($obj->equals($test)) return $test; |
||
780 | } |
||
781 | } |
||
782 | return false; |
||
20 | daniel-mar | 783 | } |
784 | } |
||
785 | |||
1116 | daniel-mar | 786 | /** |
787 | * @return OIDplusObject|null |
||
788 | */ |
||
789 | public function one_up()/*: ?OIDplusObject*/ { |
||
20 | daniel-mar | 790 | return null; // not implemented |
791 | } |
||
150 | daniel-mar | 792 | |
793 | // Caching stuff |
||
794 | |||
795 | protected static $object_info_cache = null; |
||
796 | |||
1116 | daniel-mar | 797 | /** |
798 | * @return void |
||
799 | */ |
||
150 | daniel-mar | 800 | public static function resetObjectInformationCache() { |
801 | self::$object_info_cache = null; |
||
802 | } |
||
803 | |||
975 | daniel-mar | 804 | const CACHE_ID = 'id'; |
805 | const CACHE_PARENT = 'parent'; |
||
806 | const CACHE_TITLE = 'title'; |
||
807 | const CACHE_DESCRIPTION = 'description'; |
||
808 | const CACHE_RA_EMAIL = 'ra_email'; |
||
809 | const CACHE_CONFIDENTIAL = 'confidential'; |
||
810 | const CACHE_CREATED = 'created'; |
||
811 | const CACHE_UPDATED = 'updated'; |
||
812 | const CACHE_COMMENT = 'comment'; |
||
150 | daniel-mar | 813 | |
1116 | daniel-mar | 814 | /** |
815 | * @return void |
||
816 | * @throws OIDplusException |
||
817 | */ |
||
150 | daniel-mar | 818 | private static function buildObjectInformationCache() { |
819 | if (is_null(self::$object_info_cache)) { |
||
820 | self::$object_info_cache = array(); |
||
975 | daniel-mar | 821 | $res = OIDplus::db()->query("select * from ###objects"); |
236 | daniel-mar | 822 | while ($row = $res->fetch_array()) { |
975 | daniel-mar | 823 | self::$object_info_cache[$row['id']] = $row; |
150 | daniel-mar | 824 | } |
825 | } |
||
826 | } |
||
513 | daniel-mar | 827 | |
1116 | daniel-mar | 828 | /** |
829 | * override this function if you want your object type to save |
||
830 | * attachments in directories with easy names. |
||
831 | * Take care that your custom directory name will not allow jailbreaks (../) ! |
||
832 | * @return string |
||
833 | * @throws OIDplusException |
||
834 | */ |
||
835 | public function getDirectoryName(): string { |
||
514 | daniel-mar | 836 | if ($this->isRoot()) return $this->ns(); |
837 | return $this->getLegacyDirectoryName(); |
||
513 | daniel-mar | 838 | } |
839 | |||
1116 | daniel-mar | 840 | /** |
841 | * @return string |
||
842 | * @throws OIDplusException |
||
843 | */ |
||
844 | public final function getLegacyDirectoryName(): string { |
||
804 | daniel-mar | 845 | if ($this::ns() == 'oid') { |
513 | daniel-mar | 846 | $oid = $this->nodeId(false); |
847 | } else { |
||
848 | $oid = null; |
||
849 | $alt_ids = $this->getAltIds(); |
||
850 | foreach ($alt_ids as $alt_id) { |
||
851 | if ($alt_id->getNamespace() == 'oid') { |
||
852 | $oid = $alt_id->getId(); |
||
853 | break; // we prefer the first OID (for GUIDs, the first OID is the OIDplus-OID, and the second OID is the UUID OID) |
||
854 | } |
||
855 | } |
||
856 | } |
||
857 | |||
858 | if (!is_null($oid) && ($oid != '')) { |
||
859 | // For OIDs, it is the OID, for other identifiers |
||
860 | // it it the OID alt ID (generated using the SystemID) |
||
861 | return str_replace('.', '_', $oid); |
||
862 | } else { |
||
863 | // Can happen if you don't have a system ID (due to missing OpenSSL plugin) |
||
864 | return md5($this->nodeId(true)); // we don't use $id, because $this->nodeId(true) is possibly more canonical than $id |
||
865 | } |
||
866 | } |
||
800 | daniel-mar | 867 | |
1116 | daniel-mar | 868 | /** |
869 | * @param string $mode |
||
870 | * @return string |
||
871 | */ |
||
872 | public static function treeIconFilename(string $mode): string { |
||
800 | daniel-mar | 873 | // for backwards-compatibility with older plugins |
874 | return 'img/treeicon_'.$mode.'.png'; |
||
875 | } |
||
876 | |||
415 | daniel-mar | 877 | } |