--- lbbs/src/article_post.c 2025/06/14 09:20:13 1.8 +++ lbbs/src/article_post.c 2025/06/15 02:24:45 1.15 @@ -63,6 +63,16 @@ int article_post(const SECTION_LIST *p_s log_error("NULL pointer error\n"); } + if (!checkpriv(&BBS_priv, p_section->sid, S_POST)) + { + clearscr(); + moveto(1, 1); + prints("您没有权限在本版块发表文章\n"); + press_any_key(); + + return 0; + } + p_article_new->title[0] = '\0'; title_input[0] = '\0'; p_article_new->transship = 0; @@ -243,22 +253,6 @@ int article_post(const SECTION_LIST *p_s rs = NULL; } - 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; - } - // Begin transaction if (mysql_query(db, "SET autocommit=0") != 0) { @@ -275,16 +269,38 @@ int article_post(const SECTION_LIST *p_s } // Secure SQL parameters + 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; + } + mysql_real_escape_string(db, nickname_f, BBS_nickname, (unsigned long)strnlen(BBS_nickname, sizeof(BBS_nickname))); 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, content_f, content, (unsigned long)len_content); + free(content); + content = NULL; + // Add content - snprintf(sql, SQL_BUFFER_LEN + (size_t)len_content * 2 + 1, + 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; + } + + snprintf(sql_content, SQL_BUFFER_LEN + (size_t)len_content * 2 + 1, "INSERT INTO bbs_content(AID, content) values(0, '%s')", content_f); - if (mysql_query(db, sql) != 0) + free(content_f); + content_f = NULL; + + if (mysql_query(db, sql_content) != 0) { log_error("Add article content error: %s\n", mysql_error(db)); ret = -1; @@ -293,6 +309,9 @@ int article_post(const SECTION_LIST *p_s p_article_new->cid = (int32_t)mysql_insert_id(db); + free(sql_content); + sql_content = NULL; + // Add article snprintf(sql, sizeof(sql), "INSERT INTO bbs(SID, TID, UID, username, nickname, title, CID, transship, " @@ -410,6 +429,16 @@ int article_modify(const SECTION_LIST *p return 0; } + if (!checkpriv(&BBS_priv, p_section->sid, S_POST)) + { + clearscr(); + moveto(1, 1); + prints("您没有权限在本版块发表文章\n"); + press_any_key(); + + return 0; + } + db = db_open(); if (db == NULL) { @@ -438,13 +467,30 @@ int article_modify(const SECTION_LIST *p if ((row = mysql_fetch_row(rs))) { - p_editor_data = editor_data_load(row[1]); + content = malloc(ARTICLE_CONTENT_MAX_LEN); + if (content == NULL) + { + log_error("malloc(content) error: OOM\n"); + ret = -1; + goto cleanup; + } + + strncpy(content, row[1], ARTICLE_CONTENT_MAX_LEN - 1); + content[ARTICLE_CONTENT_MAX_LEN - 1] = '\0'; + + // Remove control sequence + len_content = ctrl_seq_filter(content); + + p_editor_data = editor_data_load(content); if (p_editor_data == NULL) { log_error("editor_data_load(aid=%d, cid=%d) error\n", p_article->aid, atoi(row[0])); ret = -3; goto cleanup; } + + free(content); + content = NULL; } mysql_free_result(rs); rs = NULL; @@ -510,22 +556,6 @@ int article_modify(const SECTION_LIST *p "\n--\n※ 作者已于 %s 修改本文※\n", str_modify_dt); - 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; - } - db = db_open(); if (db == NULL) { @@ -550,14 +580,36 @@ int article_modify(const SECTION_LIST *p } // Secure SQL parameters + 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; + } + mysql_real_escape_string(db, content_f, content, (unsigned long)len_content); + free(content); + content = NULL; + // Add content - snprintf(sql, SQL_BUFFER_LEN + (size_t)len_content * 2 + 1, + 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; + } + + snprintf(sql_content, SQL_BUFFER_LEN + (size_t)len_content * 2 + 1, "INSERT INTO bbs_content(AID, content) values(%d, '%s')", p_article->aid, content_f); - if (mysql_query(db, sql) != 0) + free(content_f); + content_f = NULL; + + if (mysql_query(db, sql_content) != 0) { log_error("Add article content error: %s\n", mysql_error(db)); ret = -1; @@ -566,9 +618,12 @@ int article_modify(const SECTION_LIST *p p_article_new->cid = (int32_t)mysql_insert_id(db); + free(sql_content); + sql_content = NULL; + // Update article snprintf(sql, sizeof(sql), - "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 p_article_new->cid, len_content, p_article->aid); if (mysql_query(db, sql) != 0) @@ -613,6 +668,7 @@ int article_modify(const SECTION_LIST *p ret = 1; // Success cleanup: + mysql_free_result(rs); mysql_close(db); // Cleanup buffers @@ -649,17 +705,18 @@ int article_reply(const SECTION_LIST *p_ long quote_content_lines; long i; long ret = 0; + int topic_locked = 0; if (p_section == NULL || p_article == NULL) { log_error("NULL pointer error\n"); } - if (p_article->lock) // Reply is not allowed + if (!checkpriv(&BBS_priv, p_section->sid, S_POST)) { clearscr(); moveto(1, 1); - prints("该文章谢绝回复"); + prints("您没有权限在本版块发表文章\n"); press_any_key(); return 0; @@ -678,6 +735,41 @@ int article_reply(const SECTION_LIST *p_ } snprintf(sql, sizeof(sql), + "SELECT `lock` FROM bbs WHERE AID = %d", + (p_article->tid == 0 ? p_article->aid : p_article->tid)); + + if (mysql_query(db, sql) != 0) + { + log_error("Query article status error: %s\n", mysql_error(db)); + return -2; + } + if ((rs = mysql_store_result(db)) == NULL) + { + log_error("Get article status data failed\n"); + return -2; + } + + if ((row = mysql_fetch_row(rs))) + { + if (atoi(row[0]) != 0) + { + topic_locked = 1; + } + } + mysql_free_result(rs); + rs = NULL; + + if (topic_locked) // Reply is not allowed + { + clearscr(); + moveto(1, 1); + prints("该主题谢绝回复"); + press_any_key(); + + goto cleanup; + } + + snprintf(sql, sizeof(sql), "SELECT bbs_content.CID, bbs_content.content " "FROM bbs INNER JOIN bbs_content ON bbs.CID = bbs_content.CID " "WHERE bbs.AID = %d", @@ -731,6 +823,11 @@ int article_reply(const SECTION_LIST *p_ memcpy(content + len, content_f + line_offsets[i], (size_t)(line_offsets[i + 1] - line_offsets[i])); len += (line_offsets[i + 1] - line_offsets[i]); } + if (content[len - 1] != '\n') // Appennd \n if not exist + { + content[len] = '\n'; + len++; + } content[len] = '\0'; free(content_f); @@ -916,22 +1013,6 @@ int article_reply(const SECTION_LIST *p_ rs = NULL; } - 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; - } - // Begin transaction if (mysql_query(db, "SET autocommit=0") != 0) { @@ -948,16 +1029,38 @@ int article_reply(const SECTION_LIST *p_ } // Secure SQL parameters + 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; + } + mysql_real_escape_string(db, nickname_f, BBS_nickname, (unsigned long)strnlen(BBS_nickname, sizeof(BBS_nickname))); 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, content_f, content, (unsigned long)len_content); + free(content); + content = NULL; + // Add content - snprintf(sql, SQL_BUFFER_LEN + (size_t)len_content * 2 + 1, + 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; + } + + snprintf(sql_content, SQL_BUFFER_LEN + (size_t)len_content * 2 + 1, "INSERT INTO bbs_content(AID, content) values(0, '%s')", content_f); - if (mysql_query(db, sql) != 0) + free(content_f); + content_f = NULL; + + if (mysql_query(db, sql_content) != 0) { log_error("Add article content error: %s\n", mysql_error(db)); ret = -1; @@ -966,12 +1069,16 @@ int article_reply(const SECTION_LIST *p_ p_article_new->cid = (int32_t)mysql_insert_id(db); + free(sql_content); + sql_content = NULL; + // Add article snprintf(sql, sizeof(sql), "INSERT INTO bbs(SID, TID, UID, username, nickname, title, CID, transship, " "sub_dt, sub_ip, reply_note, exp, last_reply_dt, icon, length) " - "VALUES(%d, 0, %d, '%s', '%s', '%s', %d, 0, NOW(), '%s', 1, %d, NOW(), 1, %ld)", - p_section->sid, BBS_priv.uid, BBS_username, nickname_f, title_f, + "VALUES(%d, %d, %d, '%s', '%s', '%s', %d, 0, NOW(), '%s', 1, %d, NOW(), 1, %ld)", + p_section->sid, (p_article->tid == 0 ? p_article->aid : p_article->tid), + BBS_priv.uid, BBS_username, nickname_f, title_f, p_article_new->cid, hostaddr_client, BBS_user_exp, len_content); if (mysql_query(db, sql) != 0) @@ -987,8 +1094,16 @@ int article_reply(const SECTION_LIST *p_ snprintf(sql, sizeof(sql), "UPDATE bbs SET reply_count = reply_count + 1, " "last_reply_dt = NOW(), last_reply_UID=%d, last_reply_username = '%s', " - "last_reply_nickname = '%s' WHERE Aid = %d", - BBS_priv.uid, BBS_username, nickname_f, p_article->aid); + "last_reply_nickname = '%s' WHERE AID = %d", + BBS_priv.uid, BBS_username, nickname_f, + (p_article->tid == 0 ? p_article->aid : p_article->tid)); + + if (mysql_query(db, sql) != 0) + { + log_error("Update topic article error: %s\n", mysql_error(db)); + ret = -1; + goto cleanup; + } // Link content to article snprintf(sql, sizeof(sql),