Subversion Repositories oidplus

Rev

Rev 1125 | Rev 1130 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
635 daniel-mar 1
<?php
2
 
3
/*
4
 * OIDplus 2.0
1086 daniel-mar 5
 * Copyright 2019 - 2023 Daniel Marschall, ViaThinkSoft
635 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
 
1050 daniel-mar 20
namespace ViaThinkSoft\OIDplus;
635 daniel-mar 21
 
1086 daniel-mar 22
// phpcs:disable PSR1.Files.SideEffects
23
\defined('INSIDE_OIDPLUS') or die;
24
// phpcs:enable PSR1.Files.SideEffects
25
 
635 daniel-mar 26
class OIDplusPagePublicObjects extends OIDplusPagePluginPublic {
27
 
1116 daniel-mar 28
        /**
29
         * @param $ot
30
         * @return string|null
31
         */
32
        private function get_treeicon_root($ot)/*: ?string*/ {
1127 daniel-mar 33
                $root = $ot::parse($ot::root());
34
                if (!$root) return null;
35
                return $root->getIcon();
635 daniel-mar 36
        }
37
 
1116 daniel-mar 38
        /**
39
         * @param $id
40
         * @param $old_ra
41
         * @param $new_ra
42
         * @return void
43
         * @throws OIDplusConfigInitializationException
44
         * @throws OIDplusException
45
         */
635 daniel-mar 46
        private function ra_change_rec($id, $old_ra, $new_ra) {
47
                if (is_null($old_ra)) $old_ra = '';
48
                OIDplus::db()->query("update ###objects set ra_email = ?, updated = ".OIDplus::db()->sqlDate()." where id = ? and ".OIDplus::db()->getSlang()->isNullFunction('ra_email',"''")." = ?", array($new_ra, $id, $old_ra));
978 daniel-mar 49
                OIDplusObject::resetObjectInformationCache();
635 daniel-mar 50
 
51
                $res = OIDplus::db()->query("select id from ###objects where parent = ? and ".OIDplus::db()->getSlang()->isNullFunction('ra_email',"''")." = ?", array($id, $old_ra));
52
                while ($row = $res->fetch_array()) {
53
                        $this->ra_change_rec($row['id'], $old_ra, $new_ra);
54
                }
55
        }
56
 
1116 daniel-mar 57
        /**
58
         * @param string $actionID
59
         * @param array $params
60
         * @return array|int[]
61
         * @throws OIDplusConfigInitializationException
62
         * @throws OIDplusException
63
         */
64
        public function action(string $actionID, array $params): array {
635 daniel-mar 65
 
66
                // Action:     Delete
67
                // Method:     POST
68
                // Parameters: id
69
                // Outputs:    <0 Error, =0 Success
70
                if ($actionID == 'Delete') {
71
                        _CheckParamExists($params, 'id');
72
                        $id = $params['id'];
73
                        $obj = OIDplusObject::parse($id);
1116 daniel-mar 74
                        if (!$obj) throw new OIDplusException(_L('%1 action failed because object "%2" cannot be parsed!','DELETE',$id));
635 daniel-mar 75
 
977 daniel-mar 76
                        if (!OIDplusObject::exists($id)) {
635 daniel-mar 77
                                throw new OIDplusException(_L('Object %1 does not exist',$id));
78
                        }
79
 
80
                        // Check if permitted
81
                        if (!$obj->userHasParentalWriteRights()) throw new OIDplusException(_L('Authentication error. Please log in as the superior RA to delete this OID.'));
82
 
1005 daniel-mar 83
                        foreach (OIDplus::getAllPlugins() as $plugin) {
635 daniel-mar 84
                                if ($plugin->implementsFeature('1.3.6.1.4.1.37476.2.5.2.3.3')) {
1116 daniel-mar 85
                                        $plugin->beforeObjectDelete($id); /** @phpstan-ignore-line */
635 daniel-mar 86
                                }
87
                        }
88
 
89
                        OIDplus::logger()->log("[WARN]OID($id)+[?WARN/!OK]SUPOIDRA($id)?/[?INFO/!OK]A?", "Object '$id' (recursively) deleted");
90
                        OIDplus::logger()->log("[CRIT]OIDRA($id)!", "Lost ownership of object '$id' because it was deleted");
91
 
92
                        if ($parentObj = $obj->getParent()) {
93
                                $parent_oid = $parentObj->nodeId();
94
                                OIDplus::logger()->log("[WARN]OID($parent_oid)", "Object '$id' (recursively) deleted");
95
                        }
96
 
97
                        // Delete object
98
                        OIDplus::db()->query("delete from ###objects where id = ?", array($id));
978 daniel-mar 99
                        OIDplusObject::resetObjectInformationCache();
635 daniel-mar 100
 
101
                        // Delete orphan stuff
102
                        foreach (OIDplus::getEnabledObjectTypes() as $ot) {
103
                                do {
104
                                        $res = OIDplus::db()->query("select tchild.id from ###objects tchild " .
105
                                                                    "left join ###objects tparent on tparent.id = tchild.parent " .
106
                                                                    "where tchild.parent <> ? and tchild.id like ? and tparent.id is null;", array($ot::root(), $ot::root().'%'));
790 daniel-mar 107
                                        if (!$res->any()) break;
635 daniel-mar 108
 
109
                                        while ($row = $res->fetch_array()) {
110
                                                $id_to_delete = $row['id'];
111
                                                OIDplus::logger()->log("[CRIT]OIDRA($id_to_delete)!", "Lost ownership of object '$id_to_delete' because one of the superior objects ('$id') was recursively deleted");
112
                                                OIDplus::db()->query("delete from ###objects where id = ?", array($id_to_delete));
978 daniel-mar 113
                                                OIDplusObject::resetObjectInformationCache();
635 daniel-mar 114
                                        }
115
                                } while (true);
116
                        }
117
                        OIDplus::db()->query("delete from ###asn1id where well_known = ? and oid not in (select id from ###objects where id like 'oid:%')", array(false));
118
                        OIDplus::db()->query("delete from ###iri    where well_known = ? and oid not in (select id from ###objects where id like 'oid:%')", array(false));
119
 
1005 daniel-mar 120
                        foreach (OIDplus::getAllPlugins() as $plugin) {
635 daniel-mar 121
                                if ($plugin->implementsFeature('1.3.6.1.4.1.37476.2.5.2.3.3')) {
1116 daniel-mar 122
                                        $plugin->afterObjectDelete($id); /** @phpstan-ignore-line */
635 daniel-mar 123
                                }
124
                        }
125
 
126
                        return array("status" => 0);
127
                }
128
 
129
                // Action:     Update
130
                // Method:     POST
131
                // Parameters: id, ra_email, comment, iris, asn1ids, confidential
132
                // Outputs:    <0 Error, =0 Success, with following bitfields for further information:
133
                //             1 = RA is not registered
134
                //             2 = RA is not registered, but it cannot be invited
135
                //             4 = OID is a well-known OID, so RA, ASN.1 and IRI identifiers were reset
136
                else if ($actionID == 'Update') {
137
                        _CheckParamExists($params, 'id');
138
                        $id = $params['id'];
139
                        $obj = OIDplusObject::parse($id);
1116 daniel-mar 140
                        if (!$obj) throw new OIDplusException(_L('%1 action failed because object "%2" cannot be parsed!','UPDATE',$id));
635 daniel-mar 141
 
977 daniel-mar 142
                        if (!OIDplusObject::exists($id)) {
635 daniel-mar 143
                                throw new OIDplusException(_L('Object %1 does not exist',$id));
144
                        }
145
 
146
                        // Check if permitted
147
                        if (!$obj->userHasParentalWriteRights()) throw new OIDplusException(_L('Authentication error. Please log in as the superior RA to update this OID.'));
148
 
1005 daniel-mar 149
                        foreach (OIDplus::getAllPlugins() as $plugin) {
635 daniel-mar 150
                                if ($plugin->implementsFeature('1.3.6.1.4.1.37476.2.5.2.3.3')) {
1116 daniel-mar 151
                                        $plugin->beforeObjectUpdateSuperior($id, $params); /** @phpstan-ignore-line */
635 daniel-mar 152
                                }
153
                        }
154
 
155
                        // First, do a simulation for ASN.1 IDs and IRIs to check if there are any problems (then an Exception will be thrown)
156
                        if ($obj::ns() == 'oid') {
1116 daniel-mar 157
                                assert($obj instanceof OIDplusOid); //assert(get_class($obj) === "ViaThinkSoft\OIDplus\OIDplusOid");
635 daniel-mar 158
                                if (!$obj->isWellKnown()) {
159
                                        if (isset($params['iris'])) {
160
                                                $ids = ($params['iris'] == '') ? array() : explode(',',$params['iris']);
161
                                                $ids = array_map('trim',$ids);
162
                                                $obj->replaceIris($ids, true);
163
                                        }
164
 
165
                                        if (isset($params['asn1ids'])) {
166
                                                $ids = ($params['asn1ids'] == '') ? array() : explode(',',$params['asn1ids']);
167
                                                $ids = array_map('trim',$ids);
168
                                                $obj->replaceAsn1Ids($ids, true);
169
                                        }
170
                                }
171
                        }
172
 
173
                        // RA E-Mail change
174
                        if (isset($params['ra_email'])) {
175
                                // Validate RA email address
176
                                $new_ra = $params['ra_email'];
177
                                if ($obj::ns() == 'oid') {
1116 daniel-mar 178
                                        assert($obj instanceof OIDplusOid); //assert(get_class($obj) === "ViaThinkSoft\OIDplus\OIDplusOid");
635 daniel-mar 179
                                        if ($obj->isWellKnown()) {
180
                                                $new_ra = '';
181
                                        }
182
                                }
183
                                if (!empty($new_ra) && !OIDplus::mailUtils()->validMailAddress($new_ra)) {
184
                                        throw new OIDplusException(_L('Invalid RA email address'));
185
                                }
186
 
187
                                // Change RA recursively
977 daniel-mar 188
                                $current_ra = $obj->getRaMail();
189
                                if ($new_ra != $current_ra) {
190
                                        OIDplus::logger()->log("[INFO]OID($id)+[?INFO/!OK]SUPOIDRA($id)?/[?INFO/!OK]A?", "RA of object '$id' changed from '$current_ra' to '$new_ra'");
191
                                        OIDplus::logger()->log("[WARN]RA($current_ra)!",           "Lost ownership of object '$id' due to RA transfer of superior RA / admin.");
192
                                        OIDplus::logger()->log("[INFO]RA($new_ra)!",               "Gained ownership of object '$id' due to RA transfer of superior RA / admin.");
193
                                        if ($parentObj = $obj->getParent()) {
194
                                                $parent_oid = $parentObj->nodeId();
195
                                                OIDplus::logger()->log("[INFO]OID($parent_oid)", "RA of object '$id' changed from '$current_ra' to '$new_ra'");
635 daniel-mar 196
                                        }
977 daniel-mar 197
                                        $this->ra_change_rec($id, $current_ra, $new_ra); // Recursively change inherited RAs
635 daniel-mar 198
                                }
199
                        }
200
 
201
                        // Log if confidentially flag was changed
202
                        OIDplus::logger()->log("[INFO]OID($id)+[?INFO/!OK]SUPOIDRA($id)?/[?INFO/!OK]A?", "Identifiers/Confidential flag of object '$id' updated"); // TODO: Check if they were ACTUALLY updated!
203
                        if ($parentObj = $obj->getParent()) {
204
                                $parent_oid = $parentObj->nodeId();
205
                                OIDplus::logger()->log("[INFO]OID($parent_oid)", "Identifiers/Confidential flag of object '$id' updated"); // TODO: Check if they were ACTUALLY updated!
206
                        }
207
 
208
                        // Replace ASN.1 IDs und IRIs
209
                        if ($obj::ns() == 'oid') {
1116 daniel-mar 210
                                assert($obj instanceof OIDplusOid); //assert(get_class($obj) === "ViaThinkSoft\OIDplus\OIDplusOid");
635 daniel-mar 211
                                if (!$obj->isWellKnown()) {
212
                                        if (isset($params['iris'])) {
213
                                                $ids = ($params['iris'] == '') ? array() : explode(',',$params['iris']);
214
                                                $ids = array_map('trim',$ids);
215
                                                $obj->replaceIris($ids, false);
216
                                        }
217
 
218
                                        if (isset($params['asn1ids'])) {
219
                                                $ids = ($params['asn1ids'] == '') ? array() : explode(',',$params['asn1ids']);
220
                                                $ids = array_map('trim',$ids);
221
                                                $obj->replaceAsn1Ids($ids, false);
222
                                        }
223
                                }
224
 
225
                                // TODO: Check if any identifiers have been actually changed,
226
                                // and log it to OID($id), OID($parent), ... (see above)
227
                        }
228
 
229
                        if (isset($params['confidential'])) {
230
                                $confidential = $params['confidential'] == 'true';
231
                                OIDplus::db()->query("UPDATE ###objects SET confidential = ? WHERE id = ?", array($confidential, $id));
978 daniel-mar 232
                                OIDplusObject::resetObjectInformationCache();
635 daniel-mar 233
                        }
234
 
235
                        if (isset($params['comment'])) {
236
                                $comment = $params['comment'];
237
                                OIDplus::db()->query("UPDATE ###objects SET comment = ? WHERE id = ?", array($comment, $id));
978 daniel-mar 238
                                OIDplusObject::resetObjectInformationCache();
635 daniel-mar 239
                        }
240
 
241
                        OIDplus::db()->query("UPDATE ###objects SET updated = ".OIDplus::db()->sqlDate()." WHERE id = ?", array($id));
978 daniel-mar 242
                        OIDplusObject::resetObjectInformationCache();
635 daniel-mar 243
 
244
                        $status = 0;
245
 
246
                        if (!empty($new_ra)) {
247
                                $res = OIDplus::db()->query("select ra_name from ###ra where email = ?", array($new_ra));
248
                                $invitePlugin = OIDplus::getPluginByOid('1.3.6.1.4.1.37476.2.5.2.4.2.92'); // OIDplusPageRaInvite
790 daniel-mar 249
                                if (!$res->any()) $status = !is_null($invitePlugin) && OIDplus::config()->getValue('ra_invitation_enabled') ? 1 : 2;
635 daniel-mar 250
                        }
251
 
252
                        if ($obj::ns() == 'oid') {
1116 daniel-mar 253
                                assert($obj instanceof OIDplusOid); //assert(get_class($obj) === "ViaThinkSoft\OIDplus\OIDplusOid");
635 daniel-mar 254
                                if ($obj->isWellKnown()) {
255
                                        $status += 4;
256
                                }
257
                        }
258
 
1005 daniel-mar 259
                        foreach (OIDplus::getAllPlugins() as $plugin) {
635 daniel-mar 260
                                if ($plugin->implementsFeature('1.3.6.1.4.1.37476.2.5.2.3.3')) {
1116 daniel-mar 261
                                        $plugin->afterObjectUpdateSuperior($id, $params); /** @phpstan-ignore-line */
635 daniel-mar 262
                                }
263
                        }
264
 
265
                        return array("status" => $status);
266
                }
267
 
268
                // Action:     Update2
269
                // Method:     POST
270
                // Parameters: id, title, description
271
                // Outputs:    <0 Error, =0 Success
272
                else if ($actionID == 'Update2') {
273
                        _CheckParamExists($params, 'id');
274
                        $id = $params['id'];
275
                        $obj = OIDplusObject::parse($id);
1116 daniel-mar 276
                        if (!$obj) throw new OIDplusException(_L('%1 action failed because object "%2" cannot be parsed!','UPDATE2',$id));
635 daniel-mar 277
 
977 daniel-mar 278
                        if (!OIDplusObject::exists($id)) {
635 daniel-mar 279
                                throw new OIDplusException(_L('Object %1 does not exist',$id));
280
                        }
281
 
282
                        // Check if allowed
283
                        if (!$obj->userHasWriteRights()) throw new OIDplusException(_L('Authentication error. Please log in as the RA to update this OID.'));
284
 
1005 daniel-mar 285
                        foreach (OIDplus::getAllPlugins() as $plugin) {
635 daniel-mar 286
                                if ($plugin->implementsFeature('1.3.6.1.4.1.37476.2.5.2.3.3')) {
1116 daniel-mar 287
                                        $plugin->beforeObjectUpdateSelf($id, $params); /** @phpstan-ignore-line */
635 daniel-mar 288
                                }
289
                        }
290
 
291
                        OIDplus::logger()->log("[INFO]OID($id)+[?INFO/!OK]OIDRA($id)?/[?INFO/!OK]A?", "Title/Description of object '$id' updated");
292
 
293
                        if (isset($params['title'])) {
294
                                $title = $params['title'];
295
                                OIDplus::db()->query("UPDATE ###objects SET title = ? WHERE id = ?", array($title, $id));
978 daniel-mar 296
                                OIDplusObject::resetObjectInformationCache();
635 daniel-mar 297
                        }
298
 
299
                        if (isset($params['description'])) {
300
                                $description = $params['description'];
301
                                OIDplus::db()->query("UPDATE ###objects SET description = ? WHERE id = ?", array($description, $id));
978 daniel-mar 302
                                OIDplusObject::resetObjectInformationCache();
635 daniel-mar 303
                        }
304
 
305
                        OIDplus::db()->query("UPDATE ###objects SET updated = ".OIDplus::db()->sqlDate()." WHERE id = ?", array($id));
978 daniel-mar 306
                        OIDplusObject::resetObjectInformationCache();
635 daniel-mar 307
 
1005 daniel-mar 308
                        foreach (OIDplus::getAllPlugins() as $plugin) {
635 daniel-mar 309
                                if ($plugin->implementsFeature('1.3.6.1.4.1.37476.2.5.2.3.3')) {
1116 daniel-mar 310
                                        $plugin->afterObjectUpdateSelf($id, $params); /** @phpstan-ignore-line */
635 daniel-mar 311
                                }
312
                        }
313
 
314
                        return array("status" => 0);
315
                }
316
 
317
                // Generate UUID
318
                else if ($actionID == 'generate_uuid') {
319
                        $uuid = gen_uuid();
320
                        if (!$uuid) return array("status" => 1);
321
                        return array(
322
                                "status" => 0,
323
                                "uuid" => $uuid,
324
                                "intval" => substr(uuid_to_oid($uuid),strlen('2.25.'))
325
                        );
326
                }
327
 
328
                // Action:     Insert
329
                // Method:     POST
330
                // Parameters: parent, id, ra_email, confidential, iris, asn1ids
331
                // Outputs:    status=<0 Error, =0 Success, with following bitfields for further information:
332
                //             1 = RA is not registered
333
                //             2 = RA is not registered, but it cannot be invited
334
                //             4 = OID is a well-known OID, so RA, ASN.1 and IRI identifiers were reset
335
                else if ($actionID == 'Insert') {
336
                        // Check if you have write rights on the parent (to create a new object)
337
                        _CheckParamExists($params, 'parent');
338
                        $objParent = OIDplusObject::parse($params['parent']);
1116 daniel-mar 339
                        if (!$objParent) throw new OIDplusException(_L('%1 action failed because parent object "%2" cannot be parsed!','INSERT',$params['parent']));
635 daniel-mar 340
 
977 daniel-mar 341
                        if (!$objParent->isRoot()) {
342
                                $idParent = $objParent->nodeId();
343
                                if (!OIDplusObject::exists($idParent)) {
344
                                        throw new OIDplusException(_L('Parent object %1 does not exist',$idParent));
345
                                }
635 daniel-mar 346
                        }
347
 
348
                        if (!$objParent->userHasWriteRights()) throw new OIDplusException(_L('Authentication error. Please log in as the correct RA to insert an OID at this arc.'));
349
 
350
                        // Check if the ID is valid
351
                        _CheckParamExists($params, 'id');
352
                        if ($params['id'] == '') throw new OIDplusException(_L('ID may not be empty'));
353
 
772 daniel-mar 354
                        // For the root objects, let the user also enter a WEID
355
                        if ($objParent::ns() == 'oid') {
1116 daniel-mar 356
                                assert($objParent instanceof OIDplusOid); //assert(get_class($objParent) === "ViaThinkSoft\OIDplus\OIDplusOid");
772 daniel-mar 357
                                if (strtolower(substr(trim($params['id']),0,5)) === 'weid:') {
358
                                        if ($objParent->isRoot()) {
1050 daniel-mar 359
                                                $params['id'] = \Frdl\Weid\WeidOidConverter::weid2oid($params['id']);
772 daniel-mar 360
                                                if ($params['id'] === false) {
361
                                                        throw new OIDplusException(_L('Invalid WEID'));
362
                                                }
363
                                        } else {
364
                                                throw new OIDplusException(_L('You can use the WEID syntax only at your object tree root.'));
365
                                        }
366
                                }
367
                        }
368
 
635 daniel-mar 369
                        // Determine absolute OID name
370
                        // Note: At addString() and parse(), the syntax of the ID will be checked
371
                        $id = $objParent->addString($params['id']);
372
 
373
                        // Check, if the OID exists
977 daniel-mar 374
                        if (OIDplusObject::exists($id)) {
635 daniel-mar 375
                                throw new OIDplusException(_L('Object %1 already exists!',$id));
376
                        }
377
 
378
                        $obj = OIDplusObject::parse($id);
1116 daniel-mar 379
                        if (!$obj) throw new OIDplusException(_L('%1 action failed because object "%2" cannot be parsed!','INSERT',$id));
635 daniel-mar 380
 
1005 daniel-mar 381
                        foreach (OIDplus::getAllPlugins() as $plugin) {
635 daniel-mar 382
                                if ($plugin->implementsFeature('1.3.6.1.4.1.37476.2.5.2.3.3')) {
1116 daniel-mar 383
                                        $plugin->beforeObjectInsert($id, $params); /** @phpstan-ignore-line */
635 daniel-mar 384
                                }
385
                        }
386
 
387
                        // First simulate if there are any problems of ASN.1 IDs und IRIs
388
                        if ($obj::ns() == 'oid') {
1116 daniel-mar 389
                                assert($obj instanceof OIDplusOid); //assert(get_class($obj) === "ViaThinkSoft\OIDplus\OIDplusOid");
635 daniel-mar 390
                                if (!$obj->isWellKnown()) {
391
                                        if (isset($params['iris'])) {
392
                                                $ids = ($params['iris'] == '') ? array() : explode(',',$params['iris']);
393
                                                $ids = array_map('trim',$ids);
394
                                                $obj->replaceIris($ids, true);
395
                                        }
396
 
397
                                        if (isset($params['asn1ids'])) {
398
                                                $ids = ($params['asn1ids'] == '') ? array() : explode(',',$params['asn1ids']);
399
                                                $ids = array_map('trim',$ids);
400
                                                $obj->replaceAsn1Ids($ids, true);
401
                                        }
402
                                }
403
                        }
404
 
405
                        // Apply superior RA change
406
                        $parent = $params['parent'];
407
                        $ra_email = isset($params['ra_email']) ? $params['ra_email'] : '';
408
                        if ($obj::ns() == 'oid') {
1116 daniel-mar 409
                                assert($obj instanceof OIDplusOid); //assert(get_class($obj) === "ViaThinkSoft\OIDplus\OIDplusOid");
635 daniel-mar 410
                                if ($obj->isWellKnown()) {
411
                                        $ra_email = '';
412
                                }
413
                        }
414
                        if (!empty($ra_email) && !OIDplus::mailUtils()->validMailAddress($ra_email)) {
415
                                throw new OIDplusException(_L('Invalid RA email address'));
416
                        }
417
 
418
                        OIDplus::logger()->log("[INFO]OID($parent)+[INFO]OID($id)+[?INFO/!OK]OIDRA($parent)?/[?INFO/!OK]A?", "Object '$id' created, ".(empty($ra_email) ? "without defined RA" : "given to RA '$ra_email'")).", superior object is '$parent'";
419
                        if (!empty($ra_email)) {
420
                                OIDplus::logger()->log("[INFO]RA($ra_email)!", "Gained ownership of newly created object '$id'");
421
                        }
422
 
423
                        $confidential = isset($params['confidential']) ? ($params['confidential'] == 'true') : false;
424
                        $comment = isset($params['comment']) ? $params['comment'] : '';
425
                        $title = '';
426
                        $description = '';
427
 
428
                        if (strlen($id) > OIDplus::baseConfig()->getValue('LIMITS_MAX_ID_LENGTH')) {
429
                                $maxlen = OIDplus::baseConfig()->getValue('LIMITS_MAX_ID_LENGTH');
430
                                throw new OIDplusException(_L('The identifier %1 is too long (max allowed length: %2)',$id,$maxlen));
431
                        }
432
 
433
                        OIDplus::db()->query("INSERT INTO ###objects (id, parent, ra_email, confidential, comment, created, title, description) VALUES (?, ?, ?, ?, ?, ".OIDplus::db()->sqlDate().", ?, ?)", array($id, $parent, $ra_email, $confidential, $comment, $title, $description));
978 daniel-mar 434
                        OIDplusObject::resetObjectInformationCache();
635 daniel-mar 435
 
436
                        // Set ASN.1 IDs und IRIs
437
                        if ($obj::ns() == 'oid') {
1116 daniel-mar 438
                                assert($obj instanceof OIDplusOid); //assert(get_class($obj) === "ViaThinkSoft\OIDplus\OIDplusOid");
635 daniel-mar 439
                                if (!$obj->isWellKnown()) {
440
                                        if (isset($params['iris'])) {
441
                                                $ids = ($params['iris'] == '') ? array() : explode(',',$params['iris']);
442
                                                $ids = array_map('trim',$ids);
443
                                                $obj->replaceIris($ids, false);
444
                                        }
445
 
446
                                        if (isset($params['asn1ids'])) {
447
                                                $ids = ($params['asn1ids'] == '') ? array() : explode(',',$params['asn1ids']);
448
                                                $ids = array_map('trim',$ids);
449
                                                $obj->replaceAsn1Ids($ids, false);
450
                                        }
451
                                }
452
                        }
453
 
454
                        $status = 0;
455
 
456
                        if (!empty($ra_email)) {
457
                                // Do we need to notify that the RA does not exist?
458
                                $res = OIDplus::db()->query("select ra_name from ###ra where email = ?", array($ra_email));
459
                                $invitePlugin = OIDplus::getPluginByOid('1.3.6.1.4.1.37476.2.5.2.4.2.92'); // OIDplusPageRaInvite
790 daniel-mar 460
                                if (!$res->any()) $status = !is_null($invitePlugin) && OIDplus::config()->getValue('ra_invitation_enabled') ? 1 : 2;
635 daniel-mar 461
                        }
462
 
463
                        if ($obj::ns() == 'oid') {
1116 daniel-mar 464
                                assert($obj instanceof OIDplusOid); //assert(get_class($obj) === "ViaThinkSoft\OIDplus\OIDplusOid");
635 daniel-mar 465
                                if ($obj->isWellKnown()) {
466
                                        $status += 4;
467
                                }
468
                        }
469
 
1005 daniel-mar 470
                        foreach (OIDplus::getAllPlugins() as $plugin) {
635 daniel-mar 471
                                if ($plugin->implementsFeature('1.3.6.1.4.1.37476.2.5.2.3.3')) {
1116 daniel-mar 472
                                        $plugin->afterObjectInsert($id, $params); /** @phpstan-ignore-line */
635 daniel-mar 473
                                }
474
                        }
475
 
476
                        return array(
477
                                "status" => $status,
478
                                "inserted_id" => $id
479
                        );
480
                } else {
1116 daniel-mar 481
                        return parent::action($actionID, $params);
635 daniel-mar 482
                }
483
        }
484
 
1116 daniel-mar 485
        /**
486
         * @param bool $html
487
         * @return void
488
         * @throws OIDplusException
489
         */
490
        public function init(bool $html=true) {
635 daniel-mar 491
                OIDplus::config()->prepareConfigKey('oobe_objects_done', '"Out Of Box Experience" wizard for OIDplusPagePublicObjects done once?', '0', OIDplusConfig::PROTECTION_HIDDEN, function($value) {});
693 daniel-mar 492
                OIDplus::config()->prepareConfigKey('oid_grid_show_weid', 'Show WEID/Base36 column in CRUD grid of OIDs?', '1', OIDplusConfig::PROTECTION_EDITABLE, function($value) {
493
                        if (!is_numeric($value) || ($value < 0) || ($value > 1)) {
494
                                throw new OIDplusException(_L('Please enter a valid value (0=no, 1=yes).'));
495
                        }
496
                });
635 daniel-mar 497
        }
498
 
1116 daniel-mar 499
        /**
500
         * @param $id
501
         * @param $out
502
         * @return array|false
503
         * @throws OIDplusException
504
         */
951 daniel-mar 505
        private function tryObject($id, &$out) {
506
                $parent = null;
507
                $res = null;
508
                $row = null;
954 daniel-mar 509
                $obj = OIDplusObject::parse($id);
1116 daniel-mar 510
                if (!$obj) return false;
954 daniel-mar 511
                if ($obj->isRoot()) {
512
                        $obj->getContentPage($out['title'], $out['text'], $out['icon']);
977 daniel-mar 513
                        $objParent = null; // $obj->getParent();
954 daniel-mar 514
                } else {
977 daniel-mar 515
                        $obj = OIDplusObject::findFitting($id); // this time, the object will be found, not just the object type
516
                        if (!$obj) {
954 daniel-mar 517
                                return false;
518
                        } else {
519
                                $obj->getContentPage($out['title'], $out['text'], $out['icon']);
520
                                if (empty($out['title'])) $out['title'] = explode(':',$obj->nodeId(),2)[1];
977 daniel-mar 521
                                $objParent = $obj->getParent();
953 daniel-mar 522
                        }
951 daniel-mar 523
                }
977 daniel-mar 524
                return array($id, $obj, $objParent);
951 daniel-mar 525
        }
526
 
1116 daniel-mar 527
        /**
528
         * @param $id
529
         * @return array
530
         */
967 daniel-mar 531
        public static function getAlternativesForQuery($id) {
968 daniel-mar 532
                // Attention: This is NOT an implementation of 1.3.6.1.4.1.37476.2.5.2.3.7 !
533
                //            This is the function that calls getAlternativesForQuery() of every plugin that implements 1.3.6.1.4.1.37476.2.5.2.3.7
534
 
951 daniel-mar 535
                // e.g. used for "Reverse Alt Id"
536
                $alternatives = array();
1005 daniel-mar 537
                foreach (OIDplus::getAllPlugins() as $plugin) {
951 daniel-mar 538
                        if ($plugin->implementsFeature('1.3.6.1.4.1.37476.2.5.2.3.7')) {
1116 daniel-mar 539
                                $tmp = $plugin->getAlternativesForQuery($id); /** @phpstan-ignore-line */
951 daniel-mar 540
                                if (is_array($tmp)) {
541
                                        $alternatives = array_merge($tmp, $alternatives);
542
                                }
543
                        }
544
                }
968 daniel-mar 545
 
546
                // If something is more than one time, remove it
951 daniel-mar 547
                $alternatives = array_unique($alternatives);
968 daniel-mar 548
 
549
                // If a plugin accidentally added the own ID, remove it. This function lists only alternatives, not the own ID
550
                $alternatives_tmp = array();
551
                foreach ($alternatives as $alt) {
552
                        if ($alt !== $id) $alternatives_tmp[] = $alt;
553
                }
554
                $alternatives = $alternatives_tmp;
555
 
951 daniel-mar 556
                return $alternatives;
557
        }
558
 
1116 daniel-mar 559
        /**
560
         * @param string $id
561
         * @param array $out
562
         * @param bool $handled
563
         * @return void
564
         * @throws OIDplusConfigInitializationException
565
         * @throws OIDplusException
566
         */
567
        public function gui(string $id, array &$out, bool &$handled) {
635 daniel-mar 568
                if ($id === 'oidplus:system') {
569
                        $handled = true;
570
 
571
                        $out['title'] = OIDplus::config()->getValue('system_title');
801 daniel-mar 572
                        $out['icon'] = OIDplus::webpath(__DIR__,OIDplus::PATH_RELATIVE).'img/main_icon.png';
635 daniel-mar 573
 
574
                        if (file_exists(OIDplus::localpath() . 'userdata/welcome/welcome$'.OIDplus::getCurrentLang().'.html')) {
575
                                $cont = file_get_contents(OIDplus::localpath() . 'userdata/welcome/welcome$'.OIDplus::getCurrentLang().'.html');
576
                        } else if (file_exists(OIDplus::localpath() . 'userdata/welcome/welcome.html')) {
577
                                $cont = file_get_contents(OIDplus::localpath() . 'userdata/welcome/welcome.html');
578
                        } else if (file_exists(__DIR__ . '/welcome$'.OIDplus::getCurrentLang().'.html')) {
579
                                $cont = file_get_contents(__DIR__ . '/welcome$'.OIDplus::getCurrentLang().'.html');
580
                        } else if (file_exists(__DIR__ . '/welcome.html')) {
581
                                $cont = file_get_contents(__DIR__ . '/welcome.html');
582
                        } else {
583
                                $cont = '';
584
                        }
585
 
586
                        list($html, $js, $css) = extractHtmlContents($cont);
587
                        $cont = '';
588
                        if (!empty($js))  $cont .= "<script>\n$js\n</script>";
589
                        if (!empty($css)) $cont .= "<style>\n$css\n</style>";
821 daniel-mar 590
                        $cont .= stripHtmlComments($html);
635 daniel-mar 591
 
592
                        $out['text'] = $cont;
593
 
594
                        if (strpos($out['text'], '%%OBJECT_TYPE_LIST%%') !== false) {
595
                                $tmp = '<ul>';
596
                                foreach (OIDplus::getEnabledObjectTypes() as $ot) {
597
                                        $tmp .= '<li><a '.OIDplus::gui()->link($ot::root()).'>'.htmlentities($ot::objectTypeTitle()).'</a></li>';
598
                                }
599
                                $tmp .= '</ul>';
600
                                $out['text'] = str_replace('%%OBJECT_TYPE_LIST%%', $tmp, $out['text']);
601
                        }
602
                }
603
 
957 daniel-mar 604
                // Never answer to an object type that is called 'oidplus:',
605
                // otherwise, an object type plugin could break the whole system!
606
                else if ((strpos($id,':') !== false) && (!str_starts_with($id,'oidplus:'))) {
635 daniel-mar 607
 
955 daniel-mar 608
                        // --- Try to find the object or an alternative
635 daniel-mar 609
 
951 daniel-mar 610
                        $test = $this->tryObject($id, $out);
611
                        if ($test === false) {
955 daniel-mar 612
                                // try to find an alternative
952 daniel-mar 613
                                $alternatives = $this->getAlternativesForQuery($id);
951 daniel-mar 614
                                foreach ($alternatives as $alternative) {
615
                                        $test = $this->tryObject($alternative, $out);
955 daniel-mar 616
                                        if ($test !== false) break; // found something
635 daniel-mar 617
                                }
618
                        }
955 daniel-mar 619
                        if ($test !== false) {
977 daniel-mar 620
                                list($id, $obj, $objParent) = $test;
955 daniel-mar 621
                        }
622
 
623
                        // --- If the object type is disabled or not an object at all (e.g. "oidplus:"), then $handled=false
624
                        //     If the object type is enabled but object not found, $handled=true
625
 
970 daniel-mar 626
                        $obj = OIDplusObject::parse($id);
955 daniel-mar 627
 
951 daniel-mar 628
                        if ($test === false) {
1116 daniel-mar 629
                                if (!$obj) {
955 daniel-mar 630
                                        // Object type disabled or not known (e.g. ObjectType "oidplus:").
631
                                        $handled = false;
632
                                        return;
633
                                } else {
634
                                        // Object type enabled but identifier not in database
635
                                        $handled = true;
636
                                        if (isset($_SERVER['SCRIPT_FILENAME']) && (strtolower(basename($_SERVER['SCRIPT_FILENAME'])) !== 'ajax.php')) { // don't send HTTP error codes in ajax.php, because we want a page and not a JavaScript alert box, when someone enters an invalid OID in the GoTo-Box
637
                                                http_response_code(404);
638
                                        }
639
                                        $out['title'] = _L('Object not found');
640
                                        $out['icon'] = 'img/error.png';
641
                                        $out['text'] = _L('The object %1 was not found in this database.','<code>'.htmlentities($id).'</code>');
642
                                        return;
643
                                }
644
                        } else {
645
                                $handled = true;
646
                        }
647
 
648
                        unset($test);
649
 
650
                        // --- If found, do we have read rights?
651
 
652
                        if (!$obj->userHasReadRights()) {
843 daniel-mar 653
                                if (isset($_SERVER['SCRIPT_FILENAME']) && (strtolower(basename($_SERVER['SCRIPT_FILENAME'])) !== 'ajax.php')) { // don't send HTTP error codes in ajax.php, because we want a page and not a JavaScript alert box, when someone enters an invalid OID in the GoTo-Box
955 daniel-mar 654
                                        http_response_code(403);
843 daniel-mar 655
                                }
955 daniel-mar 656
                                $out['title'] = _L('Access denied');
800 daniel-mar 657
                                $out['icon'] = 'img/error.png';
955 daniel-mar 658
                                $out['text'] = '<p>'._L('Please <a %1>log in</a> to receive information about this object.',OIDplus::gui()->link('oidplus:login')).'</p>';
635 daniel-mar 659
                                return;
660
                        }
661
 
662
                        // ---
663
 
977 daniel-mar 664
                        if ($objParent) {
665
                                if ($objParent->isRoot()) {
666
                                        $parent_link_text = $objParent->objectTypeTitle();
667
                                        $out['text'] = '<p><a '.OIDplus::gui()->link($objParent->root()).'><img src="img/arrow_back.png" width="16" alt="'._L('Go back').'"> '._L('Parent node: %1',htmlentities($parent_link_text)).'</a></p>' . $out['text'];
635 daniel-mar 668
                                } else {
977 daniel-mar 669
                                        $parent_title = $objParent->getTitle();
670
                                        if (empty($parent_title) && ($objParent->ns() == 'oid')) {
1116 daniel-mar 671
                                                assert($objParent instanceof OIDplusOid); //assert(get_class($objParent) === "ViaThinkSoft\OIDplus\OIDplusOid");
977 daniel-mar 672
                                                // If not title is available, then use an ASN.1 identifier
673
                                                $res_asn = OIDplus::db()->query("select name from ###asn1id where oid = ?", array($objParent->nodeId()));
674
                                                if ($res_asn->any()) {
675
                                                        $row_asn = $res_asn->fetch_array();
676
                                                        $parent_title = $row_asn['name']; // TODO: multiple ASN1 ids?
635 daniel-mar 677
                                                }
977 daniel-mar 678
                                        }
635 daniel-mar 679
 
977 daniel-mar 680
                                        $parent_link_text = empty($parent_title) ? explode(':',$objParent->nodeId())[1] : $parent_title.' ('.explode(':',$objParent->nodeId())[1].')';
635 daniel-mar 681
 
977 daniel-mar 682
                                        $out['text'] = '<p><a '.OIDplus::gui()->link($objParent->nodeId()).'><img src="img/arrow_back.png" width="16" alt="'._L('Go back').'"> '._L('Parent node: %1',htmlentities($parent_link_text)).'</a></p>' . $out['text'];
635 daniel-mar 683
                                }
684
                        } else {
685
                                $parent_link_text = _L('Go back to front page');
686
                                $out['text'] = '<p><a '.OIDplus::gui()->link('oidplus:system').'><img src="img/arrow_back.png" width="16" alt="'._L('Go back').'"> '.htmlentities($parent_link_text).'</a></p>' . $out['text'];
687
                        }
688
 
689
                        // ---
690
 
977 daniel-mar 691
                        if ($obj) {
692
                                $title = $obj->getTitle();
693
                                $description = $obj->getDescription();
999 daniel-mar 694
                                if (empty(strip_tags($description)) && (stripos($description,'<img') === false)) {
977 daniel-mar 695
                                        if (empty($title)) {
635 daniel-mar 696
                                                $desc = '<p><i>'._L('No description for this object available').'</i></p>';
697
                                        } else {
977 daniel-mar 698
                                                $desc = $title;
635 daniel-mar 699
                                        }
700
                                } else {
977 daniel-mar 701
                                        $desc = self::objDescription($description);
635 daniel-mar 702
                                }
703
 
704
                                if ($obj->userHasWriteRights()) {
705
                                        $rand = ++self::$crudCounter;
706
                                        $desc = '<noscript><p><b>'._L('You need to enable JavaScript to edit title or description of this object.').'</b></p>'.$desc.'</noscript>';
707
                                        $desc .= '<div class="container box" style="display:none" id="descbox_'.$rand.'">';
977 daniel-mar 708
                                        $desc .= _L('Title').': <input type="text" name="title" id="titleedit" value="'.htmlentities($title).'"><br><br>'._L('Description').':<br>';
709
                                        $desc .= self::showMCE('description', $description);
635 daniel-mar 710
                                        $desc .= '<button type="button" name="update_desc" id="update_desc" class="btn btn-success btn-xs update" onclick="OIDplusPagePublicObjects.updateDesc()">'._L('Update description').'</button>';
711
                                        $desc .= '</div>';
712
                                        $desc .= '<script>$("#descbox_'.$rand.'")[0].style.display = "block";</script>';
713
                                }
714
                        } else {
715
                                $desc = '';
716
                        }
717
 
718
                        // ---
719
 
720
                        if (strpos($out['text'], '%%DESC%%') !== false)
721
                                $out['text'] = str_replace('%%DESC%%',    $desc,                              $out['text']);
722
                        if (strpos($out['text'], '%%CRUD%%') !== false)
856 daniel-mar 723
                                $out['text'] = str_replace('%%CRUD%%',    self::showCrud($obj->nodeId()),     $out['text']);
635 daniel-mar 724
                        if (strpos($out['text'], '%%RA_INFO%%') !== false)
977 daniel-mar 725
                                $out['text'] = str_replace('%%RA_INFO%%', OIDplusPagePublicRaInfo::showRaInfo($obj->getRaMail()), $out['text']);
635 daniel-mar 726
 
727
                        $alt_ids = $obj->getAltIds();
728
                        if (count($alt_ids) > 0) {
729
                                $out['text'] .= '<h2>'._L('Alternative Identifiers').'</h2>';
730
                                foreach ($alt_ids as $alt_id) {
731
                                        $ns = $alt_id->getNamespace();
732
                                        $aid = $alt_id->getId();
733
                                        $aiddesc = $alt_id->getDescription();
945 daniel-mar 734
                                        $suffix = $alt_id->getSuffix();
735
                                        $out['text'] .= "$aiddesc: <code>$ns:$aid</code>$suffix<br>";
635 daniel-mar 736
                                }
737
                        }
738
 
1005 daniel-mar 739
                        foreach (OIDplus::getAllPlugins() as $plugin) {
635 daniel-mar 740
                                if ($plugin->implementsFeature('1.3.6.1.4.1.37476.2.5.2.3.2')) {
1116 daniel-mar 741
                                        $plugin->modifyContent($obj->nodeId(), $out['title'], $out['icon'], $out['text']); /** @phpstan-ignore-line */
635 daniel-mar 742
                                }
743
                        }
744
                }
745
        }
746
 
1116 daniel-mar 747
        /**
748
         * @param $json
749
         * @param $out
750
         * @return void
751
         */
635 daniel-mar 752
        private function publicSitemap_rec($json, &$out) {
753
                foreach ($json as $x) {
754
                        if (isset($x['id']) && $x['id']) {
755
                                $out[] = $x['id'];
756
                        }
757
                        if (isset($x['children'])) {
758
                                $this->publicSitemap_rec($x['children'], $out);
759
                        }
760
                }
761
        }
762
 
1116 daniel-mar 763
        /**
764
         * @param array $out
765
         * @return void
766
         */
767
        public function publicSitemap(array &$out) {
635 daniel-mar 768
                $json = array();
1116 daniel-mar 769
                $this->tree($json, null/*RA EMail*/, false/*HTML tree algorithm*/, "*"/*display all*/);
635 daniel-mar 770
                $this->publicSitemap_rec($json, $out);
771
        }
772
 
1116 daniel-mar 773
        /**
774
         * @param array $json
775
         * @param string|null $ra_email
776
         * @param bool $nonjs
777
         * @param string $req_goto
778
         * @return bool
779
         * @throws OIDplusConfigInitializationException
780
         * @throws OIDplusException
781
         */
782
        public function tree(array &$json, string $ra_email=null, bool $nonjs=false, string $req_goto=''): bool {
635 daniel-mar 783
                if ($nonjs) {
784
                        $json[] = array(
785
                                'id' => 'oidplus:system',
801 daniel-mar 786
                                'icon' => OIDplus::webpath(__DIR__,OIDplus::PATH_RELATIVE).'img/main_icon16.png',
635 daniel-mar 787
                                'text' => _L('System')
788
                        );
789
 
977 daniel-mar 790
                        $objGoto = OIDplusObject::findFitting($req_goto);
998 daniel-mar 791
                        $objGotoParent = $objGoto ? $objGoto->getParent() : null;
977 daniel-mar 792
                        $parent = $objGotoParent ? $objGotoParent->nodeId() : '';
635 daniel-mar 793
 
794
                        $objTypesChildren = array();
795
                        foreach (OIDplus::getEnabledObjectTypes() as $ot) {
796
                                $icon = $this->get_treeicon_root($ot);
797
 
798
                                $json[] = array(
799
                                        'id' => $ot::root(),
800
                                        'icon' => $icon,
801
                                        'text' => $ot::objectTypeTitle()
802
                                );
803
 
954 daniel-mar 804
                                $tmp = OIDplusObject::parse($req_goto);
1116 daniel-mar 805
                                if ($tmp && ($ot == get_class($tmp))) {
635 daniel-mar 806
                                        // TODO: Instead of just having 3 levels (parent, this and children), it would be better if we'd had a full tree of all parents
807
                                        //       on the other hand, for giving search engines content, this is good enough
808
                                        if (empty($parent)) {
809
                                                $res = OIDplus::db()->query("select * from ###objects where " .
810
                                                                            "parent = ? or " .
811
                                                                            "id = ? " .
812
                                                                            "order by ".OIDplus::db()->natOrder('id'), array($req_goto, $req_goto));
813
                                        } else {
814
                                                $res = OIDplus::db()->query("select * from ###objects where " .
815
                                                                            "parent = ? or " .
816
                                                                            "id = ? or " .
817
                                                                            "id = ? ".
818
                                                                            "order by ".OIDplus::db()->natOrder('id'), array($req_goto, $req_goto, $parent));
819
                                        }
820
 
821
                                        $z_used = 0;
822
                                        $y_used = 0;
823
                                        $x_used = 0;
824
                                        $stufe = 0;
825
                                        $menu_entries = array();
826
                                        $stufen = array();
827
                                        while ($row = $res->fetch_object()) {
828
                                                $obj = OIDplusObject::parse($row->id);
1116 daniel-mar 829
                                                if (!$obj) continue; // might happen if the objectType is not available/loaded
635 daniel-mar 830
                                                if (!$obj->userHasReadRights()) continue;
831
                                                $txt = $row->title == '' ? '' : ' -- '.htmlentities($row->title);
832
 
833
                                                if ($row->id == $parent) { $stufe=0; $z_used++; }
834
                                                if ($row->id == $req_goto) { $stufe=1; $y_used++; }
835
                                                if ($row->parent == $req_goto) { $stufe=2; $x_used++; }
836
 
837
                                                $menu_entry = array('id' => $row->id, 'icon' => '', 'text' => $txt, 'indent' => 0);
838
                                                $menu_entries[] = $menu_entry;
839
                                                $stufen[] = $stufe;
840
                                        }
841
                                        if ($x_used) foreach ($menu_entries as $i => &$menu_entry) if ($stufen[$i] >= 2) $menu_entry['indent'] += 1;
842
                                        if ($y_used) foreach ($menu_entries as $i => &$menu_entry) if ($stufen[$i] >= 1) $menu_entry['indent'] += 1;
843
                                        if ($z_used) foreach ($menu_entries as $i => &$menu_entry) if ($stufen[$i] >= 0) $menu_entry['indent'] += 1;
844
                                        $json = array_merge($json, $menu_entries);
845
                                }
846
                        }
847
 
848
                        return true;
849
                } else {
1116 daniel-mar 850
                        if ($req_goto === "*") {
635 daniel-mar 851
                                $goto_path = true; // display everything recursively
1116 daniel-mar 852
                        } else if ($req_goto !== "") {
635 daniel-mar 853
                                $goto = $req_goto;
854
                                $path = array();
855
                                while (true) {
856
                                        $path[] = $goto;
977 daniel-mar 857
                                        $objGoto = OIDplusObject::findFitting($goto);
858
                                        if (!$objGoto) break;
859
                                        $objGotoParent = $objGoto->getParent();
860
                                        $goto = $objGotoParent ? $objGotoParent->nodeId() : '';
635 daniel-mar 861
                                        if ($goto == '') continue;
862
                                }
863
 
864
                                $goto_path = array_reverse($path);
865
                        } else {
866
                                $goto_path = null;
867
                        }
868
 
869
                        $objTypesChildren = array();
870
                        foreach (OIDplus::getEnabledObjectTypes() as $ot) {
871
                                $icon = $this->get_treeicon_root($ot);
872
 
873
                                $child = array('id' => $ot::root(),
874
                                               'text' => $ot::objectTypeTitle(),
875
                                               'state' => array("opened" => true),
876
                                               'icon' => $icon,
877
                                               'children' => OIDplus::menuUtils()->tree_populate($ot::root(), $goto_path)
878
                                               );
1122 daniel-mar 879
                                if ($child['icon'] && !file_exists($child['icon'])) $child['icon'] = null; // default icon (folder)
635 daniel-mar 880
                                $objTypesChildren[] = $child;
881
                        }
882
 
883
                        $json[] = array(
884
                                'id' => "oidplus:system",
885
                                'text' => _L('Objects'),
886
                                'state' => array(
887
                                        "opened" => true,
888
                                        // "selected" => true)  // "selected" is buggy:
889
                                        // 1) The select-event will not be triggered upon loading
890
                                        // 2) The nodes directly blow cannot be opened (loading infinite time)
891
                                ),
801 daniel-mar 892
                                'icon' => OIDplus::webpath(__DIR__,OIDplus::PATH_RELATIVE).'img/main_icon16.png',
635 daniel-mar 893
                                'children' => $objTypesChildren
894
                        );
895
 
896
                        return true;
897
                }
898
        }
899
 
1116 daniel-mar 900
        /**
901
         * @param string $request
902
         * @return array|false
903
         */
904
        public function tree_search(string $request) {
635 daniel-mar 905
                $ary = array();
951 daniel-mar 906
                $found_leaf = false;
635 daniel-mar 907
                if ($obj = OIDplusObject::parse($request)) {
951 daniel-mar 908
                        $found_leaf = OIDplusObject::exists($request);
909
                        do {
910
                                if ($obj->userHasReadRights()) {
635 daniel-mar 911
                                        $ary[] = $obj->nodeId();
951 daniel-mar 912
                                }
913
                        } while ($obj = $obj->getParent());
914
                        $ary = array_reverse($ary);
915
                }
916
                if (!$found_leaf) {
952 daniel-mar 917
                        $alternatives = $this->getAlternativesForQuery($request);
951 daniel-mar 918
                        foreach ($alternatives as $alternative) {
919
                                $ary_ = array();
920
                                if ($obj = OIDplusObject::parse($alternative)) {
921
                                        if ($obj->userHasReadRights() && OIDplusObject::exists($alternative)) {
922
                                                do {
923
                                                        $ary_[] = $obj->nodeId();
924
                                                } while ($obj = $obj->getParent());
925
                                                $ary_ = array_reverse($ary_);
926
                                        }
927
                                }
928
                                if (!empty($ary_)) {
929
                                        $ary = $ary_;
930
                                        break;
931
                                }
635 daniel-mar 932
                        }
933
                }
934
                return $ary;
935
        }
936
 
1116 daniel-mar 937
        /**
938
         * @var int
939
         */
635 daniel-mar 940
        private static $crudCounter = 0;
941
 
1116 daniel-mar 942
        /**
1121 daniel-mar 943
         * @param string $parent
944
         * @return string
1116 daniel-mar 945
         * @throws OIDplusConfigInitializationException
946
         * @throws OIDplusException
947
         */
1121 daniel-mar 948
        protected static function showCrud(string $parent='oid:'): string {
635 daniel-mar 949
                $items_total = 0;
950
                $items_hidden = 0;
951
 
952
                $objParent = OIDplusObject::parse($parent);
1121 daniel-mar 953
                if (!$objParent) return '';
635 daniel-mar 954
                $parentNS = $objParent::ns();
955
 
956
                // http://www.oid-info.com/cgi-bin/display?a=list-by-category&category=Not%20allocating%20identifiers
957
                $no_asn1 = array(
958
                        'oid:1.3.6.1.4.1',
959
                        'oid:1.3.6.1.4.1.37476.9000',
960
                        'oid:1.3.6.1.4.1.37553.8.8',
961
                        'oid:2.16.276.1',
719 daniel-mar 962
                        //'oid:2.25', // according to Olivier, it is OK that UUID owners define their own ASN.1 ID, since the ASN.1 ID is not required to be unique
963
                        //'oid:1.2.840.113556.1.8000.2554' // Adhoc (GUID/UUID-based) customer use. It is probably the same case as the UUID OIDs, after all, these are UUIDs, too.
635 daniel-mar 964
                );
965
 
966
                // http://www.oid-info.com/cgi-bin/display?a=list-by-category&category=Not%20allocating%20Unicode%20labels
967
                $no_iri = array(
968
                        'oid:1.2.250.1',
969
                        'oid:1.3.6.1.4.1',
970
                        'oid:1.3.6.1.4.1.37476.9000',
971
                        'oid:1.3.6.1.4.1.37553.8.8',
972
                        'oid:2.16.276.1',
973
                        'oid:2.25'
974
                );
975
 
976
                $accepts_asn1 = ($parentNS == 'oid') && (!in_array($objParent->nodeId(), $no_asn1)) && (!is_uuid_oid($objParent->nodeId(),true));
977
                $accepts_iri  = ($parentNS == 'oid') && (!in_array($objParent->nodeId(), $no_iri)) && (!is_uuid_oid($objParent->nodeId(),true));
978
 
979
                $result = OIDplus::db()->query("select o.*, r.ra_name " .
980
                                               "from ###objects o " .
981
                                               "left join ###ra r on r.email = o.ra_email " .
982
                                               "where parent = ? " .
983
                                               "order by ".OIDplus::db()->natOrder('id'), array($parent));
984
 
693 daniel-mar 985
                $rows = array();
986
                while ($row = $result->fetch_object()) {
987
                        $obj = OIDplusObject::parse($row->id);
1121 daniel-mar 988
                        if ($obj) $rows[] = array($obj,$row);
693 daniel-mar 989
                }
990
 
991
                $enable_weid_presentation = OIDplus::config()->getValue('oid_grid_show_weid');
992
 
1116 daniel-mar 993
                $output  = '<div class="container box"><div id="suboid_table" class="table-responsive">';
635 daniel-mar 994
                $output .= '<table class="table table-bordered table-striped">';
995
                $output .= '    <tr>';
996
                $output .= '         <th>'._L('ID').(($parentNS == 'gs1') ? ' '._L('(without check digit)') : '').'</th>';
692 daniel-mar 997
                if ($enable_weid_presentation && ($parentNS == 'oid') && !$objParent->isRoot()) {
693 daniel-mar 998
                        $output .= '         <th><abbr title="'._L('Binary-to-text encoding used for WEIDs').'">'._L('Base36').'</abbr></th>';
999
                }
635 daniel-mar 1000
                if ($parentNS == 'oid') {
1001
                        if ($accepts_asn1) $output .= '      <th>'._L('ASN.1 IDs (comma sep.)').'</th>';
1002
                        if ($accepts_iri)  $output .= '      <th>'._L('IRI IDs (comma sep.)').'</th>';
1003
                }
1004
                $output .= '         <th>'._L('RA').'</th>';
1005
                $output .= '         <th>'._L('Comment').'</th>';
1006
                if ($objParent->userHasWriteRights()) {
1007
                        $output .= '         <th>'._L('Hide').'</th>';
1008
                        $output .= '         <th>'._L('Update').'</th>';
1009
                        $output .= '         <th>'._L('Delete').'</th>';
1010
                }
1011
                $output .= '         <th>'._L('Created').'</th>';
1012
                $output .= '         <th>'._L('Updated').'</th>';
1013
                $output .= '    </tr>';
1014
 
1015
                foreach ($rows as list($obj,$row)) {
1016
                        $items_total++;
1017
                        if (!$obj->userHasReadRights()) {
1018
                                $items_hidden++;
1019
                                continue;
1020
                        }
1021
 
1022
                        $show_id = $obj->crudShowId($objParent);
1023
 
1024
                        $asn1ids = array();
1025
                        $res2 = OIDplus::db()->query("select name from ###asn1id where oid = ? order by lfd", array($row->id));
1026
                        while ($row2 = $res2->fetch_array()) {
1027
                                $asn1ids[] = $row2['name'];
1028
                        }
1029
 
1030
                        $iris = array();
1031
                        $res2 = OIDplus::db()->query("select name from ###iri where oid = ? order by lfd", array($row->id));
1032
                        while ($row2 = $res2->fetch_array()) {
1033
                                $iris[] = $row2['name'];
1034
                        }
1035
 
1058 daniel-mar 1036
                        $date_created = is_null($row->created) || (explode(' ', $row->created)[0] == '0000-00-00') ? '' : explode(' ', $row->created)[0];
1037
                        $date_updated = is_null($row->updated) || (explode(' ', $row->updated)[0] == '0000-00-00') ? '' : explode(' ', $row->updated)[0];
635 daniel-mar 1038
 
1039
                        $output .= '<tr>';
693 daniel-mar 1040
                        $output .= '     <td><a href="?goto='.urlencode($row->id).'" onclick="openAndSelectNode('.js_escape($row->id).', '.js_escape($parent).'); return false;">'.htmlentities($show_id).'</a>';
1041
                        if ($enable_weid_presentation && ($parentNS == 'oid') && $objParent->isRoot()) {
1042
                                // To save space horizontal space, the WEIDs were written below the OIDs
1116 daniel-mar 1043
                                assert($obj instanceof OIDplusOid); //assert(get_class($obj) === "ViaThinkSoft\OIDplus\OIDplusOid");
693 daniel-mar 1044
                                $output .= '<br>'.$obj->getWeidNotation(true);
1045
                        }
1046
                        $output .= '</td>';
1047
                        if ($enable_weid_presentation && ($parentNS == 'oid') && !$objParent->isRoot()) {
1116 daniel-mar 1048
                                assert($obj instanceof OIDplusOid); //assert(get_class($obj) === "ViaThinkSoft\OIDplus\OIDplusOid");
693 daniel-mar 1049
                                $output .= '    <td>'.htmlentities($obj->weidArc()).'</td>';
1050
                        }
635 daniel-mar 1051
                        if ($objParent->userHasWriteRights()) {
1052
                                if ($parentNS == 'oid') {
1053
                                        if ($accepts_asn1) $output .= '     <td><input type="text" id="asn1ids_'.$row->id.'" value="'.implode(', ', $asn1ids).'"></td>';
1054
                                        if ($accepts_iri)  $output .= '     <td><input type="text" id="iris_'.$row->id.'" value="'.implode(', ', $iris).'"></td>';
1055
                                }
1056
                                $output .= '     <td><input type="text" id="ra_email_'.$row->id.'" value="'.htmlentities($row->ra_email).'"></td>';
1057
                                $output .= '     <td><input type="text" id="comment_'.$row->id.'" value="'.htmlentities($row->comment).'"></td>';
1058
                                $output .= '     <td><input type="checkbox" id="hide_'.$row->id.'" '.($row->confidential ? 'checked' : '').'></td>';
1059
                                $output .= '     <td><button type="button" name="update_'.$row->id.'" id="update_'.$row->id.'" class="btn btn-success btn-xs update" onclick="OIDplusPagePublicObjects.crudActionUpdate('.js_escape($row->id).', '.js_escape($parent).')">'._L('Update').'</button></td>';
1060
                                $output .= '     <td><button type="button" name="delete_'.$row->id.'" id="delete_'.$row->id.'" class="btn btn-danger btn-xs delete" onclick="OIDplusPagePublicObjects.crudActionDelete('.js_escape($row->id).', '.js_escape($parent).')">'._L('Delete').'</button></td>';
1061
                                $output .= '     <td>'.$date_created.'</td>';
1062
                                $output .= '     <td>'.$date_updated.'</td>';
1063
                        } else {
1064
                                if ($parentNS == 'oid') {
693 daniel-mar 1065
                                        if ($asn1ids == '') $asn1ids = '<i>'._L('(none)').'</i>';
1066
                                        if ($iris == '') $iris = '<i>'._L('(none)').'</i>';
635 daniel-mar 1067
                                        $asn1ids_ext = array();
1068
                                        foreach ($asn1ids as $asn1id) {
1069
                                                $asn1ids_ext[] = '<a href="?goto='.urlencode($row->id).'" onclick="openAndSelectNode('.js_escape($row->id).', '.js_escape($parent).'); return false;">'.$asn1id.'</a>';
1070
                                        }
1071
                                        if ($accepts_asn1) $output .= '     <td>'.implode(', ', $asn1ids_ext).'</td>';
1072
                                        if ($accepts_iri)  $output .= '     <td>'.implode(', ', $iris).'</td>';
1073
                                }
1074
                                $output .= '     <td><a '.OIDplus::gui()->link('oidplus:rainfo$'.str_replace('@','&',$row->ra_email)).'>'.htmlentities(empty($row->ra_name) ? str_replace('@','&',$row->ra_email) : $row->ra_name).'</a></td>';
1075
                                $output .= '     <td>'.htmlentities($row->comment).'</td>';
1076
                                $output .= '     <td>'.$date_created.'</td>';
1077
                                $output .= '     <td>'.$date_updated.'</td>';
1078
                        }
1079
                        $output .= '</tr>';
1080
                }
1081
 
1121 daniel-mar 1082
                $parent_ra_email = $objParent->getRaMail() ;
693 daniel-mar 1083
 
692 daniel-mar 1084
                // "Create OID" row
635 daniel-mar 1085
                if ($objParent->userHasWriteRights()) {
1086
                        $output .= '<tr>';
1121 daniel-mar 1087
                        $prefix = $objParent->crudInsertPrefix();
693 daniel-mar 1088
 
1121 daniel-mar 1089
                        $suffix = $objParent->crudInsertSuffix();
693 daniel-mar 1090
                        foreach (OIDplus::getObjectTypePlugins() as $plugin) {
1091
                                if (($plugin::getObjectTypeClassName()::ns() == $parentNS) && $plugin->implementsFeature('1.3.6.1.4.1.37476.2.5.2.3.6')) {
1116 daniel-mar 1092
                                        $suffix .= $plugin->gridGeneratorLinks($objParent); /** @phpstan-ignore-line */
693 daniel-mar 1093
                                }
1094
                        }
1095
 
1096
                        if ($parentNS == 'guid') {
695 daniel-mar 1097
                                $output .= '     <td>'.$prefix.' <input type="text" id="id" value="" style="width:100%;min-width:275px">'.$suffix.'</td>';
693 daniel-mar 1098
                        } else if ($parentNS == 'oid') {
635 daniel-mar 1099
                                // TODO: Idea: Give a class name, e.g. "OID" and then with a oid-specific CSS make the width individual. So, every plugin has more control over the appearance and widths of the input fields
693 daniel-mar 1100
                                if ($objParent->nodeId() === 'oid:2.25') {
695 daniel-mar 1101
                                        $output .= '     <td>'.$prefix.' <input type="text" id="id" value="" style="width:100%;min-width:345px">'.$suffix.'</td>';
693 daniel-mar 1102
                                        if ($enable_weid_presentation) $output .= '     <td>&nbsp;</td>'; // For UUID-OIDs, you must generate a valid one. Don't be tempted to create one using the Base36 input!
1103
                                } else if ($objParent->isRoot()) {
695 daniel-mar 1104
                                        $output .= '     <td>'.$prefix.' <input type="text" id="id" value="" style="width:100%;min-width:345px">'.$suffix.'</td>';
693 daniel-mar 1105
                                        if ($enable_weid_presentation) $output .= ''; // WEID-editor not available for root nodes at the moment. For the moment you need to enter the OID (TODO: Create JavaScript WEID encoder/decoder)
635 daniel-mar 1106
                                } else {
693 daniel-mar 1107
                                        if ($enable_weid_presentation) {
695 daniel-mar 1108
                                                $output .= '     <td>'.$prefix.' <input oninput="OIDplusPagePublicObjects.frdl_oidid_change()" type="text" id="id" value="" style="width:100%;min-width:100px">'.$suffix.'</td>';
693 daniel-mar 1109
                                                $output .= '     <td><input type="text" name="weid" id="weid" value="" oninput="OIDplusPagePublicObjects.frdl_weid_change()" style="width:100%;min-width:100px"></td>';
1110
                                        } else {
695 daniel-mar 1111
                                                $output .= '     <td>'.$prefix.' <input type="text" id="id" value="" style="width:100%;min-width:100px">'.$suffix.'</td>';
693 daniel-mar 1112
                                        }
635 daniel-mar 1113
                                }
1114
                        } else {
695 daniel-mar 1115
                                $output .= '     <td>'.$prefix.' <input type="text" id="id" value="" style="width:100%;min-width:100px">'.$suffix.'</td>';
635 daniel-mar 1116
                        }
1117
                        if ($accepts_asn1) $output .= '     <td><input type="text" id="asn1ids" value=""></td>';
1118
                        if ($accepts_iri)  $output .= '     <td><input type="text" id="iris" value=""></td>';
1119
                        $output .= '     <td><input type="text" id="ra_email" value="'.htmlentities($parent_ra_email).'"></td>';
1120
                        $output .= '     <td><input type="text" id="comment" value=""></td>';
1121
                        $output .= '     <td><input type="checkbox" id="hide"></td>';
1122
                        $output .= '     <td><button type="button" name="insert" id="insert" class="btn btn-success btn-xs update" onclick="OIDplusPagePublicObjects.crudActionInsert('.js_escape($parent).')">'._L('Insert').'</button></td>';
1123
                        $output .= '     <td></td>';
1124
                        $output .= '     <td></td>';
1125
                        $output .= '     <td></td>';
1126
                        $output .= '</tr>';
1127
                } else {
1128
                        if ($items_total-$items_hidden == 0) {
1129
                                $cols = ($parentNS == 'oid') ? 7 : 5;
692 daniel-mar 1130
                                if ($enable_weid_presentation && ($parentNS == 'oid') && !$objParent->isRoot()) {
1131
                                        $cols++;
693 daniel-mar 1132
                                }
635 daniel-mar 1133
                                $output .= '<tr><td colspan="'.$cols.'">'._L('No items available').'</td></tr>';
1134
                        }
1135
                }
1136
 
1137
                $output .= '</table>';
1138
                $output .= '</div></div>';
1139
 
1140
                if ($items_hidden == 1) {
1141
                        $output .= '<p>'._L('One item is hidden. Please <a %1>log in</a> to see it.',$items_hidden,OIDplus::gui()->link('oidplus:login')).'</p>';
1142
                } else if ($items_hidden > 1) {
1143
                        $output .= '<p>'._L('%1 items are hidden. Please <a %2>log in</a> to see them.',$items_hidden,OIDplus::gui()->link('oidplus:login')).'</p>';
1144
                }
1145
 
1146
                return $output;
1147
        }
1148
 
1116 daniel-mar 1149
        /**
1150
         * @param $html
1151
         * @return array|string|string[]|null
1152
         */
635 daniel-mar 1153
        protected static function objDescription($html) {
1154
                // We allow HTML, but no hacking
1155
                $html = anti_xss($html);
1156
 
1157
                return trim_br($html);
1158
        }
1159
 
1116 daniel-mar 1160
        /**
1161
         * 'quickbars' added 11 July 2019: Disabled because of two problems:
1162
         *                                 1. When you load TinyMCE via AJAX using the left menu, the quickbar is immediately shown, even if TinyMCE does not have the focus
1163
         *                                 2. When you load a page without TinyMCE using the left menu, the quickbar is still visible, although there is no edit
1164
         * 'colorpicker', 'textcolor' and 'contextmenu' added in 07 April 2020, because it is built in in the core.
1165
         * 'importcss' added 17 September 2020, because it breaks the "Format/Style" dropdown box ("styleselect" toolbar)
1166
         * 'legacyoutput' added 24 September 2021, because it is declared as deprecated
1167
         * 'spellchecker' added 6 October 2021, because it is declared as deprecated and marked for removal in TinyMCE 6.0
1168
         * 'imagetools' and 'toc' added 23 February 2022, because they are declared as deprecated and marked for removal in TinyMCE 6.0 ("moving to premium")
1169
         * @var string[]
1170
         */
753 daniel-mar 1171
        public static $exclude_tinymce_plugins = array('fullpage', 'bbcode', 'quickbars', 'colorpicker', 'textcolor', 'contextmenu', 'importcss', 'legacyoutput', 'spellchecker', 'imagetools', 'toc');
635 daniel-mar 1172
 
1116 daniel-mar 1173
        /**
1174
         * @param $name
1175
         * @param $content
1176
         * @return string
1177
         * @throws OIDplusConfigInitializationException
1178
         * @throws OIDplusException
1179
         */
635 daniel-mar 1180
        protected static function showMCE($name, $content) {
1181
                $mce_plugins = array();
1182
                foreach (glob(OIDplus::localpath().'vendor/tinymce/tinymce/plugins/*') as $m) { // */
1183
                        $mce_plugins[] = basename($m);
1184
                }
1185
 
1186
                foreach (self::$exclude_tinymce_plugins as $exclude) {
1187
                        $index = array_search($exclude, $mce_plugins);
1188
                        if ($index !== false) unset($mce_plugins[$index]);
1189
                }
1190
 
1191
                $oidplusLang = OIDplus::getCurrentLang();
1192
 
1193
                $langCandidates = array(
1194
                        strtolower(substr($oidplusLang,0,2)).'_'.strtoupper(substr($oidplusLang,2,2)), // de_DE
1195
                        strtolower(substr($oidplusLang,0,2)) // de
1196
                );
1197
                $tinyMCELang = '';
1198
                foreach ($langCandidates as $candidate) {
1199
                        if (file_exists(OIDplus::localpath().'vendor/tweeb/tinymce-i18n/langs/'.$candidate.'.js')) {
1200
                                $tinyMCELang = $candidate;
1201
                                break;
1202
                        }
1203
                }
1204
 
1205
                $out = '<script>
1206
                                tinymce.EditorManager.baseURL = "vendor/tinymce/tinymce";
1207
                                tinymce.init({
801 daniel-mar 1208
                                        document_base_url: "'.OIDplus::webpath(null,OIDplus::PATH_ABSOLUTE_CANONICAL).'",
635 daniel-mar 1209
                                        selector: "#'.$name.'",
1210
                                        height: 200,
1211
                                        statusbar: false,
1212
//                                      menubar:false,
1213
//                                      toolbar: "undo redo | styleselect | bold italic | alignleft aligncenter alignright alignjustify | bullist numlist outdent indent | table | fontsizeselect",
1214
                                        toolbar: "undo redo | styleselect | bold italic underline forecolor | bullist numlist | outdent indent | table | fontsizeselect",
1215
                                        plugins: "'.implode(' ', $mce_plugins).'",
1216
                                        mobile: {
1217
                                                theme: "mobile",
1218
                                                toolbar: "undo redo | styleselect | bold italic underline forecolor | bullist numlist | outdent indent | table | fontsizeselect",
1219
                                                plugins: "'.implode(' ', $mce_plugins).'"
1220
                                        }
1221
                                        '.($tinyMCELang == '' ? '' : ', language : "'.$tinyMCELang.'"').'
801 daniel-mar 1222
                                        '.($tinyMCELang == '' ? '' : ', language_url : "'.OIDplus::webpath(null,OIDplus::PATH_ABSOLUTE_CANONICAL).'vendor/tweeb/tinymce-i18n/langs/'.$tinyMCELang.'.js"').'
635 daniel-mar 1223
                                });
1224
 
1225
                                pageChangeRequestCallbacks.push([OIDplusPagePublicObjects.cbQueryTinyMCE, "#'.$name.'"]);
1226
                                pageChangeCallbacks.push([OIDplusPagePublicObjects.cbRemoveTinyMCE, "#'.$name.'"]);
1227
                        </script>';
1228
 
1229
                $content = htmlentities($content); // For some reason, if we want to display the text "<xyz>" in TinyMCE, we need to double-encode things! &lt; will not be accepted, we need &amp;lt; ... why?
1230
 
1231
                $out .= '<textarea name="'.htmlentities($name).'" id="'.htmlentities($name).'">'.trim($content).'</textarea><br>';
1232
 
1233
                return $out;
1234
        }
1235
 
1116 daniel-mar 1236
        /**
1237
         * @param string $id
1238
         * @return bool
1239
         */
1240
        public function implementsFeature(string $id): bool {
635 daniel-mar 1241
                if (strtolower($id) == '1.3.6.1.4.1.37476.2.5.2.3.1') return true; // oobeEntry, oobeRequested()
952 daniel-mar 1242
                // Important: Do NOT 1.3.6.1.4.1.37476.2.5.2.3.7 because our getAlternativesForQuery() is the one that calls others!
1000 daniel-mar 1243
                if (strtolower($id) == '1.3.6.1.4.1.37476.2.5.2.3.8') return true; // getNotifications()
635 daniel-mar 1244
                return false;
1245
        }
1246
 
1116 daniel-mar 1247
        /**
1248
         * Implements interface 1.3.6.1.4.1.37476.2.5.2.3.1
1249
         * @return bool
1250
         * @throws OIDplusException
1251
         */
635 daniel-mar 1252
        public function oobeRequested(): bool {
1253
                return OIDplus::config()->getValue('oobe_objects_done') == '0';
1254
        }
1255
 
1116 daniel-mar 1256
        /**
1257
         * Implements interface 1.3.6.1.4.1.37476.2.5.2.3.1
1125 daniel-mar 1258
         * @param int $step
1259
         * @param bool $do_edits
1260
         * @param bool $errors_happened
1116 daniel-mar 1261
         * @return void
1262
         */
1125 daniel-mar 1263
        public function oobeEntry(int $step, bool $do_edits, bool &$errors_happened)/*: void*/ {
1055 daniel-mar 1264
                echo '<h2>'._L('Step %1: Enable/Disable object type plugins',$step).'</h2>';
635 daniel-mar 1265
                echo '<p>'._L('Which object types do you want to manage using OIDplus?').'</p>';
1266
 
1267
                $enabled_ary = array();
1268
 
1269
                foreach (OIDplus::getEnabledObjectTypes() as $ot) {
1270
                        echo '<input type="checkbox" name="enable_ot_'.$ot::ns().'" id="enable_ot_'.$ot::ns().'"';
1033 daniel-mar 1271
                        if (isset($_POST['sent'])) {
1272
                                if (isset($_POST['enable_ot_'.$ot::ns()])) {
635 daniel-mar 1273
                                        echo ' checked';
1274
                                        $enabled_ary[] = $ot::ns();
1275
                                }
1276
                        } else {
1277
                                echo ' checked';
1278
                        }
1279
                        echo '> <label for="enable_ot_'.$ot::ns().'">'.htmlentities($ot::objectTypeTitle()).'</label><br>';
1280
                }
1281
 
1282
                foreach (OIDplus::getDisabledObjectTypes() as $ot) {
1283
                        echo '<input type="checkbox" name="enable_ot_'.$ot::ns().'" id="enable_ot_'.$ot::ns().'"';
1033 daniel-mar 1284
                        if (isset($_POST['sent'])) {
1285
                                if (isset($_POST['enable_ot_'.$ot::ns()])) {
635 daniel-mar 1286
                                        echo ' checked';
1287
                                        $enabled_ary[] = $ot::ns();
1288
                                }
1289
                        } else {
1290
                                echo ''; // <-- difference
1291
                        }
1292
                        echo '> <label for="enable_ot_'.$ot::ns().'">'.htmlentities($ot::objectTypeTitle()).'</label><br>';
1293
                }
1294
 
1295
                $msg = '';
1296
                if ($do_edits) {
1297
                        try {
1298
                                OIDplus::config()->setValue('objecttypes_enabled', implode(';', $enabled_ary));
1299
                                OIDplus::config()->setValue('oobe_objects_done', '1');
1050 daniel-mar 1300
                        } catch (\Exception $e) {
635 daniel-mar 1301
                                $msg = $e->getMessage();
1302
                                $errors_happened = true;
1303
                        }
1304
                }
1305
 
1306
                echo ' <font color="red"><b>'.$msg.'</b></font>';
1307
        }
1308
 
1116 daniel-mar 1309
        /**
1310
         * Implements interface 1.3.6.1.4.1.37476.2.5.2.3.8
1311
         * @param $user
1312
         * @return array
1313
         * @throws OIDplusException
1314
         */
1000 daniel-mar 1315
        public function getNotifications($user=null): array {
1316
                $notifications = array();
1012 daniel-mar 1317
                $res = OIDplus::db()->query("select id, title from ###objects order by ".OIDplus::db()->natOrder('id'));
1000 daniel-mar 1318
                if ($res->any()) {
1319
                        $is_admin_logged_in = OIDplus::authUtils()->isAdminLoggedIn(); // run just once, for performance
1320
                        while ($row = $res->fetch_array()) {
1321
                                if (empty($row['title'])) {
1322
                                        if ($user === 'admin') {
1323
                                                $accept = $is_admin_logged_in;
1324
                                        } else {
1325
                                                $accept = false;
1326
                                                if ($obj = OIDplusObject::parse($row['id'])) {
1327
                                                        if ($obj->userHasWriteRights($user)) {
1328
                                                                $accept = true;
1329
                                                        }
1330
                                                }
1331
                                        }
1332
 
1333
                                        if ($accept) {
1334
                                                $notifications[] = array('WARN', _L('Object %1 has no title.', '<a '.OIDplus::gui()->link($row['id']).'>'.$row['id'].'</a>'));
1335
                                        }
1336
                                }
1337
                        }
1338
                }
1339
                return $notifications;
1340
        }
1341
 
693 daniel-mar 1342
}