<%doc>
# 
# $Id: session.ipfc,v 1.4 2002/01/28 22:09:45 fpmip Exp $
#
# session.ipfc manages the ipfc frontend user session cookies
# it implements server-side session timeout (failsafe)
#
# Author : Tycho Fruru <tycho@fruru.com>

# Copyright (C) 2001 Tycho Fruru <tycho@fruru.com>
# Copyright (C) 2001 Conostix S.A. Luxembourg - Tycho Fruru <tycho@conostix.com>

# This program is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the
# Free Software Foundation; either version 2 of the License, or (at your
# option) any later version.  See <http://www.fsf.org/copyleft/gpl.txt>.
#
# 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 General Public License
# for more details.
#
</%doc>
<%args>
$timeout=>1800
</%args>

<%perl>
use CGI::Cookie;
use Apache::Session::File;

my $session_id = $r->header_in('Cookie');

if (defined($session_id)) {
	$session_id =~ /ipfc_session=(\w*)/;
	$session_id = $1;
}

my %session;
my $good_session = 1;

if (defined($session_id)) {
  eval("tie %session,'Apache::Session::File','$session_id',
	{Directory => '/tmp/sessions',
	 LockDirectory => '/tmp/sessions'}");
  if ($@) 
  { 
#	$m->out($@);
	$good_session = 0; 
  }
} else {
	$good_session = 0;
}
  
if ($good_session) {
	if ($session{'expiration'} > time()) {
		$session{'expiration'}=time()+$timeout;
		return \%session;
	} else { # the session is expired
		$good_session = 0;
		tied(%session)->delete;
	}
}

# Here we know that it's an invalid session, we need to create a new one
tie %session,'Apache::Session::File',undef,
	{Directory => '/tmp/sessions',
	 LockDirectory => '/tmp/sessions'};
	 
my $c = new CGI::Cookie(-name=>'ipfc_session',
			-value=>$session{_session_id},
			-path=>'/',
			-expires=>'+1M');
$r->headers_out->add('Set-Cookie' => $c);

$session{'expiration'}=time()+$timeout;
$session{'authenticated'}=0;
#$session{'userid'}="unknown";

return \%session;
</%perl>
