/[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.8 by sysadm, Sat Jun 14 09:20:13 2025 UTC Revision 1.20 by sysadm, Mon Jun 16 01:36:56 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 99  int article_post(const SECTION_LIST *p_s Line 110  int article_post(const SECTION_LIST *p_s
110                  {                  {
111                          switch (toupper(ch))                          switch (toupper(ch))
112                          {                          {
113                            case KEY_NULL:
114                            case KEY_TIMEOUT:
115                                    goto cleanup;
116                          case CR:                          case CR:
117                                  igetch_reset();                                  igetch_reset();
118                                  break;                                  break;
# Line 162  int article_post(const SECTION_LIST *p_s Line 176  int article_post(const SECTION_LIST *p_s
176                          {                          {
177                                  switch (toupper(ch))                                  switch (toupper(ch))
178                                  {                                  {
179                                    case KEY_NULL:
180                                    case KEY_TIMEOUT:
181                                            goto cleanup;
182                                  case CR:                                  case CR:
183                                          igetch_reset();                                          igetch_reset();
184                                  case 'S':                                  case 'S':
# Line 190  int article_post(const SECTION_LIST *p_s Line 207  int article_post(const SECTION_LIST *p_s
207                  }                  }
208          }          }
209    
210            if (SYS_server_exit) // Do not save data on shutdown
211            {
212                    goto cleanup;
213            }
214    
215          content = malloc(ARTICLE_CONTENT_MAX_LEN);          content = malloc(ARTICLE_CONTENT_MAX_LEN);
216          if (content == NULL)          if (content == NULL)
217          {          {
# Line 202  int article_post(const SECTION_LIST *p_s Line 224  int article_post(const SECTION_LIST *p_s
224          if (len_content < 0)          if (len_content < 0)
225          {          {
226                  log_error("editor_data_save() error\n");                  log_error("editor_data_save() error\n");
227                  ret = -2;                  ret = -1;
228                  goto cleanup;                  goto cleanup;
229          }          }
230    
# Line 243  int article_post(const SECTION_LIST *p_s Line 265  int article_post(const SECTION_LIST *p_s
265                  rs = NULL;                  rs = NULL;
266          }          }
267    
         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;  
         }  
   
268          // Begin transaction          // Begin transaction
269          if (mysql_query(db, "SET autocommit=0") != 0)          if (mysql_query(db, "SET autocommit=0") != 0)
270          {          {
# Line 275  int article_post(const SECTION_LIST *p_s Line 281  int article_post(const SECTION_LIST *p_s
281          }          }
282    
283          // Secure SQL parameters          // Secure SQL parameters
284            content_f = malloc((size_t)len_content * 2 + 1);
285            if (content_f == NULL)
286            {
287                    log_error("malloc(content_f) error: OOM\n");
288                    ret = -1;
289                    goto cleanup;
290            }
291    
292          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)));
293          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)));
294          mysql_real_escape_string(db, content_f, content, (unsigned long)len_content);          mysql_real_escape_string(db, content_f, content, (unsigned long)len_content);
295    
296            free(content);
297            content = NULL;
298    
299          // Add content          // Add content
300          snprintf(sql, SQL_BUFFER_LEN + (size_t)len_content * 2 + 1,          sql_content = malloc(SQL_BUFFER_LEN + (size_t)len_content * 2 + 1);
301            if (sql_content == NULL)
302            {
303                    log_error("malloc(sql_content) error: OOM\n");
304                    ret = -1;
305                    goto cleanup;
306            }
307    
308            snprintf(sql_content, SQL_BUFFER_LEN + (size_t)len_content * 2 + 1,
309                           "INSERT INTO bbs_content(AID, content) values(0, '%s')",                           "INSERT INTO bbs_content(AID, content) values(0, '%s')",
310                           content_f);                           content_f);
311    
312          if (mysql_query(db, sql) != 0)          free(content_f);
313            content_f = NULL;
314    
315            if (mysql_query(db, sql_content) != 0)
316          {          {
317                  log_error("Add article content error: %s\n", mysql_error(db));                  log_error("Add article content error: %s\n", mysql_error(db));
318                  ret = -1;                  ret = -1;
# Line 293  int article_post(const SECTION_LIST *p_s Line 321  int article_post(const SECTION_LIST *p_s
321    
322          p_article_new->cid = (int32_t)mysql_insert_id(db);          p_article_new->cid = (int32_t)mysql_insert_id(db);
323    
324            free(sql_content);
325            sql_content = NULL;
326    
327          // Add article          // Add article
328          snprintf(sql, sizeof(sql),          snprintf(sql, sizeof(sql),
329                           "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 389  int article_post(const SECTION_LIST *p_s
389                  goto cleanup;                  goto cleanup;
390          }          }
391    
392            mysql_close(db);
393            db = NULL;
394    
395          clearscr();          clearscr();
396          moveto(1, 1);          moveto(1, 1);
397          prints("发送完成,新文章通常会在%d秒后可见", BBS_section_list_load_interval);          prints("发送完成,新文章通常会在%d秒后可见", BBS_section_list_load_interval);
# Line 414  int article_modify(const SECTION_LIST *p Line 448  int article_modify(const SECTION_LIST *p
448          if (db == NULL)          if (db == NULL)
449          {          {
450                  log_error("db_open() error: %s\n", mysql_error(db));                  log_error("db_open() error: %s\n", mysql_error(db));
451                  return -1;                  ret = -1;
452                    goto cleanup;
453          }          }
454    
455          snprintf(sql, sizeof(sql),          snprintf(sql, sizeof(sql),
# Line 426  int article_modify(const SECTION_LIST *p Line 461  int article_modify(const SECTION_LIST *p
461          if (mysql_query(db, sql) != 0)          if (mysql_query(db, sql) != 0)
462          {          {
463                  log_error("Query article content error: %s\n", mysql_error(db));                  log_error("Query article content error: %s\n", mysql_error(db));
464                  ret = -2;                  ret = -1;
465                  goto cleanup;                  goto cleanup;
466          }          }
467          if ((rs = mysql_use_result(db)) == NULL)          if ((rs = mysql_use_result(db)) == NULL)
468          {          {
469                  log_error("Get article content data failed\n");                  log_error("Get article content data failed\n");
470                  ret = -2;                  ret = -1;
471                  goto cleanup;                  goto cleanup;
472          }          }
473    
474          if ((row = mysql_fetch_row(rs)))          if ((row = mysql_fetch_row(rs)))
475          {          {
476                  p_editor_data = editor_data_load(row[1]);                  content = malloc(ARTICLE_CONTENT_MAX_LEN);
477                    if (content == NULL)
478                    {
479                            log_error("malloc(content) error: OOM\n");
480                            ret = -1;
481                            goto cleanup;
482                    }
483    
484                    strncpy(content, row[1], ARTICLE_CONTENT_MAX_LEN - 1);
485                    content[ARTICLE_CONTENT_MAX_LEN - 1] = '\0';
486    
487                    // Remove control sequence
488                    len_content = str_filter(content, 0);
489    
490                    p_editor_data = editor_data_load(content);
491                  if (p_editor_data == NULL)                  if (p_editor_data == NULL)
492                  {                  {
493                          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]));
494                          ret = -3;                          ret = -1;
495                          goto cleanup;                          goto cleanup;
496                  }                  }
497    
498                    free(content);
499                    content = NULL;
500          }          }
501          mysql_free_result(rs);          mysql_free_result(rs);
502          rs = NULL;          rs = NULL;
# Line 465  int article_modify(const SECTION_LIST *p Line 517  int article_modify(const SECTION_LIST *p
517                  {                  {
518                          switch (toupper(ch))                          switch (toupper(ch))
519                          {                          {
520                            case KEY_NULL:
521                            case KEY_TIMEOUT:
522                                    goto cleanup;
523                          case CR:                          case CR:
524                                  igetch_reset();                                  igetch_reset();
525                          case 'S':                          case 'S':
# Line 485  int article_modify(const SECTION_LIST *p Line 540  int article_modify(const SECTION_LIST *p
540                  }                  }
541          }          }
542    
543            if (SYS_server_exit) // Do not save data on shutdown
544            {
545                    goto cleanup;
546            }
547    
548          // Allocate buffers in big size          // Allocate buffers in big size
549          content = malloc(ARTICLE_CONTENT_MAX_LEN);          content = malloc(ARTICLE_CONTENT_MAX_LEN);
550          if (content == NULL)          if (content == NULL)
# Line 498  int article_modify(const SECTION_LIST *p Line 558  int article_modify(const SECTION_LIST *p
558          if (len_content < 0)          if (len_content < 0)
559          {          {
560                  log_error("editor_data_save() error\n");                  log_error("editor_data_save() error\n");
561                  ret = -2;                  ret = -1;
562                  goto cleanup;                  goto cleanup;
563          }          }
564    
# Line 510  int article_modify(const SECTION_LIST *p Line 570  int article_modify(const SECTION_LIST *p
570                                                          "\n--\n※ 作者已于 %s 修改本文※\n",                                                          "\n--\n※ 作者已于 %s 修改本文※\n",
571                                                          str_modify_dt);                                                          str_modify_dt);
572    
         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;  
         }  
   
573          db = db_open();          db = db_open();
574          if (db == NULL)          if (db == NULL)
575          {          {
# Line 550  int article_modify(const SECTION_LIST *p Line 594  int article_modify(const SECTION_LIST *p
594          }          }
595    
596          // Secure SQL parameters          // Secure SQL parameters
597            content_f = malloc((size_t)len_content * 2 + 1);
598            if (content_f == NULL)
599            {
600                    log_error("malloc(content_f) error: OOM\n");
601                    ret = -1;
602                    goto cleanup;
603            }
604    
605          mysql_real_escape_string(db, content_f, content, (unsigned long)len_content);          mysql_real_escape_string(db, content_f, content, (unsigned long)len_content);
606    
607            free(content);
608            content = NULL;
609    
610          // Add content          // Add content
611          snprintf(sql, SQL_BUFFER_LEN + (size_t)len_content * 2 + 1,          sql_content = malloc(SQL_BUFFER_LEN + (size_t)len_content * 2 + 1);
612            if (sql_content == NULL)
613            {
614                    log_error("malloc(sql_content) error: OOM\n");
615                    ret = -1;
616                    goto cleanup;
617            }
618    
619            snprintf(sql_content, SQL_BUFFER_LEN + (size_t)len_content * 2 + 1,
620                           "INSERT INTO bbs_content(AID, content) values(%d, '%s')",                           "INSERT INTO bbs_content(AID, content) values(%d, '%s')",
621                           p_article->aid, content_f);                           p_article->aid, content_f);
622    
623          if (mysql_query(db, sql) != 0)          free(content_f);
624            content_f = NULL;
625    
626            if (mysql_query(db, sql_content) != 0)
627          {          {
628                  log_error("Add article content error: %s\n", mysql_error(db));                  log_error("Add article content error: %s\n", mysql_error(db));
629                  ret = -1;                  ret = -1;
# Line 566  int article_modify(const SECTION_LIST *p Line 632  int article_modify(const SECTION_LIST *p
632    
633          p_article_new->cid = (int32_t)mysql_insert_id(db);          p_article_new->cid = (int32_t)mysql_insert_id(db);
634    
635            free(sql_content);
636            sql_content = NULL;
637    
638          // Update article          // Update article
639          snprintf(sql, sizeof(sql),          snprintf(sql, sizeof(sql),
640                           "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
641                           p_article_new->cid, len_content, p_article->aid);                           p_article_new->cid, len_content, p_article->aid);
642    
643          if (mysql_query(db, sql) != 0)          if (mysql_query(db, sql) != 0)
# Line 606  int article_modify(const SECTION_LIST *p Line 675  int article_modify(const SECTION_LIST *p
675                  goto cleanup;                  goto cleanup;
676          }          }
677    
678            mysql_close(db);
679            db = NULL;
680    
681          clearscr();          clearscr();
682          moveto(1, 1);          moveto(1, 1);
683          prints("修改完成,新内容通常会在%d秒后可见", BBS_section_list_load_interval);          prints("修改完成,新内容通常会在%d秒后可见", BBS_section_list_load_interval);
# Line 613  int article_modify(const SECTION_LIST *p Line 685  int article_modify(const SECTION_LIST *p
685          ret = 1; // Success          ret = 1; // Success
686    
687  cleanup:  cleanup:
688            mysql_free_result(rs);
689          mysql_close(db);          mysql_close(db);
690    
691          // Cleanup buffers          // Cleanup buffers
# Line 649  int article_reply(const SECTION_LIST *p_ Line 722  int article_reply(const SECTION_LIST *p_
722          long quote_content_lines;          long quote_content_lines;
723          long i;          long i;
724          long ret = 0;          long ret = 0;
725            int topic_locked = 0;
726    
727          if (p_section == NULL || p_article == NULL)          if (p_section == NULL || p_article == NULL)
728          {          {
729                  log_error("NULL pointer error\n");                  log_error("NULL pointer error\n");
730          }          }
731    
732          if (p_article->lock) // Reply is not allowed          if (!checkpriv(&BBS_priv, p_section->sid, S_POST))
733          {          {
734                  clearscr();                  clearscr();
735                  moveto(1, 1);                  moveto(1, 1);
736                  prints("该文章谢绝回复");                  prints("您没有权限在本版块发表文章\n");
737                  press_any_key();                  press_any_key();
738    
739                  return 0;                  return 0;
# Line 667  int article_reply(const SECTION_LIST *p_ Line 741  int article_reply(const SECTION_LIST *p_
741    
742          p_article_new->title[0] = '\0';          p_article_new->title[0] = '\0';
743          snprintf(title_input, sizeof(title_input), "Re: %s", p_article->title);          snprintf(title_input, sizeof(title_input), "Re: %s", p_article->title);
744          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);
745          title_input[len] = '\0';          title_input[len] = '\0';
746    
747          db = db_open();          db = db_open();
748          if (db == NULL)          if (db == NULL)
749          {          {
750                  log_error("db_open() error: %s\n", mysql_error(db));                  log_error("db_open() error: %s\n", mysql_error(db));
751                  return -1;                  ret = -1;
752                    goto cleanup;
753            }
754    
755            snprintf(sql, sizeof(sql),
756                             "SELECT `lock` FROM bbs WHERE AID = %d",
757                             (p_article->tid == 0 ? p_article->aid : p_article->tid));
758    
759            if (mysql_query(db, sql) != 0)
760            {
761                    log_error("Query article status error: %s\n", mysql_error(db));
762                    ret = -1;
763                    goto cleanup;
764            }
765            if ((rs = mysql_store_result(db)) == NULL)
766            {
767                    log_error("Get article status data failed\n");
768                    ret = -1;
769                    goto cleanup;
770            }
771    
772            if ((row = mysql_fetch_row(rs)))
773            {
774                    if (atoi(row[0]) != 0)
775                    {
776                            topic_locked = 1;
777                    }
778            }
779            mysql_free_result(rs);
780            rs = NULL;
781    
782            if (topic_locked) // Reply is not allowed
783            {
784                    mysql_close(db);
785                    db = NULL;
786    
787                    clearscr();
788                    moveto(1, 1);
789                    prints("该主题谢绝回复");
790                    press_any_key();
791    
792                    goto cleanup;
793          }          }
794    
795          snprintf(sql, sizeof(sql),          snprintf(sql, sizeof(sql),
# Line 686  int article_reply(const SECTION_LIST *p_ Line 801  int article_reply(const SECTION_LIST *p_
801          if (mysql_query(db, sql) != 0)          if (mysql_query(db, sql) != 0)
802          {          {
803                  log_error("Query article content error: %s\n", mysql_error(db));                  log_error("Query article content error: %s\n", mysql_error(db));
804                  return -2;                  ret = -1;
805                    goto cleanup;
806          }          }
807          if ((rs = mysql_use_result(db)) == NULL)          if ((rs = mysql_use_result(db)) == NULL)
808          {          {
809                  log_error("Get article content data failed\n");                  log_error("Get article content data failed\n");
810                  return -2;                  ret = -1;
811                    goto cleanup;
812          }          }
813    
814          if ((row = mysql_fetch_row(rs)))          if ((row = mysql_fetch_row(rs)))
# Line 717  int article_reply(const SECTION_LIST *p_ Line 834  int article_reply(const SECTION_LIST *p_
834                  content_f[len] = '\0';                  content_f[len] = '\0';
835    
836                  // Remove control sequence                  // Remove control sequence
837                  len = ctrl_seq_filter(content_f);                  len = str_filter(content_f, 0);
838    
839                  len = snprintf(content, ARTICLE_CONTENT_MAX_LEN,                  len = snprintf(content, ARTICLE_CONTENT_MAX_LEN,
840                                             "\n\n【 在 %s (%s) 的大作中提到: 】\n",                                             "\n\n【 在 %s (%s) 的大作中提到: 】\n",
841                                             p_article->username, p_article->nickname);                                             p_article->username, p_article->nickname);
842    
843                  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);
844                  for (i = 0; i < quote_content_lines; i++)                  for (i = 0; i < quote_content_lines; i++)
845                  {                  {
846                          memcpy(content + len, ": ", 2); // quote line prefix                          memcpy(content + len, ": ", 2); // quote line prefix
847                          len += 2;                          len += 2;
848                          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]));
849                          len += (line_offsets[i + 1] - line_offsets[i]);                          len += (line_offsets[i + 1] - line_offsets[i]);
850                            if (content[len - 1] != '\n') // Appennd \n if not exist
851                            {
852                                    content[len] = '\n';
853                                    len++;
854                            }
855                    }
856                    if (content[len - 1] != '\n') // Appennd \n if not exist
857                    {
858                            content[len] = '\n';
859                            len++;
860                  }                  }
861                  content[len] = '\0';                  content[len] = '\0';
862    
# Line 740  int article_reply(const SECTION_LIST *p_ Line 867  int article_reply(const SECTION_LIST *p_
867                  if (p_editor_data == NULL)                  if (p_editor_data == NULL)
868                  {                  {
869                          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]));
870                          ret = -3;                          ret = -1;
871                          goto cleanup;                          goto cleanup;
872                  }                  }
873    
# Line 778  int article_reply(const SECTION_LIST *p_ Line 905  int article_reply(const SECTION_LIST *p_
905                  {                  {
906                          switch (toupper(ch))                          switch (toupper(ch))
907                          {                          {
908                            case KEY_NULL:
909                            case KEY_TIMEOUT:
910                                    goto cleanup;
911                          case CR:                          case CR:
912                                  igetch_reset();                                  igetch_reset();
913                                  break;                                  break;
# Line 835  int article_reply(const SECTION_LIST *p_ Line 965  int article_reply(const SECTION_LIST *p_
965                          {                          {
966                                  switch (toupper(ch))                                  switch (toupper(ch))
967                                  {                                  {
968                                    case KEY_NULL:
969                                    case KEY_TIMEOUT:
970                                            goto cleanup;
971                                  case CR:                                  case CR:
972                                          igetch_reset();                                          igetch_reset();
973                                  case 'S':                                  case 'S':
# Line 863  int article_reply(const SECTION_LIST *p_ Line 996  int article_reply(const SECTION_LIST *p_
996                  }                  }
997          }          }
998    
999            if (SYS_server_exit) // Do not save data on shutdown
1000            {
1001                    goto cleanup;
1002            }
1003    
1004          content = malloc(ARTICLE_CONTENT_MAX_LEN);          content = malloc(ARTICLE_CONTENT_MAX_LEN);
1005          if (content == NULL)          if (content == NULL)
1006          {          {
# Line 875  int article_reply(const SECTION_LIST *p_ Line 1013  int article_reply(const SECTION_LIST *p_
1013          if (len_content < 0)          if (len_content < 0)
1014          {          {
1015                  log_error("editor_data_save() error\n");                  log_error("editor_data_save() error\n");
1016                  ret = -2;                  ret = -1;
1017                  goto cleanup;                  goto cleanup;
1018          }          }
1019    
# Line 916  int article_reply(const SECTION_LIST *p_ Line 1054  int article_reply(const SECTION_LIST *p_
1054                  rs = NULL;                  rs = NULL;
1055          }          }
1056    
         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;  
         }  
   
1057          // Begin transaction          // Begin transaction
1058          if (mysql_query(db, "SET autocommit=0") != 0)          if (mysql_query(db, "SET autocommit=0") != 0)
1059          {          {
# Line 948  int article_reply(const SECTION_LIST *p_ Line 1070  int article_reply(const SECTION_LIST *p_
1070          }          }
1071    
1072          // Secure SQL parameters          // Secure SQL parameters
1073            content_f = malloc((size_t)len_content * 2 + 1);
1074            if (content_f == NULL)
1075            {
1076                    log_error("malloc(content_f) error: OOM\n");
1077                    ret = -1;
1078                    goto cleanup;
1079            }
1080    
1081          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)));
1082          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)));
1083          mysql_real_escape_string(db, content_f, content, (unsigned long)len_content);          mysql_real_escape_string(db, content_f, content, (unsigned long)len_content);
1084    
1085            free(content);
1086            content = NULL;
1087    
1088          // Add content          // Add content
1089          snprintf(sql, SQL_BUFFER_LEN + (size_t)len_content * 2 + 1,          sql_content = malloc(SQL_BUFFER_LEN + (size_t)len_content * 2 + 1);
1090            if (sql_content == NULL)
1091            {
1092                    log_error("malloc(sql_content) error: OOM\n");
1093                    ret = -1;
1094                    goto cleanup;
1095            }
1096    
1097            snprintf(sql_content, SQL_BUFFER_LEN + (size_t)len_content * 2 + 1,
1098                           "INSERT INTO bbs_content(AID, content) values(0, '%s')",                           "INSERT INTO bbs_content(AID, content) values(0, '%s')",
1099                           content_f);                           content_f);
1100    
1101          if (mysql_query(db, sql) != 0)          free(content_f);
1102            content_f = NULL;
1103    
1104            if (mysql_query(db, sql_content) != 0)
1105          {          {
1106                  log_error("Add article content error: %s\n", mysql_error(db));                  log_error("Add article content error: %s\n", mysql_error(db));
1107                  ret = -1;                  ret = -1;
# Line 966  int article_reply(const SECTION_LIST *p_ Line 1110  int article_reply(const SECTION_LIST *p_
1110    
1111          p_article_new->cid = (int32_t)mysql_insert_id(db);          p_article_new->cid = (int32_t)mysql_insert_id(db);
1112    
1113            free(sql_content);
1114            sql_content = NULL;
1115    
1116          // Add article          // Add article
1117          snprintf(sql, sizeof(sql),          snprintf(sql, sizeof(sql),
1118                           "INSERT INTO bbs(SID, TID, UID, username, nickname, title, CID, transship, "                           "INSERT INTO bbs(SID, TID, UID, username, nickname, title, CID, transship, "
1119                           "sub_dt, sub_ip, reply_note, exp, last_reply_dt, icon, length) "                           "sub_dt, sub_ip, reply_note, exp, last_reply_dt, icon, length) "
1120                           "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)",
1121                           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),
1122                             BBS_priv.uid, BBS_username, nickname_f, title_f,
1123                           p_article_new->cid, hostaddr_client, BBS_user_exp, len_content);                           p_article_new->cid, hostaddr_client, BBS_user_exp, len_content);
1124    
1125          if (mysql_query(db, sql) != 0)          if (mysql_query(db, sql) != 0)
# Line 987  int article_reply(const SECTION_LIST *p_ Line 1135  int article_reply(const SECTION_LIST *p_
1135          snprintf(sql, sizeof(sql),          snprintf(sql, sizeof(sql),
1136                           "UPDATE bbs SET reply_count = reply_count + 1, "                           "UPDATE bbs SET reply_count = reply_count + 1, "
1137                           "last_reply_dt = NOW(), last_reply_UID=%d, last_reply_username = '%s', "                           "last_reply_dt = NOW(), last_reply_UID=%d, last_reply_username = '%s', "
1138                           "last_reply_nickname = '%s' WHERE Aid = %d",                           "last_reply_nickname = '%s' WHERE AID = %d",
1139                           BBS_priv.uid, BBS_username, nickname_f, p_article->aid);                           BBS_priv.uid, BBS_username, nickname_f,
1140                             (p_article->tid == 0 ? p_article->aid : p_article->tid));
1141    
1142            if (mysql_query(db, sql) != 0)
1143            {
1144                    log_error("Update topic article error: %s\n", mysql_error(db));
1145                    ret = -1;
1146                    goto cleanup;
1147            }
1148    
1149          // Link content to article          // Link content to article
1150          snprintf(sql, sizeof(sql),          snprintf(sql, sizeof(sql),
# Line 1038  int article_reply(const SECTION_LIST *p_ Line 1194  int article_reply(const SECTION_LIST *p_
1194                  goto cleanup;                  goto cleanup;
1195          }          }
1196    
1197            mysql_close(db);
1198            db = NULL;
1199    
1200          clearscr();          clearscr();
1201          moveto(1, 1);          moveto(1, 1);
1202          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