Version 21.34: Oct 15, 2021 New module swad_session_database for database queries related to sessions.

This commit is contained in:
acanas 2021-10-15 18:01:53 +02:00
parent b0f3d80101
commit e9607cb177
15 changed files with 428 additions and 266 deletions

View File

@ -78,8 +78,9 @@ OBJS = swad_account.o swad_account_database.o swad_action.o swad_admin.o \
swad_record.o swad_record_database.o swad_report.o \
swad_report_database.o swad_role.o swad_role_database.o swad_room.o \
swad_room_database.o swad_RSS.o \
swad_scope.o swad_search.o swad_session.o swad_setting.o \
swad_statistic.o swad_string.o swad_survey.o swad_syllabus.o \
swad_scope.o swad_search.o swad_session.o swad_session_database.o \
swad_setting.o swad_statistic.o swad_string.o swad_survey.o \
swad_syllabus.o \
swad_system_config.o \
swad_tab.o swad_tag.o swad_test.o swad_test_config.o \
swad_test_import.o swad_test_print.o swad_test_visibility.o \

View File

@ -66,6 +66,7 @@
#include "swad_project.h"
#include "swad_record_database.h"
#include "swad_report.h"
#include "swad_session_database.h"
#include "swad_setting.h"
#include "swad_test_print.h"
#include "swad_timeline.h"

View File

@ -602,13 +602,14 @@ TODO: FIX BUG, URGENT! En las fechas como par
TODO: En las encuestas, que los estudiantes no puedan ver los resultados hasta que no finalice el plazo.
*/
#define Log_PLATFORM_VERSION "SWAD 21.33.2 (2021-10-15)"
#define Log_PLATFORM_VERSION "SWAD 21.34 (2021-10-15)"
#define CSS_FILE "swad20.45.css"
#define JS_FILE "swad20.69.1.js"
/*
TODO: Rename CENTRE to CENTER in help wiki.
TODO: Rename ASSESSMENT.Announcements to ASSESSMENT.Calls_for_exams
Version 21.34: Oct 15, 2021 New module swad_session_database for database queries related to sessions. (320081 lines)
Version 21.33.2: Oct 15, 2021 Fixed bug getting name of degree. (319950 lines)
Version 21.33.1: Oct 15, 2021 Number of users/courses to show big-list warning increased from 500 to 1000. (319944 lines)
Version 21.33: Oct 15, 2021 Queries moved from search to other modules. (319942 lines)

View File

@ -44,6 +44,7 @@
#include "swad_MFU.h"
#include "swad_notification.h"
#include "swad_parameter.h"
#include "swad_session_database.h"
#include "swad_setting.h"
#include "swad_user.h"

View File

@ -1506,7 +1506,7 @@ static void Med_ShowJPG (const struct Med_Media *Media,
if (Fil_CheckIfPathExists (FullPathJPGPriv))
{
/***** Get cached public link to private file *****/
Cached = Ses_GetPublicDirFromCache (FullPathJPGPriv,TmpPubDir);
Cached = Brw_GetPublicDirFromCache (FullPathJPGPriv,TmpPubDir);
if (!Cached)
{
@ -1517,7 +1517,7 @@ static void Med_ShowJPG (const struct Med_Media *Media,
snprintf (TmpPubDir,sizeof (TmpPubDir),"%s/%s",
Gbl.FileBrowser.TmpPubDir.L,Gbl.FileBrowser.TmpPubDir.R);
Ses_AddPublicDirToCache (FullPathJPGPriv,TmpPubDir);
Brw_AddPublicDirToCache (FullPathJPGPriv,TmpPubDir);
}
/***** Show media *****/
@ -1570,7 +1570,7 @@ static void Med_ShowGIF (const struct Med_Media *Media,
if (Fil_CheckIfPathExists (FullPathGIFPriv)) // The animated GIF image
{
/***** Get cached public link to private file *****/
Cached = Ses_GetPublicDirFromCache (FullPathGIFPriv,TmpPubDir);
Cached = Brw_GetPublicDirFromCache (FullPathGIFPriv,TmpPubDir);
if (!Cached)
{
@ -1582,7 +1582,7 @@ static void Med_ShowGIF (const struct Med_Media *Media,
snprintf (TmpPubDir,sizeof (TmpPubDir),"%s/%s",
Gbl.FileBrowser.TmpPubDir.L,Gbl.FileBrowser.TmpPubDir.R);
Ses_AddPublicDirToCache (FullPathGIFPriv,TmpPubDir);
Brw_AddPublicDirToCache (FullPathGIFPriv,TmpPubDir);
}
/***** Create URLs pointing to symbolic links *****/
@ -1658,7 +1658,7 @@ static void Med_ShowVideo (const struct Med_Media *Media,
if (Fil_CheckIfPathExists (FullPathVideoPriv))
{
/***** Get cached public link to private file *****/
Cached = Ses_GetPublicDirFromCache (FullPathVideoPriv,TmpPubDir);
Cached = Brw_GetPublicDirFromCache (FullPathVideoPriv,TmpPubDir);
if (!Cached)
{
@ -1669,7 +1669,7 @@ static void Med_ShowVideo (const struct Med_Media *Media,
snprintf (TmpPubDir,sizeof (TmpPubDir),"%s/%s",
Gbl.FileBrowser.TmpPubDir.L,Gbl.FileBrowser.TmpPubDir.R);
Ses_AddPublicDirToCache (FullPathVideoPriv,TmpPubDir);
Brw_AddPublicDirToCache (FullPathVideoPriv,TmpPubDir);
}
/***** Create URL pointing to symbolic link *****/

View File

@ -58,6 +58,7 @@
#include "swad_photo.h"
#include "swad_profile.h"
#include "swad_profile_database.h"
#include "swad_session_database.h"
#include "swad_user.h"
/*****************************************************************************/

View File

@ -46,6 +46,7 @@
#include "swad_parameter.h"
#include "swad_program.h"
#include "swad_project.h"
#include "swad_session_database.h"
#include "swad_survey.h"
/*****************************************************************************/

View File

@ -45,6 +45,7 @@
#include "swad_password.h"
#include "swad_password_database.h"
#include "swad_parameter.h"
#include "swad_session_database.h"
#include "swad_user.h"
/*****************************************************************************/
@ -217,7 +218,7 @@ static void Pwd_CheckAndUpdateNewPwd (struct UsrData *UsrDat)
/* Update user's data */
Str_Copy (UsrDat->Password,NewEncryptedPassword,
sizeof (UsrDat->Password) - 1);
Ses_UpdateSessionDataInDB ();
Ses_DB_UpdateSession ();
Enr_UpdateUsrData (UsrDat);
Ale_CreateAlert (Ale_SUCCESS,Pwd_PASSWORD_SECTION_ID,

View File

@ -35,6 +35,7 @@
#include "swad_role.h"
#include "swad_role_database.h"
#include "swad_role_type.h"
#include "swad_session_database.h"
/*****************************************************************************/
/****************************** Public constants *****************************/
@ -543,7 +544,7 @@ void Rol_ChangeMyRole (void)
Gbl.Usrs.Me.Role.HasChanged = true;
/* ...update logged role in session... */
Ses_UpdateSessionDataInDB ();
Ses_DB_UpdateSession ();
/* ...and update logged role in list of connected */
Con_DB_UpdateMeInConnectedList ();

View File

@ -41,6 +41,7 @@
#include "swad_institution_database.h"
#include "swad_layout.h"
#include "swad_parameter.h"
#include "swad_session_database.h"
#include "swad_user.h"
/*****************************************************************************/

View File

@ -36,6 +36,7 @@
#include "swad_global.h"
#include "swad_pagination.h"
#include "swad_parameter.h"
#include "swad_session_database.h"
#include "swad_timeline_database.h"
/*****************************************************************************/
@ -56,7 +57,7 @@ static void Ses_RemoveSessionFromDB (void);
static bool Ses_CheckIfParamIsAlreadyInDB (const char *ParamName);
static void Ses_DeletePublicDirFromCache (const char *FullPathMediaPriv);
static void Brw_DB_DeletePublicDirFromCache (const char *FullPathMediaPriv);
/*****************************************************************************/
/************************** Get number of open sessions **********************/
@ -85,11 +86,11 @@ void Ses_CreateSession (void)
Str_Copy (Gbl.Session.Id,Gbl.UniqueNameEncrypted,sizeof (Gbl.Session.Id) - 1);
/***** Check that session is not open *****/
if (Ses_CheckIfSessionExists (Gbl.Session.Id))
if (Ses_DB_CheckIfSessionExists (Gbl.Session.Id))
Err_ShowErrorAndExit ("Can not create session.");
/***** Add session to database *****/
Ses_InsertSessionInDB ();
Ses_DB_InsertSession ();
/***** Update time and course in connected list *****/
Con_DB_UpdateMeInConnectedList ();
@ -98,22 +99,6 @@ void Ses_CreateSession (void)
Ses_GetNumSessions ();
}
/*****************************************************************************/
/*********** 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_CheckIfSessionExists (const char *IdSes)
{
/***** Get if session already exists in database *****/
return (DB_QueryCOUNT ("can not check if a session already existed",
"SELECT COUNT(*)"
" FROM ses_sessions"
" WHERE SessionId='%s'",
IdSes) != 0);
}
/*****************************************************************************/
/************************** Close current session ****************************/
/*****************************************************************************/
@ -123,7 +108,7 @@ void Ses_CloseSession (void)
if (Gbl.Usrs.Me.Logged)
{
/***** Remove links to private files from cache *****/
Ses_RemovePublicDirsCache ();
Brw_DB_RemovePublicDirsCache ();
/***** Remove session from database *****/
Ses_RemoveSessionFromDB ();
@ -136,7 +121,7 @@ void Ses_CloseSession (void)
/***** Remove unused data associated to expired sessions *****/
Ses_RemoveParamsFromExpiredSessions ();
Ses_RemovePublicDirsFromExpiredSessions ();
Brw_DB_RemovePublicDirsFromExpiredSessions ();
/***** Now, user is not logged in *****/
Gbl.Usrs.Me.Role.LoggedBeforeCloseSession = Gbl.Usrs.Me.Role.Logged;
@ -154,79 +139,6 @@ void Ses_CloseSession (void)
}
}
/*****************************************************************************/
/******************** Insert new session in the database *********************/
/*****************************************************************************/
void Ses_InsertSessionInDB (void)
{
/***** Insert session in the database *****/
if (Gbl.Search.WhatToSearch == Sch_SEARCH_UNKNOWN)
Gbl.Search.WhatToSearch = Sch_WHAT_TO_SEARCH_DEFAULT;
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.Cty.CtyCod,
Gbl.Hierarchy.Ins.InsCod,
Gbl.Hierarchy.Ctr.CtrCod,
Gbl.Hierarchy.Deg.DegCod,
Gbl.Hierarchy.Crs.CrsCod,
Gbl.Search.WhatToSearch);
}
/*****************************************************************************/
/***************** Modify data of session in the database ********************/
/*****************************************************************************/
void Ses_UpdateSessionDataInDB (void)
{
/***** Update session in database *****/
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.Cty.CtyCod,
Gbl.Hierarchy.Ins.InsCod,
Gbl.Hierarchy.Ctr.CtrCod,
Gbl.Hierarchy.Deg.DegCod,
Gbl.Hierarchy.Crs.CrsCod,
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);
}
/*****************************************************************************/
/********************** Remove session from the database *********************/
/*****************************************************************************/
@ -234,50 +146,13 @@ void Ses_DB_UpdateSessionLastRefresh (void)
static void Ses_RemoveSessionFromDB (void)
{
/***** Remove current session *****/
DB_QueryDELETE ("can not remove a session",
"DELETE FROM ses_sessions"
" WHERE SessionId='%s'",
Gbl.Session.Id);
Ses_SB_RemoveCurrentSession ();
/***** Clear old unused social timelines in database *****/
// This is necessary to prevent the table growing and growing
Tml_DB_ClearOldTimelinesNotesFromDB ();
}
/*****************************************************************************/
/*************************** 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);
}
/*****************************************************************************/
/******* Get the data (user code and password) of an initiated session *******/
/*****************************************************************************/
@ -290,20 +165,7 @@ bool Ses_GetSessionData (void)
bool Result = false;
/***** Check if the session existed in the database *****/
if (DB_QuerySELECT (&mysql_res,"can not get data of session",
"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))
if (Ses_DB_GetSessionData (&mysql_res))
{
row = mysql_fetch_row (mysql_res);
@ -318,19 +180,15 @@ bool Ses_GetSessionData (void)
if (sscanf (row[2],"%u",&Gbl.Usrs.Me.Role.FromSession) != 1)
Gbl.Usrs.Me.Role.FromSession = Rol_UNK;
/***** Get country code (row[3]) *****/
/***** Get country code (row[3]),
institution code (row[4]),
center code (row[5]),
degree code (row[6]),
and course code (row[7]) *****/
Gbl.Hierarchy.Cty.CtyCod = Str_ConvertStrCodToLongCod (row[3]);
/***** Get institution code (row[4]) *****/
Gbl.Hierarchy.Ins.InsCod = Str_ConvertStrCodToLongCod (row[4]);
/***** Get center code (row[5]) *****/
Gbl.Hierarchy.Ctr.CtrCod = Str_ConvertStrCodToLongCod (row[5]);
/***** Get degree code (row[6]) *****/
Gbl.Hierarchy.Deg.DegCod = Str_ConvertStrCodToLongCod (row[6]);
/***** Get course code (row[7]) *****/
Gbl.Hierarchy.Crs.CrsCod = Str_ConvertStrCodToLongCod (row[7]);
/***** Get last search *****/
@ -374,15 +232,7 @@ void Ses_InsertParamInDB (const char *ParamName,const char *ParamValue)
if (!Ses_CheckIfParamIsAlreadyInDB (ParamName))
{
/***** Insert session parameter in the database *****/
DB_QueryINSERT ("can not create session parameter",
"INSERT INTO ses_params"
" (SessionId,ParamName,ParamValue)"
" VALUES"
" ('%s','%s','%s')",
Gbl.Session.Id,
ParamName,
ParamValue ? ParamValue :
"");
Ses_DB_InsertParam (ParamName,ParamValue);
Gbl.Session.ParamsInsertedIntoDB = true;
}
}
@ -456,7 +306,7 @@ void Ses_GetParamFromDB (const char *ParamName,char *ParamValue,size_t StrSize)
/******** Get public directory used to link private path from cache **********/
/*****************************************************************************/
bool Ses_GetPublicDirFromCache (const char *FullPathMediaPriv,
bool Brw_GetPublicDirFromCache (const char *FullPathMediaPriv,
char TmpPubDir[PATH_MAX + 1])
{
bool Cached;
@ -468,13 +318,7 @@ bool Ses_GetPublicDirFromCache (const char *FullPathMediaPriv,
if (Gbl.Session.IsOpen)
{
/***** Get temporary directory from cache *****/
DB_QuerySELECTString (TmpPubDir,PATH_MAX,"can not get check if file is cached",
"SELECT TmpPubDir"
" FROM brw_caches"
" WHERE SessionId='%s'"
" AND PrivPath='%s'",
Gbl.Session.Id,
FullPathMediaPriv);
Brw_DB_GetPublicDirFromCache (FullPathMediaPriv,TmpPubDir);
Cached = (TmpPubDir[0] != '\0');
/***** Check if temporary public directory exists *****/
@ -484,7 +328,7 @@ bool Ses_GetPublicDirFromCache (const char *FullPathMediaPriv,
==> remove from cache */
TmpPubDirExists = Fil_CheckIfPathExists (TmpPubDir);
if (!TmpPubDirExists)
Ses_DeletePublicDirFromCache (FullPathMediaPriv);
Brw_DB_DeletePublicDirFromCache (FullPathMediaPriv);
return TmpPubDirExists;
}
}
@ -492,11 +336,27 @@ bool Ses_GetPublicDirFromCache (const char *FullPathMediaPriv,
return false;
}
/*****************************************************************************/
/******** Get public directory used to link private path from cache **********/
/*****************************************************************************/
void Brw_DB_GetPublicDirFromCache (const char *FullPathMediaPriv,
char TmpPubDir[PATH_MAX + 1])
{
DB_QuerySELECTString (TmpPubDir,PATH_MAX,"can not get check if file is cached",
"SELECT TmpPubDir"
" FROM brw_caches"
" WHERE SessionId='%s'"
" AND PrivPath='%s'",
Gbl.Session.Id,
FullPathMediaPriv);
}
/*****************************************************************************/
/********* Add public directory used to link private path to cache ***********/
/*****************************************************************************/
static void Ses_DeletePublicDirFromCache (const char *FullPathMediaPriv)
static void Brw_DB_DeletePublicDirFromCache (const char *FullPathMediaPriv)
{
/***** Delete possible entry *****/
if (Gbl.Session.IsOpen)
@ -511,32 +371,42 @@ static void Ses_DeletePublicDirFromCache (const char *FullPathMediaPriv)
/********* Add public directory used to link private path to cache ***********/
/*****************************************************************************/
void Ses_AddPublicDirToCache (const char *FullPathMediaPriv,
void Brw_AddPublicDirToCache (const char *FullPathMediaPriv,
const char TmpPubDir[PATH_MAX + 1])
{
/***** Insert into cache *****/
if (Gbl.Session.IsOpen)
{
/* Delete possible old entry */
Ses_DeletePublicDirFromCache (FullPathMediaPriv);
Brw_DB_DeletePublicDirFromCache (FullPathMediaPriv);
/* Insert new entry */
DB_QueryINSERT ("can not cache file",
"INSERT INTO brw_caches"
" (SessionId,PrivPath,TmpPubDir)"
" VALUES"
" ('%s','%s','%s')",
Gbl.Session.Id,
FullPathMediaPriv,
TmpPubDir);
Brw_DB_AddPublicDirToCache (FullPathMediaPriv,TmpPubDir);
}
}
/*****************************************************************************/
/********* Add public directory used to link private path to cache ***********/
/*****************************************************************************/
void Brw_DB_AddPublicDirToCache (const char *FullPathMediaPriv,
const char TmpPubDir[PATH_MAX + 1])
{
/* Insert new entry */
DB_QueryINSERT ("can not cache file",
"INSERT INTO brw_caches"
" (SessionId,PrivPath,TmpPubDir)"
" VALUES"
" ('%s','%s','%s')",
Gbl.Session.Id,
FullPathMediaPriv,
TmpPubDir);
}
/*****************************************************************************/
/****** Remove public directories used to link private paths from cache ******/
/*****************************************************************************/
void Ses_RemovePublicDirsCache (void)
void Brw_DB_RemovePublicDirsCache (void)
{
/***** Insert into cache *****/
if (Gbl.Session.IsOpen)
@ -551,7 +421,7 @@ void Ses_RemovePublicDirsCache (void)
/****** (from expired sessions) ******/
/*****************************************************************************/
void Ses_RemovePublicDirsFromExpiredSessions (void)
void Brw_DB_RemovePublicDirsFromExpiredSessions (void)
{
/***** Remove public directories in expired sessions *****/
DB_QueryDELETE ("can not remove public directories in expired sessions",
@ -560,55 +430,3 @@ void Ses_RemovePublicDirsFromExpiredSessions (void)
" (SELECT SessionId"
" FROM ses_sessions)");
}
/*****************************************************************************/
/********* 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);
}
/*****************************************************************************/
/********* 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);
}
/*****************************************************************************/
/********************** Save last search into session ************************/
/*****************************************************************************/
void Ses_DB_SaveLastSearchIntoSession (void)
{
DB_QueryUPDATE ("can not update last search in session",
"UPDATE ses_sessions"
" SET WhatToSearch=%u,"
"SearchStr='%s'"
" WHERE SessionId='%s'",
(unsigned) Gbl.Search.WhatToSearch,
Gbl.Search.Str,
Gbl.Session.Id);
}

View File

@ -40,13 +40,8 @@
void Ses_GetNumSessions (void);
void Ses_CreateSession (void);
bool Ses_CheckIfSessionExists (const char *IdSes);
void Ses_CloseSession (void);
void Ses_InsertSessionInDB (void);
void Ses_UpdateSessionDataInDB (void);
void Ses_DB_UpdateSessionLastRefresh (void);
void Ses_DB_RemoveExpiredSessions (void);
void Ses_DB_RemoveUsrSessions (long UsrCod);
bool Ses_GetSessionData (void);
void Ses_InsertParamInDB (const char *ParamName,const char *ParamValue);
@ -54,16 +49,15 @@ void Ses_RemoveParamFromThisSession (void);
void Ses_RemoveParamsFromExpiredSessions (void);
void Ses_GetParamFromDB (const char *ParamName,char *ParamValue,size_t StrSize);
bool Ses_GetPublicDirFromCache (const char *FullPathMediaPriv,
bool Brw_GetPublicDirFromCache (const char *FullPathMediaPriv,
char TmpPubDir[PATH_MAX + 1]);
void Ses_AddPublicDirToCache (const char *FullPathMediaPriv,
void Brw_DB_GetPublicDirFromCache (const char *FullPathMediaPriv,
char TmpPubDir[PATH_MAX + 1]);
void Brw_AddPublicDirToCache (const char *FullPathMediaPriv,
const char TmpPubDir[PATH_MAX + 1]);
void Ses_RemovePublicDirsCache (void);
void Ses_RemovePublicDirsFromExpiredSessions (void);
void Ses_DB_SaveLastPageMsgIntoSession (Pag_WhatPaginate_t WhatPaginate,unsigned NumPage);
unsigned Ses_DB_GetLastPageMsgFromSession (Pag_WhatPaginate_t WhatPaginate);
void Ses_DB_SaveLastSearchIntoSession (void);
void Brw_DB_AddPublicDirToCache (const char *FullPathMediaPriv,
const char TmpPubDir[PATH_MAX + 1]);
void Brw_DB_RemovePublicDirsCache (void);
void Brw_DB_RemovePublicDirsFromExpiredSessions (void);
#endif

281
swad_session_database.c Normal file
View File

@ -0,0 +1,281 @@
// 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-2021 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 <mysql/mysql.h> // To access MySQL databases
// #include <stddef.h> // For NULL
// #include <stdio.h> // For sprintf
// #include <string.h> // For string functions
#include "swad_config.h"
// #include "swad_connected_database.h"
#include "swad_database.h"
// #include "swad_error.h"
#include "swad_global.h"
#include "swad_pagination.h"
// #include "swad_parameter.h"
// #include "swad_session_database.h"
// #include "swad_timeline_database.h"
/*****************************************************************************/
/***************************** Private constants *****************************/
/*****************************************************************************/
/*****************************************************************************/
/************** External global variables from others modules ****************/
/*****************************************************************************/
extern struct Globals Gbl;
/*****************************************************************************/
/***************************** Private prototypes ****************************/
/*****************************************************************************/
/*****************************************************************************/
/******************** Insert new session in the database *********************/
/*****************************************************************************/
void Ses_DB_InsertSession (void)
{
if (Gbl.Search.WhatToSearch == Sch_SEARCH_UNKNOWN)
Gbl.Search.WhatToSearch = Sch_WHAT_TO_SEARCH_DEFAULT;
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.Cty.CtyCod,
Gbl.Hierarchy.Ins.InsCod,
Gbl.Hierarchy.Ctr.CtrCod,
Gbl.Hierarchy.Deg.DegCod,
Gbl.Hierarchy.Crs.CrsCod,
Gbl.Search.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.Cty.CtyCod,
Gbl.Hierarchy.Ins.InsCod,
Gbl.Hierarchy.Ctr.CtrCod,
Gbl.Hierarchy.Deg.DegCod,
Gbl.Hierarchy.Crs.CrsCod,
Gbl.Session.Id);
}
/*****************************************************************************/
/********************** Save last search into session ************************/
/*****************************************************************************/
void Ses_DB_SaveLastSearchIntoSession (void)
{
DB_QueryUPDATE ("can not update last search in session",
"UPDATE ses_sessions"
" SET WhatToSearch=%u,"
"SearchStr='%s'"
" WHERE SessionId='%s'",
(unsigned) Gbl.Search.WhatToSearch,
Gbl.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)
{
/***** Get if session already exists in database *****/
return (DB_QueryCOUNT ("can not check if a session already existed",
"SELECT COUNT(*)"
" FROM ses_sessions"
" WHERE SessionId='%s'",
IdSes) != 0);
}
/*****************************************************************************/
/******* 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 data of session",
"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 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_InsertParam (const char *ParamName,const char *ParamValue)
{
DB_QueryINSERT ("can not create session parameter",
"INSERT INTO ses_params"
" (SessionId,ParamName,ParamValue)"
" VALUES"
" ('%s','%s','%s')",
Gbl.Session.Id,
ParamName,
ParamValue ? ParamValue :
"");
}

59
swad_session_database.h Normal file
View File

@ -0,0 +1,59 @@
// swad_session_database.h: sessions operations with database
#ifndef _SWAD_SES_DB
#define _SWAD_SES_DB
/*
SWAD (Shared Workspace At a Distance in Spanish),
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-2021 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_action.h"
#include "swad_pagination.h"
/*****************************************************************************/
/***************************** Public constants ******************************/
/*****************************************************************************/
/*****************************************************************************/
/***************************** Public prototypes *****************************/
/*****************************************************************************/
void Ses_DB_InsertSession (void);
void Ses_DB_UpdateSession (void);
void Ses_DB_SaveLastSearchIntoSession (void);
void Ses_DB_SaveLastPageMsgIntoSession (Pag_WhatPaginate_t WhatPaginate,unsigned NumPage);
void Ses_DB_UpdateSessionLastRefresh (void);
bool Ses_DB_CheckIfSessionExists (const char *IdSes);
unsigned Ses_DB_GetSessionData (MYSQL_RES **mysql_res);
unsigned Ses_DB_GetLastPageMsgFromSession (Pag_WhatPaginate_t WhatPaginate);
void Ses_DB_RemoveExpiredSessions (void);
void Ses_DB_RemoveUsrSessions (long UsrCod);
void Ses_SB_RemoveCurrentSession (void);
//---------------------------- Session parameters -----------------------------
void Ses_DB_InsertParam (const char *ParamName,const char *ParamValue);
#endif

View File

@ -75,6 +75,7 @@
#include "swad_record.h"
#include "swad_record_database.h"
#include "swad_role.h"
#include "swad_session_database.h"
#include "swad_setting.h"
#include "swad_tab.h"
#include "swad_user.h"
@ -3110,7 +3111,7 @@ void Usr_ChkUsrAndGetUsrData (void)
else
{
Act_AdjustCurrentAction ();
Ses_UpdateSessionDataInDB ();
Ses_DB_UpdateSession ();
Con_DB_UpdateMeInConnectedList ();
}
}