// 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.
Copyright (C) 1999-2016 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 // For NULL
#include // To access MySQL databases
#include // For sprintf
#include // For string functions
#include "swad_connected.h"
#include "swad_database.h"
#include "swad_global.h"
#include "swad_parameter.h"
#include "swad_social.h"
/*****************************************************************************/
/**************************** Internal constants *****************************/
/*****************************************************************************/
/*****************************************************************************/
/************** External global variables from others modules ****************/
/*****************************************************************************/
extern struct Globals Gbl;
/*****************************************************************************/
/***************************** Internal prototypes ***************************/
/*****************************************************************************/
static void Ses_RemoveSessionFromDB (void);
static bool Ses_CheckIfHiddenParIsAlreadyInDB (Act_Action_t Action,const char *ParamName);
/*****************************************************************************/
/************************** Get number of open sessions **********************/
/*****************************************************************************/
void Ses_GetNumSessions (void)
{
char Query[128];
/***** Get the number of open sessions from database *****/
sprintf (Query,"SELECT COUNT(*) FROM sessions");
Gbl.Session.NumSessions = (unsigned) DB_QueryCOUNT (Query,"can not get the number of open sessions");
Gbl.Usrs.Connected.TimeToRefreshInMs = (unsigned long) (Gbl.Session.NumSessions/Cfg_TIMES_PER_SECOND_REFRESH_CONNECTED) * 1000UL;
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 *****/
strcpy (Gbl.Session.Id,Gbl.UniqueNameEncrypted);
/***** Check that session is not open *****/
if (Ses_CheckIfSessionExists (Gbl.Session.Id))
Lay_ShowErrorAndExit ("Can not create session.");
/***** Add session to database *****/
Ses_InsertSessionInDB ();
/***** Update time and course in connected list *****/
Con_UpdateMeInConnectedList ();
/***** 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)
{
char Query[512];
/***** Get if session already exists in database *****/
sprintf (Query,"SELECT COUNT(*) FROM sessions WHERE SessionId='%s'",
IdSes);
return (DB_QueryCOUNT (Query,"can not check if a session already existed") != 0);
}
/*****************************************************************************/
/************************** Close current session ****************************/
/*****************************************************************************/
void Ses_CloseSession (void)
{
if (Gbl.Usrs.Me.Logged)
{
/***** 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 *****/
Con_RemoveOldConnected ();
Ses_RemoveHiddenParFromExpiredSessions ();
/***** Now, user is not logged in *****/
Gbl.Usrs.Me.LoggedRoleBeforeCloseSession = Gbl.Usrs.Me.LoggedRole;
Gbl.Usrs.Me.Logged = false;
Gbl.Usrs.Me.IBelongToCurrentIns = false;
Gbl.Usrs.Me.IBelongToCurrentCtr = false;
Gbl.Usrs.Me.IBelongToCurrentDeg = false;
Gbl.Usrs.Me.IBelongToCurrentCrs = false;
Gbl.Usrs.Me.LoggedRole = Rol_UNKNOWN; // Don't uncomment this line. Don't change the role to unknown. Keep user's role in order to log the access
Gbl.Usrs.Me.MyCourses.Filled = false;
Gbl.Usrs.Me.MyCourses.Num = 0;
/***** Update number of open sessions in order to show them properly *****/
Ses_GetNumSessions ();
}
}
/*****************************************************************************/
/******************** Insert new session in the database *********************/
/*****************************************************************************/
void Ses_InsertSessionInDB (void)
{
char Query[1024];
/***** Insert session in the database *****/
sprintf (Query,"INSERT INTO sessions (SessionId,UsrCod,Password,Role,"
"CtyCod,InsCod,CtrCod,DegCod,CrsCod,WhatToSearch,LastTime,LastRefresh)"
" VALUES ('%s','%ld','%s','%u',"
"'%ld','%ld','%ld','%ld','%ld','%u',NOW(),NOW())",
Gbl.Session.Id,
Gbl.Usrs.Me.UsrDat.UsrCod,
Gbl.Usrs.Me.UsrDat.Password,
(unsigned) Gbl.Usrs.Me.LoggedRole,
Gbl.CurrentCty.Cty.CtyCod,
Gbl.CurrentIns.Ins.InsCod,
Gbl.CurrentCtr.Ctr.CtrCod,
Gbl.CurrentDeg.Deg.DegCod,
Gbl.CurrentCrs.Crs.CrsCod,
Gbl.Search.WhatToSearch);
DB_QueryINSERT (Query,"can not create session");
}
/*****************************************************************************/
/***************** Modify data of session in the database ********************/
/*****************************************************************************/
void Ses_UpdateSessionDataInDB (void)
{
char Query[1024];
/***** Update session in database *****/
sprintf (Query,"UPDATE 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.LoggedRole,
Gbl.CurrentCty.Cty.CtyCod,
Gbl.CurrentIns.Ins.InsCod,
Gbl.CurrentCtr.Ctr.CtrCod,
Gbl.CurrentDeg.Deg.DegCod,
Gbl.CurrentCrs.Crs.CrsCod,
Gbl.Session.Id);
DB_QueryUPDATE (Query,"can not update session");
}
/*****************************************************************************/
/******************** Modify session last refresh in database ****************/
/*****************************************************************************/
void Ses_UpdateSessionLastRefreshInDB (void)
{
char Query[512];
/***** Update session in database *****/
sprintf (Query,"UPDATE sessions SET LastRefresh=NOW()"
" WHERE SessionId='%s'",
Gbl.Session.Id);
DB_QueryUPDATE (Query,"can not update session");
}
/*****************************************************************************/
/********************** Remove session from the database *********************/
/*****************************************************************************/
static void Ses_RemoveSessionFromDB (void)
{
char Query[512];
/***** Remove current session *****/
sprintf (Query,"DELETE FROM sessions WHERE SessionId='%s'",
Gbl.Session.Id);
DB_QueryDELETE (Query,"can not remove a session");
/***** Clear old unused social timelines in database *****/
// This is necessary to prevent the table growing and growing
Soc_ClearOldTimelinesDB ();
}
/*****************************************************************************/
/*************************** Remove expired sessions *************************/
/*****************************************************************************/
void Ses_RemoveExpiredSessions (void)
{
char Query[1024];
/***** 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)) */
sprintf (Query,"DELETE LOW_PRIORITY FROM sessions WHERE"
" LastTimeLastTime+INTERVAL 1 SECOND"
" AND"
" LastRefresh MaxBytes);
}
/***** Free structure that stores the query result *****/
DB_FreeMySQLResult (&mysql_res);
}
if (ParameterIsTooBig)
{
sprintf (Gbl.Message,"Hidden parameter %s too large,"
" it exceed the maximum allowed size (%lu bytes).",
ParamName,(unsigned long) MaxBytes);
Lay_ShowErrorAndExit (Gbl.Message);
}
/***** Count number of values of the parameter *****/
Ptr = ParamValue;
while (*Ptr)
if (Par_GetNextStrUntilSeparParamMult (&Ptr,NULL,MaxBytes)) // Characters found?
NumTimes++;
return NumTimes;
}