/[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.9 by sysadm, Fri May 2 02:19:18 2025 UTC Revision 1.22 by sysadm, Fri Oct 24 02:07:01 2025 UTC
# Line 1  Line 1 
1  /***************************************************************************  /***************************************************************************
2                                                    user_priv.c  -  description                                                    user_priv.c  -  description
3                                                           -------------------                                                           -------------------
4          begin                : Mon Oct 22 2004          Copyright            : (C) 2004-2025 by Leaflet
5          copyright            : (C) 2004 by Leaflet          Email                : leaflet@leafok.com
         email                : leaflet@leafok.com  
6   ***************************************************************************/   ***************************************************************************/
7    
8  /***************************************************************************  /***************************************************************************
9   *                                                                         *   *                                                                         *
10   *   This program is free software; you can redistribute it and/or modify  *   *   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  *   *   it under the terms of the GNU General Public License as published by  *
12   *   the Free Software Foundation; either version 2 of the License, or     *   *   the Free Software Foundation; either version 3 of the License, or     *
13   *   (at your option) any later version.                                   *   *   (at your option) any later version.                                   *
14   *                                                                         *   *                                                                         *
15   ***************************************************************************/   ***************************************************************************/
16    
 #include "user_priv.h"  
17  #include "bbs.h"  #include "bbs.h"
18  #include "common.h"  #include "common.h"
19    #include "database.h"
20  #include "log.h"  #include "log.h"
21    #include "user_priv.h"
22  #include <stdio.h>  #include <stdio.h>
23  #include <mysql.h>  #include <stdlib.h>
24    #include <mysql/mysql.h>
25    
26  BBS_user_priv BBS_priv;  BBS_user_priv BBS_priv;
27    
28  int checklevel(BBS_user_priv *p_priv, int level)  inline static int search_priv(BBS_user_priv *p_priv, int sid, int *p_offset)
29  {  {
30          if (level == P_GUEST)          int left = 0;
31          {          int right = p_priv->s_count - 1;
32                  return 1;          int mid = 0;
         }  
   
         return ((p_priv->level & level) ? 1 : 0);  
 }  
33    
34  int setpriv(BBS_user_priv *p_priv, int sid, int priv)          while (left < right)
 {  
         int i;  
         if (sid > 0)  
35          {          {
36                  for (i = 0; i < p_priv->s_count; i++)                  mid = (left + right) / 2;
37    
38                    if (sid < p_priv->s_priv_list[mid].sid)
39                  {                  {
40                          if (p_priv->s_priv_list[i].sid == sid)                          right = mid - 1;
                         {  
                                 p_priv->s_priv_list[i].s_priv = priv;  
                                 return 0;  
                         }  
41                  }                  }
42                  if (i < BBS_max_section)                  else if (sid > p_priv->s_priv_list[mid].sid)
43                  {                  {
44                          p_priv->s_priv_list[i].s_priv = priv;                          left = mid + 1;
45                  }                  }
46                  else                  else // if (sid == p_priv->s_priv_list[mid].sid)
47                  {                  {
48                          return -1;                          left = mid;
49                            break;
50                  }                  }
51          }          }
52          else  
53            *p_offset = left;
54    
55            return (sid == p_priv->s_priv_list[left].sid);
56    }
57    
58    int setpriv(BBS_user_priv *p_priv, int sid, int priv, int is_favor)
59    {
60            int offset;
61            int i;
62    
63            if (sid == 0)
64          {          {
65                  p_priv->g_priv = priv;                  p_priv->g_priv = priv;
66                    return 0;
67            }
68    
69            if (search_priv(p_priv, sid, &offset)) //found
70            {
71                    p_priv->s_priv_list[offset].s_priv = priv;
72                    p_priv->s_priv_list[offset].is_favor = is_favor;
73                    return 0;
74            }
75    
76            // not found
77            if (p_priv->s_count >= BBS_max_section)
78            {
79                    return -1;
80          }          }
81    
82            // move items at [left, p_priv->s_count - 1] to [left + 1, p_priv->s_count]
83            for (i = p_priv->s_count - 1; i >= offset; i--)
84            {
85                    p_priv->s_priv_list[i + 1] = p_priv->s_priv_list[i];
86            }
87            p_priv->s_count++;
88    
89            // insert new item at offset left
90            p_priv->s_priv_list[offset].sid = sid;
91            p_priv->s_priv_list[offset].s_priv = priv;
92            p_priv->s_priv_list[offset].is_favor = is_favor;
93    
94          return 0;          return 0;
95  }  }
96    
97  int getpriv(BBS_user_priv *p_priv, int sid)  int getpriv(BBS_user_priv *p_priv, int sid, int *p_is_favor)
98  {  {
99          int i;          int offset;
100          for (i = 0; i < p_priv->s_count; i++)  
101            if (search_priv(p_priv, sid, &offset)) //found
102          {          {
103                  if (p_priv->s_priv_list[i].sid == sid)                  *p_is_favor = p_priv->s_priv_list[offset].is_favor;
104                          return p_priv->s_priv_list[i].s_priv;                  return p_priv->s_priv_list[offset].s_priv;
105          }          }
106    
107            *p_is_favor = 0;
108          return (sid >= 0 ? p_priv->g_priv : S_NONE);          return (sid >= 0 ? p_priv->g_priv : S_NONE);
109  }  }
110    
111  int checkpriv(BBS_user_priv *p_priv, int sid, int priv)  int load_priv(MYSQL *db, BBS_user_priv *p_priv, int uid)
 {  
         return (((getpriv(p_priv, sid) & priv)) == priv ? 1 : 0);  
 }  
   
 int load_priv(MYSQL *db, BBS_user_priv *p_priv, long int uid)  
112  {  {
113          MYSQL_RES *rs;          MYSQL_RES *rs;
114          MYSQL_ROW row;          MYSQL_ROW row;
115          char sql[1024];          char sql[SQL_BUFFER_LEN];
116          int i;          int priv;
117            int is_favor;
118    
119          p_priv->uid = uid;          p_priv->uid = uid;
120          p_priv->level = (uid == 0 ? P_GUEST : P_USER);          p_priv->level = (uid == 0 ? P_GUEST : P_USER);
121          p_priv->g_priv = S_DEFAULT;          p_priv->g_priv = S_DEFAULT;
122            p_priv->s_count = 0;
123    
124          if (db == NULL)          if (db == NULL)
125                  return 1;                  return 1;
126    
127          // Permission          // Permission
128          sprintf(sql, "SELECT p_post, p_msg FROM user_list WHERE UID = %ld AND verified",          snprintf(sql, sizeof(sql),
129                          uid);                           "SELECT p_post, p_msg FROM user_list WHERE UID = %d AND verified",
130                             uid);
131          if (mysql_query(db, sql) != 0)          if (mysql_query(db, sql) != 0)
132          {          {
133                  log_error("Query user_list failed\n");                  log_error("Query user_list error: %s\n", mysql_error(db));
134                  return -1;                  return -1;
135          }          }
136          if ((rs = mysql_store_result(db)) == NULL)          if ((rs = mysql_store_result(db)) == NULL)
# Line 108  int load_priv(MYSQL *db, BBS_user_priv * Line 138  int load_priv(MYSQL *db, BBS_user_priv *
138                  log_error("Get user_list data failed\n");                  log_error("Get user_list data failed\n");
139                  return -1;                  return -1;
140          }          }
141          if (row = mysql_fetch_row(rs))          if ((row = mysql_fetch_row(rs)))
142          {          {
143                  p_priv->g_priv |= (atoi(row[0]) ? S_POST : 0);                  p_priv->g_priv |= (atoi(row[0]) ? S_POST : 0);
144                  p_priv->g_priv |= (atoi(row[1]) ? S_MSG : 0);                  p_priv->g_priv |= (atoi(row[1]) ? S_MSG : 0);
# Line 116  int load_priv(MYSQL *db, BBS_user_priv * Line 146  int load_priv(MYSQL *db, BBS_user_priv *
146          mysql_free_result(rs);          mysql_free_result(rs);
147    
148          // Admin          // Admin
149          sprintf(sql, "SELECT major FROM admin_config WHERE UID = %ld "          snprintf(sql, sizeof(sql),
150                                   "AND enable AND (NOW() BETWEEN begin_dt AND end_dt)",                           "SELECT major FROM admin_config WHERE UID = %d "
151                          uid);                           "AND enable AND (NOW() BETWEEN begin_dt AND end_dt)",
152                             uid);
153          if (mysql_query(db, sql) != 0)          if (mysql_query(db, sql) != 0)
154          {          {
155                  log_error("Query admin_config failed\n");                  log_error("Query admin_config error: %s\n", mysql_error(db));
156                  return -1;                  return -1;
157          }          }
158          if ((rs = mysql_store_result(db)) == NULL)          if ((rs = mysql_store_result(db)) == NULL)
# Line 129  int load_priv(MYSQL *db, BBS_user_priv * Line 160  int load_priv(MYSQL *db, BBS_user_priv *
160                  log_error("Get admin_config data failed\n");                  log_error("Get admin_config data failed\n");
161                  return -1;                  return -1;
162          }          }
163          if (row = mysql_fetch_row(rs))          if ((row = mysql_fetch_row(rs)))
164          {          {
165                  p_priv->level |= (atoi(row[0]) ? P_ADMIN_M : P_ADMIN_S);                  p_priv->level |= (atoi(row[0]) ? P_ADMIN_M : P_ADMIN_S);
166                  p_priv->g_priv |= (atoi(row[0]) ? S_ALL : S_ADMIN);                  p_priv->g_priv |= (atoi(row[0]) ? S_ALL : S_ADMIN);
# Line 137  int load_priv(MYSQL *db, BBS_user_priv * Line 168  int load_priv(MYSQL *db, BBS_user_priv *
168          mysql_free_result(rs);          mysql_free_result(rs);
169    
170          // Section Master          // Section Master
171          sprintf(sql, "SELECT section_master.SID, major FROM section_master "          snprintf(sql, sizeof(sql),
172                                   "INNER JOIN section_config ON section_master.SID = section_config.SID "                           "SELECT section_master.SID, major FROM section_master "
173                                   "WHERE UID = %ld AND section_master.enable AND section_config.enable "                           "INNER JOIN section_config ON section_master.SID = section_config.SID "
174                                   "AND (NOW() BETWEEN begin_dt AND end_dt)",                           "WHERE UID = %d AND section_master.enable AND section_config.enable "
175                          uid);                           "AND (NOW() BETWEEN begin_dt AND end_dt)",
176                             uid);
177          if (mysql_query(db, sql) != 0)          if (mysql_query(db, sql) != 0)
178          {          {
179                  log_error("Query section_master failed\n");                  log_error("Query section_master error: %s\n", mysql_error(db));
180                  return -1;                  return -1;
181          }          }
182          if ((rs = mysql_store_result(db)) == NULL)          if ((rs = mysql_store_result(db)) == NULL)
# Line 152  int load_priv(MYSQL *db, BBS_user_priv * Line 184  int load_priv(MYSQL *db, BBS_user_priv *
184                  log_error("Get section_master data failed\n");                  log_error("Get section_master data failed\n");
185                  return -1;                  return -1;
186          }          }
187          while (row = mysql_fetch_row(rs))          while ((row = mysql_fetch_row(rs)))
188          {          {
189                  p_priv->level |= (atoi(row[1]) ? P_MAN_M : P_MAN_S);                  p_priv->level |= (atoi(row[1]) ? P_MAN_M : P_MAN_S);
190                  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));
191                    setpriv(p_priv, atoi(row[0]), priv, is_favor);
192          }          }
193          mysql_free_result(rs);          mysql_free_result(rs);
194    
195          // Section status          // Section status
196          sprintf(sql, "SELECT SID, exp_get, read_user_level, write_user_level FROM section_config "          snprintf(sql, sizeof(sql),
197                                   "INNER JOIN section_class ON section_config.CID = section_class.CID "                           "SELECT SID, exp_get, read_user_level, write_user_level FROM section_config "
198                                   "WHERE section_config.enable AND section_class.enable "                           "INNER JOIN section_class ON section_config.CID = section_class.CID "
199                                   "ORDER BY SID");                           "WHERE section_config.enable AND section_class.enable "
200                             "ORDER BY SID");
201          if (mysql_query(db, sql) != 0)          if (mysql_query(db, sql) != 0)
202          {          {
203                  log_error("Query section_config failed\n");                  log_error("Query section_config error: %s\n", mysql_error(db));
204                  return -1;                  return -1;
205          }          }
206          if ((rs = mysql_store_result(db)) == NULL)          if ((rs = mysql_store_result(db)) == NULL)
# Line 174  int load_priv(MYSQL *db, BBS_user_priv * Line 208  int load_priv(MYSQL *db, BBS_user_priv *
208                  log_error("Get section_config data failed\n");                  log_error("Get section_config data failed\n");
209                  return -1;                  return -1;
210          }          }
211          while (row = mysql_fetch_row(rs))          while ((row = mysql_fetch_row(rs)))
212          {          {
213                  int priv = getpriv(p_priv, atoi(row[0]));                  int priv = getpriv(p_priv, atoi(row[0]), &is_favor);
214                  if (p_priv->level < atoi(row[2]))                  if (p_priv->level < atoi(row[2]))
215                  {                  {
216                          priv &= (~S_LIST);                          priv &= (~S_LIST);
# Line 189  int load_priv(MYSQL *db, BBS_user_priv * Line 223  int load_priv(MYSQL *db, BBS_user_priv *
223                  {                  {
224                          priv &= (~S_GETEXP);                          priv &= (~S_GETEXP);
225                  }                  }
226                  setpriv(p_priv, atoi(row[0]), priv);                  setpriv(p_priv, atoi(row[0]), priv, is_favor);
227          }          }
228          mysql_free_result(rs);          mysql_free_result(rs);
229    
230          // Section ban          // Section ban
231          sprintf(sql, "SELECT SID FROM ban_user_list WHERE UID = %ld AND enable "          snprintf(sql, sizeof(sql),
232                                   "AND (NOW() BETWEEN ban_dt AND unban_dt)",                           "SELECT SID FROM ban_user_list WHERE UID = %d AND enable "
233                          uid);                           "AND (NOW() BETWEEN ban_dt AND unban_dt)",
234                             uid);
235          if (mysql_query(db, sql) != 0)          if (mysql_query(db, sql) != 0)
236          {          {
237                  log_error("Query ban_user_list failed\n");                  log_error("Query ban_user_list error: %s\n", mysql_error(db));
238                  return -1;                  return -1;
239          }          }
240          if ((rs = mysql_store_result(db)) == NULL)          if ((rs = mysql_store_result(db)) == NULL)
# Line 207  int load_priv(MYSQL *db, BBS_user_priv * Line 242  int load_priv(MYSQL *db, BBS_user_priv *
242                  log_error("Get ban_user_list data failed\n");                  log_error("Get ban_user_list data failed\n");
243                  return -1;                  return -1;
244          }          }
245          while (row = mysql_fetch_row(rs))          while ((row = mysql_fetch_row(rs)))
246            {
247                    priv = getpriv(p_priv, atoi(row[0]), &is_favor) & (~S_POST);
248                    setpriv(p_priv, atoi(row[0]), priv, is_favor);
249            }
250            mysql_free_result(rs);
251    
252            // User favor section
253            snprintf(sql, sizeof(sql),
254                             "SELECT SID FROM section_favorite WHERE UID = %d",
255                             uid);
256            if (mysql_query(db, sql) != 0)
257            {
258                    log_error("Query section_favorite error: %s\n", mysql_error(db));
259                    return -1;
260            }
261            if ((rs = mysql_store_result(db)) == NULL)
262            {
263                    log_error("Get section_favorite data failed\n");
264                    return -1;
265            }
266            while ((row = mysql_fetch_row(rs)))
267          {          {
268                  setpriv(p_priv, atoi(row[0]),                  priv = getpriv(p_priv, atoi(row[0]), &is_favor);
269                                  getpriv(p_priv, atoi(row[0])) & (~S_POST));                  if (!is_favor)
270                    {
271                            setpriv(p_priv, atoi(row[0]), priv, 1);
272                            priv = getpriv(p_priv, atoi(row[0]), &is_favor);
273                    }
274          }          }
275          mysql_free_result(rs);          mysql_free_result(rs);
276    


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

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