Subversion Repositories oidplus

Rev

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

Rev Author Line No. Line
104 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
 
256 daniel-mar 20
class OIDplusPagePublicObjects extends OIDplusPagePluginPublic {
104 daniel-mar 21
 
22
        public function action(&$handled) {
256 daniel-mar 23
 
24
                // Action:     Delete
25
                // Method:     POST
26
                // Parameters: id
27
                // Outputs:    Text
28
                if (isset($_POST["action"]) && ($_POST["action"] == "Delete")) {
29
                        $handled = true;
30
 
31
                        $id = $_POST['id'];
32
                        $obj = OIDplusObject::parse($id);
33
                        if ($obj === null) throw new OIDplusException("DELETE action failed because object '$id' cannot be parsed!");
34
 
261 daniel-mar 35
                        if (OIDplus::db()->query("select id from ###objects where id = ?", array($id))->num_rows() == 0) {
256 daniel-mar 36
                                throw new OIDplusException("Object '$id' does not exist");
37
                        }
38
 
39
                        // Check if permitted
40
                        if (!$obj->userHasParentalWriteRights()) throw new OIDplusException('Authentification error. Please log in as the superior RA to delete this OID.');
41
 
288 daniel-mar 42
                        OIDplus::logger()->log("[WARN]OID($id)+[?WARN/!OK]SUPOIDRA($id)?/[?INFO/!OK]A?", "Object '$id' (recursively) deleted");
43
                        OIDplus::logger()->log("[CRIT]OIDRA($id)!", "Lost ownership of object '$id' because it was deleted");
256 daniel-mar 44
 
45
                        if ($parentObj = $obj->getParent()) {
288 daniel-mar 46
                                $parent_oid = $parentObj->nodeId();
47
                                OIDplus::logger()->log("[WARN]OID($parent_oid)", "Object '$id' (recursively) deleted");
256 daniel-mar 48
                        }
49
 
50
                        // Delete object
261 daniel-mar 51
                        OIDplus::db()->query("delete from ###objects where id = ?", array($id));
256 daniel-mar 52
 
53
                        // Delete orphan stuff
54
                        foreach (OIDplus::getEnabledObjectTypes() as $ot) {
55
                                do {
261 daniel-mar 56
                                        $res = OIDplus::db()->query("select tchild.id from ###objects tchild " .
57
                                                                    "left join ###objects tparent on tparent.id = tchild.parent " .
256 daniel-mar 58
                                                                    "where tchild.parent <> ? and tchild.id like ? and tparent.id is null;", array($ot::root(), $ot::root().'%'));
59
                                        if ($res->num_rows() == 0) break;
60
 
61
                                        while ($row = $res->fetch_array()) {
62
                                                $id_to_delete = $row['id'];
288 daniel-mar 63
                                                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");
261 daniel-mar 64
                                                OIDplus::db()->query("delete from ###objects where id = ?", array($id_to_delete));
256 daniel-mar 65
                                        }
66
                                } while (true);
67
                        }
261 daniel-mar 68
                        OIDplus::db()->query("delete from ###asn1id where well_known = '0' and oid not in (select id from ###objects where id like 'oid:%')");
69
                        OIDplus::db()->query("delete from ###iri    where well_known = '0' and oid not in (select id from ###objects where id like 'oid:%')");
256 daniel-mar 70
 
71
                        echo json_encode(array("status" => 0));
72
                }
73
 
74
                // Action:     Update
75
                // Method:     POST
76
                // Parameters: id, ra_email, comment, iris, asn1ids, confidential
77
                // Outputs:    Text
78
                if (isset($_POST["action"]) && ($_POST["action"] == "Update")) {
79
                        $handled = true;
80
 
81
                        $id = $_POST['id'];
82
                        $obj = OIDplusObject::parse($id);
83
                        if ($obj === null) throw new OIDplusException("UPDATE action failed because object '$id' cannot be parsed!");
84
 
261 daniel-mar 85
                        if (OIDplus::db()->query("select id from ###objects where id = ?", array($id))->num_rows() == 0) {
256 daniel-mar 86
                                throw new OIDplusException("Object '$id' does not exist");
87
                        }
88
 
89
                        // Check if permitted
90
                        if (!$obj->userHasParentalWriteRights()) throw new OIDplusException('Authentification error. Please log in as the superior RA to update this OID.');
91
 
92
                        // Validate RA email address
93
                        $new_ra = $_POST['ra_email'];
94
                        if (!empty($new_ra) && !OIDplus::mailUtils()->validMailAddress($new_ra)) {
95
                                throw new OIDplusException('Invalid RA email address');
96
                        }
97
 
98
                        // First, do a simulation for ASN.1 IDs and IRIs to check if there are any problems (then an Exception will be thrown)
99
                        if ($obj::ns() == 'oid') {
100
                                $ids = ($_POST['iris'] == '') ? array() : explode(',',$_POST['iris']);
101
                                $ids = array_map('trim',$ids);
102
                                $obj->replaceIris($ids, true);
103
 
104
                                $ids = ($_POST['asn1ids'] == '') ? array() : explode(',',$_POST['asn1ids']);
105
                                $ids = array_map('trim',$ids);
106
                                $obj->replaceAsn1Ids($ids, true);
107
                        }
108
 
109
                        // Change RA recursively
261 daniel-mar 110
                        $res = OIDplus::db()->query("select ra_email from ###objects where id = ?", array($id));
256 daniel-mar 111
                        if ($row = $res->fetch_array()) {
112
                                $current_ra = $row['ra_email'];
113
                                if ($new_ra != $current_ra) {
288 daniel-mar 114
                                        OIDplus::logger()->log("[INFO]OID($id)+[?INFO/!OK]SUPOIDRA($id)?/[?INFO/!OK]A?", "RA of object '$id' changed from '$current_ra' to '$new_ra'");
115
                                        OIDplus::logger()->log("[WARN]RA($current_ra)!",           "Lost ownership of object '$id' due to RA transfer of superior RA / admin.");
116
                                        OIDplus::logger()->log("[INFO]RA($new_ra)!",               "Gained ownership of object '$id' due to RA transfer of superior RA / admin.");
256 daniel-mar 117
                                        if ($parentObj = $obj->getParent()) {
288 daniel-mar 118
                                                $parent_oid = $parentObj->nodeId();
119
                                                OIDplus::logger()->log("[INFO]OID($parent_oid)", "RA of object '$id' changed from '$current_ra' to '$new_ra'");
256 daniel-mar 120
                                        }
121
                                        _ra_change_rec($id, $current_ra, $new_ra); // Inherited RAs rekursiv mitändern
122
                                }
123
                        }
124
 
125
                        // Log if confidentially flag was changed
288 daniel-mar 126
                        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!
256 daniel-mar 127
                        if ($parentObj = $obj->getParent()) {
288 daniel-mar 128
                                $parent_oid = $parentObj->nodeId();
129
                                OIDplus::logger()->log("[INFO]OID($parent_oid)", "Identifiers/Confidential flag of object '$id' updated"); // TODO: Check if they were ACTUALLY updated!
256 daniel-mar 130
                        }
131
 
132
                        // Replace ASN.1 IDs und IRIs
133
                        if ($obj::ns() == 'oid') {
134
                                $ids = ($_POST['iris'] == '') ? array() : explode(',',$_POST['iris']);
135
                                $ids = array_map('trim',$ids);
136
                                $obj->replaceIris($ids, false);
137
 
138
                                $ids = ($_POST['asn1ids'] == '') ? array() : explode(',',$_POST['asn1ids']);
139
                                $ids = array_map('trim',$ids);
140
                                $obj->replaceAsn1Ids($ids, false);
141
 
142
                                // TODO: Check if any identifiers have been actually changed,
143
                                // and log it to OID($id), OID($parent), ... (see above)
144
                        }
145
 
146
                        $confidential = $_POST['confidential'] == 'true';
147
                        $comment = $_POST['comment'];
264 daniel-mar 148
                        OIDplus::db()->query("UPDATE ###objects SET confidential = ?, comment = ?, updated = ".OIDplus::db()->sqlDate()." WHERE id = ?", array($confidential, $comment, $id));
256 daniel-mar 149
 
150
                        $status = 0;
151
 
152
                        if (!empty($new_ra)) {
261 daniel-mar 153
                                $res = OIDplus::db()->query("select ra_name from ###ra where email = ?", array($new_ra));
256 daniel-mar 154
                                if ($res->num_rows() == 0) $status = class_exists('OIDplusPageRaInvite') && OIDplus::config()->getValue('ra_invitation_enabled') ? 1 : 2;
155
                        }
156
 
157
                        echo json_encode(array("status" => $status));
158
                }
159
 
160
                // Action:     Update2
161
                // Method:     POST
162
                // Parameters: id, title, description
163
                // Outputs:    Text
164
                if (isset($_POST["action"]) && ($_POST["action"] == "Update2")) {
165
                        $handled = true;
166
 
167
                        $id = $_POST['id'];
168
                        $obj = OIDplusObject::parse($id);
169
                        if ($obj === null) throw new OIDplusException("UPDATE2 action failed because object '$id' cannot be parsed!");
170
 
261 daniel-mar 171
                        if (OIDplus::db()->query("select id from ###objects where id = ?", array($id))->num_rows() == 0) {
256 daniel-mar 172
                                throw new OIDplusException("Object '$id' does not exist");
173
                        }
174
 
175
                        // Check if allowed
176
                        if (!$obj->userHasWriteRights()) throw new OIDplusException('Authentification error. Please log in as the RA to update this OID.');
177
 
288 daniel-mar 178
                        OIDplus::logger()->log("[INFO]OID($id)+[?INFO/!OK]OIDRA($id)?/[?INFO/!OK]A?", "Title/Description of object '$id' updated");
256 daniel-mar 179
 
264 daniel-mar 180
                        OIDplus::db()->query("UPDATE ###objects SET title = ?, description = ?, updated = ".OIDplus::db()->sqlDate()." WHERE id = ?", array($_POST['title'], $_POST['description'], $id));
256 daniel-mar 181
 
182
                        echo json_encode(array("status" => 0));
183
                }
184
 
185
                // Action:     Insert
186
                // Method:     POST
187
                // Parameters: parent, id, ra_email, confidential, iris, asn1ids
188
                // Outputs:    Text
189
                if (isset($_POST["action"]) && ($_POST["action"] == "Insert")) {
190
                        $handled = true;
191
 
192
                        // Validated are: ID, ra email, asn1 ids, iri ids
193
 
194
                        // Check if you have write rights on the parent (to create a new object)
195
                        $objParent = OIDplusObject::parse($_POST['parent']);
196
                        if ($objParent === null) throw new OIDplusException("INSERT action failed because parent object '".$_POST['parent']."' cannot be parsed!");
197
 
261 daniel-mar 198
                        if (!$objParent::root() && (OIDplus::db()->query("select id from ###objects where id = ?", array($objParent->nodeId()))->num_rows() == 0)) {
256 daniel-mar 199
                                throw new OIDplusException("Parent object '".($objParent->nodeId())."' does not exist");
200
                        }
201
 
202
                        if (!$objParent->userHasWriteRights()) throw new OIDplusException('Authentification error. Please log in as the correct RA to insert an OID at this arc.');
203
 
204
                        // Check if the ID is valid
205
                        if ($_POST['id'] == '') throw new OIDplusException('ID may not be empty');
206
 
207
                        // Determine absolute OID name
208
                        // Note: At addString() and parse(), the syntax of the ID will be checked
209
                        $id = $objParent->addString($_POST['id']);
210
 
211
                        // Check, if the OID exists
261 daniel-mar 212
                        $test = OIDplus::db()->query("select id from ###objects where id = ?", array($id));
256 daniel-mar 213
                        if ($test->num_rows() >= 1) {
214
                                throw new OIDplusException("Object $id already exists!");
215
                        }
216
 
217
                        $obj = OIDplusObject::parse($id);
218
                        if ($obj === null) throw new OIDplusException("INSERT action failed because object '$id' cannot be parsed!");
219
 
220
                        // First simulate if there are any problems of ASN.1 IDs und IRIs
221
                        if ($obj::ns() == 'oid') {
222
                                $ids = ($_POST['iris'] == '') ? array() : explode(',',$_POST['iris']);
223
                                $ids = array_map('trim',$ids);
224
                                $obj->replaceAsn1Ids($ids, true);
225
 
226
                                $ids = ($_POST['asn1ids'] == '') ? array() : explode(',',$_POST['asn1ids']);
227
                                $ids = array_map('trim',$ids);
228
                                $obj->replaceIris($ids, true);
229
                        }
230
 
231
                        // Apply superior RA change
232
                        $parent = $_POST['parent'];
233
                        $ra_email = $_POST['ra_email'];
234
                        if (!empty($ra_email) && !OIDplus::mailUtils()->validMailAddress($ra_email)) {
235
                                throw new OIDplusException('Invalid RA email address');
236
                        }
237
 
288 daniel-mar 238
                        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'";
256 daniel-mar 239
                        if (!empty($ra_email)) {
288 daniel-mar 240
                                OIDplus::logger()->log("[INFO]RA($ra_email)!", "Gained ownership of newly created object '$id'");
256 daniel-mar 241
                        }
242
 
243
                        $confidential = $_POST['confidential'] == 'true';
244
                        $comment = $_POST['comment'];
245
                        $title = '';
246
                        $description = '';
247
 
261 daniel-mar 248
                        if (strlen($id) > OIDplus::baseConfig()->getValue('LIMITS_MAX_ID_LENGTH')) {
249
                                throw new OIDplusException("The identifier '$id' is too long (max allowed length: ".OIDplus::baseConfig()->getValue('LIMITS_MAX_ID_LENGTH').")");
256 daniel-mar 250
                        }
251
 
264 daniel-mar 252
                        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));
256 daniel-mar 253
 
254
                        // Set ASN.1 IDs und IRIs
255
                        if ($obj::ns() == 'oid') {
256
                                $ids = ($_POST['iris'] == '') ? array() : explode(',',$_POST['iris']);
257
                                $ids = array_map('trim',$ids);
258
                                $obj->replaceIris($ids, false);
259
 
260
                                $ids = ($_POST['asn1ids'] == '') ? array() : explode(',',$_POST['asn1ids']);
261
                                $ids = array_map('trim',$ids);
262
                                $obj->replaceAsn1Ids($ids, false);
263
                        }
264
 
265
                        $status = 0;
266
 
267
                        if (!empty($ra_email)) {
268
                                // Do we need to notify that the RA does not exist?
261 daniel-mar 269
                                $res = OIDplus::db()->query("select ra_name from ###ra where email = ?", array($ra_email));
256 daniel-mar 270
                                if ($res->num_rows() == 0) $status = class_exists('OIDplusPageRaInvite') && OIDplus::config()->getValue('ra_invitation_enabled') ? 1 : 2;
271
                        }
272
 
273
                        echo json_encode(array("status" => $status));
274
                }
275
 
104 daniel-mar 276
        }
277
 
278
        public function init($html=true) {
279
        }
280
 
281
        public function gui($id, &$out, &$handled) {
282
                if ($id === 'oidplus:system') {
283
                        $handled = true;
284
 
257 daniel-mar 285
                        $out['title'] = OIDplus::config()->getValue('system_title'); // 'Object Database of ' . $_SERVER['SERVER_NAME'];
241 daniel-mar 286
                        $out['icon'] = OIDplus::webpath(__DIR__).'system_big.png';
104 daniel-mar 287
 
256 daniel-mar 288
                        if (file_exists(__DIR__ . '/welcome.local.html')) {
289
                                $out['text'] = file_get_contents(__DIR__ . '/welcome.local.html');
290
                        } else if (file_exists(__DIR__ . '/welcome.html')) {
291
                                $out['text'] = file_get_contents(__DIR__ . '/welcome.html');
292
                        } else {
293
                                $out['text'] = '';
294
                        }
295
 
104 daniel-mar 296
                        if (strpos($out['text'], '%%OBJECT_TYPE_LIST%%') !== false) {
297
                                $tmp = '<ul>';
227 daniel-mar 298
                                foreach (OIDplus::getEnabledObjectTypes() as $ot) {
250 daniel-mar 299
                                        $tmp .= '<li><a '.OIDplus::gui()->link($ot::root()).'>'.htmlentities($ot::objectTypeTitle()).'</a></li>';
104 daniel-mar 300
                                }
301
                                $tmp .= '</ul>';
302
                                $out['text'] = str_replace('%%OBJECT_TYPE_LIST%%', $tmp, $out['text']);
303
                        }
304
 
281 daniel-mar 305
                        return;
104 daniel-mar 306
                }
117 daniel-mar 307
 
256 daniel-mar 308
                try {
309
                        $obj = OIDplusObject::parse($id);
310
                } catch (Exception $e) {
311
                        $obj = null;
312
                }
313
 
314
                if (!is_null($obj)) {
315
                        $handled = true;
316
 
317
                        if (!$obj->userHasReadRights()) {
318
                                $out['title'] = 'Access denied';
319
                                $out['icon'] = 'img/error_big.png';
320
                                $out['text'] = '<p>Please <a '.OIDplus::gui()->link('oidplus:login').'>log in</a> to receive information about this object.</p>';
281 daniel-mar 321
                                return;
256 daniel-mar 322
                        }
323
 
324
                        $parent = null;
325
                        $res = null;
326
                        $row = null;
327
                        $matches_any_registered_type = false;
328
                        foreach (OIDplus::getEnabledObjectTypes() as $ot) {
329
                                if ($obj = $ot::parse($id)) {
330
                                        $matches_any_registered_type = true;
331
                                        if ($obj->isRoot()) {
332
                                                $obj->getContentPage($out['title'], $out['text'], $out['icon']);
333
                                                $parent = null; // $obj->getParent();
334
                                                break;
335
                                        } else {
261 daniel-mar 336
                                                $res = OIDplus::db()->query("select * from ###objects where id = ?", array($obj->nodeId()));
256 daniel-mar 337
                                                if ($res->num_rows() == 0) {
338
                                                        http_response_code(404);
339
                                                        $out['title'] = 'Object not found';
340
                                                        $out['icon'] = 'img/error_big.png';
341
                                                        $out['text'] = 'The object <code>'.htmlentities($id).'</code> was not found in this database.';
281 daniel-mar 342
                                                        return;
256 daniel-mar 343
                                                } else {
344
                                                        $row = $res->fetch_array(); // will be used further down the code
345
                                                        $obj->getContentPage($out['title'], $out['text'], $out['icon']);
346
                                                        if (empty($out['title'])) $out['title'] = explode(':',$id,2)[1];
347
                                                        $parent = $obj->getParent();
348
                                                        break;
349
                                                }
350
                                        }
351
                                }
352
                        }
353
                        if (!$matches_any_registered_type) {
354
                                http_response_code(404);
355
                                $out['title'] = 'Object not found';
356
                                $out['icon'] = 'img/error_big.png';
357
                                $out['text'] = 'The object <code>'.htmlentities($id).'</code> was not found in this database.';
281 daniel-mar 358
                                return;
256 daniel-mar 359
                        }
360
 
361
                        // ---
362
 
363
                        if ($parent) {
364
                                if ($parent->isRoot()) {
365
 
366
                                        $parent_link_text = $parent->objectTypeTitle();
367
                                        $out['text'] = '<p><a '.OIDplus::gui()->link($parent->root()).'><img src="img/arrow_back.png" width="16"> Parent node: '.htmlentities($parent_link_text).'</a></p>' . $out['text'];
368
 
369
                                } else {
261 daniel-mar 370
                                        $res_ = OIDplus::db()->query("select * from ###objects where id = ?", array($parent->nodeId()));
256 daniel-mar 371
                                        if ($res_->num_rows() > 0) {
372
                                                $row_ = $res_->fetch_array();
373
 
374
                                                $parent_title = $row_['title'];
375
                                                if (empty($parent_title) && ($parent->ns() == 'oid')) {
376
                                                        // If not title is available, then use an ASN.1 identifier
261 daniel-mar 377
                                                        $res_ = OIDplus::db()->query("select name from ###asn1id where oid = ?", array($parent->nodeId()));
256 daniel-mar 378
                                                        if ($res_->num_rows() > 0) {
379
                                                                $row_ = $res_->fetch_array();
380
                                                                $parent_title = $row_['name']; // TODO: multiple ASN1 ids?
381
                                                        }
382
                                                }
383
 
384
                                                $parent_link_text = empty($parent_title) ? explode(':',$parent->nodeId())[1] : $parent_title.' ('.explode(':',$parent->nodeId())[1].')';
385
 
386
                                                $out['text'] = '<p><a '.OIDplus::gui()->link($parent->nodeId()).'><img src="img/arrow_back.png" width="16"> Parent node: '.htmlentities($parent_link_text).'</a></p>' . $out['text'];
387
                                        } else {
388
                                                $out['text'] = '';
389
                                        }
390
                                }
391
                        } else {
392
                                $parent_link_text = 'Go back to front page';
393
                                $out['text'] = '<p><a '.OIDplus::gui()->link('oidplus:system').'><img src="img/arrow_back.png" width="16"> '.htmlentities($parent_link_text).'</a></p>' . $out['text'];
394
                        }
395
 
396
                        // ---
397
 
398
                        if (!is_null($row) && isset($row['description'])) {
399
                                if (empty($row['description'])) {
400
                                        if (empty($row['title'])) {
401
                                                $desc = '<p><i>No description for this object available</i></p>';
402
                                        } else {
403
                                                $desc = $row['title'];
404
                                        }
405
                                } else {
406
                                        $desc = self::objDescription($row['description']);
407
                                }
408
 
409
                                if ($obj->userHasWriteRights()) {
410
                                        $rand = ++self::$crudCounter;
411
                                        $desc = '<noscript><p><b>You need to enable JavaScript to edit title or description of this object.</b></p>'.$desc.'</noscript>';
412
                                        $desc .= '<div class="container box" style="display:none" id="descbox_'.$rand.'">';
413
                                        $desc .= 'Title: <input type="text" name="title" id="titleedit" value="'.htmlentities($row['title']).'"><br><br>Description:<br>';
414
                                        $desc .= self::showMCE('description', $row['description']);
415
                                        $desc .= '<button type="button" name="update_desc" id="update_desc" class="btn btn-success btn-xs update" onclick="updateDesc()">Update description</button>';
416
                                        $desc .= '</div>';
417
                                        $desc .= '<script>document.getElementById("descbox_'.$rand.'").style.display = "block";</script>';
418
                                }
419
                        } else {
420
                                $desc = '';
421
                        }
422
 
423
                        // ---
424
 
425
                        if (strpos($out['text'], '%%DESC%%') !== false)
426
                                $out['text'] = str_replace('%%DESC%%',    $desc,                              $out['text']);
427
                        if (strpos($out['text'], '%%CRUD%%') !== false)
428
                                $out['text'] = str_replace('%%CRUD%%',    self::showCrud($id),                $out['text']);
429
                        if (strpos($out['text'], '%%RA_INFO%%') !== false)
430
                                $out['text'] = str_replace('%%RA_INFO%%', OIDplusPagePublicRaInfo::showRaInfo($row['ra_email']), $out['text']);
431
 
432
                        $alt_ids = $obj->getAltIds();
433
                        if (count($alt_ids) > 0) {
434
                                $out['text'] .= "<h2>Alternative Identifiers</h2>";
435
                                foreach ($alt_ids as $alt_id) {
436
                                        $ns = $alt_id->getNamespace();
437
                                        $aid = $alt_id->getId();
438
                                        $aiddesc = $alt_id->getDescription();
439
                                        $out['text'] .= "$aiddesc <code>$ns:$aid</code><br>";
440
                                }
441
                        }
442
 
281 daniel-mar 443
                        foreach (OIDplus::getPagePlugins() as $plugin) $plugin->modifyContent($id, $out['title'], $out['icon'], $out['text']);
256 daniel-mar 444
                }
104 daniel-mar 445
        }
446
 
282 daniel-mar 447
        private function publicSitemap_rec($json, &$out) {
448
                foreach ($json as $x) {
449
                        if (isset($x['id']) && $x['id']) {
450
                                $out[] = OIDplus::getSystemUrl().'?goto='.urlencode($x['id']);
451
                        }
452
                        if (isset($x['children'])) {
453
                                $this->publicSitemap_rec($x['children'], $out);
454
                        }
455
                }
456
        }
457
 
458
        public function publicSitemap(&$out) {
459
                $json = array();
460
                $this->tree($json, null/*RA EMail*/, false/*HTML tree algorithm*/, true/*display all*/);
461
                $this->publicSitemap_rec($json, $out);
462
        }
463
 
106 daniel-mar 464
        public function tree(&$json, $ra_email=null, $nonjs=false, $req_goto='') {
104 daniel-mar 465
                if ($nonjs) {
241 daniel-mar 466
                        $json[] = array('id' => 'oidplus:system', 'icon' => OIDplus::webpath(__DIR__).'system.png', 'text' => 'System');
104 daniel-mar 467
 
468
                        $parent = '';
261 daniel-mar 469
                        $res = OIDplus::db()->query("select parent from ###objects where id = ?", array($req_goto));
236 daniel-mar 470
                        while ($row = $res->fetch_object()) {
104 daniel-mar 471
                                $parent = $row->parent;
472
                        }
473
 
474
                        $objTypesChildren = array();
227 daniel-mar 475
                        foreach (OIDplus::getEnabledObjectTypes() as $ot) {
104 daniel-mar 476
                                $icon = 'plugins/objectTypes/'.$ot::ns().'/img/treeicon_root.png';
477
                                $json[] = array('id' => $ot::root(), 'icon' => $icon, 'text' => $ot::objectTypeTitle());
478
 
479
                                try {
106 daniel-mar 480
                                        $tmp = OIDplusObject::parse($req_goto);
104 daniel-mar 481
                                } catch (Exception $e) {
482
                                        $tmp = null;
483
                                }
484
                                if (!is_null($tmp) && ($ot == get_class($tmp))) {
485
                                        // 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
486
                                        //       on the other hand, for giving search engines content, this is good enough
150 daniel-mar 487
                                        if (empty($parent)) {
261 daniel-mar 488
                                                $res = OIDplus::db()->query("select * from ###objects where " .
150 daniel-mar 489
                                                                                   "parent = ? or " .
490
                                                                                   "id = ? " .
491
                                                                                   "order by ".OIDplus::db()->natOrder('id'), array($req_goto, $req_goto));
492
                                        } else {
261 daniel-mar 493
                                                $res = OIDplus::db()->query("select * from ###objects where " .
150 daniel-mar 494
                                                                                   "parent = ? or " .
495
                                                                                   "id = ? or " .
496
                                                                                   "id = ? ".
497
                                                                                   "order by ".OIDplus::db()->natOrder('id'), array($req_goto, $req_goto, $parent));
498
                                        }
499
 
104 daniel-mar 500
                                        $z_used = 0;
501
                                        $y_used = 0;
502
                                        $x_used = 0;
503
                                        $stufe = 0;
504
                                        $menu_entries = array();
505
                                        $stufen = array();
236 daniel-mar 506
                                        while ($row = $res->fetch_object()) {
104 daniel-mar 507
                                                $obj = OIDplusObject::parse($row->id);
508
                                                if (is_null($obj)) continue; // might happen if the objectType is not available/loaded
509
                                                if (!$obj->userHasReadRights()) continue;
510
                                                $txt = $row->title == '' ? '' : ' -- '.htmlentities($row->title);
511
 
512
                                                if ($row->id == $parent) { $stufe=0; $z_used++; }
106 daniel-mar 513
                                                if ($row->id == $req_goto) { $stufe=1; $y_used++; }
514
                                                if ($row->parent == $req_goto) { $stufe=2; $x_used++; }
104 daniel-mar 515
 
516
                                                $menu_entry = array('id' => $row->id, 'icon' => '', 'text' => $txt, 'indent' => 0);
517
                                                $menu_entries[] = $menu_entry;
518
                                                $stufen[] = $stufe;
519
                                        }
520
                                        if ($x_used) foreach ($menu_entries as $i => &$menu_entry) if ($stufen[$i] >= 2) $menu_entry['indent'] += 1;
521
                                        if ($y_used) foreach ($menu_entries as $i => &$menu_entry) if ($stufen[$i] >= 1) $menu_entry['indent'] += 1;
522
                                        if ($z_used) foreach ($menu_entries as $i => &$menu_entry) if ($stufen[$i] >= 0) $menu_entry['indent'] += 1;
523
                                        $json = array_merge($json, $menu_entries);
524
                                }
525
                        }
526
 
527
                        return true;
528
                } else {
281 daniel-mar 529
                        if ($req_goto === true) {
530
                                $goto_path = true; // display everything recursively
531
                        } else if (isset($req_goto)) {
145 daniel-mar 532
                                $goto = $req_goto;
533
                                $path = array();
534
                                while (true) {
535
                                        $path[] = $goto;
261 daniel-mar 536
                                        $res = OIDplus::db()->query("select parent from ###objects where id = ?", array($goto));
236 daniel-mar 537
                                        if ($res->num_rows() == 0) break;
538
                                        $row = $res->fetch_array();
145 daniel-mar 539
                                        $goto = $row['parent'];
150 daniel-mar 540
                                        if ($goto == '') continue;
145 daniel-mar 541
                                }
104 daniel-mar 542
 
145 daniel-mar 543
                                $goto_path = array_reverse($path);
104 daniel-mar 544
                        } else {
545
                                $goto_path = null;
546
                        }
547
 
548
                        $objTypesChildren = array();
227 daniel-mar 549
                        foreach (OIDplus::getEnabledObjectTypes() as $ot) {
145 daniel-mar 550
                                $child = array('id' => $ot::root(),
551
                                               'text' => $ot::objectTypeTitle(),
552
                                               'state' => array("opened" => true),
553
                                               'icon' => 'plugins/objectTypes/'.$ot::ns().'/img/treeicon_root.png',
250 daniel-mar 554
                                               'children' => OIDplus::menuUtils()->tree_populate($ot::root(), $goto_path)
145 daniel-mar 555
                                               );
104 daniel-mar 556
                                if (!file_exists($child['icon'])) $child['icon'] = null; // default icon (folder)
557
                                $objTypesChildren[] = $child;
558
                        }
559
 
560
                        $json[] = array(
561
                                'id' => "oidplus:system",
562
                                'text' => "Objects",
563
                                'state' => array(
564
                                        "opened" => true,
565
                                        // "selected" => true)  // "selected" ist buggy: 1) Das select-Event wird beim Laden nicht gefeuert 2) Die direkt untergeordneten Knoten lassen sich nicht öffnen (laden für ewig)
566
                                ),
241 daniel-mar 567
                                'icon' => OIDplus::webpath(__DIR__).'system.png',
104 daniel-mar 568
                                'children' => $objTypesChildren
569
                        );
570
 
571
                        return true;
572
                }
573
        }
108 daniel-mar 574
 
575
        public function tree_search($request) {
576
                $ary = array();
577
                if ($obj = OIDplusObject::parse($request)) {
578
                        if ($obj->userHasReadRights()) {
579
                                do {
580
                                        $ary[] = $obj->nodeId();
581
                                } while ($obj = $obj->getParent());
582
                                $ary = array_reverse($ary);
583
                        }
584
                }
585
                return $ary;
586
        }
256 daniel-mar 587
 
588
        private static $crudCounter = 0;
589
 
590
        protected static function showCrud($parent='oid:') {
591
                $items_total = 0;
592
                $items_hidden = 0;
593
 
594
                $objParent = OIDplusObject::parse($parent);
595
                $parentNS = $objParent::ns();
596
 
597
                $result = OIDplus::db()->query("select o.*, r.ra_name " .
261 daniel-mar 598
                                               "from ###objects o " .
599
                                               "left join ###ra r on r.email = o.ra_email " .
256 daniel-mar 600
                                               "where parent = ? " .
601
                                               "order by ".OIDplus::db()->natOrder('id'), array($parent));
602
                $rows = array();
603
                if ($parentNS == 'oid') {
604
                        $one_weid_available = $objParent->isWeid(true);
605
                        while ($row = $result->fetch_object()) {
606
                                $obj = OIDplusObject::parse($row->id);
607
                                $rows[] = array($obj,$row);
608
                                if (!$one_weid_available) {
609
                                        if ($obj->isWeid(true)) $one_weid_available = true;
610
                                }
611
                        }
612
                } else {
613
                        $one_weid_available = false;
614
                        while ($row = $result->fetch_object()) {
615
                                $obj = OIDplusObject::parse($row->id);
616
                                $rows[] = array($obj,$row);
617
                        }
618
                }
619
 
620
                $output = '';
621
                $output .= '<div class="container box"><div id="suboid_table" class="table-responsive">';
622
                $output .= '<table class="table table-bordered table-striped">';
623
                $output .= '    <tr>';
624
                $output .= '         <th>ID'.(($parentNS == 'gs1') ? ' (without check digit)' : '').'</th>';
625
                if ($parentNS == 'oid') {
626
                        if ($one_weid_available) $output .= '        <th>WEID</th>';
627
                        $output .= '         <th>ASN.1 IDs (comma sep.)</th>';
628
                        $output .= '         <th>IRI IDs (comma sep.)</th>';
629
                }
630
                $output .= '         <th>RA</th>';
631
                $output .= '         <th>Comment</th>';
632
                if ($objParent->userHasWriteRights()) {
633
                        $output .= '         <th>Hide</th>';
634
                        $output .= '         <th>Update</th>';
635
                        $output .= '         <th>Delete</th>';
636
                }
637
                $output .= '         <th>Created</th>';
638
                $output .= '         <th>Updated</th>';
639
                $output .= '    </tr>';
640
 
641
                foreach ($rows as list($obj,$row)) {
642
                        $items_total++;
643
                        if (!$obj->userHasReadRights()) {
644
                                $items_hidden++;
645
                                continue;
646
                        }
647
 
648
                        $show_id = $obj->crudShowId($objParent);
649
 
650
                        $asn1ids = array();
261 daniel-mar 651
                        $res2 = OIDplus::db()->query("select name from ###asn1id where oid = ? order by lfd", array($row->id));
256 daniel-mar 652
                        while ($row2 = $res2->fetch_array()) {
653
                                $asn1ids[] = $row2['name'];
654
                        }
655
 
656
                        $iris = array();
261 daniel-mar 657
                        $res2 = OIDplus::db()->query("select name from ###iri where oid = ? order by lfd", array($row->id));
256 daniel-mar 658
                        while ($row2 = $res2->fetch_array()) {
659
                                $iris[] = $row2['name'];
660
                        }
661
 
662
                        $date_created = explode(' ', $row->created)[0] == '0000-00-00' ? '' : explode(' ', $row->created)[0];
663
                        $date_updated = explode(' ', $row->updated)[0] == '0000-00-00' ? '' : explode(' ', $row->updated)[0];
664
 
665
                        $output .= '<tr>';
666
                        $output .= '     <td><a href="?goto='.urlencode($row->id).'" onclick="openAndSelectNode('.js_escape($row->id).', '.js_escape($parent).'); return false;">'.htmlentities($show_id).'</a></td>';
667
                        if ($objParent->userHasWriteRights()) {
668
                                if ($parentNS == 'oid') {
669
                                        if ($one_weid_available) {
670
                                                if ($obj->isWeid(false)) {
671
                                                        $output .= '    <td>'.$obj->weidArc().'</td>';
672
                                                } else {
673
                                                        $output .= '    <td>n/a</td>';
674
                                                }
675
                                        }
676
                                        $output .= '     <td><input type="text" id="asn1ids_'.$row->id.'" value="'.implode(', ', $asn1ids).'"></td>';
677
                                        $output .= '     <td><input type="text" id="iris_'.$row->id.'" value="'.implode(', ', $iris).'"></td>';
678
                                }
679
                                $output .= '     <td><input type="text" id="ra_email_'.$row->id.'" value="'.htmlentities($row->ra_email).'"></td>';
680
                                $output .= '     <td><input type="text" id="comment_'.$row->id.'" value="'.htmlentities($row->comment).'"></td>';
681
                                $output .= '     <td><input type="checkbox" id="hide_'.$row->id.'" '.($row->confidential ? 'checked' : '').'></td>';
682
                                $output .= '     <td><button type="button" name="update_'.$row->id.'" id="update_'.$row->id.'" class="btn btn-success btn-xs update" onclick="crudActionUpdate('.js_escape($row->id).', '.js_escape($parent).')">Update</button></td>';
683
                                $output .= '     <td><button type="button" name="delete_'.$row->id.'" id="delete_'.$row->id.'" class="btn btn-danger btn-xs delete" onclick="crudActionDelete('.js_escape($row->id).', '.js_escape($parent).')">Delete</button></td>';
684
                                $output .= '     <td>'.$date_created.'</td>';
685
                                $output .= '     <td>'.$date_updated.'</td>';
686
                        } else {
687
                                if ($asn1ids == '') $asn1ids = '<i>(none)</i>';
688
                                if ($iris == '') $iris = '<i>(none)</i>';
689
                                if ($parentNS == 'oid') {
690
                                        if ($one_weid_available) {
691
                                                if ($obj->isWeid(false)) {
692
                                                        $output .= '    <td>'.$obj->weidArc().'</td>';
693
                                                } else {
694
                                                        $output .= '    <td>n/a</td>';
695
                                                }
696
                                        }
697
                                        $asn1ids_ext = array();
698
                                        foreach ($asn1ids as $asn1id) {
699
                                                $asn1ids_ext[] = '<a href="?goto='.urlencode($row->id).'" onclick="openAndSelectNode('.js_escape($row->id).', '.js_escape($parent).'); return false;">'.$asn1id.'</a>';
700
                                        }
701
                                        $output .= '     <td>'.implode(', ', $asn1ids_ext).'</td>';
702
                                        $output .= '     <td>'.implode(', ', $iris).'</td>';
703
                                }
704
                                $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>';
705
                                $output .= '     <td>'.htmlentities($row->comment).'</td>';
706
                                $output .= '     <td>'.$date_created.'</td>';
707
                                $output .= '     <td>'.$date_updated.'</td>';
708
                        }
709
                        $output .= '</tr>';
710
                }
711
 
261 daniel-mar 712
                $result = OIDplus::db()->query("select * from ###objects where id = ?", array($parent));
256 daniel-mar 713
                $parent_ra_email = $result->num_rows() > 0 ? $result->fetch_object()->ra_email : '';
714
 
715
                if ($objParent->userHasWriteRights()) {
716
                        $output .= '<tr>';
717
                        $prefix = is_null($objParent) ? '' : $objParent->crudInsertPrefix();
718
                        if ($parentNS == 'oid') {
719
                                if ($objParent->isWeid(true)) {
720
                                        $output .= '     <td>'.$prefix.' <input oninput="frdl_oidid_change()" type="text" id="id" value="" style="width:100%;min-width:100px"></td>'; // TODO: idee classname vergeben, z.B. "OID" und dann mit einem oid-spezifischen css die breite einstellbar machen, somit hat das plugin mehr kontrolle über das aussehen und die mindestbreiten
721
                                        $output .= '     <td><input type="text" name="weid" id="weid" value="" oninput="frdl_weid_change()"></td>';
722
                                } else {
723
                                        $output .= '     <td>'.$prefix.' <input type="text" id="id" value="" style="width:100%;min-width:50px"></td>'; // TODO: idee classname vergeben, z.B. "OID" und dann mit einem oid-spezifischen css die breite einstellbar machen, somit hat das plugin mehr kontrolle über das aussehen und die mindestbreiten
724
                                        if ($one_weid_available) $output .= '     <td></td>'; // WEID-editor not available for root nodes. Do it manually, please
725
                                }
726
                        } else {
727
                                $output .= '     <td>'.$prefix.' <input type="text" id="id" value=""></td>';
728
                        }
729
                        if ($parentNS == 'oid') $output .= '     <td><input type="text" id="asn1ids" value=""></td>';
730
                        if ($parentNS == 'oid') $output .= '     <td><input type="text" id="iris" value=""></td>';
731
                        $output .= '     <td><input type="text" id="ra_email" value="'.htmlentities($parent_ra_email).'"></td>';
732
                        $output .= '     <td><input type="text" id="comment" value=""></td>';
733
                        $output .= '     <td><input type="checkbox" id="hide"></td>';
734
                        $output .= '     <td><button type="button" name="insert" id="insert" class="btn btn-success btn-xs update" onclick="crudActionInsert('.js_escape($parent).')">Insert</button></td>';
735
                        $output .= '     <td></td>';
736
                        $output .= '     <td></td>';
737
                        $output .= '     <td></td>';
738
                        $output .= '</tr>';
739
                } else {
740
                        if ($items_total-$items_hidden == 0) {
741
                                $cols = ($parentNS == 'oid') ? 7 : 5;
742
                                if ($one_weid_available) $cols++;
743
                                $output .= '<tr><td colspan="'.$cols.'">No items available</td></tr>';
744
                        }
745
                }
746
 
747
                $output .= '</table>';
748
                $output .= '</div></div>';
749
 
750
                if ($items_hidden == 1) {
751
                        $output .= '<p>'.$items_hidden.' item is hidden. Please <a '.OIDplus::gui()->link('oidplus:login').'>log in</a> to see it.</p>';
752
                } else if ($items_hidden > 1) {
753
                        $output .= '<p>'.$items_hidden.' items are hidden. Please <a '.OIDplus::gui()->link('oidplus:login').'>log in</a> to see them.</p>';
754
                }
755
 
756
                return $output;
757
        }
758
 
759
        protected static function objDescription($html) {
760
                // We allow HTML, but no hacking
761
                $html = anti_xss($html);
762
 
763
                return trim_br($html);
764
        }
765
 
766
        // 'quickbars' added 11 July 2019: Disabled because of two problems:
767
        //                                 1. When you load TinyMCE via AJAX using the left menu, the quickbar is immediately shown, even if TinyMCE does not have the focus
768
        //                                 2. When you load a page without TinyMCE using the left menu, the quickbar is still visible, although there is no edit
769
        // 'colorpicker', 'textcolor' and 'contextmenu' added in 07 April 2020, because it is built in in the core.
770
        public static $exclude_tinymce_plugins = array('fullpage', 'bbcode', 'quickbars', 'colorpicker', 'textcolor', 'contextmenu');
771
 
772
        protected static function showMCE($name, $content) {
773
                $mce_plugins = array();
774
                foreach (glob(__DIR__ . '/../../3p/tinymce/plugins/*') as $m) { // */
775
                        $mce_plugins[] = basename($m);
776
                }
777
 
778
                foreach (self::$exclude_tinymce_plugins as $exclude) {
779
                        $index = array_search($exclude, $mce_plugins);
780
                        if ($index !== false) unset($mce_plugins[$index]);
781
                }
782
 
783
                $out = '<script>
784
                                tinymce.remove("#'.$name.'");
785
                                tinymce.EditorManager.baseURL = "3p/tinymce";
786
                                tinymce.init({
787
                                        document_base_url: "'.OIDplus::getSystemUrl().'",
788
                                        selector: "#'.$name.'",
789
                                        height: 200,
790
                                        statusbar: false,
791
//                                      menubar:false,
792
//                                      toolbar: "undo redo | styleselect | bold italic | alignleft aligncenter alignright alignjustify | bullist numlist outdent indent | table | fontsizeselect",
793
                                        toolbar: "undo redo | styleselect | bold italic underline forecolor | bullist numlist | outdent indent | table | fontsizeselect",
794
                                        plugins: "'.implode(' ', $mce_plugins).'",
795
                                        mobile: {
796
                                                theme: "mobile",
797
                                                toolbar: "undo redo | styleselect | bold italic underline forecolor | bullist numlist | outdent indent | table | fontsizeselect",
798
                                                plugins: "'.implode(' ', $mce_plugins).'"
799
                                        }
800
 
801
                                });
802
                        </script>';
803
 
804
                $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?
805
 
806
                $out .= '<textarea name="'.htmlentities($name).'" id="'.htmlentities($name).'">'.trim($content).'</textarea><br>';
807
 
808
                return $out;
809
        }
810
 
292 daniel-mar 811
        public function implementsFeature($id) {
812
                if (strtolower($id) == '1.3.6.1.4.1.37476.2.5.2.3.1') return true; // oobeEntry
813
                return false;
814
        }
815
 
816
        public function oobeEntry($step, $do_edits, &$errors_happened)/*: void*/ {
817
                // Interface 1.3.6.1.4.1.37476.2.5.2.3.1
818
 
819
                echo "<p><u>Step $step: Enable/Disable object type plugins</u></p>";
820
                echo '<p>Which object types do you want to manage using OIDplus?</p>';
821
 
822
                $enabled_ary = array();
823
 
824
                foreach (OIDplus::getEnabledObjectTypes() as $ot) {
825
                        echo '<input type="checkbox" name="enable_ot_'.$ot::ns().'" id="enable_ot_'.$ot::ns().'"';
826
                        if (isset($_REQUEST['sent'])) {
827
                                if (isset($_REQUEST['enable_ot_'.$ot::ns()])) {
828
                                        echo ' checked';
829
                                        $enabled_ary[] = $ot::ns();
830
                                }
831
                        } else {
832
                                echo ' checked';
833
                        }
834
                        echo '> <label for="enable_ot_'.$ot::ns().'">'.htmlentities($ot::objectTypeTitle()).'</label><br>';
835
                }
836
 
837
                foreach (OIDplus::getDisabledObjectTypes() as $ot) {
838
                        echo '<input type="checkbox" name="enable_ot_'.$ot::ns().'" id="enable_ot_'.$ot::ns().'"';
839
                        if (isset($_REQUEST['sent'])) {
840
                                if (isset($_REQUEST['enable_ot_'.$ot::ns()])) {
841
                                        echo ' checked';
842
                                        $enabled_ary[] = $ot::ns();
843
                                }
844
                        } else {
845
                                echo ''; // <-- difference
846
                        }
847
                        echo '> <label for="enable_ot_'.$ot::ns().'">'.htmlentities($ot::objectTypeTitle()).'</label><br>';
848
                }
849
 
850
                $msg = '';
851
                if ($do_edits) {
852
                        try {
853
                                OIDplus::config()->setValue('objecttypes_enabled', implode(';', $enabled_ary));
854
                        } catch (Exception $e) {
855
                                $msg = $e->getMessage();
856
                                $errors_happened = true;
857
                        }
858
                }
859
 
860
                echo ' <font color="red"><b>'.$msg.'</b></font>';
861
        }
862
 
104 daniel-mar 863
}