Subversion Repositories oidplus

Rev

Rev 107 | Rev 115 | 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
OIDplus::db()->set_charset("UTF8");
25
OIDplus::db()->query("SET NAMES 'utf8'");
26
 
27
header('Content-Type:application/json; charset=utf-8');
28
 
29
try {
30
        $handled = false;
31
 
32
        // Action:     (actions defined by plugins)
33
        // Method:     GET / POST
34
        // Parameters: ...
35
        // Outputs:    ...
36
        foreach (OIDplus::getPagePlugins('*') as $plugin) {
37
                $plugin->action($handled);
38
        }
39
 
40
        // Action:     get_description
41
        // Method:     GET / POST
42
        // Parameters: id
43
        // Outputs:    JSON
44
        if (isset($_REQUEST["action"]) && ($_REQUEST['action'] == 'get_description')) {
45
                $handled = true;
46
                if (!isset($_REQUEST['id'])) throw new Exception("Invalid args");
47
                try {
48
                        $out = OIDplus::gui()::generateContentPage($_REQUEST['id']);
49
                } catch(Exception $e) {
50
                        $out = array();
51
                        $out['title'] = 'Error';
52
                        $out['icon'] = 'img/error_big.png';
53
                        $out['text'] = $e->getMessage();
54
                }
55
                echo json_encode($out);
56
        }
57
 
108 daniel-mar 58
        // === jsTree ===
59
 
107 daniel-mar 60
        // Action:     tree_search
61
        // Method:     GET / POST
62
        // Parameters: search
63
        // Outputs:    JSON
64
        if (isset($_REQUEST["action"]) && ($_REQUEST['action'] == 'tree_search')) {
65
                $handled = true;
66
                if (!isset($_REQUEST['search'])) throw new Exception("Invalid args");
108 daniel-mar 67
 
68
                $found = false;
69
                foreach (OIDplus::getPagePlugins('*') as $plugin) {
70
                        $res = $plugin->tree_search($_REQUEST['search']);
71
                        if ($res) {
72
                                echo json_encode($res);
73
                                $found = true;
74
                                break;
75
                        }
107 daniel-mar 76
                }
108 daniel-mar 77
 
78
                if (!$found) {
79
                        echo json_encode(array());
80
                }
107 daniel-mar 81
        }
82
 
83
        // Action:     tree_load
84
        // Method:     GET / POST
85
        // Parameters: id; goto (optional)
86
        // Outputs:    JSON
87
        if (isset($_REQUEST["action"]) && ($_REQUEST['action'] == 'tree_load')) {
88
                $handled = true;
89
                if (!isset($_REQUEST['id'])) throw new Exception("Invalid args");
90
                $json = OIDplusTree::json_tree($_REQUEST['id'], isset($_REQUEST['goto']) ? $_REQUEST['goto'] : '');
91
                echo $json;
92
        }
93
 
94
        // === Admin / RA actions ===
95
 
96
        // Action:     delete_ra
97
        // Method:     POST
98
        // Parameters: email
99
        // Outputs:    Text
100
        if (isset($_POST["action"]) && ($_POST["action"] == "delete_ra")) {
101
                $handled = true;
102
 
103
                $email = $_POST['email'];
104
 
105
                $ra_logged_in = OIDplus::authUtils()::isRaLoggedIn($email);
106
 
107
                if (!OIDplus::authUtils()::isAdminLoggedIn() && !$ra_logged_in) {
108
                        throw new Exception('You need to log in as administrator');
109
                }
110
 
111
                if ($ra_logged_in) OIDplus::authUtils()::raLogout($email);
112
 
113
                $ra = new OIDplusRA($email);
114
                $ra->delete();
115
 
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);
130
 
131
                // Prüfen ob zugelassen
132
                if (!$obj->userHasParentalWriteRights()) throw new Exception('Authentification error. Please log in as the superior RA to delete this OID.');
133
 
134
                // Delete object
135
                OIDplus::db()->query("delete from ".OIDPLUS_TABLENAME_PREFIX."objects where id = '".OIDplus::db()->real_escape_string($id)."'");
136
 
137
                // Delete orphan stuff
138
                foreach (OIDplus::getRegisteredObjectTypes() as $ot) {
139
                        $where = "where parent <> '".OIDplus::db()->real_escape_string($ot::root())."' and " .
140
                                 "      parent like '".OIDplus::db()->real_escape_string($ot::root().'%')."' and " .
141
                                 "      parent not in (select id from ".OIDPLUS_TABLENAME_PREFIX."objects where id like '".OIDplus::db()->real_escape_string($ot::root().'%')."')";
142
                        do {
143
                                $res = OIDplus::db()->query("select * from ".OIDPLUS_TABLENAME_PREFIX."objects $where");
144
                                while ($row = OIDplus::db()->fetch_array($res)) {
145
                                        if (!OIDplus::db()->query("delete from ".OIDPLUS_TABLENAME_PREFIX."objects where id = '".OIDplus::db()->real_escape_string($row['id'])."'")) {
146
                                                throw new Exception(OIDplus::db()->error());
147
                                        }
148
                                }
149
                        } while (OIDplus::db()->num_rows($res) > 0);
150
                }
151
                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:%');");
152
                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:%');");
153
 
154
                echo json_encode(array("status" => 0));
155
        }
156
 
157
        // Action:     Update
158
        // Method:     POST
159
        // Parameters: id, ra_email, iris, asn1ids, confidential
160
        // Outputs:    Text
161
        if (isset($_POST["action"]) && ($_POST["action"] == "Update")) {
162
                $handled = true;
163
 
164
                $id = $_POST['id'];
165
                $obj = OIDplusObject::parse($id);
166
 
167
                // Validate RA email address
168
                $new_ra = $_POST['ra_email'];
169
                if (!empty($new_ra) && !oidplus_valid_email($new_ra)) {
170
                        throw new Exception('Invalid RA email address');
171
                }
172
 
173
                // Prüfen ob zugelassen
174
                if (!$obj->userHasParentalWriteRights()) throw new Exception('Authentification error. Please log in as the superior RA to update this OID.');
175
 
176
                // RA ändern (rekursiv)
177
                $res = OIDplus::db()->query("select ra_email from ".OIDPLUS_TABLENAME_PREFIX."objects where id = '".OIDplus::db()->real_escape_string($id)."'");
178
                $row = OIDplus::db()->fetch_array($res);
179
                $current_ra = $row['ra_email'];
180
 
181
                if ($new_ra != $current_ra) _ra_change_rec($id, $current_ra, $new_ra); // Inherited RAs rekursiv mitändern
182
 
183
                // Replace ASN.1 und IRI IDs
184
                if ($obj::ns() == 'oid') {
185
                        $oid = $obj;
186
 
187
                        $ids = ($_POST['iris'] == '') ? array() : explode(',',$_POST['iris']);
188
                        $ids = array_map('trim',$ids);
189
                        $oid->replaceIris($ids);
190
 
191
                        $ids = ($_POST['asn1ids'] == '') ? array() : explode(',',$_POST['asn1ids']);
192
                        $ids = array_map('trim',$ids);
193
                        $oid->replaceAsn1Ids($ids);
194
                }
195
 
196
                $confidential = $_POST['confidential'] == 'true' ? '1' : '0';
197
                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)."'")) {
198
                        throw new Exception('Error at setting confidential flag:' . OIDplus::db()->error());
199
                }
200
 
201
                $status = 0;
202
 
203
                if (!empty($new_ra)) {
204
                        $res = OIDplus::db()->query("select ra_name from ".OIDPLUS_TABLENAME_PREFIX."ra where email = '".OIDplus::db()->real_escape_string($new_ra)."'");
205
                        if (OIDplus::db()->num_rows($res) == 0) $status = 1;
206
                }
207
 
208
                echo json_encode(array("status" => $status));
209
        }
210
 
211
        // Action:     Update2
212
        // Method:     POST
213
        // Parameters: id, title, description
214
        // Outputs:    Text
215
        if (isset($_POST["action"]) && ($_POST["action"] == "Update2")) {
216
                $handled = true;
217
 
218
                $id = $_POST['id'];
219
                $obj = OIDplusObject::parse($id);
220
 
221
                // Prüfen ob zugelassen
222
                if (!$obj->userHasWriteRights()) throw new Exception('Authentification error. Please log in as the RA to update this OID.');
223
 
224
                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)."'")) {
225
                        throw new Exception(OIDplus::db()->error());
226
                }
227
 
228
                echo json_encode(array("status" => 0));
229
        }
230
 
231
        // Action:     Insert
232
        // Method:     POST
233
        // Parameters: parent, id, ra_email, confidential, iris, asn1ids
234
        // Outputs:    Text
235
        if (isset($_POST["action"]) && ($_POST["action"] == "Insert")) {
236
                $handled = true;
237
 
238
                // Es wird validiert: ID, ra email, asn1 ids, iri ids
239
 
240
                // Check if you have write rights on the parent (to create a new object)
241
                $objParent = OIDplusObject::parse($_POST['parent']);
242
                if (!$objParent->userHasWriteRights()) throw new Exception('Authentification error. Please log in as the correct RA to insert an OID at this arc.');
243
 
244
                // Check if the ID is valid
245
                if ($_POST['id'] == '') throw new Exception('ID may not be empty');
246
 
247
                // Absoluten OID namen bestimmen
248
                // Note: At addString() and parse(), the syntax of the ID will be checked
249
                $id = $objParent->addString($_POST['id']);
250
                $obj = OIDplusObject::parse($id);
251
 
252
                // Superior RA Änderung durchführen
253
                $parent = $_POST['parent'];
254
                $ra_email = $_POST['ra_email'];
255
                if (!empty($ra_email) && !oidplus_valid_email($ra_email)) {
256
                        throw new Exception('Invalid RA email address');
257
                }
258
                $confidential = $_POST['confidential'] == 'true' ? '1' : '0';
259
                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())")) {
260
                        throw new Exception(OIDplus::db()->error());
261
                }
262
 
263
                // Set ASN.1 und IRI IDs
264
                if ($obj::ns() == 'oid') {
265
                        $oid = $obj;
266
 
267
                        $ids = ($_POST['iris'] == '') ? array() : explode(',',$_POST['iris']);
268
                        $ids = array_map('trim',$ids);
269
                        $oid->replaceIris($ids);
270
 
271
                        $ids = ($_POST['asn1ids'] == '') ? array() : explode(',',$_POST['asn1ids']);
272
                        $ids = array_map('trim',$ids);
273
                        $oid->replaceAsn1Ids($ids);
274
                }
275
 
276
                $status = 0;
277
 
278
                if (!empty($ra_email)) {
279
                        $res = OIDplus::db()->query("select ra_name from ".OIDPLUS_TABLENAME_PREFIX."ra where email = '".OIDplus::db()->real_escape_string($ra_email)."'");
280
                        if (OIDplus::db()->num_rows($res) == 0) $status = 1;
281
                }
282
 
283
                echo json_encode(array("status" => $status));
284
        }
285
 
286
        if (!$handled) {
287
                throw new Exception('Invalid action ID');
288
        }
289
} catch (Exception $e) {
290
        $ary = array();
291
        $ary['error'] = $e->getMessage();
292
        echo json_encode($ary);
293
}
294
 
295
# ---
296
 
297
function _ra_change_rec($id, $old_ra, $new_ra) {
298
        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)."'");
299
 
300
        $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)."'");
301
        while ($row = OIDplus::db()->fetch_array($res)) {
302
                _ra_change_rec($row['id'], $old_ra, $new_ra);
303
        }
304
}