2017-03-11 06:29:58 +05:30
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdbool.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <sqlite3.h>
|
|
|
|
|
2017-03-13 09:43:44 +05:30
|
|
|
inline char *get_filename(const char *c) {
|
|
|
|
char *qout = "queries/mranks.sql";
|
|
|
|
if (c != NULL) {
|
|
|
|
switch(*c) {
|
|
|
|
case 'm':
|
|
|
|
qout = "queries/mleaderboard-ojoin.sql";
|
|
|
|
break;
|
|
|
|
case 'p':
|
|
|
|
qout = "queries/rplayers.sql";
|
|
|
|
break;
|
|
|
|
case 't':
|
|
|
|
qout = "queries/test.txt";
|
|
|
|
break;
|
|
|
|
}
|
2017-03-11 06:29:58 +05:30
|
|
|
}
|
|
|
|
return qout;
|
|
|
|
}
|
|
|
|
|
2017-03-13 08:33:51 +05:30
|
|
|
inline void print_tblheader(const char *c) {
|
2017-03-13 08:02:57 +05:30
|
|
|
char *out;
|
2017-03-13 08:33:51 +05:30
|
|
|
switch (*c) {
|
2017-03-13 08:02:57 +05:30
|
|
|
default:
|
|
|
|
out = "<TABLE class='center'>\
|
|
|
|
<TH class='tablename' COLSPAN='4'> <H3><BR>Map List</H3> </TH>\
|
|
|
|
<TR>\
|
|
|
|
<TH class='columnname'>Name</TH>\
|
|
|
|
<TH class='columnname'>Records</TH>\
|
|
|
|
<TH class='columnname'>Best Time</TH>\
|
|
|
|
<TH class='columnname'>Held By</TH>\
|
|
|
|
</TR>";
|
|
|
|
break;
|
|
|
|
case 'm':
|
|
|
|
out = "<TABLE class='center'>\
|
|
|
|
<TH class='tablename' COLSPAN='3'><H3><BR>Leaderboard</H3></TH>\
|
|
|
|
<TR>\
|
|
|
|
<TH class='columnname'>Rank</TH>\
|
|
|
|
<TH class='columnname'>Name</TH>\
|
|
|
|
<TH class='columnname'>Time</TH>\
|
|
|
|
</TR>";
|
|
|
|
break;
|
|
|
|
case 'p':
|
|
|
|
out = "<TABLE class='center'>\
|
|
|
|
<TH class='tablename' COLSPAN='3'> <H3><BR>Ranks</H3> </TH>\
|
|
|
|
<TR>\
|
|
|
|
<TH class='columnname'>Name</TH>\
|
|
|
|
<TH class='columnname'>Map</TH>\
|
|
|
|
<TH class='columnname'>Rank</TH>\
|
|
|
|
</TR>";
|
|
|
|
break;
|
|
|
|
}
|
2017-03-13 08:33:51 +05:30
|
|
|
printf("%s", out);
|
2017-03-13 08:02:57 +05:30
|
|
|
}
|
|
|
|
|
2017-03-13 09:43:44 +05:30
|
|
|
// display and format the results of the query.
|
2017-03-13 08:33:51 +05:30
|
|
|
void qresult(sqlite3_stmt *sp, const char *c) {
|
2017-03-13 08:02:57 +05:30
|
|
|
int e;
|
2017-03-13 08:33:51 +05:30
|
|
|
unsigned int i;
|
|
|
|
unsigned int cc = sqlite3_column_count(sp);
|
|
|
|
print_tblheader(c);
|
2017-03-13 08:02:57 +05:30
|
|
|
while ((e = sqlite3_step(sp)) == SQLITE_ROW) {
|
2017-03-13 08:33:51 +05:30
|
|
|
printf("<TR>");
|
2017-03-13 08:02:57 +05:30
|
|
|
for (i = 0; i < cc; i++) {
|
2017-03-13 08:33:51 +05:30
|
|
|
printf("<TD>%s</TD>", sqlite3_column_text(sp, i));
|
2017-03-13 08:02:57 +05:30
|
|
|
}
|
2017-03-13 08:33:51 +05:30
|
|
|
printf("</TR>");
|
2017-03-13 08:02:57 +05:30
|
|
|
}
|
2017-03-13 08:33:51 +05:30
|
|
|
printf("</TABLE>");
|
2017-03-13 08:02:57 +05:30
|
|
|
}
|
|
|
|
|
2017-03-13 09:43:44 +05:30
|
|
|
// const char 'sql' is never NULL.
|
|
|
|
// const char 'str' may be NULL, however it is only when
|
|
|
|
// the contents of mranks.sql is in const char 'sql'.
|
2017-03-13 08:33:51 +05:30
|
|
|
bool executequery(const char *sql, const char *str) {
|
2017-03-13 09:43:44 +05:30
|
|
|
const char *p = (str != NULL) ? strchr(str, '=') + 1 : NULL;
|
|
|
|
const char qc = (str != NULL) ? str[0] : '\0';
|
2017-03-13 08:02:57 +05:30
|
|
|
sqlite3 *db;
|
|
|
|
char *error;
|
2017-03-11 06:29:58 +05:30
|
|
|
int con = sqlite3_open("db/cts.db", &db);
|
|
|
|
if (con != SQLITE_OK) {
|
|
|
|
fprintf(stderr, "Can not open database: %s\n", sqlite3_errmsg(db));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
sqlite3_stmt *s;
|
|
|
|
// prepare sql to s, reading until null character.
|
|
|
|
con = sqlite3_prepare_v2(db, sql, -1, &s, NULL);
|
|
|
|
if (con != SQLITE_OK) {
|
|
|
|
fprintf(stderr, "Could not prepare SQL statement: %s\n", sqlite3_errmsg(db));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
// bind parameter with a string length that is the # of bytes to null char.
|
|
|
|
con = sqlite3_bind_text(s, 1, p, -1, NULL);
|
2017-03-13 08:33:51 +05:30
|
|
|
qresult(s, &qc);
|
2017-03-11 06:29:58 +05:30
|
|
|
sqlite3_finalize(s);
|
|
|
|
sqlite3_close(db);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// both allocates and frees memory used up by the string..
|
|
|
|
// containing the data from query files.
|
|
|
|
void getquery(const char *qs) {
|
|
|
|
char *qf, *qp;
|
2017-03-13 09:43:44 +05:30
|
|
|
char *fname = get_filename(qs);
|
2017-03-11 06:29:58 +05:30
|
|
|
if (fname != NULL) {
|
|
|
|
FILE *f = fopen(fname, "r");
|
|
|
|
if (f != NULL && qf != NULL) {
|
|
|
|
// change from fseek/SEEK_END when sql queries are >2 GB in size.
|
|
|
|
fseek(f, 0, SEEK_END); // go to the end of file
|
|
|
|
unsigned int size = ftell(f);
|
2017-03-11 07:10:03 +05:30
|
|
|
qf = calloc(size, sizeof(char));
|
2017-03-11 06:29:58 +05:30
|
|
|
if (qf != NULL) {
|
|
|
|
fseek(f, 0, SEEK_SET); // go to the start of file
|
|
|
|
fread(qf, sizeof(char), size, f);
|
|
|
|
}
|
|
|
|
}
|
2017-03-13 08:02:57 +05:30
|
|
|
fclose(f);
|
2017-03-11 06:29:58 +05:30
|
|
|
}
|
2017-03-13 08:33:51 +05:30
|
|
|
executequery(qf, qs);
|
2017-03-11 06:29:58 +05:30
|
|
|
free(qf);
|
|
|
|
}
|
|
|
|
|
|
|
|
void html(void) {
|
|
|
|
const char *html_top = "Content-Type: text/html\n\n\
|
|
|
|
<html><head><meta content=\"text/html; charset=UTF-8\" />\
|
|
|
|
<link rel=\"stylesheet\" type=\"text/css\" href=\"page.css\">\
|
|
|
|
<title>/v/ - Xonotic</title>\
|
|
|
|
<p class='hidden'>:-) / nice one<br></p>";
|
|
|
|
const char *html_bot = "<br><br><p classname='footer'>Pages under construction.<br>\
|
|
|
|
Service may sporadically become unavailable.<br>\
|
|
|
|
In-game database is not directly synced with this web server.\
|
|
|
|
</p>\
|
|
|
|
</body></html>";
|
2017-03-13 08:33:51 +05:30
|
|
|
printf("%s", html_top);
|
2017-03-13 08:02:57 +05:30
|
|
|
// getquery(getenv("QUERY_STRING"));
|
2017-03-13 09:43:44 +05:30
|
|
|
// getquery("map=pornstar-kaine");
|
|
|
|
getquery(NULL);
|
2017-03-13 08:33:51 +05:30
|
|
|
printf("%s", html_bot);
|
2017-03-11 06:29:58 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
int main(void) {
|
2017-03-13 08:33:51 +05:30
|
|
|
html();
|
|
|
|
// getquery("player=duHTyaSGpdTk7oebwPFoo899xPoTwP9bja4DUjCjTLo=");
|
2017-03-11 06:29:58 +05:30
|
|
|
}
|