/
dump_session.coretag
132 lines (117 loc) · 3.43 KB
/
dump_session.coretag
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# Copyright 2002-2007 Interchange Development Group and others
#
# 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 the LICENSE file for details.
UserTag dump_session Order name
UserTag dump_session AddAttr
UserTag dump_session Version 1.8
UserTag dump_session Routine <<EOR
sub show_part {
my ($ref, $key) = @_;
return $ref unless $key;
if ($key eq 'SCALAR') {
my $newref = {};
foreach my $k (keys %$ref) {
next if ref $ref->{$k};
$newref->{$k} = $ref->{$k};
}
return $newref;
}
else {
return { $key, $ref->{$key} };
}
}
sub {
my ($name, $opt) = @_;
my $joiner = $opt->{joiner} || ' ';
return "Cannot dump or find sessions with session type $Vend::Cfg->{SessionType}."
if ($Vend::Cfg->{SessionType} ne 'File' && $Vend::Cfg->{SessionType} ne 'DBI');
if ($Vend::Cfg->{SessionType} eq 'File') {
if($opt->{find}) {
require File::Find;
my $expire = $Vend::Cfg->{SessionExpire};
if( int($::Variable->{ACTIVE_SESSION_MINUTES}) ) {
$expire = $::Variable->{ACTIVE_SESSION_MINUTES} * 60;
}
my $now = time();
$expire = $now - $expire;
my @files;
my $wanted = sub {
return unless -f $_;
return if (stat(_))[9] < $expire;
return if /\.lock$/;
push @files, $_;
};
File::Find::find($wanted, $Vend::Cfg->{SessionDatabase});
return join $joiner, @files;
}
elsif (! $name) {
return "dump-session: Nothing to do.";
}
else {
my $fn = Vend::Util::get_filename($name, 2, 1, $Vend::Cfg->{SessionDatabase});
return '' unless -f $fn;
my $ref = Vend::Util::eval_file($fn);
$ref = show_part($ref, $opt->{key}) if $opt->{key};
my $out = '';
eval {
$out = Vend::Util::uneval($ref);
};
return uneval($ref) if $@;
return $out;
}
}
if ($Vend::Cfg->{SessionType} eq 'DBI') {
if($opt->{find}) {
my $expire = $Vend::Cfg->{SessionExpire};
if( int($::Variable->{ACTIVE_SESSION_MINUTES}) ) {
$expire = $::Variable->{ACTIVE_SESSION_MINUTES} * 60;
}
my $now = time();
$expire = $now - $expire;
my @sesscodes;
my $db = Vend::Data::database_exists_ref($Vend::Cfg->{SessionDB})
or return errmsg("Table %s is not available", $Vend::Cfg->{SessionDB});
my $dbh = $db->dbh();
my $tname = $db->name();
my $sql = "select code from $tname where UNIX_TIMESTAMP(last_accessed) >= ?";
my $sth = $dbh->prepare($sql);
$sth->execute($expire) || return $DBI::errstr;
my $code;
$sth->bind_columns( undef, \$code);
while($sth->fetch) {
push @sesscodes, $code;
}
$sth->finish;
return join $joiner, @sesscodes;
}
elsif (! $name) {
return "dump-session: Nothing to do.";
}
else {
my $db = Vend::Data::database_exists_ref($Vend::Cfg->{SessionDB})
or return errmsg("Table %s is not available", $Vend::Cfg->{SessionDB});
my $dbh = $db->dbh();
my $tname = $db->name();
my $sql = "select session from $tname where code=?";
my $sth = $dbh->prepare($sql);
$sth->execute($name);
my $session;
$sth->bind_columns( undef, \$session);
$sth->fetch;
$sth->finish;
my $out = '';
my $ref = Vend::Util::evalr($session);
## Allow show of only part
$ref = show_part($ref, $opt->{key}) if $opt->{key};
eval {
$out = Vend::Util::uneval($ref);
};
return uneval($ref) if $@;
return $out;
}
}
}
EOR