/[LeafOK_CVS]/lbbs/src/article_post.c
ViewVC logotype

Diff of /lbbs/src/article_post.c

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

Revision 1.11 by sysadm, Sat Jun 14 11:59:09 2025 UTC Revision 1.17 by sysadm, Sun Jun 15 04:46:19 2025 UTC
# Line 63  int article_post(const SECTION_LIST *p_s Line 63  int article_post(const SECTION_LIST *p_s
63                  log_error("NULL pointer error\n");                  log_error("NULL pointer error\n");
64          }          }
65    
66            if (!checkpriv(&BBS_priv, p_section->sid, S_POST))
67            {
68                    clearscr();
69                    moveto(1, 1);
70                    prints("您没有权限在本版块发表文章\n");
71                    press_any_key();
72    
73                    return 0;
74            }
75    
76          p_article_new->title[0] = '\0';          p_article_new->title[0] = '\0';
77          title_input[0] = '\0';          title_input[0] = '\0';
78          p_article_new->transship = 0;          p_article_new->transship = 0;
# Line 71  int article_post(const SECTION_LIST *p_s Line 81  int article_post(const SECTION_LIST *p_s
81          if (p_editor_data == NULL)          if (p_editor_data == NULL)
82          {          {
83                  log_error("editor_data_load() error\n");                  log_error("editor_data_load() error\n");
84                  return -2;                  ret = -1;
85                    goto cleanup;
86          }          }
87    
88          // Set title and sign          // Set title and sign
# Line 202  int article_post(const SECTION_LIST *p_s Line 213  int article_post(const SECTION_LIST *p_s
213          if (len_content < 0)          if (len_content < 0)
214          {          {
215                  log_error("editor_data_save() error\n");                  log_error("editor_data_save() error\n");
216                  ret = -2;                  ret = -1;
217                  goto cleanup;                  goto cleanup;
218          }          }
219    
# Line 243  int article_post(const SECTION_LIST *p_s Line 254  int article_post(const SECTION_LIST *p_s
254                  rs = NULL;                  rs = NULL;
255          }          }
256    
         content_f = malloc((size_t)len_content * 2 + 1);  
         if (content_f == NULL)  
         {  
                 log_error("malloc(content_f) error: OOM\n");  
                 ret = -1;  
                 goto cleanup;  
         }  
   
         sql_content = malloc(SQL_BUFFER_LEN + (size_t)len_content * 2 + 1);  
         if (sql_content == NULL)  
         {  
                 log_error("malloc(sql_content) error: OOM\n");  
                 ret = -1;  
                 goto cleanup;  
         }  
   
257          // Begin transaction          // Begin transaction
258          if (mysql_query(db, "SET autocommit=0") != 0)          if (mysql_query(db, "SET autocommit=0") != 0)
259          {          {
# Line 275  int article_post(const SECTION_LIST *p_s Line 270  int article_post(const SECTION_LIST *p_s
270          }          }
271    
272          // Secure SQL parameters          // Secure SQL parameters
273            content_f = malloc((size_t)len_content * 2 + 1);
274            if (content_f == NULL)
275            {
276                    log_error("malloc(content_f) error: OOM\n");
277                    ret = -1;
278                    goto cleanup;
279            }
280    
281          mysql_real_escape_string(db, nickname_f, BBS_nickname, (unsigned long)strnlen(BBS_nickname, sizeof(BBS_nickname)));          mysql_real_escape_string(db, nickname_f, BBS_nickname, (unsigned long)strnlen(BBS_nickname, sizeof(BBS_nickname)));
282          mysql_real_escape_string(db, title_f, p_article_new->title, strnlen(p_article_new->title, sizeof(p_article_new->title)));          mysql_real_escape_string(db, title_f, p_article_new->title, strnlen(p_article_new->title, sizeof(p_article_new->title)));
283          mysql_real_escape_string(db, content_f, content, (unsigned long)len_content);          mysql_real_escape_string(db, content_f, content, (unsigned long)len_content);
284    
285            free(content);
286            content = NULL;
287    
288          // Add content          // Add content
289          snprintf(sql, SQL_BUFFER_LEN + (size_t)len_content * 2 + 1,          sql_content = malloc(SQL_BUFFER_LEN + (size_t)len_content * 2 + 1);
290            if (sql_content == NULL)
291            {
292                    log_error("malloc(sql_content) error: OOM\n");
293                    ret = -1;
294                    goto cleanup;
295            }
296    
297            snprintf(sql_content, SQL_BUFFER_LEN + (size_t)len_content * 2 + 1,
298                           "INSERT INTO bbs_content(AID, content) values(0, '%s')",                           "INSERT INTO bbs_content(AID, content) values(0, '%s')",
299                           content_f);                           content_f);
300    
301          if (mysql_query(db, sql) != 0)          free(content_f);
302            content_f = NULL;
303    
304            if (mysql_query(db, sql_content) != 0)
305          {          {
306                  log_error("Add article content error: %s\n", mysql_error(db));                  log_error("Add article content error: %s\n", mysql_error(db));
307                  ret = -1;                  ret = -1;
# Line 293  int article_post(const SECTION_LIST *p_s Line 310  int article_post(const SECTION_LIST *p_s
310    
311          p_article_new->cid = (int32_t)mysql_insert_id(db);          p_article_new->cid = (int32_t)mysql_insert_id(db);
312    
313            free(sql_content);
314            sql_content = NULL;
315    
316          // Add article          // Add article
317          snprintf(sql, sizeof(sql),          snprintf(sql, sizeof(sql),
318                           "INSERT INTO bbs(SID, TID, UID, username, nickname, title, CID, transship, "                           "INSERT INTO bbs(SID, TID, UID, username, nickname, title, CID, transship, "
# Line 358  int article_post(const SECTION_LIST *p_s Line 378  int article_post(const SECTION_LIST *p_s
378                  goto cleanup;                  goto cleanup;
379          }          }
380    
381            mysql_close(db);
382            db = NULL;
383    
384          clearscr();          clearscr();
385          moveto(1, 1);          moveto(1, 1);
386          prints("发送完成,新文章通常会在%d秒后可见", BBS_section_list_load_interval);          prints("发送完成,新文章通常会在%d秒后可见", BBS_section_list_load_interval);
# Line 414  int article_modify(const SECTION_LIST *p Line 437  int article_modify(const SECTION_LIST *p
437          if (db == NULL)          if (db == NULL)
438          {          {
439                  log_error("db_open() error: %s\n", mysql_error(db));                  log_error("db_open() error: %s\n", mysql_error(db));
440                  return -1;                  ret = -1;
441                    goto cleanup;
442          }          }
443    
444          snprintf(sql, sizeof(sql),          snprintf(sql, sizeof(sql),
# Line 426  int article_modify(const SECTION_LIST *p Line 450  int article_modify(const SECTION_LIST *p
450          if (mysql_query(db, sql) != 0)          if (mysql_query(db, sql) != 0)
451          {          {
452                  log_error("Query article content error: %s\n", mysql_error(db));                  log_error("Query article content error: %s\n", mysql_error(db));
453                  ret = -2;                  ret = -1;
454                  goto cleanup;                  goto cleanup;
455          }          }
456          if ((rs = mysql_use_result(db)) == NULL)          if ((rs = mysql_use_result(db)) == NULL)
457          {          {
458                  log_error("Get article content data failed\n");                  log_error("Get article content data failed\n");
459                  ret = -2;                  ret = -1;
460                  goto cleanup;                  goto cleanup;
461          }          }
462    
463          if ((row = mysql_fetch_row(rs)))          if ((row = mysql_fetch_row(rs)))
464          {          {
465                  p_editor_data = editor_data_load(row[1]);                  content = malloc(ARTICLE_CONTENT_MAX_LEN);
466                    if (content == NULL)
467                    {
468                            log_error("malloc(content) error: OOM\n");
469                            ret = -1;
470                            goto cleanup;
471                    }
472    
473                    strncpy(content, row[1], ARTICLE_CONTENT_MAX_LEN - 1);
474                    content[ARTICLE_CONTENT_MAX_LEN - 1] = '\0';
475    
476                    // Remove control sequence
477                    len_content = ctrl_seq_filter(content);
478    
479                    p_editor_data = editor_data_load(content);
480                  if (p_editor_data == NULL)                  if (p_editor_data == NULL)
481                  {                  {
482                          log_error("editor_data_load(aid=%d, cid=%d) error\n", p_article->aid, atoi(row[0]));                          log_error("editor_data_load(aid=%d, cid=%d) error\n", p_article->aid, atoi(row[0]));
483                          ret = -3;                          ret = -1;
484                          goto cleanup;                          goto cleanup;
485                  }                  }
486    
487                    free(content);
488                    content = NULL;
489          }          }
490          mysql_free_result(rs);          mysql_free_result(rs);
491          rs = NULL;          rs = NULL;
# Line 498  int article_modify(const SECTION_LIST *p Line 539  int article_modify(const SECTION_LIST *p
539          if (len_content < 0)          if (len_content < 0)
540          {          {
541                  log_error("editor_data_save() error\n");                  log_error("editor_data_save() error\n");
542                  ret = -2;                  ret = -1;
543                  goto cleanup;                  goto cleanup;
544          }          }
545    
# Line 510  int article_modify(const SECTION_LIST *p Line 551  int article_modify(const SECTION_LIST *p
551                                                          "\n--\n※ 作者已于 %s 修改本文※\n",                                                          "\n--\n※ 作者已于 %s 修改本文※\n",
552                                                          str_modify_dt);                                                          str_modify_dt);
553    
         content_f = malloc((size_t)len_content * 2 + 1);  
         if (content_f == NULL)  
         {  
                 log_error("malloc(content_f) error: OOM\n");  
                 ret = -1;  
                 goto cleanup;  
         }  
   
         sql_content = malloc(SQL_BUFFER_LEN + (size_t)len_content * 2 + 1);  
         if (sql_content == NULL)  
         {  
                 log_error("malloc(sql_content) error: OOM\n");  
                 ret = -1;  
                 goto cleanup;  
         }  
   
554          db = db_open();          db = db_open();
555          if (db == NULL)          if (db == NULL)
556          {          {
# Line 550  int article_modify(const SECTION_LIST *p Line 575  int article_modify(const SECTION_LIST *p
575          }          }
576    
577          // Secure SQL parameters          // Secure SQL parameters
578            content_f = malloc((size_t)len_content * 2 + 1);
579            if (content_f == NULL)
580            {
581                    log_error("malloc(content_f) error: OOM\n");
582                    ret = -1;
583                    goto cleanup;
584            }
585    
586          mysql_real_escape_string(db, content_f, content, (unsigned long)len_content);          mysql_real_escape_string(db, content_f, content, (unsigned long)len_content);
587    
588            free(content);
589            content = NULL;
590    
591          // Add content          // Add content
592          snprintf(sql, SQL_BUFFER_LEN + (size_t)len_content * 2 + 1,          sql_content = malloc(SQL_BUFFER_LEN + (size_t)len_content * 2 + 1);
593            if (sql_content == NULL)
594            {
595                    log_error("malloc(sql_content) error: OOM\n");
596                    ret = -1;
597                    goto cleanup;
598            }
599    
600            snprintf(sql_content, SQL_BUFFER_LEN + (size_t)len_content * 2 + 1,
601                           "INSERT INTO bbs_content(AID, content) values(%d, '%s')",                           "INSERT INTO bbs_content(AID, content) values(%d, '%s')",
602                           p_article->aid, content_f);                           p_article->aid, content_f);
603    
604          if (mysql_query(db, sql) != 0)          free(content_f);
605            content_f = NULL;
606    
607            if (mysql_query(db, sql_content) != 0)
608          {          {
609                  log_error("Add article content error: %s\n", mysql_error(db));                  log_error("Add article content error: %s\n", mysql_error(db));
610                  ret = -1;                  ret = -1;
# Line 566  int article_modify(const SECTION_LIST *p Line 613  int article_modify(const SECTION_LIST *p
613    
614          p_article_new->cid = (int32_t)mysql_insert_id(db);          p_article_new->cid = (int32_t)mysql_insert_id(db);
615    
616            free(sql_content);
617            sql_content = NULL;
618    
619          // Update article          // Update article
620          snprintf(sql, sizeof(sql),          snprintf(sql, sizeof(sql),
621                           "UPDATE bbs SET CID = %d, length = %ld WHERE AID = %d",                           "UPDATE bbs SET CID = %d, length = %ld, excerption = 0 WHERE AID = %d", // Set excerption = 0 explictly in case of rare condition
622                           p_article_new->cid, len_content, p_article->aid);                           p_article_new->cid, len_content, p_article->aid);
623    
624          if (mysql_query(db, sql) != 0)          if (mysql_query(db, sql) != 0)
# Line 606  int article_modify(const SECTION_LIST *p Line 656  int article_modify(const SECTION_LIST *p
656                  goto cleanup;                  goto cleanup;
657          }          }
658    
659            mysql_close(db);
660            db = NULL;
661    
662          clearscr();          clearscr();
663          moveto(1, 1);          moveto(1, 1);
664          prints("修改完成,新内容通常会在%d秒后可见", BBS_section_list_load_interval);          prints("修改完成,新内容通常会在%d秒后可见", BBS_section_list_load_interval);
# Line 613  int article_modify(const SECTION_LIST *p Line 666  int article_modify(const SECTION_LIST *p
666          ret = 1; // Success          ret = 1; // Success
667    
668  cleanup:  cleanup:
669            mysql_free_result(rs);
670          mysql_close(db);          mysql_close(db);
671    
672          // Cleanup buffers          // Cleanup buffers
# Line 649  int article_reply(const SECTION_LIST *p_ Line 703  int article_reply(const SECTION_LIST *p_
703          long quote_content_lines;          long quote_content_lines;
704          long i;          long i;
705          long ret = 0;          long ret = 0;
706            int topic_locked = 0;
707    
708          if (p_section == NULL || p_article == NULL)          if (p_section == NULL || p_article == NULL)
709          {          {
710                  log_error("NULL pointer error\n");                  log_error("NULL pointer error\n");
711          }          }
712    
713          if (p_article->lock) // Reply is not allowed          if (!checkpriv(&BBS_priv, p_section->sid, S_POST))
714          {          {
715                  clearscr();                  clearscr();
716                  moveto(1, 1);                  moveto(1, 1);
717                  prints("该文章谢绝回复");                  prints("您没有权限在本版块发表文章\n");
718                  press_any_key();                  press_any_key();
719    
720                  return 0;                  return 0;
# Line 674  int article_reply(const SECTION_LIST *p_ Line 729  int article_reply(const SECTION_LIST *p_
729          if (db == NULL)          if (db == NULL)
730          {          {
731                  log_error("db_open() error: %s\n", mysql_error(db));                  log_error("db_open() error: %s\n", mysql_error(db));
732                  return -1;                  ret = -1;
733                    goto cleanup;
734            }
735    
736            snprintf(sql, sizeof(sql),
737                             "SELECT `lock` FROM bbs WHERE AID = %d",
738                             (p_article->tid == 0 ? p_article->aid : p_article->tid));
739    
740            if (mysql_query(db, sql) != 0)
741            {
742                    log_error("Query article status error: %s\n", mysql_error(db));
743                    ret = -1;
744                    goto cleanup;
745            }
746            if ((rs = mysql_store_result(db)) == NULL)
747            {
748                    log_error("Get article status data failed\n");
749                    ret = -1;
750                    goto cleanup;
751            }
752    
753            if ((row = mysql_fetch_row(rs)))
754            {
755                    if (atoi(row[0]) != 0)
756                    {
757                            topic_locked = 1;
758                    }
759            }
760            mysql_free_result(rs);
761            rs = NULL;
762    
763            if (topic_locked) // Reply is not allowed
764            {
765                    mysql_close(db);
766                    db = NULL;
767                    
768                    clearscr();
769                    moveto(1, 1);
770                    prints("该主题谢绝回复");
771                    press_any_key();
772    
773                    goto cleanup;
774          }          }
775    
776          snprintf(sql, sizeof(sql),          snprintf(sql, sizeof(sql),
# Line 686  int article_reply(const SECTION_LIST *p_ Line 782  int article_reply(const SECTION_LIST *p_
782          if (mysql_query(db, sql) != 0)          if (mysql_query(db, sql) != 0)
783          {          {
784                  log_error("Query article content error: %s\n", mysql_error(db));                  log_error("Query article content error: %s\n", mysql_error(db));
785                  return -2;                  ret = -1;
786                    goto cleanup;
787          }          }
788          if ((rs = mysql_use_result(db)) == NULL)          if ((rs = mysql_use_result(db)) == NULL)
789          {          {
790                  log_error("Get article content data failed\n");                  log_error("Get article content data failed\n");
791                  return -2;                  ret = -1;
792                    goto cleanup;
793          }          }
794    
795          if ((row = mysql_fetch_row(rs)))          if ((row = mysql_fetch_row(rs)))
# Line 745  int article_reply(const SECTION_LIST *p_ Line 843  int article_reply(const SECTION_LIST *p_
843                  if (p_editor_data == NULL)                  if (p_editor_data == NULL)
844                  {                  {
845                          log_error("editor_data_load(aid=%d, cid=%d) error\n", p_article->aid, atoi(row[0]));                          log_error("editor_data_load(aid=%d, cid=%d) error\n", p_article->aid, atoi(row[0]));
846                          ret = -3;                          ret = -1;
847                          goto cleanup;                          goto cleanup;
848                  }                  }
849    
# Line 880  int article_reply(const SECTION_LIST *p_ Line 978  int article_reply(const SECTION_LIST *p_
978          if (len_content < 0)          if (len_content < 0)
979          {          {
980                  log_error("editor_data_save() error\n");                  log_error("editor_data_save() error\n");
981                  ret = -2;                  ret = -1;
982                  goto cleanup;                  goto cleanup;
983          }          }
984    
# Line 921  int article_reply(const SECTION_LIST *p_ Line 1019  int article_reply(const SECTION_LIST *p_
1019                  rs = NULL;                  rs = NULL;
1020          }          }
1021    
         content_f = malloc((size_t)len_content * 2 + 1);  
         if (content_f == NULL)  
         {  
                 log_error("malloc(content_f) error: OOM\n");  
                 ret = -1;  
                 goto cleanup;  
         }  
   
         sql_content = malloc(SQL_BUFFER_LEN + (size_t)len_content * 2 + 1);  
         if (sql_content == NULL)  
         {  
                 log_error("malloc(sql_content) error: OOM\n");  
                 ret = -1;  
                 goto cleanup;  
         }  
   
1022          // Begin transaction          // Begin transaction
1023          if (mysql_query(db, "SET autocommit=0") != 0)          if (mysql_query(db, "SET autocommit=0") != 0)
1024          {          {
# Line 953  int article_reply(const SECTION_LIST *p_ Line 1035  int article_reply(const SECTION_LIST *p_
1035          }          }
1036    
1037          // Secure SQL parameters          // Secure SQL parameters
1038            content_f = malloc((size_t)len_content * 2 + 1);
1039            if (content_f == NULL)
1040            {
1041                    log_error("malloc(content_f) error: OOM\n");
1042                    ret = -1;
1043                    goto cleanup;
1044            }
1045    
1046          mysql_real_escape_string(db, nickname_f, BBS_nickname, (unsigned long)strnlen(BBS_nickname, sizeof(BBS_nickname)));          mysql_real_escape_string(db, nickname_f, BBS_nickname, (unsigned long)strnlen(BBS_nickname, sizeof(BBS_nickname)));
1047          mysql_real_escape_string(db, title_f, p_article_new->title, strnlen(p_article_new->title, sizeof(p_article_new->title)));          mysql_real_escape_string(db, title_f, p_article_new->title, strnlen(p_article_new->title, sizeof(p_article_new->title)));
1048          mysql_real_escape_string(db, content_f, content, (unsigned long)len_content);          mysql_real_escape_string(db, content_f, content, (unsigned long)len_content);
1049    
1050            free(content);
1051            content = NULL;
1052    
1053          // Add content          // Add content
1054          snprintf(sql, SQL_BUFFER_LEN + (size_t)len_content * 2 + 1,          sql_content = malloc(SQL_BUFFER_LEN + (size_t)len_content * 2 + 1);
1055            if (sql_content == NULL)
1056            {
1057                    log_error("malloc(sql_content) error: OOM\n");
1058                    ret = -1;
1059                    goto cleanup;
1060            }
1061    
1062            snprintf(sql_content, SQL_BUFFER_LEN + (size_t)len_content * 2 + 1,
1063                           "INSERT INTO bbs_content(AID, content) values(0, '%s')",                           "INSERT INTO bbs_content(AID, content) values(0, '%s')",
1064                           content_f);                           content_f);
1065    
1066          if (mysql_query(db, sql) != 0)          free(content_f);
1067            content_f = NULL;
1068    
1069            if (mysql_query(db, sql_content) != 0)
1070          {          {
1071                  log_error("Add article content error: %s\n", mysql_error(db));                  log_error("Add article content error: %s\n", mysql_error(db));
1072                  ret = -1;                  ret = -1;
# Line 971  int article_reply(const SECTION_LIST *p_ Line 1075  int article_reply(const SECTION_LIST *p_
1075    
1076          p_article_new->cid = (int32_t)mysql_insert_id(db);          p_article_new->cid = (int32_t)mysql_insert_id(db);
1077    
1078            free(sql_content);
1079            sql_content = NULL;
1080    
1081          // Add article          // Add article
1082          snprintf(sql, sizeof(sql),          snprintf(sql, sizeof(sql),
1083                           "INSERT INTO bbs(SID, TID, UID, username, nickname, title, CID, transship, "                           "INSERT INTO bbs(SID, TID, UID, username, nickname, title, CID, transship, "
# Line 993  int article_reply(const SECTION_LIST *p_ Line 1100  int article_reply(const SECTION_LIST *p_
1100          snprintf(sql, sizeof(sql),          snprintf(sql, sizeof(sql),
1101                           "UPDATE bbs SET reply_count = reply_count + 1, "                           "UPDATE bbs SET reply_count = reply_count + 1, "
1102                           "last_reply_dt = NOW(), last_reply_UID=%d, last_reply_username = '%s', "                           "last_reply_dt = NOW(), last_reply_UID=%d, last_reply_username = '%s', "
1103                           "last_reply_nickname = '%s' WHERE Aid = %d",                           "last_reply_nickname = '%s' WHERE AID = %d",
1104                           BBS_priv.uid, BBS_username, nickname_f, p_article->aid);                           BBS_priv.uid, BBS_username, nickname_f,
1105                             (p_article->tid == 0 ? p_article->aid : p_article->tid));
1106    
1107            if (mysql_query(db, sql) != 0)
1108            {
1109                    log_error("Update topic article error: %s\n", mysql_error(db));
1110                    ret = -1;
1111                    goto cleanup;
1112            }
1113    
1114          // Link content to article          // Link content to article
1115          snprintf(sql, sizeof(sql),          snprintf(sql, sizeof(sql),
# Line 1044  int article_reply(const SECTION_LIST *p_ Line 1159  int article_reply(const SECTION_LIST *p_
1159                  goto cleanup;                  goto cleanup;
1160          }          }
1161    
1162            mysql_close(db);
1163            db = NULL;
1164    
1165          clearscr();          clearscr();
1166          moveto(1, 1);          moveto(1, 1);
1167          prints("发送完成,新文章通常会在%d秒后可见", BBS_section_list_load_interval);          prints("发送完成,新文章通常会在%d秒后可见", BBS_section_list_load_interval);


Legend:
Removed lines/characters  
Changed lines/characters
  Added lines/characters

webmaster@leafok.com
ViewVC Help
Powered by ViewVC 1.3.0-beta1