2008-12-10 05:42:49 +01:00
|
|
|
/*
|
|
|
|
* This file is a part of CMSLU -- Content Management System like Unix
|
|
|
|
* and is not publicly distributed
|
|
|
|
*
|
2009-04-21 22:50:55 +02:00
|
|
|
* Copyright (c) 2008-2009, Tomasz Sowa
|
2008-12-10 05:42:49 +01:00
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "sessionmanager.h"
|
2009-04-21 22:50:55 +02:00
|
|
|
#include "request.h"
|
|
|
|
#include "log.h"
|
|
|
|
#include "data.h"
|
|
|
|
#include "session.h"
|
|
|
|
|
2008-12-10 05:42:49 +01:00
|
|
|
|
|
|
|
|
2009-01-31 07:53:36 +01:00
|
|
|
bool SessionManager::IsSession(long id)
|
2008-12-10 05:42:49 +01:00
|
|
|
{
|
2009-01-31 07:53:36 +01:00
|
|
|
if( session_table.FindById(id) == session_table.End() )
|
2008-12-10 05:42:49 +01:00
|
|
|
return false;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
long SessionManager::CreateSessionId()
|
|
|
|
{
|
|
|
|
long id;
|
|
|
|
|
|
|
|
// make sure to call std::srand() somewhere at the beginning
|
|
|
|
// id must be != 0 (0 is reserved)
|
|
|
|
do
|
|
|
|
{
|
|
|
|
if( sizeof(long) == 8 )
|
|
|
|
{
|
|
|
|
id = ((unsigned long)std::rand()) << 32 + std::rand();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
id = std::rand();
|
|
|
|
}
|
|
|
|
|
|
|
|
id += std::time(0);
|
|
|
|
|
|
|
|
if( id < 0 )
|
|
|
|
id = -id;
|
|
|
|
|
|
|
|
}
|
|
|
|
while( id == 0 ); // 0 reserved for a temporary session
|
|
|
|
|
|
|
|
|
|
|
|
return id;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void SessionManager::CreateTemporarySession()
|
|
|
|
{
|
2009-01-31 07:53:36 +01:00
|
|
|
SessionContainer::Iterator i = session_table.FindById( 0 );
|
2008-12-10 05:42:49 +01:00
|
|
|
|
2009-01-31 07:53:36 +01:00
|
|
|
if( i == session_table.End() )
|
2008-12-10 05:42:49 +01:00
|
|
|
{
|
2009-01-31 07:53:36 +01:00
|
|
|
Session s;
|
|
|
|
s.id = 0;
|
|
|
|
|
|
|
|
session_table.PushBack(s);
|
|
|
|
request.session = &session_table.Back();
|
2008-12-10 05:42:49 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2009-01-31 07:53:36 +01:00
|
|
|
request.session = &(*i);
|
2008-12-10 05:42:49 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void SessionManager::CreateSession()
|
|
|
|
{
|
|
|
|
Session s;
|
|
|
|
int attempts = 100;
|
|
|
|
|
|
|
|
for( ; attempts > 0 ; --attempts )
|
|
|
|
{
|
|
|
|
s.id = CreateSessionId();
|
|
|
|
|
2009-01-31 07:53:36 +01:00
|
|
|
bool added = session_table.PushBack(s);
|
2008-12-10 05:42:49 +01:00
|
|
|
|
2009-01-31 07:53:36 +01:00
|
|
|
if( added )
|
2008-12-10 05:42:49 +01:00
|
|
|
{
|
2009-01-31 07:53:36 +01:00
|
|
|
request.session = &session_table.Back();
|
2008-12-10 05:42:49 +01:00
|
|
|
request.SetCookie(data.http_session_id_name.c_str(), request.session->id);
|
2009-01-31 07:53:36 +01:00
|
|
|
log << log2 << "SM: created a new session: " << request.session->id << logend;
|
2008-12-10 05:42:49 +01:00
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// there is a problem with generating a new session id
|
|
|
|
// we do not set a session cookie
|
|
|
|
CreateTemporarySession();
|
2008-12-31 14:36:46 +01:00
|
|
|
log << log1 << "SM: cannot create a session id (temporary used: with id 0)" << logend;
|
2008-12-10 05:42:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void SessionManager::SetSession()
|
|
|
|
{
|
|
|
|
CookieTable::iterator i = request.cookie_table.find(data.http_session_id_name);
|
|
|
|
|
|
|
|
if( i == request.cookie_table.end() )
|
|
|
|
{
|
|
|
|
CreateSession();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2009-01-31 07:53:36 +01:00
|
|
|
long id = atol(i->second.c_str());
|
|
|
|
SessionContainer::Iterator s = session_table.FindById(id);
|
2008-12-10 05:42:49 +01:00
|
|
|
|
2009-01-31 07:53:36 +01:00
|
|
|
if( s != session_table.End() )
|
2008-12-10 05:42:49 +01:00
|
|
|
{
|
|
|
|
// that session is in the table
|
2009-01-31 07:53:36 +01:00
|
|
|
request.session = &(*s);
|
2009-01-26 21:49:28 +01:00
|
|
|
|
2009-01-31 07:53:36 +01:00
|
|
|
session_table.UpdateLastTime(s, std::time(0));
|
|
|
|
|
2009-01-26 21:49:28 +01:00
|
|
|
log << log2 << "SM: session: " << s->id;
|
|
|
|
|
|
|
|
if( request.session->puser )
|
|
|
|
log << log2 << ", user: " << request.session->puser->name << ", id: " << request.session->puser->id;
|
|
|
|
|
|
|
|
log << log2 << logend;
|
2008-12-10 05:42:49 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// there is no such a session
|
|
|
|
// deleting the old cookie
|
|
|
|
request.cookie_table.erase(i);
|
|
|
|
|
|
|
|
// and creating a new one
|
|
|
|
CreateSession();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// request.session is set now
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2009-01-31 07:53:36 +01:00
|
|
|
SessionContainer::Iterator SessionManager::SessionBegin()
|
|
|
|
{
|
|
|
|
return session_table.Begin();
|
|
|
|
}
|
|
|
|
|
|
|
|
SessionContainer::Iterator SessionManager::SessionEnd()
|
|
|
|
{
|
|
|
|
return session_table.End();
|
|
|
|
}
|
2008-12-10 05:42:49 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2009-01-31 07:53:36 +01:00
|
|
|
void SessionManager::DeleteOldSessions()
|
|
|
|
{
|
|
|
|
session_table.DelFirstByTimeInterval(data.session_max_iddle);
|
|
|
|
}
|
2008-12-10 05:42:49 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|