2014-12-01 23:55:08 +01:00
|
|
|
|
// swad_session.c: sessions
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
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.
|
2021-02-09 12:43:45 +01:00
|
|
|
|
Copyright (C) 1999-2021 Antonio Ca<EFBFBD>as Vargas
|
2014-12-01 23:55:08 +01:00
|
|
|
|
|
|
|
|
|
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
|
2019-12-29 12:39:00 +01:00
|
|
|
|
#include <stddef.h> // For NULL
|
2014-12-01 23:55:08 +01:00
|
|
|
|
#include <stdio.h> // For sprintf
|
|
|
|
|
#include <string.h> // For string functions
|
|
|
|
|
|
2021-05-20 23:45:36 +02:00
|
|
|
|
#include "swad_connected_database.h"
|
2014-12-01 23:55:08 +01:00
|
|
|
|
#include "swad_database.h"
|
2021-04-26 15:27:27 +02:00
|
|
|
|
#include "swad_error.h"
|
2014-12-01 23:55:08 +01:00
|
|
|
|
#include "swad_global.h"
|
|
|
|
|
#include "swad_parameter.h"
|
2021-02-27 17:23:22 +01:00
|
|
|
|
#include "swad_timeline_database.h"
|
2014-12-01 23:55:08 +01:00
|
|
|
|
|
|
|
|
|
/*****************************************************************************/
|
2019-11-21 16:47:07 +01:00
|
|
|
|
/***************************** Private constants *****************************/
|
2014-12-01 23:55:08 +01:00
|
|
|
|
/*****************************************************************************/
|
|
|
|
|
|
|
|
|
|
/*****************************************************************************/
|
|
|
|
|
/************** External global variables from others modules ****************/
|
|
|
|
|
/*****************************************************************************/
|
|
|
|
|
|
|
|
|
|
extern struct Globals Gbl;
|
|
|
|
|
|
|
|
|
|
/*****************************************************************************/
|
2019-11-21 16:47:07 +01:00
|
|
|
|
/***************************** Private prototypes ****************************/
|
2014-12-01 23:55:08 +01:00
|
|
|
|
/*****************************************************************************/
|
|
|
|
|
|
2016-01-12 20:58:19 +01:00
|
|
|
|
static void Ses_RemoveSessionFromDB (void);
|
|
|
|
|
|
2021-03-18 20:38:40 +01:00
|
|
|
|
static bool Ses_CheckIfParamIsAlreadyInDB (const char *ParamName);
|
2014-12-01 23:55:08 +01:00
|
|
|
|
|
2020-05-17 22:16:39 +02:00
|
|
|
|
static void Ses_DeletePublicDirFromCache (const char *FullPathMediaPriv);
|
|
|
|
|
|
2014-12-01 23:55:08 +01:00
|
|
|
|
/*****************************************************************************/
|
|
|
|
|
/************************** Get number of open sessions **********************/
|
|
|
|
|
/*****************************************************************************/
|
|
|
|
|
|
|
|
|
|
void Ses_GetNumSessions (void)
|
|
|
|
|
{
|
|
|
|
|
/***** Get the number of open sessions from database *****/
|
2021-03-19 00:27:46 +01:00
|
|
|
|
Gbl.Session.NumSessions = (unsigned) DB_GetNumRowsTable ("ses_sessions");
|
2014-12-01 23:55:08 +01:00
|
|
|
|
|
2021-03-19 00:27:46 +01:00
|
|
|
|
Gbl.Usrs.Connected.TimeToRefreshInMs = (unsigned long) (Gbl.Session.NumSessions/
|
|
|
|
|
Cfg_TIMES_PER_SECOND_REFRESH_CONNECTED) * 1000UL;
|
2014-12-01 23:55:08 +01:00
|
|
|
|
if (Gbl.Usrs.Connected.TimeToRefreshInMs < Con_MIN_TIME_TO_REFRESH_CONNECTED_IN_MS)
|
|
|
|
|
Gbl.Usrs.Connected.TimeToRefreshInMs = Con_MIN_TIME_TO_REFRESH_CONNECTED_IN_MS;
|
|
|
|
|
else if (Gbl.Usrs.Connected.TimeToRefreshInMs > Con_MAX_TIME_TO_REFRESH_CONNECTED_IN_MS)
|
|
|
|
|
Gbl.Usrs.Connected.TimeToRefreshInMs = Con_MAX_TIME_TO_REFRESH_CONNECTED_IN_MS;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*****************************************************************************/
|
|
|
|
|
/*************************** Create a new session ****************************/
|
|
|
|
|
/*****************************************************************************/
|
|
|
|
|
|
|
|
|
|
void Ses_CreateSession (void)
|
|
|
|
|
{
|
|
|
|
|
/***** Create a unique name for the session *****/
|
2021-02-15 16:25:55 +01:00
|
|
|
|
Str_Copy (Gbl.Session.Id,Gbl.UniqueNameEncrypted,sizeof (Gbl.Session.Id) - 1);
|
2014-12-01 23:55:08 +01:00
|
|
|
|
|
|
|
|
|
/***** Check that session is not open *****/
|
|
|
|
|
if (Ses_CheckIfSessionExists (Gbl.Session.Id))
|
2021-04-26 15:27:27 +02:00
|
|
|
|
Err_ShowErrorAndExit ("Can not create session.");
|
2014-12-01 23:55:08 +01:00
|
|
|
|
|
|
|
|
|
/***** Add session to database *****/
|
|
|
|
|
Ses_InsertSessionInDB ();
|
|
|
|
|
|
|
|
|
|
/***** Update time and course in connected list *****/
|
2021-05-20 23:45:36 +02:00
|
|
|
|
Con_DB_UpdateMeInConnectedList ();
|
2014-12-01 23:55:08 +01:00
|
|
|
|
|
|
|
|
|
/***** Update number of open sessions in order to show them properly *****/
|
|
|
|
|
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 *****/
|
2018-11-03 20:52:00 +01:00
|
|
|
|
return (DB_QueryCOUNT ("can not check if a session already existed",
|
2021-03-18 20:47:28 +01:00
|
|
|
|
"SELECT COUNT(*)"
|
|
|
|
|
" FROM ses_sessions"
|
2018-11-03 20:52:00 +01:00
|
|
|
|
" WHERE SessionId='%s'",
|
|
|
|
|
IdSes) != 0);
|
2014-12-01 23:55:08 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*****************************************************************************/
|
|
|
|
|
/************************** Close current session ****************************/
|
|
|
|
|
/*****************************************************************************/
|
|
|
|
|
|
|
|
|
|
void Ses_CloseSession (void)
|
|
|
|
|
{
|
|
|
|
|
if (Gbl.Usrs.Me.Logged)
|
|
|
|
|
{
|
2020-05-13 02:12:11 +02:00
|
|
|
|
/***** Remove links to private files from cache *****/
|
|
|
|
|
Ses_RemovePublicDirsCache ();
|
|
|
|
|
|
2014-12-01 23:55:08 +01:00
|
|
|
|
/***** Remove session from database *****/
|
|
|
|
|
Ses_RemoveSessionFromDB ();
|
|
|
|
|
Gbl.Session.IsOpen = false;
|
|
|
|
|
// Gbl.Session.HasBeenDisconnected = true;
|
|
|
|
|
Gbl.Session.Id[0] = '\0';
|
|
|
|
|
|
|
|
|
|
/***** If there are no more sessions for current user ==> remove user from connected list *****/
|
2021-05-10 21:10:06 +02:00
|
|
|
|
Con_DB_RemoveOldConnected ();
|
2014-12-01 23:55:08 +01:00
|
|
|
|
|
2020-05-13 02:12:11 +02:00
|
|
|
|
/***** Remove unused data associated to expired sessions *****/
|
2021-03-18 20:38:40 +01:00
|
|
|
|
Ses_RemoveParamsFromExpiredSessions ();
|
2020-05-13 02:12:11 +02:00
|
|
|
|
Ses_RemovePublicDirsFromExpiredSessions ();
|
2014-12-01 23:55:08 +01:00
|
|
|
|
|
|
|
|
|
/***** Now, user is not logged in *****/
|
2017-06-04 18:18:54 +02:00
|
|
|
|
Gbl.Usrs.Me.Role.LoggedBeforeCloseSession = Gbl.Usrs.Me.Role.Logged;
|
2014-12-01 23:55:08 +01:00
|
|
|
|
Gbl.Usrs.Me.Logged = false;
|
2015-01-20 20:03:38 +01:00
|
|
|
|
Gbl.Usrs.Me.IBelongToCurrentIns = false;
|
|
|
|
|
Gbl.Usrs.Me.IBelongToCurrentCtr = false;
|
|
|
|
|
Gbl.Usrs.Me.IBelongToCurrentDeg = false;
|
2014-12-01 23:55:08 +01:00
|
|
|
|
Gbl.Usrs.Me.IBelongToCurrentCrs = false;
|
2017-06-04 18:18:54 +02:00
|
|
|
|
Gbl.Usrs.Me.Role.Logged = Rol_UNK; // Don't uncomment this line. Don't change the role to unknown. Keep user's role in order to log the access
|
2016-10-28 10:03:37 +02:00
|
|
|
|
Gbl.Usrs.Me.MyCrss.Filled = false;
|
|
|
|
|
Gbl.Usrs.Me.MyCrss.Num = 0;
|
2014-12-01 23:55:08 +01:00
|
|
|
|
|
|
|
|
|
/***** Update number of open sessions in order to show them properly *****/
|
|
|
|
|
Ses_GetNumSessions ();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*****************************************************************************/
|
|
|
|
|
/******************** Insert new session in the database *********************/
|
|
|
|
|
/*****************************************************************************/
|
|
|
|
|
|
|
|
|
|
void Ses_InsertSessionInDB (void)
|
|
|
|
|
{
|
|
|
|
|
/***** Insert session in the database *****/
|
2017-01-29 12:42:19 +01:00
|
|
|
|
if (Gbl.Search.WhatToSearch == Sch_SEARCH_UNKNOWN)
|
|
|
|
|
Gbl.Search.WhatToSearch = Sch_WHAT_TO_SEARCH_DEFAULT;
|
|
|
|
|
|
2018-11-02 19:37:11 +01:00
|
|
|
|
DB_QueryINSERT ("can not create session",
|
2021-03-18 20:47:28 +01:00
|
|
|
|
"INSERT INTO ses_sessions"
|
2018-11-02 19:37:11 +01:00
|
|
|
|
" (SessionId,UsrCod,Password,Role,"
|
2021-04-17 11:27:33 +02:00
|
|
|
|
"CtyCod,InsCod,CtrCod,DegCod,CrsCod,LastTime,LastRefresh,WhatToSearch)"
|
2018-11-02 19:37:11 +01:00
|
|
|
|
" VALUES"
|
|
|
|
|
" ('%s',%ld,'%s',%u,"
|
2021-04-17 11:27:33 +02:00
|
|
|
|
"%ld,%ld,%ld,%ld,%ld,NOW(),NOW(),%u)",
|
2018-11-02 19:37:11 +01:00
|
|
|
|
Gbl.Session.Id,
|
|
|
|
|
Gbl.Usrs.Me.UsrDat.UsrCod,
|
|
|
|
|
Gbl.Usrs.Me.UsrDat.Password,
|
|
|
|
|
(unsigned) Gbl.Usrs.Me.Role.Logged,
|
2019-04-03 20:57:04 +02:00
|
|
|
|
Gbl.Hierarchy.Cty.CtyCod,
|
|
|
|
|
Gbl.Hierarchy.Ins.InsCod,
|
|
|
|
|
Gbl.Hierarchy.Ctr.CtrCod,
|
|
|
|
|
Gbl.Hierarchy.Deg.DegCod,
|
2019-04-04 10:45:15 +02:00
|
|
|
|
Gbl.Hierarchy.Crs.CrsCod,
|
2018-11-02 19:37:11 +01:00
|
|
|
|
Gbl.Search.WhatToSearch);
|
2014-12-01 23:55:08 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*****************************************************************************/
|
|
|
|
|
/***************** Modify data of session in the database ********************/
|
|
|
|
|
/*****************************************************************************/
|
|
|
|
|
|
|
|
|
|
void Ses_UpdateSessionDataInDB (void)
|
|
|
|
|
{
|
|
|
|
|
/***** Update session in database *****/
|
2018-11-03 12:16:40 +01:00
|
|
|
|
DB_QueryUPDATE ("can not update session",
|
2021-03-18 20:47:28 +01:00
|
|
|
|
"UPDATE ses_sessions"
|
|
|
|
|
" SET UsrCod=%ld,"
|
|
|
|
|
"Password='%s',"
|
|
|
|
|
"Role=%u,"
|
|
|
|
|
"CtyCod=%ld,"
|
|
|
|
|
"InsCod=%ld,"
|
|
|
|
|
"CtrCod=%ld,"
|
|
|
|
|
"DegCod=%ld,"
|
|
|
|
|
"CrsCod=%ld,"
|
|
|
|
|
"LastTime=NOW(),"
|
|
|
|
|
"LastRefresh=NOW()"
|
2018-11-03 12:16:40 +01:00
|
|
|
|
" WHERE SessionId='%s'",
|
|
|
|
|
Gbl.Usrs.Me.UsrDat.UsrCod,
|
|
|
|
|
Gbl.Usrs.Me.UsrDat.Password,
|
|
|
|
|
(unsigned) Gbl.Usrs.Me.Role.Logged,
|
2019-04-03 20:57:04 +02:00
|
|
|
|
Gbl.Hierarchy.Cty.CtyCod,
|
|
|
|
|
Gbl.Hierarchy.Ins.InsCod,
|
|
|
|
|
Gbl.Hierarchy.Ctr.CtrCod,
|
|
|
|
|
Gbl.Hierarchy.Deg.DegCod,
|
2019-04-04 10:45:15 +02:00
|
|
|
|
Gbl.Hierarchy.Crs.CrsCod,
|
2018-11-03 12:16:40 +01:00
|
|
|
|
Gbl.Session.Id);
|
2014-12-01 23:55:08 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*****************************************************************************/
|
|
|
|
|
/******************** Modify session last refresh in database ****************/
|
|
|
|
|
/*****************************************************************************/
|
|
|
|
|
|
|
|
|
|
void Ses_UpdateSessionLastRefreshInDB (void)
|
|
|
|
|
{
|
|
|
|
|
/***** Update session in database *****/
|
2018-11-03 12:16:40 +01:00
|
|
|
|
DB_QueryUPDATE ("can not update session",
|
2021-03-18 20:47:28 +01:00
|
|
|
|
"UPDATE ses_sessions"
|
|
|
|
|
" SET LastRefresh=NOW()"
|
|
|
|
|
" WHERE SessionId='%s'",
|
2018-11-03 12:16:40 +01:00
|
|
|
|
Gbl.Session.Id);
|
2014-12-01 23:55:08 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*****************************************************************************/
|
|
|
|
|
/********************** Remove session from the database *********************/
|
|
|
|
|
/*****************************************************************************/
|
|
|
|
|
|
2016-01-12 20:58:19 +01:00
|
|
|
|
static void Ses_RemoveSessionFromDB (void)
|
2014-12-01 23:55:08 +01:00
|
|
|
|
{
|
|
|
|
|
/***** Remove current session *****/
|
2018-11-02 22:00:31 +01:00
|
|
|
|
DB_QueryDELETE ("can not remove a session",
|
2021-03-18 20:47:28 +01:00
|
|
|
|
"DELETE FROM ses_sessions"
|
|
|
|
|
" WHERE SessionId='%s'",
|
2018-11-02 22:00:31 +01:00
|
|
|
|
Gbl.Session.Id);
|
2016-01-12 20:58:19 +01:00
|
|
|
|
|
|
|
|
|
/***** Clear old unused social timelines in database *****/
|
|
|
|
|
// This is necessary to prevent the table growing and growing
|
2021-03-04 17:47:08 +01:00
|
|
|
|
Tml_DB_ClearOldTimelinesNotesFromDB ();
|
2014-12-01 23:55:08 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*****************************************************************************/
|
|
|
|
|
/*************************** Remove expired sessions *************************/
|
|
|
|
|
/*****************************************************************************/
|
|
|
|
|
|
|
|
|
|
void Ses_RemoveExpiredSessions (void)
|
|
|
|
|
{
|
|
|
|
|
/***** Remove expired sessions *****/
|
2015-11-11 09:53:36 +01:00
|
|
|
|
/* 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)) */
|
2018-11-02 22:00:31 +01:00
|
|
|
|
DB_QueryDELETE ("can not remove expired sessions",
|
2021-03-18 20:47:28 +01:00
|
|
|
|
"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))",
|
2018-11-02 22:00:31 +01:00
|
|
|
|
Cfg_TIME_TO_CLOSE_SESSION_FROM_LAST_CLICK,
|
|
|
|
|
Cfg_TIME_TO_CLOSE_SESSION_FROM_LAST_REFRESH);
|
2014-12-01 23:55:08 +01:00
|
|
|
|
}
|
|
|
|
|
|
2021-05-10 21:10:06 +02:00
|
|
|
|
/*****************************************************************************/
|
|
|
|
|
/******************* 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);
|
|
|
|
|
}
|
|
|
|
|
|
2014-12-01 23:55:08 +01:00
|
|
|
|
/*****************************************************************************/
|
|
|
|
|
/******* Get the data (user code and password) of an initiated session *******/
|
|
|
|
|
/*****************************************************************************/
|
|
|
|
|
|
|
|
|
|
bool Ses_GetSessionData (void)
|
|
|
|
|
{
|
|
|
|
|
MYSQL_RES *mysql_res;
|
|
|
|
|
MYSQL_ROW row;
|
|
|
|
|
unsigned UnsignedNum;
|
|
|
|
|
bool Result = false;
|
|
|
|
|
|
|
|
|
|
/***** Check if the session existed in the database *****/
|
2018-11-01 21:59:42 +01:00
|
|
|
|
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]
|
2021-03-18 20:47:28 +01:00
|
|
|
|
" FROM ses_sessions"
|
2018-11-01 21:59:42 +01:00
|
|
|
|
" WHERE SessionId='%s'",
|
|
|
|
|
Gbl.Session.Id))
|
2014-12-01 23:55:08 +01:00
|
|
|
|
{
|
|
|
|
|
row = mysql_fetch_row (mysql_res);
|
|
|
|
|
|
|
|
|
|
/***** Get user code (row[0]) *****/
|
|
|
|
|
Gbl.Session.UsrCod = Str_ConvertStrCodToLongCod (row[0]);
|
|
|
|
|
|
|
|
|
|
/***** Get password (row[1]) *****/
|
2017-01-15 22:58:26 +01:00
|
|
|
|
Str_Copy (Gbl.Usrs.Me.LoginEncryptedPassword,row[1],
|
2021-02-15 16:25:55 +01:00
|
|
|
|
sizeof (Gbl.Usrs.Me.LoginEncryptedPassword) - 1);
|
2014-12-01 23:55:08 +01:00
|
|
|
|
|
|
|
|
|
/***** Get logged user type (row[2]) *****/
|
2017-06-04 18:18:54 +02:00
|
|
|
|
if (sscanf (row[2],"%u",&Gbl.Usrs.Me.Role.FromSession) != 1)
|
|
|
|
|
Gbl.Usrs.Me.Role.FromSession = Rol_UNK;
|
2014-12-01 23:55:08 +01:00
|
|
|
|
|
|
|
|
|
/***** Get country code (row[3]) *****/
|
2019-04-03 20:57:04 +02:00
|
|
|
|
Gbl.Hierarchy.Cty.CtyCod = Str_ConvertStrCodToLongCod (row[3]);
|
2014-12-01 23:55:08 +01:00
|
|
|
|
|
|
|
|
|
/***** Get institution code (row[4]) *****/
|
2019-04-03 20:57:04 +02:00
|
|
|
|
Gbl.Hierarchy.Ins.InsCod = Str_ConvertStrCodToLongCod (row[4]);
|
2014-12-01 23:55:08 +01:00
|
|
|
|
|
2021-03-07 21:21:04 +01:00
|
|
|
|
/***** Get center code (row[5]) *****/
|
2019-04-03 20:57:04 +02:00
|
|
|
|
Gbl.Hierarchy.Ctr.CtrCod = Str_ConvertStrCodToLongCod (row[5]);
|
2014-12-01 23:55:08 +01:00
|
|
|
|
|
|
|
|
|
/***** Get degree code (row[6]) *****/
|
2019-04-03 20:57:04 +02:00
|
|
|
|
Gbl.Hierarchy.Deg.DegCod = Str_ConvertStrCodToLongCod (row[6]);
|
2014-12-01 23:55:08 +01:00
|
|
|
|
|
|
|
|
|
/***** Get course code (row[7]) *****/
|
2019-04-04 10:45:15 +02:00
|
|
|
|
Gbl.Hierarchy.Crs.CrsCod = Str_ConvertStrCodToLongCod (row[7]);
|
2014-12-01 23:55:08 +01:00
|
|
|
|
|
|
|
|
|
/***** Get last search *****/
|
2016-01-17 15:10:54 +01:00
|
|
|
|
if (Gbl.Action.Act != ActLogOut) // When closing session, last search will not be needed
|
2014-12-01 23:55:08 +01:00
|
|
|
|
{
|
|
|
|
|
/* Get what to search (row[8]) */
|
2017-01-29 12:42:19 +01:00
|
|
|
|
Gbl.Search.WhatToSearch = Sch_SEARCH_UNKNOWN;
|
2014-12-01 23:55:08 +01:00
|
|
|
|
if (sscanf (row[8],"%u",&UnsignedNum) == 1)
|
|
|
|
|
if (UnsignedNum < Sch_NUM_WHAT_TO_SEARCH)
|
|
|
|
|
Gbl.Search.WhatToSearch = (Sch_WhatToSearch_t) UnsignedNum;
|
2017-01-29 12:42:19 +01:00
|
|
|
|
if (Gbl.Search.WhatToSearch == Sch_SEARCH_UNKNOWN)
|
|
|
|
|
Gbl.Search.WhatToSearch = Sch_WHAT_TO_SEARCH_DEFAULT;
|
2014-12-01 23:55:08 +01:00
|
|
|
|
|
|
|
|
|
/* Get search string (row[9]) */
|
2021-02-15 16:25:55 +01:00
|
|
|
|
Str_Copy (Gbl.Search.Str,row[9],sizeof (Gbl.Search.Str) - 1);
|
2014-12-01 23:55:08 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Result = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/***** Free structure that stores the query result *****/
|
|
|
|
|
DB_FreeMySQLResult (&mysql_res);
|
|
|
|
|
|
|
|
|
|
return Result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*****************************************************************************/
|
2021-03-18 20:38:40 +01:00
|
|
|
|
/******************* Insert session parameter in the database ****************/
|
2014-12-01 23:55:08 +01:00
|
|
|
|
/*****************************************************************************/
|
|
|
|
|
|
2021-03-18 20:38:40 +01:00
|
|
|
|
void Ses_InsertParamInDB (const char *ParamName,const char *ParamValue)
|
2014-12-01 23:55:08 +01:00
|
|
|
|
{
|
2021-03-18 20:38:40 +01:00
|
|
|
|
/***** Before of inserting the first session parameter passed to the next action,
|
2014-12-01 23:55:08 +01:00
|
|
|
|
delete all the parameters coming from the previous action *****/
|
2021-03-18 20:38:40 +01:00
|
|
|
|
Ses_RemoveParamFromThisSession ();
|
2014-12-01 23:55:08 +01:00
|
|
|
|
|
2019-04-22 10:10:21 +02:00
|
|
|
|
/***** For a unique session-parameter,
|
|
|
|
|
don't insert a parameter more than one time *****/
|
2017-03-15 11:10:16 +01:00
|
|
|
|
if (ParamName)
|
2018-10-29 12:31:24 +01:00
|
|
|
|
if (ParamName[0])
|
2021-03-18 20:38:40 +01:00
|
|
|
|
if (!Ses_CheckIfParamIsAlreadyInDB (ParamName))
|
2017-03-15 11:10:16 +01:00
|
|
|
|
{
|
2021-03-18 20:38:40 +01:00
|
|
|
|
/***** Insert session parameter in the database *****/
|
|
|
|
|
DB_QueryINSERT ("can not create session parameter",
|
|
|
|
|
"INSERT INTO ses_params"
|
2019-04-22 01:06:48 +02:00
|
|
|
|
" (SessionId,ParamName,ParamValue)"
|
|
|
|
|
" VALUES"
|
|
|
|
|
" ('%s','%s','%s')",
|
|
|
|
|
Gbl.Session.Id,
|
|
|
|
|
ParamName,
|
|
|
|
|
ParamValue ? ParamValue :
|
|
|
|
|
"");
|
2021-03-18 20:38:40 +01:00
|
|
|
|
Gbl.Session.ParamsInsertedIntoDB = true;
|
2017-03-15 11:10:16 +01:00
|
|
|
|
}
|
2014-12-01 23:55:08 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*****************************************************************************/
|
2021-03-18 20:38:40 +01:00
|
|
|
|
/************ Remove session parameters of a session from database ***********/
|
2014-12-01 23:55:08 +01:00
|
|
|
|
/*****************************************************************************/
|
|
|
|
|
|
2021-03-18 20:38:40 +01:00
|
|
|
|
void Ses_RemoveParamFromThisSession (void)
|
2014-12-01 23:55:08 +01:00
|
|
|
|
{
|
2017-02-05 22:23:41 +01:00
|
|
|
|
if (Gbl.Session.IsOpen && // There is an open session
|
2021-03-18 20:38:40 +01:00
|
|
|
|
!Gbl.Session.ParamsInsertedIntoDB) // 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'",
|
2018-11-02 22:00:31 +01:00
|
|
|
|
Gbl.Session.Id);
|
2014-12-01 23:55:08 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*****************************************************************************/
|
|
|
|
|
/********* Remove expired hidden parameters (from expired sessions) **********/
|
|
|
|
|
/*****************************************************************************/
|
|
|
|
|
|
2021-03-18 20:38:40 +01:00
|
|
|
|
void Ses_RemoveParamsFromExpiredSessions (void)
|
2014-12-01 23:55:08 +01:00
|
|
|
|
{
|
2021-03-18 20:38:40 +01:00
|
|
|
|
/***** Remove session parameters from expired sessions *****/
|
|
|
|
|
DB_QueryDELETE ("can not remove session parameters of expired sessions",
|
|
|
|
|
"DELETE FROM ses_params"
|
2018-11-02 22:00:31 +01:00
|
|
|
|
" WHERE SessionId NOT IN"
|
2021-03-29 16:38:56 +02:00
|
|
|
|
" (SELECT SessionId"
|
|
|
|
|
" FROM ses_sessions)");
|
2014-12-01 23:55:08 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*****************************************************************************/
|
2021-03-18 20:38:40 +01:00
|
|
|
|
/************** Check if a session parameter existed in database *************/
|
2014-12-01 23:55:08 +01:00
|
|
|
|
/*****************************************************************************/
|
|
|
|
|
// Return true if the parameter already existed in database
|
|
|
|
|
|
2021-03-18 20:38:40 +01:00
|
|
|
|
static bool Ses_CheckIfParamIsAlreadyInDB (const char *ParamName)
|
2014-12-01 23:55:08 +01:00
|
|
|
|
{
|
2021-03-18 20:38:40 +01:00
|
|
|
|
return (DB_QueryCOUNT ("can not check if a session parameter"
|
2019-04-22 01:06:48 +02:00
|
|
|
|
" is already in database",
|
2021-03-18 20:38:40 +01:00
|
|
|
|
"SELECT COUNT(*)"
|
|
|
|
|
" FROM ses_params"
|
2019-04-22 01:06:48 +02:00
|
|
|
|
" WHERE SessionId='%s'"
|
2021-03-18 20:38:40 +01:00
|
|
|
|
" AND ParamName='%s'",
|
2019-04-22 01:06:48 +02:00
|
|
|
|
Gbl.Session.Id,
|
|
|
|
|
ParamName) != 0);
|
2014-12-01 23:55:08 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*****************************************************************************/
|
2021-03-18 20:38:40 +01:00
|
|
|
|
/***************** Get session parameter from the database *******************/
|
2014-12-01 23:55:08 +01:00
|
|
|
|
/*****************************************************************************/
|
2021-04-16 13:51:12 +02:00
|
|
|
|
// StrSize is the size of the parameter value, not including the ending '\0'
|
2014-12-01 23:55:08 +01:00
|
|
|
|
|
2021-04-16 13:51:12 +02:00
|
|
|
|
void Ses_GetParamFromDB (const char *ParamName,char *ParamValue,size_t StrSize)
|
2014-12-01 23:55:08 +01:00
|
|
|
|
{
|
|
|
|
|
ParamValue[0] = '\0';
|
|
|
|
|
if (Gbl.Session.IsOpen) // If the session is open, get parameter from DB
|
2021-03-18 20:38:40 +01:00
|
|
|
|
/***** Get a session parameter from database *****/
|
2021-04-16 13:51:12 +02:00
|
|
|
|
DB_QuerySELECTString (ParamValue,StrSize,"can not get a session parameter",
|
|
|
|
|
"SELECT ParamValue" // row[0]
|
|
|
|
|
" FROM ses_params"
|
|
|
|
|
" WHERE SessionId='%s'"
|
|
|
|
|
" AND ParamName='%s'",
|
|
|
|
|
Gbl.Session.Id,
|
|
|
|
|
ParamName);
|
2014-12-01 23:55:08 +01:00
|
|
|
|
}
|
2020-05-13 02:12:11 +02:00
|
|
|
|
|
|
|
|
|
/*****************************************************************************/
|
|
|
|
|
/******** Get public directory used to link private path from cache **********/
|
|
|
|
|
/*****************************************************************************/
|
|
|
|
|
|
|
|
|
|
bool Ses_GetPublicDirFromCache (const char *FullPathMediaPriv,
|
|
|
|
|
char TmpPubDir[PATH_MAX + 1])
|
|
|
|
|
{
|
2020-05-17 22:16:39 +02:00
|
|
|
|
bool Cached;
|
|
|
|
|
bool TmpPubDirExists;
|
2020-05-13 02:12:11 +02:00
|
|
|
|
|
|
|
|
|
/***** Reset temporary directory *****/
|
|
|
|
|
TmpPubDir[0] = '\0';
|
|
|
|
|
|
|
|
|
|
if (Gbl.Session.IsOpen)
|
|
|
|
|
{
|
|
|
|
|
/***** Get temporary directory from cache *****/
|
2021-04-13 23:34:07 +02:00
|
|
|
|
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);
|
|
|
|
|
Cached = (TmpPubDir[0] != '\0');
|
2020-05-17 22:16:39 +02:00
|
|
|
|
|
|
|
|
|
/***** Check if temporary public directory exists *****/
|
|
|
|
|
if (Cached)
|
|
|
|
|
{
|
|
|
|
|
/* If not exists (it could be deleted if its lifetime has expired)
|
|
|
|
|
==> remove from cache */
|
|
|
|
|
TmpPubDirExists = Fil_CheckIfPathExists (TmpPubDir);
|
|
|
|
|
if (!TmpPubDirExists)
|
|
|
|
|
Ses_DeletePublicDirFromCache (FullPathMediaPriv);
|
|
|
|
|
return TmpPubDirExists;
|
|
|
|
|
}
|
2020-05-13 02:12:11 +02:00
|
|
|
|
}
|
|
|
|
|
|
2020-05-17 22:16:39 +02:00
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*****************************************************************************/
|
|
|
|
|
/********* Add public directory used to link private path to cache ***********/
|
|
|
|
|
/*****************************************************************************/
|
|
|
|
|
|
|
|
|
|
static void Ses_DeletePublicDirFromCache (const char *FullPathMediaPriv)
|
|
|
|
|
{
|
|
|
|
|
/***** Delete possible entry *****/
|
|
|
|
|
if (Gbl.Session.IsOpen)
|
|
|
|
|
DB_QueryDELETE ("can not remove cached file",
|
2021-03-24 10:09:40 +01:00
|
|
|
|
"DELETE FROM brw_caches"
|
2021-03-18 12:51:01 +01:00
|
|
|
|
" WHERE SessionId='%s'"
|
|
|
|
|
" AND PrivPath='%s'",
|
2020-05-17 22:16:39 +02:00
|
|
|
|
Gbl.Session.Id,FullPathMediaPriv);
|
2020-05-13 02:12:11 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*****************************************************************************/
|
|
|
|
|
/********* Add public directory used to link private path to cache ***********/
|
|
|
|
|
/*****************************************************************************/
|
|
|
|
|
|
|
|
|
|
void Ses_AddPublicDirToCache (const char *FullPathMediaPriv,
|
|
|
|
|
const char TmpPubDir[PATH_MAX + 1])
|
|
|
|
|
{
|
|
|
|
|
/***** Insert into cache *****/
|
|
|
|
|
if (Gbl.Session.IsOpen)
|
2020-05-17 22:16:39 +02:00
|
|
|
|
{
|
|
|
|
|
/* Delete possible old entry */
|
|
|
|
|
Ses_DeletePublicDirFromCache (FullPathMediaPriv);
|
|
|
|
|
|
|
|
|
|
/* Insert new entry */
|
2020-05-13 02:12:11 +02:00
|
|
|
|
DB_QueryINSERT ("can not cache file",
|
2021-03-24 10:09:40 +01:00
|
|
|
|
"INSERT INTO brw_caches"
|
2020-05-13 02:12:11 +02:00
|
|
|
|
" (SessionId,PrivPath,TmpPubDir)"
|
|
|
|
|
" VALUES"
|
|
|
|
|
" ('%s','%s','%s')",
|
2021-04-17 11:27:33 +02:00
|
|
|
|
Gbl.Session.Id,
|
|
|
|
|
FullPathMediaPriv,
|
|
|
|
|
TmpPubDir);
|
2020-05-17 22:16:39 +02:00
|
|
|
|
}
|
2020-05-13 02:12:11 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*****************************************************************************/
|
|
|
|
|
/****** Remove public directories used to link private paths from cache ******/
|
|
|
|
|
/*****************************************************************************/
|
|
|
|
|
|
|
|
|
|
void Ses_RemovePublicDirsCache (void)
|
|
|
|
|
{
|
|
|
|
|
/***** Insert into cache *****/
|
|
|
|
|
if (Gbl.Session.IsOpen)
|
|
|
|
|
DB_QueryDELETE ("can not cache file",
|
2021-03-24 10:09:40 +01:00
|
|
|
|
"DELETE FROM brw_caches"
|
2021-03-18 12:51:01 +01:00
|
|
|
|
" WHERE SessionId='%s'",
|
2020-05-13 02:12:11 +02:00
|
|
|
|
Gbl.Session.Id);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*****************************************************************************/
|
|
|
|
|
/****** Remove public directories used to link private paths from cache ******/
|
|
|
|
|
/****** (from expired sessions) ******/
|
|
|
|
|
/*****************************************************************************/
|
|
|
|
|
|
|
|
|
|
void Ses_RemovePublicDirsFromExpiredSessions (void)
|
|
|
|
|
{
|
|
|
|
|
/***** Remove public directories in expired sessions *****/
|
|
|
|
|
DB_QueryDELETE ("can not remove public directories in expired sessions",
|
2021-03-24 10:09:40 +01:00
|
|
|
|
"DELETE FROM brw_caches"
|
2020-05-13 02:12:11 +02:00
|
|
|
|
" WHERE SessionId NOT IN"
|
2021-03-18 20:47:28 +01:00
|
|
|
|
" (SELECT SessionId"
|
|
|
|
|
" FROM ses_sessions)");
|
2020-05-13 02:12:11 +02:00
|
|
|
|
}
|