/[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.9 by sysadm, Sat Jun 14 11:15:46 2025 UTC Revision 1.22 by sysadm, Sat Jun 21 02:15:18 2025 UTC
# Line 14  Line 14 
14   *                                                                         *   *                                                                         *
15   ***************************************************************************/   ***************************************************************************/
16    
 #define _POSIX_C_SOURCE 200809L  
   
 #include "article_post.h"  
17  #include "article_cache.h"  #include "article_cache.h"
18  #include "editor.h"  #include "article_post.h"
 #include "screen.h"  
19  #include "bbs.h"  #include "bbs.h"
20  #include "log.h"  #include "database.h"
21    #include "editor.h"
22  #include "io.h"  #include "io.h"
23    #include "log.h"
24  #include "lml.h"  #include "lml.h"
25  #include "database.h"  #include "screen.h"
26  #include "user_priv.h"  #include "user_priv.h"
27  #include <ctype.h>  #include <ctype.h>
28  #include <string.h>  #include <string.h>
# Line 63  int article_post(const SECTION_LIST *p_s Line 61  int article_post(const SECTION_LIST *p_s
61                  log_error("NULL pointer error\n");                  log_error("NULL pointer error\n");
62          }          }
63    
64            if (!checkpriv(&BBS_priv, p_section->sid, S_POST))
65            {
66                    clearscr();
67                    moveto(1, 1);
68                    prints("您没有权限在本版块发表文章\n");
69                    press_any_key();
70    
71                    return 0;
72            }
73    
74          p_article_new->title[0] = '\0';          p_article_new->title[0] = '\0';
75          title_input[0] = '\0';          title_input[0] = '\0';
76          p_article_new->transship = 0;          p_article_new->transship = 0;
# Line 71  int article_post(const SECTION_LIST *p_s Line 79  int article_post(const SECTION_LIST *p_s
79          if (p_editor_data == NULL)          if (p_editor_data == NULL)
80          {          {
81                  log_error("editor_data_load() error\n");                  log_error("editor_data_load() error\n");
82                  return -2;                  ret = -1;
83                    goto cleanup;
84          }          }
85    
86          // Set title and sign          // Set title and sign
# Line 99  int article_post(const SECTION_LIST *p_s Line 108  int article_post(const SECTION_LIST *p_s
108                  {                  {
109                          switch (toupper(ch))                          switch (toupper(ch))
110                          {                          {
111                            case KEY_NULL:
112                            case KEY_TIMEOUT:
113                                    goto cleanup;
114                          case CR:                          case CR:
115                                  igetch_reset();                                  igetch_reset();
116                                  break;                                  break;
# Line 162  int article_post(const SECTION_LIST *p_s Line 174  int article_post(const SECTION_LIST *p_s
174                          {                          {
175                                  switch (toupper(ch))                                  switch (toupper(ch))
176                                  {                                  {
177                                    case KEY_NULL:
178                                    case KEY_TIMEOUT:
179                                            goto cleanup;
180                                  case CR:                                  case CR:
181                                          igetch_reset();                                          igetch_reset();
182                                  case 'S':                                  case 'S':
# Line 190  int article_post(const SECTION_LIST *p_s Line 205  int article_post(const SECTION_LIST *p_s
205                  }                  }
206          }          }
207    
208            if (SYS_server_exit) // Do not save data on shutdown
209            {
210                    goto cleanup;
211            }
212    
213          content = malloc(ARTICLE_CONTENT_MAX_LEN);          content = malloc(ARTICLE_CONTENT_MAX_LEN);
214          if (content == NULL)          if (content == NULL)
215          {          {
# Line 202  int article_post(const SECTION_LIST *p_s Line 222  int article_post(const SECTION_LIST *p_s
222          if (len_content < 0)          if (len_content < 0)
223          {          {
224                  log_error("editor_data_save() error\n");                  log_error("editor_data_save() error\n");
225                  ret = -2;                  ret = -1;
226                  goto cleanup;                  goto cleanup;
227          }          }
228    
# Line 243  int article_post(const SECTION_LIST *p_s Line 263  int article_post(const SECTION_LIST *p_s
263                  rs = NULL;                  rs = NULL;
264          }          }
265    
         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;  
         }  
   
266          // Begin transaction          // Begin transaction
267          if (mysql_query(db, "SET autocommit=0") != 0)          if (mysql_query(db, "SET autocommit=0") != 0)
268          {          {
# Line 275  int article_post(const SECTION_LIST *p_s Line 279  int article_post(const SECTION_LIST *p_s
279          }          }
280    
281          // Secure SQL parameters          // Secure SQL parameters
282            content_f = malloc((size_t)len_content * 2 + 1);
283            if (content_f == NULL)
284            {
285                    log_error("malloc(content_f) error: OOM\n");
286                    ret = -1;
287                    goto cleanup;
288            }
289    
290          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)));
291          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)));
292          mysql_real_escape_string(db, content_f, content, (unsigned long)len_content);          mysql_real_escape_string(db, content_f, content, (unsigned long)len_content);
293    
294            free(content);
295            content = NULL;
296    
297          // Add content          // Add content
298          snprintf(sql, SQL_BUFFER_LEN + (size_t)len_content * 2 + 1,          sql_content = malloc(SQL_BUFFER_LEN + (size_t)len_content * 2 + 1);
299            if (sql_content == NULL)
300            {
301                    log_error("malloc(sql_content) error: OOM\n");
302                    ret = -1;
303                    goto cleanup;
304            }
305    
306            snprintf(sql_content, SQL_BUFFER_LEN + (size_t)len_content * 2 + 1,
307                           "INSERT INTO bbs_content(AID, content) values(0, '%s')",                           "INSERT INTO bbs_content(AID, content) values(0, '%s')",
308                           content_f);                           content_f);
309    
310          if (mysql_query(db, sql) != 0)          free(content_f);
311            content_f = NULL;
312    
313            if (mysql_query(db, sql_content) != 0)
314          {          {
315                  log_error("Add article content error: %s\n", mysql_error(db));                  log_error("Add article content error: %s\n", mysql_error(db));
316                  ret = -1;                  ret = -1;
# Line 293  int article_post(const SECTION_LIST *p_s Line 319  int article_post(const SECTION_LIST *p_s
319    
320          p_article_new->cid = (int32_t)mysql_insert_id(db);          p_article_new->cid = (int32_t)mysql_insert_id(db);
321    
322            free(sql_content);
323            sql_content = NULL;
324    
325          // Add article          // Add article
326          snprintf(sql, sizeof(sql),          snprintf(sql, sizeof(sql),
327                           "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 387  int article_post(const SECTION_LIST *p_s
387                  goto cleanup;                  goto cleanup;
388          }          }
389    
390            mysql_close(db);
391            db = NULL;
392    
393          clearscr();          clearscr();
394          moveto(1, 1);          moveto(1, 1);
395          prints("发送完成,新文章通常会在%d秒后可见", BBS_section_list_load_interval);          prints("发送完成,新文章通常会在%d秒后可见", BBS_section_list_load_interval);
# Line 414  int article_modify(const SECTION_LIST *p Line 446  int article_modify(const SECTION_LIST *p
446          if (db == NULL)          if (db == NULL)
447          {          {
448                  log_error("db_open() error: %s\n", mysql_error(db));                  log_error("db_open() error: %s\n", mysql_error(db));
449                  return -1;                  ret = -1;
450                    goto cleanup;
451          }          }
452    
453          snprintf(sql, sizeof(sql),          snprintf(sql, sizeof(sql),
# Line 426  int article_modify(const SECTION_LIST *p Line 459  int article_modify(const SECTION_LIST *p
459          if (mysql_query(db, sql) != 0)          if (mysql_query(db, sql) != 0)
460          {          {
461                  log_error("Query article content error: %s\n", mysql_error(db));                  log_error("Query article content error: %s\n", mysql_error(db));
462                  ret = -2;                  ret = -1;
463                  goto cleanup;                  goto cleanup;
464          }          }
465          if ((rs = mysql_use_result(db)) == NULL)          if ((rs = mysql_use_result(db)) == NULL)
466          {          {
467                  log_error("Get article content data failed\n");                  log_error("Get article content data failed\n");
468                  ret = -2;                  ret = -1;
469                  goto cleanup;                  goto cleanup;
470          }          }
471    
472          if ((row = mysql_fetch_row(rs)))          if ((row = mysql_fetch_row(rs)))
473          {          {
474                  p_editor_data = editor_data_load(row[1]);                  content = malloc(ARTICLE_CONTENT_MAX_LEN);
475                    if (content == NULL)
476                    {
477                            log_error("malloc(content) error: OOM\n");
478                            ret = -1;
479                            goto cleanup;
480                    }
481    
482                    strncpy(content, row[1], ARTICLE_CONTENT_MAX_LEN - 1);
483                    content[ARTICLE_CONTENT_MAX_LEN - 1] = '\0';
484    
485                    // Remove control sequence
486                    len_content = str_filter(content, 0);
487    
488                    p_editor_data = editor_data_load(content);
489                  if (p_editor_data == NULL)                  if (p_editor_data == NULL)
490                  {                  {
491                          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]));
492                          ret = -3;                          ret = -1;
493                          goto cleanup;                          goto cleanup;
494                  }                  }
495    
496                    free(content);
497                    content = NULL;
498          }          }
499          mysql_free_result(rs);          mysql_free_result(rs);
500          rs = NULL;          rs = NULL;
# Line 465  int article_modify(const SECTION_LIST *p Line 515  int article_modify(const SECTION_LIST *p
515                  {                  {
516                          switch (toupper(ch))                          switch (toupper(ch))
517                          {                          {
518                            case KEY_NULL:
519                            case KEY_TIMEOUT:
520                                    goto cleanup;
521                          case CR:                          case CR:
522                                  igetch_reset();                                  igetch_reset();
523                          case 'S':                          case 'S':
# Line 485  int article_modify(const SECTION_LIST *p Line 538  int article_modify(const SECTION_LIST *p
538                  }                  }
539          }          }
540    
541            if (SYS_server_exit) // Do not save data on shutdown
542            {
543                    goto cleanup;
544            }
545    
546          // Allocate buffers in big size          // Allocate buffers in big size
547          content = malloc(ARTICLE_CONTENT_MAX_LEN);          content = malloc(ARTICLE_CONTENT_MAX_LEN);
548          if (content == NULL)          if (content == NULL)
# Line 498  int article_modify(const SECTION_LIST *p Line 556  int article_modify(const SECTION_LIST *p
556          if (len_content < 0)          if (len_content < 0)
557          {          {
558                  log_error("editor_data_save() error\n");                  log_error("editor_data_save() error\n");
559                  ret = -2;                  ret = -1;
560                  goto cleanup;                  goto cleanup;
561          }          }
562    
# Line 510  int article_modify(const SECTION_LIST *p Line 568  int article_modify(const SECTION_LIST *p
568                                                          "\n--\n※ 作者已于 %s 修改本文※\n",                                                          "\n--\n※ 作者已于 %s 修改本文※\n",
569                                                          str_modify_dt);                                                          str_modify_dt);
570    
         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;  
         }  
   
571          db = db_open();          db = db_open();
572          if (db == NULL)          if (db == NULL)
573          {          {
# Line 550  int article_modify(const SECTION_LIST *p Line 592  int article_modify(const SECTION_LIST *p
592          }          }
593    
594          // Secure SQL parameters          // Secure SQL parameters
595            content_f = malloc((size_t)len_content * 2 + 1);
596            if (content_f == NULL)
597            {
598                    log_error("malloc(content_f) error: OOM\n");
599                    ret = -1;
600                    goto cleanup;
601            }
602    
603          mysql_real_escape_string(db, content_f, content, (unsigned long)len_content);          mysql_real_escape_string(db, content_f, content, (unsigned long)len_content);
604    
605            free(content);
606            content = NULL;
607    
608          // Add content          // Add content
609          snprintf(sql, SQL_BUFFER_LEN + (size_t)len_content * 2 + 1,          sql_content = malloc(SQL_BUFFER_LEN + (size_t)len_content * 2 + 1);
610            if (sql_content == NULL)
611            {
612                    log_error("malloc(sql_content) error: OOM\n");
613                    ret = -1;
614                    goto cleanup;
615            }
616    
617            snprintf(sql_content, SQL_BUFFER_LEN + (size_t)len_content * 2 + 1,
618                           "INSERT INTO bbs_content(AID, content) values(%d, '%s')",                           "INSERT INTO bbs_content(AID, content) values(%d, '%s')",
619                           p_article->aid, content_f);                           p_article->aid, content_f);
620    
621          if (mysql_query(db, sql) != 0)          free(content_f);
622            content_f = NULL;
623    
624            if (mysql_query(db, sql_content) != 0)
625          {          {
626                  log_error("Add article content error: %s\n", mysql_error(db));                  log_error("Add article content error: %s\n", mysql_error(db));
627                  ret = -1;                  ret = -1;
# Line 566  int article_modify(const SECTION_LIST *p Line 630  int article_modify(const SECTION_LIST *p
630    
631          p_article_new->cid = (int32_t)mysql_insert_id(db);          p_article_new->cid = (int32_t)mysql_insert_id(db);
632    
633            free(sql_content);
634            sql_content = NULL;
635    
636          // Update article          // Update article
637          snprintf(sql, sizeof(sql),          snprintf(sql, sizeof(sql),
638                           "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
639                           p_article_new->cid, len_content, p_article->aid);                           p_article_new->cid, len_content, p_article->aid);
640    
641          if (mysql_query(db, sql) != 0)          if (mysql_query(db, sql) != 0)
# Line 606  int article_modify(const SECTION_LIST *p Line 673  int article_modify(const SECTION_LIST *p
673                  goto cleanup;                  goto cleanup;
674          }          }
675    
676            mysql_close(db);
677            db = NULL;
678    
679          clearscr();          clearscr();
680          moveto(1, 1);          moveto(1, 1);
681          prints("修改完成,新内容通常会在%d秒后可见", BBS_section_list_load_interval);          prints("修改完成,新内容通常会在%d秒后可见", BBS_section_list_load_interval);
# Line 613  int article_modify(const SECTION_LIST *p Line 683  int article_modify(const SECTION_LIST *p
683          ret = 1; // Success          ret = 1; // Success
684    
685  cleanup:  cleanup:
686            mysql_free_result(rs);
687          mysql_close(db);          mysql_close(db);
688    
689          // Cleanup buffers          // Cleanup buffers
# Line 649  int article_reply(const SECTION_LIST *p_ Line 720  int article_reply(const SECTION_LIST *p_
720          long quote_content_lines;          long quote_content_lines;
721          long i;          long i;
722          long ret = 0;          long ret = 0;
723            int topic_locked = 0;
724    
725          if (p_section == NULL || p_article == NULL)          if (p_section == NULL || p_article == NULL)
726          {          {
727                  log_error("NULL pointer error\n");                  log_error("NULL pointer error\n");
728          }          }
729    
730          if (p_article->lock) // Reply is not allowed          if (!checkpriv(&BBS_priv, p_section->sid, S_POST))
731          {          {
732                  clearscr();                  clearscr();
733                  moveto(1, 1);                  moveto(1, 1);
734                  prints("该文章谢绝回复");                  prints("您没有权限在本版块发表文章\n");
735                  press_any_key();                  press_any_key();
736    
737                  return 0;                  return 0;
# Line 667  int article_reply(const SECTION_LIST *p_ Line 739  int article_reply(const SECTION_LIST *p_
739    
740          p_article_new->title[0] = '\0';          p_article_new->title[0] = '\0';
741          snprintf(title_input, sizeof(title_input), "Re: %s", p_article->title);          snprintf(title_input, sizeof(title_input), "Re: %s", p_article->title);
742          len = split_line(title_input, TITLE_INPUT_MAX_LEN, &eol, &display_len);          len = split_line(title_input, TITLE_INPUT_MAX_LEN, &eol, &display_len, 0);
743          title_input[len] = '\0';          title_input[len] = '\0';
744    
745          db = db_open();          db = db_open();
746          if (db == NULL)          if (db == NULL)
747          {          {
748                  log_error("db_open() error: %s\n", mysql_error(db));                  log_error("db_open() error: %s\n", mysql_error(db));
749                  return -1;                  ret = -1;
750                    goto cleanup;
751            }
752    
753            snprintf(sql, sizeof(sql),
754                             "SELECT `lock` FROM bbs WHERE AID = %d",
755                             (p_article->tid == 0 ? p_article->aid : p_article->tid));
756    
757            if (mysql_query(db, sql) != 0)
758            {
759                    log_error("Query article status error: %s\n", mysql_error(db));
760                    ret = -1;
761                    goto cleanup;
762            }
763            if ((rs = mysql_store_result(db)) == NULL)
764            {
765                    log_error("Get article status data failed\n");
766                    ret = -1;
767                    goto cleanup;
768            }
769    
770            if ((row = mysql_fetch_row(rs)))
771            {
772                    if (atoi(row[0]) != 0)
773                    {
774                            topic_locked = 1;
775                    }
776            }
777            mysql_free_result(rs);
778            rs = NULL;
779    
780            if (topic_locked) // Reply is not allowed
781            {
782                    mysql_close(db);
783                    db = NULL;
784    
785                    clearscr();
786                    moveto(1, 1);
787                    prints("该主题谢绝回复");
788                    press_any_key();
789    
790                    goto cleanup;
791          }          }
792    
793          snprintf(sql, sizeof(sql),          snprintf(sql, sizeof(sql),
# Line 686  int article_reply(const SECTION_LIST *p_ Line 799  int article_reply(const SECTION_LIST *p_
799          if (mysql_query(db, sql) != 0)          if (mysql_query(db, sql) != 0)
800          {          {
801                  log_error("Query article content error: %s\n", mysql_error(db));                  log_error("Query article content error: %s\n", mysql_error(db));
802                  return -2;                  ret = -1;
803                    goto cleanup;
804          }          }
805          if ((rs = mysql_use_result(db)) == NULL)          if ((rs = mysql_use_result(db)) == NULL)
806          {          {
807                  log_error("Get article content data failed\n");                  log_error("Get article content data failed\n");
808                  return -2;                  ret = -1;
809                    goto cleanup;
810          }          }
811    
812          if ((row = mysql_fetch_row(rs)))          if ((row = mysql_fetch_row(rs)))
# Line 717  int article_reply(const SECTION_LIST *p_ Line 832  int article_reply(const SECTION_LIST *p_
832                  content_f[len] = '\0';                  content_f[len] = '\0';
833    
834                  // Remove control sequence                  // Remove control sequence
835                  len = ctrl_seq_filter(content_f);                  len = str_filter(content_f, 0);
836    
837                  len = snprintf(content, ARTICLE_CONTENT_MAX_LEN,                  len = snprintf(content, ARTICLE_CONTENT_MAX_LEN,
838                                             "\n\n【 在 %s (%s) 的大作中提到: 】\n",                                             "\n\n【 在 %s (%s) 的大作中提到: 】\n",
839                                             p_article->username, p_article->nickname);                                             p_article->username, p_article->nickname);
840    
841                  quote_content_lines = split_data_lines(content_f, ARTICLE_QUOTE_LINE_MAX_LEN, line_offsets, ARTICLE_QUOTE_MAX_LINES + 1);                  quote_content_lines = split_data_lines(content_f, ARTICLE_QUOTE_LINE_MAX_LEN, line_offsets, ARTICLE_QUOTE_MAX_LINES + 1, 0);
842                  for (i = 0; i < quote_content_lines; i++)                  for (i = 0; i < quote_content_lines; i++)
843                  {                  {
844                          memcpy(content + len, ": ", 2); // quote line prefix                          memcpy(content + len, ": ", 2); // quote line prefix
845                          len += 2;                          len += 2;
846                          memcpy(content + len, content_f + line_offsets[i], (size_t)(line_offsets[i + 1] - line_offsets[i]));                          memcpy(content + len, content_f + line_offsets[i], (size_t)(line_offsets[i + 1] - line_offsets[i]));
847                          len += (line_offsets[i + 1] - line_offsets[i]);                          len += (line_offsets[i + 1] - line_offsets[i]);
848                            if (content[len - 1] != '\n') // Appennd \n if not exist
849                            {
850                                    content[len] = '\n';
851                                    len++;
852                            }
853                  }                  }
854                  if (content[len - 1] != '\n') // Appennd \n if not exist                  if (content[len - 1] != '\n') // Appennd \n if not exist
855                  {                  {
# Line 745  int article_reply(const SECTION_LIST *p_ Line 865  int article_reply(const SECTION_LIST *p_
865                  if (p_editor_data == NULL)                  if (p_editor_data == NULL)
866                  {                  {
867                          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]));
868                          ret = -3;                          ret = -1;
869                          goto cleanup;                          goto cleanup;
870                  }                  }
871    
# Line 783  int article_reply(const SECTION_LIST *p_ Line 903  int article_reply(const SECTION_LIST *p_
903                  {                  {
904                          switch (toupper(ch))                          switch (toupper(ch))
905                          {                          {
906                            case KEY_NULL:
907                            case KEY_TIMEOUT:
908                                    goto cleanup;
909                          case CR:                          case CR:
910                                  igetch_reset();                                  igetch_reset();
911                                  break;                                  break;
# Line 840  int article_reply(const SECTION_LIST *p_ Line 963  int article_reply(const SECTION_LIST *p_
963                          {                          {
964                                  switch (toupper(ch))                                  switch (toupper(ch))
965                                  {                                  {
966                                    case KEY_NULL:
967                                    case KEY_TIMEOUT:
968                                            goto cleanup;
969                                  case CR:                                  case CR:
970                                          igetch_reset();                                          igetch_reset();
971                                  case 'S':                                  case 'S':
# Line 868  int article_reply(const SECTION_LIST *p_ Line 994  int article_reply(const SECTION_LIST *p_
994                  }                  }
995          }          }
996    
997            if (SYS_server_exit) // Do not save data on shutdown
998            {
999                    goto cleanup;
1000            }
1001    
1002          content = malloc(ARTICLE_CONTENT_MAX_LEN);          content = malloc(ARTICLE_CONTENT_MAX_LEN);
1003          if (content == NULL)          if (content == NULL)
1004          {          {
# Line 880  int article_reply(const SECTION_LIST *p_ Line 1011  int article_reply(const SECTION_LIST *p_
1011          if (len_content < 0)          if (len_content < 0)
1012          {          {
1013                  log_error("editor_data_save() error\n");                  log_error("editor_data_save() error\n");
1014                  ret = -2;                  ret = -1;
1015                  goto cleanup;                  goto cleanup;
1016          }          }
1017    
# Line 921  int article_reply(const SECTION_LIST *p_ Line 1052  int article_reply(const SECTION_LIST *p_
1052                  rs = NULL;                  rs = NULL;
1053          }          }
1054    
         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;  
         }  
   
1055          // Begin transaction          // Begin transaction
1056          if (mysql_query(db, "SET autocommit=0") != 0)          if (mysql_query(db, "SET autocommit=0") != 0)
1057          {          {
# Line 953  int article_reply(const SECTION_LIST *p_ Line 1068  int article_reply(const SECTION_LIST *p_
1068          }          }
1069    
1070          // Secure SQL parameters          // Secure SQL parameters
1071            content_f = malloc((size_t)len_content * 2 + 1);
1072            if (content_f == NULL)
1073            {
1074                    log_error("malloc(content_f) error: OOM\n");
1075                    ret = -1;
1076                    goto cleanup;
1077            }
1078    
1079          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)));
1080          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)));
1081          mysql_real_escape_string(db, content_f, content, (unsigned long)len_content);          mysql_real_escape_string(db, content_f, content, (unsigned long)len_content);
1082    
1083            free(content);
1084            content = NULL;
1085    
1086          // Add content          // Add content
1087          snprintf(sql, SQL_BUFFER_LEN + (size_t)len_content * 2 + 1,          sql_content = malloc(SQL_BUFFER_LEN + (size_t)len_content * 2 + 1);
1088            if (sql_content == NULL)
1089            {
1090                    log_error("malloc(sql_content) error: OOM\n");
1091                    ret = -1;
1092                    goto cleanup;
1093            }
1094    
1095            snprintf(sql_content, SQL_BUFFER_LEN + (size_t)len_content * 2 + 1,
1096                           "INSERT INTO bbs_content(AID, content) values(0, '%s')",                           "INSERT INTO bbs_content(AID, content) values(0, '%s')",
1097                           content_f);                           content_f);
1098    
1099          if (mysql_query(db, sql) != 0)          free(content_f);
1100            content_f = NULL;
1101    
1102            if (mysql_query(db, sql_content) != 0)
1103          {          {
1104                  log_error("Add article content error: %s\n", mysql_error(db));                  log_error("Add article content error: %s\n", mysql_error(db));
1105                  ret = -1;                  ret = -1;
# Line 971  int article_reply(const SECTION_LIST *p_ Line 1108  int article_reply(const SECTION_LIST *p_
1108    
1109          p_article_new->cid = (int32_t)mysql_insert_id(db);          p_article_new->cid = (int32_t)mysql_insert_id(db);
1110    
1111            free(sql_content);
1112            sql_content = NULL;
1113    
1114          // Add article          // Add article
1115          snprintf(sql, sizeof(sql),          snprintf(sql, sizeof(sql),
1116                           "INSERT INTO bbs(SID, TID, UID, username, nickname, title, CID, transship, "                           "INSERT INTO bbs(SID, TID, UID, username, nickname, title, CID, transship, "
1117                           "sub_dt, sub_ip, reply_note, exp, last_reply_dt, icon, length) "                           "sub_dt, sub_ip, reply_note, exp, last_reply_dt, icon, length) "
1118                           "VALUES(%d, 0, %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)",
1119                           p_section->sid, BBS_priv.uid, BBS_username, nickname_f, title_f,                           p_section->sid, (p_article->tid == 0 ? p_article->aid : p_article->tid),
1120                             BBS_priv.uid, BBS_username, nickname_f, title_f,
1121                           p_article_new->cid, hostaddr_client, BBS_user_exp, len_content);                           p_article_new->cid, hostaddr_client, BBS_user_exp, len_content);
1122    
1123          if (mysql_query(db, sql) != 0)          if (mysql_query(db, sql) != 0)
# Line 992  int article_reply(const SECTION_LIST *p_ Line 1133  int article_reply(const SECTION_LIST *p_
1133          snprintf(sql, sizeof(sql),          snprintf(sql, sizeof(sql),
1134                           "UPDATE bbs SET reply_count = reply_count + 1, "                           "UPDATE bbs SET reply_count = reply_count + 1, "
1135                           "last_reply_dt = NOW(), last_reply_UID=%d, last_reply_username = '%s', "                           "last_reply_dt = NOW(), last_reply_UID=%d, last_reply_username = '%s', "
1136                           "last_reply_nickname = '%s' WHERE Aid = %d",                           "last_reply_nickname = '%s' WHERE AID = %d",
1137                           BBS_priv.uid, BBS_username, nickname_f, p_article->aid);                           BBS_priv.uid, BBS_username, nickname_f,
1138                             (p_article->tid == 0 ? p_article->aid : p_article->tid));
1139    
1140            if (mysql_query(db, sql) != 0)
1141            {
1142                    log_error("Update topic article error: %s\n", mysql_error(db));
1143                    ret = -1;
1144                    goto cleanup;
1145            }
1146    
1147          // Link content to article          // Link content to article
1148          snprintf(sql, sizeof(sql),          snprintf(sql, sizeof(sql),
# Line 1043  int article_reply(const SECTION_LIST *p_ Line 1192  int article_reply(const SECTION_LIST *p_
1192                  goto cleanup;                  goto cleanup;
1193          }          }
1194    
1195            mysql_close(db);
1196            db = NULL;
1197    
1198          clearscr();          clearscr();
1199          moveto(1, 1);          moveto(1, 1);
1200          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