CAM-Session

 view release on metacpan or  search on metacpan

Build.PL  view on Meta::CPAN

use Module::Build;
Module::Build->new(
                   module_name => 'CAM::Session',
                   license => 'perl',
                   requires => {
                      'CGI'                      => '2.0',
                      'CGI::Cookie'              => '1.0',
                      'DBI'                      => '1.0',
                      'Digest::MD5'              => '2.0',
                   },
                   build_requires => {
                      'Test::More'               => '0.01',
                      'DBD::mysql'               => '0.01',
                   },
                   )->create_build_script;

META.yml  view on Meta::CPAN

---
name: CAM-Session
version: 1.03
author:
  - Clotho Advanced Media Inc., I<cpan@clotho.com>
abstract: DBI and cookie CGI session state maintenance
license: perl
requires:
  CGI: 2.0
  CGI::Cookie: 1.0
  DBI: 1.0
  Digest::MD5: 2.0
build_requires:
  DBD::mysql: 0.01
  Test::More: 0.01
provides:
  CAM::Session:
    file: lib/CAM/Session.pm
    version: 1.03
generated_by: Module::Build version 0.2609

Makefile.PL  view on Meta::CPAN

use ExtUtils::MakeMaker;
WriteMakefile(
              'NAME'		=> 'CAM::Session',
              'VERSION_FROM'	=> 'lib/CAM/Session.pm',
              'PREREQ_PM'		=> {
                 'CGI'                      => '2.0',
                 'CGI::Cookie'              => '1.0',
                 'DBI'                      => '1.0',
                 'Digest::MD5'              => '2.0',
                 # Build depends:
                 'Test::More'               => '0.01',
                 'DBD::mysql'               => '0.01',
              },
              'PL_FILES' => {},
              ($] >= 5.005 ?
               (ABSTRACT_FROM => 'lib/CAM/Session.pm',
                AUTHOR        => 'Clotho Advanced Media Inc. <cpan@clotho.com>') : ()),

lib/CAM/Session.pm  view on Meta::CPAN

See README for more detail.

=head1 SYNOPSIS

  use CAM::Session;
  use DBI;
  my $dbh = DBI->connect(...);
  CAM::Session->setDBH($dbh);
  
  my $session = new CAM::Session();
  $session->printCookie();
  
  $session->set("username", $username);
  ...
  $session->get("username", $username);
  $session->delete("username");

To periodically clean up the session table, run a script like the
following as a daily scheduled task:

  use CAM::Session;

lib/CAM/Session.pm  view on Meta::CPAN

visitor's cookie to create a storage space for persistent data.

=cut

#----------------

require 5.005_62;
use strict;
use warnings;
use Carp;
use CGI::Cookie;
use CGI;
use DBI;

our @ISA = qw();
our $VERSION = '1.03';

# global settings, can be overridden for the whole class or for
# individual instances.
our $global_expiration = 24*60*60;  # one day, in seconds
our $global_dbh = undef;

lib/CAM/Session.pm  view on Meta::CPAN

      &carp("No database connection has been specified.  Please use ".$pkg."::setDBH()");
      return undef;
   }
   if (!ref($self->{dbh}) || ref($self->{dbh}) !~ /^(DBI|DBD)\b/)
   {
      my $type = ref($self->{dbh}) ? ref($self->{dbh}) : "scalar";
      &carp("The DBH object is not a valid DBI/DBD connection: $type");
      return undef;
   }

   my %cookies = CGI::Cookie->fetch();
   if (exists $cookies{$self->{cookieName}})
   {
      # existing session
      $self->{id} = $cookies{$self->{cookieName}}->value;
      if (!$self->loadSessionData())
      {
         $self->_newSession();
      }
   }
   else

lib/CAM/Session.pm  view on Meta::CPAN


=cut

sub getID
{
   my $self = shift;
   return $self->{id};
}
#----------------

=item getCookie

Return a cookie that indicates this session.  Any arguments are passed
to CGI::Cookie::new().  Use this, for example, with

    print CGI->header(-cookie => $session->getCookie);

=cut

sub getCookie
{
   my $self = shift;

   my $id = $self->getID();
   my $cookie = CGI::Cookie->new(-name => $self->{cookieName},
                                 -value => $id,
                                 -path => "/",
                                 @_);
   return $cookie;
}
#----------------

=item printCookie

Outputs a cookie that indicates this session.  Use this just before
"print CGI->header()", for example.

=cut

sub printCookie
{
   my $self = shift;

   my $cookie = $self->getCookie(@_);
   print "Set-Cookie: $cookie\n";
}
#----------------

=item getAll

Retrieve a hash of all of the session data.

=cut

sub getAll

lib/CAM/Session.pm  view on Meta::CPAN

=cut

sub setTableName
{
   my $pkg = shift; # unused
   my $val = shift;
   $global_dbTablename = $val;
}
#----------------

=item setCookieName NAME

Set the name of the cookie that is used for the recording the session.
This is a class method.

Use like this:

  CAM::Session->setCookieName($name);

=cut

sub setCookieName
{
   my $pkg = shift; # unused
   my $val = shift;
   $global_cookieName = $val;
}
#----------------


# PRIVATE FUNCTION
sub _implode

test.pl  view on Meta::CPAN

           # Hack: get the number of tests we expect, skip all but one
           # This hack relies on the soliton nature of Test::Builder
           Test::Builder->new()->expected_tests() - 
           Test::Builder->new()->current_test());
   }

   $dbh->do("drop table $config{table}"); # don't care if it fails

   ok(CAM::Session->setDBH($dbh), "setDBH");
   ok(CAM::Session->setTableName($config{table}), "setTableName");
   ok(CAM::Session->setCookieName($config{cookie}), "setCookieName");
   ok(CAM::Session->setup(), "setup");
   ok(CAM::Session->clean(), "clean");

   my %data;
   my $session;
   my $newsession;
   my $cookie;
   my $cookiedata;

   $session = CAM::Session->new();
   ok($session, "new");
   ok($session->isNewSession(), "isNewSession");

   $cookie = $session->getCookie();
   ok($cookie, "getCookie");
   ok($cookie =~ /^$config{cookie}/, "getCookie");

   ($cookiedata = $cookie) =~ s/;.*//;
   ok($cookiedata, "extract data from cookie");

   {
      local *FILE;
      my $filename = "test.tmp$$";
      open(FILE, ">$filename") or die "Failed to write temp file $filename";
      local *STDOUT = *FILE;
      $session->printCookie();
      close FILE;
      open(FILE, "<$filename") or die "Failed to read temp file $filename";
      my $out = join("", <FILE>);
      close FILE;
      unlink($filename) or die "Failed to delete temp file $filename";

      is($out, "Set-Cookie: $cookie\n", "printCookie");
   }

   %data = $session->getAll();
   is(scalar keys %data, 0, "empty cookie");

   #warn $cookie,"\n";
   #warn $cookiedata,"\n";

   # Hack: pretend we just got this cookie
   $ENV{HTTP_COOKIE} = $cookiedata;



( run in 0.779 second using v1.01-cache-2.11-cpan-e9199f4ba4c )