/[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.1 by sysadm, Fri Oct 22 15:21:28 2004 UTC Revision 1.15 by sysadm, Mon May 26 02:56:59 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    
17    #include "user_priv.h"
18  #include "bbs.h"  #include "bbs.h"
19    #include "common.h"
20    #include "database.h"
21    #include "log.h"
22    #include <stdio.h>
23  #include <mysql.h>  #include <mysql.h>
24    #include <stdlib.h>
25    
26  int  BBS_user_priv BBS_priv;
27  load_priv (MYSQL * db, BBS_user_priv * p_priv, int uid,  
28    int auth_uid, int priv_excluse)  int setpriv(BBS_user_priv *p_priv, int sid, int priv)
29    {
30            int i;
31            if (sid > 0)
32            {
33                    for (i = 0; i < p_priv->s_count; i++)
34                    {
35                            if (p_priv->s_priv_list[i].sid == sid)
36                            {
37                                    p_priv->s_priv_list[i].s_priv = priv;
38                                    return 0;
39                            }
40                    }
41                    if (i < BBS_max_section)
42                    {
43                            p_priv->s_priv_list[i].s_priv = priv;
44                    }
45                    else
46                    {
47                            return -1;
48                    }
49            }
50            else
51            {
52                    p_priv->g_priv = priv;
53            }
54    
55            return 0;
56    }
57    
58    int getpriv(BBS_user_priv *p_priv, int sid)
59    {
60            int i;
61            for (i = 0; i < p_priv->s_count; i++)
62            {
63                    if (p_priv->s_priv_list[i].sid == sid)
64                            return p_priv->s_priv_list[i].s_priv;
65            }
66    
67            return (sid >= 0 ? p_priv->g_priv : S_NONE);
68    }
69    
70    int load_priv(MYSQL *db, BBS_user_priv *p_priv, long int uid)
71  {  {
72            MYSQL_RES *rs;
73            MYSQL_ROW row;
74            char sql[SQL_BUFFER_LEN];
75    
76            p_priv->uid = uid;
77            p_priv->level = (uid == 0 ? P_GUEST : P_USER);
78            p_priv->g_priv = S_DEFAULT;
79            p_priv->s_count = 0;
80    
81            if (db == NULL)
82                    return 1;
83    
84            // Permission
85            snprintf(sql, sizeof(sql), "SELECT p_post, p_msg FROM user_list WHERE UID = %ld AND verified",
86                            uid);
87            if (mysql_query(db, sql) != 0)
88            {
89                    log_error("Query user_list error: %s\n", mysql_error(db));
90                    return -1;
91            }
92            if ((rs = mysql_store_result(db)) == NULL)
93            {
94                    log_error("Get user_list data failed\n");
95                    return -1;
96            }
97            if ((row = mysql_fetch_row(rs)))
98            {
99                    p_priv->g_priv |= (atoi(row[0]) ? S_POST : 0);
100                    p_priv->g_priv |= (atoi(row[1]) ? S_MSG : 0);
101            }
102            mysql_free_result(rs);
103    
104            // Admin
105            snprintf(sql, sizeof(sql), "SELECT major FROM admin_config WHERE UID = %ld "
106                                     "AND enable AND (NOW() BETWEEN begin_dt AND end_dt)",
107                            uid);
108            if (mysql_query(db, sql) != 0)
109            {
110                    log_error("Query admin_config error: %s\n", mysql_error(db));
111                    return -1;
112            }
113            if ((rs = mysql_store_result(db)) == NULL)
114            {
115                    log_error("Get admin_config data failed\n");
116                    return -1;
117            }
118            if ((row = mysql_fetch_row(rs)))
119            {
120                    p_priv->level |= (atoi(row[0]) ? P_ADMIN_M : P_ADMIN_S);
121                    p_priv->g_priv |= (atoi(row[0]) ? S_ALL : S_ADMIN);
122            }
123            mysql_free_result(rs);
124    
125            // Section Master
126            snprintf(sql, sizeof(sql), "SELECT section_master.SID, major FROM section_master "
127                                     "INNER JOIN section_config ON section_master.SID = section_config.SID "
128                                     "WHERE UID = %ld AND section_master.enable AND section_config.enable "
129                                     "AND (NOW() BETWEEN begin_dt AND end_dt)",
130                            uid);
131            if (mysql_query(db, sql) != 0)
132            {
133                    log_error("Query section_master error: %s\n", mysql_error(db));
134                    return -1;
135            }
136            if ((rs = mysql_store_result(db)) == NULL)
137            {
138                    log_error("Get section_master data failed\n");
139                    return -1;
140            }
141            while ((row = mysql_fetch_row(rs)))
142            {
143                    p_priv->level |= (atoi(row[1]) ? P_MAN_M : P_MAN_S);
144                    setpriv(p_priv, atoi(row[0]), getpriv(p_priv, atoi(row[0])) | (atoi(row[1]) ? S_MAN_M : S_MAN_S));
145            }
146            mysql_free_result(rs);
147    
148            // Section status
149            snprintf(sql, sizeof(sql), "SELECT SID, exp_get, read_user_level, write_user_level FROM section_config "
150                                     "INNER JOIN section_class ON section_config.CID = section_class.CID "
151                                     "WHERE section_config.enable AND section_class.enable "
152                                     "ORDER BY SID");
153            if (mysql_query(db, sql) != 0)
154            {
155                    log_error("Query section_config error: %s\n", mysql_error(db));
156                    return -1;
157            }
158            if ((rs = mysql_store_result(db)) == NULL)
159            {
160                    log_error("Get section_config data failed\n");
161                    return -1;
162            }
163            while ((row = mysql_fetch_row(rs)))
164            {
165                    int priv = getpriv(p_priv, atoi(row[0]));
166                    if (p_priv->level < atoi(row[2]))
167                    {
168                            priv &= (~S_LIST);
169                    }
170                    if (p_priv->level < atoi(row[3]))
171                    {
172                            priv &= (~S_POST);
173                    }
174                    if (!atoi(row[1]))
175                    {
176                            priv &= (~S_GETEXP);
177                    }
178                    setpriv(p_priv, atoi(row[0]), priv);
179            }
180            mysql_free_result(rs);
181    
182            // Section ban
183            snprintf(sql, sizeof(sql), "SELECT SID FROM ban_user_list WHERE UID = %ld AND enable "
184                                     "AND (NOW() BETWEEN ban_dt AND unban_dt)",
185                            uid);
186            if (mysql_query(db, sql) != 0)
187            {
188                    log_error("Query ban_user_list error: %s\n", mysql_error(db));
189                    return -1;
190            }
191            if ((rs = mysql_store_result(db)) == NULL)
192            {
193                    log_error("Get ban_user_list data failed\n");
194                    return -1;
195            }
196            while ((row = mysql_fetch_row(rs)))
197            {
198                    setpriv(p_priv, atoi(row[0]),
199                                    getpriv(p_priv, atoi(row[0])) & (~S_POST));
200            }
201            mysql_free_result(rs);
202    
203    return 0;          return 0;
204  }  }


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

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