| 1 |
/*************************************************************************** |
/* SPDX-License-Identifier: GPL-3.0-or-later */ |
| 2 |
user_priv.c - description |
/* |
| 3 |
------------------- |
* user_priv |
| 4 |
Copyright : (C) 2004-2025 by Leaflet |
* - basic operations of user privilege |
| 5 |
Email : leaflet@leafok.com |
* |
| 6 |
***************************************************************************/ |
* Copyright (C) 2004-2025 Leaflet <leaflet@leafok.com> |
| 7 |
|
*/ |
| 8 |
/*************************************************************************** |
|
| 9 |
* * |
#ifdef HAVE_CONFIG_H |
| 10 |
* This program is free software; you can redistribute it and/or modify * |
#include "config.h" |
| 11 |
* it under the terms of the GNU General Public License as published by * |
#endif |
|
* the Free Software Foundation; either version 3 of the License, or * |
|
|
* (at your option) any later version. * |
|
|
* * |
|
|
***************************************************************************/ |
|
| 12 |
|
|
|
#include "user_priv.h" |
|
| 13 |
#include "bbs.h" |
#include "bbs.h" |
| 14 |
#include "common.h" |
#include "common.h" |
| 15 |
#include "database.h" |
#include "database.h" |
| 16 |
#include "log.h" |
#include "log.h" |
| 17 |
|
#include "user_priv.h" |
| 18 |
#include <stdio.h> |
#include <stdio.h> |
|
#include <mysql.h> |
|
| 19 |
#include <stdlib.h> |
#include <stdlib.h> |
| 20 |
|
#include <mysql/mysql.h> |
| 21 |
|
|
| 22 |
BBS_user_priv BBS_priv; |
BBS_user_priv BBS_priv; |
| 23 |
|
|
| 24 |
int setpriv(BBS_user_priv *p_priv, int sid, int priv, int is_favor) |
inline static int search_priv(BBS_user_priv *p_priv, int sid, int *p_offset) |
| 25 |
{ |
{ |
| 26 |
int left = 0; |
int left = 0; |
| 27 |
int right = p_priv->s_count - 1; |
int right = p_priv->s_count - 1; |
| 28 |
int mid = 0; |
int mid = 0; |
| 29 |
|
|
|
if (sid == 0) |
|
|
{ |
|
|
p_priv->g_priv = priv; |
|
|
return 0; |
|
|
} |
|
|
|
|
| 30 |
while (left < right) |
while (left < right) |
| 31 |
{ |
{ |
| 32 |
mid = (left + right) / 2; |
mid = (left + right) / 2; |
| 33 |
|
|
| 34 |
if (sid <= p_priv->s_priv_list[mid].sid) |
if (sid < p_priv->s_priv_list[mid].sid) |
| 35 |
{ |
{ |
| 36 |
right = mid; |
right = mid - 1; |
| 37 |
} |
} |
| 38 |
else |
else if (sid > p_priv->s_priv_list[mid].sid) |
| 39 |
{ |
{ |
| 40 |
left = mid + 1; |
left = mid + 1; |
| 41 |
} |
} |
| 42 |
|
else // if (sid == p_priv->s_priv_list[mid].sid) |
| 43 |
|
{ |
| 44 |
|
left = mid; |
| 45 |
|
break; |
| 46 |
|
} |
| 47 |
|
} |
| 48 |
|
|
| 49 |
|
*p_offset = left; |
| 50 |
|
|
| 51 |
|
return (sid == p_priv->s_priv_list[left].sid); |
| 52 |
|
} |
| 53 |
|
|
| 54 |
|
int setpriv(BBS_user_priv *p_priv, int sid, int priv, int is_favor) |
| 55 |
|
{ |
| 56 |
|
int offset; |
| 57 |
|
int i; |
| 58 |
|
|
| 59 |
|
if (sid == 0) |
| 60 |
|
{ |
| 61 |
|
p_priv->g_priv = priv; |
| 62 |
|
return 0; |
| 63 |
} |
} |
| 64 |
|
|
| 65 |
if (left == right && sid == p_priv->s_priv_list[left].sid) // found |
if (search_priv(p_priv, sid, &offset)) // found |
| 66 |
{ |
{ |
| 67 |
p_priv->s_priv_list[left].s_priv = priv; |
p_priv->s_priv_list[offset].s_priv = priv; |
| 68 |
p_priv->s_priv_list[left].is_favor = is_favor; |
p_priv->s_priv_list[offset].is_favor = is_favor; |
| 69 |
return 0; |
return 0; |
| 70 |
} |
} |
| 71 |
|
|
| 76 |
} |
} |
| 77 |
|
|
| 78 |
// move items at [left, p_priv->s_count - 1] to [left + 1, p_priv->s_count] |
// move items at [left, p_priv->s_count - 1] to [left + 1, p_priv->s_count] |
| 79 |
for (right = p_priv->s_count - 1; right >= left; right--) |
for (i = p_priv->s_count - 1; i >= offset; i--) |
| 80 |
{ |
{ |
| 81 |
p_priv->s_priv_list[right + 1] = p_priv->s_priv_list[right]; |
p_priv->s_priv_list[i + 1] = p_priv->s_priv_list[i]; |
| 82 |
} |
} |
| 83 |
p_priv->s_count++; |
p_priv->s_count++; |
| 84 |
|
|
| 85 |
// insert new item at offset left |
// insert new item at offset left |
| 86 |
p_priv->s_priv_list[left].sid = sid; |
p_priv->s_priv_list[offset].sid = sid; |
| 87 |
p_priv->s_priv_list[left].s_priv = priv; |
p_priv->s_priv_list[offset].s_priv = priv; |
| 88 |
p_priv->s_priv_list[left].is_favor = is_favor; |
p_priv->s_priv_list[offset].is_favor = is_favor; |
| 89 |
|
|
| 90 |
return 0; |
return 0; |
| 91 |
} |
} |
| 92 |
|
|
| 93 |
int getpriv(BBS_user_priv *p_priv, int sid, int *p_is_favor) |
int getpriv(BBS_user_priv *p_priv, int sid, int *p_is_favor) |
| 94 |
{ |
{ |
| 95 |
int left = 0; |
int offset; |
|
int right = p_priv->s_count - 1; |
|
|
int mid = 0; |
|
|
|
|
|
while (left < right) |
|
|
{ |
|
|
mid = (left + right) / 2; |
|
|
|
|
|
if (sid <= p_priv->s_priv_list[mid].sid) |
|
|
{ |
|
|
right = mid; |
|
|
} |
|
|
else |
|
|
{ |
|
|
left = mid + 1; |
|
|
} |
|
|
} |
|
|
|
|
|
if (left == right && sid == p_priv->s_priv_list[left].sid) // found |
|
|
{ |
|
|
*p_is_favor = p_priv->s_priv_list[left].is_favor; |
|
|
return p_priv->s_priv_list[left].s_priv; |
|
|
} |
|
| 96 |
|
|
| 97 |
if (sid != 0) |
if (search_priv(p_priv, sid, &offset)) // found |
| 98 |
{ |
{ |
| 99 |
*p_is_favor = 0; |
*p_is_favor = p_priv->s_priv_list[offset].is_favor; |
| 100 |
|
return p_priv->s_priv_list[offset].s_priv; |
| 101 |
} |
} |
| 102 |
|
|
| 103 |
|
*p_is_favor = 0; |
| 104 |
return (sid >= 0 ? p_priv->g_priv : S_NONE); |
return (sid >= 0 ? p_priv->g_priv : S_NONE); |
| 105 |
} |
} |
| 106 |
|
|
| 107 |
int load_priv(MYSQL *db, BBS_user_priv *p_priv, long int uid) |
int load_priv(MYSQL *db, BBS_user_priv *p_priv, int uid) |
| 108 |
{ |
{ |
| 109 |
MYSQL_RES *rs; |
MYSQL_RES *rs; |
| 110 |
MYSQL_ROW row; |
MYSQL_ROW row; |
| 122 |
|
|
| 123 |
// Permission |
// Permission |
| 124 |
snprintf(sql, sizeof(sql), |
snprintf(sql, sizeof(sql), |
| 125 |
"SELECT p_post, p_msg FROM user_list WHERE UID = %ld AND verified", |
"SELECT p_post, p_msg FROM user_list WHERE UID = %d AND verified", |
| 126 |
uid); |
uid); |
| 127 |
if (mysql_query(db, sql) != 0) |
if (mysql_query(db, sql) != 0) |
| 128 |
{ |
{ |
| 143 |
|
|
| 144 |
// Admin |
// Admin |
| 145 |
snprintf(sql, sizeof(sql), |
snprintf(sql, sizeof(sql), |
| 146 |
"SELECT major FROM admin_config WHERE UID = %ld " |
"SELECT major FROM admin_config WHERE UID = %d " |
| 147 |
"AND enable AND (NOW() BETWEEN begin_dt AND end_dt)", |
"AND enable AND (NOW() BETWEEN begin_dt AND end_dt)", |
| 148 |
uid); |
uid); |
| 149 |
if (mysql_query(db, sql) != 0) |
if (mysql_query(db, sql) != 0) |
| 167 |
snprintf(sql, sizeof(sql), |
snprintf(sql, sizeof(sql), |
| 168 |
"SELECT section_master.SID, major FROM section_master " |
"SELECT section_master.SID, major FROM section_master " |
| 169 |
"INNER JOIN section_config ON section_master.SID = section_config.SID " |
"INNER JOIN section_config ON section_master.SID = section_config.SID " |
| 170 |
"WHERE UID = %ld AND section_master.enable AND section_config.enable " |
"WHERE UID = %d AND section_master.enable AND section_config.enable " |
| 171 |
"AND (NOW() BETWEEN begin_dt AND end_dt)", |
"AND (NOW() BETWEEN begin_dt AND end_dt)", |
| 172 |
uid); |
uid); |
| 173 |
if (mysql_query(db, sql) != 0) |
if (mysql_query(db, sql) != 0) |
| 225 |
|
|
| 226 |
// Section ban |
// Section ban |
| 227 |
snprintf(sql, sizeof(sql), |
snprintf(sql, sizeof(sql), |
| 228 |
"SELECT SID FROM ban_user_list WHERE UID = %ld AND enable " |
"SELECT SID FROM ban_user_list WHERE UID = %d AND enable " |
| 229 |
"AND (NOW() BETWEEN ban_dt AND unban_dt)", |
"AND (NOW() BETWEEN ban_dt AND unban_dt)", |
| 230 |
uid); |
uid); |
| 231 |
if (mysql_query(db, sql) != 0) |
if (mysql_query(db, sql) != 0) |
| 247 |
|
|
| 248 |
// User favor section |
// User favor section |
| 249 |
snprintf(sql, sizeof(sql), |
snprintf(sql, sizeof(sql), |
| 250 |
"SELECT SID FROM section_favorite WHERE UID = %ld", |
"SELECT SID FROM section_favorite WHERE UID = %d", |
| 251 |
uid); |
uid); |
| 252 |
if (mysql_query(db, sql) != 0) |
if (mysql_query(db, sql) != 0) |
| 253 |
{ |
{ |