App::ZofCMS::Plugin::Session - plugin for storing data across requests


App-ZofCMS-Plugin-Session documentation Contained in the App-ZofCMS-Plugin-Session distribution.

Index


Code Index:

NAME

Top

App::ZofCMS::Plugin::Session - plugin for storing data across requests

SYNOPSIS

Top

    plugins => [
        { Session => 2000 },
        { Sub     => 3000 },
    ],

    plugins2 => [
        qw/Session/,
    ],

    plug_session => {
        dsn     => "DBI:mysql:database=test;host=localhost",
        user    => 'test',
        pass    => 'test',
    },

    plug_sub => sub {
        my $t = shift;
        $t->{d}{session}{time} = localtime;
    },

DESCRIPTION

Top

The module is a plugin for App::ZofCMS that provides means to store data across HTTP requests.

The docs for this plugin are incomplete

This plugin requires ZofCMS version of at least 0.0211 where multi-level plugin sets are implemented

This documentation assumes you've read App::ZofCMS, App::ZofCMS::Config and App::ZofCMS::Template

FIRST-LEVEL ZofCMS TEMPLATE AND MAIN CONFIG FILE KEYS

Top

plugins

    plugins => [
        qw/Session/,
    ],

    plugins2 => [
        qw/Session/,
    ],

Important. This plugin requires to be executed twice. On first execution [currently] it will load the session data into $t->{d}{session} where $t is ZofCMS Template hashref. On second execution, it will save that data into an SQL table.

plug_session

    plug_session => {
        dsn     => "DBI:mysql:database=test;host=localhost",
        user    => 'test',
        pass    => 'test',
        opt     => { RaiseError => 1, AutoCommit => 1 },
        create_table => 1,
    },

Mandatory. The plug_session key takes a hashref as a value. The possible keys/values of that hashref are described below. There are quite a few more options to come - see source code - but those are untested and may be changed, thus use them at your own risk.

dsn

    dsn => "DBI:mysql:database=test;host=localhost",

Mandatory. Specifies the DSN for database, see DBI for more information on what to use here.

user and pass

        user    => 'test',
        pass    => 'test',

Semi-optional. The user and pass key should contain username and password for the SQL database that plugin will use. Defaults are: user is root and pass is set to undef.

opt

    opt => { RaiseError => 1, AutoCommit => 0 },

The opt key takes a hashref of any additional options you want to pass to connect_cached DBI's method.

Defaults to: { RaiseError => 1, AutoCommit => 0 },

table

    table   => 'session',

Optional. Takes a string as a value. Specifies the name of the SQL table that plugin will use to store data. Defaults to: session

create_table

    create_table => 1,

Optional. Takes either true or false values. When set to a true value, the plugin will automatically create the database table that it nees for operation. Defaults to: 0. Here is the table that it creates ($conf{table} is the table plugin's argument):

    CREATE TABLE `$conf{table}` (
        `id`      TEXT,
        `time`    VARCHAR(10),
        `data`    TEXT
    );

USAGE

Top

Currently just store your data in $t->{d}{session}. I suggest you use it as a hashref.

More options to come soon!

MORE INFO

Top

See source code, much of it is understandable (e.g. that session cookies last for 24 hours). I'll write better documentation once I get more time.

AUTHOR

Top

'Zoffix, <'zoffix at cpan.org'> (http://zoffix.com/, http://haslayout.net/, http://zofdesign.com/)

BUGS

Top

Please report any bugs or feature requests to bug-app-zofcms-plugin-session at rt.cpan.org, or through the web interface at http://rt.cpan.org/NoAuth/ReportBug.html?Queue=App-ZofCMS-Plugin-Session. I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.

SUPPORT

Top

You can find documentation for this module with the perldoc command.

    perldoc App::ZofCMS::Plugin::Session

You can also look for information at:

* RT: CPAN's request tracker

http://rt.cpan.org/NoAuth/Bugs.html?Dist=App-ZofCMS-Plugin-Session

* AnnoCPAN: Annotated CPAN documentation

http://annocpan.org/dist/App-ZofCMS-Plugin-Session

* CPAN Ratings

http://cpanratings.perl.org/d/App-ZofCMS-Plugin-Session

* Search CPAN

http://search.cpan.org/dist/App-ZofCMS-Plugin-Session

COPYRIGHT & LICENSE

Top


App-ZofCMS-Plugin-Session documentation Contained in the App-ZofCMS-Plugin-Session distribution.

package App::ZofCMS::Plugin::Session;

use warnings;
use strict;

our $VERSION = '0.0102';

use Storable qw/freeze thaw/;
use DBI;

sub new { bless {}, shift }

sub process {
    my ( $self, $template, $query, $config ) = @_;

    my %conf = (
        #dsn     => "DBI:mysql:database=test;host=localhost",
        user            => 'root',
        pass            => undef,
        opt             => { RaiseError => 1, AutoCommit => 1 },
        table           => 'session',
        create_table    => 0,
        cookie_name     => 'plug_session_id',
        cookie_expiry   => '+24h',
        auto            => 1,
        cell            => 'd',
        key             => 'session',
        no_op           => 0,
        no_load         => 0,
        no_save         => 0,
        session_expiry  => 86400, # 24 hours
        %{ $config->conf->{plug_session} || {} },
        %{ $template->{plug_session} || {} },
    );

    return
        if $conf{no_op};

    my $dbh = DBI->connect_cached(
        @conf{ qw/dsn user pass opt/ }
    );

    if ( $conf{create_table} ) {
        $dbh->do(
            "CREATE TABLE `$conf{table}` (
                                `id`      TEXT,
                                `time`    VARCHAR(10),
                                `data`    TEXT
                        );",
        );
    }

    my $session_id = $config->cgi->cookie( $conf{cookie_name} );

    if ( $conf{auto} ) {
        $template->{plug_session} = \%conf;
        if ( defined $template->{d}{session} ) {

            if ( defined $session_id ) {
                $dbh->do(
                    "UPDATE $conf{table} SET data = ?, time = ? WHERE id = ?",
                    undef,
                    freeze( $template->{d}{session} ),
                    time(),
                    $session_id,
                );
            }
            else {
                $session_id = rand() . time() . rand();
                $session_id =~ tr/.//d;
                my $cookie = $config->cgi->cookie(
                    -name       => $conf{cookie_name},
                    -path       => '/',
                    -value      => $session_id,
                    -expires    => $conf{cookie_expiry},
                );
                print "Set-Cookie: $cookie\n";

                $dbh->do(
                    "INSERT INTO $conf{table} VALUES( ?, ?, ? );",
                    undef,
                    $session_id,
                    time(),
                    freeze( $template->{ $conf{cell} }{ $conf{key} } ),
                );
            }
            
            $dbh->do(
                "DELETE FROM $conf{table} WHERE time < ?",
                undef,
                time() - $conf{session_expiry},
            );
        }
        elsif ( defined $session_id ) {
            my $session = $dbh->selectall_arrayref(
                "SELECT * FROM $conf{table} WHERE id = ?",
                { Slice => {} },
                $session_id,
            ) || [];

            @$session
                and $template->{ $conf{cell} }{ $conf{key} } = thaw $session->[0]{data};
        }
    }
}


1;
__END__