/[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.6 by sysadm, Sat Jun 14 01:40:52 2025 UTC Revision 1.17 by sysadm, Sun Jun 15 04:46:19 2025 UTC
# Line 14  Line 14 
14   *                                                                         *   *                                                                         *
15   ***************************************************************************/   ***************************************************************************/
16    
17    #define _POSIX_C_SOURCE 200809L
18    
19  #include "article_post.h"  #include "article_post.h"
20  #include "article_cache.h"  #include "article_cache.h"
21  #include "editor.h"  #include "editor.h"
22  #include "screen.h"  #include "screen.h"
23    #include "bbs.h"
24  #include "log.h"  #include "log.h"
25  #include "io.h"  #include "io.h"
26  #include "lml.h"  #include "lml.h"
27  #include "database.h"  #include "database.h"
28    #include "user_priv.h"
29  #include <ctype.h>  #include <ctype.h>
30  #include <string.h>  #include <string.h>
31  #include <stdlib.h>  #include <stdlib.h>
32    #include <time.h>
33    
34  #define TITLE_INPUT_MAX_LEN 74  #define TITLE_INPUT_MAX_LEN 74
35  #define ARTICLE_CONTENT_MAX_LEN 1024 * 1024 * 4 // 4MB  #define ARTICLE_CONTENT_MAX_LEN 1024 * 1024 * 4 // 4MB
36  #define ARTICLE_QUOTE_MAX_LINES 20  #define ARTICLE_QUOTE_MAX_LINES 20
37  #define ARTICLE_QUOTE_LINE_MAX_LEN 76  #define ARTICLE_QUOTE_LINE_MAX_LEN 76
38    
39  int article_post(SECTION_LIST *p_section)  #define MODIFY_DT_MAX_LEN 50
40    
41    int article_post(const SECTION_LIST *p_section, ARTICLE *p_article_new)
42  {  {
43            MYSQL *db = NULL;
44            MYSQL_RES *rs = NULL;
45            MYSQL_ROW row;
46            char sql[SQL_BUFFER_LEN];
47            char *sql_content = NULL;
48          EDITOR_DATA *p_editor_data = NULL;          EDITOR_DATA *p_editor_data = NULL;
         char title[BBS_article_title_max_len + 1];  
49          char title_input[TITLE_INPUT_MAX_LEN + 1];          char title_input[TITLE_INPUT_MAX_LEN + 1];
50            char title_f[BBS_article_title_max_len * 2 + 1];
51            char *content = NULL;
52            char *content_f = NULL;
53            long len_content;
54            char nickname_f[BBS_nickname_max_len * 2 + 1];
55          int sign_id = 0;          int sign_id = 0;
56          long len;          long len;
57          int ch;          int ch;
58          char *p, *q;          char *p, *q;
59            long ret = 0;
60    
61          if (p_section == NULL)          if (p_section == NULL || p_article_new == NULL)
62          {          {
63                  log_error("NULL pointer error\n");                  log_error("NULL pointer error\n");
64          }          }
65    
66          title[0] = '\0';          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';
77          title_input[0] = '\0';          title_input[0] = '\0';
78            p_article_new->transship = 0;
79    
80          p_editor_data = editor_data_load("");          p_editor_data = editor_data_load("");
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 61  int article_post(SECTION_LIST *p_section Line 90  int article_post(SECTION_LIST *p_section
90          {          {
91                  clearscr();                  clearscr();
92                  moveto(21, 1);                  moveto(21, 1);
93                  prints("发表文章于 %s[%s] 讨论区", p_section->stitle, p_section->sname);                  prints("发表文章于 %s[%s] 讨论区,类型: %s", p_section->stitle, p_section->sname, (p_article_new->transship ? "转载" : "原创"));
94                  moveto(22, 1);                  moveto(22, 1);
95                  prints("标题: %s", (title[0] == '\0' ? "[无]" : title));                  prints("标题: %s", (p_article_new->title[0] == '\0' ? "[无]" : p_article_new->title));
96                  moveto(23, 1);                  moveto(23, 1);
97                  prints("使用第 %d 个签名", sign_id);                  prints("使用第 %d 个签名", sign_id);
98    
# Line 72  int article_post(SECTION_LIST *p_section Line 101  int article_post(SECTION_LIST *p_section
101                          prints("    按0~3选签名档(0表示不使用)");                          prints("    按0~3选签名档(0表示不使用)");
102    
103                          moveto(24, 1);                          moveto(24, 1);
104                          prints("T改标题, C取消, Enter继续: ");                          prints("T改标题, C取消, Z设为转载, Y设为原创, Enter继续: ");
105                          iflush();                          iflush();
106                          ch = 0;                          ch = 0;
107                  }                  }
# Line 96  int article_post(SECTION_LIST *p_section Line 125  int article_post(SECTION_LIST *p_section
125                                  len = q - p;                                  len = q - p;
126                                  if (*p != '\0')                                  if (*p != '\0')
127                                  {                                  {
128                                          memcpy(title, p, (size_t)len + 1);                                          memcpy(p_article_new->title, p, (size_t)len + 1);
129                                          memcpy(title_input, title, (size_t)len + 1);                                          memcpy(title_input, p_article_new->title, (size_t)len + 1);
130                                  }                                  }
131                                  ch = 0;                                  ch = 0;
132                                  break;                                  break;
# Line 107  int article_post(SECTION_LIST *p_section Line 136  int article_post(SECTION_LIST *p_section
136                                  prints("取消...");                                  prints("取消...");
137                                  press_any_key();                                  press_any_key();
138                                  goto cleanup;                                  goto cleanup;
139                            case 'Y':
140                                    p_article_new->transship = 0;
141                                    break;
142                            case 'Z':
143                                    p_article_new->transship = 1;
144                                    break;
145                          case '0':                          case '0':
146                          case '1':                          case '1':
147                          case '2':                          case '2':
# Line 120  int article_post(SECTION_LIST *p_section Line 155  int article_post(SECTION_LIST *p_section
155                          break;                          break;
156                  }                  }
157    
158                  if (ch != CR || title[0] == '\0')                  if (ch != CR || p_article_new->title[0] == '\0')
159                  {                  {
160                          continue;                          continue;
161                  }                  }
# Line 166  int article_post(SECTION_LIST *p_section Line 201  int article_post(SECTION_LIST *p_section
201                  }                  }
202          }          }
203    
204          // editor_data_save(p_editor_data, p_data_new, data_new_len);          content = malloc(ARTICLE_CONTENT_MAX_LEN);
205          log_common("Debug: post article\n");          if (content == NULL)
206            {
207                    log_error("malloc(content) error: OOM\n");
208                    ret = -1;
209                    goto cleanup;
210            }
211    
212            len_content = editor_data_save(p_editor_data, content, ARTICLE_CONTENT_MAX_LEN);
213            if (len_content < 0)
214            {
215                    log_error("editor_data_save() error\n");
216                    ret = -1;
217                    goto cleanup;
218            }
219    
220            db = db_open();
221            if (db == NULL)
222            {
223                    log_error("db_open() error: %s\n", mysql_error(db));
224                    ret = -1;
225                    goto cleanup;
226            }
227    
228            if (sign_id > 0)
229            {
230                    snprintf(sql, sizeof(sql),
231                                     "SELECT sign_%d AS sign FROM user_pubinfo WHERE UID = %d",
232                                     sign_id, BBS_priv.uid);
233    
234                    if (mysql_query(db, sql) != 0)
235                    {
236                            log_error("Query sign error: %s\n", mysql_error(db));
237                            ret = -1;
238                            goto cleanup;
239                    }
240                    if ((rs = mysql_use_result(db)) == NULL)
241                    {
242                            log_error("Get sign data failed\n");
243                            ret = -1;
244                            goto cleanup;
245                    }
246    
247                    if ((row = mysql_fetch_row(rs)))
248                    {
249                            len_content += snprintf(content + len_content,
250                                                                            ARTICLE_CONTENT_MAX_LEN - (size_t)len_content,
251                                                                            "\n\n--\n%s\n", row[0]);
252                    }
253                    mysql_free_result(rs);
254                    rs = NULL;
255            }
256    
257            // Begin transaction
258            if (mysql_query(db, "SET autocommit=0") != 0)
259            {
260                    log_error("SET autocommit=0 error: %s\n", mysql_error(db));
261                    ret = -1;
262                    goto cleanup;
263            }
264    
265            if (mysql_query(db, "BEGIN") != 0)
266            {
267                    log_error("Begin transaction error: %s\n", mysql_error(db));
268                    ret = -1;
269                    goto cleanup;
270            }
271    
272            // 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)));
282            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);
284    
285            free(content);
286            content = NULL;
287    
288            // Add content
289            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')",
299                             content_f);
300    
301            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));
307                    ret = -1;
308                    goto cleanup;
309            }
310    
311            p_article_new->cid = (int32_t)mysql_insert_id(db);
312    
313            free(sql_content);
314            sql_content = NULL;
315    
316            // Add article
317            snprintf(sql, sizeof(sql),
318                             "INSERT INTO bbs(SID, TID, UID, username, nickname, title, CID, transship, "
319                             "sub_dt, sub_ip, reply_note, exp, last_reply_dt, icon, length) "
320                             "VALUES(%d, 0, %d, '%s', '%s', '%s', %d, %d, NOW(), '%s', 1, %d, NOW(), 1, %ld)",
321                             p_section->sid, BBS_priv.uid, BBS_username, nickname_f, title_f,
322                             p_article_new->cid, p_article_new->transship, hostaddr_client, BBS_user_exp, len_content);
323    
324            if (mysql_query(db, sql) != 0)
325            {
326                    log_error("Add article error: %s\n", mysql_error(db));
327                    ret = -1;
328                    goto cleanup;
329            }
330    
331            p_article_new->aid = (int32_t)mysql_insert_id(db);
332    
333            // Link content to article
334            snprintf(sql, sizeof(sql),
335                             "UPDATE bbs_content SET AID = %d WHERE CID = %d",
336                             p_article_new->aid, p_article_new->cid);
337    
338            if (mysql_query(db, sql) != 0)
339            {
340                    log_error("Update content error: %s\n", mysql_error(db));
341                    ret = -1;
342                    goto cleanup;
343            }
344    
345            // Add exp
346            if (checkpriv(&BBS_priv, p_section->sid, S_GETEXP)) // Except in test section
347            {
348                    snprintf(sql, sizeof(sql),
349                                     "UPDATE user_pubinfo SET exp = exp + %d WHERE UID = %d",
350                                     (p_article_new->transship ? 5 : 15), BBS_priv.uid);
351    
352                    if (mysql_query(db, sql) != 0)
353                    {
354                            log_error("Update exp error: %s\n", mysql_error(db));
355                            ret = -1;
356                            goto cleanup;
357                    }
358            }
359    
360            // Add log
361            snprintf(sql, sizeof(sql),
362                             "INSERT INTO bbs_article_op(AID, UID, type, op_dt, op_ip)"
363                             "VALUES(%d, %d, 'A', NOW(), '%s')",
364                             p_article_new->aid, BBS_priv.uid, hostaddr_client);
365    
366            if (mysql_query(db, sql) != 0)
367            {
368                    log_error("Add log error: %s\n", mysql_error(db));
369                    ret = -1;
370                    goto cleanup;
371            }
372    
373            // Commit transaction
374            if (mysql_query(db, "COMMIT") != 0)
375            {
376                    log_error("Commit transaction error: %s\n", mysql_error(db));
377                    ret = -1;
378                    goto cleanup;
379            }
380    
381            mysql_close(db);
382            db = NULL;
383    
384            clearscr();
385            moveto(1, 1);
386            prints("发送完成,新文章通常会在%d秒后可见", BBS_section_list_load_interval);
387            press_any_key();
388            ret = 1; // Success
389    
390  cleanup:  cleanup:
391            mysql_close(db);
392    
393            // Cleanup buffers
394          editor_data_cleanup(p_editor_data);          editor_data_cleanup(p_editor_data);
395    
396          return 0;          free(sql_content);
397            free(content);
398            free(content_f);
399    
400            return (int)ret;
401  }  }
402    
403  int article_modify(SECTION_LIST *p_section, ARTICLE *p_article)  int article_modify(const SECTION_LIST *p_section, const ARTICLE *p_article, ARTICLE *p_article_new)
404  {  {
405          MYSQL *db;          MYSQL *db = NULL;
406          MYSQL_RES *rs;          MYSQL_RES *rs = NULL;
407          MYSQL_ROW row;          MYSQL_ROW row;
408          char sql[SQL_BUFFER_LEN];          char sql[SQL_BUFFER_LEN];
409            char *sql_content = NULL;
410            char *content = NULL;
411            char *content_f = NULL;
412            long len_content;
413          int ch;          int ch;
414            long ret = 0;
415            time_t now;
416            struct tm tm_modify_dt;
417            char str_modify_dt[MODIFY_DT_MAX_LEN + 1];
418    
419          EDITOR_DATA *p_editor_data = NULL;          EDITOR_DATA *p_editor_data = NULL;
420    
# Line 190  int article_modify(SECTION_LIST *p_secti Line 423  int article_modify(SECTION_LIST *p_secti
423                  log_error("NULL pointer error\n");                  log_error("NULL pointer error\n");
424          }          }
425    
426            if (p_article->excerption) // Modify is not allowed
427            {
428                    clearscr();
429                    moveto(1, 1);
430                    prints("该文章无法被编辑,请联系版主。");
431                    press_any_key();
432    
433                    return 0;
434            }
435    
436          db = db_open();          db = db_open();
437          if (db == NULL)          if (db == NULL)
438          {          {
439                  log_error("db_open() error: %s\n", mysql_error(db));                  log_error("db_open() error: %s\n", mysql_error(db));
440                  return -1;                  ret = -1;
441                    goto cleanup;
442          }          }
443    
444          snprintf(sql, sizeof(sql),          snprintf(sql, sizeof(sql),
# Line 206  int article_modify(SECTION_LIST *p_secti Line 450  int article_modify(SECTION_LIST *p_secti
450          if (mysql_query(db, sql) != 0)          if (mysql_query(db, sql) != 0)
451          {          {
452                  log_error("Query article content error: %s\n", mysql_error(db));                  log_error("Query article content error: %s\n", mysql_error(db));
453                  return -2;                  ret = -1;
454                    goto cleanup;
455          }          }
456          if ((rs = mysql_use_result(db)) == NULL)          if ((rs = mysql_use_result(db)) == NULL)
457          {          {
458                  log_error("Get article content data failed\n");                  log_error("Get article content data failed\n");
459                  return -2;                  ret = -1;
460                    goto cleanup;
461          }          }
462    
463          if ((row = mysql_fetch_row(rs)))          if ((row = mysql_fetch_row(rs)))
464          {          {
465                  p_editor_data = editor_data_load(row[1]);                  content = malloc(ARTICLE_CONTENT_MAX_LEN);
466                    if (content == NULL)
467                    {
468                            log_error("malloc(content) error: OOM\n");
469                            ret = -1;
470                            goto cleanup;
471                    }
472    
473                    strncpy(content, row[1], ARTICLE_CONTENT_MAX_LEN - 1);
474                    content[ARTICLE_CONTENT_MAX_LEN - 1] = '\0';
475    
476                    // Remove control sequence
477                    len_content = ctrl_seq_filter(content);
478    
479                    p_editor_data = editor_data_load(content);
480                  if (p_editor_data == NULL)                  if (p_editor_data == NULL)
481                  {                  {
482                          log_error("editor_data_load(aid=%d, cid=%d) error\n", p_article->aid, atoi(row[0]));                          log_error("editor_data_load(aid=%d, cid=%d) error\n", p_article->aid, atoi(row[0]));
483                          mysql_free_result(rs);                          ret = -1;
484                          mysql_close(db);                          goto cleanup;
                         return -3;  
485                  }                  }
486    
487                    free(content);
488                    content = NULL;
489          }          }
490          mysql_free_result(rs);          mysql_free_result(rs);
491            rs = NULL;
492    
493          mysql_close(db);          mysql_close(db);
494            db = NULL;
495    
496          for (ch = 'E'; !SYS_server_exit && toupper(ch) == 'E';)          for (ch = 'E'; !SYS_server_exit && toupper(ch) == 'E';)
497          {          {
# Line 261  int article_modify(SECTION_LIST *p_secti Line 526  int article_modify(SECTION_LIST *p_secti
526                  }                  }
527          }          }
528    
529          // editor_data_save(p_editor_data, p_data_new, data_new_len);          // Allocate buffers in big size
530          log_common("Debug: modify article\n");          content = malloc(ARTICLE_CONTENT_MAX_LEN);
531            if (content == NULL)
532            {
533                    log_error("malloc(content) error: OOM\n");
534                    ret = -1;
535                    goto cleanup;
536            }
537    
538            len_content = editor_data_save(p_editor_data, content, ARTICLE_CONTENT_MAX_LEN - LINE_BUFFER_LEN);
539            if (len_content < 0)
540            {
541                    log_error("editor_data_save() error\n");
542                    ret = -1;
543                    goto cleanup;
544            }
545    
546            time(&now);
547            localtime_r(&now, &tm_modify_dt);
548            strftime(str_modify_dt, sizeof(str_modify_dt), "%Y-%m-%d %H:%M:%S (UTC %z)", &tm_modify_dt);
549    
550            len_content += snprintf(content + len_content, LINE_BUFFER_LEN,
551                                                            "\n--\n※ 作者已于 %s 修改本文※\n",
552                                                            str_modify_dt);
553    
554            db = db_open();
555            if (db == NULL)
556            {
557                    log_error("db_open() error: %s\n", mysql_error(db));
558                    ret = -1;
559                    goto cleanup;
560            }
561    
562            // Begin transaction
563            if (mysql_query(db, "SET autocommit=0") != 0)
564            {
565                    log_error("SET autocommit=0 error: %s\n", mysql_error(db));
566                    ret = -1;
567                    goto cleanup;
568            }
569    
570            if (mysql_query(db, "BEGIN") != 0)
571            {
572                    log_error("Begin transaction error: %s\n", mysql_error(db));
573                    ret = -1;
574                    goto cleanup;
575            }
576    
577            // Secure SQL parameters
578            content_f = malloc((size_t)len_content * 2 + 1);
579            if (content_f == NULL)
580            {
581                    log_error("malloc(content_f) error: OOM\n");
582                    ret = -1;
583                    goto cleanup;
584            }
585    
586            mysql_real_escape_string(db, content_f, content, (unsigned long)len_content);
587    
588            free(content);
589            content = NULL;
590    
591            // Add content
592            sql_content = malloc(SQL_BUFFER_LEN + (size_t)len_content * 2 + 1);
593            if (sql_content == NULL)
594            {
595                    log_error("malloc(sql_content) error: OOM\n");
596                    ret = -1;
597                    goto cleanup;
598            }
599    
600            snprintf(sql_content, SQL_BUFFER_LEN + (size_t)len_content * 2 + 1,
601                             "INSERT INTO bbs_content(AID, content) values(%d, '%s')",
602                             p_article->aid, content_f);
603    
604            free(content_f);
605            content_f = NULL;
606    
607            if (mysql_query(db, sql_content) != 0)
608            {
609                    log_error("Add article content error: %s\n", mysql_error(db));
610                    ret = -1;
611                    goto cleanup;
612            }
613    
614            p_article_new->cid = (int32_t)mysql_insert_id(db);
615    
616            free(sql_content);
617            sql_content = NULL;
618    
619            // Update article
620            snprintf(sql, sizeof(sql),
621                             "UPDATE bbs SET CID = %d, length = %ld, excerption = 0 WHERE AID = %d", // Set excerption = 0 explictly in case of rare condition
622                             p_article_new->cid, len_content, p_article->aid);
623    
624            if (mysql_query(db, sql) != 0)
625            {
626                    log_error("Add article error: %s\n", mysql_error(db));
627                    ret = -1;
628                    goto cleanup;
629            }
630    
631            if (mysql_query(db, sql) != 0)
632            {
633                    log_error("Update content error: %s\n", mysql_error(db));
634                    ret = -1;
635                    goto cleanup;
636            }
637    
638            // Add log
639            snprintf(sql, sizeof(sql),
640                             "INSERT INTO bbs_article_op(AID, UID, type, op_dt, op_ip)"
641                             "VALUES(%d, %d, 'M', NOW(), '%s')",
642                             p_article->aid, BBS_priv.uid, hostaddr_client);
643    
644            if (mysql_query(db, sql) != 0)
645            {
646                    log_error("Add log error: %s\n", mysql_error(db));
647                    ret = -1;
648                    goto cleanup;
649            }
650    
651            // Commit transaction
652            if (mysql_query(db, "COMMIT") != 0)
653            {
654                    log_error("Commit transaction error: %s\n", mysql_error(db));
655                    ret = -1;
656                    goto cleanup;
657            }
658    
659            mysql_close(db);
660            db = NULL;
661    
662            clearscr();
663            moveto(1, 1);
664            prints("修改完成,新内容通常会在%d秒后可见", BBS_section_list_load_interval);
665            press_any_key();
666            ret = 1; // Success
667    
668  cleanup:  cleanup:
669            mysql_free_result(rs);
670            mysql_close(db);
671    
672            // Cleanup buffers
673          editor_data_cleanup(p_editor_data);          editor_data_cleanup(p_editor_data);
674    
675          return 0;          free(sql_content);
676            free(content);
677            free(content_f);
678    
679            return (int)ret;
680  }  }
681    
682  int article_reply(SECTION_LIST *p_section, ARTICLE *p_article)  int article_reply(const SECTION_LIST *p_section, const ARTICLE *p_article, ARTICLE *p_article_new)
683  {  {
684          MYSQL *db;          MYSQL *db = NULL;
685          MYSQL_RES *rs;          MYSQL_RES *rs = NULL;
686          MYSQL_ROW row;          MYSQL_ROW row;
         char sql[SQL_BUFFER_LEN];  
         char content[LINE_BUFFER_LEN * ARTICLE_QUOTE_MAX_LINES + 1];  
         char content_f[ARTICLE_CONTENT_MAX_LEN];  
687          long line_offsets[ARTICLE_QUOTE_MAX_LINES + 1];          long line_offsets[ARTICLE_QUOTE_MAX_LINES + 1];
688            char sql[SQL_BUFFER_LEN];
689            char *sql_content = NULL;
690          EDITOR_DATA *p_editor_data = NULL;          EDITOR_DATA *p_editor_data = NULL;
691          char title[BBS_article_title_max_len + 1];          char title_input[BBS_article_title_max_len + sizeof("Re: ")];
692          char title_input[BBS_article_title_max_len + 1 + 4]; // + "Re: "          char title_f[BBS_article_title_max_len * 2 + 1];
693            char *content = NULL;
694            char *content_f = NULL;
695            long len_content;
696            char nickname_f[BBS_nickname_max_len * 2 + 1];
697          int sign_id = 0;          int sign_id = 0;
698          long len;          long len;
699          int ch;          int ch;
# Line 290  int article_reply(SECTION_LIST *p_sectio Line 702  int article_reply(SECTION_LIST *p_sectio
702          int display_len;          int display_len;
703          long quote_content_lines;          long quote_content_lines;
704          long i;          long i;
705            long ret = 0;
706            int topic_locked = 0;
707    
708          if (p_section == NULL || p_article == NULL)          if (p_section == NULL || p_article == NULL)
709          {          {
710                  log_error("NULL pointer error\n");                  log_error("NULL pointer error\n");
711          }          }
712    
713          title[0] = '\0';          if (!checkpriv(&BBS_priv, p_section->sid, S_POST))
714            {
715                    clearscr();
716                    moveto(1, 1);
717                    prints("您没有权限在本版块发表文章\n");
718                    press_any_key();
719    
720                    return 0;
721            }
722    
723            p_article_new->title[0] = '\0';
724          snprintf(title_input, sizeof(title_input), "Re: %s", p_article->title);          snprintf(title_input, sizeof(title_input), "Re: %s", p_article->title);
725          len = split_line(title_input, TITLE_INPUT_MAX_LEN, &eol, &display_len);          len = split_line(title_input, TITLE_INPUT_MAX_LEN, &eol, &display_len);
726          title_input[len] = '\0';          title_input[len] = '\0';
# Line 305  int article_reply(SECTION_LIST *p_sectio Line 729  int article_reply(SECTION_LIST *p_sectio
729          if (db == NULL)          if (db == NULL)
730          {          {
731                  log_error("db_open() error: %s\n", mysql_error(db));                  log_error("db_open() error: %s\n", mysql_error(db));
732                  return -1;                  ret = -1;
733                    goto cleanup;
734            }
735    
736            snprintf(sql, sizeof(sql),
737                             "SELECT `lock` FROM bbs WHERE AID = %d",
738                             (p_article->tid == 0 ? p_article->aid : p_article->tid));
739    
740            if (mysql_query(db, sql) != 0)
741            {
742                    log_error("Query article status error: %s\n", mysql_error(db));
743                    ret = -1;
744                    goto cleanup;
745            }
746            if ((rs = mysql_store_result(db)) == NULL)
747            {
748                    log_error("Get article status data failed\n");
749                    ret = -1;
750                    goto cleanup;
751            }
752    
753            if ((row = mysql_fetch_row(rs)))
754            {
755                    if (atoi(row[0]) != 0)
756                    {
757                            topic_locked = 1;
758                    }
759            }
760            mysql_free_result(rs);
761            rs = NULL;
762    
763            if (topic_locked) // Reply is not allowed
764            {
765                    mysql_close(db);
766                    db = NULL;
767                    
768                    clearscr();
769                    moveto(1, 1);
770                    prints("该主题谢绝回复");
771                    press_any_key();
772    
773                    goto cleanup;
774          }          }
775    
776          snprintf(sql, sizeof(sql),          snprintf(sql, sizeof(sql),
# Line 317  int article_reply(SECTION_LIST *p_sectio Line 782  int article_reply(SECTION_LIST *p_sectio
782          if (mysql_query(db, sql) != 0)          if (mysql_query(db, sql) != 0)
783          {          {
784                  log_error("Query article content error: %s\n", mysql_error(db));                  log_error("Query article content error: %s\n", mysql_error(db));
785                  return -2;                  ret = -1;
786                    goto cleanup;
787          }          }
788          if ((rs = mysql_use_result(db)) == NULL)          if ((rs = mysql_use_result(db)) == NULL)
789          {          {
790                  log_error("Get article content data failed\n");                  log_error("Get article content data failed\n");
791                  return -2;                  ret = -1;
792                    goto cleanup;
793          }          }
794    
795          if ((row = mysql_fetch_row(rs)))          if ((row = mysql_fetch_row(rs)))
796          {          {
797                    content = malloc(ARTICLE_CONTENT_MAX_LEN);
798                    if (content == NULL)
799                    {
800                            log_error("malloc(content) error: OOM\n");
801                            ret = -1;
802                            goto cleanup;
803                    }
804    
805                    content_f = malloc(ARTICLE_CONTENT_MAX_LEN);
806                    if (content_f == NULL)
807                    {
808                            log_error("malloc(content_f) error: OOM\n");
809                            ret = -1;
810                            goto cleanup;
811                    }
812    
813                  // Apply LML render to content body                  // Apply LML render to content body
814                  len = lml_plain(row[1], content_f, ARTICLE_CONTENT_MAX_LEN);                  len = lml_plain(row[1], content_f, ARTICLE_CONTENT_MAX_LEN);
815                  content_f[len] = '\0';                  content_f[len] = '\0';
816    
817                    // Remove control sequence
818                    len = ctrl_seq_filter(content_f);
819    
820                    len = snprintf(content, ARTICLE_CONTENT_MAX_LEN,
821                                               "\n\n【 在 %s (%s) 的大作中提到: 】\n",
822                                               p_article->username, p_article->nickname);
823    
824                  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);
825                  for (i = 0, len = 0; i < quote_content_lines; i++)                  for (i = 0; i < quote_content_lines; i++)
826                  {                  {
827                          memcpy(content + len, ": ", 2); // quote line prefix                          memcpy(content + len, ": ", 2); // quote line prefix
828                          len += 2;                          len += 2;
829                          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]));
830                          len += (line_offsets[i + 1] - line_offsets[i]);                          len += (line_offsets[i + 1] - line_offsets[i]);
831                  }                  }
832                    if (content[len - 1] != '\n') // Appennd \n if not exist
833                    {
834                            content[len] = '\n';
835                            len++;
836                    }
837                  content[len] = '\0';                  content[len] = '\0';
838    
839                    free(content_f);
840                    content_f = NULL;
841    
842                    p_editor_data = editor_data_load(content);
843                    if (p_editor_data == NULL)
844                    {
845                            log_error("editor_data_load(aid=%d, cid=%d) error\n", p_article->aid, atoi(row[0]));
846                            ret = -1;
847                            goto cleanup;
848                    }
849    
850                    free(content);
851                    content = NULL;
852          }          }
853          mysql_free_result(rs);          mysql_free_result(rs);
854          mysql_close(db);          rs = NULL;
855    
856          p_editor_data = editor_data_load(content);          mysql_close(db);
857          if (p_editor_data == NULL)          db = NULL;
         {  
                 log_error("editor_data_load(aid=%d, cid=%d) error\n", p_article->aid, atoi(row[0]));  
                 return -3;  
         }  
858    
859          // Set title and sign          // Set title and sign
860          for (ch = 'T'; !SYS_server_exit;)          for (ch = 'T'; !SYS_server_exit;)
# Line 358  int article_reply(SECTION_LIST *p_sectio Line 863  int article_reply(SECTION_LIST *p_sectio
863                  moveto(21, 1);                  moveto(21, 1);
864                  prints("回复文章于 %s[%s] 讨论区", p_section->stitle, p_section->sname);                  prints("回复文章于 %s[%s] 讨论区", p_section->stitle, p_section->sname);
865                  moveto(22, 1);                  moveto(22, 1);
866                  prints("标题: %s", (title[0] == '\0' ? "[无]" : title));                  prints("标题: %s", (p_article_new->title[0] == '\0' ? "[无]" : p_article_new->title));
867                  moveto(23, 1);                  moveto(23, 1);
868                  prints("使用第 %d 个签名", sign_id);                  prints("使用第 %d 个签名", sign_id);
869    
# Line 391  int article_reply(SECTION_LIST *p_sectio Line 896  int article_reply(SECTION_LIST *p_sectio
896                                  len = q - p;                                  len = q - p;
897                                  if (*p != '\0')                                  if (*p != '\0')
898                                  {                                  {
899                                          memcpy(title, p, (size_t)len + 1);                                          memcpy(p_article_new->title, p, (size_t)len + 1);
900                                          memcpy(title_input, title, (size_t)len + 1);                                          memcpy(title_input, p_article_new->title, (size_t)len + 1);
901                                  }                                  }
902                                  ch = 0;                                  ch = 0;
903                                  break;                                  break;
# Line 415  int article_reply(SECTION_LIST *p_sectio Line 920  int article_reply(SECTION_LIST *p_sectio
920                          break;                          break;
921                  }                  }
922    
923                  if (ch != CR || title[0] == '\0')                  if (ch != CR || p_article_new->title[0] == '\0')
924                  {                  {
925                          continue;                          continue;
926                  }                  }
# Line 461  int article_reply(SECTION_LIST *p_sectio Line 966  int article_reply(SECTION_LIST *p_sectio
966                  }                  }
967          }          }
968    
969          // editor_data_save(p_editor_data, p_data_new, data_new_len);          content = malloc(ARTICLE_CONTENT_MAX_LEN);
970          log_common("Debug: reply article\n");          if (content == NULL)
971            {
972                    log_error("malloc(content) error: OOM\n");
973                    ret = -1;
974                    goto cleanup;
975            }
976    
977            len_content = editor_data_save(p_editor_data, content, ARTICLE_CONTENT_MAX_LEN);
978            if (len_content < 0)
979            {
980                    log_error("editor_data_save() error\n");
981                    ret = -1;
982                    goto cleanup;
983            }
984    
985            db = db_open();
986            if (db == NULL)
987            {
988                    log_error("db_open() error: %s\n", mysql_error(db));
989                    ret = -1;
990                    goto cleanup;
991            }
992    
993            if (sign_id > 0)
994            {
995                    snprintf(sql, sizeof(sql),
996                                     "SELECT sign_%d AS sign FROM user_pubinfo WHERE UID = %d",
997                                     sign_id, BBS_priv.uid);
998    
999                    if (mysql_query(db, sql) != 0)
1000                    {
1001                            log_error("Query sign error: %s\n", mysql_error(db));
1002                            ret = -1;
1003                            goto cleanup;
1004                    }
1005                    if ((rs = mysql_use_result(db)) == NULL)
1006                    {
1007                            log_error("Get sign data failed\n");
1008                            ret = -1;
1009                            goto cleanup;
1010                    }
1011    
1012                    if ((row = mysql_fetch_row(rs)))
1013                    {
1014                            len_content += snprintf(content + len_content,
1015                                                                            ARTICLE_CONTENT_MAX_LEN - (size_t)len_content,
1016                                                                            "\n\n--\n%s\n", row[0]);
1017                    }
1018                    mysql_free_result(rs);
1019                    rs = NULL;
1020            }
1021    
1022            // Begin transaction
1023            if (mysql_query(db, "SET autocommit=0") != 0)
1024            {
1025                    log_error("SET autocommit=0 error: %s\n", mysql_error(db));
1026                    ret = -1;
1027                    goto cleanup;
1028            }
1029    
1030            if (mysql_query(db, "BEGIN") != 0)
1031            {
1032                    log_error("Begin transaction error: %s\n", mysql_error(db));
1033                    ret = -1;
1034                    goto cleanup;
1035            }
1036    
1037            // Secure SQL parameters
1038            content_f = malloc((size_t)len_content * 2 + 1);
1039            if (content_f == NULL)
1040            {
1041                    log_error("malloc(content_f) error: OOM\n");
1042                    ret = -1;
1043                    goto cleanup;
1044            }
1045    
1046            mysql_real_escape_string(db, nickname_f, BBS_nickname, (unsigned long)strnlen(BBS_nickname, sizeof(BBS_nickname)));
1047            mysql_real_escape_string(db, title_f, p_article_new->title, strnlen(p_article_new->title, sizeof(p_article_new->title)));
1048            mysql_real_escape_string(db, content_f, content, (unsigned long)len_content);
1049    
1050            free(content);
1051            content = NULL;
1052    
1053            // Add content
1054            sql_content = malloc(SQL_BUFFER_LEN + (size_t)len_content * 2 + 1);
1055            if (sql_content == NULL)
1056            {
1057                    log_error("malloc(sql_content) error: OOM\n");
1058                    ret = -1;
1059                    goto cleanup;
1060            }
1061    
1062            snprintf(sql_content, SQL_BUFFER_LEN + (size_t)len_content * 2 + 1,
1063                             "INSERT INTO bbs_content(AID, content) values(0, '%s')",
1064                             content_f);
1065    
1066            free(content_f);
1067            content_f = NULL;
1068    
1069            if (mysql_query(db, sql_content) != 0)
1070            {
1071                    log_error("Add article content error: %s\n", mysql_error(db));
1072                    ret = -1;
1073                    goto cleanup;
1074            }
1075    
1076            p_article_new->cid = (int32_t)mysql_insert_id(db);
1077    
1078            free(sql_content);
1079            sql_content = NULL;
1080    
1081            // Add article
1082            snprintf(sql, sizeof(sql),
1083                             "INSERT INTO bbs(SID, TID, UID, username, nickname, title, CID, transship, "
1084                             "sub_dt, sub_ip, reply_note, exp, last_reply_dt, icon, length) "
1085                             "VALUES(%d, %d, %d, '%s', '%s', '%s', %d, 0, NOW(), '%s', 1, %d, NOW(), 1, %ld)",
1086                             p_section->sid, (p_article->tid == 0 ? p_article->aid : p_article->tid),
1087                             BBS_priv.uid, BBS_username, nickname_f, title_f,
1088                             p_article_new->cid, hostaddr_client, BBS_user_exp, len_content);
1089    
1090            if (mysql_query(db, sql) != 0)
1091            {
1092                    log_error("Add article error: %s\n", mysql_error(db));
1093                    ret = -1;
1094                    goto cleanup;
1095            }
1096    
1097            p_article_new->aid = (int32_t)mysql_insert_id(db);
1098    
1099            // Update topic article
1100            snprintf(sql, sizeof(sql),
1101                             "UPDATE bbs SET reply_count = reply_count + 1, "
1102                             "last_reply_dt = NOW(), last_reply_UID=%d, last_reply_username = '%s', "
1103                             "last_reply_nickname = '%s' WHERE AID = %d",
1104                             BBS_priv.uid, BBS_username, nickname_f,
1105                             (p_article->tid == 0 ? p_article->aid : p_article->tid));
1106    
1107            if (mysql_query(db, sql) != 0)
1108            {
1109                    log_error("Update topic article error: %s\n", mysql_error(db));
1110                    ret = -1;
1111                    goto cleanup;
1112            }
1113    
1114            // Link content to article
1115            snprintf(sql, sizeof(sql),
1116                             "UPDATE bbs_content SET AID = %d WHERE CID = %d",
1117                             p_article_new->aid, p_article_new->cid);
1118    
1119            if (mysql_query(db, sql) != 0)
1120            {
1121                    log_error("Update content error: %s\n", mysql_error(db));
1122                    ret = -1;
1123                    goto cleanup;
1124            }
1125    
1126            // Add exp
1127            if (checkpriv(&BBS_priv, p_section->sid, S_GETEXP)) // Except in test section
1128            {
1129                    snprintf(sql, sizeof(sql),
1130                                     "UPDATE user_pubinfo SET exp = exp + %d WHERE UID = %d",
1131                                     3, BBS_priv.uid);
1132    
1133                    if (mysql_query(db, sql) != 0)
1134                    {
1135                            log_error("Update exp error: %s\n", mysql_error(db));
1136                            ret = -1;
1137                            goto cleanup;
1138                    }
1139            }
1140    
1141            // Add log
1142            snprintf(sql, sizeof(sql),
1143                             "INSERT INTO bbs_article_op(AID, UID, type, op_dt, op_ip)"
1144                             "VALUES(%d, %d, 'A', NOW(), '%s')",
1145                             p_article_new->aid, BBS_priv.uid, hostaddr_client);
1146    
1147            if (mysql_query(db, sql) != 0)
1148            {
1149                    log_error("Add log error: %s\n", mysql_error(db));
1150                    ret = -1;
1151                    goto cleanup;
1152            }
1153    
1154            // Commit transaction
1155            if (mysql_query(db, "COMMIT") != 0)
1156            {
1157                    log_error("Commit transaction error: %s\n", mysql_error(db));
1158                    ret = -1;
1159                    goto cleanup;
1160            }
1161    
1162            mysql_close(db);
1163            db = NULL;
1164    
1165            clearscr();
1166            moveto(1, 1);
1167            prints("发送完成,新文章通常会在%d秒后可见", BBS_section_list_load_interval);
1168            press_any_key();
1169            ret = 1; // Success
1170    
1171  cleanup:  cleanup:
1172            mysql_free_result(rs);
1173            mysql_close(db);
1174    
1175            // Cleanup buffers
1176          editor_data_cleanup(p_editor_data);          editor_data_cleanup(p_editor_data);
1177    
1178          return 0;          free(sql_content);
1179            free(content);
1180            free(content_f);
1181    
1182            return (int)ret;
1183  }  }


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

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