| 1 |
/***************************************************************************
|
| 2 |
section_list_loader.c - description
|
| 3 |
-------------------
|
| 4 |
Copyright : (C) 2004-2025 by Leaflet
|
| 5 |
Email : leaflet@leafok.com
|
| 6 |
***************************************************************************/
|
| 7 |
|
| 8 |
/***************************************************************************
|
| 9 |
* *
|
| 10 |
* This program is free software; you can redistribute it and/or modify *
|
| 11 |
* it under the terms of the GNU General Public License as published by *
|
| 12 |
* the Free Software Foundation; either version 3 of the License, or *
|
| 13 |
* (at your option) any later version. *
|
| 14 |
* *
|
| 15 |
***************************************************************************/
|
| 16 |
|
| 17 |
#include "section_list_loader.h"
|
| 18 |
#include "log.h"
|
| 19 |
#include "database.h"
|
| 20 |
#include <stdio.h>
|
| 21 |
#include <string.h>
|
| 22 |
#include <errno.h>
|
| 23 |
#include <stdlib.h>
|
| 24 |
|
| 25 |
int load_section_config_from_db(void)
|
| 26 |
{
|
| 27 |
MYSQL *db;
|
| 28 |
MYSQL_RES *rs, *rs2;
|
| 29 |
MYSQL_ROW row, row2;
|
| 30 |
char sql[SQL_BUFFER_LEN];
|
| 31 |
int32_t sid;
|
| 32 |
char master_name[BBS_username_max_len + 1];
|
| 33 |
SECTION_LIST *p_section;
|
| 34 |
int ret;
|
| 35 |
|
| 36 |
db = db_open();
|
| 37 |
if (db == NULL)
|
| 38 |
{
|
| 39 |
log_error("db_open() error: %s\n", mysql_error(db));
|
| 40 |
return -2;
|
| 41 |
}
|
| 42 |
|
| 43 |
snprintf(sql, sizeof(sql),
|
| 44 |
"SELECT section_config.SID, sname, section_config.title, section_config.CID, "
|
| 45 |
"read_user_level, write_user_level, section_config.enable * section_class.enable AS enable "
|
| 46 |
"FROM section_config INNER JOIN section_class ON section_config.CID = section_class.CID "
|
| 47 |
"ORDER BY section_config.SID");
|
| 48 |
|
| 49 |
if (mysql_query(db, sql) != 0)
|
| 50 |
{
|
| 51 |
log_error("Query section_list error: %s\n", mysql_error(db));
|
| 52 |
return -3;
|
| 53 |
}
|
| 54 |
if ((rs = mysql_store_result(db)) == NULL)
|
| 55 |
{
|
| 56 |
log_error("Get section_list data failed\n");
|
| 57 |
return -3;
|
| 58 |
}
|
| 59 |
|
| 60 |
ret = 0;
|
| 61 |
while ((row = mysql_fetch_row(rs)))
|
| 62 |
{
|
| 63 |
sid = atoi(row[0]);
|
| 64 |
|
| 65 |
// Query section master
|
| 66 |
snprintf(sql, sizeof(sql),
|
| 67 |
"SELECT username FROM section_master "
|
| 68 |
"INNER JOIN user_list ON section_master.UID = user_list.UID "
|
| 69 |
"WHERE SID = %d AND section_master.enable AND (NOW() BETWEEN begin_dt AND end_dt) "
|
| 70 |
"ORDER BY major DESC LIMIT 1",
|
| 71 |
sid);
|
| 72 |
|
| 73 |
if (mysql_query(db, sql) != 0)
|
| 74 |
{
|
| 75 |
log_error("Query section_master error: %s\n", mysql_error(db));
|
| 76 |
ret = -3;
|
| 77 |
break;
|
| 78 |
}
|
| 79 |
if ((rs2 = mysql_store_result(db)) == NULL)
|
| 80 |
{
|
| 81 |
log_error("Get section_master data failed\n");
|
| 82 |
ret = -3;
|
| 83 |
break;
|
| 84 |
}
|
| 85 |
if ((row2 = mysql_fetch_row(rs2)))
|
| 86 |
{
|
| 87 |
strncpy(master_name, row2[0], sizeof(master_name) - 1);
|
| 88 |
master_name[sizeof(master_name) - 1] = '\0';
|
| 89 |
}
|
| 90 |
else
|
| 91 |
{
|
| 92 |
master_name[0] = '\0';
|
| 93 |
}
|
| 94 |
mysql_free_result(rs2);
|
| 95 |
|
| 96 |
p_section = section_list_find_by_sid(sid);
|
| 97 |
|
| 98 |
if (p_section == NULL)
|
| 99 |
{
|
| 100 |
p_section = section_list_create(sid, row[1], row[2], "");
|
| 101 |
if (p_section == NULL)
|
| 102 |
{
|
| 103 |
log_error("section_list_create() error: load new section sid = %d sname = %s\n", sid, row[1]);
|
| 104 |
ret = -4;
|
| 105 |
break;
|
| 106 |
}
|
| 107 |
|
| 108 |
// acquire rw lock
|
| 109 |
ret = section_list_rw_lock(p_section);
|
| 110 |
if (ret < 0)
|
| 111 |
{
|
| 112 |
break;
|
| 113 |
}
|
| 114 |
}
|
| 115 |
else
|
| 116 |
{
|
| 117 |
// acquire rw lock
|
| 118 |
ret = section_list_rw_lock(p_section);
|
| 119 |
if (ret < 0)
|
| 120 |
{
|
| 121 |
break;
|
| 122 |
}
|
| 123 |
|
| 124 |
strncpy(p_section->sname, row[1], sizeof(p_section->sname) - 1);
|
| 125 |
p_section->sname[sizeof(p_section->sname) - 1] = '\0';
|
| 126 |
strncpy(p_section->stitle, row[1], sizeof(p_section->stitle) - 1);
|
| 127 |
p_section->stitle[sizeof(p_section->stitle) - 1] = '\0';
|
| 128 |
strncpy(p_section->master_name, master_name, sizeof(p_section->master_name) - 1);
|
| 129 |
p_section->master_name[sizeof(p_section->master_name) - 1] = '\0';
|
| 130 |
}
|
| 131 |
|
| 132 |
p_section->class_id = atoi(row[3]);
|
| 133 |
p_section->read_user_level = atoi(row[4]);
|
| 134 |
p_section->write_user_level = atoi(row[5]);
|
| 135 |
p_section->enable = (int8_t)atoi(row[6]);
|
| 136 |
|
| 137 |
// release rw lock
|
| 138 |
ret = section_list_rw_unlock(p_section);
|
| 139 |
if (ret < 0)
|
| 140 |
{
|
| 141 |
break;
|
| 142 |
}
|
| 143 |
}
|
| 144 |
mysql_free_result(rs);
|
| 145 |
|
| 146 |
mysql_close(db);
|
| 147 |
|
| 148 |
return ret;
|
| 149 |
}
|