Subversion Repositories oidplus

Rev

Rev 292 | 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
 
294 daniel-mar 288
                        if (file_exists(OIDplus::basePath() . '/userdata/welcome/welcome.html')) {
289
                                $out['text'] = file_get_contents(OIDplus::basePath() . '/userdata/welcome/welcome.html');
290
                        } else if (file_exists(__DIR__ . '/welcome.local.html')) {
256 daniel-mar 291
                                $out['text'] = file_get_contents(__DIR__ . '/welcome.local.html');
292
                        } else if (file_exists(__DIR__ . '/welcome.html')) {
293
                                $out['text'] = file_get_contents(__DIR__ . '/welcome.html');
294
                        } else {
295
                                $out['text'] = '';
296
                        }
297
 
104 daniel-mar 298
                        if (strpos($out['text'], '%%OBJECT_TYPE_LIST%%') !== false) {
299
                                $tmp = '<ul>';
227 daniel-mar 300
                                foreach (OIDplus::getEnabledObjectTypes() as $ot) {
250 daniel-mar 301
                                        $tmp .= '<li><a '.OIDplus::gui()->link($ot::root()).'>'.htmlentities($ot::objectTypeTitle()).'</a></li>';
104 daniel-mar 302
                                }
303
                                $tmp .= '</ul>';
304
                                $out['text'] = str_replace('%%OBJECT_TYPE_LIST%%', $tmp, $out['text']);
305
                        }
306
 
281 daniel-mar 307
                        return;
104 daniel-mar 308
                }
117 daniel-mar 309
 
256 daniel-mar 310
                try {
311
                        $obj = OIDplusObject::parse($id);
312
                } catch (Exception $e) {
313
                        $obj = null;
314
                }
315
 
316
                if (!is_null($obj)) {
317
                        $handled = true;
318
 
319
                        if (!$obj->userHasReadRights()) {
320
                                $out['title'] = 'Access denied';
321
                                $out['icon'] = 'img/error_big.png';
322
                                $out['text'] = '<p>Please <a '.OIDplus::gui()->link('oidplus:login').'>log in</a> to receive information about this object.</p>';
281 daniel-mar 323
                                return;
256 daniel-mar 324
                        }
325
 
326
                        $parent = null;
327
                        $res = null;
328
                        $row = null;
329
                        $matches_any_registered_type = false;
330
                        foreach (OIDplus::getEnabledObjectTypes() as $ot) {
331
                                if ($obj = $ot::parse($id)) {
332
                                        $matches_any_registered_type = true;
333
                                        if ($obj->isRoot()) {
334
                                                $obj->getContentPage($out['title'], $out['text'], $out['icon']);
335
                                                $parent = null; // $obj->getParent();
336
                                                break;
337
                                        } else {
261 daniel-mar 338
                                                $res = OIDplus::db()->query("select * from ###objects where id = ?", array($obj->nodeId()));
256 daniel-mar 339
                                                if ($res->num_rows() == 0) {
340
                                                        http_response_code(404);
341
                                                        $out['title'] = 'Object not found';
342
                                                        $out['icon'] = 'img/error_big.png';
343
                                                        $out['text'] = 'The object <code>'.htmlentities($id).'</code> was not found in this database.';
281 daniel-mar 344
                                                        return;
256 daniel-mar 345
                                                } else {
346
                                                        $row = $res->fetch_array(); // will be used further down the code
347
                                                        $obj->getContentPage($out['title'], $out['text'], $out['icon']);
348
                                                        if (empty($out['title'])) $out['title'] = explode(':',$id,2)[1];
349
                                                        $parent = $obj->getParent();
350
                                                        break;
351
                                                }
352
                                        }
353
                                }
354
                        }
355
                        if (!$matches_any_registered_type) {
356
                                http_response_code(404);
357
                                $out['title'] = 'Object not found';
358
                                $out['icon'] = 'img/error_big.png';
359
                                $out['text'] = 'The object <code>'.htmlentities($id).'</code> was not found in this database.';
281 daniel-mar 360
                                return;
256 daniel-mar 361
                        }
362
 
363
                        // ---
364
 
365
                        if ($parent) {
366
                                if ($parent->isRoot()) {
367
 
368
                                        $parent_link_text = $parent->objectTypeTitle();
369
                                        $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'];
370
 
371
                                } else {
261 daniel-mar 372
                                        $res_ = OIDplus::db()->query("select * from ###objects where id = ?", array($parent->nodeId()));
256 daniel-mar 373
                                        if ($res_->num_rows() > 0) {
374
                                                $row_ = $res_->fetch_array();
375
 
376
                                                $parent_title = $row_['title'];
377
                                                if (empty($parent_title) && ($parent->ns() == 'oid')) {
378
                                                        // If not title is available, then use an ASN.1 identifier
261 daniel-mar 379
                                                        $res_ = OIDplus::db()->query("select name from ###asn1id where oid = ?", array($parent->nodeId()));
256 daniel-mar 380
                                                        if ($res_->num_rows() > 0) {
381
                                                                $row_ = $res_->fetch_array();
382
                                                                $parent_title = $row_['name']; // TODO: multiple ASN1 ids?
383
                                                        }
384
                                                }
385
 
386
                                                $parent_link_text = empty($parent_title) ? explode(':',$parent->nodeId())[1] : $parent_title.' ('.explode(':',$parent->nodeId())[1].')';
387
 
388
                                                $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'];
389
                                        } else {
390
                                                $out['text'] = '';
391
                                        }
392
                                }
393
                        } else {
394
                                $parent_link_text = 'Go back to front page';
395
                                $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'];
396
                        }
397
 
398
                        // ---
399
 
400
                        if (!is_null($row) && isset($row['description'])) {
401
                                if (empty($row['description'])) {
402
                                        if (empty($row['title'])) {
403
                                                $desc = '<p><i>No description for this object available</i></p>';
404
                                        } else {
405
                                                $desc = $row['title'];
406
                                        }
407
                                } else {
408
                                        $desc = self::objDescription($row['description']);
409
                                }
410
 
411
                                if ($obj->userHasWriteRights()) {
412
                                        $rand = ++self::$crudCounter;
413
                                        $desc = '<noscript><p><b>You need to enable JavaScript to edit title or description of this object.</b></p>'.$desc.'</noscript>';
414
                                        $desc .= '<div class="container box" style="display:none" id="descbox_'.$rand.'">';
415
                                        $desc .= 'Title: <input type="text" name="title" id="titleedit" value="'.htmlentities($row['title']).'"><br><br>Description:<br>';
416
                                        $desc .= self::showMCE('description', $row['description']);
417
                                        $desc .= '<button type="button" name="update_desc" id="update_desc" class="btn btn-success btn-xs update" onclick="updateDesc()">Update description</button>';
418
                                        $desc .= '</div>';
419
                                        $desc .= '<script>document.getElementById("descbox_'.$rand.'").style.display = "block";</script>';
420
                                }
421
                        } else {
422
                                $desc = '';
423
                        }
424
 
425
                        // ---
426
 
427
                        if (strpos($out['text'], '%%DESC%%') !== false)
428
                                $out['text'] = str_replace('%%DESC%%',    $desc,                              $out['text']);
429
                        if (strpos($out['text'], '%%CRUD%%') !== false)
430
                                $out['text'] = str_replace('%%CRUD%%',    self::showCrud($id),                $out['text']);
431
                        if (strpos($out['text'], '%%RA_INFO%%') !== false)
432
                                $out['text'] = str_replace('%%RA_INFO%%', OIDplusPagePublicRaInfo::showRaInfo($row['ra_email']), $out['text']);
433
 
434
                        $alt_ids = $obj->getAltIds();
435
                        if (count($alt_ids) > 0) {
436
                                $out['text'] .= "<h2>Alternative Identifiers</h2>";
437
                                foreach ($alt_ids as $alt_id) {
438
                                        $ns = $alt_id->getNamespace();
439
                                        $aid = $alt_id->getId();
440
                                        $aiddesc = $alt_id->getDescription();
441
                                        $out['text'] .= "$aiddesc <code>$ns:$aid</code><br>";
442
                                }
443
                        }
444
 
281 daniel-mar 445
                        foreach (OIDplus::getPagePlugins() as $plugin) $plugin->modifyContent($id, $out['title'], $out['icon'], $out['text']);
256 daniel-mar 446
                }
104 daniel-mar 447
        }
448
 
282 daniel-mar 449
        private function publicSitemap_rec($json, &$out) {
450
                foreach ($json as $x) {
451
                        if (isset($x['id']) && $x['id']) {
452
                                $out[] = OIDplus::getSystemUrl().'?goto='.urlencode($x['id']);
453
                        }
454
                        if (isset($x['children'])) {
455
                                $this->publicSitemap_rec($x['children'], $out);
456
                        }
457
                }
458
        }
459
 
460
        public function publicSitemap(&$out) {
461
                $json = array();
462
                $this->tree($json, null/*RA EMail*/, false/*HTML tree algorithm*/, true/*display all*/);
463
                $this->publicSitemap_rec($json, $out);
464
        }
465
 
106 daniel-mar 466
        public function tree(&$json, $ra_email=null, $nonjs=false, $req_goto='') {
104 daniel-mar 467
                if ($nonjs) {
241 daniel-mar 468
                        $json[] = array('id' => 'oidplus:system', 'icon' => OIDplus::webpath(__DIR__).'system.png', 'text' => 'System');
104 daniel-mar 469
 
470
                        $parent = '';
261 daniel-mar 471
                        $res = OIDplus::db()->query("select parent from ###objects where id = ?", array($req_goto));
236 daniel-mar 472
                        while ($row = $res->fetch_object()) {
104 daniel-mar 473
                                $parent = $row->parent;
474
                        }
475
 
476
                        $objTypesChildren = array();
227 daniel-mar 477
                        foreach (OIDplus::getEnabledObjectTypes() as $ot) {
104 daniel-mar 478
                                $icon = 'plugins/objectTypes/'.$ot::ns().'/img/treeicon_root.png';
479
                                $json[] = array('id' => $ot::root(), 'icon' => $icon, 'text' => $ot::objectTypeTitle());
480
 
481
                                try {
106 daniel-mar 482
                                        $tmp = OIDplusObject::parse($req_goto);
104 daniel-mar 483
                                } catch (Exception $e) {
484
                                        $tmp = null;
485
                                }
486
                                if (!is_null($tmp) && ($ot == get_class($tmp))) {
487
                                        // 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
488
                                        //       on the other hand, for giving search engines content, this is good enough
150 daniel-mar 489
                                        if (empty($parent)) {
261 daniel-mar 490
                                                $res = OIDplus::db()->query("select * from ###objects where " .
150 daniel-mar 491
                                                                                   "parent = ? or " .
492
                                                                                   "id = ? " .
493
                                                                                   "order by ".OIDplus::db()->natOrder('id'), array($req_goto, $req_goto));
494
                                        } else {
261 daniel-mar 495
                                                $res = OIDplus::db()->query("select * from ###objects where " .
150 daniel-mar 496
                                                                                   "parent = ? or " .
497
                                                                                   "id = ? or " .
498
                                                                                   "id = ? ".
499
                                                                                   "order by ".OIDplus::db()->natOrder('id'), array($req_goto, $req_goto, $parent));
500
                                        }
501
 
104 daniel-mar 502
                                        $z_used = 0;
503
                                        $y_used = 0;
504
                                        $x_used = 0;
505
                                        $stufe = 0;
506
                                        $menu_entries = array();
507
                                        $stufen = array();
236 daniel-mar 508
                                        while ($row = $res->fetch_object()) {
104 daniel-mar 509
                                                $obj = OIDplusObject::parse($row->id);
510
                                                if (is_null($obj)) continue; // might happen if the objectType is not available/loaded
511
                                                if (!$obj->userHasReadRights()) continue;
512
                                                $txt = $row->title == '' ? '' : ' -- '.htmlentities($row->title);
513
 
514
                                                if ($row->id == $parent) { $stufe=0; $z_used++; }
106 daniel-mar 515
                                                if ($row->id == $req_goto) { $stufe=1; $y_used++; }
516
                                                if ($row->parent == $req_goto) { $stufe=2; $x_used++; }
104 daniel-mar 517
 
518
                                                $menu_entry = array('id' => $row->id, 'icon' => '', 'text' => $txt, 'indent' => 0);
519
                                                $menu_entries[] = $menu_entry;
520
                                                $stufen[] = $stufe;
521
                                        }
522
                                        if ($x_used) foreach ($menu_entries as $i => &$menu_entry) if ($stufen[$i] >= 2) $menu_entry['indent'] += 1;
523
                                        if ($y_used) foreach ($menu_entries as $i => &$menu_entry) if ($stufen[$i] >= 1) $menu_entry['indent'] += 1;
524
                                        if ($z_used) foreach ($menu_entries as $i => &$menu_entry) if ($stufen[$i] >= 0) $menu_entry['indent'] += 1;
525
                                        $json = array_merge($json, $menu_entries);
526
                                }
527
                        }
528
 
529
                        return true;
530
                } else {
281 daniel-mar 531
                        if ($req_goto === true) {
532
                                $goto_path = true; // display everything recursively
533
                        } else if (isset($req_goto)) {
145 daniel-mar 534
                                $goto = $req_goto;
535
                                $path = array();
536
                                while (true) {
537
                                        $path[] = $goto;
261 daniel-mar 538
                                        $res = OIDplus::db()->query("select parent from ###objects where id = ?", array($goto));
236 daniel-mar 539
                                        if ($res->num_rows() == 0) break;
540
                                        $row = $res->fetch_array();
145 daniel-mar 541
                                        $goto = $row['parent'];
150 daniel-mar 542
                                        if ($goto == '') continue;
145 daniel-mar 543
                                }
104 daniel-mar 544
 
145 daniel-mar 545
                                $goto_path = array_reverse($path);
104 daniel-mar 546
                        } else {
547
                                $goto_path = null;
548
                        }
549
 
550
                        $objTypesChildren = array();
227 daniel-mar 551
                        foreach (OIDplus::getEnabledObjectTypes() as $ot) {
145 daniel-mar 552
                                $child = array('id' => $ot::root(),
553
                                               'text' => $ot::objectTypeTitle(),
554
                                               'state' => array("opened" => true),
555
                                               'icon' => 'plugins/objectTypes/'.$ot::ns().'/img/treeicon_root.png',
250 daniel-mar 556
                                               'children' => OIDplus::menuUtils()->tree_populate($ot::root(), $goto_path)
145 daniel-mar 557
                                               );
104 daniel-mar 558
                                if (!file_exists($child['icon'])) $child['icon'] = null; // default icon (folder)
559
                                $objTypesChildren[] = $child;
560
                        }
561
 
562
                        $json[] = array(
563
                                'id' => "oidplus:system",
564
                                'text' => "Objects",
565
                                'state' => array(
566
                                        "opened" => true,
567
                                        // "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)
568
                                ),
241 daniel-mar 569
                                'icon' => OIDplus::webpath(__DIR__).'system.png',
104 daniel-mar 570
                                'children' => $objTypesChildren
571
                        );
572
 
573
                        return true;
574
                }
575
        }
108 daniel-mar 576
 
577
        public function tree_search($request) {
578
                $ary = array();
579
                if ($obj = OIDplusObject::parse($request)) {
580
                        if ($obj->userHasReadRights()) {
581
                                do {
582
                                        $ary[] = $obj->nodeId();
583
                                } while ($obj = $obj->getParent());
584
                                $ary = array_reverse($ary);
585
                        }
586
                }
587
                return $ary;
588
        }
256 daniel-mar 589
 
590
        private static $crudCounter = 0;
591
 
592
        protected static function showCrud($parent='oid:') {
593
                $items_total = 0;
594
                $items_hidden = 0;
595
 
596
                $objParent = OIDplusObject::parse($parent);
597
                $parentNS = $objParent::ns();
598
 
599
                $result = OIDplus::db()->query("select o.*, r.ra_name " .
261 daniel-mar 600
                                               "from ###objects o " .
601
                                               "left join ###ra r on r.email = o.ra_email " .
256 daniel-mar 602
                                               "where parent = ? " .
603
                                               "order by ".OIDplus::db()->natOrder('id'), array($parent));
604
                $rows = array();
605
                if ($parentNS == 'oid') {
606
                        $one_weid_available = $objParent->isWeid(true);
607
                        while ($row = $result->fetch_object()) {
608
                                $obj = OIDplusObject::parse($row->id);
609
                                $rows[] = array($obj,$row);
610
                                if (!$one_weid_available) {
611
                                        if ($obj->isWeid(true)) $one_weid_available = true;
612
                                }
613
                        }
614
                } else {
615
                        $one_weid_available = false;
616
                        while ($row = $result->fetch_object()) {
617
                                $obj = OIDplusObject::parse($row->id);
618
                                $rows[] = array($obj,$row);
619
                        }
620
                }
621
 
622
                $output = '';
623
                $output .= '<div class="container box"><div id="suboid_table" class="table-responsive">';
624
                $output .= '<table class="table table-bordered table-striped">';
625
                $output .= '    <tr>';
626
                $output .= '         <th>ID'.(($parentNS == 'gs1') ? ' (without check digit)' : '').'</th>';
627
                if ($parentNS == 'oid') {
628
                        if ($one_weid_available) $output .= '        <th>WEID</th>';
629
                        $output .= '         <th>ASN.1 IDs (comma sep.)</th>';
630
                        $output .= '         <th>IRI IDs (comma sep.)</th>';
631
                }
632
                $output .= '         <th>RA</th>';
633
                $output .= '         <th>Comment</th>';
634
                if ($objParent->userHasWriteRights()) {
635
                        $output .= '         <th>Hide</th>';
636
                        $output .= '         <th>Update</th>';
637
                        $output .= '         <th>Delete</th>';
638
                }
639
                $output .= '         <th>Created</th>';
640
                $output .= '         <th>Updated</th>';
641
                $output .= '    </tr>';
642
 
643
                foreach ($rows as list($obj,$row)) {
644
                        $items_total++;
645
                        if (!$obj->userHasReadRights()) {
646
                                $items_hidden++;
647
                                continue;
648
                        }
649
 
650
                        $show_id = $obj->crudShowId($objParent);
651
 
652
                        $asn1ids = array();
261 daniel-mar 653
                        $res2 = OIDplus::db()->query("select name from ###asn1id where oid = ? order by lfd", array($row->id));
256 daniel-mar 654
                        while ($row2 = $res2->fetch_array()) {
655
                                $asn1ids[] = $row2['name'];
656
                        }
657
 
658
                        $iris = array();
261 daniel-mar 659
                        $res2 = OIDplus::db()->query("select name from ###iri where oid = ? order by lfd", array($row->id));
256 daniel-mar 660
                        while ($row2 = $res2->fetch_array()) {
661
                                $iris[] = $row2['name'];
662
                        }
663
 
664
                        $date_created = explode(' ', $row->created)[0] == '0000-00-00' ? '' : explode(' ', $row->created)[0];
665
                        $date_updated = explode(' ', $row->updated)[0] == '0000-00-00' ? '' : explode(' ', $row->updated)[0];
666
 
667
                        $output .= '<tr>';
668
                        $output .= '     <td><a href="?goto='.urlencode($row->id).'" onclick="openAndSelectNode('.js_escape($row->id).', '.js_escape($parent).'); return false;">'.htmlentities($show_id).'</a></td>';
669
                        if ($objParent->userHasWriteRights()) {
670
                                if ($parentNS == 'oid') {
671
                                        if ($one_weid_available) {
672
                                                if ($obj->isWeid(false)) {
673
                                                        $output .= '    <td>'.$obj->weidArc().'</td>';
674
                                                } else {
675
                                                        $output .= '    <td>n/a</td>';
676
                                                }
677
                                        }
678
                                        $output .= '     <td><input type="text" id="asn1ids_'.$row->id.'" value="'.implode(', ', $asn1ids).'"></td>';
679
                                        $output .= '     <td><input type="text" id="iris_'.$row->id.'" value="'.implode(', ', $iris).'"></td>';
680
                                }
681
                                $output .= '     <td><input type="text" id="ra_email_'.$row->id.'" value="'.htmlentities($row->ra_email).'"></td>';
682
                                $output .= '     <td><input type="text" id="comment_'.$row->id.'" value="'.htmlentities($row->comment).'"></td>';
683
                                $output .= '     <td><input type="checkbox" id="hide_'.$row->id.'" '.($row->confidential ? 'checked' : '').'></td>';
684
                                $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>';
685
                                $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>';
686
                                $output .= '     <td>'.$date_created.'</td>';
687
                                $output .= '     <td>'.$date_updated.'</td>';
688
                        } else {
689
                                if ($asn1ids == '') $asn1ids = '<i>(none)</i>';
690
                                if ($iris == '') $iris = '<i>(none)</i>';
691
                                if ($parentNS == 'oid') {
692
                                        if ($one_weid_available) {
693
                                                if ($obj->isWeid(false)) {
694
                                                        $output .= '    <td>'.$obj->weidArc().'</td>';
695
                                                } else {
696
                                                        $output .= '    <td>n/a</td>';
697
                                                }
698
                                        }
699
                                        $asn1ids_ext = array();
700
                                        foreach ($asn1ids as $asn1id) {
701
                                                $asn1ids_ext[] = '<a href="?goto='.urlencode($row->id).'" onclick="openAndSelectNode('.js_escape($row->id).', '.js_escape($parent).'); return false;">'.$asn1id.'</a>';
702
                                        }
703
                                        $output .= '     <td>'.implode(', ', $asn1ids_ext).'</td>';
704
                                        $output .= '     <td>'.implode(', ', $iris).'</td>';
705
                                }
706
                                $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>';
707
                                $output .= '     <td>'.htmlentities($row->comment).'</td>';
708
                                $output .= '     <td>'.$date_created.'</td>';
709
                                $output .= '     <td>'.$date_updated.'</td>';
710
                        }
711
                        $output .= '</tr>';
712
                }
713
 
261 daniel-mar 714
                $result = OIDplus::db()->query("select * from ###objects where id = ?", array($parent));
256 daniel-mar 715
                $parent_ra_email = $result->num_rows() > 0 ? $result->fetch_object()->ra_email : '';
716
 
717
                if ($objParent->userHasWriteRights()) {
718
                        $output .= '<tr>';
719
                        $prefix = is_null($objParent) ? '' : $objParent->crudInsertPrefix();
720
                        if ($parentNS == 'oid') {
721
                                if ($objParent->isWeid(true)) {
722
                                        $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
723
                                        $output .= '     <td><input type="text" name="weid" id="weid" value="" oninput="frdl_weid_change()"></td>';
724
                                } else {
725
                                        $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
726
                                        if ($one_weid_available) $output .= '     <td></td>'; // WEID-editor not available for root nodes. Do it manually, please
727
                                }
728
                        } else {
729
                                $output .= '     <td>'.$prefix.' <input type="text" id="id" value=""></td>';
730
                        }
731
                        if ($parentNS == 'oid') $output .= '     <td><input type="text" id="asn1ids" value=""></td>';
732
                        if ($parentNS == 'oid') $output .= '     <td><input type="text" id="iris" value=""></td>';
733
                        $output .= '     <td><input type="text" id="ra_email" value="'.htmlentities($parent_ra_email).'"></td>';
734
                        $output .= '     <td><input type="text" id="comment" value=""></td>';
735
                        $output .= '     <td><input type="checkbox" id="hide"></td>';
736
                        $output .= '     <td><button type="button" name="insert" id="insert" class="btn btn-success btn-xs update" onclick="crudActionInsert('.js_escape($parent).')">Insert</button></td>';
737
                        $output .= '     <td></td>';
738
                        $output .= '     <td></td>';
739
                        $output .= '     <td></td>';
740
                        $output .= '</tr>';
741
                } else {
742
                        if ($items_total-$items_hidden == 0) {
743
                                $cols = ($parentNS == 'oid') ? 7 : 5;
744
                                if ($one_weid_available) $cols++;
745
                                $output .= '<tr><td colspan="'.$cols.'">No items available</td></tr>';
746
                        }
747
                }
748
 
749
                $output .= '</table>';
750
                $output .= '</div></div>';
751
 
752
                if ($items_hidden == 1) {
753
                        $output .= '<p>'.$items_hidden.' item is hidden. Please <a '.OIDplus::gui()->link('oidplus:login').'>log in</a> to see it.</p>';
754
                } else if ($items_hidden > 1) {
755
                        $output .= '<p>'.$items_hidden.' items are hidden. Please <a '.OIDplus::gui()->link('oidplus:login').'>log in</a> to see them.</p>';
756
                }
757
 
758
                return $output;
759
        }
760
 
761
        protected static function objDescription($html) {
762
                // We allow HTML, but no hacking
763
                $html = anti_xss($html);
764
 
765
                return trim_br($html);
766
        }
767
 
768
        // 'quickbars' added 11 July 2019: Disabled because of two problems:
769
        //                                 1. When you load TinyMCE via AJAX using the left menu, the quickbar is immediately shown, even if TinyMCE does not have the focus
770
        //                                 2. When you load a page without TinyMCE using the left menu, the quickbar is still visible, although there is no edit
771
        // 'colorpicker', 'textcolor' and 'contextmenu' added in 07 April 2020, because it is built in in the core.
772
        public static $exclude_tinymce_plugins = array('fullpage', 'bbcode', 'quickbars', 'colorpicker', 'textcolor', 'contextmenu');
773
 
774
        protected static function showMCE($name, $content) {
775
                $mce_plugins = array();
294 daniel-mar 776
                foreach (glob(OIDplus::basePath().'/3p/tinymce/plugins/*') as $m) { // */
256 daniel-mar 777
                        $mce_plugins[] = basename($m);
778
                }
779
 
780
                foreach (self::$exclude_tinymce_plugins as $exclude) {
781
                        $index = array_search($exclude, $mce_plugins);
782
                        if ($index !== false) unset($mce_plugins[$index]);
783
                }
784
 
785
                $out = '<script>
786
                                tinymce.remove("#'.$name.'");
787
                                tinymce.EditorManager.baseURL = "3p/tinymce";
788
                                tinymce.init({
789
                                        document_base_url: "'.OIDplus::getSystemUrl().'",
790
                                        selector: "#'.$name.'",
791
                                        height: 200,
792
                                        statusbar: false,
793
//                                      menubar:false,
794
//                                      toolbar: "undo redo | styleselect | bold italic | alignleft aligncenter alignright alignjustify | bullist numlist outdent indent | table | fontsizeselect",
795
                                        toolbar: "undo redo | styleselect | bold italic underline forecolor | bullist numlist | outdent indent | table | fontsizeselect",
796
                                        plugins: "'.implode(' ', $mce_plugins).'",
797
                                        mobile: {
798
                                                theme: "mobile",
799
                                                toolbar: "undo redo | styleselect | bold italic underline forecolor | bullist numlist | outdent indent | table | fontsizeselect",
800
                                                plugins: "'.implode(' ', $mce_plugins).'"
801
                                        }
802
 
803
                                });
804
                        </script>';
805
 
806
                $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?
807
 
808
                $out .= '<textarea name="'.htmlentities($name).'" id="'.htmlentities($name).'">'.trim($content).'</textarea><br>';
809
 
810
                return $out;
811
        }
812
 
292 daniel-mar 813
        public function implementsFeature($id) {
814
                if (strtolower($id) == '1.3.6.1.4.1.37476.2.5.2.3.1') return true; // oobeEntry
815
                return false;
816
        }
817
 
818
        public function oobeEntry($step, $do_edits, &$errors_happened)/*: void*/ {
819
                // Interface 1.3.6.1.4.1.37476.2.5.2.3.1
820
 
821
                echo "<p><u>Step $step: Enable/Disable object type plugins</u></p>";
822
                echo '<p>Which object types do you want to manage using OIDplus?</p>';
823
 
824
                $enabled_ary = array();
825
 
826
                foreach (OIDplus::getEnabledObjectTypes() as $ot) {
827
                        echo '<input type="checkbox" name="enable_ot_'.$ot::ns().'" id="enable_ot_'.$ot::ns().'"';
828
                        if (isset($_REQUEST['sent'])) {
829
                                if (isset($_REQUEST['enable_ot_'.$ot::ns()])) {
830
                                        echo ' checked';
831
                                        $enabled_ary[] = $ot::ns();
832
                                }
833
                        } else {
834
                                echo ' checked';
835
                        }
836
                        echo '> <label for="enable_ot_'.$ot::ns().'">'.htmlentities($ot::objectTypeTitle()).'</label><br>';
837
                }
838
 
839
                foreach (OIDplus::getDisabledObjectTypes() as $ot) {
840
                        echo '<input type="checkbox" name="enable_ot_'.$ot::ns().'" id="enable_ot_'.$ot::ns().'"';
841
                        if (isset($_REQUEST['sent'])) {
842
                                if (isset($_REQUEST['enable_ot_'.$ot::ns()])) {
843
                                        echo ' checked';
844
                                        $enabled_ary[] = $ot::ns();
845
                                }
846
                        } else {
847
                                echo ''; // <-- difference
848
                        }
849
                        echo '> <label for="enable_ot_'.$ot::ns().'">'.htmlentities($ot::objectTypeTitle()).'</label><br>';
850
                }
851
 
852
                $msg = '';
853
                if ($do_edits) {
854
                        try {
855
                                OIDplus::config()->setValue('objecttypes_enabled', implode(';', $enabled_ary));
856
                        } catch (Exception $e) {
857
                                $msg = $e->getMessage();
858
                                $errors_happened = true;
859
                        }
860
                }
861
 
862
                echo ' <font color="red"><b>'.$msg.'</b></font>';
863
        }
864
 
104 daniel-mar 865
}