Subversion Repositories oidplus

Rev

Rev 251 | Rev 277 | 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
5
 * Copyright 2019 Daniel Marschall, ViaThinkSoft
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
 
112 daniel-mar 20
if (!defined('IN_OIDPLUS')) die();
21
 
261 daniel-mar 22
abstract class OIDplusObject {
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)) {
42
                                $oid = $sid . '.' . smallhash($this->nodeId());
228 daniel-mar 43
                                $ids[] = new OIDplusAltId('oid', $oid, 'OIDplus Information Object ID');
193 daniel-mar 44
                        }
45
                }
46
                if ($this->ns() != 'guid') {
261 daniel-mar 47
                        $ids[] = new OIDplusAltId('guid', gen_uuid_md5_namebased(self::UUID_NAMEBASED_NS_OidPlusMisc, $this->nodeId()), 'Namebased version 3 / MD5 UUID with namespace UUID_NAMEBASED_NS_OidPlusMisc');
48
                        $ids[] = new OIDplusAltId('guid', gen_uuid_sha1_namebased(self::UUID_NAMEBASED_NS_OidPlusMisc, $this->nodeId()), 'Namebased version 5 / SHA1 UUID with namespace UUID_NAMEBASED_NS_OidPlusMisc');
193 daniel-mar 49
                }
50
                return $ids;
83 daniel-mar 51
        }
52
 
2 daniel-mar 53
        public abstract static function objectTypeTitle();
54
 
55
        public abstract static function objectTypeTitleShort();
56
 
57
        public abstract static function ns();
58
 
59
        public abstract static function root();
60
 
61
        public abstract function isRoot();
62
 
247 daniel-mar 63
        public abstract function nodeId($with_ns=true);
2 daniel-mar 64
 
65
        public abstract function addString($str);
66
 
67
        public abstract function crudShowId(OIDplusObject $parent);
68
 
69
        public abstract function crudInsertPrefix();
70
 
71
        public abstract function jsTreeNodeName(OIDplusObject $parent = null);
72
 
73
        public abstract function defaultTitle();
74
 
16 daniel-mar 75
        public abstract function isLeafNode();
76
 
34 daniel-mar 77
        public abstract function getContentPage(&$title, &$content, &$icon);
2 daniel-mar 78
 
27 daniel-mar 79
        public static function getRaRoots($ra_email=null) {
115 daniel-mar 80
                if ($ra_email instanceof OIDplusRA) $ra_email = $ra_email->raEmail();
81
 
27 daniel-mar 82
                $out = array();
150 daniel-mar 83
 
261 daniel-mar 84
                if (!OIDplus::baseConfig()->getValue('OBJECT_CACHING', true)) {
150 daniel-mar 85
                        if (is_null($ra_email)) {
261 daniel-mar 86
                                $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 ".
87
                                                            "left join ###objects as oParent on oChild.parent = oParent.id ".
150 daniel-mar 88
                                                            "order by ".OIDplus::db()->natOrder('oChild.id'));
236 daniel-mar 89
                                while ($row = $res->fetch_array()) {
150 daniel-mar 90
                                        if (!OIDplus::authUtils()::isRaLoggedIn($row['parent_mail']) && OIDplus::authUtils()::isRaLoggedIn($row['child_mail'])) {
91
                                                $x = self::parse($row['id']); // can be FALSE if namespace was disabled
92
                                                if ($x) $out[] = $x;
93
                                        }
94
                                }
95
                        } else {
261 daniel-mar 96
                                $res = OIDplus::db()->query("select oChild.id as id from ###objects as oChild ".
97
                                                            "left join ###objects as oParent on oChild.parent = oParent.id ".
150 daniel-mar 98
                                                            "where (ifnull(oParent.ra_email,'') <> ? and ifnull(oChild.ra_email,'') = ?) or ".
99
                                                            "      (oParent.ra_email is null and ifnull(oChild.ra_email,'') = ?) ".
100
                                                            "order by ".OIDplus::db()->natOrder('oChild.id'), array($ra_email, $ra_email, $ra_email));
236 daniel-mar 101
                                while ($row = $res->fetch_array()) {
68 daniel-mar 102
                                        $x = self::parse($row['id']); // can be FALSE if namespace was disabled
150 daniel-mar 103
                                        if ($x) $out[] = self::parse($row['id']);
27 daniel-mar 104
                                }
2 daniel-mar 105
                        }
106
                } else {
150 daniel-mar 107
                        if (is_null($ra_email)) {
108
                                $ra_mails_to_check = OIDplusAuthUtils::loggedInRaList();
109
                                if (count($ra_mails_to_check) == 0) return $out;
110
                        } else {
111
                                $ra_mails_to_check = array($ra_email);
2 daniel-mar 112
                        }
150 daniel-mar 113
 
114
                        self::buildObjectInformationCache();
115
 
116
                        foreach ($ra_mails_to_check as $check_ra_mail) {
193 daniel-mar 117
                                $out_part = array();
150 daniel-mar 118
 
193 daniel-mar 119
                                foreach (self::$object_info_cache as $id => list($confidential, $parent, $ra_email, $title)) {
120
                                        // If the OID RA is the RA we are searching, then add the object to the choice list
121
                                        if ($ra_email == $check_ra_mail) $out_part[] = $id;
150 daniel-mar 122
                                }
123
 
193 daniel-mar 124
                                foreach (self::$object_info_cache as $id => list($confidential, $parent, $ra_email, $title)) {
125
                                        if (isset(self::$object_info_cache[$parent])) {
126
                                                if (self::$object_info_cache[$parent][self::CACHE_RA_EMAIL] == $ra_email) {
127
                                                        // if the parent has the same RA, then this OID cannot be a root => remove the element from the choice list
128
                                                        foreach (array_keys($out_part, $id) as $key) unset($out_part[$key]);
150 daniel-mar 129
                                                }
130
                                        }
131
                                }
132
 
133
                                natsort($out_part);
134
 
135
                                foreach ($out_part as $id) {
136
                                        $obj = self::parse($id);
137
                                        if ($obj) $out[] = $obj;
138
                                }
139
                        }
2 daniel-mar 140
                }
150 daniel-mar 141
 
2 daniel-mar 142
                return $out;
143
        }
144
 
150 daniel-mar 145
        public static function getAllNonConfidential() {
146
                $out = array();
147
 
261 daniel-mar 148
                if (!OIDplus::baseConfig()->getValue('OBJECT_CACHING', true)) {
149
                        $res = OIDplus::db()->query("select id from ###objects where confidential = '0' order by ".OIDplus::db()->natOrder('id'));
150 daniel-mar 150
 
236 daniel-mar 151
                        while ($row = $res->fetch_array()) {
150 daniel-mar 152
                                $obj = self::parse($row['id']); // will be NULL if the object type is not registered
169 daniel-mar 153
                                if ($obj && (!$obj->isConfidential())) {
150 daniel-mar 154
                                        $out[] = $row['id'];
155
                                }
2 daniel-mar 156
                        }
157
                } else {
150 daniel-mar 158
                        self::buildObjectInformationCache();
2 daniel-mar 159
 
193 daniel-mar 160
                        foreach (self::$object_info_cache as $id => list($confidential, $parent, $ra_email, $title)) {
150 daniel-mar 161
                                if (!$confidential) {
162
                                        $obj = self::parse($id); // will be NULL if the object type is not registered
169 daniel-mar 163
                                        if ($obj && (!$obj->isConfidential())) {
150 daniel-mar 164
                                                $out[] = $id;
165
                                        }
166
                                }
2 daniel-mar 167
                        }
168
                }
169
 
170
                return $out;
171
        }
172
 
173
        public function isConfidential() {
261 daniel-mar 174
                if (!OIDplus::baseConfig()->getValue('OBJECT_CACHING', true)) {
150 daniel-mar 175
                        $curid = $this->nodeId();
176
                        $orig_curid = $curid;
177
                        if (isset(self::$object_info_cache[$curid])) return self::$object_info_cache[$curid];
178
                        // Recursively search for the confidential flag in the parents
261 daniel-mar 179
                        while (($res = OIDplus::db()->query("select parent, confidential from ###objects where id = ?", array($curid)))->num_rows() > 0) {
236 daniel-mar 180
                                $row = $res->fetch_array();
150 daniel-mar 181
                                if ($row['confidential']) {
182
                                        self::$object_info_cache[$curid] = true;
183
                                        self::$object_info_cache[$orig_curid] = true;
184
                                        return true;
185
                                } else {
186
                                        self::$object_info_cache[$curid] = false;
187
                                }
188
                                $curid = $row['parent'];
189
                                if (isset(self::$object_info_cache[$curid])) {
190
                                        self::$object_info_cache[$orig_curid] = self::$object_info_cache[$curid];
191
                                        return self::$object_info_cache[$curid];
192
                                }
193
                        }
194
 
195
                        self::$object_info_cache[$orig_curid] = false;
196
                        return false;
197
                } else {
198
                        self::buildObjectInformationCache();
199
 
200
                        $curid = $this->nodeId();
201
                        // Recursively search for the confidential flag in the parents
169 daniel-mar 202
                        while (isset(self::$object_info_cache[$curid])) {
150 daniel-mar 203
                                if (self::$object_info_cache[$curid][self::CACHE_CONFIDENTIAL]) return true;
204
                                $curid = self::$object_info_cache[$curid][self::CACHE_PARENT];
205
                        }
206
                        return false;
2 daniel-mar 207
                }
208
        }
209
 
210
        public function isChildOf(OIDplusObject $obj) {
261 daniel-mar 211
                if (!OIDplus::baseConfig()->getValue('OBJECT_CACHING', true)) {
150 daniel-mar 212
                        $curid = $this->nodeId();
261 daniel-mar 213
                        while (($res = OIDplus::db()->query("select parent from ###objects where id = ?", array($curid)))->num_rows() > 0) {
236 daniel-mar 214
                                $row = $res->fetch_array();
150 daniel-mar 215
                                if ($curid == $obj->nodeId()) return true;
216
                                $curid = $row['parent'];
217
                        }
218
                        return false;
219
                } else {
220
                        self::buildObjectInformationCache();
221
 
222
                        $curid = $this->nodeId();
169 daniel-mar 223
                        while (isset(self::$object_info_cache[$curid])) {
150 daniel-mar 224
                                if ($curid == $obj->nodeId()) return true;
225
                                $curid = self::$object_info_cache[$curid][self::CACHE_PARENT];
226
                        }
227
                        return false;
2 daniel-mar 228
                }
150 daniel-mar 229
        }
2 daniel-mar 230
 
150 daniel-mar 231
        public function getChildren() {
232
                $out = array();
261 daniel-mar 233
                if (!OIDplus::baseConfig()->getValue('OBJECT_CACHING', true)) {
234
                        $res = OIDplus::db()->query("select id from ###objects where parent = ?", array($this->nodeId()));
236 daniel-mar 235
                        while ($row = $res->fetch_array()) {
150 daniel-mar 236
                                $obj = self::parse($row['id']);
237
                                if (!$obj) continue;
238
                                $out[] = $obj;
239
                        }
240
                } else {
241
                        self::buildObjectInformationCache();
242
 
193 daniel-mar 243
                        foreach (self::$object_info_cache as $id => list($confidential, $parent, $ra_email, $title)) {
150 daniel-mar 244
                                if ($parent == $this->nodeId()) {
245
                                        $obj = self::parse($id);
246
                                        if (!$obj) continue;
247
                                        $out[] = $obj;
248
                                }
249
                        }
250
                }
251
                return $out;
2 daniel-mar 252
        }
253
 
115 daniel-mar 254
        public function getRa() {
150 daniel-mar 255
                return new OIDplusRA($this->getRaMail());
115 daniel-mar 256
        }
257
 
2 daniel-mar 258
        public function userHasReadRights($ra_email=null) {
115 daniel-mar 259
                if ($ra_email instanceof OIDplusRA) $ra_email = $ra_email->raEmail();
260
 
2 daniel-mar 261
                // Admin may do everything
262
                if (OIDplus::authUtils()::isAdminLoggedIn()) return true;
263
 
264
                // If it is not confidential, everybody can read/see it.
265
                if (!$this->isConfidential()) return true;
266
 
267
                // If we own the object, we may see it
268
                if (is_null($ra_email)) {
269
                        if ($this->userHasWriteRights()) return true;
270
                } else {
150 daniel-mar 271
                        if ($this->getRaMail() == $ra_email) return true;
2 daniel-mar 272
                }
273
 
274
                // If someone has rights to an object below our confidential node,
275
                // we let him see the confidential node,
276
                // Otherwise he could not browse through to his own node.
277
                $roots = $this->getRaRoots($ra_email);
278
                foreach ($roots as $root) {
279
                        if ($root->isChildOf($this)) return true;
280
                }
281
 
282
                return false;
283
        }
284
 
285
        public function getIcon($row=null) {
20 daniel-mar 286
                $namespace = $this->ns(); // must use $this, not self::, otherwise the virtual method will not be called
2 daniel-mar 287
 
288
                if (is_null($row)) {
150 daniel-mar 289
                        $ra_email = $this->getRaMail();
290
                } else {
291
                        $ra_email = $row['ra_email'];
2 daniel-mar 292
                }
293
                // TODO: have different icons for Leaf-Nodes
150 daniel-mar 294
                if (OIDplus::authUtils()::isRaLoggedIn($ra_email)) {
2 daniel-mar 295
                        $icon = 'plugins/objectTypes/'.$namespace.'/img/treeicon_own.png';
296
                } else {
297
                        $icon = 'plugins/objectTypes/'.$namespace.'/img/treeicon_general.png';
298
                }
299
                if (!file_exists($icon)) $icon = null; // default icon (folder)
300
                return $icon;
301
        }
302
 
12 daniel-mar 303
        public static function exists($id) {
261 daniel-mar 304
                if (!OIDplus::baseConfig()->getValue('OBJECT_CACHING', true)) {
305
                        $res = OIDplus::db()->query("select id from ###objects where id = ?", array($id));
236 daniel-mar 306
                        return $res->num_rows() > 0;
150 daniel-mar 307
                } else {
308
                        self::buildObjectInformationCache();
309
                        return isset(self::$object_info_cache[$id]);
310
                }
12 daniel-mar 311
        }
312
 
2 daniel-mar 313
        public function getParent() {
261 daniel-mar 314
                if (!OIDplus::baseConfig()->getValue('OBJECT_CACHING', true)) {
315
                        $res = OIDplus::db()->query("select parent from ###objects where id = ?", array($this->nodeId()));
251 daniel-mar 316
                        if ($res->num_rows() == 0) return null;
236 daniel-mar 317
                        $row = $res->fetch_array();
150 daniel-mar 318
                        $parent = $row['parent'];
319
                        $obj = OIDplusObject::parse($parent);
320
                        if ($obj) return $obj;
321
                } else {
322
                        self::buildObjectInformationCache();
323
                        if (isset(self::$object_info_cache[$this->nodeId()])) {
324
                                $parent = self::$object_info_cache[$this->nodeId()][self::CACHE_PARENT];
325
                                $obj = OIDplusObject::parse($parent);
326
                                if ($obj) return $obj;
327
                        }
20 daniel-mar 328
 
150 daniel-mar 329
                        // 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()
330
                        $cur = $this->one_up();
20 daniel-mar 331
                        if (!$cur) return false;
150 daniel-mar 332
                        do {
333
                                if ($fitting = self::findFitting($cur->nodeId())) return $fitting;
20 daniel-mar 334
 
150 daniel-mar 335
                                $prev = $cur;
336
                                $cur = $cur->one_up();
337
                                if (!$cur) return false;
338
                        } while ($prev != $cur);
339
 
340
                        return false;
341
                }
2 daniel-mar 342
        }
343
 
344
        public function getRaMail() {
261 daniel-mar 345
                if (!OIDplus::baseConfig()->getValue('OBJECT_CACHING', true)) {
346
                        $res = OIDplus::db()->query("select ra_email from ###objects where id = ?", array($this->nodeId()));
251 daniel-mar 347
                        if ($res->num_rows() == 0) return null;
236 daniel-mar 348
                        $row = $res->fetch_array();
150 daniel-mar 349
                        return $row['ra_email'];
350
                } else {
351
                        self::buildObjectInformationCache();
352
                        if (isset(self::$object_info_cache[$this->nodeId()])) {
353
                                return self::$object_info_cache[$this->nodeId()][self::CACHE_RA_EMAIL];
354
                        }
355
                        return false;
356
                }
2 daniel-mar 357
        }
358
 
192 daniel-mar 359
        public function getTitle() {
261 daniel-mar 360
                if (!OIDplus::baseConfig()->getValue('OBJECT_CACHING', true)) {
361
                        $res = OIDplus::db()->query("select title from ###objects where id = ?", array($this->nodeId()));
251 daniel-mar 362
                        if ($res->num_rows() == 0) return null;
236 daniel-mar 363
                        $row = $res->fetch_array();
192 daniel-mar 364
                        return $row['title'];
365
                } else {
366
                        self::buildObjectInformationCache();
367
                        if (isset(self::$object_info_cache[$this->nodeId()])) {
368
                                return self::$object_info_cache[$this->nodeId()][self::CACHE_TITLE];
369
                        }
370
                        return false;
371
                }
372
        }
373
 
2 daniel-mar 374
        public function userHasParentalWriteRights($ra_email=null) {
115 daniel-mar 375
                if ($ra_email instanceof OIDplusRA) $ra_email = $ra_email->raEmail();
376
 
2 daniel-mar 377
                if (is_null($ra_email)) {
378
                        if (OIDplus::authUtils()::isAdminLoggedIn()) return true;
379
                }
380
 
381
                $objParent = $this->getParent();
382
                if (is_null($objParent)) return false;
383
                return $objParent->userHasWriteRights($ra_email);
384
        }
385
 
386
        public function userHasWriteRights($ra_email=null) {
115 daniel-mar 387
                if ($ra_email instanceof OIDplusRA) $ra_email = $ra_email->raEmail();
388
 
2 daniel-mar 389
                if (is_null($ra_email)) {
390
                        if (OIDplus::authUtils()::isAdminLoggedIn()) return true;
391
                        return OIDplus::authUtils()::isRaLoggedIn($this->getRaMail());
392
                } else {
393
                        return $this->getRaMail() == $ra_email;
394
                }
395
        }
12 daniel-mar 396
 
397
        public function distance($to) {
398
                return null; // not implemented
399
        }
20 daniel-mar 400
 
401
        public function equals($obj) {
402
                if (!is_object($obj)) $obj = OIDplusObject::parse($obj);
28 daniel-mar 403
                if (!($obj instanceof $this)) return false;
404
 
20 daniel-mar 405
                $distance = $this->distance($obj);
406
                if (is_numeric($distance)) return $distance === 0; // if the distance function is implemented, use it
407
 
408
                return $this->nodeId() == $obj->nodeId(); // otherwise compare the node id case-sensitive
409
        }
410
 
411
        public static function findFitting($id) {
412
                $obj = OIDplusObject::parse($id);
250 daniel-mar 413
                if (!$obj) throw new OIDplusException("findFitting: Parse failed\n");
20 daniel-mar 414
 
261 daniel-mar 415
                if (!OIDplus::baseConfig()->getValue('OBJECT_CACHING', true)) {
416
                        $res = OIDplus::db()->query("select id from ###objects where id like ?", array($obj->ns().':%'));
236 daniel-mar 417
                        while ($row = $res->fetch_object()) {
150 daniel-mar 418
                                $test = OIDplusObject::parse($row->id);
419
                                if ($obj->equals($test)) return $test;
420
                        }
421
                        return false;
422
                } else {
423
                        self::buildObjectInformationCache();
193 daniel-mar 424
                        foreach (self::$object_info_cache as $id => list($confidential, $parent, $ra_email, $title)) {
150 daniel-mar 425
                                if (strpos($id, $obj->ns().':') === 0) {
426
                                        $test = OIDplusObject::parse($id);
427
                                        if ($obj->equals($test)) return $test;
428
                                }
429
                        }
430
                        return false;
20 daniel-mar 431
                }
432
        }
433
 
434
        public function one_up() {
435
                return null; // not implemented
436
        }
150 daniel-mar 437
 
438
        // Caching stuff
439
 
440
        protected static $object_info_cache = null;
441
 
442
        public static function resetObjectInformationCache() {
443
                self::$object_info_cache = null;
444
        }
445
 
193 daniel-mar 446
        const CACHE_CONFIDENTIAL = 0; // TODO: An object would be better so you can use $cacheitem->isConfidential() etc.
150 daniel-mar 447
        const CACHE_PARENT = 1;
448
        const CACHE_RA_EMAIL = 2;
192 daniel-mar 449
        const CACHE_TITLE = 3;
150 daniel-mar 450
 
451
        private static function buildObjectInformationCache() {
452
                if (is_null(self::$object_info_cache)) {
453
                        self::$object_info_cache = array();
261 daniel-mar 454
                        $res = OIDplus::db()->query("select id, parent, confidential, ra_email, title from ###objects");
236 daniel-mar 455
                        while ($row = $res->fetch_array()) {
192 daniel-mar 456
                                self::$object_info_cache[$row['id']] = array($row['confidential'], $row['parent'], $row['ra_email'], $row['title']);
150 daniel-mar 457
                        }
458
                }
459
        }
2 daniel-mar 460
}