Subversion Repositories oidplus

Rev

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

Rev Author Line No. Line
107 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
 
20
require_once __DIR__ . '/includes/oidplus.inc.php';
21
 
22
OIDplus::init(false);
23
 
24
header('Content-Type:application/json; charset=utf-8');
25
 
26
try {
150 daniel-mar 27
        OIDplus::db()->transaction_begin();
107 daniel-mar 28
        $handled = false;
29
 
30
        // Action:     (actions defined by plugins)
31
        // Method:     GET / POST
32
        // Parameters: ...
33
        // Outputs:    ...
34
        foreach (OIDplus::getPagePlugins('*') as $plugin) {
35
                $plugin->action($handled);
36
        }
37
 
38
        // Action:     get_description
39
        // Method:     GET / POST
40
        // Parameters: id
41
        // Outputs:    JSON
42
        if (isset($_REQUEST["action"]) && ($_REQUEST['action'] == 'get_description')) {
43
                $handled = true;
44
                if (!isset($_REQUEST['id'])) throw new Exception("Invalid args");
45
                try {
46
                        $out = OIDplus::gui()::generateContentPage($_REQUEST['id']);
47
                } catch(Exception $e) {
48
                        $out = array();
49
                        $out['title'] = 'Error';
50
                        $out['icon'] = 'img/error_big.png';
51
                        $out['text'] = $e->getMessage();
52
                }
53
                echo json_encode($out);
54
        }
55
 
108 daniel-mar 56
        // === jsTree ===
57
 
107 daniel-mar 58
        // Action:     tree_search
59
        // Method:     GET / POST
60
        // Parameters: search
61
        // Outputs:    JSON
62
        if (isset($_REQUEST["action"]) && ($_REQUEST['action'] == 'tree_search')) {
63
                $handled = true;
64
                if (!isset($_REQUEST['search'])) throw new Exception("Invalid args");
108 daniel-mar 65
 
66
                $found = false;
67
                foreach (OIDplus::getPagePlugins('*') as $plugin) {
68
                        $res = $plugin->tree_search($_REQUEST['search']);
69
                        if ($res) {
70
                                echo json_encode($res);
71
                                $found = true;
72
                                break;
73
                        }
107 daniel-mar 74
                }
108 daniel-mar 75
 
76
                if (!$found) {
77
                        echo json_encode(array());
78
                }
107 daniel-mar 79
        }
80
 
81
        // Action:     tree_load
82
        // Method:     GET / POST
83
        // Parameters: id; goto (optional)
84
        // Outputs:    JSON
85
        if (isset($_REQUEST["action"]) && ($_REQUEST['action'] == 'tree_load')) {
86
                $handled = true;
87
                if (!isset($_REQUEST['id'])) throw new Exception("Invalid args");
88
                $json = OIDplusTree::json_tree($_REQUEST['id'], isset($_REQUEST['goto']) ? $_REQUEST['goto'] : '');
89
                echo $json;
90
        }
91
 
92
        // === Admin / RA actions ===
93
 
94
        // Action:     delete_ra
95
        // Method:     POST
96
        // Parameters: email
97
        // Outputs:    Text
98
        if (isset($_POST["action"]) && ($_POST["action"] == "delete_ra")) {
99
                $handled = true;
100
 
101
                $email = $_POST['email'];
102
 
115 daniel-mar 103
                $ra_logged_in = OIDplus::authUtils()->isRaLoggedIn($email);
107 daniel-mar 104
 
115 daniel-mar 105
                if (!OIDplus::authUtils()->isAdminLoggedIn() && !$ra_logged_in) {
106
                        throw new Exception('Authentification error. Please log in.');
107 daniel-mar 107
                }
108
 
115 daniel-mar 109
                if ($ra_logged_in) OIDplus::authUtils()->raLogout($email);
107 daniel-mar 110
 
111
                $ra = new OIDplusRA($email);
112
                $ra->delete();
113
 
115 daniel-mar 114
                OIDplus::logger()->log("RA($email)?/A?", "RA '$email' deleted");
115
 
107 daniel-mar 116
                echo json_encode(array("status" => 0));
117
        }
118
 
119
        // === OID CRUD ===
120
 
121
        // Action:     Delete
122
        // Method:     POST
123
        // Parameters: id
124
        // Outputs:    Text
125
        if (isset($_POST["action"]) && ($_POST["action"] == "Delete")) {
126
                $handled = true;
127
 
128
                $id = $_POST['id'];
129
                $obj = OIDplusObject::parse($id);
179 daniel-mar 130
                if ($obj === null) throw new Exception("DELETE action failed because object '$id' cannot be parsed!");
107 daniel-mar 131
 
184 daniel-mar 132
                // Check if permitted
107 daniel-mar 133
                if (!$obj->userHasParentalWriteRights()) throw new Exception('Authentification error. Please log in as the superior RA to delete this OID.');
150 daniel-mar 134
 
115 daniel-mar 135
                OIDplus::logger()->log("OID($id)+SUPOIDRA($id)?/A?", "Object '$id' (recursively) deleted");
119 daniel-mar 136
                OIDplus::logger()->log("OIDRA($id)!", "Lost ownership of object '$id' because it was deleted");
227 daniel-mar 137
 
209 daniel-mar 138
                if ($parentObj = $obj->getParent()) {
139
                        OIDplus::logger()->log("OID(".$parentObj->nodeId().")", "Object '$id' (recursively) deleted");
140
                }
227 daniel-mar 141
 
107 daniel-mar 142
                // Delete object
150 daniel-mar 143
                OIDplus::db()->query("delete from ".OIDPLUS_TABLENAME_PREFIX."objects where id = ?", array($id));
107 daniel-mar 144
 
145
                // Delete orphan stuff
227 daniel-mar 146
                foreach (OIDplus::getEnabledObjectTypes() as $ot) {
107 daniel-mar 147
                        do {
150 daniel-mar 148
                                $res = OIDplus::db()->query("select id from ".OIDPLUS_TABLENAME_PREFIX."objects where parent <> ? and parent like ? and parent not in (select id from ".OIDPLUS_TABLENAME_PREFIX."objects where id like ?)", array($ot::root(), $ot::root().'%', $ot::root().'%'));
149
 
236 daniel-mar 150
                                while ($row = $res->fetch_array()) {
119 daniel-mar 151
                                        $id_to_delete = $row['id'];
152
                                        OIDplus::logger()->log("OIDRA($id_to_delete)!", "Lost ownership of object '$id_to_delete' because one of the superior objects ('$id') was recursively deleted");
150 daniel-mar 153
                                        if (!OIDplus::db()->query("delete from ".OIDPLUS_TABLENAME_PREFIX."objects where id = ?", array($id_to_delete))) {
107 daniel-mar 154
                                                throw new Exception(OIDplus::db()->error());
155
                                        }
156
                                }
236 daniel-mar 157
                        } while ($res->num_rows() > 0);
107 daniel-mar 158
                }
159
                OIDplus::db()->query("delete from ".OIDPLUS_TABLENAME_PREFIX."asn1id where well_known <> 1 and oid not in (select id from ".OIDPLUS_TABLENAME_PREFIX."objects where id like 'oid:%');");
160
                OIDplus::db()->query("delete from ".OIDPLUS_TABLENAME_PREFIX."iri    where well_known <> 1 and oid not in (select id from ".OIDPLUS_TABLENAME_PREFIX."objects where id like 'oid:%');");
161
 
162
                echo json_encode(array("status" => 0));
163
        }
164
 
165
        // Action:     Update
166
        // Method:     POST
204 daniel-mar 167
        // Parameters: id, ra_email, comment, iris, asn1ids, confidential
107 daniel-mar 168
        // Outputs:    Text
169
        if (isset($_POST["action"]) && ($_POST["action"] == "Update")) {
170
                $handled = true;
171
 
172
                $id = $_POST['id'];
173
                $obj = OIDplusObject::parse($id);
179 daniel-mar 174
                if ($obj === null) throw new Exception("UPDATE action failed because object '$id' cannot be parsed!");
107 daniel-mar 175
 
184 daniel-mar 176
                // Check if permitted
115 daniel-mar 177
                if (!$obj->userHasParentalWriteRights()) throw new Exception('Authentification error. Please log in as the superior RA to update this OID.');
178
 
107 daniel-mar 179
                // Validate RA email address
180
                $new_ra = $_POST['ra_email'];
181
                if (!empty($new_ra) && !oidplus_valid_email($new_ra)) {
182
                        throw new Exception('Invalid RA email address');
183
                }
147 daniel-mar 184
 
185
                // First, do a simulation for ASN.1 IDs and IRIs to check if there are any problems (then an Exception will be thrown)
186
                if ($obj::ns() == 'oid') {
187
                        $oid = $obj;
188
 
189
                        $ids = ($_POST['iris'] == '') ? array() : explode(',',$_POST['iris']);
190
                        $ids = array_map('trim',$ids);
191
                        $oid->replaceIris($ids, true);
192
 
193
                        $ids = ($_POST['asn1ids'] == '') ? array() : explode(',',$_POST['asn1ids']);
194
                        $ids = array_map('trim',$ids);
195
                        $oid->replaceAsn1Ids($ids, true);
196
                }
150 daniel-mar 197
 
147 daniel-mar 198
                // Change RA recursively
150 daniel-mar 199
                $res = OIDplus::db()->query("select ra_email from ".OIDPLUS_TABLENAME_PREFIX."objects where id = ?", array($id));
236 daniel-mar 200
                $row = $res->fetch_array();
107 daniel-mar 201
                $current_ra = $row['ra_email'];
115 daniel-mar 202
                if ($new_ra != $current_ra) {
203
                        OIDplus::logger()->log("OID($id)+SUPOIDRA($id)?/A?", "RA of object '$id' changed from '$current_ra' to '$new_ra'");
119 daniel-mar 204
                        OIDplus::logger()->log("RA($current_ra)!",           "Lost ownership of object '$id' due to RA transfer of superior RA / admin.");
205
                        OIDplus::logger()->log("RA($new_ra)!",               "Gained ownership of object '$id' due to RA transfer of superior RA / admin.");
209 daniel-mar 206
                        if ($parentObj = $obj->getParent()) {
207
                                OIDplus::logger()->log("OID(".$parentObj->nodeId().")", "RA of object '$id' changed from '$current_ra' to '$new_ra'");
208
                        }
115 daniel-mar 209
                        _ra_change_rec($id, $current_ra, $new_ra); // Inherited RAs rekursiv mitändern
210
                }
107 daniel-mar 211
 
209 daniel-mar 212
                // Log if confidentially flag was changed
115 daniel-mar 213
                OIDplus::logger()->log("OID($id)+SUPOIDRA($id)?/A?", "Identifiers/Confidential flag of object '$id' updated"); // TODO: Check if they were ACTUALLY updated!
209 daniel-mar 214
                if ($parentObj = $obj->getParent()) {
215
                        OIDplus::logger()->log("OID(".$parentObj->nodeId().")", "Identifiers/Confidential flag of object '$id' updated"); // TODO: Check if they were ACTUALLY updated!
216
                }
115 daniel-mar 217
 
147 daniel-mar 218
                // Replace ASN.1 IDs und IRIs
107 daniel-mar 219
                if ($obj::ns() == 'oid') {
220
                        $oid = $obj;
221
 
222
                        $ids = ($_POST['iris'] == '') ? array() : explode(',',$_POST['iris']);
223
                        $ids = array_map('trim',$ids);
147 daniel-mar 224
                        $oid->replaceIris($ids, false);
107 daniel-mar 225
 
226
                        $ids = ($_POST['asn1ids'] == '') ? array() : explode(',',$_POST['asn1ids']);
227
                        $ids = array_map('trim',$ids);
147 daniel-mar 228
                        $oid->replaceAsn1Ids($ids, false);
227 daniel-mar 229
 
209 daniel-mar 230
                        // TODO: Check if any identifiers have been actually changed,
231
                        // and log it to OID($id), OID($parent), ... (see above)
107 daniel-mar 232
                }
233
 
150 daniel-mar 234
                $confidential = $_POST['confidential'] == 'true';
204 daniel-mar 235
                $comment = $_POST['comment'];
236
                if (!OIDplus::db()->query("UPDATE ".OIDPLUS_TABLENAME_PREFIX."objects SET confidential = ?, comment = ?, updated = now() WHERE id = ?", array($confidential ? 1 : 0, $comment, $id))) {
107 daniel-mar 237
                        throw new Exception('Error at setting confidential flag:' . OIDplus::db()->error());
238
                }
239
 
240
                $status = 0;
241
 
242
                if (!empty($new_ra)) {
150 daniel-mar 243
                        $res = OIDplus::db()->query("select ra_name from ".OIDPLUS_TABLENAME_PREFIX."ra where email = ?", array($new_ra));
236 daniel-mar 244
                        if ($res->num_rows() == 0) $status = OIDplus::config()->getValue('ra_invitation_enabled') ? 1 : 2;
107 daniel-mar 245
                }
246
 
247
                echo json_encode(array("status" => $status));
248
        }
249
 
250
        // Action:     Update2
251
        // Method:     POST
252
        // Parameters: id, title, description
253
        // Outputs:    Text
254
        if (isset($_POST["action"]) && ($_POST["action"] == "Update2")) {
255
                $handled = true;
256
 
257
                $id = $_POST['id'];
258
                $obj = OIDplusObject::parse($id);
179 daniel-mar 259
                if ($obj === null) throw new Exception("UPDATE2 action failed because object '$id' cannot be parsed!");
107 daniel-mar 260
 
150 daniel-mar 261
                // Check if allowed
107 daniel-mar 262
                if (!$obj->userHasWriteRights()) throw new Exception('Authentification error. Please log in as the RA to update this OID.');
150 daniel-mar 263
 
119 daniel-mar 264
                OIDplus::logger()->log("OID($id)+OIDRA($id)?/A?", "Title/Description of object '$id' updated");
107 daniel-mar 265
 
150 daniel-mar 266
                if (!OIDplus::db()->query("UPDATE ".OIDPLUS_TABLENAME_PREFIX."objects SET title = ?, description = ?, updated = now() WHERE id = ?", array($_POST['title'], $_POST['description'], $id))) {
107 daniel-mar 267
                        throw new Exception(OIDplus::db()->error());
268
                }
269
 
270
                echo json_encode(array("status" => 0));
271
        }
272
 
273
        // Action:     Insert
274
        // Method:     POST
275
        // Parameters: parent, id, ra_email, confidential, iris, asn1ids
276
        // Outputs:    Text
277
        if (isset($_POST["action"]) && ($_POST["action"] == "Insert")) {
278
                $handled = true;
279
 
150 daniel-mar 280
                // Validated are: ID, ra email, asn1 ids, iri ids
107 daniel-mar 281
 
282
                // Check if you have write rights on the parent (to create a new object)
283
                $objParent = OIDplusObject::parse($_POST['parent']);
187 daniel-mar 284
                if ($objParent === null) throw new Exception("INSERT action failed because parent object '".$_POST['parent']."' cannot be parsed!");
107 daniel-mar 285
                if (!$objParent->userHasWriteRights()) throw new Exception('Authentification error. Please log in as the correct RA to insert an OID at this arc.');
286
 
287
                // Check if the ID is valid
288
                if ($_POST['id'] == '') throw new Exception('ID may not be empty');
150 daniel-mar 289
 
204 daniel-mar 290
                // Determine absolute OID name
107 daniel-mar 291
                // Note: At addString() and parse(), the syntax of the ID will be checked
292
                $id = $objParent->addString($_POST['id']);
147 daniel-mar 293
 
294
                // Check, if the OID exists
150 daniel-mar 295
                $test = OIDplus::db()->query("select id from ".OIDPLUS_TABLENAME_PREFIX."objects where id = ?", array($id));
236 daniel-mar 296
                if ($test->num_rows() >= 1) {
147 daniel-mar 297
                        throw new Exception("Object $id already exists!");
298
                }
299
 
107 daniel-mar 300
                $obj = OIDplusObject::parse($id);
179 daniel-mar 301
                if ($obj === null) throw new Exception("INSERT action failed because object '$id' cannot be parsed!");
107 daniel-mar 302
 
147 daniel-mar 303
                // First simulate if there are any problems of ASN.1 IDs und IRIs
304
                if ($obj::ns() == 'oid') {
305
                        $oid = $obj;
306
 
307
                        $ids = ($_POST['iris'] == '') ? array() : explode(',',$_POST['iris']);
308
                        $ids = array_map('trim',$ids);
309
                        $oid->replaceIris($ids, true);
310
 
311
                        $ids = ($_POST['asn1ids'] == '') ? array() : explode(',',$_POST['asn1ids']);
312
                        $ids = array_map('trim',$ids);
313
                        $oid->replaceAsn1Ids($ids, true);
314
                }
315
 
204 daniel-mar 316
                // Apply superior RA change
107 daniel-mar 317
                $parent = $_POST['parent'];
318
                $ra_email = $_POST['ra_email'];
319
                if (!empty($ra_email) && !oidplus_valid_email($ra_email)) {
320
                        throw new Exception('Invalid RA email address');
321
                }
150 daniel-mar 322
                $confidential = $_POST['confidential'] == 'true';
115 daniel-mar 323
 
119 daniel-mar 324
                OIDplus::logger()->log("OID($parent)+OID($id)+OIDRA($parent)?/A?", "Object '$id' created, ".(empty($ra_email) ? "without defined RA" : "given to RA '$ra_email'")).", superior object is '$parent'";
325
                if (!empty($ra_email)) {
326
                        OIDplus::logger()->log("RA($ra_email)!", "Gained ownership of newly created object '$id'");
327
                }
115 daniel-mar 328
 
204 daniel-mar 329
                $comment = $_POST['comment'];
330
                if (!OIDplus::db()->query("INSERT INTO ".OIDPLUS_TABLENAME_PREFIX."objects (id, parent, ra_email, confidential, comment, created) VALUES (?, ?, ?, ?, ?, now())", array($id, $parent, $ra_email, $confidential ? 1 : 0, $comment))) {
107 daniel-mar 331
                        throw new Exception(OIDplus::db()->error());
332
                }
333
 
147 daniel-mar 334
                // Set ASN.1 IDs und IRIs
107 daniel-mar 335
                if ($obj::ns() == 'oid') {
336
                        $oid = $obj;
337
 
338
                        $ids = ($_POST['iris'] == '') ? array() : explode(',',$_POST['iris']);
339
                        $ids = array_map('trim',$ids);
147 daniel-mar 340
                        $oid->replaceIris($ids, false);
107 daniel-mar 341
 
342
                        $ids = ($_POST['asn1ids'] == '') ? array() : explode(',',$_POST['asn1ids']);
343
                        $ids = array_map('trim',$ids);
147 daniel-mar 344
                        $oid->replaceAsn1Ids($ids, false);
107 daniel-mar 345
                }
346
 
347
                $status = 0;
176 daniel-mar 348
 
107 daniel-mar 349
                if (!empty($ra_email)) {
115 daniel-mar 350
                        // Do we need to notify that the RA does not exist?
150 daniel-mar 351
                        $res = OIDplus::db()->query("select ra_name from ".OIDPLUS_TABLENAME_PREFIX."ra where email = ?", array($ra_email));
236 daniel-mar 352
                        if ($res->num_rows() == 0) $status = OIDplus::config()->getValue('ra_invitation_enabled') ? 1 : 2;
107 daniel-mar 353
                }
354
 
355
                echo json_encode(array("status" => $status));
356
        }
357
 
358
        if (!$handled) {
359
                throw new Exception('Invalid action ID');
360
        }
150 daniel-mar 361
 
362
        OIDplus::db()->transaction_commit();
107 daniel-mar 363
} catch (Exception $e) {
150 daniel-mar 364
        OIDplus::db()->transaction_rollback();
107 daniel-mar 365
        $ary = array();
366
        $ary['error'] = $e->getMessage();
367
        echo json_encode($ary);
368
}
369
 
370
# ---
371
 
372
function _ra_change_rec($id, $old_ra, $new_ra) {
150 daniel-mar 373
        OIDplus::db()->query("update ".OIDPLUS_TABLENAME_PREFIX."objects set ra_email = ?, updated = now() where id = ? and ifnull(ra_email,'') = ?", array($new_ra, $id, $old_ra));
107 daniel-mar 374
 
150 daniel-mar 375
        $res = OIDplus::db()->query("select id from ".OIDPLUS_TABLENAME_PREFIX."objects where parent = ? and ifnull(ra_email,'') = ?", array($id, $old_ra));
236 daniel-mar 376
        while ($row = $res->fetch_array()) {
107 daniel-mar 377
                _ra_change_rec($row['id'], $old_ra, $new_ra);
378
        }
379
}