-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy patheditPost.php
209 lines (194 loc) · 5.35 KB
/
editPost.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
<?php
/**
* api/editPost.php
*
* API endpoint to set value and organization data by the AI.
*/
/**
* Including the configuration and function loader.
*/
define('api', TRUE);
require_once(__DIR__.DIRECTORY_SEPARATOR."..".DIRECTORY_SEPARATOR.'includes'.DIRECTORY_SEPARATOR.'loader.php');
/**
* Set output format to JSON.
*/
header("Content-Type: application/json; charset=utf-8");
/**
* Check whether the script was called via HTTP-POST method.
*/
if($_SERVER['REQUEST_METHOD'] !== 'POST') {
http_response_code(405);
header('Access-Control-Allow-Methods: POST');
die(
json_encode(
[
'error' => 'HTTPMethod',
'errorMsg' => 'This endpoint must be called via HTTP POST.'
]
)
);
}
/**
* Check whether a token for transmission and an userId for the AI user is configured.
*/
if(empty($aiSettings['editPostToken']) OR empty($aiSettings['userId'])) {
http_response_code(501);
die(
json_encode(
[
'error' => 'apiNotConfigured',
'errorMsg' => 'This API was not properly configured.'
]
)
);
}
/**
* Read out the header and check whether the Auth header is set and correct.
*/
$headers = getallheaders();
if(empty($headers['apiAuth'])) {
http_response_code(400);
die(
json_encode(
[
'error' => 'tokenNotProvided',
'errorMsg' => 'You have to provide a token for your operation.'
]
)
);
}
if($headers['apiAuth'] != $aiSettings['editPostToken']) {
http_response_code(401);
die(
json_encode(
[
'error' => 'wrongToken',
'errorMsg' => 'The token you provided is wrong.'
]
)
);
}
/**
* Check the transferred body to see whether it contains valid JSON.
*/
$postData = json_decode(file_get_contents('php://input'), TRUE);
if(json_last_error() != JSON_ERROR_NONE) {
http_response_code(400);
die(
json_encode(
[
'error' => 'wrongFormat',
'errorMsg' => 'The post data has to be passed in JSON format.'
]
)
);
}
/**
* If an empty array was transmitted, then it should be cancelled.
* Prevents further parsing of {} as postData.
*/
if(empty($postData)) {
http_response_code(400);
die(
json_encode(
[
'error' => 'emptyPostData',
'errorMsg' => 'The postData field has to contain at least one entry.'
]
)
);
}
/**
* Check whether a itemId has been transferred.
*/
if(empty($postData['id']) OR intval($postData['id']) == 0) {
http_response_code(400);
die(
json_encode(
[
'error' => 'invalidId',
'errorMsg' => 'The id is invalid.'
]
)
);
}
/**
* Check whether the transmitted itemId exists.
*/
$itemId = intval(defuse($postData['id']));
$result = mysqli_query($dbl, "SELECT * FROM `items` WHERE `itemId`='".$itemId."' LIMIT 1") OR DIE(MYSQLI_ERROR($dbl));
if(mysqli_num_rows($result) == 0) {
http_response_code(404);
die(
json_encode(
[
'error' => 'idNotFound',
'errorMsg' => 'The id was not found.'
]
)
);
}
$row = mysqli_fetch_assoc($result);
/**
* Checking the optionally transferable value.
*/
if(isset($postData['amount']) AND (is_numeric($postData['amount']) AND $postData['amount'] != '')) {
/**
* Conversion of the value into a float number.
*/
$value = floatval(defuse($postData['amount']));
/**
* As the post exists, the system first checks whether an initial inspection has already been made.
*/
if($row['firstsightValue'] === NULL OR $row['firstsightUserId'] === NULL) {
/**
* Inserting the data when the first sight has not yet been made.
*/
mysqli_query($dbl, "UPDATE `items` SET `firstsightValue`='".$value."', `firstsightUserId`='".$aiSettings['userId']."' WHERE `itemId`='".$itemId."' LIMIT 1") OR DIE(MYSQLI_ERROR($dbl));
mysqli_query($dbl, "INSERT INTO `log` (`userId`, `logLevel`, `itemId`, `text`) VALUES ('".$aiSettings['userId']."', 2, '".$itemId."', '".number_format($value, 2, ",", ".")." €')") OR DIE(MYSQLI_ERROR($dbl));
}
}
/**
* Checking the optionally transferable organization.
*/
$orga = 0;
if((isset($postData['orga']) AND !empty($postData['orga']))) {
$orga = intval(defuse($postData['orga']));
$orgaResult = mysqli_query($dbl, "SELECT `id` FROM `metaOrganizations` WHERE `id`=".$orga." LIMIT 1") OR DIE(MYSQLI_ERROR($dbl));
/**
* If the organization does not exist, the process ends with an error message.
*/
if(mysqli_num_rows($orgaResult) == 0) {
http_response_code(404);
die(
json_encode(
[
'error' => 'organizationNotFound',
'errorMsg' => 'The organization with the provided id could not be found.'
]
)
);
} else {
/**
* A valid organization ID has been transmitted.
*/
if($row['firstsightOrgaId'] === NULL OR $row['firstsightOrgaUserId'] === NULL) {
/**
* Inserting the data when the first sight has not yet been made.
*/
mysqli_query($dbl, "UPDATE `items` SET `firstsightOrgaId`='".$orga."', `firstsightOrgaUserId`='".$aiSettings['userId']."' WHERE `itemId`='".$itemId."' LIMIT 1") OR DIE(MYSQLI_ERROR($dbl));
mysqli_query($dbl, "INSERT INTO `log` (`userId`, `logLevel`, `itemId`, `text`) VALUES ('".$aiSettings['userId']."', 2, '".$itemId."', 'Orga: ".$orga."')") OR DIE(MYSQLI_ERROR($dbl));
}
}
}
/**
* Everyting is okay.
*/
http_response_code(200);
die(json_encode(
[
'ok' => 'ok'
]
)
);
?>