-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconnectResponse.php
138 lines (122 loc) · 4.19 KB
/
connectResponse.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
<?php
if (!defined('CORRECT_ENTRY')) {
http_response_code(500);
echo 'Internal server error';
exit();
}
if($_SERVER['REQUEST_METHOD'] !== 'POST') {
http_response_code(405);
echo 'Method not allowed';
exit();
}
$poststr = file_get_contents('php://input');
$post_data = json_decode($poststr, true);
if(!isset($post_data) || !is_array($post_data) ||
!isset($post_data['type']) || !is_string($post_data['type']) ||
!isset($post_data['ver']) || !is_string($post_data['ver']) ||
!isset($post_data['establishId']) || !is_string($post_data['establishId']) ||
!isset($post_data['package']) || !is_array($post_data['package']) ) {
http_response_code(400);
echo 'Bad request';
exit();
}
$type = $post_data['type'];
$ver = $post_data['ver'];
$establishId = $post_data['establishId'];
$package = $post_data['package'];
if($ver != '0.3' && $ver != '0.4') {
http_response_code(400);
echo 'Bad request';
exit();
}
if($package === array_values($package)) {
http_response_code(400);
echo 'Bad request';
exit();
}
if($type != 'connection_accept') {
http_response_code(400);
echo 'Bad request';
exit();
}
$packageStatement = $pdo->prepare('SELECT * FROM prepared_connections WHERE profile = ? and establish_id = ?');
$packageStatement->bindValue(1, $profile['name'], PDO::PARAM_STR);
$packageStatement->bindValue(2, $establishId, PDO::PARAM_STR);
try {
$packageStatement->execute();
} catch (PDOException $e) {
http_response_code(500);
echo 'Internal server error';
exit();
}
$packageRow = $packageStatement->fetch();
$packageStatement->closeCursor();
if(!$packageRow) {
http_response_code(404);
echo 'Invalid establishId';
exit();
}
if($packageRow['expires'] < datetime_to_microtime($now)) {
http_response_code(404);
echo 'Invalid establishId';
exit();
}
$data = json_encode(array(
'received' => spxp_format_datetime($now),
'ver' => $ver,
'establishId' => $establishId,
'package' => $package
));
$createStatement = $pdo->prepare('INSERT INTO service_messages (profile,seqts,type,data) VALUES (?,?,2,?)');
$createStatement->bindValue(1, $profile['name'], PDO::PARAM_STR);
$createStatement->bindValue(2, datetime_to_microtime($now), PDO::PARAM_INT);
$createStatement->bindValue(3, $data, PDO::PARAM_STR);
try {
if(!($createStatement->execute() && $createStatement->rowCount() > 0)) {
http_response_code(500);
echo 'Internal server error';
exit();
}
} catch (PDOException $e) {
http_response_code(500);
echo 'Internal server error';
exit();
}
$activateKeysStatement = $pdo->prepare('INSERT IGNORE INTO profile_keys (profile, grp, rnd, enc_with_grp, enc_with_rnd, data, published) SELECT profile, grp, rnd, enc_with_grp, enc_with_rnd, data, published FROM prepared_keys WHERE profile = ? and establish_id = ?');
$activateKeysStatement->bindValue(1, $profile['name'], PDO::PARAM_STR);
$activateKeysStatement->bindValue(2, $establishId, PDO::PARAM_STR);
try {
$activateKeysStatement->execute();
} catch (PDOException $e) {
http_response_code(500);
echo 'Internal server error';
exit();
}
$deletePreparedKeysStatement = $pdo->prepare('DELETE FROM prepared_keys WHERE profile = ? and establish_id = ?');
$deletePreparedKeysStatement->bindValue(1, $profile['name'], PDO::PARAM_STR);
$deletePreparedKeysStatement->bindValue(2, $establishId, PDO::PARAM_STR);
try {
$deletePreparedKeysStatement->execute();
} catch (PDOException $e) {
http_response_code(500);
echo 'Internal server error';
exit();
}
$deletePreparedPackageStatement = $pdo->prepare('DELETE FROM prepared_connections WHERE profile = ? and establish_id = ?');
$deletePreparedPackageStatement->bindValue(1, $profile['name'], PDO::PARAM_STR);
$deletePreparedPackageStatement->bindValue(2, $establishId, PDO::PARAM_STR);
try {
$deletePreparedPackageStatement->execute();
} catch (PDOException $e) {
http_response_code(500);
echo 'Internal server error';
exit();
}
$responsePackage = json_decode($packageRow['package']);
header('Content-Type: application/json');
echo json_encode(array(
'type' => 'connection_finish',
'ver' => '0.3',
'establishId' => $establishId,
'package' => $responsePackage,
));