Subversion Repositories oidplus

Rev

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