Subversion Repositories oidplus

Rev

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

Rev Author Line No. Line
2 daniel-mar 1
<?php
2
 
3
/*
4
 * OIDplus 2.0
5
 * Copyright 2019 Daniel Marschall, ViaThinkSoft
6
 *
7
 * Licensed under the Apache License, Version 2.0 (the "License");
8
 * you may not use this file except in compliance with the License.
9
 * You may obtain a copy of the License at
10
 *
11
 *     http://www.apache.org/licenses/LICENSE-2.0
12
 *
13
 * Unless required by applicable law or agreed to in writing, software
14
 * distributed under the License is distributed on an "AS IS" BASIS,
15
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16
 * See the License for the specific language governing permissions and
17
 * limitations under the License.
18
 */
19
 
20
require_once __DIR__ . '/includes/oidplus.inc.php';
21
 
22
OIDplus::init(false);
23
 
24
OIDplus::db()->set_charset("UTF8");
25
OIDplus::db()->query("SET NAMES 'utf8'");
26
 
27
header('Content-Type:text/plain; charset=utf-8');
28
 
29
try {
30
        if (isset($_POST["action"])) {
31
 
32
                $handled = false;
33
 
34
                // === Plugins ===
35
 
36
                $ary = glob(__DIR__ . '/plugins/publicPages/'.'*'.'/action.inc.php');
37
                sort($ary);
38
                foreach ($ary as $a) include $a;
39
 
40
                $ary = glob(__DIR__ . '/plugins/adminPages/'.'*'.'/action.inc.php');
41
                sort($ary);
42
                foreach ($ary as $a) include $a;
43
 
44
                $ary = glob(__DIR__ . '/plugins/raPages/'.'*'.'/action.inc.php');
45
                sort($ary);
46
                foreach ($ary as $a) include $a;
47
 
48
                // === INVITATION ===
49
 
50
                if ($_POST["action"] == "invite_ra") {
51
                        $handled = true;
52
                        $email = $_POST['email'];
53
 
54
                        if (!oiddb_valid_email($email)) {
55
                                die('Invalid email address');
56
                        }
57
 
58
                        if (RECAPTCHA_ENABLED) {
59
                                $secret=RECAPTCHA_PRIVATE;
60
                                $response=$_POST["captcha"];
61
                                $verify=file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret={$secret}&response={$response}");
62
                                $captcha_success=json_decode($verify);
63
                                if ($captcha_success->success==false) {
64
                                        die('Captcha wrong');
65
                                }
66
                        }
67
 
68
                        $timestamp = time();
69
                        $activate_url = OIDplus::system_url() . '?goto='.urlencode('oidplus:activate_ra$'.$email.'$'.$timestamp.'$'.OIDplus::authUtils()::makeAuthKey('activate_ra;'.$email.';'.$timestamp));
70
 
71
                        $message = OIDplus::gui()::getInvitationText($_POST['email']);
72
                        $message = str_replace('{{ACTIVATE_URL}}', $activate_url, $message);
73
 
5 daniel-mar 74
                        my_mail($email, OIDplus::config()->systemTitle().' - Invitation', $message, OIDplus::config()->globalCC());
2 daniel-mar 75
 
76
                        die("OK");
77
                }
78
 
79
                if ($_POST["action"] == "activate_ra") {
80
                        $handled = true;
81
 
82
                        $password1 = $_POST['password1'];
83
                        $password2 = $_POST['password2'];
84
                        $email = $_POST['email'];
85
                        $auth = $_POST['auth'];
86
                        $timestamp = $_POST['timestamp'];
87
 
88
                        if (!OIDplus::authUtils()::validateAuthKey('activate_ra;'.$email.';'.$timestamp, $auth)) {
89
                                die('Invalid auth key');
90
                        }
91
 
92
                        if ((OIDplus::config()->maxInviteTime() > 0) && (time()-$timestamp > OIDplus::config()->maxInviteTime())) {
93
                                die('Invitation expired!');
94
                        }
95
 
96
                        if ($password1 !== $password2) {
97
                                die('Passwords are not equal');
98
                        }
99
 
100
                        if (strlen($password1) < OIDplus::config()->minRaPasswordLength()) {
101
                                die('Password is too short. Minimum password length: '.OIDplus::config()->minRaPasswordLength());
102
                        }
103
 
104
                        $ra = new OIDplusRA($email);
105
                        $ra->register_ra($password1);
106
 
107
                        die('OK');
108
                }
109
 
110
                // === FORGOT PASSWORD ===
111
 
112
                if ($_POST["action"] == "forgot_password") {
113
                        $handled = true;
114
 
115
                        $email = $_POST['email'];
116
 
117
                        if (!oiddb_valid_email($email)) {
118
                                die('Invalid email address');
119
                        }
120
 
121
                        if (RECAPTCHA_ENABLED) {
122
                                $secret=RECAPTCHA_PRIVATE;
123
                                $response=$_POST["captcha"];
124
                                $verify=file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret={$secret}&response={$response}");
125
                                $captcha_success=json_decode($verify);
126
                                if ($captcha_success->success==false) {
127
                                        die('Captcha wrong');
128
                                }
129
                        }
130
 
131
                        $timestamp = time();
132
                        $activate_url = OIDplus::system_url() . '?goto='.urlencode('oidplus:reset_password$'.$email.'$'.$timestamp.'$'.OIDplus::authUtils()::makeAuthKey('reset_password;'.$email.';'.$timestamp));
133
 
134
                        $message = OIDplus::gui()::getForgotPasswordText($_POST['email']);
135
                        $message = str_replace('{{ACTIVATE_URL}}', $activate_url, $message);
136
 
5 daniel-mar 137
                        my_mail($email, OIDplus::config()->systemTitle().' - Password reset request', $message, OIDplus::config()->globalCC());
2 daniel-mar 138
 
139
                        die("OK");
140
                }
141
 
142
                if ($_POST["action"] == "reset_password") {
143
                        $handled = true;
144
 
145
                        $password1 = $_POST['password1'];
146
                        $password2 = $_POST['password2'];
147
                        $email = $_POST['email'];
148
                        $auth = $_POST['auth'];
149
                        $timestamp = $_POST['timestamp'];
150
 
151
                        if (!OIDplus::authUtils()::validateAuthKey('reset_password;'.$email.';'.$timestamp, $auth)) {
152
                                die('Invalid auth key');
153
                        }
154
 
155
                        if ((OIDplus::config()->maxPasswordResetTime() > 0) && (time()-$timestamp > OIDplus::config()->maxPasswordResetTime())) {
156
                                die('Invitation expired!');
157
                        }
158
 
159
                        if ($password1 !== $password2) {
160
                                die('Passwords are not equal');
161
                        }
162
 
163
                        if (strlen($password1) < OIDplus::config()->minRaPasswordLength()) {
164
                                die('Password is too short. Minimum password length: '.OIDplus::config()->minRaPasswordLength());
165
                        }
166
 
167
                        $ra = new OIDplusRA($email);
168
                        $ra->change_password($password1);
169
 
170
                        die('OK');
171
                }
172
 
173
                // === Admin / RA actions ===
174
 
175
                if ($_POST["action"] == "delete_ra") {
176
                        $handled = true;
177
 
178
                        $email = $_POST['email'];
179
 
180
                        $ra_logged_in = OIDplus::authUtils()::isRaLoggedIn($email);
181
 
182
                        if (!OIDplus::authUtils()::isAdminLoggedIn() && !$ra_logged_in) {
183
                                die('You need to log in as administrator');
184
                        }
185
 
186
                        if ($ra_logged_in) OIDplus::authUtils()::raLogout($email);
187
 
188
                        $ra = new OIDplusRA($email);
189
                        $ra->delete();
190
 
191
                        die('OK');
192
                }
193
 
194
                // === OID CRUD ===
195
 
196
                if ($_POST["action"] == "Delete") {
197
                        $handled = true;
198
 
199
                        $id = $_POST['id'];
200
                        $obj = OIDplusObject::parse($_POST['id']);
201
 
202
                        // Prüfen ob zugelassen
203
                        if (!$obj->userHasParentalWriteRights()) die('Authentification error. Please log in as the superior RA to delete this OID.');
204
 
13 daniel-mar 205
                        // Delete object
2 daniel-mar 206
                        OIDplus::db()->query("delete from ".OIDPLUS_TABLENAME_PREFIX."objects where id = '".OIDplus::db()->real_escape_string($id)."'");
207
 
13 daniel-mar 208
                        // Delete orphan stuff
209
                        $test = OIDplus::db()->query("select * from ".OIDPLUS_TABLENAME_PREFIX."objects where parent <> 'oid:' and parent like 'oid:%' and parent not in (select id from ".OIDPLUS_TABLENAME_PREFIX."objects where id like 'oid:%');");
210
                        if (OIDplus::db()->num_rows($test) > 0) {
211
                                OIDplus::db()->query("delete from ".OIDPLUS_TABLENAME_PREFIX."objects where parent <> 'oid:' and parent like 'oid:%' and parent not in (select id from ".OIDPLUS_TABLENAME_PREFIX."objects where id like 'oid:%');");
2 daniel-mar 212
                        }
13 daniel-mar 213
                        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:%');");
214
                        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:%');");
2 daniel-mar 215
 
216
                        echo "OK";
217
                }
218
                if ($_POST["action"] == "Update") {
219
                        $handled = true;
220
 
5 daniel-mar 221
                        // Es wird validiert: ra email, asn1 ids, iri ids
222
 
2 daniel-mar 223
                        $id = $_POST['id'];
224
                        $obj = OIDplusObject::parse($_POST['id']);
5 daniel-mar 225
 
226
                        // Validate RA email address
2 daniel-mar 227
                        $new_ra = $_POST['ra_email'];
5 daniel-mar 228
                        if (!empty($new_ra) && !oiddb_valid_email($new_ra)) {
229
                                die('Invalid RA email address');
230
                        }
2 daniel-mar 231
 
232
                        // Prüfen ob zugelassen
233
                        if (!$obj->userHasParentalWriteRights()) die('Authentification error. Please log in as the superior RA to update this OID.');
234
 
235
                        // RA ändern (rekursiv)
236
                        $res = OIDplus::db()->query("select ra_email from ".OIDPLUS_TABLENAME_PREFIX."objects where id = '".OIDplus::db()->real_escape_string($id)."'");
237
                        $row = OIDplus::db()->fetch_array($res);
238
                        $current_ra = $row['ra_email'];
239
 
240
                        if ($new_ra != $current_ra) _ra_change_rec($id, $current_ra, $new_ra); // Inherited RAs rekursiv mitändern
241
 
24 daniel-mar 242
                        // Replace ASN.1 und IRI IDs
2 daniel-mar 243
                        if ($obj::ns() == 'oid') {
244
                                $oid = $obj;
245
 
13 daniel-mar 246
                                $ids = ($_POST['iris'] == '') ? array() : explode(',',$_POST['iris']);
247
                                $ids = array_map('trim',$ids);
248
                                $oid->replaceIris($ids);
2 daniel-mar 249
 
13 daniel-mar 250
                                $ids = ($_POST['asn1ids'] == '') ? array() : explode(',',$_POST['asn1ids']);
251
                                $ids = array_map('trim',$ids);
252
                                $oid->replaceAsn1Ids($ids);
2 daniel-mar 253
                        }
254
 
255
                        $confidential = $_POST['confidential'] == 'true' ? '1' : '0';
256
                        if (!OIDplus::db()->query("UPDATE ".OIDPLUS_TABLENAME_PREFIX."objects SET confidential = ".OIDplus::db()->real_escape_string($confidential).", updated = now() WHERE id = '".OIDplus::db()->real_escape_string($id)."'")) {
257
                                die('Error at setting confidential flag:' . OIDplus::db()->error());
258
                        }
259
 
13 daniel-mar 260
                        echo "OK";
2 daniel-mar 261
 
13 daniel-mar 262
                        if (!empty($new_ra)) {
263
                                $res = OIDplus::db()->query("select ra_name from ".OIDPLUS_TABLENAME_PREFIX."ra where email = '".OIDplus::db()->real_escape_string($new_ra)."'");
264
                                if (OIDplus::db()->num_rows($res) == 0) echo " (RaNotInDatabase)"; // do not change
2 daniel-mar 265
                        }
266
                }
267
                if ($_POST["action"] == "Update2") {
268
                        $handled = true;
269
 
270
                        $id = $_POST['id'];
271
                        $obj = OIDplusObject::parse($_POST['id']);
272
 
273
                        // Prüfen ob zugelassen
274
                        if (!$obj->userHasWriteRights()) die('Authentification error. Please log in as the RA to update this OID.');
275
 
276
                        if (!OIDplus::db()->query("UPDATE ".OIDPLUS_TABLENAME_PREFIX."objects SET title = '".OIDplus::db()->real_escape_string($_POST['title'])."', description = '".OIDplus::db()->real_escape_string($_POST['description'])."', updated = now() WHERE id = '".OIDplus::db()->real_escape_string($id)."'")) {
277
                                die(OIDplus::db()->error());
278
                        }
279
 
13 daniel-mar 280
                        echo "OK";
2 daniel-mar 281
                }
282
                if ($_POST["action"] == "Insert") {
283
                        $handled = true;
284
 
5 daniel-mar 285
                        // Es wird validiert: ID, ra email, asn1 ids, iri ids
286
 
287
                        // Check if you have write rights on the parent (to create a new object)
2 daniel-mar 288
                        $objParent = OIDplusObject::parse($_POST['parent']);
289
                        if (!$objParent->userHasWriteRights()) die('Authentification error. Please log in as the correct RA to insert an OID at this arc.');
290
 
5 daniel-mar 291
                        // Check if the ID is valid
292
                        if ($_POST['id'] == '') die('ID may not be empty');
2 daniel-mar 293
 
294
                        // Absoluten OID namen bestimmen
24 daniel-mar 295
                        // Note: At addString() and parse(), the syntax of the ID will be checked
296
                        $id = $objParent->addString($_POST['id']);
2 daniel-mar 297
                        $obj = OIDplusObject::parse($id);
298
 
299
                        // Superior RA Änderung durchführen
300
                        $parent = $_POST['parent'];
301
                        $ra_email = $_POST['ra_email'];
5 daniel-mar 302
                        if (!empty($ra_email) && !oiddb_valid_email($ra_email)) {
303
                                die('Invalid RA email address');
304
                        }
2 daniel-mar 305
                        $confidential = $_POST['confidential'] == 'true' ? '1' : '0';
306
                        if (!OIDplus::db()->query("INSERT INTO ".OIDPLUS_TABLENAME_PREFIX."objects (id, parent, ra_email, confidential, created) VALUES ('".OIDplus::db()->real_escape_string($id)."', '".OIDplus::db()->real_escape_string($parent)."', '".OIDplus::db()->real_escape_string($ra_email)."', ".OIDplus::db()->real_escape_string($confidential).", now())")) {
307
                                die(OIDplus::db()->error());
308
                        }
309
 
5 daniel-mar 310
                        // Set ASN.1 und IRI IDs
2 daniel-mar 311
                        if ($obj::ns() == 'oid') {
312
                                $oid = $obj;
313
 
13 daniel-mar 314
                                $ids = ($_POST['iris'] == '') ? array() : explode(',',$_POST['iris']);
315
                                $ids = array_map('trim',$ids);
316
                                $oid->replaceIris($ids);
2 daniel-mar 317
 
13 daniel-mar 318
                                $ids = ($_POST['asn1ids'] == '') ? array() : explode(',',$_POST['asn1ids']);
319
                                $ids = array_map('trim',$ids);
320
                                $oid->replaceAsn1Ids($ids);
2 daniel-mar 321
                        }
322
 
13 daniel-mar 323
                        echo "OK";
2 daniel-mar 324
 
13 daniel-mar 325
                        if (!empty($ra_email)) {
326
                                $res = OIDplus::db()->query("select ra_name from ".OIDPLUS_TABLENAME_PREFIX."ra where email = '".OIDplus::db()->real_escape_string($ra_email)."'");
327
                                if (OIDplus::db()->num_rows($res) == 0) echo " (RaNotInDatabase)"; // do not change
2 daniel-mar 328
                        }
329
                }
330
 
331
                // === RA LOGIN/LOGOUT ===
332
 
333
                if ($_POST["action"] == "ra_login") {
334
                        $handled = true;
335
 
336
                        $ra = new OIDplusRA($_POST['email']);
337
 
338
                        if (RECAPTCHA_ENABLED) {
339
                                $secret=RECAPTCHA_PRIVATE;
340
                                $response=$_POST["captcha"];
341
                                $verify=file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret={$secret}&response={$response}");
342
                                $captcha_success=json_decode($verify);
343
                                if ($captcha_success->success==false) {
344
                                        die('Captcha wrong');
345
                                }
346
                        }
347
 
348
                        if ($ra->checkPassword($_POST['password'])) {
349
                                OIDplus::authUtils()::raLogin($_POST['email']);
350
 
351
                                if (!OIDplus::db()->query("UPDATE ".OIDPLUS_TABLENAME_PREFIX."ra set last_login = now() where email = '".OIDplus::db()->real_escape_string($_POST['email'])."'")) {
352
                                        die(OIDplus::db()->error());
353
                                }
354
 
355
                                echo "OK";
356
                        } else {
357
                                echo "Wrong password";
358
                        }
359
                }
360
                if ($_POST["action"] == "ra_logout") {
361
                        $handled = true;
362
                        OIDplus::authUtils()::raLogout($_POST['email']);
363
                        echo "OK";
364
                }
365
 
366
                // === ADMIN LOGIN/LOGOUT ===
367
 
368
                if ($_POST["action"] == "admin_login") {
369
                        $handled = true;
370
 
371
                        if (RECAPTCHA_ENABLED) {
372
                                $secret=RECAPTCHA_PRIVATE;
373
                                $response=$_POST["captcha"];
374
                                $verify=file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret={$secret}&response={$response}");
375
                                $captcha_success=json_decode($verify);
376
                                if ($captcha_success->success==false) {
377
                                        die('Captcha wrong');
378
                                }
379
                        }
380
 
381
                        if (OIDplus::authUtils()::adminCheckPassword($_POST['password'])) {
382
                                OIDplus::authUtils()::adminLogin();
383
                                echo "OK";
384
                        } else {
385
                                echo "Wrong password";
386
                        }
387
                }
388
                if ($_POST["action"] == "admin_logout") {
389
                        $handled = true;
390
                        OIDplus::authUtils()::adminLogout();
391
                        echo "OK";
392
                }
393
 
394
                // === Not found ===
395
 
396
                if (!$handled) {
397
                        die('Invalid action ID');
398
                }
399
        }
400
} catch (Exception $e) {
401
        echo $e->getMessage();
402
}
403
 
404
# ---
405
 
406
function _ra_change_rec($id, $old_ra, $new_ra) {
407
        OIDplus::db()->query("update ".OIDPLUS_TABLENAME_PREFIX."objects set ra_email = '".OIDplus::db()->real_escape_string($new_ra)."', updated = now() where id = '".OIDplus::db()->real_escape_string($id)."' and ifnull(ra_email,'') = '".OIDplus::db()->real_escape_string($old_ra)."'");
408
 
409
        $res = OIDplus::db()->query("select id from ".OIDPLUS_TABLENAME_PREFIX."objects where parent = '".OIDplus::db()->real_escape_string($id)."' and ifnull(ra_email,'') = '".OIDplus::db()->real_escape_string($old_ra)."'");
410
        while ($row = OIDplus::db()->fetch_array($res)) {
411
                _ra_change_rec($row['id'], $old_ra, $new_ra);
412
        }
413
}