/[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.10 by sysadm, Sat Jun 14 11:58:00 2025 UTC Revision 1.16 by sysadm, Sun Jun 15 03:16:44 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 414  int article_modify(const SECTION_LIST *p Line 434  int article_modify(const SECTION_LIST *p
434          if (db == NULL)          if (db == NULL)
435          {          {
436                  log_error("db_open() error: %s\n", mysql_error(db));                  log_error("db_open() error: %s\n", mysql_error(db));
437                  return -1;                  ret = -1;
438                    goto cleanup;
439          }          }
440    
441          snprintf(sql, sizeof(sql),          snprintf(sql, sizeof(sql),
# Line 426  int article_modify(const SECTION_LIST *p Line 447  int article_modify(const SECTION_LIST *p
447          if (mysql_query(db, sql) != 0)          if (mysql_query(db, sql) != 0)
448          {          {
449                  log_error("Query article content error: %s\n", mysql_error(db));                  log_error("Query article content error: %s\n", mysql_error(db));
450                  ret = -2;                  ret = -1;
451                  goto cleanup;                  goto cleanup;
452          }          }
453          if ((rs = mysql_use_result(db)) == NULL)          if ((rs = mysql_use_result(db)) == NULL)
454          {          {
455                  log_error("Get article content data failed\n");                  log_error("Get article content data failed\n");
456                  ret = -2;                  ret = -1;
457                  goto cleanup;                  goto cleanup;
458          }          }
459    
460          if ((row = mysql_fetch_row(rs)))          if ((row = mysql_fetch_row(rs)))
461          {          {
462                  p_editor_data = editor_data_load(row[1]);                  content = malloc(ARTICLE_CONTENT_MAX_LEN);
463                    if (content == NULL)
464                    {
465                            log_error("malloc(content) error: OOM\n");
466                            ret = -1;
467                            goto cleanup;
468                    }
469    
470                    strncpy(content, row[1], ARTICLE_CONTENT_MAX_LEN - 1);
471                    content[ARTICLE_CONTENT_MAX_LEN - 1] = '\0';
472    
473                    // Remove control sequence
474                    len_content = ctrl_seq_filter(content);
475    
476                    p_editor_data = editor_data_load(content);
477                  if (p_editor_data == NULL)                  if (p_editor_data == NULL)
478                  {                  {
479                          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]));
480                          ret = -3;                          ret = -1;
481                          goto cleanup;                          goto cleanup;
482                  }                  }
483    
484                    free(content);
485                    content = NULL;
486          }          }
487          mysql_free_result(rs);          mysql_free_result(rs);
488          rs = NULL;          rs = NULL;
# Line 498  int article_modify(const SECTION_LIST *p Line 536  int article_modify(const SECTION_LIST *p
536          if (len_content < 0)          if (len_content < 0)
537          {          {
538                  log_error("editor_data_save() error\n");                  log_error("editor_data_save() error\n");
539                  ret = -2;                  ret = -1;
540                  goto cleanup;                  goto cleanup;
541          }          }
542    
# Line 510  int article_modify(const SECTION_LIST *p Line 548  int article_modify(const SECTION_LIST *p
548                                                          "\n--\n※ 作者已于 %s 修改本文※\n",                                                          "\n--\n※ 作者已于 %s 修改本文※\n",
549                                                          str_modify_dt);                                                          str_modify_dt);
550    
         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;  
         }  
   
551          db = db_open();          db = db_open();
552          if (db == NULL)          if (db == NULL)
553          {          {
# Line 550  int article_modify(const SECTION_LIST *p Line 572  int article_modify(const SECTION_LIST *p
572          }          }
573    
574          // Secure SQL parameters          // Secure SQL parameters
575            content_f = malloc((size_t)len_content * 2 + 1);
576            if (content_f == NULL)
577            {
578                    log_error("malloc(content_f) error: OOM\n");
579                    ret = -1;
580                    goto cleanup;
581            }
582    
583          mysql_real_escape_string(db, content_f, content, (unsigned long)len_content);          mysql_real_escape_string(db, content_f, content, (unsigned long)len_content);
584    
585            free(content);
586            content = NULL;
587    
588          // Add content          // Add content
589          snprintf(sql, SQL_BUFFER_LEN + (size_t)len_content * 2 + 1,          sql_content = malloc(SQL_BUFFER_LEN + (size_t)len_content * 2 + 1);
590            if (sql_content == NULL)
591            {
592                    log_error("malloc(sql_content) error: OOM\n");
593                    ret = -1;
594                    goto cleanup;
595            }
596    
597            snprintf(sql_content, SQL_BUFFER_LEN + (size_t)len_content * 2 + 1,
598                           "INSERT INTO bbs_content(AID, content) values(%d, '%s')",                           "INSERT INTO bbs_content(AID, content) values(%d, '%s')",
599                           p_article->aid, content_f);                           p_article->aid, content_f);
600    
601          if (mysql_query(db, sql) != 0)          free(content_f);
602            content_f = NULL;
603    
604            if (mysql_query(db, sql_content) != 0)
605          {          {
606                  log_error("Add article content error: %s\n", mysql_error(db));                  log_error("Add article content error: %s\n", mysql_error(db));
607                  ret = -1;                  ret = -1;
# Line 566  int article_modify(const SECTION_LIST *p Line 610  int article_modify(const SECTION_LIST *p
610    
611          p_article_new->cid = (int32_t)mysql_insert_id(db);          p_article_new->cid = (int32_t)mysql_insert_id(db);
612    
613            free(sql_content);
614            sql_content = NULL;
615    
616          // Update article          // Update article
617          snprintf(sql, sizeof(sql),          snprintf(sql, sizeof(sql),
618                           "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
619                           p_article_new->cid, len_content, p_article->aid);                           p_article_new->cid, len_content, p_article->aid);
620    
621          if (mysql_query(db, sql) != 0)          if (mysql_query(db, sql) != 0)
# Line 613  int article_modify(const SECTION_LIST *p Line 660  int article_modify(const SECTION_LIST *p
660          ret = 1; // Success          ret = 1; // Success
661    
662  cleanup:  cleanup:
663            mysql_free_result(rs);
664          mysql_close(db);          mysql_close(db);
665    
666          // Cleanup buffers          // Cleanup buffers
# Line 649  int article_reply(const SECTION_LIST *p_ Line 697  int article_reply(const SECTION_LIST *p_
697          long quote_content_lines;          long quote_content_lines;
698          long i;          long i;
699          long ret = 0;          long ret = 0;
700            int topic_locked = 0;
701    
702          if (p_section == NULL || p_article == NULL)          if (p_section == NULL || p_article == NULL)
703          {          {
704                  log_error("NULL pointer error\n");                  log_error("NULL pointer error\n");
705          }          }
706    
707          if (p_article->lock) // Reply is not allowed          if (!checkpriv(&BBS_priv, p_section->sid, S_POST))
708          {          {
709                  clearscr();                  clearscr();
710                  moveto(1, 1);                  moveto(1, 1);
711                  prints("该文章谢绝回复");                  prints("您没有权限在本版块发表文章\n");
712                  press_any_key();                  press_any_key();
713    
714                  return 0;                  return 0;
# Line 674  int article_reply(const SECTION_LIST *p_ Line 723  int article_reply(const SECTION_LIST *p_
723          if (db == NULL)          if (db == NULL)
724          {          {
725                  log_error("db_open() error: %s\n", mysql_error(db));                  log_error("db_open() error: %s\n", mysql_error(db));
726                  return -1;                  ret = -1;
727                    goto cleanup;
728            }
729    
730            snprintf(sql, sizeof(sql),
731                             "SELECT `lock` FROM bbs WHERE AID = %d",
732                             (p_article->tid == 0 ? p_article->aid : p_article->tid));
733    
734            if (mysql_query(db, sql) != 0)
735            {
736                    log_error("Query article status error: %s\n", mysql_error(db));
737                    ret = -1;
738                    goto cleanup;
739            }
740            if ((rs = mysql_store_result(db)) == NULL)
741            {
742                    log_error("Get article status data failed\n");
743                    ret = -1;
744                    goto cleanup;
745            }
746    
747            if ((row = mysql_fetch_row(rs)))
748            {
749                    if (atoi(row[0]) != 0)
750                    {
751                            topic_locked = 1;
752                    }
753            }
754            mysql_free_result(rs);
755            rs = NULL;
756    
757            if (topic_locked) // Reply is not allowed
758            {
759                    clearscr();
760                    moveto(1, 1);
761                    prints("该主题谢绝回复");
762                    press_any_key();
763    
764                    goto cleanup;
765          }          }
766    
767          snprintf(sql, sizeof(sql),          snprintf(sql, sizeof(sql),
# Line 686  int article_reply(const SECTION_LIST *p_ Line 773  int article_reply(const SECTION_LIST *p_
773          if (mysql_query(db, sql) != 0)          if (mysql_query(db, sql) != 0)
774          {          {
775                  log_error("Query article content error: %s\n", mysql_error(db));                  log_error("Query article content error: %s\n", mysql_error(db));
776                  return -2;                  ret = -1;
777                    goto cleanup;
778          }          }
779          if ((rs = mysql_use_result(db)) == NULL)          if ((rs = mysql_use_result(db)) == NULL)
780          {          {
781                  log_error("Get article content data failed\n");                  log_error("Get article content data failed\n");
782                  return -2;                  ret = -1;
783                    goto cleanup;
784          }          }
785    
786          if ((row = mysql_fetch_row(rs)))          if ((row = mysql_fetch_row(rs)))
# Line 745  int article_reply(const SECTION_LIST *p_ Line 834  int article_reply(const SECTION_LIST *p_
834                  if (p_editor_data == NULL)                  if (p_editor_data == NULL)
835                  {                  {
836                          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]));
837                          ret = -3;                          ret = -1;
838                          goto cleanup;                          goto cleanup;
839                  }                  }
840    
# Line 880  int article_reply(const SECTION_LIST *p_ Line 969  int article_reply(const SECTION_LIST *p_
969          if (len_content < 0)          if (len_content < 0)
970          {          {
971                  log_error("editor_data_save() error\n");                  log_error("editor_data_save() error\n");
972                  ret = -2;                  ret = -1;
973                  goto cleanup;                  goto cleanup;
974          }          }
975    
# Line 921  int article_reply(const SECTION_LIST *p_ Line 1010  int article_reply(const SECTION_LIST *p_
1010                  rs = NULL;                  rs = NULL;
1011          }          }
1012    
         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;  
         }  
   
1013          // Begin transaction          // Begin transaction
1014          if (mysql_query(db, "SET autocommit=0") != 0)          if (mysql_query(db, "SET autocommit=0") != 0)
1015          {          {
# Line 953  int article_reply(const SECTION_LIST *p_ Line 1026  int article_reply(const SECTION_LIST *p_
1026          }          }
1027    
1028          // Secure SQL parameters          // Secure SQL parameters
1029            content_f = malloc((size_t)len_content * 2 + 1);
1030            if (content_f == NULL)
1031            {
1032                    log_error("malloc(content_f) error: OOM\n");
1033                    ret = -1;
1034                    goto cleanup;
1035            }
1036    
1037          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)));
1038          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)));
1039          mysql_real_escape_string(db, content_f, content, (unsigned long)len_content);          mysql_real_escape_string(db, content_f, content, (unsigned long)len_content);
1040    
1041            free(content);
1042            content = NULL;
1043    
1044          // Add content          // Add content
1045          snprintf(sql, SQL_BUFFER_LEN + (size_t)len_content * 2 + 1,          sql_content = malloc(SQL_BUFFER_LEN + (size_t)len_content * 2 + 1);
1046            if (sql_content == NULL)
1047            {
1048                    log_error("malloc(sql_content) error: OOM\n");
1049                    ret = -1;
1050                    goto cleanup;
1051            }
1052    
1053            snprintf(sql_content, SQL_BUFFER_LEN + (size_t)len_content * 2 + 1,
1054                           "INSERT INTO bbs_content(AID, content) values(0, '%s')",                           "INSERT INTO bbs_content(AID, content) values(0, '%s')",
1055                           content_f);                           content_f);
1056    
1057          if (mysql_query(db, sql) != 0)          free(content_f);
1058            content_f = NULL;
1059    
1060            if (mysql_query(db, sql_content) != 0)
1061          {          {
1062                  log_error("Add article content error: %s\n", mysql_error(db));                  log_error("Add article content error: %s\n", mysql_error(db));
1063                  ret = -1;                  ret = -1;
# Line 971  int article_reply(const SECTION_LIST *p_ Line 1066  int article_reply(const SECTION_LIST *p_
1066    
1067          p_article_new->cid = (int32_t)mysql_insert_id(db);          p_article_new->cid = (int32_t)mysql_insert_id(db);
1068    
1069            free(sql_content);
1070            sql_content = NULL;
1071    
1072          // Add article          // Add article
1073          snprintf(sql, sizeof(sql),          snprintf(sql, sizeof(sql),
1074                           "INSERT INTO bbs(SID, TID, UID, username, nickname, title, CID, transship, "                           "INSERT INTO bbs(SID, TID, UID, username, nickname, title, CID, transship, "
1075                           "sub_dt, sub_ip, reply_note, exp, last_reply_dt, icon, length) "                           "sub_dt, sub_ip, reply_note, exp, last_reply_dt, icon, length) "
1076                           "VALUES(%d, %d, %d, '%s', '%s', '%s', %d, 0, NOW(), '%s', 1, %d, NOW(), 1, %ld)",                           "VALUES(%d, %d, %d, '%s', '%s', '%s', %d, 0, NOW(), '%s', 1, %d, NOW(), 1, %ld)",
1077                           p_section->sid, p_article->aid, BBS_priv.uid, BBS_username, nickname_f, title_f,                           p_section->sid, (p_article->tid == 0 ? p_article->aid : p_article->tid),
1078                             BBS_priv.uid, BBS_username, nickname_f, title_f,
1079                           p_article_new->cid, hostaddr_client, BBS_user_exp, len_content);                           p_article_new->cid, hostaddr_client, BBS_user_exp, len_content);
1080    
1081          if (mysql_query(db, sql) != 0)          if (mysql_query(db, sql) != 0)
# Line 992  int article_reply(const SECTION_LIST *p_ Line 1091  int article_reply(const SECTION_LIST *p_
1091          snprintf(sql, sizeof(sql),          snprintf(sql, sizeof(sql),
1092                           "UPDATE bbs SET reply_count = reply_count + 1, "                           "UPDATE bbs SET reply_count = reply_count + 1, "
1093                           "last_reply_dt = NOW(), last_reply_UID=%d, last_reply_username = '%s', "                           "last_reply_dt = NOW(), last_reply_UID=%d, last_reply_username = '%s', "
1094                           "last_reply_nickname = '%s' WHERE Aid = %d",                           "last_reply_nickname = '%s' WHERE AID = %d",
1095                           BBS_priv.uid, BBS_username, nickname_f, p_article->aid);                           BBS_priv.uid, BBS_username, nickname_f,
1096                             (p_article->tid == 0 ? p_article->aid : p_article->tid));
1097    
1098            if (mysql_query(db, sql) != 0)
1099            {
1100                    log_error("Update topic article error: %s\n", mysql_error(db));
1101                    ret = -1;
1102                    goto cleanup;
1103            }
1104    
1105          // Link content to article          // Link content to article
1106          snprintf(sql, sizeof(sql),          snprintf(sql, sizeof(sql),


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

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