Subversion Repositories oidplus

Rev

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