Subversion Repositories oidplus

Rev

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