From e9607cb177a2f69b94cffdb7b1085026d5e15423 Mon Sep 17 00:00:00 2001 From: acanas Date: Fri, 15 Oct 2021 18:01:53 +0200 Subject: [PATCH] Version 21.34: Oct 15, 2021 New module swad_session_database for database queries related to sessions. --- Makefile | 5 +- swad_account.c | 1 + swad_changelog.h | 3 +- swad_main.c | 1 + swad_media.c | 12 +- swad_message.c | 1 + swad_pagination.c | 1 + swad_password.c | 3 +- swad_role.c | 3 +- swad_search.c | 1 + swad_session.c | 296 ++++++++-------------------------------- swad_session.h | 24 ++-- swad_session_database.c | 281 ++++++++++++++++++++++++++++++++++++++ swad_session_database.h | 59 ++++++++ swad_user.c | 3 +- 15 files changed, 428 insertions(+), 266 deletions(-) create mode 100644 swad_session_database.c create mode 100644 swad_session_database.h diff --git a/Makefile b/Makefile index b3cc513fa..932718c12 100644 --- a/Makefile +++ b/Makefile @@ -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 \ diff --git a/swad_account.c b/swad_account.c index 34efc1d78..d5f0ca521 100644 --- a/swad_account.c +++ b/swad_account.c @@ -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" diff --git a/swad_changelog.h b/swad_changelog.h index 1e62b736a..ddc58594a 100644 --- a/swad_changelog.h +++ b/swad_changelog.h @@ -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) diff --git a/swad_main.c b/swad_main.c index 1c42a6120..7a5774791 100644 --- a/swad_main.c +++ b/swad_main.c @@ -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" diff --git a/swad_media.c b/swad_media.c index 0e0ee6ccb..3580b6899 100644 --- a/swad_media.c +++ b/swad_media.c @@ -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 *****/ diff --git a/swad_message.c b/swad_message.c index 91cf9a3ce..2611b20de 100644 --- a/swad_message.c +++ b/swad_message.c @@ -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" /*****************************************************************************/ diff --git a/swad_pagination.c b/swad_pagination.c index d5e18a945..457455df2 100644 --- a/swad_pagination.c +++ b/swad_pagination.c @@ -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" /*****************************************************************************/ diff --git a/swad_password.c b/swad_password.c index 137eec1fa..cde1ae917 100644 --- a/swad_password.c +++ b/swad_password.c @@ -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, diff --git a/swad_role.c b/swad_role.c index c515279be..d7b1387de 100644 --- a/swad_role.c +++ b/swad_role.c @@ -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 (); diff --git a/swad_search.c b/swad_search.c index a938ad3f5..7f8384ade 100644 --- a/swad_search.c +++ b/swad_search.c @@ -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" /*****************************************************************************/ diff --git a/swad_session.c b/swad_session.c index 5a3804877..2df076fed 100644 --- a/swad_session.c +++ b/swad_session.c @@ -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 LastTimeLastTime+INTERVAL 1 SECOND" - " AND" - " LastRefresh 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); - } diff --git a/swad_session.h b/swad_session.h index d8fafae64..442a66565 100644 --- a/swad_session.h +++ b/swad_session.h @@ -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 diff --git a/swad_session_database.c b/swad_session_database.c new file mode 100644 index 000000000..b5e86aa28 --- /dev/null +++ b/swad_session_database.c @@ -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 . +*/ +/*****************************************************************************/ +/************************************ Headers ********************************/ +/*****************************************************************************/ + +// #include // To access MySQL databases +// #include // For NULL +// #include // For sprintf +// #include // 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 LastTimeLastTime+INTERVAL 1 SECOND" + " AND" + " LastRefresh. +*/ +/*****************************************************************************/ +/********************************* 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 diff --git a/swad_user.c b/swad_user.c index ac6c46d71..404c89ea2 100644 --- a/swad_user.c +++ b/swad_user.c @@ -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 (); } }