Subversion Repositories oidplus

Rev

Rev 433 | Rev 511 | 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
 
261 daniel-mar 20
abstract class OIDplusObject {
21
        const UUID_NAMEBASED_NS_OidPlusMisc = 'ad1654e6-7e15-11e4-9ef6-78e3b5fc7f22';
150 daniel-mar 22
 
2 daniel-mar 23
        public static function parse($node_id) { // please overwrite this function!
24
                // TODO: in case we are not calling this class directly, check if function is overwritten and throw exception otherwise
227 daniel-mar 25
                foreach (OIDplus::getEnabledObjectTypes() as $ot) {
2 daniel-mar 26
                        if ($obj = $ot::parse($node_id)) return $obj;
27
                }
28
                return null;
29
        }
30
 
228 daniel-mar 31
        public function /*OIDplusAltId[]*/ getAltIds() {
193 daniel-mar 32
                if ($this->isRoot()) return array();
33
 
34
                $ids = array();
35
                if ($this->ns() != 'oid') {
36
                        // Creates an OIDplus-Hash-OID
37
                        // If the object type has a better way of defining an OID, please override this method!
227 daniel-mar 38
                        $sid = OIDplus::getSystemId(true);
193 daniel-mar 39
                        if (!empty($sid)) {
40
                                $oid = $sid . '.' . smallhash($this->nodeId());
360 daniel-mar 41
                                $ids[] = new OIDplusAltId('oid', $oid, _L('OIDplus Information Object ID'));
193 daniel-mar 42
                        }
43
                }
44
                if ($this->ns() != 'guid') {
360 daniel-mar 45
                        $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'));
46
                        $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 47
                }
48
                return $ids;
83 daniel-mar 49
        }
50
 
2 daniel-mar 51
        public abstract static function objectTypeTitle();
52
 
53
        public abstract static function objectTypeTitleShort();
54
 
55
        public abstract static function ns();
56
 
57
        public abstract static function root();
58
 
59
        public abstract function isRoot();
60
 
247 daniel-mar 61
        public abstract function nodeId($with_ns=true);
2 daniel-mar 62
 
63
        public abstract function addString($str);
64
 
65
        public abstract function crudShowId(OIDplusObject $parent);
66
 
67
        public abstract function crudInsertPrefix();
68
 
69
        public abstract function jsTreeNodeName(OIDplusObject $parent = null);
70
 
71
        public abstract function defaultTitle();
72
 
16 daniel-mar 73
        public abstract function isLeafNode();
74
 
34 daniel-mar 75
        public abstract function getContentPage(&$title, &$content, &$icon);
2 daniel-mar 76
 
27 daniel-mar 77
        public static function getRaRoots($ra_email=null) {
115 daniel-mar 78
                if ($ra_email instanceof OIDplusRA) $ra_email = $ra_email->raEmail();
79
 
27 daniel-mar 80
                $out = array();
150 daniel-mar 81
 
261 daniel-mar 82
                if (!OIDplus::baseConfig()->getValue('OBJECT_CACHING', true)) {
150 daniel-mar 83
                        if (is_null($ra_email)) {
261 daniel-mar 84
                                $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 ".
85
                                                            "left join ###objects as oParent on oChild.parent = oParent.id ".
150 daniel-mar 86
                                                            "order by ".OIDplus::db()->natOrder('oChild.id'));
236 daniel-mar 87
                                while ($row = $res->fetch_array()) {
150 daniel-mar 88
                                        if (!OIDplus::authUtils()::isRaLoggedIn($row['parent_mail']) && OIDplus::authUtils()::isRaLoggedIn($row['child_mail'])) {
89
                                                $x = self::parse($row['id']); // can be FALSE if namespace was disabled
90
                                                if ($x) $out[] = $x;
91
                                        }
92
                                }
93
                        } else {
261 daniel-mar 94
                                $res = OIDplus::db()->query("select oChild.id as id from ###objects as oChild ".
95
                                                            "left join ###objects as oParent on oChild.parent = oParent.id ".
433 daniel-mar 96
                                                            "where (".OIDplus::db()->getSlang()->isNullFunction('oParent.ra_email',"''")." <> ? and ".
97
                                                            OIDplus::db()->getSlang()->isNullFunction('oChild.ra_email',"''")." = ?) or ".
98
                                                            "      (oParent.ra_email is null and ".OIDplus::db()->getSlang()->isNullFunction('oChild.ra_email',"''")." = ?) ".
150 daniel-mar 99
                                                            "order by ".OIDplus::db()->natOrder('oChild.id'), array($ra_email, $ra_email, $ra_email));
236 daniel-mar 100
                                while ($row = $res->fetch_array()) {
68 daniel-mar 101
                                        $x = self::parse($row['id']); // can be FALSE if namespace was disabled
150 daniel-mar 102
                                        if ($x) $out[] = self::parse($row['id']);
27 daniel-mar 103
                                }
2 daniel-mar 104
                        }
105
                } else {
150 daniel-mar 106
                        if (is_null($ra_email)) {
415 daniel-mar 107
                                $ra_mails_to_check = OIDplus::authUtils()->loggedInRaList();
150 daniel-mar 108
                                if (count($ra_mails_to_check) == 0) return $out;
109
                        } else {
110
                                $ra_mails_to_check = array($ra_email);
2 daniel-mar 111
                        }
150 daniel-mar 112
 
113
                        self::buildObjectInformationCache();
114
 
115
                        foreach ($ra_mails_to_check as $check_ra_mail) {
193 daniel-mar 116
                                $out_part = array();
150 daniel-mar 117
 
193 daniel-mar 118
                                foreach (self::$object_info_cache as $id => list($confidential, $parent, $ra_email, $title)) {
119
                                        // If the OID RA is the RA we are searching, then add the object to the choice list
120
                                        if ($ra_email == $check_ra_mail) $out_part[] = $id;
150 daniel-mar 121
                                }
122
 
193 daniel-mar 123
                                foreach (self::$object_info_cache as $id => list($confidential, $parent, $ra_email, $title)) {
124
                                        if (isset(self::$object_info_cache[$parent])) {
125
                                                if (self::$object_info_cache[$parent][self::CACHE_RA_EMAIL] == $ra_email) {
126
                                                        // if the parent has the same RA, then this OID cannot be a root => remove the element from the choice list
127
                                                        foreach (array_keys($out_part, $id) as $key) unset($out_part[$key]);
150 daniel-mar 128
                                                }
129
                                        }
130
                                }
131
 
132
                                natsort($out_part);
133
 
134
                                foreach ($out_part as $id) {
135
                                        $obj = self::parse($id);
136
                                        if ($obj) $out[] = $obj;
137
                                }
138
                        }
2 daniel-mar 139
                }
150 daniel-mar 140
 
2 daniel-mar 141
                return $out;
142
        }
143
 
150 daniel-mar 144
        public static function getAllNonConfidential() {
145
                $out = array();
146
 
261 daniel-mar 147
                if (!OIDplus::baseConfig()->getValue('OBJECT_CACHING', true)) {
443 daniel-mar 148
                        $res = OIDplus::db()->query("select id from ###objects where confidential = ? order by ".OIDplus::db()->natOrder('id'), array(false));
150 daniel-mar 149
 
236 daniel-mar 150
                        while ($row = $res->fetch_array()) {
150 daniel-mar 151
                                $obj = self::parse($row['id']); // will be NULL if the object type is not registered
169 daniel-mar 152
                                if ($obj && (!$obj->isConfidential())) {
150 daniel-mar 153
                                        $out[] = $row['id'];
154
                                }
2 daniel-mar 155
                        }
156
                } else {
150 daniel-mar 157
                        self::buildObjectInformationCache();
2 daniel-mar 158
 
193 daniel-mar 159
                        foreach (self::$object_info_cache as $id => list($confidential, $parent, $ra_email, $title)) {
150 daniel-mar 160
                                if (!$confidential) {
161
                                        $obj = self::parse($id); // will be NULL if the object type is not registered
169 daniel-mar 162
                                        if ($obj && (!$obj->isConfidential())) {
150 daniel-mar 163
                                                $out[] = $id;
164
                                        }
165
                                }
2 daniel-mar 166
                        }
167
                }
168
 
169
                return $out;
170
        }
171
 
172
        public function isConfidential() {
261 daniel-mar 173
                if (!OIDplus::baseConfig()->getValue('OBJECT_CACHING', true)) {
426 daniel-mar 174
                        //static $confidential_cache = array();
150 daniel-mar 175
                        $curid = $this->nodeId();
426 daniel-mar 176
                        //$orig_curid = $curid;
177
                        //if (isset($confidential_cache[$curid])) return $confidential_cache[$curid];
150 daniel-mar 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']) {
426 daniel-mar 182
                                        //$confidential_cache[$curid] = true;
183
                                        //$confidential_cache[$orig_curid] = true;
150 daniel-mar 184
                                        return true;
185
                                } else {
426 daniel-mar 186
                                        //$confidential_cache[$curid] = false;
150 daniel-mar 187
                                }
188
                                $curid = $row['parent'];
426 daniel-mar 189
                                //if (isset($confidential_cache[$curid])) {
190
                                        //$confidential_cache[$orig_curid] = $confidential_cache[$curid];
191
                                        //return $confidential_cache[$curid];
192
                                //}
150 daniel-mar 193
                        }
194
 
426 daniel-mar 195
                        //$confidential_cache[$orig_curid] = false;
150 daniel-mar 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
                // If it is not confidential, everybody can read/see it.
416 daniel-mar 262
                // Note: This also checks if superior OIDs are confidential.
2 daniel-mar 263
                if (!$this->isConfidential()) return true;
264
 
265
                if (is_null($ra_email)) {
416 daniel-mar 266
                        // Admin may do everything
267
                        if (OIDplus::authUtils()::isAdminLoggedIn()) return true;
268
 
269
                        // If the RA is logged in, then they can see the OID.
270
                        if (OIDplus::authUtils()::isRaLoggedIn($this->getRaMail())) return true;
2 daniel-mar 271
                } else {
416 daniel-mar 272
                        // If this OID belongs to the requested RA, then they may see it.
150 daniel-mar 273
                        if ($this->getRaMail() == $ra_email) return true;
2 daniel-mar 274
                }
275
 
276
                // If someone has rights to an object below our confidential node,
277
                // we let him see the confidential node,
278
                // Otherwise he could not browse through to his own node.
279
                $roots = $this->getRaRoots($ra_email);
280
                foreach ($roots as $root) {
281
                        if ($root->isChildOf($this)) return true;
282
                }
283
 
284
                return false;
285
        }
286
 
287
        public function getIcon($row=null) {
20 daniel-mar 288
                $namespace = $this->ns(); // must use $this, not self::, otherwise the virtual method will not be called
2 daniel-mar 289
 
290
                if (is_null($row)) {
150 daniel-mar 291
                        $ra_email = $this->getRaMail();
292
                } else {
293
                        $ra_email = $row['ra_email'];
2 daniel-mar 294
                }
295
                // TODO: have different icons for Leaf-Nodes
150 daniel-mar 296
                if (OIDplus::authUtils()::isRaLoggedIn($ra_email)) {
2 daniel-mar 297
                        $icon = 'plugins/objectTypes/'.$namespace.'/img/treeicon_own.png';
298
                } else {
299
                        $icon = 'plugins/objectTypes/'.$namespace.'/img/treeicon_general.png';
300
                }
301
                if (!file_exists($icon)) $icon = null; // default icon (folder)
302
                return $icon;
303
        }
304
 
12 daniel-mar 305
        public static function exists($id) {
261 daniel-mar 306
                if (!OIDplus::baseConfig()->getValue('OBJECT_CACHING', true)) {
307
                        $res = OIDplus::db()->query("select id from ###objects where id = ?", array($id));
236 daniel-mar 308
                        return $res->num_rows() > 0;
150 daniel-mar 309
                } else {
310
                        self::buildObjectInformationCache();
311
                        return isset(self::$object_info_cache[$id]);
312
                }
12 daniel-mar 313
        }
314
 
415 daniel-mar 315
        // Get parent gives the next possible parent which is EXISTING in OIDplus
316
        // It does not give the immediate parent
2 daniel-mar 317
        public function getParent() {
261 daniel-mar 318
                if (!OIDplus::baseConfig()->getValue('OBJECT_CACHING', true)) {
319
                        $res = OIDplus::db()->query("select parent from ###objects where id = ?", array($this->nodeId()));
251 daniel-mar 320
                        if ($res->num_rows() == 0) return null;
236 daniel-mar 321
                        $row = $res->fetch_array();
150 daniel-mar 322
                        $parent = $row['parent'];
323
                        $obj = OIDplusObject::parse($parent);
324
                        if ($obj) return $obj;
415 daniel-mar 325
                        // TODO: Also implement one_up() like below
150 daniel-mar 326
                } else {
327
                        self::buildObjectInformationCache();
328
                        if (isset(self::$object_info_cache[$this->nodeId()])) {
329
                                $parent = self::$object_info_cache[$this->nodeId()][self::CACHE_PARENT];
330
                                $obj = OIDplusObject::parse($parent);
331
                                if ($obj) return $obj;
332
                        }
20 daniel-mar 333
 
150 daniel-mar 334
                        // 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()
335
                        $cur = $this->one_up();
418 daniel-mar 336
                        if (!$cur) return null;
150 daniel-mar 337
                        do {
415 daniel-mar 338
                                // findFitting() checks if that OID exists
150 daniel-mar 339
                                if ($fitting = self::findFitting($cur->nodeId())) return $fitting;
20 daniel-mar 340
 
150 daniel-mar 341
                                $prev = $cur;
342
                                $cur = $cur->one_up();
418 daniel-mar 343
                                if (!$cur) return null;
150 daniel-mar 344
                        } while ($prev != $cur);
345
 
418 daniel-mar 346
                        return null;
150 daniel-mar 347
                }
2 daniel-mar 348
        }
349
 
350
        public function getRaMail() {
261 daniel-mar 351
                if (!OIDplus::baseConfig()->getValue('OBJECT_CACHING', true)) {
352
                        $res = OIDplus::db()->query("select ra_email from ###objects where id = ?", array($this->nodeId()));
251 daniel-mar 353
                        if ($res->num_rows() == 0) return null;
236 daniel-mar 354
                        $row = $res->fetch_array();
150 daniel-mar 355
                        return $row['ra_email'];
356
                } else {
357
                        self::buildObjectInformationCache();
358
                        if (isset(self::$object_info_cache[$this->nodeId()])) {
359
                                return self::$object_info_cache[$this->nodeId()][self::CACHE_RA_EMAIL];
360
                        }
361
                        return false;
362
                }
2 daniel-mar 363
        }
364
 
192 daniel-mar 365
        public function getTitle() {
261 daniel-mar 366
                if (!OIDplus::baseConfig()->getValue('OBJECT_CACHING', true)) {
367
                        $res = OIDplus::db()->query("select title from ###objects where id = ?", array($this->nodeId()));
251 daniel-mar 368
                        if ($res->num_rows() == 0) return null;
236 daniel-mar 369
                        $row = $res->fetch_array();
192 daniel-mar 370
                        return $row['title'];
371
                } else {
372
                        self::buildObjectInformationCache();
373
                        if (isset(self::$object_info_cache[$this->nodeId()])) {
374
                                return self::$object_info_cache[$this->nodeId()][self::CACHE_TITLE];
375
                        }
376
                        return false;
377
                }
378
        }
379
 
2 daniel-mar 380
        public function userHasParentalWriteRights($ra_email=null) {
115 daniel-mar 381
                if ($ra_email instanceof OIDplusRA) $ra_email = $ra_email->raEmail();
382
 
2 daniel-mar 383
                if (is_null($ra_email)) {
384
                        if (OIDplus::authUtils()::isAdminLoggedIn()) return true;
385
                }
386
 
387
                $objParent = $this->getParent();
419 daniel-mar 388
                if (!$objParent) return false;
2 daniel-mar 389
                return $objParent->userHasWriteRights($ra_email);
390
        }
391
 
392
        public function userHasWriteRights($ra_email=null) {
115 daniel-mar 393
                if ($ra_email instanceof OIDplusRA) $ra_email = $ra_email->raEmail();
394
 
2 daniel-mar 395
                if (is_null($ra_email)) {
396
                        if (OIDplus::authUtils()::isAdminLoggedIn()) return true;
397
                        return OIDplus::authUtils()::isRaLoggedIn($this->getRaMail());
398
                } else {
399
                        return $this->getRaMail() == $ra_email;
400
                }
401
        }
12 daniel-mar 402
 
403
        public function distance($to) {
404
                return null; // not implemented
405
        }
20 daniel-mar 406
 
407
        public function equals($obj) {
408
                if (!is_object($obj)) $obj = OIDplusObject::parse($obj);
28 daniel-mar 409
                if (!($obj instanceof $this)) return false;
410
 
20 daniel-mar 411
                $distance = $this->distance($obj);
412
                if (is_numeric($distance)) return $distance === 0; // if the distance function is implemented, use it
413
 
414
                return $this->nodeId() == $obj->nodeId(); // otherwise compare the node id case-sensitive
415
        }
416
 
417
        public static function findFitting($id) {
418
                $obj = OIDplusObject::parse($id);
360 daniel-mar 419
                if (!$obj) throw new OIDplusException(_L('findFitting: Parse failed'));
20 daniel-mar 420
 
261 daniel-mar 421
                if (!OIDplus::baseConfig()->getValue('OBJECT_CACHING', true)) {
422
                        $res = OIDplus::db()->query("select id from ###objects where id like ?", array($obj->ns().':%'));
236 daniel-mar 423
                        while ($row = $res->fetch_object()) {
150 daniel-mar 424
                                $test = OIDplusObject::parse($row->id);
425
                                if ($obj->equals($test)) return $test;
426
                        }
427
                        return false;
428
                } else {
429
                        self::buildObjectInformationCache();
193 daniel-mar 430
                        foreach (self::$object_info_cache as $id => list($confidential, $parent, $ra_email, $title)) {
150 daniel-mar 431
                                if (strpos($id, $obj->ns().':') === 0) {
432
                                        $test = OIDplusObject::parse($id);
433
                                        if ($obj->equals($test)) return $test;
434
                                }
435
                        }
436
                        return false;
20 daniel-mar 437
                }
438
        }
439
 
440
        public function one_up() {
441
                return null; // not implemented
442
        }
150 daniel-mar 443
 
444
        // Caching stuff
445
 
446
        protected static $object_info_cache = null;
447
 
448
        public static function resetObjectInformationCache() {
449
                self::$object_info_cache = null;
450
        }
451
 
193 daniel-mar 452
        const CACHE_CONFIDENTIAL = 0; // TODO: An object would be better so you can use $cacheitem->isConfidential() etc.
150 daniel-mar 453
        const CACHE_PARENT = 1;
454
        const CACHE_RA_EMAIL = 2;
192 daniel-mar 455
        const CACHE_TITLE = 3;
150 daniel-mar 456
 
457
        private static function buildObjectInformationCache() {
458
                if (is_null(self::$object_info_cache)) {
459
                        self::$object_info_cache = array();
261 daniel-mar 460
                        $res = OIDplus::db()->query("select id, parent, confidential, ra_email, title from ###objects");
236 daniel-mar 461
                        while ($row = $res->fetch_array()) {
192 daniel-mar 462
                                self::$object_info_cache[$row['id']] = array($row['confidential'], $row['parent'], $row['ra_email'], $row['title']);
150 daniel-mar 463
                        }
464
                }
465
        }
415 daniel-mar 466
}