-
Notifications
You must be signed in to change notification settings - Fork 0
/
promenade.c
1819 lines (1776 loc) · 68.3 KB
/
promenade.c
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*************************************************************************************************
* The Web interface of Tokyo Promenade
* Copyright (C) 2008-2010 Mikio Hirabayashi
* This file is part of Tokyo Promenade.
* This program is free software: you can redistribute it and/or modify it under the terms of
* the GNU General Public License as published by the Free Software Foundation, either version
* 3 of the License, or any later version.
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU General Public License for more details.
* You should have received a copy of the GNU General Public License along with this program.
* If not, see <http://www.gnu.org/licenses/>.
*************************************************************************************************/
#include "common.h"
#include "scrext.h"
#if defined(MYFCGI)
#include <fcgi_stdio.h>
#endif
#define SALTNAME "[salt]" // dummy user name of the salt
#define RIDDLENAME "[riddle]" // dummy user name of the riddle
#define ADMINNAME "admin" // user name of the administrator
typedef struct { // type of structure for a record
int64_t id; // ID of the article
int64_t date; // date
const char *owner; // owner
const char *text; // text
} COMMENT;
/* global variables */
time_t g_starttime = 0; // start time of the process
TCMPOOL *g_mpool = NULL; // global memory pool
TCTMPL *g_tmpl = NULL; // template serializer
TCMAP *g_users = NULL; // user list
void *g_scrextproc = NULL; // processor of the script extension
unsigned long g_eventcount = 0; // event counter
const char *g_scriptname; // script name
const char *g_scriptprefix; // script prefix
const char *g_scriptpath; // script path
const char *g_docroot; // document root
const char *g_database; // path of the database file
const char *g_password; // path of the password file
const char *g_upload; // path of the upload directory
const char *g_uploadpub; // public path of the upload directory
const char *g_scrext; // path of the script extension file
int64_t g_recvmax; // maximum size of received data
const char *g_title; // site title
int g_searchnum; // number of articles in a search page
int g_listnum; // number of articles in a list page
int g_feedlistnum; // number of articles in a RSS feed
int g_filenum; // number of files in a file list page
int g_sidebarnum; // number of items in the side bar
const char *g_commentmode; // comment mode
const char *g_updatecmd; // path of the update command
int g_sessionlife; // lifetime of each session
const char *g_frontpage; // name of the front page
/* function prototypes */
int main(int argc, char **argv);
static int realmain(int argc, char **argv);
static void showerror(int code, const char *msg);
static void showcache(void);
static void readpasswd(void);
static bool writepasswd(void);
static void dosession(TCMPOOL *mpool);
static void setdberrmsg(TCLIST *emsgs, TCTDB *tdb, const char *msg);
static void setarthtml(TCMPOOL *mpool, TCMAP *cols, int64_t id, int bhl, bool tiny);
static TCLIST *searcharts(TCMPOOL *mpool, TCTDB *tdb, const char *cond, const char *expr,
const char *order, int max, int skip, bool ls);
static void getdaterange(const char *expr, int64_t *lowerp, int64_t *upper);
static bool putfile(TCMPOOL *mpool, const char *path, const char *name,
const char *ptr, int size);
static bool outfile(TCMPOOL *mpool, const char *path);
static TCLIST *searchfiles(TCMPOOL *mpool, const char *expr, const char *order,
int max, int skip, bool thum);
static int comparecomments(const TCLISTDATUM *a, const TCLISTDATUM *b);
static bool doupdatecmd(TCMPOOL *mpool, const char *mode, const char *baseurl, const char *user,
double now, int64_t id, TCMAP *ncols, TCMAP *ocols);
/* main routine */
int main(int argc, char **argv){
#if defined(MYFCGI)
g_starttime = time(NULL);
g_mpool = tcmpoolnew();
int rv = 0;
while(FCGI_Accept() >= 0){
g_eventcount++;
if(realmain(argc, argv) != 0) rv = 1;
}
tcmpooldel(g_mpool);
return rv;
#else
g_starttime = time(NULL);
g_mpool = tcmpoolnew();
int rv = realmain(argc, argv);
tcmpooldel(g_mpool);
return rv;
#endif
}
/* real main routine */
static int realmain(int argc, char **argv){
TCMPOOL *mpool = tcmpoolnew();
g_scriptname = getenv("SCRIPT_NAME");
if(!g_scriptname) g_scriptname = argv[0];
char *prefix = tcmpoolpushptr(mpool, tcregexreplace(g_scriptname, "\\.[a-zA-Z0-9]*$", ""));
g_scriptprefix = prefix;
g_scriptpath = getenv("SCRIPT_FILENAME");
if(!g_scriptpath) g_scriptpath = argv[0];
g_docroot = getenv("DOCUMENT_ROOT");
if(!g_docroot){
g_docroot = "/";
if(*g_scriptpath == '/'){
int diff = strlen(g_scriptpath) - strlen(g_scriptname);
if(diff > 0 && !strcmp(g_scriptpath + diff, g_scriptname))
g_docroot = tcmpoolpushptr(mpool, tcmemdup(g_scriptpath, diff));
}
}
char *barepath = tcmpoolpushptr(mpool, tcregexreplace(g_scriptpath, "\\.[a-zA-Z0-9]*$", ""));
char *tmplpath = tcmpoolpushptr(mpool, tcsprintf("%s.tmpl", barepath));
const char *rp = getenv("REQUEST_URI");
if(rp && *rp == '/'){
char *buf = tcmpoolpushptr(mpool, strdup(rp));
char *pv = strchr(buf, '#');
if(pv) *pv = '\0';
pv = strchr(buf, '?');
if(pv) *pv = '\0';
if(*buf != '\0') g_scriptname = buf;
}
if(g_tmpl){
if(g_users){
int64_t mtime;
if(tcstatfile(g_password, NULL, NULL, &mtime) && mtime >= g_starttime){
tcmapclear(g_users);
readpasswd();
}
}
dosession(mpool);
} else {
g_tmpl = tcmpoolpush(g_mpool, tctmplnew(), (void (*)(void *))tctmpldel);
if(tctmplload2(g_tmpl, tmplpath)){
g_database = tctmplconf(g_tmpl, "database");
if(!g_database) g_database = "promenade.tct";
g_password = tctmplconf(g_tmpl, "password");
if(g_password){
g_users = tcmpoolpushmap(g_mpool, tcmapnew2(TINYBNUM));
readpasswd();
}
g_upload = tctmplconf(g_tmpl, "upload");
g_uploadpub = NULL;
if(g_upload && *g_upload != '\0'){
if(!strchr(g_upload, '/')){
g_uploadpub = g_upload;
} else {
char *rpath = tcmpoolpushptr(g_mpool, tcrealpath(g_upload));
if(rpath){
if(tcstrfwm(rpath, g_docroot)){
int plen = strlen(g_docroot);
if(rpath[plen] == '/') g_uploadpub = rpath + plen;
}
}
}
}
g_scrext = tctmplconf(g_tmpl, "scrext");
if(g_scrext && *g_scrext != '\0')
g_scrextproc = tcmpoolpush(g_mpool, scrextnew(), scrextdel);
const char *rp = tctmplconf(g_tmpl, "recvmax");
g_recvmax = rp ? tcatoix(rp) : INT_MAX;
g_title = tctmplconf(g_tmpl, "title");
if(!g_title) g_title = "Tokyo Promenade";
rp = tctmplconf(g_tmpl, "searchnum");
g_searchnum = tclmax(rp ? tcatoi(rp) : 10, 1);
rp = tctmplconf(g_tmpl, "listnum");
g_listnum = tclmax(rp ? tcatoi(rp) : 10, 1);
rp = tctmplconf(g_tmpl, "feedlistnum");
g_feedlistnum = tclmax(rp ? tcatoi(rp) : 10, 1);
rp = tctmplconf(g_tmpl, "filenum");
g_filenum = tclmax(rp ? tcatoi(rp) : 10, 1);
rp = tctmplconf(g_tmpl, "sidebarnum");
g_sidebarnum = tclmax(rp ? tcatoi(rp) : 0, 0);
g_commentmode = tctmplconf(g_tmpl, "commentmode");
if(!g_commentmode) g_commentmode = "";
g_updatecmd = tctmplconf(g_tmpl, "updatecmd");
if(!g_updatecmd) g_updatecmd = "";
rp = tctmplconf(g_tmpl, "sessionlife");
g_sessionlife = tclmax(rp ? tcatoi(rp) : 0, 0);
g_frontpage = tctmplconf(g_tmpl, "frontpage");
if(!g_frontpage) g_frontpage = "";
TCMAP *conf = g_tmpl->conf;
tcmapiterinit(conf);
while((rp = tcmapiternext2(conf)) != NULL){
const char *pv = tcmapiterval2(rp);
char *name = tcmpoolpushptr(g_mpool, tcsprintf("TP_%s", rp));
tcstrtoupper(name);
setenv(name, pv, 1);
}
if(g_scrextproc){
scrextsetmapvar(g_scrextproc, "_conf", conf);
scrextload(g_scrextproc, g_scrext);
}
dosession(mpool);
} else {
showerror(500, "The template file is missing.");
}
}
tcmpooldel(mpool);
return 0;
}
/* show the error page */
static void showerror(int code, const char *msg){
switch(code){
case 400:
printf("Status: %d Bad Request\r\n", code);
break;
case 404:
printf("Status: %d File Not Found\r\n", code);
break;
case 413:
printf("Status: %d Request Entity Too Large\r\n", code);
break;
case 500:
printf("Status: %d Internal Server Error\r\n", code);
break;
default:
printf("Status: %d Error\r\n", code);
break;
}
printf("Content-Type: text/plain; charset=UTF-8\r\n");
printf("\r\n");
printf("%s\n", msg);
}
/* show the not-modified page */
static void showcache(void){
printf("Status: 304 Not Modified\r\n");
printf("\r\n");
}
/* read the password file */
static void readpasswd(void){
if(!g_password) return;
TCLIST *lines = tcreadfilelines(g_password);
if(!lines) return;
int lnum = tclistnum(lines);
for(int i = 0; i < lnum; i++){
const char *line = tclistval2(lines, i);
const char *pv = strchr(line, ':');
if(!pv) continue;
tcmapputkeep(g_users, line, pv - line, pv + 1, strlen(pv + 1));
}
tclistdel(lines);
}
/* write the password file */
static bool writepasswd(void){
if(!g_password) return false;
bool err = false;
TCXSTR *xstr = tcxstrnew();
tcmapiterinit(g_users);
const char *name;
while((name = tcmapiternext2(g_users)) != NULL){
const char *value = tcmapiterval2(name);
tcxstrprintf(xstr, "%s:%s\n", name, value);
}
if(!tcwritefile(g_password, tcxstrptr(xstr), tcxstrsize(xstr))) err = true;
tcxstrdel(xstr);
return !err;
}
/* process each session */
static void dosession(TCMPOOL *mpool){
// download a file
const char *rp = getenv("HTTP_IF_MODIFIED_SINCE");
int64_t p_ifmod = rp ? tcstrmktime(rp) : 0;
rp = getenv("PATH_INFO");
if(rp && *rp == '/'){
rp++;
if(!g_upload){
showerror(404, "The upload directory is missing.");
return;
}
if(*rp == '\0' || strchr(rp, '/')){
showerror(404, "The request path is invalid.");
return;
}
const char *path = tcmpoolpushptr(mpool, tcsprintf("%s/%s", g_upload, rp));
int64_t mtime;
if(!tcstatfile(path, NULL, NULL, &mtime)){
showerror(404, "The requested file is missing.");
return;
}
if(mtime <= p_ifmod){
showcache();
return;
}
FILE *ifp = tcmpoolpush(mpool, fopen(path, "rb"), (void (*)(void *))fclose);
if(!ifp){
showerror(404, "The requested file is missing.");
return;
}
printf("Content-Type: %s\r\n", mimetype(rp));
printf("Cache-Control: no-cache\r\n");
char numbuf[NUMBUFSIZ];
tcdatestrhttp(mtime, 0, numbuf);
printf("Last-Modified: %s\r\n", numbuf);
printf("\r\n");
int c;
while((c = fgetc(ifp)) != EOF){
putchar(c);
}
return;
}
// prepare session-scope variables
TCMAP *vars = tcmpoolpushmap(mpool, tcmapnew2(TINYBNUM));
TCLIST *emsgs = tcmpoollistnew(mpool);
TCMAP *params = tcmpoolpushmap(mpool, tcmapnew2(TINYBNUM));
double now = tctime();
// read query parameters
rp = getenv("CONTENT_LENGTH");
bool post = false;
if(rp && *rp != '\0'){
int clen = tcatoi(rp);
if(clen > g_recvmax){
showerror(413, "The entity body was too long.");
return;
}
char *cbuf = tcmpoolmalloc(mpool, clen + 1);
if(fread(cbuf, 1, clen, stdin) != clen){
showerror(500, "Reading the entity body was failed.");
return;
}
tcwwwformdecode2(cbuf, clen, getenv("CONTENT_TYPE"), params);
post = true;
}
rp = getenv("QUERY_STRING");
if(rp) tcwwwformdecode(rp, params);
rp = getenv("HTTP_COOKIE");
if(rp) tcwwwformdecode(rp, params);
const char *p_user = tcstrskipspc(tcmapget4(params, "user", ""));
const char *p_pass = tcstrskipspc(tcmapget4(params, "pass", ""));
const char *p_format = tcstrskipspc(tcmapget4(params, "format", ""));
const char *p_act = tcstrskipspc(tcmapget4(params, "act", ""));
int64_t p_id = tcatoi(tcmapget4(params, "id", ""));
const char *p_name = tcstrskipspc(tcmapget4(params, "name", ""));
const char *p_order = tcstrskipspc(tcmapget4(params, "order", ""));
const char *p_adjust = tcstrskipspc(tcmapget4(params, "adjust", ""));
const char *p_expr = tcstrskipspc(tcmapget4(params, "expr", ""));
const char *p_cond = tcstrskipspc(tcmapget4(params, "cond", ""));
int p_page = tclmax(tcatoi(tcmapget4(params, "page", "")), 1);
const char *p_wiki = tcstrskipspc(tcmapget4(params, "wiki", ""));
bool p_mts = *tcmapget4(params, "mts", "") != '\0';
const char *p_hash = tcstrskipspc(tcmapget4(params, "hash", ""));
uint32_t p_seskey = tcatoi(tcmapget4(params, "seskey", ""));
const char *p_comowner = tcstrskipspc(tcmapget4(params, "comowner", ""));
const char *p_comtext = tcstrskipspc(tcmapget4(params, "comtext", ""));
const char *p_ummode = tcstrskipspc(tcmapget4(params, "ummode", ""));
const char *p_umname = tcstrskipspc(tcmapget4(params, "umname", ""));
const char *p_uminfo = tcstrskipspc(tcmapget4(params, "uminfo", ""));
const char *p_umpassone = tcstrskipspc(tcmapget4(params, "umpassone", ""));
const char *p_umpasstwo = tcstrskipspc(tcmapget4(params, "umpasstwo", ""));
const char *p_umridque = tcstrskipspc(tcmapget4(params, "umridque", ""));
const char *p_umridans = tcstrskipspc(tcmapget4(params, "umridans", ""));
const char *p_fmmode = tcstrskipspc(tcmapget4(params, "fmmode", ""));
const char *p_fmpath = tcstrskipspc(tcmapget4(params, "fmpath", ""));
const char *p_fmname = tcstrskipspc(tcmapget4(params, "fmname", ""));
int p_fmfilesiz;
const char *p_fmfilebuf = tcmapget(params, "fmfile", 6, &p_fmfilesiz);
const char *p_fmfilename = tcstrskipspc(tcmapget4(params, "fmfile_filename", ""));
bool p_fmthum = *tcmapget4(params, "fmthum", "") != '\0';
bool p_confirm = *tcmapget4(params, "confirm", "") != '\0';
rp = getenv("REMOTE_HOST");
const char *p_remotehost = rp ? rp : "";
rp = getenv("HTTP_HOST");
const char *p_hostname = rp ? rp : "";
if(*p_hostname == '\0'){
rp = getenv("SERVER_NAME");
p_hostname = rp ? rp : "";
}
rp = getenv("SSL_PROTOCOL_VERSION");
const char *p_scheme = (rp && *rp != '\0') ? "https" : "http";
const char *p_scripturl = tcmpoolpushptr(mpool, tcsprintf("%s://%s%s",
p_scheme, p_hostname, g_scriptname));
rp = getenv("HTTP_REFERER");
const char *p_referrer = rp ? rp : "";
rp = getenv("HTTP_USER_AGENT");
const char *p_useragent = rp ? rp : "";
const char *p_userlang = "";
rp = getenv("HTTP_ACCEPT_LANGUAGE");
if(rp){
char *lang = tcmpoolpushptr(mpool, tcstrdup(rp));
char *pv = strchr(lang, ',');
if(pv) *pv = '\0';
pv = strchr(lang, ';');
if(pv) *pv = '\0';
pv = strchr(lang, '-');
if(pv) *pv = '\0';
tcstrtrim(lang);
p_userlang = lang;
}
if(*p_format == '\0'){
rp = getenv("HTTP_ACCEPT");
if(rp && strstr(rp, "application/xhtml+xml")) p_format = "xhtml";
}
// perform authentication
bool auth = true;
const char *userinfo = NULL;
uint32_t seskey = 0;
const char *authcookie = NULL;
const char *ridque = "";
const char *ridans = "";
const char *ridcookie = NULL;
rp = getenv("AUTH_TYPE");
if(rp && (!tcstricmp(rp, "Basic") || !tcstricmp(rp, "Digest")) &&
(rp = getenv("REMOTE_USER")) != NULL){
p_user = rp;
userinfo = "";
tcmapput2(vars, "basicauth", "true");
} else if(g_users){
auth = false;
const char *salt = tcmapget4(g_users, SALTNAME, "");
int saltsiz = strlen(salt);
bool cont = false;
if(*p_user == '\0'){
int authsiz;
const char *authbuf = tcmapget(params, "auth", 4, &authsiz);
if(authbuf && authsiz > 0){
char *token = tcmpoolmalloc(mpool, authsiz + 1);
tcarccipher(authbuf, authsiz, salt, saltsiz, token);
token[authsiz] = '\0';
TCLIST *elems = tcmpoolpushlist(mpool, tcstrsplit(token, ":"));
if(tclistnum(elems) >= 4 && !strcmp(tclistval2(elems, 0), salt)){
seskey = tcatoi(tclistval2(elems, 3));
if(seskey > 0){
p_user = tclistval2(elems, 1);
p_pass = tclistval2(elems, 2);
cont = true;
}
}
}
}
if(*p_user != '\0'){
rp = tcmapget2(g_users, p_user);
if(rp){
char *hash = tcmpoolpushptr(mpool, tcstrdup(rp));
char *pv = strchr(hash, ':');
if(pv) *(pv++) = '\0';
char numbuf[NUMBUFSIZ];
passwordhash(p_pass, salt, numbuf);
if(!strcmp(hash, numbuf)){
auth = true;
userinfo = pv ? pv : "";
if(seskey < 1){
uint32_t seed = 19780211;
for(rp = p_pass; *rp != '\0'; rp++){
seed = seed * 31 + *(unsigned char *)rp;
}
double integ;
double fract = modf(now, &integ) * (1ULL << 31);
seskey = (((uint32_t)integ + (uint32_t)fract) ^ (seed << 8)) & INT32_MAX;
if(seskey < 1) seskey = INT32_MAX;
}
int tsiz = strlen(p_user) + strlen(p_pass) + saltsiz + NUMBUFSIZ * 2;
char token[tsiz];
tsiz = sprintf(token, "%s:%s:%s:%u:%lld",
salt, p_user, p_pass, (unsigned int)seskey, (long long)now);
tcmd5hash(token, tsiz, numbuf);
sprintf(token + tsiz, ":%s", numbuf);
tcarccipher(token, tsiz, salt, saltsiz, token);
if(!cont){
authcookie = tcmpoolpushptr(mpool, tcurlencode(token, tsiz));
p_seskey = seskey;
}
}
}
}
rp = tcmapget2(g_users, RIDDLENAME);
if(rp){
const char *pv = strstr(rp, ":");
if(pv){
ridque = tcmpoolpushptr(mpool, tcstrdup(pv + 1));
ridans = tcmpoolpushptr(mpool, tcmemdup(rp, pv - rp));
}
}
if(*ridans != '\0'){
const char *ridbuf = tcmapget2(params, "riddle");
if((ridbuf && !tcstricmp(ridbuf, ridans)) || !tcstricmp(p_umridans, ridans))
ridcookie = ridans;
}
}
if(!strcmp(p_act, "logout")){
p_user = "";
p_pass = "";
auth = false;
seskey = 0;
authcookie = "";
}
bool admin = auth && !strcmp(p_user, ADMINNAME);
bool cancom = false;
if(!strcmp(g_commentmode, "all") || (!strcmp(g_commentmode, "login") && auth) ||
(!strcmp(g_commentmode, "riddle") && (ridcookie || auth))) cancom = true;
// execute the beginning script
if(g_scrextproc){
const char *emsg = screxterrmsg(g_scrextproc);
if(emsg) tclistprintf(emsgs, "Loading scripting extension was failed (%s).", emsg);
scrextsetmapvar(g_scrextproc, "_params", params);
if(*p_user != '\0'){
TCMAP *user = tcmpoolpushmap(mpool, tcmapnew2(TINYBNUM));
tcmapput2(user, "name", p_user);
tcmapput2(user, "pass", p_pass);
if(userinfo) tcmapput2(user, "info", userinfo);
scrextsetmapvar(g_scrextproc, "_user", user);
}
}
if(g_scrextproc && scrextcheckfunc(g_scrextproc, "_begin")){
char *obuf = tcmpoolpushptr(mpool, scrextcallfunc(g_scrextproc, "_begin", ""));
if(obuf){
tcmapput2(vars, "beginmsg", obuf);
} else {
const char *emsg = screxterrmsg(g_scrextproc);
tclistprintf(emsgs, "Scripting extension was failed (%s).", emsg ? emsg : "(unknown)");
}
}
// open the database
TCTDB *tdb = tcmpoolpush(mpool, tctdbnew(), (void (*)(void *))tctdbdel);
int omode = TDBOREADER;
if(!strcmp(p_act, "update") && auth && post) omode = TDBOWRITER;
if(!strcmp(p_act, "comment") && cancom && post) omode = TDBOWRITER;
if(post && auth && *p_referrer != '\0'){
char *src = tcmpoolpushptr(mpool, tcstrdup(p_referrer));
char *wp = strchr(src, '?');
if(wp) *wp = '\0';
if(strcmp(src, p_scripturl)){
tclistprintf(emsgs, "Referrer is invalid (%s).", src);
admin = false;
post = false;
omode = TDBOREADER;
}
}
if(!tctdbopen(tdb, g_database, omode))
setdberrmsg(emsgs, tdb, "Opening the database was failed.");
int64_t mtime = tctdbmtime(tdb);
if(mtime < 1) mtime = now;
// prepare the common query
TCXSTR *comquery = tcmpoolxstrnew(mpool);
if(*p_act != '\0') tcxstrprintf(comquery, "&act=%?", p_act);
if(p_id > 0) tcxstrprintf(comquery, "&id=%lld", (long long)p_id);
if(*p_name != '\0') tcxstrprintf(comquery, "&name=%?", p_name);
if(*p_order != '\0') tcxstrprintf(comquery, "&order=%?", p_order);
if(*p_expr != '\0') tcxstrprintf(comquery, "&expr=%?", p_expr);
if(*p_cond != '\0') tcxstrprintf(comquery, "&cond=%?", p_cond);
if(p_fmthum) tcxstrprintf(comquery, "&fmthum=on");
// save a comment
if(!strcmp(p_act, "comment") && p_id > 0 && *p_comowner != '\0' && *p_comtext != '\0'){
char *owner = tcmpoolpushptr(mpool, tcstrdup(p_comowner));
tcstrsqzspc(owner);
char *text = tcmpoolpushptr(mpool, tcstrdup(p_comtext));
tcstrsqzspc(text);
if(*owner != '\0' && *text != '\0'){
if(checkusername(p_comowner)){
TCMAP *cols = tcmpoolpushmap(mpool, dbgetart(tdb, p_id));
if(cols){
if(checkfrozen(cols) && !admin){
tclistprintf(emsgs, "Frozen articles are not editable by normal users.");
} else {
TCMAP *ocols = *g_updatecmd != '\0' ? tcmpoolpushmap(mpool, tcmapdup(cols)) : NULL;
TCXSTR *wiki = tcmpoolxstrnew(mpool);
wikidump(wiki, cols);
TCXSTR *line = tcmpoolxstrnew(mpool);
tcxstrprintf(line, "%lld|%s|%s\n", (long long)now, owner, text);
tcmapputcat(cols, "comments", 8, tcxstrptr(line), tcxstrsize(line));
if(dbputart(tdb, p_id, cols)){
if(*g_updatecmd != '\0' &&
!doupdatecmd(mpool, "comment", p_scripturl, p_user, now, p_id, cols, ocols))
tclistprintf(emsgs, "The update command was failed.");
} else {
setdberrmsg(emsgs, tdb, "Storing the article was failed.");
}
}
}
} else {
tclistprintf(emsgs, "An invalid user name was specified.");
}
}
}
// perform each view
if(!strcmp(p_act, "login")){
// login view
tcmapprintf(vars, "titletip", "[login]");
tcmapput2(vars, "view", "login");
} else if(!strcmp(p_act, "logincheck") && !auth){
// login view
tcmapprintf(vars, "titletip", "[login]");
tcmapput2(vars, "view", "login");
} else if(!strcmp(p_act, "edit")){
// edit view
if(p_id > 0){
TCMAP *cols = tcmpoolpushmap(mpool, dbgetart(tdb, p_id));
if(cols){
if(checkfrozen(cols) && !admin){
tclistprintf(emsgs, "Frozen articles are not editable by normal users.");
} else {
TCXSTR *wiki = tcmpoolxstrnew(mpool);
wikidump(wiki, cols);
tcmapprintf(cols, "id", "%lld", (long long)p_id);
char numbuf[NUMBUFSIZ];
tcmd5hash(tcxstrptr(wiki), tcxstrsize(wiki), numbuf);
tcmapput2(cols, "hash", numbuf);
tcmapput2(vars, "view", "edit");
tcmapputmap(vars, "art", cols);
tcmapput(vars, "wiki", 4, tcxstrptr(wiki), tcxstrsize(wiki));
}
} else {
tcmapput2(vars, "view", "empty");
}
tcmapprintf(vars, "cond", "id:%lld", (long long)p_id);
} else {
tcmapprintf(vars, "titletip", "[edit]");
tcmapput2(vars, "view", "edit");
if(*p_name != '\0') tcmapput2(vars, "name", p_name);
if(*p_user != '\0') tcmapput2(vars, "user", p_user);
if(!strcmp(p_adjust, "front")) tcmapput2(vars, "tags", "*,?");
}
} else if(!strcmp(p_act, "preview")){
// preview view
if(p_id > 0){
TCMAP *cols = tcmpoolpushmap(mpool, dbgetart(tdb, p_id));
if(cols){
if(checkfrozen(cols) && !admin){
tclistprintf(emsgs, "Frozen articles are not editable by normal users.");
} else {
TCXSTR *wiki = tcmpoolxstrnew(mpool);
wikidump(wiki, cols);
char numbuf[NUMBUFSIZ];
tcmd5hash(tcxstrptr(wiki), tcxstrsize(wiki), numbuf);
if(!strcmp(numbuf, p_hash)){
if(*p_wiki != '\0'){
tcmapclear(cols);
wikiload(cols, p_wiki);
if(p_mts) tcmapprintf(cols, "mdate", "%lld", (long long)now);
const char *name = tcmapget2(cols, "name");
if(!name || *name == '\0'){
tclistprintf(emsgs, "The name can not be empty.");
} else if(checkfrozen(cols) && !admin){
tclistprintf(emsgs, "The frozen tag is not available by normal users.");
} else {
setarthtml(mpool, cols, p_id, 0, false);
tcmapprintf(vars, "titletip", "[preview]");
tcmapput2(vars, "view", "preview");
tcmapputmap(vars, "art", cols);
tcmapput2(vars, "wiki", p_wiki);
if(p_mts) tcmapput2(vars, "mts", "on");
tcmapprintf(vars, "id", "%lld", (long long)p_id);
tcmapput2(vars, "hash", p_hash);
}
} else {
tcmapput2(vars, "view", "removecheck");
tcmapputmap(vars, "art", cols);
tcmapprintf(vars, "id", "%lld", (long long)p_id);
tcmapput2(vars, "hash", p_hash);
}
} else {
tcmapput2(vars, "view", "collision");
tcmapput(vars, "wiki", 4, tcxstrptr(wiki), tcxstrsize(wiki));
tcmapput2(vars, "yourwiki", p_wiki);
}
}
} else {
tcmapput2(vars, "view", "empty");
}
tcmapprintf(vars, "cond", "id:%lld", (long long)p_id);
} else {
TCMAP *cols = tcmpoolpushmap(mpool, tcmapnew2(TINYBNUM));
wikiload(cols, p_wiki);
if(p_mts){
tcmapprintf(cols, "cdate", "%lld", (long long)now);
tcmapprintf(cols, "mdate", "%lld", (long long)now);
}
const char *name = tcmapget2(cols, "name");
if(!name || *name == '\0'){
tclistprintf(emsgs, "The name can not be empty.");
tcmapput2(vars, "view", "edit");
tcmapput2(vars, "wiki", p_wiki);
} else if(checkfrozen(cols) && !admin){
tclistprintf(emsgs, "The frozen tag is not available by normal users.");
tcmapput2(vars, "view", "edit");
tcmapput2(vars, "wiki", p_wiki);
} else {
setarthtml(mpool, cols, 0, 0, false);
tcmapprintf(vars, "titletip", "[preview]");
tcmapput2(vars, "view", "preview");
tcmapputmap(vars, "art", cols);
tcmapput2(vars, "wiki", p_wiki);
if(p_mts) tcmapput2(vars, "mts", "on");
}
}
} else if(!strcmp(p_act, "update")){
// update view
if(seskey > 0 && p_seskey != seskey){
tclistprintf(emsgs, "The session key is invalid (%u).", (unsigned int)p_seskey);
} else if(p_id > 0){
TCMAP *cols = tcmpoolpushmap(mpool, dbgetart(tdb, p_id));
if(cols){
if(checkfrozen(cols) && !admin){
tclistprintf(emsgs, "Frozen articles are not editable by normal users.");
} else {
TCXSTR *wiki = tcmpoolxstrnew(mpool);
wikidump(wiki, cols);
char numbuf[NUMBUFSIZ];
tcmd5hash(tcxstrptr(wiki), tcxstrsize(wiki), numbuf);
if(!strcmp(numbuf, p_hash)){
TCMAP *ocols = *g_updatecmd != '\0' ? tcmpoolpushmap(mpool, tcmapdup(cols)) : NULL;
if(*p_wiki != '\0'){
tcmapclear(cols);
wikiload(cols, p_wiki);
if(p_mts) tcmapprintf(cols, "mdate", "%lld", (long long)now);
const char *name = tcmapget2(cols, "name");
if(!name || *name == '\0'){
tclistprintf(emsgs, "The name can not be empty.");
} else if(checkfrozen(cols) && !admin){
tclistprintf(emsgs, "The frozen tag is not available by normal users.");
} else if(dbputart(tdb, p_id, cols)){
if(*g_updatecmd != '\0' &&
!doupdatecmd(mpool, "update", p_scripturl, p_user, now, p_id, cols, ocols))
tclistprintf(emsgs, "The update command was failed.");
tcmapput2(vars, "view", "store");
tcmapputmap(vars, "art", cols);
} else {
setdberrmsg(emsgs, tdb, "Storing the article was failed.");
}
} else {
if(dboutart(tdb, p_id)){
if(*g_updatecmd != '\0' &&
!doupdatecmd(mpool, "remove", p_scripturl, p_user, now, p_id, NULL, ocols))
tclistprintf(emsgs, "The update command was failed.");
tcmapprintf(cols, "id", "%lld", (long long)p_id);
tcmapput2(vars, "view", "remove");
tcmapputmap(vars, "art", cols);
} else {
setdberrmsg(emsgs, tdb, "Removing the article was failed.");
}
}
} else {
tcmapput2(vars, "view", "collision");
tcmapput(vars, "wiki", 4, tcxstrptr(wiki), tcxstrsize(wiki));
tcmapput2(vars, "yourwiki", p_wiki);
}
}
} else {
tcmapput2(vars, "view", "empty");
}
tcmapprintf(vars, "cond", "id:%lld", (long long)p_id);
} else {
TCMAP *cols = tcmpoolpushmap(mpool, tcmapnew2(TINYBNUM));
wikiload(cols, p_wiki);
if(p_mts){
tcmapprintf(cols, "cdate", "%lld", (long long)now);
tcmapprintf(cols, "mdate", "%lld", (long long)now);
}
const char *name = tcmapget2(cols, "name");
if(!name || *name == '\0'){
tclistprintf(emsgs, "The name can not be empty.");
tcmapput2(vars, "view", "edit");
tcmapput2(vars, "wiki", p_wiki);
} else if(dbputart(tdb, 0, cols)){
rp = tcmapget2(cols, "id");
int64_t nid = rp ? tcatoi(rp) : 0;
if(*g_updatecmd != '\0' &&
!doupdatecmd(mpool, "new", p_scripturl, p_user, now, nid, cols, NULL))
tclistprintf(emsgs, "The update command was failed.");
tcmapput2(vars, "view", "store");
tcmapputmap(vars, "art", cols);
} else {
setdberrmsg(emsgs, tdb, "Storing the article was failed.");
}
}
} else if(!strcmp(p_act, "users")){
// users view
if(g_users){
if(admin){
if(post && p_umname != '\0'){
if(seskey > 0 && p_seskey != seskey){
tclistprintf(emsgs, "The session key is invalid (%u).", (unsigned int)p_seskey);
} else if(!strcmp(p_ummode, "new")){
if(tcmapget2(g_users, p_umname)){
tclistprintf(emsgs, "The user already exists.");
} else if(!checkusername(p_umname)){
tclistprintf(emsgs, "The user name is invalid.");
} else if(strcmp(p_umpassone, p_umpasstwo)){
tclistprintf(emsgs, "The two passwords are different.");
} else {
const char *salt = tcmapget4(g_users, SALTNAME, "");
char numbuf[NUMBUFSIZ];
passwordhash(p_umpassone, salt, numbuf);
tcmapprintf(g_users, p_umname, "%s:%s", numbuf, p_uminfo);
if(writepasswd()){
tcmapput2(vars, "newuser", p_umname);
} else {
tclistprintf(emsgs, "Storing the password file was failed.");
}
}
} else if(!strcmp(p_ummode, "chpw")){
const char *pass = tcmapget2(g_users, p_umname);
if(!pass){
tclistprintf(emsgs, "The user does not exist.");
} else if(strcmp(p_umpassone, p_umpasstwo)){
tclistprintf(emsgs, "The two passwords are different.");
} else {
char *str = tcmpoolpushptr(mpool, tcstrdup(pass));
char *pv = strchr(str, ':');
if(pv){
*(pv++) = '\0';
} else {
pv = "";
}
const char *salt = tcmapget4(g_users, SALTNAME, "");
char numbuf[NUMBUFSIZ];
passwordhash(p_umpassone, salt, numbuf);
tcmapprintf(g_users, p_umname, "%s:%s", numbuf, pv);
if(writepasswd()){
tcmapput2(vars, "chpwuser", p_umname);
if(!strcmp(p_umname, p_user)){
p_user = "";
p_pass = "";
auth = false;
authcookie = "";
tcmapput2(vars, "tologin", p_umname);
}
} else {
tclistprintf(emsgs, "Storing the password file was failed.");
}
}
} else if(!strcmp(p_ummode, "del") && p_confirm){
if(!tcmapget2(g_users, p_umname)){
tclistprintf(emsgs, "The user does not exist.");
} else {
tcmapout2(g_users, p_umname);
if(writepasswd()){
tcmapput2(vars, "deluser", p_umname);
if(!strcmp(p_umname, p_user)){
p_user = "";
p_pass = "";
auth = false;
authcookie = "";
tcmapput2(vars, "tologin", p_umname);
}
} else {
tclistprintf(emsgs, "Storing the password file was failed.");
}
}
} else if(!strcmp(p_ummode, "rid")){
if(!checkusername(p_umridans)){
tclistprintf(emsgs, "The answer is invalid.");
} else {
tcmapprintf(g_users, RIDDLENAME, "%s:%s", p_umridans, p_umridque);
if(writepasswd()){
tcmapput2(vars, "chrid", p_umname);
ridque = p_umridque;
ridans = p_umridans;
} else {
tclistprintf(emsgs, "Storing the password file was failed.");
}
}
}
}
TCLIST *ulist = tcmpoollistnew(mpool);
tcmapiterinit(g_users);
const char *salt = NULL;
const char *name;
while((name = tcmapiternext2(g_users)) != NULL){
const char *pass = tcmapiterval2(name);
if(!strcmp(name, SALTNAME)){
salt = pass;
} else if(*name != SALTNAME[0]){
TCMAP *user = tcmpoolpushmap(mpool, tcmapnew2(TINYBNUM));
char *str = tcmpoolpushptr(mpool, tcstrdup(pass));
char *pv = strchr(str, ':');
if(pv){
*(pv++) = '\0';
} else {
pv = "";
}
tcmapput2(user, "name", name);
tcmapput2(user, "pass", str);
tcmapput2(user, "info", pv);
if(!strcmp(name, ADMINNAME)) tcmapput2(user, "admin", "true");
tclistpushmap(ulist, user);
}
}
tcmapprintf(vars, "titletip", "[user management]");
tcmapput2(vars, "view", "users");
if(tclistnum(ulist) > 0) tcmapputlist(vars, "userlist", ulist);
if(salt) tcmapput2(vars, "salt", salt);
tcmapput2(vars, "ridque", ridque);
tcmapput2(vars, "ridans", ridans);
} else {
tclistprintf(emsgs, "The user management function is not available by normal users.");
}
} else {
tclistprintf(emsgs, "The password file is missing.");
}
} else if(!strcmp(p_act, "files")){
// files view
bool isdir;
if(g_upload && tcstatfile(g_upload, &isdir, NULL, &mtime) && isdir){
if(auth){
if(post && (p_fmfilebuf || *p_fmpath != '\0')){
if(seskey > 0 && p_seskey != seskey){
tclistprintf(emsgs, "The session key is invalid (%u).", (unsigned int)p_seskey);
} else if(!strcmp(p_fmmode, "new")){
if(p_fmfilebuf && p_fmfilesiz > 0){
const char *name = p_fmname;
if(*name == '\0') name = p_fmfilename;
if(*name == '\0') name = "_noname_";
const char *ext = strrchr(name, '.');
if(!ext && (ext = strrchr(p_fmfilename, '.')) != NULL)
name = tcmpoolpushptr(mpool, tcsprintf("%s%s", name, ext));
if(putfile(mpool, p_fmpath, name, p_fmfilebuf, p_fmfilesiz)){
tcmapput2(vars, "newfile", name);
} else {
tclistprintf(emsgs, "Storing the file was failed.");
}
} else {
tclistprintf(emsgs, "There is no data.");
}
} else if(!strcmp(p_fmmode, "repl")){
if(p_fmfilebuf && p_fmfilesiz > 0){
if(putfile(mpool, p_fmpath, "", p_fmfilebuf, p_fmfilesiz)){
tcmapput2(vars, "replfile", p_fmname);
} else {
tclistprintf(emsgs, "Storing the file was failed.");
}
} else {
tclistprintf(emsgs, "There is no data.");
}
} else if(!strcmp(p_fmmode, "del") && p_confirm){
if(outfile(mpool, p_fmpath)){
tcmapput2(vars, "delfile", p_fmname);
} else {
tclistprintf(emsgs, "Removing the file was failed.");
}
}
} else {
if(mtime <= p_ifmod){
showcache();
return;
}
char numbuf[NUMBUFSIZ];
tcdatestrhttp(mtime, 0, numbuf);
tcmapput2(vars, "lastmod", numbuf);
}
int max = g_filenum;
int skip = max * (p_page - 1);
TCLIST *files = searchfiles(mpool, p_expr, p_order, max + 1, skip, p_fmthum);
bool over = false;
if(tclistnum(files) > max){
tcfree(tclistpop2(files));
over = true;
}
tcmapprintf(vars, "titletip", "[file management]");
tcmapput2(vars, "view", "files");
if(p_page > 1) tcmapprintf(vars, "prev", "%d", p_page - 1);
if(over) tcmapprintf(vars, "next", "%d", p_page + 1);
if(tclistnum(files) > 0) tcmapputlist(vars, "files", files);
} else {
tclistprintf(emsgs, "The file management function is not available by outer users.");
}
} else {
tclistprintf(emsgs, "The upload directory is missing.");
}
} else if(p_id > 0){
// single view
if(!auth && !ridcookie){
if(mtime <= p_ifmod){
showcache();
return;
}
char numbuf[NUMBUFSIZ];
tcdatestrhttp(mtime, 0, numbuf);
tcmapput2(vars, "lastmod", numbuf);
}
TCMAP *cols = tcmpoolpushmap(mpool, dbgetart(tdb, p_id));
if(cols){
setarthtml(mpool, cols, p_id, 0, false);
if(checkfrozen(cols) && !admin){
tcmapput2(cols, "frozen", "true");
} else if(cancom){
tcmapputkeep2(cols, "comnum", "0");