summaryrefslogtreecommitdiff
path: root/fml/lib/FML/CGI/Anonymous/DB.pm
blob: 598066f12473b9fb0206734845d65643b3d9a3b6 (plain)
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
#-*- perl -*-
#
#  Copyright (C) 2008 Ken'ichi Fukamachi
#   All rights reserved. This program is free software; you can
#   redistribute it and/or modify it under the same terms as Perl itself.
#
# $FML: @template.pm,v 1.12 2008/08/24 08:28:36 fukachan Exp $
#

package FML::CGI::Anonymous::DB;
use strict;
use vars qw(@ISA @EXPORT @EXPORT_OK $AUTOLOAD);
use Carp;

=head1 NAME

FML::CGI::Anonymous::DB - anonymous user database.

=head1 SYNOPSIS

=head1 DESCRIPTION

=head1 METHODS

=head2 new()

constructor.

=cut


# Descriptions: constructor.
#    Arguments: OBJ($self) OBJ($curproc)
# Side Effects: none
# Return Value: OBJ
sub new
{
    my ($self, $curproc) = @_;
    my ($type) = ref($self) || $self;
    my $me     = { _curproc => $curproc };
    return bless $me, $type;
}


# Descriptions: assign a magic string and an identifier.
#               see FML::CGI::Skin::Anonymous::run_cgi_main()
#               for more details.
#    Arguments: OBJ($self)
# Side Effects: update PCB and database.
# Return Value: none
sub assign_id
{
    my ($self)    = @_;
    my ($curproc) = $self->{ _curproc };
    my ($config)  = $curproc->config();
    my ($pcb)     = $curproc->pcb();

    # generate a session identifier and a challenge magic string.
    use FML::String::Random;
    my $string = new FML::String::Random;
    my $magic_string = $string->magic_string();
    my $session_id   = $string->identifier($magic_string);
    $self->set_session_id($session_id);
    $self->set_magic_string($magic_string);

    # save them and the request into the temporal database => {
    #   session_id-$session_id   => $session_id,
    #   magic_string-$session_id => $magic_string.
    # };
    my $expire    = $config->as_second("anonymous_cgi_expire_limit") || 300;
    my $confirm   = $self->_db_open();
    $confirm->set($session_id, "session_id",   $session_id);
    $confirm->set($session_id, "magic_string", $magic_string);
    $confirm->set($session_id, "expire_time",  time + $expire);
}


=head1 UTILITIES

=head2 set_session_id($session_id)

save the current session_id in PCB.

=head2 get_session_id()

get the current session_id.

=head2 set_magic_string($magic_string)

save the current magic string in PCB.

=head2 get_magic_string()

get the current magic string.

=cut


# Descriptions: save session_id in PCB.
#    Arguments: OBJ($self) STR($session_id)
# Side Effects: update pcb.
# Return Value: none
sub set_session_id
{
    my ($self, $session_id) = @_;
    my ($curproc) = $self->{ _curproc };
    my ($pcb)     = $curproc->pcb();

    $pcb->set("cgi", "anonymous_session_id", $session_id);
}


# Descriptions: get the current session_id in PCB.
#    Arguments: OBJ($self)
# Side Effects: none
# Return Value: STR
sub get_session_id
{
    my ($self)    = @_;
    my ($curproc) = $self->{ _curproc };
    my ($pcb)     = $curproc->pcb();

    if (defined $pcb) {
	return $pcb->get("cgi", "anonymous_session_id");
    }
    else {
	$curproc->logerror("get_session_id: no pcb");
	return undef;
    }
}


# Descriptions: save magic string in PCB.
#    Arguments: OBJ($self) STR($magic_string)
# Side Effects: update pcb.
# Return Value: none
sub set_magic_string
{
    my ($self, $magic_string) = @_;
    my ($curproc) = $self->{ _curproc };
    my ($pcb)     = $curproc->pcb();

    $pcb->set("cgi", "magic_string", $magic_string);
}


# Descriptions: get the current magic string in PCB.
#    Arguments: OBJ($self)
# Side Effects: none
# Return Value: STR
sub get_magic_string
{
    my ($self)    = @_;
    my ($curproc) = $self->{ _curproc };
    my ($pcb)     = $curproc->pcb();

    if (defined $pcb) {
	return $pcb->get("cgi", "magic_string");
    }
    else {
	$curproc->logerror("get_magic_string: no pcb");
	return undef;
    }
}


# Descriptions: check if the request for this session_id is too old ?
#    Arguments: OBJ($self) STR($session_id)
# Side Effects: none
# Return Value: NUM(1 or 0)
sub is_expired
{
    my ($self, $session_id) = @_;
    my $confirm     = $self->_db_open();
    my $expire_time = $confirm->get($session_id, "expire_time") || 0;

    return( time > $expire_time ? 1 : 0 );
}


# Descriptions: check if the $magic_string is correct for $session_id.
#    Arguments: OBJ($self) STR($session_id) STR($magic_string)
# Side Effects: none
# Return Value: NUM(1 or 0)
sub is_correct_magic_string
{
    my ($self, $session_id, $magic_string) = @_;
    my $confirm         = $self->_db_open();
    my $db_session_id   = $confirm->get($session_id, "session_id");
    my $db_magic_string = $confirm->get($session_id, "magic_string");

    # case insensitive
    $magic_string    =~ tr/A-Z/a-z/;
    $db_magic_string =~ tr/A-Z/a-z/;
    if ($magic_string eq $db_magic_string) {
	return 1;
    }
    else {
	return 0;
    }
}


=head1 DATABASE OPERATIONS

private. 

DO NOT USE THESE METHODS.

=cut


# Descriptions: open the database.
#    Arguments: OBJ($self)
# Side Effects: none
# Return Value: OBJ
sub _db_open
{
    my ($self)    = @_;
    my ($curproc) = $self->{ _curproc };
    my ($config)  = $curproc->config();
    my ($pcb)     = $curproc->pcb();

    use FML::Confirm;
    my $cache_dir = $config->{ db_dir };
    my $confirm   = new FML::Confirm $curproc, {
	keyword   => "confirm",
	cache_dir => $cache_dir,
	class     => "cgi",
	address   => "dummy",
	buffer    => "dummy",
    };
    return $confirm;
}


# Descriptions: close the database (dummy).
#    Arguments: OBJ($self)
# Side Effects: none
# Return Value: none
sub _db_close
{
    my ($self) = @_;
}


=head1 CODING STYLE

See C<http://www.fml.org/software/FNF/> on fml coding style guide.

=head1 AUTHOR

Ken'ichi Fukamachi

=head1 COPYRIGHT

Copyright (C) 2008 Ken'ichi Fukamachi

All rights reserved. This program is free software; you can
redistribute it and/or modify it under the same terms as Perl itself.

=head1 HISTORY

FML::CGI::Anonymous::DB appeared in fml8 mailing list driver package.
See C<http://www.fml.org/> for more details.

=cut


1;