blib/lib/OpenCA/Session.pm untested

filter: Statementstmt 0.0% 0 / 115 Branchbran 0.0% 0 / 26 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 0.0% 0 / 17 Podpod 0.0% 0 / 13 totaltotal 0.0% 0 / 176 CC 14 SCAR 23.1
CC
14
Cov
0%
new
20.0
update
20.0
load
12.0
CRAP 10.1
1 ## OpenCA::Session.pm
2 ##
3 ## Copyright (C) 2000-2003 Michael Bell <michael.bell@web.de>
4 ## All rights reserved.
5 ##
6 ## This library is free software; you can redistribute it and/or
7 ## modify it under the terms of the GNU Lesser General Public
8 ## License as published by the Free Software Foundation; either
9 ## version 2.1 of the License, or (at your option) any later version.
10 ##
11 ## This library is distributed in the hope that it will be useful,
12 ## but WITHOUT ANY WARRANTY; without even the implied warranty of
13 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 ## Lesser General Public License for more details.
15 ##
16 ## You should have received a copy of the GNU Lesser General Public
17 ## License along with this library; if not, write to the Free Software
18 ## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 ##
20  
21 use strict;
22  
23 package OpenCA::Session;
24  
25 use CGI::Session qw/-ip-match/;
26 use OpenCA::Log::Message;
27  
28 use FileHandle;
29 our ($STDERR, $STDOUT);
30 $STDOUT = \*STDOUT;
31 $STDERR = \*STDERR;
32  
33 our ($errno, $errval);
34  
35 ($OpenCA::Session::VERSION = '$Revision: 1.2 $' )=~ s/(?:^.*: (\d+))|(?:\s+\$$)/defined $1?"0\.9":""/eg;
36  
37 # Preloaded methods go here.
38  
39 ##
40 ## supported functions
41 ##
42 ## new
43 ##
44 ## load
45 ## update
46 ## start
47 ## stop
48 ## clear
49 ## getID
50 ##
51 ## cleanup
52 ##
53 ## getParam
54 ## setParam
55 ## loadParams
56 ## saveParams
57 ##
58  
59 ## Create an instance of the Class
60 sub new {
61 my $that = shift;
62 my $class = ref($that) || $that;
63  
64 my $self = {
65 DEBUG => 0,
66 debug_fd => $STDOUT,
67 ## debug_msg => ()
68 };
69  
70 bless $self, $class;
71  
72 my $keys = { @_ };
73 $self->{cgi} = $keys->{CGI};
74 $self->{lifetime} = 1200;
75 $self->{lifetime} = $keys->{LIFETIME} if ($keys->{LIFETIME});
76 $self->{DEBUG} = 1 if ($keys->{DEBUG});
77 $self->{dir} = $keys->{DIR};
78 $self->{journal} = $keys->{LOG};
79  
80 $self->{printed_header} = 0;
81  
82 print "Content-type: text/html\n\n" if ($self->{DEBUG});
83  
84 return $self;
85 }
86  
87 sub setError {
88 my $self = shift;
89  
90 if (scalar (@_) == 4) {
91 my $keys = { @_ };
92 $self->{errval} = $keys->{ERRVAL};
93 $self->{errno} = $keys->{ERRNO};
94 } else {
95 $self->{errno} = $_[0];
96 $self->{errval} = $_[1];
97 }
98 $errno = $self->{errno};
99 $errval = $self->{errval};
100  
101 $self->{journal}->{errno} = $self->{errno};
102 $self->{journal}->{errval} = $self->{errval};
103 $self->{journal}->{message} = "";
104 foreach my $msg (@{$self->{debug_msg}}) {
105 $self->{journal}->{message} .= $msg."\n";
106 }
107  
108 ## support for: return $self->setError (1234, "Something fails.") if (not $xyz);
109 return undef;
110 }
111  
112 #####################################
113 ## operate on the complete session ##
114 #####################################
115  
116 sub load {
117 my $self = shift;
118  
119 return undef if (not $self->{cgi}->cookie("CGISESSID"));
120  
121 $self->{session} = new CGI::Session(
122 undef,
123 $self->{cgi}->cookie("CGISESSID"),
124 {Directory=>$self->{dir}});
125  
126 return 1 if ($self->{session});
127  
128 ## this can happen if the session is timed out
129 return undef;
130 }
131  
132 sub start {
133 my $self = shift;
134  
135 ## destroy old session if present
136 if ($self->{session}) {
137 $self->{session}->delete;
138 undef ($self->{session});
139 }
140  
141 ## create new session
142 $self->{session} = new CGI::Session(
143 undef,
144 undef,
145 {Directory=>$self->{dir}});
146  
147 ## set lifetime
148 $self->{session}->expire($self->{lifetime});
149  
150 ## store cookie
151 $self->{session}->flush;
152  
153 ## prepare header
154 $self->{cookie} = $self->{cgi}->cookie(CGISESSID => $self->{session}->id);
155  
156 ## send header without content-type
157 if (not $self->{printed_header})
158 {
159 my $header = $self->{cgi}->header( -cookie=>$self->{cookie} );
160 $header =~ s/\n*Content-Type:[^\n]*\n*//s;
161 print $header;
162 $self->{printed_header} = 1;
163 }
164  
165 return 1;
166 }
167  
168 sub update {
169 my $self = shift;
170  
171 ## set lifetime
172 $self->{session}->expire($self->{lifetime});
173  
174 ## prepare header
175 $self->{cookie} = $self->{cgi}->cookie(CGISESSID => $self->{session}->id);
176  
177 ## send header without content-type
178 if (not $self->{printed_header})
179 {
180 my $header = $self->{cgi}->header( -cookie=>$self->{cookie} );
181 my @lines = split "\n", $header;
182 $header = "";
183 foreach my $line (@lines) {
184 $line = substr ($line, 0, length($line)-1);
185 next if (not $line);
186 next if ($line =~ /content-type/i);
187 $header .= $line."\n";
188 }
189 print $header;
190 $self->{printed_header} = 1;
191 }
192 $self->{session}->flush;
193  
194 return 1;
195 }
196  
197 sub stop {
198 my $self = shift;
199  
200 $self->{session}->delete;
201 undef ($self->{session});
202  
203 return 1;
204 }
205  
206 sub clear
207 {
208 my $self = shift;
209 $self->{session}->clear();
210 }
211  
212 sub getID
213 {
214 my $self = shift;
215 $self->{session}->id;
216 }
217  
218 #############################
219 ## operate on all sessions ##
220 #############################
221  
222 sub cleanup {
223  
224 my $self = shift;
225  
226 my $expired = 0;
227 my $dir = $self->{dir};
228  
229 ## load all sessions
230 opendir DIR, $dir;
231 my @session_files = grep /^(?!\.\.$).*/, grep /^(?!\.$)./, readdir DIR;
232 closedir DIR;
233  
234 return $expired if (not scalar @session_files);
235  
236 ## check every session
237 foreach my $session_file (@session_files)
238 {
239 ## extract session_id
240 $session_file =~ s/cgisess_//;
241  
242 ## load session
243 my $session = new CGI::Session(
244 undef,
245 $session_file,
246 {Directory=>$dir});
247  
248 $expired++ if (not $session);
249 }
250  
251 ## return the number of expired sessions
252 return $expired;
253 }
254  
255 ######################
256 ## param operations ##
257 ######################
258  
259 sub saveParams
260 {
261 my $self = shift;
262 $self->{session}->save_param ($self->{cgi});
263 $self->{session}->flush;
264 }
265  
266 sub loadParams
267 {
268 my $self = shift;
269 $self->{session}->load_param ($self->{cgi});
270 $self->{session}->flush;
271 }
272  
273 sub setParam
274 {
275 my $self = shift;
276 $self->{session}->param ($_[0], $_[1]);
277 $self->{session}->flush;
278 }
279  
280 sub getParam
281 {
282 my $self = shift;
283 $self->{session}->param ($_[0]);
284 }
285  
286 1;