-
Notifications
You must be signed in to change notification settings - Fork 0
/
submit.php
91 lines (76 loc) · 2.16 KB
/
submit.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
<?php
ini_set('display_errors', '1');
ini_set('display_startup_errors', '1');
error_reporting(E_ALL);
const DB_HOST = 'rdbms.strato.de';
const DB_NAME = 'dbs9305631';
const DB_USER = 'dbu5407203';
const DB_PASS = 'mukkeg-bowdan-3tIbco';
$dsn = "mysql:dbname=" . DB_NAME . ";host=" . DB_HOST;
$dbh = new PDO($dsn, DB_USER, DB_PASS);
function vd($input) {
echo '<pre>';
var_dump($input);
echo '</pre>';
}
function verifyPayload($payload) {
// Verify the general payload
if (
!isset($payload['uuid']) ||
!isset($payload['started']) ||
!isset($payload['ended']) ||
!isset($payload['results'])
) {
return 1;
}
// Verify result entries
foreach ($payload["results"] as $result) {
if (
!isset($result['task']) ||
!isset($result['answer'])
) {
return 2;
}
if (
!isset($result['task']['task']) ||
!isset($result['task']['hypothesis']) ||
!isset($result['task']['type']) ||
!isset($result['task']['name']) ||
!isset($result['task']['video_link'])
) {
return 3;
}
}
return 0;
}
if (!isset($_POST)) {
http_response_code(400);
return;
}
$sessionPayload = $_POST;
if (verifyPayload($sessionPayload) !== 0) {
http_response_code(400);
return;
}
$uuid = $_POST['uuid'];
$started = $_POST['started'];
$ended = $_POST['ended'];
$results = $_POST['results'];
$ip = $_SERVER['REMOTE_ADDR'];
$user_agent = $_SERVER['HTTP_USER_AGENT'];
$sth = $dbh->prepare("insert into mt_sessions (uuid, ip, user_agent, started, ended) values (?, ?, ?, ?, ?)");
$ret = $sth->execute([ $uuid, $ip, $user_agent, $started, $ended ]);
$sid = $dbh->lastInsertId();
foreach ($results as $result) {
$answer = $result['answer'];
$task = $result['task'];
$taskStr = $task['task'];
$hypothesis = $task['hypothesis'];
$type = $task['type'];
$name = $task['name'];
$video_link = $task['video_link'];
$comment = isset($result['comment']) && !empty($result['comment']) ? $result['comment'] : '';
$sth = $dbh->prepare("insert into mt_results (sid, answer, task, hypothesis, type, name, video_link, comment) values (?, ?, ?, ?, ?, ?, ?, ?)");
$ret = $sth->execute([ $sid, $answer, $taskStr, $hypothesis, $type, $name, $video_link, $comment]);
}
http_response_code(200);