Subversion Repositories oidplus

Rev

Rev 221 | Rev 228 | 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
 
150 daniel-mar 22
define('OIDPLUS_OBJECT_CACHING', true);
23
 
193 daniel-mar 24
define('UUID_NAMEBASED_NS_OidPlusMisc', 'ad1654e6-7e15-11e4-9ef6-78e3b5fc7f22');
25
 
227 daniel-mar 26
abstract class OIDplusObject {
2 daniel-mar 27
        public static function parse($node_id) { // please overwrite this function!
28
                // TODO: in case we are not calling this class directly, check if function is overwritten and throw exception otherwise
227 daniel-mar 29
                foreach (OIDplus::getEnabledObjectTypes() as $ot) {
2 daniel-mar 30
                        if ($obj = $ot::parse($node_id)) return $obj;
31
                }
32
                return null;
33
        }
34
 
193 daniel-mar 35
        public function getAltIds() {
36
                if ($this->isRoot()) return array();
37
 
38
                $ids = array();
39
                if ($this->ns() != 'oid') {
40
                        // Creates an OIDplus-Hash-OID
41
                        // If the object type has a better way of defining an OID, please override this method!
227 daniel-mar 42
                        $sid = OIDplus::getSystemId(true);
193 daniel-mar 43
                        if (!empty($sid)) {
44
                                $oid = $sid . '.' . smallhash($this->nodeId());
45
                                $ids[] = array('oid', $oid, 'OIDplus Information Object ID');
46
                        }
47
                }
48
                if ($this->ns() != 'guid') {
49
                        // TODO: Instead of having the array($ns,$id,$desc) we should use an object oriented class
50
                        $ids[] = array('guid', gen_uuid_md5_namebased(UUID_NAMEBASED_NS_OidPlusMisc, $this->nodeId()), 'Namebased version 3 / MD5 UUID with namespace UUID_NAMEBASED_NS_OidPlusMisc');
51
                        $ids[] = array('guid', gen_uuid_sha1_namebased(UUID_NAMEBASED_NS_OidPlusMisc, $this->nodeId()), 'Namebased version 5 / SHA1 UUID with namespace UUID_NAMEBASED_NS_OidPlusMisc');
52
                }
53
                return $ids;
83 daniel-mar 54
        }
55
 
2 daniel-mar 56
        public abstract static function objectTypeTitle();
57
 
58
        public abstract static function objectTypeTitleShort();
59
 
60
        public abstract static function ns();
61
 
62
        public abstract static function root();
63
 
64
        public abstract function isRoot();
65
 
66
        public abstract function nodeId();
67
 
68
        public abstract function addString($str);
69
 
70
        public abstract function crudShowId(OIDplusObject $parent);
71
 
72
        public abstract function crudInsertPrefix();
73
 
74
        public abstract function jsTreeNodeName(OIDplusObject $parent = null);
75
 
76
        public abstract function defaultTitle();
77
 
16 daniel-mar 78
        public abstract function isLeafNode();
79
 
34 daniel-mar 80
        public abstract function getContentPage(&$title, &$content, &$icon);
2 daniel-mar 81
 
27 daniel-mar 82
        public static function getRaRoots($ra_email=null) {
115 daniel-mar 83
                if ($ra_email instanceof OIDplusRA) $ra_email = $ra_email->raEmail();
84
 
27 daniel-mar 85
                $out = array();
150 daniel-mar 86
 
87
                if (!OIDPLUS_OBJECT_CACHING) {
88
                        if (is_null($ra_email)) {
89
                                $res = OIDplus::db()->query("select oChild.id as id, oChild.ra_email as child_mail, oParent.ra_email as parent_mail from ".OIDPLUS_TABLENAME_PREFIX."objects as oChild ".
90
                                                            "left join ".OIDPLUS_TABLENAME_PREFIX."objects as oParent on oChild.parent = oParent.id ".
91
                                                            "order by ".OIDplus::db()->natOrder('oChild.id'));
92
                                while ($row = OIDplus::db()->fetch_array($res)) {
93
                                        if (!OIDplus::authUtils()::isRaLoggedIn($row['parent_mail']) && OIDplus::authUtils()::isRaLoggedIn($row['child_mail'])) {
94
                                                $x = self::parse($row['id']); // can be FALSE if namespace was disabled
95
                                                if ($x) $out[] = $x;
96
                                        }
97
                                }
98
                        } else {
99
                                $res = OIDplus::db()->query("select oChild.id as id from ".OIDPLUS_TABLENAME_PREFIX."objects as oChild ".
100
                                                            "left join ".OIDPLUS_TABLENAME_PREFIX."objects as oParent on oChild.parent = oParent.id ".
101
                                                            "where (ifnull(oParent.ra_email,'') <> ? and ifnull(oChild.ra_email,'') = ?) or ".
102
                                                            "      (oParent.ra_email is null and ifnull(oChild.ra_email,'') = ?) ".
103
                                                            "order by ".OIDplus::db()->natOrder('oChild.id'), array($ra_email, $ra_email, $ra_email));
104
                                while ($row = OIDplus::db()->fetch_array($res)) {
68 daniel-mar 105
                                        $x = self::parse($row['id']); // can be FALSE if namespace was disabled
150 daniel-mar 106
                                        if ($x) $out[] = self::parse($row['id']);
27 daniel-mar 107
                                }
2 daniel-mar 108
                        }
109
                } else {
150 daniel-mar 110
                        if (is_null($ra_email)) {
111
                                $ra_mails_to_check = OIDplusAuthUtils::loggedInRaList();
112
                                if (count($ra_mails_to_check) == 0) return $out;
113
                        } else {
114
                                $ra_mails_to_check = array($ra_email);
2 daniel-mar 115
                        }
150 daniel-mar 116
 
117
                        self::buildObjectInformationCache();
118
 
119
                        foreach ($ra_mails_to_check as $check_ra_mail) {
193 daniel-mar 120
                                $out_part = array();
150 daniel-mar 121
 
193 daniel-mar 122
                                foreach (self::$object_info_cache as $id => list($confidential, $parent, $ra_email, $title)) {
123
                                        // If the OID RA is the RA we are searching, then add the object to the choice list
124
                                        if ($ra_email == $check_ra_mail) $out_part[] = $id;
150 daniel-mar 125
                                }
126
 
193 daniel-mar 127
                                foreach (self::$object_info_cache as $id => list($confidential, $parent, $ra_email, $title)) {
128
                                        if (isset(self::$object_info_cache[$parent])) {
129
                                                if (self::$object_info_cache[$parent][self::CACHE_RA_EMAIL] == $ra_email) {
130
                                                        // if the parent has the same RA, then this OID cannot be a root => remove the element from the choice list
131
                                                        foreach (array_keys($out_part, $id) as $key) unset($out_part[$key]);
150 daniel-mar 132
                                                }
133
                                        }
134
                                }
135
 
136
                                natsort($out_part);
137
 
138
                                foreach ($out_part as $id) {
139
                                        $obj = self::parse($id);
140
                                        if ($obj) $out[] = $obj;
141
                                }
142
                        }
2 daniel-mar 143
                }
150 daniel-mar 144
 
2 daniel-mar 145
                return $out;
146
        }
147
 
150 daniel-mar 148
        public static function getAllNonConfidential() {
149
                $out = array();
150
 
151
                if (!OIDPLUS_OBJECT_CACHING) {
169 daniel-mar 152
                        $res = OIDplus::db()->query("select id from ".OIDPLUS_TABLENAME_PREFIX."objects where confidential = 0 order by ".OIDplus::db()->natOrder('id'));
150 daniel-mar 153
 
154
                        while ($row = OIDplus::db()->fetch_array($res)) {
155
                                $obj = self::parse($row['id']); // will be NULL if the object type is not registered
169 daniel-mar 156
                                if ($obj && (!$obj->isConfidential())) {
150 daniel-mar 157
                                        $out[] = $row['id'];
158
                                }
2 daniel-mar 159
                        }
160
                } else {
150 daniel-mar 161
                        self::buildObjectInformationCache();
2 daniel-mar 162
 
193 daniel-mar 163
                        foreach (self::$object_info_cache as $id => list($confidential, $parent, $ra_email, $title)) {
150 daniel-mar 164
                                if (!$confidential) {
165
                                        $obj = self::parse($id); // will be NULL if the object type is not registered
169 daniel-mar 166
                                        if ($obj && (!$obj->isConfidential())) {
150 daniel-mar 167
                                                $out[] = $id;
168
                                        }
169
                                }
2 daniel-mar 170
                        }
171
                }
172
 
173
                return $out;
174
        }
175
 
176
        public function isConfidential() {
150 daniel-mar 177
                if (!OIDPLUS_OBJECT_CACHING) {
178
                        $curid = $this->nodeId();
179
                        $orig_curid = $curid;
180
                        if (isset(self::$object_info_cache[$curid])) return self::$object_info_cache[$curid];
181
                        // Recursively search for the confidential flag in the parents
182
                        while (OIDplus::db()->num_rows($res = OIDplus::db()->query("select parent, confidential from ".OIDPLUS_TABLENAME_PREFIX."objects where id = ?", array($curid))) > 0) {
183
                                $row = OIDplus::db()->fetch_array($res);
184
                                if ($row['confidential']) {
185
                                        self::$object_info_cache[$curid] = true;
186
                                        self::$object_info_cache[$orig_curid] = true;
187
                                        return true;
188
                                } else {
189
                                        self::$object_info_cache[$curid] = false;
190
                                }
191
                                $curid = $row['parent'];
192
                                if (isset(self::$object_info_cache[$curid])) {
193
                                        self::$object_info_cache[$orig_curid] = self::$object_info_cache[$curid];
194
                                        return self::$object_info_cache[$curid];
195
                                }
196
                        }
197
 
198
                        self::$object_info_cache[$orig_curid] = false;
199
                        return false;
200
                } else {
201
                        self::buildObjectInformationCache();
202
 
203
                        $curid = $this->nodeId();
204
                        // Recursively search for the confidential flag in the parents
169 daniel-mar 205
                        while (isset(self::$object_info_cache[$curid])) {
150 daniel-mar 206
                                if (self::$object_info_cache[$curid][self::CACHE_CONFIDENTIAL]) return true;
207
                                $curid = self::$object_info_cache[$curid][self::CACHE_PARENT];
208
                        }
209
                        return false;
2 daniel-mar 210
                }
211
        }
212
 
213
        public function isChildOf(OIDplusObject $obj) {
150 daniel-mar 214
                if (!OIDPLUS_OBJECT_CACHING) {
215
                        $curid = $this->nodeId();
216
                        while (OIDplus::db()->num_rows($res = OIDplus::db()->query("select parent from ".OIDPLUS_TABLENAME_PREFIX."objects where id = ?", array($curid))) > 0) {
217
                                $row = OIDplus::db()->fetch_array($res);
218
                                if ($curid == $obj->nodeId()) return true;
219
                                $curid = $row['parent'];
220
                        }
221
                        return false;
222
                } else {
223
                        self::buildObjectInformationCache();
224
 
225
                        $curid = $this->nodeId();
169 daniel-mar 226
                        while (isset(self::$object_info_cache[$curid])) {
150 daniel-mar 227
                                if ($curid == $obj->nodeId()) return true;
228
                                $curid = self::$object_info_cache[$curid][self::CACHE_PARENT];
229
                        }
230
                        return false;
2 daniel-mar 231
                }
150 daniel-mar 232
        }
2 daniel-mar 233
 
150 daniel-mar 234
        public function getChildren() {
235
                $out = array();
236
                if (!OIDPLUS_OBJECT_CACHING) {
237
                        $res = OIDplus::db()->query("select id from ".OIDPLUS_TABLENAME_PREFIX."objects where parent = ?", array($this->nodeId()));
238
                        while ($row = OIDplus::db()->fetch_array($res)) {
239
                                $obj = self::parse($row['id']);
240
                                if (!$obj) continue;
241
                                $out[] = $obj;
242
                        }
243
                } else {
244
                        self::buildObjectInformationCache();
245
 
193 daniel-mar 246
                        foreach (self::$object_info_cache as $id => list($confidential, $parent, $ra_email, $title)) {
150 daniel-mar 247
                                if ($parent == $this->nodeId()) {
248
                                        $obj = self::parse($id);
249
                                        if (!$obj) continue;
250
                                        $out[] = $obj;
251
                                }
252
                        }
253
                }
254
                return $out;
2 daniel-mar 255
        }
256
 
115 daniel-mar 257
        public function getRa() {
150 daniel-mar 258
                return new OIDplusRA($this->getRaMail());
115 daniel-mar 259
        }
260
 
2 daniel-mar 261
        public function userHasReadRights($ra_email=null) {
115 daniel-mar 262
                if ($ra_email instanceof OIDplusRA) $ra_email = $ra_email->raEmail();
263
 
2 daniel-mar 264
                // Admin may do everything
265
                if (OIDplus::authUtils()::isAdminLoggedIn()) return true;
266
 
267
                // If it is not confidential, everybody can read/see it.
268
                if (!$this->isConfidential()) return true;
269
 
270
                // If we own the object, we may see it
271
                if (is_null($ra_email)) {
272
                        if ($this->userHasWriteRights()) return true;
273
                } else {
150 daniel-mar 274
                        if ($this->getRaMail() == $ra_email) return true;
2 daniel-mar 275
                }
276
 
277
                // If someone has rights to an object below our confidential node,
278
                // we let him see the confidential node,
279
                // Otherwise he could not browse through to his own node.
280
                $roots = $this->getRaRoots($ra_email);
281
                foreach ($roots as $root) {
282
                        if ($root->isChildOf($this)) return true;
283
                }
284
 
285
                return false;
286
        }
287
 
288
        public function getIcon($row=null) {
20 daniel-mar 289
                $namespace = $this->ns(); // must use $this, not self::, otherwise the virtual method will not be called
2 daniel-mar 290
 
291
                if (is_null($row)) {
150 daniel-mar 292
                        $ra_email = $this->getRaMail();
293
                } else {
294
                        $ra_email = $row['ra_email'];
2 daniel-mar 295
                }
296
                // TODO: have different icons for Leaf-Nodes
150 daniel-mar 297
                if (OIDplus::authUtils()::isRaLoggedIn($ra_email)) {
2 daniel-mar 298
                        $icon = 'plugins/objectTypes/'.$namespace.'/img/treeicon_own.png';
299
                } else {
300
                        $icon = 'plugins/objectTypes/'.$namespace.'/img/treeicon_general.png';
301
                }
302
                if (!file_exists($icon)) $icon = null; // default icon (folder)
303
                return $icon;
304
        }
305
 
12 daniel-mar 306
        public static function exists($id) {
150 daniel-mar 307
                if (!OIDPLUS_OBJECT_CACHING) {
308
                        $res = OIDplus::db()->query("select id from ".OIDPLUS_TABLENAME_PREFIX."objects where id = ?", array($id));
309
                        return OIDplus::db()->num_rows($res) > 0;
310
                } else {
311
                        self::buildObjectInformationCache();
312
                        return isset(self::$object_info_cache[$id]);
313
                }
12 daniel-mar 314
        }
315
 
2 daniel-mar 316
        public function getParent() {
150 daniel-mar 317
                if (!OIDPLUS_OBJECT_CACHING) {
318
                        $res = OIDplus::db()->query("select parent from ".OIDPLUS_TABLENAME_PREFIX."objects where id = ?", array($this->nodeId()));
319
                        $row = OIDplus::db()->fetch_array($res);
320
                        $parent = $row['parent'];
321
                        $obj = OIDplusObject::parse($parent);
322
                        if ($obj) return $obj;
323
                } else {
324
                        self::buildObjectInformationCache();
325
                        if (isset(self::$object_info_cache[$this->nodeId()])) {
326
                                $parent = self::$object_info_cache[$this->nodeId()][self::CACHE_PARENT];
327
                                $obj = OIDplusObject::parse($parent);
328
                                if ($obj) return $obj;
329
                        }
20 daniel-mar 330
 
150 daniel-mar 331
                        // 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()
332
                        $cur = $this->one_up();
20 daniel-mar 333
                        if (!$cur) return false;
150 daniel-mar 334
                        do {
335
                                if ($fitting = self::findFitting($cur->nodeId())) return $fitting;
20 daniel-mar 336
 
150 daniel-mar 337
                                $prev = $cur;
338
                                $cur = $cur->one_up();
339
                                if (!$cur) return false;
340
                        } while ($prev != $cur);
341
 
342
                        return false;
343
                }
2 daniel-mar 344
        }
345
 
346
        public function getRaMail() {
150 daniel-mar 347
                if (!OIDPLUS_OBJECT_CACHING) {
348
                        $res = OIDplus::db()->query("select ra_email from ".OIDPLUS_TABLENAME_PREFIX."objects where id = ?", array($this->nodeId()));
349
                        $row = OIDplus::db()->fetch_array($res);
350
                        return $row['ra_email'];
351
                } else {
352
                        self::buildObjectInformationCache();
353
                        if (isset(self::$object_info_cache[$this->nodeId()])) {
354
                                return self::$object_info_cache[$this->nodeId()][self::CACHE_RA_EMAIL];
355
                        }
356
                        return false;
357
                }
2 daniel-mar 358
        }
359
 
192 daniel-mar 360
        public function getTitle() {
361
                if (!OIDPLUS_OBJECT_CACHING) {
362
                        $res = OIDplus::db()->query("select title from ".OIDPLUS_TABLENAME_PREFIX."objects where id = ?", array($this->nodeId()));
363
                        $row = OIDplus::db()->fetch_array($res);
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);
413
                if (!$obj) throw new Exception("findFitting: Parse failed\n");
414
 
150 daniel-mar 415
                if (!OIDPLUS_OBJECT_CACHING) {
416
                        $res = OIDplus::db()->query("select id from ".OIDPLUS_TABLENAME_PREFIX."objects where id like ?", array($obj->ns().':%'));
417
                        while ($row = OIDplus::db()->fetch_object($res)) {
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();
192 daniel-mar 454
                        $res = OIDplus::db()->query("select id, parent, confidential, ra_email, title from ".OIDPLUS_TABLENAME_PREFIX."objects");
150 daniel-mar 455
                        while ($row = OIDplus::db()->fetch_array($res)) {
456
                                if ($row['confidential'] == chr(0)) $row['confidential'] = false; // ODBC...
457
                                if ($row['confidential'] == chr(1)) $row['confidential'] = true; // ODBC...
192 daniel-mar 458
                                self::$object_info_cache[$row['id']] = array($row['confidential'], $row['parent'], $row['ra_email'], $row['title']);
150 daniel-mar 459
                        }
460
                }
461
        }
2 daniel-mar 462
}