Subversion Repositories oidplus

Rev

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