-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathapi_article.c
More file actions
1722 lines (1476 loc) · 50.1 KB
/
api_article.c
File metadata and controls
1722 lines (1476 loc) · 50.1 KB
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
#include <ctype.h>
#include <string.h>
#include <fcntl.h>
#include <errno.h>
#include <unistd.h>
#include <sys/file.h>
#include <sys/mman.h>
#include <libxml/HTMLtree.h>
#include <libxml/xpath.h>
#include <json-c/json.h>
#include <onion/block.h>
#include "bbs.h"
#include "ytht/numbyte.h"
#include "ytht/common.h"
#include "ytht/fileop.h"
#include "ytht/random.h"
#include "bmy/convcode.h"
#include "bmy/article.h"
#include "ythtbbs/cache.h"
#include "ythtbbs/commend.h"
#include "ythtbbs/docutil.h"
#include "ythtbbs/article.h"
#include "ythtbbs/notification.h"
#include "ythtbbs/session.h"
#include "api.h"
#include "apilib.h"
#include "apiconfig.h"
/**
* @brief 将 struct api_article 数组序列化为 json 字符串。
* 这个方法不考虑异常,因此方法里确定了 errcode 为 0,也就是 API_RT_SUCCESSFUL,
* 相关的异常应该在从 BMY 数据转为 api_article 的过程中判断、处理。
* @param ba_list struct api_article 数组
* @param count 数组长度
* @return json_object 指针
* @warning 记得调用完成 json_object_put
*/
static struct json_object *api_article_json_array(struct api_article *ba_list, int count);
static struct json_object *api_article_with_num_array_to_json(struct api_article *ba_list, int count, int mode);
/**
* @brief 将十大、分区热门话题转为 JSON 数据输出
* @param mode 0 为十大模式,1 为分区模式
* @param secstr 分区字符
* @return
*/
static int api_article_list_xmltopfile(ONION_FUNC_PROTO_STR, int mode, const char *secstr);
/**
* @brief 将美文推荐,或通知公告转为JSON数据输出
* @param board 版面名
* @param mode 0 为美文推荐,1为通知公告
* @param startnum 输出的第一篇文章序号,默认为(最新的文章-number)
* @param number 总共输出的文章数,暂时默认为20
* @return 返回json格式的查询结果
*/
static int api_article_list_commend(ONION_FUNC_PROTO_STR, int mode, int startnum, int number);
/**
* @brief 将版面文章列表转为JSON数据输出
* @param board 版面名
* @param mode 0为一般模式, 1为主题模式
* @param startnum 输出的第一篇文章序号,默认为(最新的文章-number)
* @param number 总共输出的文章数,由用户设定,暂时默认为20
* @return 返回json格式的查询结果
*/
static int api_article_list_board(ONION_FUNC_PROTO_STR);
/**
* @brief 将同主题文章列表转为JSON数据输出
* @param board 版面名
* @param thread 主题ID
* @param startnum 输出的第一篇文章序号,默认为(1)
* @param number 总共输出的文章数,由用户设定,默认为全部内容
* @return 返回json格式的查询结果
*/
static int api_article_list_thread(ONION_FUNC_PROTO_STR);
static int api_article_list_boardtop(ONION_FUNC_PROTO_STR);
static int api_article_list_section(ONION_FUNC_PROTO_STR);
/**
* @brief 实际处理发文的接口。
* 使用 api_article_post 和 api_article_reply 封装。
* @param mode 参见 API_POST_TYPE
* @return
*/
static int api_article_do_post(ONION_FUNC_PROTO_STR, int mode);
/**
* @brief 通过版面名,文章ID,查找对应主题ID
* @param board : board name
* @param filetime : file id
* @return thread id; return 0 means not find the thread id
*/
static int get_thread_by_filetime(char *board, int filetime);
/**
* @brief 通过同主题ID查找同主题文章的帖子数、总大小,以及参与评论的用户 ID
* 更新的字段包括:
* * th_num
* * th_size
* * latest
* * th_commenter_count
* * th_commenter
* @param ba struct api_article,API 中缓存帖子信息的结构体
* @param pmf 映射的 .DIR mmapfile
*/
static void parse_thread_info(struct api_article *ba, const struct mmapfile *pmf);
/**
* @brief 通过主题ID查找同主题文章数量
* @param thread : the thread id
* @return the nubmer of articles in the thread
*/
static int get_number_of_articles_in_thread(char *board, int thread);
/**
* @brief
* @param mode
* mode = 1 : 通过 boardname 和 主题ID 查找该主题第一篇文章 fileheader
* mode = 0 : 通过 boardname 和 filetime 查找该文章的 fileheader
* @param id:
* mode = 1 : thread = id;
* mode = 0 : filetime = id;
* @param fh_for_return : 查找到的fileheader,值全为0 表示未找到
* @return void
*/
static void get_fileheader_by_filetime_thread(int mode, char *board, int id, struct fileheader * fh_for_return);
/**
* @brief 获取文章内容。
* api_article_getHTMLContent() 和 api_article_getRAWContent() 放个方法
* 实际上是这个方法的封装,通过 mode 参数进行区别。
* article/getHTMLContent 和 article/getRAWContent 两个接口单独区分开,意
* 在强调在做修改文章操作时,<strong>应当</strong>调用 getRAWcontent。
* @param mode 参见 enum article_parse_mode
* @return
*/
static int api_article_get_content(ONION_FUNC_PROTO_STR, int mode);
/**
* @brief 从 .DIR 中依据 filetime 寻找文章对应的 fileheader 数据
* @param mf 映射到内存中的 .DIR 文件内容
* @param filetime 文件的时间戳
* @param num
* @param mode 1表示 .DIR 按时间排序,0表示不排序
* @return
*/
static struct fileheader * findbarticle(struct mmapfile *mf, time_t filetime, int *num, int mode);
int api_article_list(ONION_FUNC_PROTO_STR)
{
const char *type = onion_request_get_query(req, "type");
//type不能为空
if( type == NULL)
return api_error(p, req, res, API_RT_WRONGPARAM);
if(strcasecmp(type, "top10")==0) { // 十大
return api_article_list_xmltopfile(p, req, res, 0, NULL);
} else if(strcasecmp(type, "sectop")==0) { // 分区热门
const char *secstr = onion_request_get_query(req, "secstr");
if(secstr == NULL)
return api_error(p, req, res, API_RT_WRONGPARAM);
if(strlen(secstr)>2)
return api_error(p, req, res, API_RT_WRONGPARAM);
return api_article_list_xmltopfile(p, req, res, 1, secstr);
} else if(strcasecmp(type, "commend")==0) { // 美文推荐
const char *str_start = onion_request_get_query(req, "startnum");
const char *str_number = onion_request_get_query(req, "count");
int start = 0, number = 0;
if(NULL != str_start)
start = atoi(str_start);
if(NULL != str_number)
number = atoi(str_number);
return api_article_list_commend(p, req, res, 0, start, number);
} else if(strcasecmp(type, "announce")==0) { // 通知公告
const char *str_start = onion_request_get_query(req, "startnum");
const char *str_number = onion_request_get_query(req, "count");
int start = 0, number = 0;
if(NULL != str_start)
start = atoi(str_start);
if(NULL != str_number)
number = atoi(str_number);
return api_article_list_commend(p, req, res, 1, start, number);
} else if(strcasecmp(type, "board")==0) { // 版面文章
return api_article_list_board(p, req, res);
} else if(strcasecmp(type, "thread")==0) { // 同主题列表
return api_article_list_thread(p, req, res);
} else if(strcasecmp(type, "boardtop")==0) {
return api_article_list_boardtop(p, req, res);
} else if (strcasecmp(type, "section") == 0) {
return api_article_list_section(p, req, res);
} else
return api_error(p, req, res, API_RT_WRONGPARAM);
}
int api_article_getHTMLContent(ONION_FUNC_PROTO_STR)
{
return api_article_get_content(p, req, res, ARTICLE_PARSE_WITH_ANSICOLOR);
}
int api_article_getRAWContent(ONION_FUNC_PROTO_STR)
{
return api_article_get_content(p, req, res, ARTICLE_PARSE_WITHOUT_ANSICOLOR);
}
int api_article_getContent(ONION_FUNC_PROTO_STR) {
return api_article_get_content(p, req, res, ARTICLE_PARSE_JAVASCRIPT);
}
static int api_article_list_xmltopfile(ONION_FUNC_PROTO_STR, int mode, const char *secstr)
{
int listmax;
char ttfile[40];
if (mode == 0) { // 十大热门
listmax = 10;
sprintf(ttfile, "wwwtmp/ctopten");
} else { // 分区热门
listmax = 5;
sprintf(ttfile, "etc/Area_Dir/%c", (secstr != NULL) ? secstr[0] : '0');
}
struct api_article *top_list = calloc(listmax, sizeof(struct api_article));
if (top_list == NULL) {
return api_error(p, req, res, API_RT_NOTENGMEM);
}
struct fileheader fh;
htmlDocPtr doc = htmlParseFile(ttfile, "GBK");
if (doc == NULL) {
free(top_list);
return api_error(p, req, res, API_RT_NOTOP10FILE);
}
char xpath_links[40], xpath_nums[16];
if (mode == 0) { //十大
sprintf(xpath_links, "//div[@class='td-overflow']/a");
sprintf(xpath_nums, "//tr/td[4]");
} else { //分区
sprintf(xpath_links, "//div[@class='bd-overflow']/a");
sprintf(xpath_nums, "//tr/td[3]");
}
xmlXPathContextPtr ctx = xmlXPathNewContext(doc);
if (ctx == NULL) {
xmlFreeDoc(doc);
free(top_list);
return api_error(p, req, res, API_RT_XMLFMTERROR);
}
xmlXPathObjectPtr r_links = xmlXPathEvalExpression((const xmlChar*)xpath_links, ctx);
xmlXPathObjectPtr r_nums = xmlXPathEvalExpression((const xmlChar*)xpath_nums, ctx);
if(r_links->nodesetval == 0 || r_nums->nodesetval == 0) {
goto ERROR;
}
int total = r_links->nodesetval->nodeNr;
if (total == 0 || total>listmax ||
(mode == 0 && r_nums->nodesetval->nodeNr - total != 1) ||
(mode == 1 && r_nums->nodesetval->nodeNr != total)) {
goto ERROR;
}
int i;
xmlNodePtr cur_link, cur_num;
char *link, *num, *t1, *t2, buf[256], tmp[16];
for (i = 0; i < total; ++i) {
cur_link = r_links->nodesetval->nodeTab[i];
if (mode==0)
cur_num = r_nums->nodesetval->nodeTab[i+1];
else
cur_num = r_nums->nodesetval->nodeTab[i];
link = (char *)xmlGetProp(cur_link, (const xmlChar*)"href");
num = (char *)xmlNodeGetContent(cur_num);
top_list[i].type = (strstr(link, "tfind?board") != NULL);
if (mode == 0) {
top_list[i].th_num = atoi(num);
} else {
// 分区模式下num格式为 (7)
snprintf(tmp, strlen(num)-1, "%s", num+1);
top_list[i].th_num = atoi(tmp);
}
ytht_strsncpy(top_list[i].title, (const char*)xmlNodeGetContent(cur_link), 80);
ytht_strsncpy(buf, link, sizeof(buf));
if ((t1 = strtok(buf, "&")) == NULL)
goto ERROR;
if ((t2 = strchr(t1, '=')) == NULL)
goto ERROR;
t2++;
ytht_strsncpy(top_list[i].board, t2, sizeof(top_list[i].board));
if ((t1 = strtok(NULL, "&")) == NULL)
goto ERROR;
if ((t2 = strchr(t1, '=')) == NULL)
goto ERROR;
if (top_list[i].type) {
t2++;
ytht_strsncpy(tmp, t2, sizeof(tmp));
top_list[i].thread = atoi(tmp);
} else {
t2 = t2 + 3;
snprintf(tmp, 11, "%s", t2);
top_list[i].filetime = atoi(tmp);
}
//根据 board、thread 或 filetime 得到 fileheader 补全所有信息
if (top_list[i].type) {
get_fileheader_by_filetime_thread(1, top_list[i].board, top_list[i].thread, &fh);
if (fh.filetime != 0) {
top_list[i].filetime = fh.filetime;
strcpy(top_list[i].author, fh2owner(&fh));
}
} else {
get_fileheader_by_filetime_thread(0, top_list[i].board, top_list[i].filetime, &fh);
if (fh.thread != 0) {
top_list[i].thread = fh.thread;
strcpy(top_list[i].author, fh2owner(&fh));
}
}
}
struct json_object *result = api_article_json_array(top_list, total);
xmlXPathFreeObject(r_links);
xmlXPathFreeObject(r_nums);
xmlXPathFreeContext(ctx);
xmlFreeDoc(doc);
free(top_list);
if (result == NULL) {
return api_error(p, req, res, API_RT_NOTENGMEM);
}
api_set_json_header(res);
onion_response_write0(res, json_object_to_json_string(result));
json_object_put(result);
return OCS_PROCESSED;
ERROR:
xmlXPathFreeObject(r_links);
xmlXPathFreeObject(r_nums);
xmlXPathFreeContext(ctx);
xmlFreeDoc(doc);
free(top_list);
return api_error(p, req, res, API_RT_XMLFMTERROR);
}
static int api_article_list_commend(ONION_FUNC_PROTO_STR, int mode, int startnum, int number)
{
if(0 >= number)
number = 20;
struct api_article *commend_list, EMPTY_ARTICLE;
struct commend x;
char dir[80];
FILE *fp = NULL;
if(0 == mode)
strcpy(dir, ".COMMEND");
else if(1 == mode)
strcpy(dir, ".COMMEND2");
int fsize = ytht_file_size_s(dir);
int total = fsize / sizeof(struct commend);
commend_list = calloc(number, sizeof(struct api_article));
if (commend_list == NULL) {
return api_error(p, req, res, API_RT_NOTENGMEM);
}
fp = fopen(dir, "r");
if (!fp || fsize == 0) {
free(commend_list);
if (fp)
fclose(fp);
return api_error(p, req, res, API_RT_NOCMMNDFILE);
}
if (startnum == 0)
startnum = total - number + 1;
if (startnum <= 0)
startnum = 1;
if (fseek(fp, (startnum - 1) * sizeof(struct commend), SEEK_SET) == -1) {
free(commend_list);
if (fp)
fclose(fp);
return api_error(p, req, res, API_RT_FILEERROR);
}
int count = 0, length = 0;
for (int i = 0; i < number; i++) {
if (fread(&x, sizeof(struct commend), 1, fp) != 1)
break;
// 显示添加字符串终止符
x.board[sizeof(x.board) - 1] = 0;
x.userid[IDLEN + 1] = 0;
x.com_user[IDLEN + 1] = 0;
x.title[sizeof(x.title) - 1] = 0;
x.filename[sizeof(x.filename) - 1] = 0;
if(x.accessed & FH_ALLREPLY)
commend_list[i].mark = x.accessed;
commend_list[i].type = 0;
length = strlen(x.title);
g2u(x.title, length, commend_list[i].title, 80);
ytht_strsncpy(commend_list[i].author, x.userid, sizeof(EMPTY_ARTICLE.author));
ytht_strsncpy(commend_list[i].board, x.board, sizeof(EMPTY_ARTICLE.board));
commend_list[i].filetime = atoi((char *)x.filename + 2);
commend_list[i].thread = get_thread_by_filetime(commend_list[i].board, commend_list[i].filetime);
commend_list[i].th_num = get_number_of_articles_in_thread(commend_list[i].board, commend_list[i].thread);
commend_list[i].type = 0;
++count;
}
fclose(fp);
struct json_object *result = api_article_json_array(commend_list, count);
free(commend_list);
if (result == NULL) {
return api_error(p, req, res, API_RT_NOTENGMEM);
}
api_set_json_header(res);
onion_response_write0(res, json_object_to_json_string(result));
json_object_put(result);
return OCS_PROCESSED;
}
static int api_article_list_board(ONION_FUNC_PROTO_STR)
{
DEFINE_COMMON_SESSION_VARS;
int rc = api_check_session(req, cookie_buf, sizeof(cookie_buf), &cookie, &utmp_idx, &ptr_info);
const char * board = onion_request_get_query(req, "board");
const char * str_btype = onion_request_get_query(req, "btype");
const char * str_startnum = onion_request_get_query(req, "startnum");
const char * str_count = onion_request_get_query(req, "count");
const char * str_page = onion_request_get_query(req, "page");
char logbuf[512];
//判断必要参数
if(!(board && str_btype))
return api_error(p, req, res, API_RT_WRONGPARAM);
struct boardmem *b = ythtbbs_cache_Board_get_board_by_name(board);
if (b == NULL) {
return api_error(p, req, res, API_RT_NOSUCHBRD);
}
if (rc == API_RT_SUCCESSFUL) {
if (!check_user_read_perm_x(ptr_info, b)) {
return api_error(p, req, res, API_RT_NOBRDRPERM);
}
} else {
if (!check_guest_read_perm_x(b)) {
return api_error(p, req, res, API_RT_NOBRDRPERM);
}
}
int mode = 0, startnum = 0, count = 0;
if (str_startnum != NULL)
startnum = atoi(str_startnum);
if (str_count != NULL)
count = atoi(str_count);
if (0 >= count)
count = COUNT_PER_PAGE;
if (str_btype[0] == 't')
mode = 1;
else
mode = 0;
int fd = 0;
struct api_article *board_list = calloc(count, sizeof(struct api_article));
if (board_list == NULL) {
return api_error(p, req, res, API_RT_NOTENGMEM);
}
struct fileheader *data = NULL, x2;
char dir[80], filename[80], fname[16];
int i = 0, total = 0, total_article = 0;
snprintf(dir, sizeof(dir), "boards/%s/.DIR", board);
struct mmapfile mf = { .ptr = NULL };
if (mmapfile(dir, &mf) == -1 || mf.size == 0) {
free(board_list);
return api_error(p, req, res, API_RT_EMPTYBRD);
}
data = (struct fileheader *) mf.ptr;
total = mf.size / sizeof(struct fileheader);
if (0 == mode) {
// 一般模式
total_article = total;
} else if (1 == mode) {
// 主题模式
total_article = 0;
for (i = 0; i < total; ++i)
if (data[i].thread == data[i].filetime)
++total_article;
}
// 如果使用分页参数,则首先依据分页计算
if (str_page != NULL)
startnum = total_article - count * (atoi(str_page)) + 1;
if (startnum == 0)
startnum = total_article - count + 1;
if (startnum <= 0)
startnum = 1;
int sum = 0, num = 0;
unsigned char c;
struct api_article EMPTY_ARTICLE;
for (i = 0; i < total; ++i) {
// TODO: 高亮标题处理
if (0 == mode)
++sum;
else if (1 == mode && data[i].thread == data[i].filetime)
++sum;
if (sum < startnum || (1 == mode && data[i].thread != data[i].filetime)) {
continue;
}
if (data[i].sizebyte == 0) { // 如果内存中数据库记录的 sizebyte 为 0,则修正 .DIR 文件
snprintf(filename, sizeof(filename), "boards/%s/%s", board, fh2fname_s(&data[i], fname, sizeof(fname)));
c = ytht_num2byte(eff_size(filename));
fd = open(dir, O_RDWR);
if (fd < 0)
break;
flock(fd, LOCK_EX);
lseek(fd, (startnum - 1 + i) * sizeof (struct fileheader),SEEK_SET);
if (read(fd, &x2, sizeof (x2)) == sizeof (x2) && data[i].filetime == x2.filetime) {
x2.sizebyte = c;
if (lseek(fd, -1 * sizeof (x2), SEEK_CUR) != (off_t) -1) {
if (write(fd, &x2, sizeof (x2)) == -1) {
snprintf(logbuf, sizeof(logbuf), "write error to fileheader %s, at No. %d record, from file %s. Errno %d: %s.", dir, (startnum-1+i), filename, errno, strerror(errno));
newtrace(logbuf);
}
}
}
flock(fd, LOCK_UN);
close(fd);
}
board_list[num].mark = data[i].accessed;
board_list[num].filetime = data[i].filetime;
board_list[num].thread = data[i].thread;
board_list[num].latest = (data[i].edittime ? data[i].edittime : data[i].filetime);
board_list[num].type = mode;
board_list[num].sequence_num = i;
ytht_strsncpy(board_list[num].board, board, sizeof(EMPTY_ARTICLE.board));
ytht_strsncpy(board_list[num].author, data[i].owner, sizeof(EMPTY_ARTICLE.author));
g2u(data[i].title, strlen(data[i].title), board_list[num].title, 80);
++num;
if (num >= count) {
break;
}
}
bool brc_inited = false;
char brc_file[80];
int ulock;
struct onebrc onebrc;
if (ptr_info) {
if ((ulock = userlock(ptr_info->userid, LOCK_SH)) >= 0) {
brc_inited = true;
sethomefile_s(brc_file, sizeof(brc_file), ptr_info->userid, "brc");
brc_init(&ptr_info->allbrc, ptr_info->userid, brc_file);
memset(&onebrc, 0, sizeof(struct onebrc));
brc_getboard(&ptr_info->allbrc, &onebrc, b->header.filename);
userunlock(ptr_info->userid, ulock);
}
}
for (i = 0; i < num; ++i) {
if (mode == 1) {
parse_thread_info(&board_list[i], &mf);
}
board_list[i].unread = (ptr_info == NULL || !brc_inited) ? 1 : brc_unreadt(&onebrc, board_list[i].latest);
}
mmapfile(NULL, &mf);
struct json_object *result = api_article_with_num_array_to_json(board_list, num, mode);
free(board_list);
if (result == NULL) {
return api_error(p, req, res, API_RT_NOTENGMEM);
}
struct json_object *jp;
if ((jp = json_object_new_int(total_article)) != NULL) {
json_object_object_add(result, "total", jp);
}
api_set_json_header(res);
onion_response_write0(res, json_object_to_json_string(result));
return OCS_PROCESSED;
}
static int api_article_list_thread(ONION_FUNC_PROTO_STR)
{
DEFINE_COMMON_SESSION_VARS;
int rc = api_check_session(req, cookie_buf, sizeof(cookie_buf), &cookie, &utmp_idx, &ptr_info);
const char *board = onion_request_get_query(req, "board");
const char *str_thread = onion_request_get_query(req, "thread");
const char *str_startnum = onion_request_get_query(req, "startnum");
const char *str_count = onion_request_get_query(req, "count");
//判断必要参数
if (!(board && str_thread))
return api_error(p, req, res, API_RT_WRONGPARAM);
int thread = atoi(str_thread);
if (thread == 0)
return api_error(p, req, res, API_RT_WRONGPARAM);
//判断版面访问权
struct boardmem *b = ythtbbs_cache_Board_get_board_by_name(board);
if (b == NULL)
return api_error(p, req, res, API_RT_NOSUCHBRD);
if (rc == API_RT_SUCCESSFUL) {
if (!check_user_read_perm_x(ptr_info, b))
return api_error(p, req, res, API_RT_FBDNUSER);
} else {
if (!check_guest_read_perm_x(b))
return api_error(p, req, res, API_RT_NOSUCHBRD);
}
int startnum = 0,
count = 0,
fd = 0,
ulock,
i = 0,
total = 0,
total_article = 0,
sum = 0,
num = 0;
char dir[80],
filename[80],
brc_file[80],
fname[16],
logbuf[512];
bool brc_inited = false;
unsigned char c;
struct fileheader *data = NULL, x2;
struct onebrc onebrc;
struct api_article *board_list;
struct api_article EMPTY_ARTICLE;
enum api_error_code errcode = API_RT_SUCCESSFUL;
if (str_startnum != NULL)
startnum = atoi(str_startnum);
if (str_count != NULL)
count = atoi(str_count);
snprintf(dir, sizeof(dir), "boards/%s/.DIR", board);
struct mmapfile mf = { .ptr = NULL };
if (mmapfile(dir, &mf) >= 0) {
data = (struct fileheader *) mf.ptr;
total = mf.size / sizeof(struct fileheader);
total_article = 0;
for (i = 0; i < total; ++i) {
if (data[i].thread == thread)
++total_article;
}
if (count == 0)
count = total_article;
if ((board_list = calloc(count, sizeof(struct api_article))) != NULL) {
if (startnum == 0)
startnum = total_article - count + 1;
if (startnum <= 0)
startnum = 1;
if (ptr_info) {
if ((ulock = userlock(ptr_info->userid, LOCK_EX)) >= 0) {
brc_inited = true;
sethomefile_s(brc_file, sizeof(brc_file), ptr_info->userid, "brc");
brc_init(&ptr_info->allbrc, ptr_info->userid, brc_file);
brc_getboard(&ptr_info->allbrc, &onebrc, board);
userunlock(ptr_info->userid, ulock);
}
}
for (i = 0; i < total; ++i) {
if(data[i].thread != thread)
continue;
++sum;
if(sum < startnum)
continue;
if (data[i].sizebyte == 0) {
snprintf(filename, sizeof(filename), "boards/%s/%s", board, fh2fname_s(&data[i], fname, sizeof(fname)));
c = ytht_num2byte(eff_size(filename));
fd = open(dir, O_RDWR);
if (fd < 0)
break;
flock(fd, LOCK_EX);
lseek(fd, (startnum - 1 + i) * sizeof (struct fileheader), SEEK_SET);
if (read(fd, &x2, sizeof (x2)) == sizeof (x2) && data[i].filetime == x2.filetime) {
x2.sizebyte = c;
lseek(fd, -1 * sizeof (x2), SEEK_CUR);
if (write(fd, &x2, sizeof (x2)) == -1) {
snprintf(logbuf, sizeof(logbuf), "write error to fileheader %s, at No. %d record, from file %s. Errno %d: %s.", dir, (startnum-1+i), filename, errno, strerror(errno));
newtrace(logbuf);
}
}
flock(fd, LOCK_UN);
close(fd);
}
board_list[num].mark = data[i].accessed;
board_list[num].filetime = data[i].filetime;
board_list[num].thread = data[i].thread;
board_list[num].type = 0;
board_list[num].latest = data[i].edittime ? data[i].edittime : data[i].filetime;
board_list[num].unread = (ptr_info == NULL || !brc_inited) ? true : brc_unreadt(&onebrc, board_list[num].latest);
ytht_strsncpy(board_list[num].board, board, sizeof(EMPTY_ARTICLE.board));
ytht_strsncpy(board_list[num].author, data[i].owner, sizeof(EMPTY_ARTICLE.author));
g2u(data[i].title, strlen(data[i].title), board_list[num].title, 80);
++num;
if (num >= count)
break;
}
struct json_object *result = api_article_json_array(board_list, num);
free(board_list);
if (result) {
api_set_json_header(res);
onion_response_write0(res, json_object_to_json_string(result));
json_object_put(result);
} else {
errcode = API_RT_NOTENGMEM;
}
} else {
errcode = API_RT_NOTENGMEM;
}
mmapfile(NULL, &mf);
} else {
errcode = API_RT_EMPTYBRD;
}
return (errcode == API_RT_SUCCESSFUL) ? OCS_PROCESSED : api_error(p, req, res, errcode);
}
static int api_article_list_boardtop(ONION_FUNC_PROTO_STR)
{
DEFINE_COMMON_SESSION_VARS;
int rc;
struct userec ue;
size_t size;
const char *board = onion_request_get_query(req, "board");
//判断必要参数
if (!board)
return api_error(p, req, res, API_RT_WRONGPARAM);
rc = api_check_session(req, cookie_buf, sizeof(cookie_buf), &cookie, &utmp_idx, &ptr_info);
if (rc != API_RT_SUCCESSFUL)
return api_error(p, req, res, rc);
//判断版面访问权
if (getuser_s(&ue, ptr_info->userid) < 0)
return api_error(p, req, res, API_RT_NOSUCHUSER);
struct boardmem *b = ythtbbs_cache_Board_get_board_by_name(board);
if (b == NULL)
return api_error(p, req, res, API_RT_NOSUCHBRD);
ptr_info->userlevel = ue.userlevel;
if (!check_user_read_perm_x(ptr_info, b))
return api_error(p, req, res, API_RT_NOBRDRPERM);
char topdir[80];
FILE *fp;
struct fileheader x;
snprintf(topdir, sizeof(topdir), "boards/%s/.TOPFILE", b->header.filename);
fp = fopen(topdir, "r");
if (fp == 0)
return api_error(p, req, res, API_RT_NOBRDTPFILE);
int count = ytht_file_size_s(topdir) / sizeof(struct fileheader);
struct api_article *board_list = calloc(count, sizeof(struct api_article));
if (board_list == NULL) {
fclose(fp);
return api_error(p, req, res, API_RT_NOTENGMEM);
}
int i;
for (i = 0; i < count; ++i) {
size = fread(&x, sizeof(x), 1, fp);
if (size != 1)
break;
board_list[i].filetime = x.filetime;
board_list[i].mark = x.accessed;
board_list[i].sequence_num = 0;
board_list[i].thread = x.thread;
board_list[i].th_num = get_number_of_articles_in_thread(b->header.filename, x.thread);
strcpy(board_list[i].board, b->header.filename);
strcpy(board_list[i].author, fh2owner(&x));
x.title[sizeof(x.title) - 1] = 0;
g2u(x.title, strlen(x.title), board_list[i].title, 80);
}
fclose(fp);
struct json_object *result = api_article_json_array(board_list, count);
free(board_list);
if (result == NULL) {
return api_error(p, req, res, API_RT_NOTENGMEM);
}
api_set_json_header(res);
onion_response_write0(res, json_object_to_json_string(result));
json_object_put(result);
return OCS_PROCESSED;
}
static int api_article_get_content(ONION_FUNC_PROTO_STR, int mode)
{
DEFINE_COMMON_SESSION_VARS;
int rc = api_check_session(req, cookie_buf, sizeof(cookie_buf), &cookie, &utmp_idx, &ptr_info);
const char * bname = onion_request_get_query(req, "board");
const char * aid_str = onion_request_get_query(req, "aid");
if (!bname || !aid_str) {
return api_error(p, req, res, API_RT_WRONGPARAM);
}
struct boardmem *bmem = ythtbbs_cache_Board_get_board_by_name(bname);
if (!bmem)
return api_error(p, req, res, API_RT_NOSUCHBRD);
int aid = atoi(aid_str);
if (rc == API_RT_SUCCESSFUL) {
if (!check_user_read_perm_x(ptr_info, bmem)) {
return api_error(p, req, res, API_RT_NOBRDRPERM);
}
} else {
if (!check_guest_read_perm_x(bmem))
return api_error(p, req, res, API_RT_NOSUCHBRD);
}
// 删除回复提醒
if (rc == API_RT_SUCCESSFUL && is_post_in_notification(ptr_info->userid, bname, aid))
del_post_notification(ptr_info->userid, bname, aid);
int total = bmem->total;
if (total <= 0) {
return api_error(p, req, res, API_RT_EMPTYBRD);
}
char dir_file[80], filename[80];
struct fileheader *fh = NULL;
snprintf(filename, sizeof(filename), "M.%d.A", aid);
snprintf(dir_file, sizeof(dir_file), "boards/%s/.DIR", bname);
struct mmapfile mf = { .ptr = NULL };
if (mmapfile(dir_file, &mf) == -1) {
return api_error(p, req, res, API_RT_EMPTYBRD);
}
const char * num_str = onion_request_get_query(req, "num");
int num = (num_str == NULL) ? -1 : (atoi(num_str)-1);
fh = findbarticle(&mf, aid, &num, 1);
if (fh == NULL) {
mmapfile(NULL, &mf);
return api_error(p, req, res, API_RT_NOSUCHATCL);
}
if (fh->owner[0] == '-') {
mmapfile(NULL, &mf);
return api_error(p, req, res, API_RT_ATCLDELETED);
}
int ulock;
char brc_file[80];
time_t fh_time;
struct onebrc onebrc;
if (rc == API_RT_SUCCESSFUL) {
// 处理已读
if ((ulock = userlock(ptr_info->userid, LOCK_EX)) >= 0) {
sethomefile_s(brc_file, sizeof(brc_file), ptr_info->userid, "brc");
brc_init(&ptr_info->allbrc, ptr_info->userid, brc_file);
brc_getboard(&ptr_info->allbrc, &onebrc, bmem->header.filename);
fh_time = fh->edittime ? fh->edittime : fh->filetime;
if (brc_unreadt(&onebrc, fh_time)) {
brc_addlistt(&onebrc, fh_time);
brc_putboard(&ptr_info->allbrc, &onebrc);
brc_fini(&ptr_info->allbrc, ptr_info->userid);
}
userunlock(ptr_info->userid, ulock);
}
}
char title_utf8[180];
memset(title_utf8, 0, 180);
g2u(fh->title, strlen(fh->title), title_utf8, 180);
struct attach_link *attach_link_list=NULL;
char *article_content_utf8;
if (mode == ARTICLE_PARSE_JAVASCRIPT)
article_content_utf8 = parse_article_js(bmem->header.filename, filename, &attach_link_list);
else
article_content_utf8 = parse_article(bmem->header.filename, filename, mode, &attach_link_list);
char article_json_str[256];
int curr_permission = (rc == API_RT_SUCCESSFUL) ? !strncmp(ptr_info->userid, fh->owner, IDLEN+1) : 0;
snprintf(article_json_str, sizeof(article_json_str), "{\"errcode\":0, \"attach\":[], "
"\"can_edit\":%d, \"can_delete\":%d, \"can_reply\":%d, "
"\"board\":\"%s\", \"author\":\"%s\", \"thread\":%ld, \"num\":%d}",
curr_permission, curr_permission,
!(fh->accessed & FH_NOREPLY), bmem->header.filename,
fh2owner(fh), fh->thread, num);
struct json_object * jp = json_tokener_parse(article_json_str);
json_object_object_add(jp, "content", json_object_new_string(article_content_utf8));
json_object_object_add(jp, "title", json_object_new_string(title_utf8));
if (attach_link_list) {
struct attach_link *alp = attach_link_list;
struct json_object *attach_array = json_object_object_get(jp, "attach");
struct json_object *attach = NULL;
struct json_object *signature = NULL;
while (alp) {
if ((attach = json_object_new_object()) != NULL) {
json_object_object_add(attach, "link", json_object_new_string(alp->link));
json_object_object_add(attach, "size", json_object_new_int(alp->size));
if (alp->name) {
json_object_object_add(attach, "name", json_object_new_string(alp->name));
if ((signature = json_object_new_array_ext(BMY_SIGNATURE_LEN)) != NULL) {
for (size_t idx = 0; idx < BMY_SIGNATURE_LEN; idx++) {
json_object_array_put_idx(signature, idx, json_object_new_int(alp->signature[idx]));
}
json_object_object_add(attach, "signature", signature);
}
}
json_object_array_add(attach_array, attach);
}
alp = alp->next;
}
}
char * api_output = strdup(json_object_to_json_string(jp));
mmapfile(NULL, &mf);
free(article_content_utf8);
json_object_put(jp);
free_attach_link_list(attach_link_list);
api_set_json_header(res);
onion_response_write0(res, api_output);
free(api_output);
return OCS_PROCESSED;
}
int api_article_post(ONION_FUNC_PROTO_STR)
{
return api_article_do_post(p, req, res, API_POST_TYPE_POST);
}
int api_article_reply(ONION_FUNC_PROTO_STR)
{
return api_article_do_post(p, req, res, API_POST_TYPE_REPLY);
}
static int api_article_do_post(ONION_FUNC_PROTO_STR, int mode)
{
DEFINE_COMMON_SESSION_VARS;
int rc, rid = 0, mark = 0, ulock;
size_t title_len, data_len, i;
time_t ref, thread = -1, r;
bool replyMode = false,