#!/usr/bin/perl
package RVL::Session;
##LICENSE##
use CGI::Session;
use CGI;
use vars qw( %STATIC );
use define 'SGL_SESSION_UPDATE_WINDOW' => 10;
use Class::Std::Utils;
{
sub new {
my ($class, $opt) = @_;
my ($this) = bless( {}, $class);
my ($cgi) = new CGI;
my ($sessionName) = (RVL::Config::singleton()->get('cookie.name')) ? RVL::Config::singleton()->get('cookie.name') : 'RVLSESSID';
$sessionName = ($sessionName) ? $sessionName : 'RVLSESSID';
CGI::Session->name($sessionName);
my ($session) = new CGI::Session(undef, $cgi, {Directory=> RVL::CpHandle::singleton()->getHomeDir() . '/tmp'});
#$session->expire('+1M');
return $session;
}
sub singleton {
my ($class) = __PACKAGE__;
if (!defined $RVL::INSTANCE{$class}) {
$RVL::INSTANCE{$class} = $class->new();
}
return $RVL::INSTANCE{$class}
}
sub isFirstAnonRequest {
my ($clean) = @_;
#$RVL::Session::STATIC{'isFirstAnonRequest'}{'ret'}
if (defined $clean) {
RVL::Session::singleton()->clear('isFirstAnonRequest');
if (defined $RVL::Session::STATIC{'isFirstAnonRequest'}{'ret'}) {
delete $RVL::Session::STATIC{'isFirstAnonRequest'}{'ret'};
}
return 1;
} elsif (RVL::Session::getRoleId() eq __CONSTANT__::RVL_GUEST
&& !defined $RVL::Session::STATIC{'isFirstAnonRequest'}{'ret'}) {
$RVL::Session::STATIC{'isFirstAnonRequest'}{'ret'} = !defined(RVL::Session::singleton()->param('isFirstAnonRequest'));
if (!defined(RVL::Session::singleton()->param('isFirstAnonRequest'))) {
RVL::Session::singleton()->param(-name => 'isFirstAnonRequest', -value => 1);
}
return defined($RVL::Session::STATIC{'isFirstAnonRequest'}{'ret'})
? $RVL::Session::STATIC{'isFirstAnonRequest'}{'ret'}
: 0;
}
}
sub getRoleId {
if (RVL::Session::singleton()->param('rid')) {
return RVL::Session::singleton()->param('rid');
} else {
return '';
}
}
sub get {
my ($sessVarName) = $_[0];
return RVL::Session::singleton()->param($sessVarName);
}
=head3
RVL::Session::set({-name => value});
=cut
sub set {
my ($sessVarName, $sessVarValue) = @_;
RVL::Session::singleton()->param($sessVarName, $sessVarValue);
}
sub remove {
my ($sess) = $_[0];
RVL::Session::singleton()->clear($sess);
}
sub updateIdle {
my ($ret) = 0;
if (!RVL::Session::isTimedOut()) {
my ($time) = time();
my ($lastRefreshed) = defined(RVL::Session::singleton()->param('lastRefreshed'))
? RVL::Session::singleton()->param('lastRefreshed')
: 0;
my ($timeIdle) = $time - $lastRefreshed;
if ($timeIdle gt __CONSTANT__::RVL_SESSION_UPDATE_WINDOW ) {
RVL::Session::singleton()->param(-name => 'lastRefreshed', -value => time());
}
$ret = 1;
}
return $ret;
}
sub isTimedOut{
my ($currentTime) = time();
my ($lastPageRefreshTime) = defined(RVL::Session::singleton()->param('lastRefreshed'))
? RVL::Session::singleton()->param('lastRefreshed')
: 0;
my ($timeout) = defined(RVL::Session::singleton()->param('aPrefs.sessionTimeout'))
? RVL::Session::singleton()->param('aPrefs.sessionTimeout')
: 0;
if ($timeout eq 0) {
return 0;
}
my ($useTime) = $currentTime - $lastPageRefreshTime;
if ($useTime gt $timeout) {
return 1;
} else {
return 0;
}
}
sub isAnonymous {
if (defined RVL::Session::singleton()->param('uid')) {
return 0;
} else {
return 1;
}
}
}
1;
Copyright 2K16 - 2K18 Indonesian Hacker Rulez