| 20 |
#include "database.h" |
#include "database.h" |
| 21 |
#include "log.h" |
#include "log.h" |
| 22 |
#include <stdio.h> |
#include <stdio.h> |
|
#include <mysql.h> |
|
| 23 |
#include <stdlib.h> |
#include <stdlib.h> |
| 24 |
|
#include <mysql/mysql.h> |
| 25 |
|
|
| 26 |
BBS_user_priv BBS_priv; |
BBS_user_priv BBS_priv; |
| 27 |
|
|
| 28 |
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) |
| 29 |
{ |
{ |
| 30 |
int left = 0; |
int left = 0; |
| 31 |
int right = p_priv->s_count - 1; |
int right = p_priv->s_count - 1; |
| 32 |
int mid = 0; |
int mid = 0; |
| 33 |
|
|
|
if (sid == 0) |
|
|
{ |
|
|
p_priv->g_priv = priv; |
|
|
return 0; |
|
|
} |
|
|
|
|
| 34 |
while (left < right) |
while (left < right) |
| 35 |
{ |
{ |
| 36 |
mid = (left + right) / 2; |
mid = (left + right) / 2; |
| 45 |
} |
} |
| 46 |
} |
} |
| 47 |
|
|
| 48 |
if (left == right && sid == p_priv->s_priv_list[left].sid) // found |
*p_offset = left; |
| 49 |
|
|
| 50 |
|
return (left == right && sid == p_priv->s_priv_list[left].sid); |
| 51 |
|
} |
| 52 |
|
|
| 53 |
|
int setpriv(BBS_user_priv *p_priv, int sid, int priv, int is_favor) |
| 54 |
|
{ |
| 55 |
|
int offset; |
| 56 |
|
int i; |
| 57 |
|
|
| 58 |
|
if (sid == 0) |
| 59 |
|
{ |
| 60 |
|
p_priv->g_priv = priv; |
| 61 |
|
return 0; |
| 62 |
|
} |
| 63 |
|
|
| 64 |
|
if (search_priv(p_priv, sid, &offset)) //found |
| 65 |
{ |
{ |
| 66 |
p_priv->s_priv_list[left].s_priv = priv; |
p_priv->s_priv_list[offset].s_priv = priv; |
| 67 |
p_priv->s_priv_list[left].is_favor = is_favor; |
p_priv->s_priv_list[offset].is_favor = is_favor; |
| 68 |
return 0; |
return 0; |
| 69 |
} |
} |
| 70 |
|
|
| 75 |
} |
} |
| 76 |
|
|
| 77 |
// 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] |
| 78 |
for (right = p_priv->s_count - 1; right >= left; right--) |
for (i = p_priv->s_count - 1; i >= offset; i--) |
| 79 |
{ |
{ |
| 80 |
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]; |
| 81 |
} |
} |
| 82 |
p_priv->s_count++; |
p_priv->s_count++; |
| 83 |
|
|
| 84 |
// insert new item at offset left |
// insert new item at offset left |
| 85 |
p_priv->s_priv_list[left].sid = sid; |
p_priv->s_priv_list[offset].sid = sid; |
| 86 |
p_priv->s_priv_list[left].s_priv = priv; |
p_priv->s_priv_list[offset].s_priv = priv; |
| 87 |
p_priv->s_priv_list[left].is_favor = is_favor; |
p_priv->s_priv_list[offset].is_favor = is_favor; |
| 88 |
|
|
| 89 |
return 0; |
return 0; |
| 90 |
} |
} |
| 91 |
|
|
| 92 |
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) |
| 93 |
{ |
{ |
| 94 |
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; |
|
|
} |
|
| 95 |
|
|
| 96 |
if (sid != 0) |
if (search_priv(p_priv, sid, &offset)) //found |
| 97 |
{ |
{ |
| 98 |
*p_is_favor = 0; |
*p_is_favor = p_priv->s_priv_list[offset].is_favor; |
| 99 |
|
return p_priv->s_priv_list[offset].s_priv; |
| 100 |
} |
} |
| 101 |
|
|
| 102 |
|
*p_is_favor = 0; |
| 103 |
return (sid >= 0 ? p_priv->g_priv : S_NONE); |
return (sid >= 0 ? p_priv->g_priv : S_NONE); |
| 104 |
} |
} |
| 105 |
|
|
| 106 |
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) |
| 107 |
{ |
{ |
| 108 |
MYSQL_RES *rs; |
MYSQL_RES *rs; |
| 109 |
MYSQL_ROW row; |
MYSQL_ROW row; |
| 121 |
|
|
| 122 |
// Permission |
// Permission |
| 123 |
snprintf(sql, sizeof(sql), |
snprintf(sql, sizeof(sql), |
| 124 |
"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", |
| 125 |
uid); |
uid); |
| 126 |
if (mysql_query(db, sql) != 0) |
if (mysql_query(db, sql) != 0) |
| 127 |
{ |
{ |
| 142 |
|
|
| 143 |
// Admin |
// Admin |
| 144 |
snprintf(sql, sizeof(sql), |
snprintf(sql, sizeof(sql), |
| 145 |
"SELECT major FROM admin_config WHERE UID = %ld " |
"SELECT major FROM admin_config WHERE UID = %d " |
| 146 |
"AND enable AND (NOW() BETWEEN begin_dt AND end_dt)", |
"AND enable AND (NOW() BETWEEN begin_dt AND end_dt)", |
| 147 |
uid); |
uid); |
| 148 |
if (mysql_query(db, sql) != 0) |
if (mysql_query(db, sql) != 0) |
| 166 |
snprintf(sql, sizeof(sql), |
snprintf(sql, sizeof(sql), |
| 167 |
"SELECT section_master.SID, major FROM section_master " |
"SELECT section_master.SID, major FROM section_master " |
| 168 |
"INNER JOIN section_config ON section_master.SID = section_config.SID " |
"INNER JOIN section_config ON section_master.SID = section_config.SID " |
| 169 |
"WHERE UID = %ld AND section_master.enable AND section_config.enable " |
"WHERE UID = %d AND section_master.enable AND section_config.enable " |
| 170 |
"AND (NOW() BETWEEN begin_dt AND end_dt)", |
"AND (NOW() BETWEEN begin_dt AND end_dt)", |
| 171 |
uid); |
uid); |
| 172 |
if (mysql_query(db, sql) != 0) |
if (mysql_query(db, sql) != 0) |
| 224 |
|
|
| 225 |
// Section ban |
// Section ban |
| 226 |
snprintf(sql, sizeof(sql), |
snprintf(sql, sizeof(sql), |
| 227 |
"SELECT SID FROM ban_user_list WHERE UID = %ld AND enable " |
"SELECT SID FROM ban_user_list WHERE UID = %d AND enable " |
| 228 |
"AND (NOW() BETWEEN ban_dt AND unban_dt)", |
"AND (NOW() BETWEEN ban_dt AND unban_dt)", |
| 229 |
uid); |
uid); |
| 230 |
if (mysql_query(db, sql) != 0) |
if (mysql_query(db, sql) != 0) |
| 246 |
|
|
| 247 |
// User favor section |
// User favor section |
| 248 |
snprintf(sql, sizeof(sql), |
snprintf(sql, sizeof(sql), |
| 249 |
"SELECT SID FROM section_favorite WHERE UID = %ld", |
"SELECT SID FROM section_favorite WHERE UID = %d", |
| 250 |
uid); |
uid); |
| 251 |
if (mysql_query(db, sql) != 0) |
if (mysql_query(db, sql) != 0) |
| 252 |
{ |
{ |