File Coverage

lib/Plack/App/WebMySQL/Key.pm
Criterion Covered Total %
statement 8 50 16.0
branch 0 20 0.0
condition 0 3 0.0
subroutine 4 9 44.4
pod 0 5 0.0
total 12 87 13.7


line stmt bran cond sub pod time code
1             #the dumb terminal webmysql module
2             #mt 21/09/2003 2.3 updated readkey sub
3             #mt 28/09/2003 2.3 updated readkey sub
4             package Plack::App::WebMySQL::Key;
5             BEGIN {
6 1     1   605 use Exporter();
  1         2  
  1         18  
7 1     1   3 use Plack::App::WebMySQL;
  1         1  
  1         80  
8 1     1   7 @ISA = qw(Exporter);
9 1         458 @EXPORT = qw(expireKeys createKey readKey updateKey deleteKey);
10             }
11             ###############################################################################################################
12             sub expireKeys{ #deletes old keys from the server
13 0 0   0 0   if(opendir(KEYS, "keys")){
14 0           foreach(readdir(KEYS)){
15 0 0 0       if($_ =~ m/^\d+$/ && (time - $_) > 86400){unlink("keys/$_");} #valid key older than a day found
  0            
16             }
17 0           closedir(KEYS);
18             }
19 0           else{$error = "Could not check for expired keys: $!\n";}
20             }
21             ###############################################################################################################
22             sub createKey{ #creates a new server side cookie
23 0     0 0   my $key = time;
24 0 0         if(!-e "keys/$key"){ #key does not exist already
25 0 0         if(open(COOKIE, ">keys/$key")){close(COOKIE);}
  0            
26 0           else{$error = "Could not create session file: $!";}
27 0           return $key;
28             }
29 0           else{$error = "New session already exists";}
30 0           return undef; #must of got an error somewhere in this sub
31             }
32             ###############################################################################################################
33             sub readKey{ #read the contents of a server side cookie back into the form hash
34 0 0   0 0   if($_[0]){ #got a key to try and open
35 0 0         if(open(COOKIE, "
36 0           while(){
37 0           chomp $_;
38 0 0         if(m/^([A-Z0-9]+) = (.+)$/){
39             #print STDERR "$0: cookie line: $1 = $2\n";
40 0           $form{lc($1)} = $2;
41             } #store the valid lines
42             #else{print STDERR "$0: Ignoring invalid session file line: $_\n";} #log warning, not really a problem
43             }
44 0           close(COOKIE);
45 0           return 1; #everything ok
46             }
47 0           else{$error = "Your session has expired, please re-login";}
48             }
49 0           else{$error = "No session givin, please re-login";}
50 0           return 0;
51             }
52             ###############################################################################################################
53             sub updateKey{ #saves last form's data, overwriting the existing key file
54 0     0 0   $_[0] =~ m/^(\d+)$/; #untaint
55 0           my @wanted = ("database", "password", "host", "user", "type", "fields", "criteria", "tables", "creationfnames", "creationftypes", "creationfsizes", "creationfnulls", "db", "insertdata\\d+");
56 0 0         if(open(COOKIE, ">keys/$1")){
57 0           foreach my $name (keys %form){
58 0           my $found = 0;
59 0           foreach(@wanted){ #dont save unwanted elements
60 0 0         if($name =~ m/^$_/){
61 0           $found = 1;
62 0           last;
63             }
64             }
65 0 0         if($found){print COOKIE uc($name) . " = " . $form{$name} . "\n";} #save this hash element
  0            
66             }
67 0           close(COOKIE);
68 0           return 1; #everything ok
69             }
70 0           else{$error = "Cant write session file: $!";}
71 0           return 0;
72             }
73             ##############################################################################################################
74             sub deleteKey{
75 0     0 0   $_[0] =~ m/^(\d+)$/; #untaint
76 0           unlink("keys/$1");
77             }
78             ###############################################################################
79             return 1;
80       1     END {}