-
Notifications
You must be signed in to change notification settings - Fork 0
/
message.php
179 lines (157 loc) · 4.55 KB
/
message.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
<?php
$retVal["status"]="Internal Error";
$retVal["code"]=-1;
//TODO enforce max length on all user supplied data
/*
Iterate over all keys in posted data
Err out if too long
*/
//parse input to json
$http_raw=file_get_contents("php://input");
if(!empty($http_raw)){
$input=json_decode($http_raw,true);
}
else{
$retVal["status"]="No parameters supplied";
die(json_encode($retVal));
}
if($input===null){
$retVal["status"]="Could not decode posted JSON";
die(json_encode($retVal));
}
if(empty($input["auth"]["name"])||empty($input["auth"]["secret"])||empty($input["auth"]["session"])){
$retVal["status"]="Missing credentials";
die(json_encode($retVal));
}
$retVal["player"]=$input["auth"]["name"]."!".md5(hash("sha256",$input["auth"]["secret"].$input["auth"]["name"]));
if(!empty($input["game"]["id"])){
$retVal["handle"]=hash("sha256",$input["auth"]["session"].$input["game"]["id"].$retVal["player"]);
}
else{
$retVal["handle"]="invalid handle";
}
//open db
$db=new PDO("sqlite:/var/www/tictac/tictac.db3");
//get messages
if(isset($_GET["get"])){
//query all messages for this id & gid
$GET_STMT=$db->prepare("SELECT * FROM messages WHERE receiver=:target LIMIT 50");
$DEL_STMT=$db->prepare("DELETE FROM messages WHERE msgid=:msg");
$failTrans=false;
$db->beginTransaction();
$state=$GET_STMT->execute(array($retVal["handle"]));
if($state){
//fetch rows
$ROW=$GET_STMT->fetch(PDO::FETCH_ASSOC);
while($ROW!==FALSE){
//execute delete
$state=$DEL_STMT->execute(array($ROW["msgid"]));
$DEL_STMT->closeCursor();
if(!$state){
$retVal["status"]="Could not delete message ".$ROW["msgid"];
$failTrans=true;
break;
}
//build array
$msg["sender"]=$ROW["sender"];
$msg["type"]=$ROW["type"];
$msg["data"]=json_decode($ROW["data"],true);
$retVal["messages"][]=$msg;
$ROW=$GET_STMT->fetch(PDO::FETCH_ASSOC);
}
}
else{
$retVal["status"]="Could not fetch messages";
$failTrans=true;
}
$GET_STMT->closeCursor();
if($failTrans){
$db->rollBack();
}
else{
$db->commit();
$retVal["status"]="Messages fetched for ".$retVal["handle"];
$retVal["code"]=1;
}
}
//create game
if(isset($_GET["gid"])){
$retVal["game"]["id"]=mt_rand();
$retVal["game"]["target"]=hash("sha256",$input["auth"]["session"].$retVal["game"]["id"].$retVal["player"]);
$retVal["code"]=1;
$retVal["status"]="Generated Game Identifier";
}
//push status query
if(isset($_GET["getstate"])){
if(!empty($input["game"]["target"])&&!empty($input["game"]["id"])){
storeMessage($retVal["handle"], $input["game"]["target"], "getstate", json_encode(array('hashname'=>$retVal["player"])));
}
else{
$retVal["status"]="No target specified";
}
}
if(isset($_GET["pushstate"])){
//(game:id)
//(to)
//(meta:current|x|o)
//[moves]
//validate posted json
if(!(empty($input["game"]["id"])||empty($input["to"]))){
if(!(empty($input["meta"]["currentplayer"])||empty($input["meta"]["player_x"])||empty($input["meta"]["player_o"]))){
//TODO validate move array
storeMessage($retVal["handle"], $input["to"], "pushstate", json_encode(array('game'=>$input["game"],'meta'=>$input["meta"])));
}
else{
$retVal["status"]="Meta info incomplete";
}
}
else{
$retVal["status"]="No target/GID specified";
}
}
if(isset($_GET["move"])){
//to
//game
//move
//check outer parameters
if(!(empty($input["game"]["id"])||empty($input["to"]))){
//check move parameter
if(empty($input["move"]["inner"])||empty($input["move"]["outer"])||empty($input["move"]["player"])){
$retVal="Move structure invalid";
}
else{
storeMessage($retVal["handle"], $input["to"], "move", json_encode(array('game'=>$input["game"],'move'=>$input["move"])));
}
}
else{
$retVal["status"]="No target/GID specified";
}
}
print(json_encode($retVal));
function storeMessage($sender, $receiver, $type, $data){
global $db;
global $retVal;
$STMT=$db->prepare("INSERT INTO messages (sender,time,receiver,type,data) VALUES (:from, :time, :to, :type, :data)");
if($STMT===FALSE){
$retVal["status"]="Failed to prepare statement";
}
else{
$state=$STMT->execute(
array( ':from' =>$sender,
':time' =>time(),
':to' =>$receiver,
':type' =>$type,
':data' =>$data
));
if($state===FALSE){
$err=$STMT->errorInfo();
$retVal["status"]="DB Failed: ".$err[2];
}
else{
$retVal["code"]=1;
$retVal["status"]="Message sent";
}
$STMT->closeCursor();
}
}
?>