added: 'remember me' flag when logging

added: the session file
       sessions can still be available between starting and stopping the cmslu system


git-svn-id: svn://ttmath.org/publicrep/cmslu/trunk@529 e52654a7-88a9-db11-a3e9-0013d4bc506e
This commit is contained in:
2009-11-20 23:09:52 +00:00
parent 848afac803
commit 4827c116f0
21 changed files with 443 additions and 36 deletions

View File

@@ -7,13 +7,20 @@
*
*/
#include <sys/stat.h>
#include "sessionmanager.h"
#include "request.h"
#include "log.h"
#include "data.h"
#include "session.h"
#include "sessionparser.h"
SessionManager::SessionManager()
{
session_checker = 0;
}
bool SessionManager::IsSession(long id)
{
@@ -87,7 +94,8 @@ int attempts = 100;
if( added )
{
request.session = &session_table.Back();
request.SetCookie(data.http_session_id_name.c_str(), request.session->id);
request.session->new_session = true;
log << log2 << "SM: created a new session: " << request.session->id << logend;
return;
@@ -119,6 +127,7 @@ void SessionManager::SetSession()
{
// that session is in the table
request.session = &(*s);
request.session->new_session = false;
session_table.UpdateLastTime(s, std::time(0));
@@ -163,12 +172,63 @@ SessionContainer::Iterator SessionManager::SessionEnd()
void SessionManager::DeleteOldSessions()
{
session_table.DelFirstByTimeInterval(data.session_max_iddle);
if( ++session_checker > 1000 )
{
// we make the test after 1000 requests
log << log3 << "SM: checking sessions which have 'remember me' flag set" << logend;
session_checker = 0;
session_table.DelFirstByTimeInterval(data.session_remember_max_iddle, false);
}
}
void SessionManager::LoadSessions()
{
SessionParser sp;
sp.Parse(data.session_file, session_table);
}
void SessionManager::SaveSessions()
{
if( data.session_file.empty() )
return;
std::ofstream file(data.session_file.c_str());
if( !file )
{
log << log1 << "SM: cannot open the session file for writing - sessions lost" << logend;
return;
}
log << log2 << "SM: saving sessions" << logend;
long len = 0;
SessionContainer::Iterator i = session_table.Begin();
for( ; i!=session_table.End() ; ++i )
{
if( i->id != 0 && i->puser )
{
file << i->id << ' ' << i->puser->id << ' ' << i->remember_me << ' ';
file << (long)i->time << ' ' << (long)i->last_time << std::endl;
++len;
}
}
file.close();
chmod(data.session_file.c_str(), 0600);
log << log2 << "SM: saved " << len << " session(s)" << logend;
}