Session/blib/lib/CGI/Session/CookieJar/DBI.pm

filter: Statementstmt 8.9% 22 / 245 Branchbran 0.0% 0 / 106 Conditioncond 0.0% 0 / 18 MC/DCmcdc 0.0% 0 / 11 Subroutinesub 10.8% 4 / 37 Podpod 62.5% 20 / 32 totaltotal 10.2% 46 / 449 CC 60 SCAR 45.5
CC
60
Cov
6%
set_session
306.0
session
182.0
_param
72.0
CRAP 95.0
« blib/lib/CGI/Session.pm ↑ Summary blib/lib/CGI/Session/Authenticator/NetLDAP.pm »
1 #!/usr/local/bin/perl5
2 #
3  
4 package CGI::Session::CookieJar::DBI;
5  
6 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use Time::Local;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use CGI::Session::CookieJar;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 1 ▶use vars qw( @ISA );
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 push @ISA, qw( CGI::Session::CookieJar );
12  
13 # Mechanisms for managing databases.
14 #
15 1 my $DB_MYSQL='DB_MYSQL';
16 0 ▶sub db_type { _param( shift, '-db_type', @_ ); }
Subroutine db_type: not called CC=1 SCAR=6.9
Pod undocumented
17 0 ▶sub use_mysql { shift->db_type( $DB_MYSQL ); }
Subroutine use_mysql: not called CC=1 SCAR=6.9
Pod undocumented
18 0 ▶sub db { _param( shift, '-db', @_ ); }
Subroutine db: not called CC=1 SCAR=6.9
Pod undocumented
19  
20 1 my $I_USER = 'user';
21 1 my $I_COOKIE = 'cookie';
22 1 my $I_COOKIE_NAME = 'cookie_name';
23 1 my $I_EXPIRATION = 'expiration';
24 1 my $I_PASSKEY = 'passkey';
25 1 my $I_SERVER_SIDE_DATA = 'server_side_data';
26  
27  
28 sub new
29 0 ▶ {
Subroutine new: not called CC=2 SCAR=17.9
Pod undocumented
30 0 my ( $type ) = shift;
31 0 my $self = {};
32 0 bless $self, $type;
33 0 $self->use_mysql;
34 0 $self->set( -cookie_table => 'sessions',
35 -user_column => 'user',
36 -cookie_column => 'cookie',
37 -cookie_name_column => 'cookie_name',
38 -passkey_column => 'passkey',
39 -login_expiration_column => 'expiration',
40 -server_side_data_column => 'data',
41 -host => 'localhost', );
42  
43 0 ▶ $self->set(@_) if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 0 0
44 0 return $self;
45 }
46  
47  
48 =name CGI::Session::CookieJar::DBI
49  
50 A DBI Based CookieJar.
51  
52 The general usage is as follows:
53  
54 my $cookiejar;
55 $cookiejar = new CGI::Session::CookieJar::DBI();
56 $cookiejar->use_mysql();
57 $cookiejar->user( 'MyMYSQLUser' );
58 $cookiejar->password( 'lijlkdfsf' );
59 $cookiejar->database( 'DBI' );
60 $cookiejar->host( 'my.dbi.server.myco.com' );
61 $cookiejar->open();
62 ...
63 ... cookie operations
64 ...
65 $cookiejar->close();
66
67  
68 Most functions manipulating cookies use "queries" to specify the
69 cookies which will be operated upon. These queries are references to
70 associative arrays. The keys indicate variables which will be
71 compared, and the values specify the query operations. These
72 intersection of all the results determines which ones will be
73 selected.
74  
75 Here are examples of simple queries:
76  
77 { user => "bob" } selects all cookies with the user value of "bob"
78  
79 { expiration => "<".time() } selects all cookies which have already
80 expired
81  
82 { expiration => ">".time() } selects all cookies which have not
83 expired
84  
85 { cookie_name => "$cookie_name" } selects all cookies which are named $cookie_name
86  
87 { cookie => "$cookie" } selects all cookies which have the value $cookie
88  
89 { passkey => "435765" } selects all cookies which have the passkey value
90 of "435765"
91  
92  
93 { user => "bob", passkey => "6578" } selects all cookies which have the user
94 "bob" and the passkey set to "6578"
95  
96 There is no way to select a union of search results.
97  
98 =cut
99  
100  
101  
102 =item CGI::Session::CookieJar::DBI::open
103  
104 $cookiejar->open();
105  
106 Opens up the cookie jar. This must be called before any operations
107 can take place. When you are through with the cookie jar the close
108 operation must be called.
109  
110 =cut
111  
112 sub open
113 0 ▶ {
Subroutine open: not called CC=5 SCAR=34.0
Pod documented
114 0 my $self = shift;
115 0 $self->error( undef );
116  
117 0 my $db = $self->db;
118  
119 0 ▶ if ( $db )
Branch: 1 branch
BranchTrueFalseTotal
if ($db) 0 0 0
120 {
121 # Open should be safe to be called more than once.
122 #
123 # $self->error( "Attempted to open a database connection without closing the previous one." );
124 0 return;
125 }
126  
127 0 ▶ if ( $self->db_type eq $DB_MYSQL )
Branch: 1 branch
BranchTrueFalseTotal
if ($self->db_type eq $DB_MYSQL) { } 0 0 0
128 {
129 0 my $host = $self->host;
130 0 my $database = $self->database;
131 0 ▶ $db = DBI->connect( "DBI:mysql:$database".($host ? ":$host" : ""), $self->user, $self->password );
Branch: 1 branch
BranchTrueFalseTotal
$host ? : 0 0 0
132 0 ▶ if ( !$db )
Branch: 1 branch
BranchTrueFalseTotal
unless ($db) 0 0 0
133 {
134 0 $self->error( sprintf( "Could not connect to backend database: %s, %s", $DBI::err, $DBI::errstr ) );
135 0 return;
136 }
137 0 $self->db( $db );
138 }
139 else
140 {
141 0 $self->error( "Could not determine the type of database that should be connected to." );
142 }
143 }
144  
145 =item CGI::Session::CookieJar::DBI::close
146  
147 $cookiejar->close;
148  
149 Closes a previously opened cookie jar. This must be done before your program ends.
150  
151 =cut
152  
153 sub close
154 0 ▶ {
Subroutine close: not called CC=2 SCAR=17.9
Pod documented
155 0 my $self=shift;
156 0 $self->error( undef );
157 0 ▶ return unless defined $self->db();
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->db 0 0 0
158 0 $self->db->disconnect;
159 0 $self->db( undef );
160 }
161  
162  
163 =item CGI::Session::CookieJar::DBI::contains
164  
165 Determines if a session contains a given cookie.
166  
167 my $time = time;
168 my %query = ( -user=>'bob',
169 -cookie_name=>'sdfljkjj48jf',
170 -cookie=>'3476dfgh',
171 -passkey=>'23438',
172 -expiration=>'>$time' );
173  
174 my $has_cookie = $cookie_jar->contains( -query => \$query );
175  
176 =cut
177  
178 sub contains
179 0 ▶ {
Subroutine contains: not called CC=5 SCAR=34.0
Pod documented
180 0 my ( $self ) = shift;
181 0 $self->error(undef);
182 0 ▶ return unless $self->has_valid_connection;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->has_valid_connection 0 0 0
183 0 my $db = $self->db;
184 0 my $query = $self->build_where_query(@_);
185 0 my $select = sprintf( "SELECT count(*) FROM %s WHERE $query", $self->cookie_table );
186 0 my $statement = $db->prepare( $select );
187 0 my $rows = $statement->execute();
188 0 ▶ if ( $self->db_error( "Encountered DBI error: %s" ) )
Branch: 1 branch
BranchTrueFalseTotal
if ($self->db_error("Encountered DBI error: %s")) 0 0 0
189 {
190 0 $statement->finish;
191 0 return;
192 }
193 0 ▶ if ( $self->db_type eq $DB_MYSQL and $rows != 1 )
Branch: 1 branch
BranchTrueFalseTotal
if ($self->db_type eq $DB_MYSQL and $rows != 1) 0 0 0
Condition0%0/3
$self->db_type eq $DB_MYSQL and $rows != 1
!ll&&!rl&&r
000
MC/DC0%0/2
$self->db_type eq $DB_MYSQL and $rows != 1
$self->db_type eq $DB_MYSQL $rows != 1
Truth table0%0/3
A and B
A= $self->db_type eq $DB_MYSQLB= $rows != 1
ABresult
0X0
100
111
194 {
195 0 $self->error( "SQL should have generated one result, but it resulted in $rows" );
196 0 $statement->finish;
197 0 return;
198 }
199 0 my @count = $statement->fetchrow_array();
200 0 my $matches = $count[0];
201 0 $statement->finish;
202 0 return $matches != 0;
203 }
204
205  
206 =item CGI::Session::CookieJar::DBI::cookie
207  
208 Retreives a cookie from the cookie jar using a specified query. If no
209 cookie is found then it returns 'undef'.
210  
211 By default all cookie fields are returned. If your application potentially
212 contains large 'server_data' fields this may not be what you want. In these
213 cases you can specify a list of fields to omit. These fields are passed in
214 via array reference.
215  
216 my $time = time;
217 my %query = ( -user=>'bob',
218 -cookie_name=>'myApp7887',
219 -cookie=>'3476dfgh',
220 -passkey=>'23438',
221 -expiration=>">$time" );
222  
223 my $sessions = $cookie_jar->session( -query => \%query );
224  
225 if ( !defined $cookie ) { croak "There is no such cookie."; }
226  
227 ...or...
228  
229 my $cookie = $cookie_jar->session( -query => \%query,
230 -omit_server_side_data => 1 );
231
232 =cut
233  
234 sub session
235 0 ▶ {
Subroutine session: not called CC=13 SCAR=52.0
Pod undocumented
236 0 my $self = shift;
237 0 ▶ my %args = ( ref $_[0] eq 'HASH' ) ? %{$_[0]} : @_ ;
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] eq 'HASH' ? : 0 0 0
238 0 $self->error(undef);
239 0 ▶ return unless $self->has_valid_connection;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->has_valid_connection 0 0 0
240 0 ▶ return unless defined $args{-query};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $args{"-query"} 0 0 0
241 0 my $query = $self->build_where_query( $args{-query} );
242 0 my $db = $self->db;
243 0 my $user_column = $self->user_column;
244 0 my $cookie_column = $self->cookie_column;
245 0 my $cookie_name_column = $self->cookie_name_column;
246 0 my $passkey_column = $self->passkey_column;
247 0 my $login_expiration_column = $self->login_expiration_column;
248 0 my $server_side_data_column = $self->server_side_data_column;
249 0 my %columns = ( $user_column=>1,
250 $cookie_column=>1,
251 $cookie_name_column=>1,
252 $passkey_column=>1,
253 $login_expiration_column=>1,
254 $server_side_data_column=>1 );
255 0 ▶ if ( defined $args{-omit_server_side_data} )
Branch: 1 branch
BranchTrueFalseTotal
if (defined $args{"-omit_server_side_data"}) 0 0 0
256 {
257 0 delete $columns{$server_side_data_column};
258 }
259 0 my $select_columns = join( ', ', ( keys %columns ) );
260 0 my $select = sprintf( "SELECT $select_columns FROM %s WHERE $query", $self->cookie_table );
261 0 my $statement = $db->prepare( $select );
262 0 my $rows = $statement->execute();
263 0 ▶ if ( $self->db_error( "Encountered error while attempting to retreive a cookie from ".($self->cookie_table).': %s' ) )
Branch: 1 branch
BranchTrueFalseTotal
if ($self->db_error('Encountered error while attempting to retreive a cookie from ' . $self->cookie_table . ': %s')) 0 0 0
264 {
265 0 $statement->finish;
266 0 return;
267 }
268 0 my @results;
269 0 ▶ while( my $row = $statement->fetchrow_hashref )
Branch: 1 branch
BranchTrueFalseTotal
if (my $row = $statement->fetchrow_hashref) 0 0 0
270 {
271 0 my $session = {};
272 0 ▶ $session->{$I_COOKIE} = $row->{$cookie_column} if defined $row->{$cookie_column};
Branch: 1 branch
BranchTrueFalseTotal
if defined $row->{$cookie_column} 0 0 0
273 0 ▶ $session->{$I_COOKIE_NAME} = $row->{$cookie_name_column} if defined $row->{$cookie_name_column};
Branch: 1 branch
BranchTrueFalseTotal
if defined $row->{$cookie_name_column} 0 0 0
274 0 ▶ $session->{$I_USER} = $row->{$user_column} if defined $row->{$user_column};
Branch: 1 branch
BranchTrueFalseTotal
if defined $row->{$user_column} 0 0 0
275 0 ▶ $session->{$I_PASSKEY} = $row->{$passkey_column} if defined $row->{$passkey_column};
Branch: 1 branch
BranchTrueFalseTotal
if defined $row->{$passkey_column} 0 0 0
276 0 ▶ $session->{$I_EXPIRATION} = $self->timestamp_to_time($row->{$login_expiration_column}) if defined $row->{$login_expiration_column};
Branch: 1 branch
BranchTrueFalseTotal
if defined $row->{$login_expiration_column} 0 0 0
277 0 ▶ $session->{$I_SERVER_SIDE_DATA} = $row->{$server_side_data_column} if defined $row->{$server_side_data_column};
Branch: 1 branch
BranchTrueFalseTotal
if defined $row->{$server_side_data_column} 0 0 0
278 0 push @results, $session;
279 }
280 0 $statement->finish;
281 0 return \@results;
282 }
283  
284  
285 =item CGI::Session::CookieJar::DBI::delete
286  
287 Deletes the specified cookies from the cookie jar.
288  
289 my $time = time;
290 my %query = ( -expiration=>'<$time' );
291 $cookie_jar->delete( %query );
292  
293  
294 =cut
295  
296 sub delete
297 0 ▶ {
Subroutine delete: not called CC=2 SCAR=17.9
Pod documented
298 0 my $self = shift;
299 0 ▶ return unless $self->has_valid_connection;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->has_valid_connection 0 0 0
300 0 $self->error(undef);
301 0 my $query = $self->build_where_query( @_ );
302 0 my $db = $self->db;
303 0 $db->do( "DELETE FROM $db->cookie_table WHERE $query" );
304 0 $self->db_error( "Database error while attempting to delete from ".($db->cookie_table).": %s" );
305 }
306  
307  
308  
309 =item CGI::Session::CookieJar::DBI::set_session
310  
311 Creates a new session
312  
313 my $time = time;
314 my %session = ( user=>'bob',
315 cookie_name=>'3476dfgh',
316 passkey=>'23438',
317 expiration=>'$time',
318 server_side_data=>$data );
319  
320 my $cookie_jar->set_session( -session => \$cookie );
321  
322 =cut
323  
324 sub set_session
325 0 ▶ {
Subroutine set_session: not called CC=17 SCAR=57.2
Pod documented
326 0 my $self = shift;
327 0 $self->error(undef);
328 0 ▶ my %args = ( ref $_[0] eq 'HASH' ) ? %{$_[0]} : @_ ;
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] eq 'HASH' ? : 0 0 0
329 0 ▶ return unless $self->has_valid_connection;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->has_valid_connection 0 0 0
330 0 ▶ return unless defined $args{-session};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $args{"-session"} 0 0 0
331 0 my $db = $self->db;
332 0 my %session = %{$args{-session}};
333  
334 0 my $user_column = $self->user_column;
335 0 my $cookie_column = $self->cookie_column;
336 0 my $cookie_name_column = $self->cookie_name_column;
337 0 my $passkey_column = $self->passkey_column;
338 0 my $login_expiration_column = $self->login_expiration_column;
339 0 my $server_side_data_column = $self->server_side_data_column;
340  
341 0 ▶ croak "Set_session requires either a username or cookie to be specified in the session." unless $session{$I_USER} or $session{$I_COOKIE} ;
Branch: 1 branch
BranchTrueFalseTotal
unless $session{$I_USER} or $session{$I_COOKIE} 0 0 0
Condition0%0/3
$session{$I_USER} or $session{$I_COOKIE}
l!l&&r!l&&!r
000
MC/DC0%0/2
$session{$I_USER} or $session{$I_COOKIE}
$session{$I_USER} $session{$I_COOKIE}
Truth table0%0/3
A or B
A= $session{$I_USER}B= $session{$I_COOKIE}
ABresult
1X1
011
000
342  
343 0 my ( @assignment_clause, $key, $key_column );
344  
345 0 my %key;
346 0 ▶ if ( $session{$I_USER} )
Branch: 1 branch
BranchTrueFalseTotal
if ($session{$I_USER}) { } 0 0 0
347 {
348 0 $key{-user} = $session{$I_USER};
349 0 ▶ push( @assignment_clause, "$cookie_column = ".($db->quote($session{$I_COOKIE}))) if defined $session{$I_COOKIE};
Branch: 1 branch
BranchTrueFalseTotal
if defined $session{$I_COOKIE} 0 0 0
350 }
351 else
352 {
353 0 $key{-passkey} = $session{$I_KEY};
354 0 $key{-cookie} = $session{$I_COOKIE};
355 0 ▶ push( @assignment_clause, "$user_column = ".($db->quote($session{$I_USER}))) if defined $session{$I_USER};
Branch: 1 branch
BranchTrueFalseTotal
if defined $session{$I_USER} 0 0 0
356 }
357 0 ▶ if ( $session{$I_COOKIE_NAME} )
Branch: 1 branch
BranchTrueFalseTotal
if ($session{$I_COOKIE_NAME}) 0 0 0
358 {
359 0 $key{-cookie_name} = $session{$I_COOKIE_NAME};
360 0 push( @assignment_clause, "$cookie_name_column = ".($db->quote($session{$I_COOKIE_NAME})) );
361 }
362 0 ▶ push( @assignment_clause, "$passkey_column = ".($db->quote($session{$I_PASSKEY}))) if defined $session{$I_PASSKEY};
Branch: 1 branch
BranchTrueFalseTotal
if defined $session{$I_PASSKEY} 0 0 0
363 0 ▶ push( @assignment_clause, "$login_expiration_column = ".($db->quote( $self->time_to_timestamp($session{$I_EXPIRATION})))) if defined $session{$I_EXPIRATION};
Branch: 1 branch
BranchTrueFalseTotal
if defined $session{$I_EXPIRATION} 0 0 0
364 0 ▶ push( @assignment_clause, "$server_side_data_column = ".($db->quote($session{$I_SERVER_SIDE_DATA}))) if defined $session{$I_SERVER_SIDE_DATA};
Branch: 1 branch
BranchTrueFalseTotal
if defined $session{$I_SERVER_SIDE_DATA} 0 0 0
365 0 my $set_list = join( ', ', @assignment_clause );
366  
367 0 my $where = $self->build_where_query( \%key );
368 0 my $update = sprintf( 'UPDATE %s SET %s WHERE %s',
369 $self->cookie_table,
370 $set_list,
371 $where );
372 0 my $rows = $db->do( $update );
373 0 ▶ return if $self->db_error( 'Update of session into '.($self->cookie_table).' failed: %s: '.$update );
Branch: 1 branch
BranchTrueFalseTotal
if $self->db_error('Update of session into ' . $self->cookie_table . ' failed: %s: ' . $update) 0 0 0
374 0 ▶ if ( $self->db_type eq $DB_MYSQL and $rows != 1 and $rows != 0 )
Branch: 1 branch
BranchTrueFalseTotal
if ($self->db_type eq $DB_MYSQL and $rows != 1 and $rows != 0) 0 0 0
Condition0%0/6
$self->db_type eq $DB_MYSQL and $rows != 1 and $rows != 0
!ll&&!rl&&r
000
$self->db_type eq $DB_MYSQL and $rows != 1
!ll&&!rl&&r
000
MC/DC0%0/3
$self->db_type eq $DB_MYSQL and $rows != 1 and $rows != 0
$self->db_type eq $DB_MYSQL $rows != 1 $rows != 0
Truth table0%0/4
A and B and C
A= $self->db_type eq $DB_MYSQLB= $rows != 1C= $rows != 0
ABCresult
0XX0
10X0
1100
1111
375 {
376 0 $self->error( "SQL statement should have created exactly one line, but $rows seem to have been created." );
377 }
378 }
379  
380  
381 sub time_to_timestamp
382 0 ▶ {
Subroutine time_to_timestamp: not called CC=1 SCAR=6.9
Pod undocumented
383 0 my( $self, $time ) = @_;
384 0 my ( $sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst ) = gmtime($time);
385 0 $mon = $mon+1;
386 0 $year = $year+1900;
387 0 return sprintf( "%.4d%.2d%.2d%.2d%.2d%.2d", $year, $mon, $mday, $hour, $min, $sec );
388 }
389  
390 sub timestamp_to_time
391 0 ▶ {
Subroutine timestamp_to_time: not called CC=1 SCAR=6.9
Pod undocumented
392 0 my ( $self, $timestamp ) = @_;
393 0 $timestamp =~ /(\d\d\d\d)(\d\d)(\d\d)(\d\d)(\d\d)(\d\d)/;
394 0 return timegm( $6, $5, $4, $3, ($2-1), ($1-1900) );
395 }
396  
397 sub db_error
398 0 ▶ {
Subroutine db_error: not called CC=3 SCAR=24.8
Pod undocumented
399 0 my ( $self, $msg ) = @_;
400 0 my $db = $self->db;
401 0 ▶ if ( defined $db and $db->err )
Branch: 1 branch
BranchTrueFalseTotal
if (defined $db and $db->err) 0 0 0
Condition0%0/3
defined $db and $db->err
!ll&&!rl&&r
000
MC/DC0%0/2
defined $db and $db->err
defined $db $db->err
Truth table0%0/3
A and B
A= defined $dbB= $db->err
ABresult
0X0
100
111
402 {
403 0 my $error = ($db->err).', '.($db->errstr);
404 0 $self->error( sprintf( $msg, $error ) );
405 0 return 1;
406 }
407 0 return 0;
408 }
409  
410 =item CGI::Session::CookieJar::DBI::register_user
411  
412 Creates an entry for the specified user within the cookie table.
413  
414 if ( ! $self->contains( -cookie_name=>$cookie_name, -user=>$username ) )
415 {
416 $self->register_user( $cookie_name, $username );
417 }
418  
419 =cut
420  
421 sub register_user
422 0 ▶ {
Subroutine register_user: not called CC=5 SCAR=34.0
Pod documented
423 0 my ( $self, $cookie_name, $username ) = @_;
424  
425 0 my $cookie_table = $self->cookie_table;
426 0 my $user_column = $self->user_column;
427 0 my $cookie_name_column = $self->cookie_name_column;
428  
429 0 $self->error(undef);
430 0 ▶ return unless $self->has_valid_connection();
Branch: 1 branch
BranchTrueFalseTotal
unless $self->has_valid_connection 0 0 0
431 0 my $db = $self->db;
432 0 my $rows = $db->do(
433 sprintf( "INSERT INTO $cookie_table ( $cookie_name_column, $user_column ) VALUES ( %s, %s )",
434 $db->quote($cookie_name),
435 $db->quote($username) ) );
436 0 ▶ return if $self->db_error( "Encountered error while attempting to create $username entry for the cookie name $cookie in $cookie_table: DBI Error: %s" );
Branch: 1 branch
BranchTrueFalseTotal
if $self->db_error("Encountered error while attempting to create $username entry for the cookie name $cookie in ${cookie_table}: DBI Error: %s") 0 0 0
437 0 ▶ if ( $self->db_type eq $DB_MYSQL and $rows != 0 )
Branch: 1 branch
BranchTrueFalseTotal
if ($self->db_type eq $DB_MYSQL and $rows != 0) 0 0 0
Condition0%0/3
$self->db_type eq $DB_MYSQL and $rows != 0
!ll&&!rl&&r
000
MC/DC0%0/2
$self->db_type eq $DB_MYSQL and $rows != 0
$self->db_type eq $DB_MYSQL $rows != 0
Truth table0%0/3
A and B
A= $self->db_type eq $DB_MYSQLB= $rows != 0
ABresult
0X0
100
111
438 {
439 0 $self->error( "SQL statement should have created exactly one line, but $rows seem to have been created." );
440 }
441 }
442  
443  
444  
445 =item CGI::Session::CookieJar::DBI::version
446  
447 Returns the version of CGI::Session that was used to create this
448 data store.
449  
450 my $version => $cookie_jar->version();
451  
452 =cut
453  
454 0 ▶sub version { return "0.0001"; }
Subroutine version: not called CC=1 SCAR=6.9
Pod documented
455  
456  
457  
458 =item CGI::Session::CookieJar::DBI::destroy_cookie_jar
459  
460 Destroys an existing cookie jar. For a database cookie jar this would
461 drop all of the tables. For a file based cookie jar this might
462 delete all the existing files and directories.
463  
464 This should only be necessary once.
465  
466 $cookie_jar->destroy_cookie_jar();
467  
468 =cut
469  
470 sub destroy_cookie_jar
471 0 ▶ {
Subroutine destroy_cookie_jar: not called CC=1 SCAR=6.9
Pod documented
472 0 my ($self) = @_;
473  
474 0 my $cookie_table = $self->cookie_table;
475 0 my $db = $self->db();
476 0 $db->do( "DROP TABLE $cookie_table" );
477 }
478  
479  
480  
481  
482 =item CGI::Session::CookieJar::DBI::error
483  
484 If the previous cookie operation resulted in an error then
485 the value of this error will be found here. If the operation
486 did not result in an error then this will return 'undef'.
487  
488 Calling error() does not alter the value. Each cookie jar object has
489 it's own error state, which is independent of the backend database.
490  
491 my $error = $cookie_jar->error();
492  
493 =cut
494  
495 0 ▶sub error { _param( shift, "-error", @_ ); }
Subroutine error: not called CC=1 SCAR=6.9
Pod documented
496  
497  
498 # These are the legal parameters.
499 #
500 1 my %_params = ( -error => __PACKAGE__.".error",
501 -cookie_table => __PACKAGE__.".cookie_table",
502 -user_column => __PACKAGE__.".user_column",
503 -passkey_column => __PACKAGE__.".passkey_column",
504 -cookie_column => __PACKAGE__.".cookie_column",
505 -cookie_name_column => __PACKAGE__.".cookie_name_column",
506 -login_expiration_column => __PACKAGE__.".login_expiration_column",
507 -server_side_data_column => __PACKAGE__.".server_side_data_column",
508 -use_mysql => __PACKAGE__.".db_type",
509 -database => __PACKAGE__.".database",
510 -db_type => __PACKAGE__.".db_type",
511 -db => __PACKAGE__.".db",
512 -host => __PACKAGE__.".host",
513 -user => __PACKAGE__.".user",
514 -password => __PACKAGE__.".password", );
515  
516  
517 sub _param
518 0 ▶ {
Subroutine _param: not called CC=8 SCAR=42.8
519 0 my $self = shift;
520 0 ▶ if ( scalar @_ == 1 )
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @_ == 1) { } 0 0 0
521 {
522 0 my $field = shift;
523  
524 # Hack for db types.
525 #
526 0 ▶ if ( $field eq '-use_mysql' ) { return $db_type eq $DB_MYSQL; }
Branch: 1 branch
BranchTrueFalseTotal
if ($field eq "-use_mysql") 0 0 0
527 #
528 0 my $slot = $_params{$field};
529 0 ▶ croak "Programmer Error: $field is not a known parameter" unless defined $slot;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $slot 0 0 0
530 0 return $self->{$slot};
531 }
532 else
533 {
534 0 ▶ while( my $field = shift )
Branch: 1 branch
BranchTrueFalseTotal
if (my $field = shift()) 0 0 0
535 {
536 0 my $slot = $_params{$field};
537  
538 # Hack for db types
539 #
540 0 ▶ if ( $field eq '-use_mysql' ) { $self->use_mysql if shift; return; }
Branch: 2 branches
BranchTrueFalseTotal
if ($field eq "-use_mysql") 0 0 0
if shift() 0 0 0
541 #
542 0 ▶ croak "Programmer Error: $field is not a known parameter" unless defined $slot;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $slot 0 0 0
543 0 $self->{$slot} = shift;
544 }
545 }
546 }
547  
548 0 ▶sub set { _param(shift,@_); }
Subroutine set: not called CC=1 SCAR=6.9
Pod undocumented
549  
550 # Login/cookie table description.
551 #
552 =item CGI::Session::CookieJar::DBI::host
553  
554 Accessor method. The name of the host.
555  
556 =cut
557  
558 0 ▶sub host { _param( shift, '-host', @_ ); }
Subroutine host: not called CC=1 SCAR=6.9
Pod undocumented
559  
560  
561 =item CGI::Session::CookieJar::DBI::user
562  
563 Accessor method. The name of the user.
564  
565 =cut
566  
567 0 ▶sub user { _param( shift, '-user', @_ ); }
Subroutine user: not called CC=1 SCAR=6.9
Pod documented
568  
569  
570 =item CGI::Session::CookieJar::DBI::password
571  
572 Accessor method. The name of the password.
573  
574 =cut
575  
576 0 ▶sub password { _param( shift, '-password', @_ ); }
Subroutine password: not called CC=1 SCAR=6.9
Pod documented
577  
578  
579 =item CGI::Session::CookieJar::DBI::database
580  
581 Accessor method. The name of the database.
582  
583 =cut
584  
585 0 ▶sub database { _param( shift, '-database', @_ ); }
Subroutine database: not called CC=1 SCAR=6.9
Pod documented
586  
587  
588 =item Database tables
589  
590 The names of the database tables.
591  
592 =item CGI::Session::CookieJar::DBI::cookie_table
593  
594 Accessor method. The name of the cookie table.
595  
596 =cut
597  
598 0 ▶sub cookie_table { _param( shift, '-cookie_table', @_ ); }
Subroutine cookie_table: not called CC=1 SCAR=6.9
Pod documented
599  
600  
601 =item CGI::Session::CookieJar::DBI::user_column
602  
603 Accessor method. The column containing the usernames.
604  
605 =cut
606  
607 0 ▶sub user_column { _param( shift, '-user_column', @_ ); }
Subroutine user_column: not called CC=1 SCAR=6.9
Pod documented
608  
609  
610 =item CGI::Session::CookieJar::DBI::passkey_column
611  
612 Accessor method. The column containing the passkey.
613  
614 =cut
615  
616 0 ▶sub passkey_column { _param( shift, '-passkey_column', @_ ); }
Subroutine passkey_column: not called CC=1 SCAR=6.9
Pod documented
617  
618  
619 =item CGI::Session::CookieJar::DBI::cookie_name_column
620  
621 Accessor method. The column containing the name of the cookie.
622  
623 =cut
624  
625 0 ▶sub cookie_name_column { _param( shift, '-cookie_name_column', @_ ); }
Subroutine cookie_name_column: not called CC=1 SCAR=6.9
Pod documented
626  
627  
628 =item CGI::Session::CookieJar::DBI::cookie_column
629  
630 Accessor method. The column containing the cookie value.
631  
632 =cut
633  
634 0 ▶sub cookie_column { _param( shift, '-cookie_column', @_ ); }
Subroutine cookie_column: not called CC=1 SCAR=6.9
Pod documented
635  
636  
637 =item CGI::Session::CookieJar::DBI::login_expiration_column
638  
639 Accessor method. The expiration time for the cookie. Currently not
640 used, but it will be used in the future.
641  
642 =cut
643  
644 0 ▶sub login_expiration_column { _param( shift, '-login_expiration_column', @_ ); }
Subroutine login_expiration_column: not called CC=1 SCAR=6.9
Pod documented
645  
646  
647 =item CGI::Session::CookieJar::DBI::server_side_data_column
648  
649 Accessor method. The name of the column containing server side data.
650  
651 =cut
652  
653 0 ▶sub server_side_data_column { _param( shift, '-server_side_data_column', @_ ); }
Subroutine server_side_data_column: not called CC=1 SCAR=6.9
Pod documented
654  
655  
656  
657 =item CGI::Session::CookieJar::DBI::create_cookie_jar
658  
659 Creates the database tables that are described by a CGI::Session.
660  
661 my $session = new CGI::Session;
662 $session->create_cookie_jar;
663 exit;
664  
665 Fill out your CGI::Session just like your going to make
666 a connection. Call this routine, and voila! Your database
667 tables are created.
668  
669 =cut
670  
671 sub create_cookie_jar
672 0 ▶ {
Subroutine create_cookie_jar: not called CC=1 SCAR=6.9
Pod documented
673 0 my ($self) = @_;
674  
675 0 my $cookie_table = $self->cookie_table;
676 0 my $user_column = $self->user_column;
677 0 my $cookie_name_column = $self->cookie_name_column;
678 0 my $cookie_column = $self->cookie_column;
679 0 my $passkey_column = $self->passkey_column;
680 0 my $expiration_column = $self->login_expiration_column;
681 0 my $server_side_data_column = $self->server_side_data_column;
682  
683 0 my $db = $self->db();
684 0 $db->do( <<EOSQL );
685 CREATE TABLE $cookie_table (
686 $user_column varchar(64),
687 $cookie_name_column varchar(64),
688 $cookie_column varchar(32),
689 $passkey_column bigint(20),
690 $expiration_column timestamp(14),
691 $server_side_data_column longblob )
692 EOSQL
693  
694 }
695  
696  
697 # Check to see if a valid connection has been established. If it has not
698 # then this routine returns 0 and sets the error code. If there is a true
699 # value then it returns true.
700 #
701 # This should be called in every user level routine just before it
702 # uses the value of $self->db();
703 #
704 sub has_valid_connection
705 0 ▶ {
Subroutine has_valid_connection: not called CC=2 SCAR=17.9
Pod undocumented
706 0 my ( $self ) = @_;
707 0 my $db = $self->db;
708 0 ▶ if ( !defined $db )
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $db) 0 0 0
709 {
710 0 $self->error( "A database connection must be established before this function is called." );
711 0 return 0;
712 }
713 0 return 1;
714 }
715  
716 # Builds a query for use with in an SQL where clause. The query is passed as
717 # a hash.
718 #
719 # my $query = $jar->build_where_query( -user=>'bob' );
720 #
721 # Currently it the valid queries are:
722 #
723 # -cookie_name=>$cookie_name
724 # -user=>$username
725 # -cookie=>$cookie
726 # -passkey=>$passkey
727 #
728 # The resulting query returns the intersection of all the queried properties.
729 # If no queries are specified then it returns a query which will select _all_
730 # entries in the database. "$username_column IS NOT NULL"
731 #
732 sub build_where_query
733 0 ▶ {
Subroutine build_where_query: not called CC=7 SCAR=40.3
Pod undocumented
734 0 my ( $self ) = shift;
735 0 ▶ my %query = ( ref $_[0] eq 'HASH' ) ? %{$_[0]} : @_ ;
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] eq 'HASH' ? : 0 0 0
736 0 my $db = $self->db;
737 0 my @clauses;
738 0 ▶ if ( defined $query{-user} )
Branch: 1 branch
BranchTrueFalseTotal
if (defined $query{"-user"}) 0 0 0
739 {
740 0 my $quoted_user = $db->quote( $query{-user} );
741 0 push @clauses, ($self->user_column)." = $quoted_user";
742 }
743 0 ▶ if ( defined $query{-cookie} )
Branch: 1 branch
BranchTrueFalseTotal
if (defined $query{"-cookie"}) 0 0 0
744 {
745 0 my $quoted_cookie = $db->quote( $query{-cookie} );
746 0 push @clauses, ($self->cookie_column)." = $quoted_cookie";
747 }
748 0 ▶ if ( defined $query{-cookie_name} )
Branch: 1 branch
BranchTrueFalseTotal
if (defined $query{"-cookie_name"}) 0 0 0
749 {
750 0 my $quoted_cookie = $db->quote( $query{-cookie_name} );
751 0 push @clauses, ($self->cookie_name_column)." = $quoted_cookie";
752 }
753 0 ▶ if ( defined $query{-passkey} )
Branch: 1 branch
BranchTrueFalseTotal
if (defined $query{"-passkey"}) 0 0 0
754 {
755 0 my $quoted_passkey = $db->quote( $query{-passkey} );
756 0 push @clauses, ($self->passkey_column)." = $quoted_passkey";
757 }
758 0 ▶ if ( scalar @clauses )
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @clauses) { } 0 0 0
759 {
760 0 return join( ' AND ', @clauses );
761 }
762 else
763 {
764 0 return ( ($self->user_column)." IS NOT NULL" );
765 }
766 }
767  
768 1 1;
769  
770  
771  
772  
773  
774  
775  
776  
777  
778  
779  
780  
781  
782  
783
« blib/lib/CGI/Session.pm ↑ Summary blib/lib/CGI/Session/Authenticator/NetLDAP.pm »