Subversion Repositories oidplus

Rev

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