-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathswad_session_database.c
343 lines (300 loc) · 12.9 KB
/
swad_session_database.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
// swad_session_database.c: sessions operations with database
/*
SWAD (Shared Workspace At a Distance),
is a web platform developed at the University of Granada (Spain),
and used to support university teaching.
This file is part of SWAD core.
Copyright (C) 1999-2025 Antonio Cañas Vargas
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/*****************************************************************************/
/************************************ Headers ********************************/
/*****************************************************************************/
#include "swad_config.h"
#include "swad_database.h"
#include "swad_global.h"
#include "swad_pagination.h"
/*****************************************************************************/
/************** External global variables from others modules ****************/
/*****************************************************************************/
extern struct Globals Gbl;
/*****************************************************************************/
/******************** Insert new session in the database *********************/
/*****************************************************************************/
void Ses_DB_InsertSession (Sch_WhatToSearch_t WhatToSearch)
{
DB_QueryINSERT ("can not create session",
"INSERT INTO ses_sessions"
" (SessionId,UsrCod,Password,Role,"
"CtyCod,InsCod,CtrCod,DegCod,CrsCod,LastTime,LastRefresh,WhatToSearch)"
" VALUES"
" ('%s',%ld,'%s',%u,"
"%ld,%ld,%ld,%ld,%ld,NOW(),NOW(),%u)",
Gbl.Session.Id,
Gbl.Usrs.Me.UsrDat.UsrCod,
Gbl.Usrs.Me.UsrDat.Password,
(unsigned) Gbl.Usrs.Me.Role.Logged,
Gbl.Hierarchy.Node[Hie_CTY].HieCod,
Gbl.Hierarchy.Node[Hie_INS].HieCod,
Gbl.Hierarchy.Node[Hie_CTR].HieCod,
Gbl.Hierarchy.Node[Hie_DEG].HieCod,
Gbl.Hierarchy.Node[Hie_CRS].HieCod,
WhatToSearch);
}
/*****************************************************************************/
/***************** Modify data of session in the database ********************/
/*****************************************************************************/
void Ses_DB_UpdateSession (void)
{
DB_QueryUPDATE ("can not update session",
"UPDATE ses_sessions"
" SET UsrCod=%ld,"
"Password='%s',"
"Role=%u,"
"CtyCod=%ld,"
"InsCod=%ld,"
"CtrCod=%ld,"
"DegCod=%ld,"
"CrsCod=%ld,"
"LastTime=NOW(),"
"LastRefresh=NOW()"
" WHERE SessionId='%s'",
Gbl.Usrs.Me.UsrDat.UsrCod,
Gbl.Usrs.Me.UsrDat.Password,
(unsigned) Gbl.Usrs.Me.Role.Logged,
Gbl.Hierarchy.Node[Hie_CTY].HieCod,
Gbl.Hierarchy.Node[Hie_INS].HieCod,
Gbl.Hierarchy.Node[Hie_CTR].HieCod,
Gbl.Hierarchy.Node[Hie_DEG].HieCod,
Gbl.Hierarchy.Node[Hie_CRS].HieCod,
Gbl.Session.Id);
}
/*****************************************************************************/
/********************** Save last search into session ************************/
/*****************************************************************************/
void Ses_DB_SaveLastSearchIntoSession (const struct Sch_Search *Search)
{
DB_QueryUPDATE ("can not update last search in session",
"UPDATE ses_sessions"
" SET WhatToSearch=%u,"
"SearchStr='%s'"
" WHERE SessionId='%s'",
(unsigned) Search->WhatToSearch,
Search->Str,
Gbl.Session.Id);
}
/*****************************************************************************/
/********* Save last page of received/sent messages into session *************/
/*****************************************************************************/
void Ses_DB_SaveLastPageMsgIntoSession (Pag_WhatPaginate_t WhatPaginate,unsigned NumPage)
{
/***** Save last page of received/sent messages *****/
DB_QueryUPDATE ("can not update last page of messages",
"UPDATE ses_sessions"
" SET %s=%u"
" WHERE SessionId='%s'",
WhatPaginate == Pag_MESSAGES_RECEIVED ? "LastPageMsgRcv" :
"LastPageMsgSnt",
NumPage,Gbl.Session.Id);
}
/*****************************************************************************/
/******************** Update session last refresh in database ****************/
/*****************************************************************************/
void Ses_DB_UpdateSessionLastRefresh (void)
{
DB_QueryUPDATE ("can not update session",
"UPDATE ses_sessions"
" SET LastRefresh=NOW()"
" WHERE SessionId='%s'",
Gbl.Session.Id);
}
/*****************************************************************************/
/*********** Check if the session already exists in the database *************/
/*****************************************************************************/
// Return true if session exists
// Return false if session does not exist or error
bool Ses_DB_CheckIfSessionExists (const char *IdSes)
{
return
DB_QueryEXISTS ("can not check if a session already existed",
"SELECT EXISTS"
"(SELECT *"
" FROM ses_sessions"
" WHERE SessionId='%s')",
IdSes);
}
/*****************************************************************************/
/******* Get the data (user code and password) of an initiated session *******/
/*****************************************************************************/
unsigned Ses_DB_GetSessionData (MYSQL_RES **mysql_res)
{
return (unsigned)
DB_QuerySELECT (mysql_res,"can not get session data",
"SELECT UsrCod," // row[0]
"Password," // row[1]
"Role," // row[2]
"CtyCod," // row[3]
"InsCod," // row[4]
"CtrCod," // row[5]
"DegCod," // row[6]
"CrsCod," // row[7]
"WhatToSearch," // row[8]
"SearchStr" // row[9]
" FROM ses_sessions"
" WHERE SessionId='%s'",
Gbl.Session.Id);
}
/*****************************************************************************/
/************************* Get some data of a session ************************/
/*****************************************************************************/
unsigned Ses_DB_GetSomeSessionData (MYSQL_RES **mysql_res,const char *SessionId)
{
return (unsigned)
DB_QuerySELECT (mysql_res,"can not get session data",
"SELECT UsrCod," // row[0]
"DegCod," // row[1]
"CrsCod" // row[2]
" FROM ses_sessions"
" WHERE SessionId='%s'",
SessionId);
}
/*****************************************************************************/
/********* Get last page of received/sent messages stored in session *********/
/*****************************************************************************/
unsigned Ses_DB_GetLastPageMsgFromSession (Pag_WhatPaginate_t WhatPaginate)
{
static const char *Field[Pag_NUM_WHAT_PAGINATE] =
{
[Pag_MESSAGES_RECEIVED] = "LastPageMsgRcv",
[Pag_MESSAGES_SENT ] = "LastPageMsgSnt",
};
return DB_QuerySELECTUnsigned ("can not get last page of messages",
"SELECT %s"
" FROM ses_sessions"
" WHERE SessionId='%s'",
Field[WhatPaginate],
Gbl.Session.Id);
}
/*****************************************************************************/
/*************************** Remove expired sessions *************************/
/*****************************************************************************/
void Ses_DB_RemoveExpiredSessions (void)
{
/***** Remove expired sessions *****/
/* A session expire
when last click (LastTime) is too old,
or (when there was at least one refresh (navigator supports AJAX)
and last refresh is too old (browser probably was closed)) */
DB_QueryDELETE ("can not remove expired sessions",
"DELETE LOW_PRIORITY FROM ses_sessions"
" WHERE LastTime<FROM_UNIXTIME(UNIX_TIMESTAMP()-%lu)"
" OR "
"(LastRefresh>LastTime+INTERVAL 1 SECOND"
" AND"
" LastRefresh<FROM_UNIXTIME(UNIX_TIMESTAMP()-%lu))",
Cfg_TIME_TO_CLOSE_SESSION_FROM_LAST_CLICK,
Cfg_TIME_TO_CLOSE_SESSION_FROM_LAST_REFRESH);
}
/*****************************************************************************/
/******************* Remove all sessions of a given user *********************/
/*****************************************************************************/
void Ses_DB_RemoveUsrSessions (long UsrCod)
{
DB_QueryDELETE ("can not remove sessions of a user",
"DELETE FROM ses_sessions"
" WHERE UsrCod=%ld",
UsrCod);
}
/*****************************************************************************/
/************************** Remove current session ***************************/
/*****************************************************************************/
void Ses_SB_RemoveCurrentSession (void)
{
/***** Remove current session *****/
DB_QueryDELETE ("can not remove a session",
"DELETE FROM ses_sessions"
" WHERE SessionId='%s'",
Gbl.Session.Id);
}
/*****************************************************************************/
/******************* Insert session parameter in the database ****************/
/*****************************************************************************/
void Ses_DB_InsertPar (const char *ParName,const char *ParValue)
{
DB_QueryINSERT ("can not create session parameter",
"INSERT INTO ses_params"
" (SessionId,ParamName,ParamValue)"
" VALUES"
" ('%s','%s','%s')",
Gbl.Session.Id,
ParName,
ParValue ? ParValue :
"");
}
/*****************************************************************************/
/************** Check if a session parameter existed in database *************/
/*****************************************************************************/
// Return true if the parameter already existed in database
bool Ses_DB_CheckIfParIsAlreadyStored (const char *ParName)
{
return
DB_QueryEXISTS ("can not check if a session parameter is already in database",
"SELECT EXISTS"
"(SELECT *"
" FROM ses_params"
" WHERE SessionId='%s'"
" AND ParamName='%s')",
Gbl.Session.Id,
ParName);
}
/*****************************************************************************/
/***************** Get session parameter from the database *******************/
/*****************************************************************************/
// StrSize is the size of the parameter value, not including the ending '\0'
void Ses_DB_GetPar (const char *ParName,char *ParValue,size_t StrSize)
{
ParValue[0] = '\0';
if (Gbl.Session.IsOpen) // If the session is open, get parameter from DB
/***** Get a session parameter from database *****/
DB_QuerySELECTString (ParValue,StrSize,"can not get a session parameter",
"SELECT ParamValue" // row[0]
" FROM ses_params"
" WHERE SessionId='%s'"
" AND ParamName='%s'",
Gbl.Session.Id,
ParName);
}
/*****************************************************************************/
/************ Remove session parameters of a session from database ***********/
/*****************************************************************************/
void Ses_DB_RemovePar (void)
{
if (Gbl.Session.IsOpen && // There is an open session
!Gbl.Session.ParsInsertedIntoDB) // No params just inserted
/***** Remove session parameters of this session *****/
DB_QueryDELETE ("can not remove session parameters of current session",
"DELETE FROM ses_params"
" WHERE SessionId='%s'",
Gbl.Session.Id);
}
/*****************************************************************************/
/********* Remove expired hidden parameters (from expired sessions) **********/
/*****************************************************************************/
void Ses_DB_RemoveParsFromExpiredSessions (void)
{
/***** Remove session parameters from expired sessions *****/
DB_QueryDELETE ("can not remove session parameters of expired sessions",
"DELETE FROM ses_params"
" WHERE SessionId NOT IN"
" (SELECT SessionId"
" FROM ses_sessions)");
}