| 1 |
/* SPDX-License-Identifier: GPL-3.0-or-later */
|
| 2 |
/*
|
| 3 |
* article_op
|
| 4 |
* - basic operations of articles
|
| 5 |
*
|
| 6 |
* Copyright (C) 2004-2026 Leaflet <leaflet@leafok.com>
|
| 7 |
*/
|
| 8 |
|
| 9 |
#ifdef HAVE_CONFIG_H
|
| 10 |
#include "config.h"
|
| 11 |
#endif
|
| 12 |
|
| 13 |
#include "article_op.h"
|
| 14 |
#include "bbs.h"
|
| 15 |
#include "database.h"
|
| 16 |
#include "io.h"
|
| 17 |
#include "log.h"
|
| 18 |
#include "screen.h"
|
| 19 |
#include "user_priv.h"
|
| 20 |
#include <stdlib.h>
|
| 21 |
|
| 22 |
int display_article_meta(int32_t aid)
|
| 23 |
{
|
| 24 |
clearscr();
|
| 25 |
moveto(3, 1);
|
| 26 |
prints("Web版 文章链接: ");
|
| 27 |
moveto(4, 1);
|
| 28 |
prints("https://%s/bbs/view_article.php?id=%d#%d", BBS_server, aid, aid);
|
| 29 |
press_any_key();
|
| 30 |
|
| 31 |
return 0;
|
| 32 |
}
|
| 33 |
|
| 34 |
int article_excerption_set(SECTION_LIST *p_section, int32_t aid, int8_t set)
|
| 35 |
{
|
| 36 |
MYSQL *db = NULL;
|
| 37 |
MYSQL_RES *rs = NULL;
|
| 38 |
MYSQL_ROW row;
|
| 39 |
char sql[SQL_BUFFER_LEN];
|
| 40 |
int ret = 0;
|
| 41 |
int uid = 0;
|
| 42 |
int tid = 0;
|
| 43 |
int sid = 0;
|
| 44 |
int8_t transship = 0;
|
| 45 |
int8_t excerption = 0;
|
| 46 |
|
| 47 |
if (p_section == NULL)
|
| 48 |
{
|
| 49 |
log_error("NULL pointer error");
|
| 50 |
ret = -1;
|
| 51 |
goto cleanup;
|
| 52 |
}
|
| 53 |
|
| 54 |
if (set)
|
| 55 |
{
|
| 56 |
set = 1;
|
| 57 |
}
|
| 58 |
|
| 59 |
db = db_open();
|
| 60 |
if (db == NULL)
|
| 61 |
{
|
| 62 |
log_error("db_open() error: %s", mysql_error(db));
|
| 63 |
ret = -1;
|
| 64 |
goto cleanup;
|
| 65 |
}
|
| 66 |
|
| 67 |
// Begin transaction
|
| 68 |
if (mysql_query(db, "SET autocommit=0") != 0)
|
| 69 |
{
|
| 70 |
log_error("SET autocommit=0 error: %s", mysql_error(db));
|
| 71 |
ret = -1;
|
| 72 |
goto cleanup;
|
| 73 |
}
|
| 74 |
|
| 75 |
if (mysql_query(db, "BEGIN") != 0)
|
| 76 |
{
|
| 77 |
log_error("Begin transaction error: %s", mysql_error(db));
|
| 78 |
ret = -1;
|
| 79 |
goto cleanup;
|
| 80 |
}
|
| 81 |
|
| 82 |
snprintf(sql, sizeof(sql),
|
| 83 |
"SELECT UID, TID, SID, transship, excerption FROM bbs "
|
| 84 |
"WHERE AID = %d AND visible FOR UPDATE",
|
| 85 |
aid);
|
| 86 |
if (mysql_query(db, sql) != 0)
|
| 87 |
{
|
| 88 |
log_error("Query article error: %s", mysql_error(db));
|
| 89 |
ret = -2;
|
| 90 |
goto cleanup;
|
| 91 |
}
|
| 92 |
|
| 93 |
if ((rs = mysql_store_result(db)) == NULL)
|
| 94 |
{
|
| 95 |
log_error("Get article data failed");
|
| 96 |
ret = -2;
|
| 97 |
goto cleanup;
|
| 98 |
}
|
| 99 |
|
| 100 |
if ((row = mysql_fetch_row(rs)))
|
| 101 |
{
|
| 102 |
uid = atoi(row[0]);
|
| 103 |
tid = atoi(row[1]);
|
| 104 |
sid = atoi(row[2]);
|
| 105 |
transship = (int8_t)atoi(row[3]);
|
| 106 |
excerption = (int8_t)atoi(row[4]);
|
| 107 |
}
|
| 108 |
else
|
| 109 |
{
|
| 110 |
log_error("Article not found: aid=%d", aid);
|
| 111 |
ret = -2;
|
| 112 |
goto cleanup;
|
| 113 |
}
|
| 114 |
mysql_free_result(rs);
|
| 115 |
rs = NULL;
|
| 116 |
|
| 117 |
if (p_section->sid != sid)
|
| 118 |
{
|
| 119 |
log_error("Inconsistent SID of article (aid=%d): %d!=%d", aid, p_section->sid, sid);
|
| 120 |
ret = -2;
|
| 121 |
goto cleanup;
|
| 122 |
}
|
| 123 |
|
| 124 |
// Check if already set
|
| 125 |
if (excerption != set)
|
| 126 |
{
|
| 127 |
snprintf(sql, sizeof(sql),
|
| 128 |
"UPDATE bbs SET excerption = %d WHERE AID = %d",
|
| 129 |
set, aid);
|
| 130 |
if (mysql_query(db, sql) != 0)
|
| 131 |
{
|
| 132 |
log_error("Set excerption error: %s", mysql_error(db));
|
| 133 |
ret = -2;
|
| 134 |
goto cleanup;
|
| 135 |
}
|
| 136 |
|
| 137 |
// Clear gen_ex if unset excerption
|
| 138 |
if (set == 0)
|
| 139 |
{
|
| 140 |
snprintf(sql, sizeof(sql),
|
| 141 |
"UPDATE bbs SET gen_ex = 0, static = 0 WHERE AID = %d OR TID = %d",
|
| 142 |
aid, aid);
|
| 143 |
|
| 144 |
if (mysql_query(db, sql) != 0)
|
| 145 |
{
|
| 146 |
log_error("Set gen_ex error: %s", mysql_error(db));
|
| 147 |
ret = -2;
|
| 148 |
goto cleanup;
|
| 149 |
}
|
| 150 |
|
| 151 |
// Delete ex_dir path if head of thread
|
| 152 |
if (tid == 0)
|
| 153 |
{
|
| 154 |
snprintf(sql, sizeof(sql),
|
| 155 |
"DELETE FROM ex_file WHERE AID = %d",
|
| 156 |
aid);
|
| 157 |
|
| 158 |
if (mysql_query(db, sql) != 0)
|
| 159 |
{
|
| 160 |
log_error("Delete ex_file error: %s", mysql_error(db));
|
| 161 |
ret = -2;
|
| 162 |
goto cleanup;
|
| 163 |
}
|
| 164 |
}
|
| 165 |
}
|
| 166 |
|
| 167 |
// Change UID of attachments
|
| 168 |
snprintf(sql, sizeof(sql),
|
| 169 |
"UPDATE upload_file SET UID = %d "
|
| 170 |
"WHERE ref_AID = %d AND deleted = 0",
|
| 171 |
(set ? 0 : uid), aid);
|
| 172 |
if (mysql_query(db, sql) != 0)
|
| 173 |
{
|
| 174 |
log_error("Set attachment status error: %s", mysql_error(db));
|
| 175 |
ret = -2;
|
| 176 |
goto cleanup;
|
| 177 |
}
|
| 178 |
|
| 179 |
// Modify exp
|
| 180 |
if (checkpriv(&BBS_priv, sid, S_GETEXP)) // Except in test section
|
| 181 |
{
|
| 182 |
snprintf(sql, sizeof(sql),
|
| 183 |
"UPDATE user_pubinfo SET exp = exp + %d WHERE UID = %d",
|
| 184 |
(set ? 1 : -1) * (tid == 0 ? (transship ? 20 : 50) : 10), uid);
|
| 185 |
|
| 186 |
if (mysql_query(db, sql) != 0)
|
| 187 |
{
|
| 188 |
log_error("Change exp error: %s", mysql_error(db));
|
| 189 |
ret = -2;
|
| 190 |
goto cleanup;
|
| 191 |
}
|
| 192 |
}
|
| 193 |
|
| 194 |
// Add log
|
| 195 |
snprintf(sql, sizeof(sql),
|
| 196 |
"INSERT INTO bbs_article_op(AID, UID, type, op_dt, op_ip) "
|
| 197 |
"VALUES(%d, %d, '%c', NOW(), '%s')",
|
| 198 |
aid, BBS_priv.uid, (set ? 'E' : 'O'), hostaddr_client);
|
| 199 |
|
| 200 |
if (mysql_query(db, sql) != 0)
|
| 201 |
{
|
| 202 |
log_error("Add log error: %s", mysql_error(db));
|
| 203 |
ret = -2;
|
| 204 |
goto cleanup;
|
| 205 |
}
|
| 206 |
}
|
| 207 |
|
| 208 |
if (section_list_rw_lock(p_section) < 0)
|
| 209 |
{
|
| 210 |
log_error("section_list_rw_lock(sid=%d) error", sid);
|
| 211 |
ret = -3;
|
| 212 |
goto cleanup;
|
| 213 |
}
|
| 214 |
|
| 215 |
ret = section_list_set_article_excerption(p_section, aid, set);
|
| 216 |
if (ret < 0)
|
| 217 |
{
|
| 218 |
log_error("section_list_set_article_excerption(sid=%d, aid=%d, set=%d) error", sid, aid, set);
|
| 219 |
ret = -3;
|
| 220 |
goto cleanup;
|
| 221 |
}
|
| 222 |
|
| 223 |
if (section_list_rw_unlock(p_section) < 0)
|
| 224 |
{
|
| 225 |
log_error("section_list_rw_unlock(sid=%d) error", sid);
|
| 226 |
ret = -3;
|
| 227 |
goto cleanup;
|
| 228 |
}
|
| 229 |
|
| 230 |
// Commit transaction
|
| 231 |
if (mysql_query(db, "COMMIT") != 0)
|
| 232 |
{
|
| 233 |
log_error("Mysqli error: %s", mysql_error(db));
|
| 234 |
|
| 235 |
// Rollback change to avoid data inconsistency
|
| 236 |
if (section_list_rw_lock(p_section) < 0)
|
| 237 |
{
|
| 238 |
log_error("section_list_rw_lock(sid=%d) error", sid);
|
| 239 |
ret = -3;
|
| 240 |
goto cleanup;
|
| 241 |
}
|
| 242 |
|
| 243 |
ret = section_list_set_article_excerption(p_section, aid, excerption);
|
| 244 |
if (ret < 0)
|
| 245 |
{
|
| 246 |
log_error("section_list_set_article_excerption(sid=%d, aid=%d, set=%d) error", sid, aid, excerption);
|
| 247 |
ret = -3;
|
| 248 |
goto cleanup;
|
| 249 |
}
|
| 250 |
|
| 251 |
if (section_list_rw_unlock(p_section) < 0)
|
| 252 |
{
|
| 253 |
log_error("section_list_rw_unlock(sid=%d) error", sid);
|
| 254 |
ret = -3;
|
| 255 |
goto cleanup;
|
| 256 |
}
|
| 257 |
|
| 258 |
ret = 0;
|
| 259 |
}
|
| 260 |
else
|
| 261 |
{
|
| 262 |
ret = 1; // Success
|
| 263 |
}
|
| 264 |
|
| 265 |
cleanup:
|
| 266 |
mysql_free_result(rs);
|
| 267 |
mysql_close(db);
|
| 268 |
|
| 269 |
return ret;
|
| 270 |
}
|