-
Notifications
You must be signed in to change notification settings - Fork 0
/
post.php
281 lines (242 loc) · 7.56 KB
/
post.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
<?php
/***************************************************************
* Copyright notice
*
* (c) 2003-2015 Renzo Lauper (renzo@churchtool.org)
* All rights reserved
*
* This script is part of the kOOL project. The kOOL project is
* free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* The GNU General Public License can be found at
* http://www.gnu.org/copyleft/gpl.html.
* A copy is found in the textfile GPL.txt and important notices to the license
* from the author is found in LICENSE.txt distributed with these scripts.
*
* kOOL is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* This copyright notice MUST APPEAR in all copies of the script!
***************************************************************/
function responseError($id, $section = null) {
$response = array();
$response['status'] = 'error';
if ($section === null) {
$llKey = 'error_post_';
}
else {
$llKey = 'error_' . 'post_' . $section . '_';
}
$response['message'] = getLL($llKey . $id);
print json_encode($response);
exit;
}
error_reporting(0);
$ko_path = "./";
$ko_menu_akt = 'post.php';
require($ko_path."config/ko-config.php");
require($ko_path."inc/ko.inc");
/*
// Small testsuite, set $test to {1..9}
$test = 0;
// error
if ($test == 1) {
$_POST['action'] = 'sddas';
$_POST['request'] = '';
$_POST['key'] = md5($_POST['action'] . KOOL_ENCRYPTION_KEY . $_POST['request']);
}
// error
else if ($test == 2) {
$_POST['action'] = 'newgroupsubscription';
$_POST['request'] = '';
$_POST['key'] = md5($_POST['action'] . KOOL_ENCRYPTION_KEY . $_POST['request']);
}
// error
else if ($test == 3) {
$_POST['action'] = 'newgroupsubscription';
$_POST['request'] = '';
$_POST['key'] = md5($_POST['action'] . KOOL_ENCRYPTION_KEY . 'sdfsdf' . $_POST['request']);
}
// success
else if ($test == 4) {
$_POST['action'] = 'newgroupsubscription';
$requestArray = array(
'_group_id' => 'g000607',
'_bemerkung' => 'Test Bemerkung',
'vorname' => 'John',
'nachname' => 'Doe',
);
$_POST['request'] = json_encode($requestArray);
$_POST['key'] = md5($_POST['action'] . KOOL_ENCRYPTION_KEY . $_POST['request']);
}
// error
else if ($test == 5) {
$_POST['action'] = 'newgroupsubscription';
$requestArray = array(
'_group_id' => '000607',
'_bemerkung' => 'Test Bemerkung',
'vorname' => 'John',
'nachname' => 'Doe',
);
$_POST['request'] = json_encode($requestArray);
$_POST['key'] = md5($_POST['action'] . KOOL_ENCRYPTION_KEY . $_POST['request']);
}
// success
else if ($test == 6) {
$_POST['action'] = 'newgroupsubscription';
$requestArray = array(
'_group_id' => 'g000607',
'_bemerkung' => 'Test Bemerkung',
'vorname' => 'John',
'nachname' => 'Doe',
'_group_datafields' => array('000008' => utf8_encode('Test Eintrag für Datenfeld "test_mod"')),
);
$_POST['request'] = json_encode($requestArray);
$_POST['key'] = md5($_POST['action'] . KOOL_ENCRYPTION_KEY . $_POST['request']);
}
// error
else if ($test == 7) {
$_POST['action'] = 'newgroupsubscription';
$requestArray = array(
'_group_id' => 'g000607',
'_bemerkung' => 'Test Bemerkung',
'vorname' => 'John',
'nachname' => 'Doe',
'_group_datafields' => array('000006' => utf8_encode('Test Eintrag für Datenfeld "test_mod"')),
);
$_POST['request'] = json_encode($requestArray);
$_POST['key'] = md5($_POST['action'] . KOOL_ENCRYPTION_KEY . $_POST['request']);
}
// error
else if ($test == 8) {
$_POST['action'] = 'newgroupsubscription';
$requestArray = array(
'_group_id' => 'g000607:r000400',
'_bemerkung' => 'Test Bemerkung',
'vorname' => 'John',
'nachname' => 'Doe',
'_group_datafields' => array('000005' => 'Test Eintrag fur Datenfeld "test_mod"'),
);
$_POST['request'] = json_encode($requestArray);
$_POST['key'] = md5($_POST['action'] . KOOL_ENCRYPTION_KEY . $_POST['request']);
}
// success
else if ($test == 9) {
$_POST['action'] = 'newgroupsubscription';
$requestArray = array(
'_group_id' => 'g000607:r000001',
'_bemerkung' => 'Test Bemerkung',
'vorname' => 'John',
'nachname' => 'Doe',
);
$_POST['request'] = json_encode($requestArray);
$_POST['key'] = md5($_POST['action'] . KOOL_ENCRYPTION_KEY . $_POST['request']);
}
*/
//Get request from _POST
$action = $_POST['action'];
$requestJSON = $_POST['request'];
$key = $_POST['key'];
if (!isset($action) || ! isset($key)) {
responseError(1);
}
// Check if encryption is set
if(!KOOL_ENCRYPTION_KEY) {
responseError(2);
}
// Check if request is valid
$checkSum = md5($action . KOOL_ENCRYPTION_KEY . $requestJSON);
if ($checkSum != $key) {
responseError(1);
}
$response = array();
switch ($action) {
case 'newgroupsubscription':
if ($requestJSON == '') {
responseError(1);
}
$request = json_decode($requestJSON, true);
// Decode UTF8 values
array_walk_recursive($request, "utf8_decode_array");
$groupString = format_userinput($request['_group_id'], 'alphanumlist');
if ($groupString == '') {
responseError(1, $action);
}
if (strpos($groupString, ':') !== false) {
list($groupId, $roleId) = explode(':', $groupString);
if (substr($groupId, 0, 1) != 'g') {
responseError(1, $action);
}
$groupId = substr($groupId, 1);
if (substr($roleId, 0, 1) != 'r') {
responseError(2, $action);
}
$roleId = substr($roleId, 1);
}
else {
if (substr($groupString, 0, 1) != 'g') {
responseError(1, $action);
}
$groupId = substr($groupString, 1);
$roleId = null;
}
$group = db_select_data('ko_groups', 'where id = ' . $groupId, 'id, roles, datafields, maxcount, count', '', '', TRUE, TRUE);
// Check if group id is valid
if ($group === null) {
responseError(1, $action);
}
// check if role id is valid
if ($roleId !== null && !in_array($roleId, explode(',', $group['roles']))) {
responseError(2, $action);
}
// Check if group if full
if ($group['maxcount'] != 0 && $group['count'] >= $group['maxcount']) {
responseError(3, $action);
}
// Check if all fields exist in ko_leute
$dbColumnsRaw = db_get_columns("ko_leute");
$dbColumns = array();
foreach ($dbColumnsRaw as $dbColumnRaw) $dbColumns[] = $dbColumnRaw['Field'];
foreach ($request as $requestColumn => $v) {
if (substr($requestColumn, 0, 1) != '_' && !in_array($requestColumn, $dbColumns)) {
responseError(4, $action);
}
}
// Check if datafields exist
$dbDataFields = explode(',', $group['datafields']);
$requestDataFields = $request['_group_datafields'];
foreach ($requestDataFields as $dataFieldName => $dataFieldValue) {
if ($dataFieldName != '' && !in_array($dataFieldName, $dbDataFields)) {
responseError(6, $action);
}
}
// Insert data into ko_leute_mod
$allowFilling = array('_bemerkung', '_group_id', '_group_datafields');
$insert = array();
foreach ($request as $requestColumn => $v) {
// Check if column is writable
if (substr($requestColumn, 0, 1) == '_' && !in_array($requestColumn, $allowFilling)) {
responseError(5, $action);
}
if ($requestColumn == '_group_datafields') {
$insert[$requestColumn] = serialize($v);
}
else {
$insert[$requestColumn] = $v;
}
}
$insert['_crdate'] = date("Y-m-d H:i:s");
// Insert new entry into ko_leute_mod
db_insert_data('ko_leute_mod', $insert);
$response['status'] = 'success';
break;
default:
responseError(1);
break;
}
print json_encode($response);