forked from coandco/TSSSFF-Generator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dbInterface.php
144 lines (131 loc) · 5.09 KB
/
dbInterface.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
<?php
#error_reporting(0);
error_reporting(ALL);
function uniqueKey($start,$field){
$key = substr(md5($start),0,32);
$tries = 0;
#while(pg_fetch_row(pg_query("SELECT COUNT(1) FROM tsssff_savedcards2 WHERE $field='$key'"))){
# $key = substr(hash("sha2",$start+$tries),0,32);
# $tries++;
#}
return $key;
}
function dieError($error,$details){
die(json_encode(Array(
"error"=>$error,
"details"=>$details
)));
}
function getCard($mode,$key){
$key = pg_escape_string($key);
$query = "SELECT * FROM tsssff_savedcards2 WHERE ${mode}Key = '$key';";
$result = pg_query($query) or dieError("Query error getting card",pg_last_error());
$card = pg_fetch_assoc($result);
if ($card){
foreach($card as &$v){
$v = trim($v);
}
}
return $card;
}
function getRange($minViewKey,$amount){
$minViewKey = pg_escape_string($minViewId);
$amount = pg_escape_string($amount);
$query = "SELECT viewKey,classes,name,attr,image,copyright FROM tsssff_savedcards2 WHERE viewKey >= '$minViewKey' ORDER BY viewKey LIMIT $amount;";
$result = pg_query($query) or dieError("Query error getting cards",pg_last_error());
$cards = pg_fetch_all($result);
if (!$cards){
dieError("Query error getting cards",pg_last_error());
}
return $cards;
}
function putCard($editKey,$classes,$name,$attr,$effect,$flavour,$image,$copyright){
if (is_null($editKey) or $editKey == ''){
$editKey = uniqueKey(rand(),"editkey");
$viewKey = uniqueKey($editKey,"viewkey");
} else {
if (!preg_match("/^[0-9a-f]+$/",$editKey)){
dieError("Invalid paramater","editKey ($editKey) was not valid or is a reserved key");
}
$editKey = pg_escape_string($editKey);
}
if($viewKey){
$query =
"INSERT INTO tsssff_savedcards2 VALUES (
E'$editKey',E'$viewKey',E'$classes',E'$name',E'$attr',E'$effect',E'$flavour',E'$image',E'$copyright'
) RETURNING editKey, viewKey;";
} else {
$query =
"UPDATE tsssff_savedcards2 SET
classes = E'$classes',
name = E'$name',
attr = E'$attr',
effect = E'$effect',
flavour = E'$flavour',
image = E'$image',
copyright = E'$copyright'
WHERE
editKey = E'$editKey'
RETURNING editKey,viewKey;";
}
$result = pg_query($query) or die(json_encode(Array(
"error"=>'Putting card failed',
"details"=>pg_last_error()
)));
return pg_fetch_assoc($result);
}
if ($_SERVER['REQUEST_METHOD'] === 'GET'){ #Get a card from the database
$conn = pg_connect("host=/var/run/postgresql/ dbname=ripp_ user=ripp_");
if(!$conn) {
dieError("Error connecting to PostGresSQL",pg_last_error());
}
$mode="";
if(array_key_exists("edit",$_GET)) {
$mode = "edit";
}
if(array_key_exists("view",$_GET)) {
if ($mode){
dieError("Invalid request","Both edit and view paramaters given");
}
$mode = "view";
}
if(!$mode){
dieError("Invalid request","One of edit or view paramaters must be given");
}
if (array_key_exists("amount",$_GET)){
if ($mode == "view"){
print json_encode(getRange($view,$_GET["amount"]));
} else {
dieError("amount paramater only valid with view paramater");
}
} else {
$card = getCard($mode,$_GET[$mode]);
if (!$card){
$card = getCard("view","SPC-404");
}
if ($mode != "edit" or $card["editkey"] != $_GET[$mode]){
unset($card["editkey"]);
}
print json_encode($card);
}
} else if ($_SERVER['REQUEST_METHOD'] === 'POST'){ #Save a card to the database
$conn = pg_connect("host=/var/run/postgresql/ dbname=ripp_ user=ripp_");
if(!$conn){
dieError("Error connecting to PostGresSQL",pg_last_error());
}
$classes = pg_escape_string($_POST["classes"]);
$name = pg_escape_string($_POST["name"]);
$attr = pg_escape_string($_POST["attr"]);
$effect = pg_escape_string($_POST["effect"]);
$flavour = pg_escape_string($_POST["flavour"]);
$image = pg_escape_string($_POST["image"]);
$copyright = pg_escape_string($_POST["copyright"]);
if(array_key_exists("editkey",$_POST)){
$editKey = pg_escape_string($_POST["editkey"]);
} else {
$editKey = null;
}
print json_encode(putCard($editKey,$classes,$name,$attr,$effect,$flavour,$image,$copyright));
print pg_last_error();
}
?>