# ==================================================================
# File manager - enhanced web based file management system
#
#   Website  : http://gossamer-threads.com/
#   Support  : http://gossamer-threads.com/scripts/support/
#   CVS Info : 087,071,086,086,085      
#   Revision : $Id: Session.pm,v 1.1 2007/12/19 23:32:47 bao Exp $
#
# Copyright (c) 2001 Gossamer Threads Inc.  All Rights Reserved.
# Redistribution in part or in whole strictly prohibited. Please
# see LICENSE file for full details.
# ==================================================================
package GT::FileMan::Session;

use strict;
use GT::Session::File;

sub session_valid {
# This function checks to see if the session is valid, and returns a
# hash of session information
#
    my $self = shift;

    my $session_path = "$self->{cfg}->{private_path}/sessions";

# Clear out old sessions.
    GT::Session::File->cleanup($self->{cfg}->{session}->{expiry} * 3600, $session_path);

# Validate the session
    my $session_id = $self->{in}->param('sid') || $self->{in}->cookie($self->{cfg}->{session}->{cookie}) || return;
    my $session    = new GT::Session::File (
        directory => $session_path,
        id        => $session_id
    ) || return;

# Update the session
    $session->save;

    return { id => $session_id, data => $session->{data}  };
}

sub session_create {
    my ($self, $user, $use_cookie) = @_;

    my $session_path = "$self->{cfg}->{private_path}/sessions";

# Clear out old sessions.
    GT::Session::File->cleanup($self->{cfg}->{session}->{expiry} * 3600, $session_path);

# Create a new session and save the information.
    my $session = new GT::Session::File (directory => $session_path);
    $session->{data}->{user} = $user->{username};
    $session->save;

# Now redirect to another URL and set cookies, or set URL string.
    if ($use_cookie) {
        print $self->{in}->cookie(
            -name  => $self->{cfg}->{session}->{cookie},
            -value => $session->{id},
            -path  => '/'
        )->cookie_header() . "\n";
    }
    else {
        $self->{cgi}->{sid} = $session->{id};
    }
    return { id => $session->{id}, data => $session->{data} };
}

sub session_delete {
    my $self = shift;

    print $self->{in}->cookie(
        -name  => $self->{cfg}->{session}->{cookie},
        -value => '',
        -path  => '/'
    )->cookie_header() . "\n";

    my $session_id = $self->{in}->cookie($self->{cfg}->{session}->{cookie}) || $self->{in}->param('sid') || return;
    my $session    = new GT::Session::File (
        directory => "$self->{cfg}->{private_path}/sessions",
        id        => $session_id
    ) || return;
    return $session->delete();
}

sub session_save {
    my ($self, $id, $args) = @_;

    return unless $id and $args;

    my $session_path = "$self->{cfg}->{private_path}/sessions";
    my $session      = new GT::Session::File (
        directory => $session_path,
        id        => $id
    );

    foreach (keys %$args) {
        next unless $args->{$_};
        $session->{data}->{$_} = $args->{$_};
    }
    $session->save();
}
1;
