-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpublish-post.php
78 lines (72 loc) · 2.17 KB
/
publish-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
<?php
if (!defined('CORRECT_ENTRY')) {
http_response_code(500);
echo 'Internal server error';
exit();
}
if(!isset($post_data['post']) || !is_array($post_data['post']) ||
!isset($post_data['token']) || !is_string($post_data['token'])) {
http_response_code(403);
echo 'Bad post data';
exit();
}
$post = $post_data['post'];
$token = $post_data['token'];
if($post === array_values($post)) {
http_response_code(400);
echo 'Bad request';
exit();
}
$tokenStatement = $pdo->prepare('SELECT `scope` FROM `publish` WHERE `profile` = ? and `token` = ?');
$tokenStatement->bindValue(1, $profile['name'], PDO::PARAM_STR);
$tokenStatement->bindValue(2, $token, PDO::PARAM_STR);
try {
$tokenStatement->execute();
} catch (PDOException $e) {
http_response_code(500);
echo 'Internal server error';
exit();
}
$tokenRow = $tokenStatement->fetch();
$tokenStatement->closeCursor();
if(!$tokenRow) {
http_response_code(403);
echo 'Invalid token';
exit();
}
$scope = $tokenRow['scope'];
if($scope == 'public') {
// TODO verify public post
} elseif($scope == 'private') {
// TODO verify private post
} else {
http_response_code(403);
echo 'Invalid scope';
exit();
}
$revokeTokenStatement = $pdo->prepare('DELETE `publish` WHERE `profile` = ? and `token` = ?');
$revokeTokenStatement->bindValue(1, $profile['name'], PDO::PARAM_STR);
$revokeTokenStatement->bindValue(2, $token, PDO::PARAM_STR);
try {
$tokenStatement->execute();
} catch (PDOException $e) {
http_response_code(500);
echo 'Internal server error';
exit();
}
$createStatement = $pdo->prepare('INSERT INTO posts (profile,seqts,data) VALUES (?,?,?)');
$createStatement->bindValue(1, $profile['name'], PDO::PARAM_STR);
$createStatement->bindValue(2, datetime_to_microtime($now), PDO::PARAM_INT);
$createStatement->bindValue(3, json_encode($post), PDO::PARAM_STR);
try {
if($createStatement->execute() && $createStatement->rowCount() > 0) {
http_response_code(204);
} else {
http_response_code(500);
echo 'Internal server error';
}
} catch (PDOException $e) {
http_response_code(500);
echo 'Internal server error';
exit();
}