/[LeafOK_CVS]/lbbs/src/user_priv.c
ViewVC logotype

Diff of /lbbs/src/user_priv.c

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

Revision 1.15 by sysadm, Mon May 26 02:56:59 2025 UTC Revision 1.24 by sysadm, Tue Nov 4 14:58:56 2025 UTC
# Line 1  Line 1 
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     */
 /***************************************************************************  
  *                                                                         *  
  *   This program is free software; you can redistribute it and/or modify  *  
  *   it under the terms of the GNU General Public License as published by  *  
  *   the Free Software Foundation; either version 3 of the License, or     *  
  *   (at your option) any later version.                                   *  
  *                                                                         *  
  ***************************************************************************/  
8    
 #include "user_priv.h"  
9  #include "bbs.h"  #include "bbs.h"
10  #include "common.h"  #include "common.h"
11  #include "database.h"  #include "database.h"
12  #include "log.h"  #include "log.h"
13    #include "user_priv.h"
14  #include <stdio.h>  #include <stdio.h>
 #include <mysql.h>  
15  #include <stdlib.h>  #include <stdlib.h>
16    #include <mysql/mysql.h>
17    
18  BBS_user_priv BBS_priv;  BBS_user_priv BBS_priv;
19    
20  int setpriv(BBS_user_priv *p_priv, int sid, int priv)  inline static int search_priv(BBS_user_priv *p_priv, int sid, int *p_offset)
21  {  {
22          int i;          int left = 0;
23          if (sid > 0)          int right = p_priv->s_count - 1;
24            int mid = 0;
25    
26            while (left < right)
27          {          {
28                  for (i = 0; i < p_priv->s_count; i++)                  mid = (left + right) / 2;
29    
30                    if (sid < p_priv->s_priv_list[mid].sid)
31                  {                  {
32                          if (p_priv->s_priv_list[i].sid == sid)                          right = mid - 1;
                         {  
                                 p_priv->s_priv_list[i].s_priv = priv;  
                                 return 0;  
                         }  
33                  }                  }
34                  if (i < BBS_max_section)                  else if (sid > p_priv->s_priv_list[mid].sid)
35                  {                  {
36                          p_priv->s_priv_list[i].s_priv = priv;                          left = mid + 1;
37                  }                  }
38                  else                  else // if (sid == p_priv->s_priv_list[mid].sid)
39                  {                  {
40                          return -1;                          left = mid;
41                            break;
42                  }                  }
43          }          }
44          else  
45            *p_offset = left;
46    
47            return (sid == p_priv->s_priv_list[left].sid);
48    }
49    
50    int setpriv(BBS_user_priv *p_priv, int sid, int priv, int is_favor)
51    {
52            int offset;
53            int i;
54    
55            if (sid == 0)
56          {          {
57                  p_priv->g_priv = priv;                  p_priv->g_priv = priv;
58                    return 0;
59            }
60    
61            if (search_priv(p_priv, sid, &offset)) // found
62            {
63                    p_priv->s_priv_list[offset].s_priv = priv;
64                    p_priv->s_priv_list[offset].is_favor = is_favor;
65                    return 0;
66            }
67    
68            // not found
69            if (p_priv->s_count >= BBS_max_section)
70            {
71                    return -1;
72            }
73    
74            // move items at [left, p_priv->s_count - 1] to [left + 1, p_priv->s_count]
75            for (i = p_priv->s_count - 1; i >= offset; i--)
76            {
77                    p_priv->s_priv_list[i + 1] = p_priv->s_priv_list[i];
78          }          }
79            p_priv->s_count++;
80    
81            // insert new item at offset left
82            p_priv->s_priv_list[offset].sid = sid;
83            p_priv->s_priv_list[offset].s_priv = priv;
84            p_priv->s_priv_list[offset].is_favor = is_favor;
85    
86          return 0;          return 0;
87  }  }
88    
89  int getpriv(BBS_user_priv *p_priv, int sid)  int getpriv(BBS_user_priv *p_priv, int sid, int *p_is_favor)
90  {  {
91          int i;          int offset;
92          for (i = 0; i < p_priv->s_count; i++)  
93            if (search_priv(p_priv, sid, &offset)) // found
94          {          {
95                  if (p_priv->s_priv_list[i].sid == sid)                  *p_is_favor = p_priv->s_priv_list[offset].is_favor;
96                          return p_priv->s_priv_list[i].s_priv;                  return p_priv->s_priv_list[offset].s_priv;
97          }          }
98    
99            *p_is_favor = 0;
100          return (sid >= 0 ? p_priv->g_priv : S_NONE);          return (sid >= 0 ? p_priv->g_priv : S_NONE);
101  }  }
102    
103  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)
104  {  {
105          MYSQL_RES *rs;          MYSQL_RES *rs;
106          MYSQL_ROW row;          MYSQL_ROW row;
107          char sql[SQL_BUFFER_LEN];          char sql[SQL_BUFFER_LEN];
108            int priv;
109            int is_favor;
110    
111          p_priv->uid = uid;          p_priv->uid = uid;
112          p_priv->level = (uid == 0 ? P_GUEST : P_USER);          p_priv->level = (uid == 0 ? P_GUEST : P_USER);
# Line 82  int load_priv(MYSQL *db, BBS_user_priv * Line 117  int load_priv(MYSQL *db, BBS_user_priv *
117                  return 1;                  return 1;
118    
119          // Permission          // Permission
120          snprintf(sql, sizeof(sql), "SELECT p_post, p_msg FROM user_list WHERE UID = %ld AND verified",          snprintf(sql, sizeof(sql),
121                          uid);                           "SELECT p_post, p_msg FROM user_list WHERE UID = %d AND verified",
122                             uid);
123          if (mysql_query(db, sql) != 0)          if (mysql_query(db, sql) != 0)
124          {          {
125                  log_error("Query user_list error: %s\n", mysql_error(db));                  log_error("Query user_list error: %s\n", mysql_error(db));
# Line 102  int load_priv(MYSQL *db, BBS_user_priv * Line 138  int load_priv(MYSQL *db, BBS_user_priv *
138          mysql_free_result(rs);          mysql_free_result(rs);
139    
140          // Admin          // Admin
141          snprintf(sql, sizeof(sql), "SELECT major FROM admin_config WHERE UID = %ld "          snprintf(sql, sizeof(sql),
142                                   "AND enable AND (NOW() BETWEEN begin_dt AND end_dt)",                           "SELECT major FROM admin_config WHERE UID = %d "
143                          uid);                           "AND enable AND (NOW() BETWEEN begin_dt AND end_dt)",
144                             uid);
145          if (mysql_query(db, sql) != 0)          if (mysql_query(db, sql) != 0)
146          {          {
147                  log_error("Query admin_config error: %s\n", mysql_error(db));                  log_error("Query admin_config error: %s\n", mysql_error(db));
# Line 123  int load_priv(MYSQL *db, BBS_user_priv * Line 160  int load_priv(MYSQL *db, BBS_user_priv *
160          mysql_free_result(rs);          mysql_free_result(rs);
161    
162          // Section Master          // Section Master
163          snprintf(sql, sizeof(sql), "SELECT section_master.SID, major FROM section_master "          snprintf(sql, sizeof(sql),
164                                   "INNER JOIN section_config ON section_master.SID = section_config.SID "                           "SELECT section_master.SID, major FROM section_master "
165                                   "WHERE UID = %ld AND section_master.enable AND section_config.enable "                           "INNER JOIN section_config ON section_master.SID = section_config.SID "
166                                   "AND (NOW() BETWEEN begin_dt AND end_dt)",                           "WHERE UID = %d AND section_master.enable AND section_config.enable "
167                          uid);                           "AND (NOW() BETWEEN begin_dt AND end_dt)",
168                             uid);
169          if (mysql_query(db, sql) != 0)          if (mysql_query(db, sql) != 0)
170          {          {
171                  log_error("Query section_master error: %s\n", mysql_error(db));                  log_error("Query section_master error: %s\n", mysql_error(db));
# Line 141  int load_priv(MYSQL *db, BBS_user_priv * Line 179  int load_priv(MYSQL *db, BBS_user_priv *
179          while ((row = mysql_fetch_row(rs)))          while ((row = mysql_fetch_row(rs)))
180          {          {
181                  p_priv->level |= (atoi(row[1]) ? P_MAN_M : P_MAN_S);                  p_priv->level |= (atoi(row[1]) ? P_MAN_M : P_MAN_S);
182                  setpriv(p_priv, atoi(row[0]), getpriv(p_priv, atoi(row[0])) | (atoi(row[1]) ? S_MAN_M : S_MAN_S));                  priv = (getpriv(p_priv, atoi(row[0]), &is_favor) | (atoi(row[1]) ? S_MAN_M : S_MAN_S));
183                    setpriv(p_priv, atoi(row[0]), priv, is_favor);
184          }          }
185          mysql_free_result(rs);          mysql_free_result(rs);
186    
187          // Section status          // Section status
188          snprintf(sql, sizeof(sql), "SELECT SID, exp_get, read_user_level, write_user_level FROM section_config "          snprintf(sql, sizeof(sql),
189                                   "INNER JOIN section_class ON section_config.CID = section_class.CID "                           "SELECT SID, exp_get, read_user_level, write_user_level FROM section_config "
190                                   "WHERE section_config.enable AND section_class.enable "                           "INNER JOIN section_class ON section_config.CID = section_class.CID "
191                                   "ORDER BY SID");                           "WHERE section_config.enable AND section_class.enable "
192                             "ORDER BY SID");
193          if (mysql_query(db, sql) != 0)          if (mysql_query(db, sql) != 0)
194          {          {
195                  log_error("Query section_config error: %s\n", mysql_error(db));                  log_error("Query section_config error: %s\n", mysql_error(db));
# Line 162  int load_priv(MYSQL *db, BBS_user_priv * Line 202  int load_priv(MYSQL *db, BBS_user_priv *
202          }          }
203          while ((row = mysql_fetch_row(rs)))          while ((row = mysql_fetch_row(rs)))
204          {          {
205                  int priv = getpriv(p_priv, atoi(row[0]));                  int priv = getpriv(p_priv, atoi(row[0]), &is_favor);
206                  if (p_priv->level < atoi(row[2]))                  if (p_priv->level < atoi(row[2]))
207                  {                  {
208                          priv &= (~S_LIST);                          priv &= (~S_LIST);
# Line 175  int load_priv(MYSQL *db, BBS_user_priv * Line 215  int load_priv(MYSQL *db, BBS_user_priv *
215                  {                  {
216                          priv &= (~S_GETEXP);                          priv &= (~S_GETEXP);
217                  }                  }
218                  setpriv(p_priv, atoi(row[0]), priv);                  setpriv(p_priv, atoi(row[0]), priv, is_favor);
219          }          }
220          mysql_free_result(rs);          mysql_free_result(rs);
221    
222          // Section ban          // Section ban
223          snprintf(sql, sizeof(sql), "SELECT SID FROM ban_user_list WHERE UID = %ld AND enable "          snprintf(sql, sizeof(sql),
224                                   "AND (NOW() BETWEEN ban_dt AND unban_dt)",                           "SELECT SID FROM ban_user_list WHERE UID = %d AND enable "
225                          uid);                           "AND (NOW() BETWEEN ban_dt AND unban_dt)",
226                             uid);
227          if (mysql_query(db, sql) != 0)          if (mysql_query(db, sql) != 0)
228          {          {
229                  log_error("Query ban_user_list error: %s\n", mysql_error(db));                  log_error("Query ban_user_list error: %s\n", mysql_error(db));
# Line 195  int load_priv(MYSQL *db, BBS_user_priv * Line 236  int load_priv(MYSQL *db, BBS_user_priv *
236          }          }
237          while ((row = mysql_fetch_row(rs)))          while ((row = mysql_fetch_row(rs)))
238          {          {
239                  setpriv(p_priv, atoi(row[0]),                  priv = getpriv(p_priv, atoi(row[0]), &is_favor) & (~S_POST);
240                                  getpriv(p_priv, atoi(row[0])) & (~S_POST));                  setpriv(p_priv, atoi(row[0]), priv, is_favor);
241            }
242            mysql_free_result(rs);
243    
244            // User favor section
245            snprintf(sql, sizeof(sql),
246                             "SELECT SID FROM section_favorite WHERE UID = %d",
247                             uid);
248            if (mysql_query(db, sql) != 0)
249            {
250                    log_error("Query section_favorite error: %s\n", mysql_error(db));
251                    return -1;
252            }
253            if ((rs = mysql_store_result(db)) == NULL)
254            {
255                    log_error("Get section_favorite data failed\n");
256                    return -1;
257            }
258            while ((row = mysql_fetch_row(rs)))
259            {
260                    priv = getpriv(p_priv, atoi(row[0]), &is_favor);
261                    if (!is_favor)
262                    {
263                            setpriv(p_priv, atoi(row[0]), priv, 1);
264                            priv = getpriv(p_priv, atoi(row[0]), &is_favor);
265                    }
266          }          }
267          mysql_free_result(rs);          mysql_free_result(rs);
268    


Legend:
Removed lines/characters  
Changed lines/characters
  Added lines/characters

webmaster@leafok.com
ViewVC Help
Powered by ViewVC 1.3.0-beta1