33
33
query[clen] = '\0';
35
35
if(query) tcwwwformdecode(query, params);
36
const char *type = tcmapget4(params, "type", "");
36
37
const char *author = tcmapget4(params, "author", "");
37
38
const char *text = tcmapget4(params, "text", "");
38
39
int page = tcatoi(tcmapget4(params, "page", "1"));
39
40
const char *dbpath = tctmplconf(tmpl, "dbpath");
40
41
if(!dbpath) dbpath = "casket.tct";
41
42
TCLIST *msgs = tcmpoollistnew(mpool);
42
TCTDB *tdb = tctdbnew();
43
tcmpoolpush(mpool, tdb, (void (*)(void *))tctdbdel);
43
TCTDB *tdb = tcmpoolpush(mpool, tctdbnew(), (void (*)(void *))tctdbdel);
44
44
rp = getenv("REQUEST_METHOD");
45
45
if(rp && !strcmp(rp, "POST") && *author != '\0' && *text != '\0'){
46
46
if(!tctdbopen(tdb, dbpath, TDBOWRITER | TDBOCREAT))
59
59
tclistprintf(msgs, "The database could not be opened (%s).", tctdberrmsg(tctdbecode(tdb)));
61
61
TCLIST *logs = tcmpoollistnew(mpool);
62
TDBQRY *qry = tctdbqrynew(tdb);
63
tcmpoolpush(mpool, qry, (void (*)(void *))tctdbqrydel);
62
TDBQRY *qry = tcmpoolpush(mpool, tctdbqrynew(tdb), (void (*)(void *))tctdbqrydel);
64
63
tctdbqrysetorder(qry, "", TDBQONUMDESC);
65
64
tctdbqrysetlimit(qry, 16, page > 0 ? (page - 1) * 16 : 0);
66
TCLIST *res = tctdbqrysearch(qry);
67
tcmpoolpushlist(mpool, res);
65
TCLIST *res = tcmpoolpushlist(mpool, tctdbqrysearch(qry));
68
66
int rnum = tclistnum(res);
69
67
for(int i = rnum - 1; i >= 0; i--){
71
69
const char *pkbuf = tclistval(res, i, &pksiz);
72
TCMAP *cols = tctdbget(tdb, pkbuf, pksiz);
70
TCMAP *cols = tcmpoolpushmap(mpool, tctdbget(tdb, pkbuf, pksiz));
74
tcmpoolpushmap(mpool, cols);
75
72
tcmapprintf(cols, "pk", "%s", pkbuf);
77
74
tcdatestrwww(tcatoi(pkbuf) / 1000, INT_MAX, date);
87
84
tcmapprintf(vars, "author", "%s", author);
88
85
if(page > 1) tcmapprintf(vars, "prev", "%d", page - 1);
89
86
if(tctdbrnum(tdb) > page * 16) tcmapprintf(vars, "next", "%d", page + 1);
90
char *str = tctmpldump(tmpl, vars);
91
printf("Content-Type: text/html; charset=UTF-8\r\n");
87
char *str = tcmpoolpushptr(mpool, tctmpldump(tmpl, vars));
88
printf("Content-Type: %s\r\n", !strcmp(type, "xml") ? "application/xml" :
89
!strcmp(type, "xhtml") ? "application/xhtml+xml" : "text/html; charset=UTF-8");
90
printf("Cache-Control: no-cache\r\n");
93
92
fwrite(str, 1, strlen(str), stdout);