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