/
backend.php
172 lines (164 loc) · 5.46 KB
/
backend.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
<?php
function arr($key, $array, $default = null) {
return isset($array[$key]) ? $array[$key] : $default;
}
function api() {
if (arr('SCRIPT_NAME', $_SERVER) != '/backend.php' || strpos(arr('CONTENT_TYPE', $_SERVER, ''), 'application/json') === false) {
return false;
}
$api = array_intersect_key(
json_decode(file_get_contents('php://input'), true), array_flip(array(
'jsonrpc', 'method', 'id', 'params'
)));
return $api;
}
function jsonrpc($response, $result, $keys = array(), $err_code = -1, $err_data = null) {
header('Expires: ' . gmdate('r', 0));
header('Content-type: application/json');
$jsonrpc = array(
'jsonrpc' => '2.0',
'id' => 1234,
);
switch ($result) {
case 0:
case 1:
$keys = array_flip($keys);
$jsonrpc['result'] = array($keys, $response);
break;
case 2:
$jsonrpc['error'] = array(
'code' => $err_code,
'message' => $response,
);
if ($err_data !== null) {
$jsonrpc['error']['data'] = $err_data;
}
break;
}
print json($jsonrpc);
exit;
}
function json($var) {
// return json_encode($var, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES | JSON_NUMERIC_CHECK);
return json_encode_php53($var);
}
function json_encode_php53($var) {
$json = preg_replace_callback('/\\\\u([0-9a-f]{4})/i', function($matches) {
return mb_convert_encoding(pack('H*', $matches[1]), 'UTF-8', 'UTF-16');
}, json_encode($var, JSON_NUMERIC_CHECK));
return $json;
}
function redirect($uri, $status_code = 302) {
send_header($status_code);
// Again, why are we doing this? :)
session_write_close();
session_regenerate_id(true);
header('Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0');
header('Location: ' . $uri);
exit(0);
}
/**
* Send header.
*
* @param int $status_code
*/
function send_header($status_code) {
$status_codes = array (
100 => 'Continue',
101 => 'Switching Protocols',
102 => 'Processing',
200 => 'OK',
201 => 'Created',
202 => 'Accepted',
203 => 'Non-Authoritative Information',
204 => 'No Content',
205 => 'Reset Content',
206 => 'Partial Content',
207 => 'Multi-Status',
300 => 'Multiple Choices',
301 => 'Moved Permanently',
302 => 'Found',
303 => 'See Other',
304 => 'Not Modified',
305 => 'Use Proxy',
307 => 'Temporary Redirect',
400 => 'Bad Request',
401 => 'Unauthorized',
402 => 'Payment Required',
403 => 'Forbidden',
404 => 'Not Found',
405 => 'Method Not Allowed',
406 => 'Not Acceptable',
407 => 'Proxy Authentication Required',
408 => 'Request Timeout',
409 => 'Conflict',
410 => 'Gone',
411 => 'Length Required',
412 => 'Precondition Failed',
413 => 'Request Entity Too Large',
414 => 'Request-URI Too Long',
415 => 'Unsupported Media Type',
416 => 'Requested Range Not Satisfiable',
417 => 'Expectation Failed',
422 => 'Unprocessable Entity',
423 => 'Locked',
424 => 'Failed Dependency',
426 => 'Upgrade Required',
500 => 'Internal Server Error',
501 => 'Not Implemented',
502 => 'Bad Gateway',
503 => 'Service Unavailable',
504 => 'Gateway Timeout',
505 => 'HTTP Version Not Supported',
506 => 'Variant Also Negotiates',
507 => 'Insufficient Storage',
509 => 'Bandwidth Limit Exceeded',
510 => 'Not Extended',
);
if (isset($status_codes[$status_code])) {
$status_string = "$status_code {$status_codes[$status_code]}";
header("{$_SERVER['SERVER_PROTOCOL']} $status_string", true, $status_code);
}
}
if ($api = api()) {
$servername = "localhost";
$dbname = "";
$username = "";
$password = "";
try {
$input = arr('score', $api['params'], 0);
switch ((string)$input) {
default:
$score = 0;
break;
case 'friberg':
$score = 1;
break;
case 'pasha':
$score = 2;
break;
}
$ip = arr('REMOTE_ADDR', $_SERVER);
$ts = time();
$conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
// set the PDO error mode to exception
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
if ($score > 0) {
$stmt = $conn->prepare("INSERT INTO score (score, ip, ts) VALUES (:score, :ip, :ts)");
$stmt->bindParam(':score', $score, PDO::PARAM_INT);
$stmt->bindParam(':ip', $ip);
$stmt->bindParam(':ts', $ts, PDO::PARAM_INT);
$stmt->execute();
}
$stmt = $conn->prepare(' select '
. ' (select count(*) from score where score = 1) as friberg,'
. ' (select count(*) from score where score = 2) as pasha,'
. ' (select count(*) from score) as total'
. ' from score group by score');
$stmt->execute();
$stmt->setFetchMode(PDO::FETCH_ASSOC);
$score = $stmt->fetchAll();
$score = $score[0];
} catch(PDOException $e) { }
jsonrpc(array(array_values($score)), 1, array_keys($score));
}