line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Cot::Plugin::Session; |
2
|
1
|
|
|
1
|
|
837
|
use strict; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
36
|
|
3
|
1
|
|
|
1
|
|
5
|
use warnings; |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
31
|
|
4
|
1
|
|
|
1
|
|
40
|
use 5.008005; |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
51
|
|
5
|
|
|
|
|
|
|
our $VERSION = "0.07"; |
6
|
1
|
|
|
1
|
|
5
|
use parent qw(Cot::Plugin); |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
8
|
|
7
|
1
|
|
|
1
|
|
1620
|
use File::Spec; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
26
|
|
8
|
1
|
|
|
1
|
|
781
|
use Digest::SHA1 (); |
|
1
|
|
|
|
|
802
|
|
|
1
|
|
|
|
|
20
|
|
9
|
1
|
|
|
1
|
|
675
|
use YAML (); |
|
1
|
|
|
|
|
7799
|
|
|
1
|
|
|
|
|
24
|
|
10
|
1
|
|
|
1
|
|
10
|
use Carp; |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
85
|
|
11
|
|
|
|
|
|
|
our $SESSIONID = 'sessid'; |
12
|
1
|
|
|
1
|
|
6
|
use constant STOREDIR => '_cotsession'; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
340
|
|
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
sub init { |
15
|
1
|
|
|
1
|
0
|
81
|
my ( $self, $cot ) = @_; |
16
|
1
|
|
|
|
|
7
|
$self->{_app} = $cot; |
17
|
1
|
|
50
|
|
|
23
|
$self->{_dir} = File::Spec->catdir( $ENV{TMPDIR} || '/tmp', STOREDIR ); |
18
|
1
|
|
|
|
|
11
|
$cot->session($self); |
19
|
|
|
|
|
|
|
} |
20
|
|
|
|
|
|
|
|
21
|
|
|
|
|
|
|
sub load { |
22
|
0
|
|
|
0
|
0
|
|
my ( $self, $sessid ) = @_; |
23
|
0
|
|
0
|
|
|
|
$sessid ||= $self->{_app}->req->cookies->{$SESSIONID}; |
24
|
0
|
0
|
0
|
|
|
|
$sessid = Digest::SHA1::sha1_hex( rand() . $$ . {} . time ) |
25
|
|
|
|
|
|
|
unless ( $sessid && -f $self->_path($sessid) ); |
26
|
0
|
|
|
|
|
|
my $path = $self->_path($sessid); |
27
|
0
|
|
|
|
|
|
my $sessobj = {}; |
28
|
0
|
0
|
|
|
|
|
$sessobj = YAML::LoadFile($path) if ( -f $path ); |
29
|
0
|
|
|
|
|
|
new Cot::Plugin::Session::Object( |
30
|
|
|
|
|
|
|
{ |
31
|
|
|
|
|
|
|
_app => $self->{_app}, |
32
|
|
|
|
|
|
|
dir => $self->_dir($sessid), |
33
|
|
|
|
|
|
|
path => $path, |
34
|
|
|
|
|
|
|
obj => $sessobj, |
35
|
|
|
|
|
|
|
id => $sessid, |
36
|
|
|
|
|
|
|
} |
37
|
|
|
|
|
|
|
); |
38
|
|
|
|
|
|
|
} |
39
|
|
|
|
|
|
|
|
40
|
|
|
|
|
|
|
sub _dir { |
41
|
0
|
|
|
0
|
|
|
my ( $self, $sessid ) = @_; |
42
|
0
|
|
|
|
|
|
File::Spec->catdir( |
43
|
|
|
|
|
|
|
$self->{_dir}, |
44
|
|
|
|
|
|
|
substr( $sessid, 0, 2 ), |
45
|
|
|
|
|
|
|
substr( $sessid, 2, 2 ) |
46
|
|
|
|
|
|
|
); |
47
|
|
|
|
|
|
|
} |
48
|
|
|
|
|
|
|
|
49
|
|
|
|
|
|
|
sub _path { |
50
|
0
|
|
|
0
|
|
|
my ( $self, $sessid ) = @_; |
51
|
0
|
|
|
|
|
|
File::Spec->catfile( |
52
|
|
|
|
|
|
|
$self->{_dir}, |
53
|
|
|
|
|
|
|
substr( $sessid, 0, 2 ), |
54
|
|
|
|
|
|
|
substr( $sessid, 2, 2 ), $sessid |
55
|
|
|
|
|
|
|
); |
56
|
|
|
|
|
|
|
} |
57
|
|
|
|
|
|
|
|
58
|
|
|
|
|
|
|
package Cot::Plugin::Session::Object; |
59
|
1
|
|
|
1
|
|
5
|
use File::Path qw/mkpath/; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
251
|
|
60
|
|
|
|
|
|
|
|
61
|
|
|
|
|
|
|
sub new { |
62
|
0
|
|
|
0
|
|
|
my ( $class, $obj ) = @_; |
63
|
0
|
|
|
|
|
|
bless $obj, $class; |
64
|
|
|
|
|
|
|
} |
65
|
|
|
|
|
|
|
|
66
|
|
|
|
|
|
|
sub get { |
67
|
0
|
|
|
0
|
|
|
my ( $self, $k ) = @_; |
68
|
0
|
|
|
|
|
|
$self->{obj}->{$k}; |
69
|
|
|
|
|
|
|
} |
70
|
|
|
|
|
|
|
|
71
|
|
|
|
|
|
|
sub set { |
72
|
0
|
|
|
0
|
|
|
my ( $self, $k, $v ) = @_; |
73
|
0
|
|
|
|
|
|
$self->{obj}->{$k} = $v; |
74
|
0
|
0
|
|
|
|
|
mkpath( $self->{dir} ) unless -e $self->{dir}; |
75
|
0
|
|
|
|
|
|
YAML::DumpFile( $self->{path}, $self->{obj} ); |
76
|
|
|
|
|
|
|
} |
77
|
|
|
|
|
|
|
|
78
|
|
|
|
|
|
|
sub delete { |
79
|
0
|
|
|
0
|
|
|
my $self = shift; |
80
|
0
|
0
|
|
|
|
|
unlink $self->{path} if ( -f $self->{path} ); |
81
|
|
|
|
|
|
|
} |
82
|
|
|
|
|
|
|
|
83
|
|
|
|
|
|
|
sub bakecookie { |
84
|
0
|
|
|
0
|
|
|
my ( $self, %param ) = @_; |
85
|
0
|
|
|
|
|
|
$self->{_app}->res->cookies->{$Cot::Plugin::Session::SESSIONID} = { |
86
|
|
|
|
|
|
|
value => $self->{id}, |
87
|
|
|
|
|
|
|
%param, |
88
|
|
|
|
|
|
|
}; |
89
|
|
|
|
|
|
|
} |
90
|
|
|
|
|
|
|
|
91
|
|
|
|
|
|
|
1; |
92
|
|
|
|
|
|
|
__END__ |