blib/lib/Apache/OneTimeURL.pm untested

filter: Statementstmt 0.0% 0 / 71 Branchbran 0.0% 0 / 12 Conditioncond 0.0% 0 / 16 MC/DCmcdc 0.0% 0 / 9 Subroutinesub 0.0% 0 / 13 Podpod 0.0% 0 / 5 totaltotal 0.0% 0 / 126 CC 7 SCAR 19.5
CC
7
Cov
0%
handler
20.0
deliver
6.0
intruder
6.0
CRAP 7.1
1 package Apache::OneTimeURL;
2 $Apache::OneTimeURL::VERSION = '1.35';
3 use 5.006;
4 use strict;
5 use warnings;
6 use MLDBM qw(DB_File);
7 use Digest::MD5 qw(md5_hex);
8 use Mail::Send;
9 use Apache;
10 use Apache::Constants;
11  
12 sub handler {
13 my ($class,$r) = @_;
14 # This allows us to use inheritance in situations where we're called
15 # as "Apache::OneTimeURL->handler" but not otherwise.
16 if (!$r and $class) { $r = $class; $class = __PACKAGE__ }
17  
18 $r->path_info() =~ /([a-f0-9]{32})/ or return DECLINED;
19 my $key = $1;
20 my %o;
21 my $db = $r->dir_config("OneTimeDb")
22 or die "Database not specified in OneTimeDb!";
23 tie %o, "MLDBM", $db
24 or die "Couldn't open database $db: $!";
25 return DECLINED if !exists $o{$key};
26 my $stuff = $o{$key};
27 if ($stuff->{count}++) {
28 $o{$key} = $stuff;
29 untie %o;
30 return $class->intruder($r, $stuff);
31 }
32 $o{$key} = $stuff;
33 untie %o;
34 return $class->deliver($r);
35 }
36  
37 sub deliver {
38 my ($class, $r) = @_;
39 die "No OneTimeDoc specified!" unless my $file = $r->dir_config("OneTimeDoc");
40 my $subr = $r->lookup_file($file);
41 return $subr->run;
42 }
43  
44 sub authorize {
45 my ($class, $db, $comments) = @_;
46 my $key = md5_hex(time().{}.rand().$$);
47 my %o;
48 tie %o, "MLDBM", $db or die "Couldn't open database: $!";
49 $o{$key} = {
50 comments => $comments,
51 count => 0,
52 created => time
53 };
54 untie %o;
55 return $key;
56 }
57  
58 sub intruder {
59 my ($class, $r, $hash) = @_;
60 my $sendcount = $r->dir_config("OneTimeMailCount") || 5;
61 if ($hash->{count} < $sendcount) {
62 $class->send_mail($r, $hash);
63 }
64 $r->send_http_header("text/html");
65 print "<HTML><HEAD><title>Unauthorized access</title></HEAD>
66 <BODY>
67 You are not authorized to access this resource. This attempt has been
68 recorded.
69 </BODY>
70 <HTML>";
71 return OK; # Can't return forbidden, since that calls other handlers.
72 }
73  
74 sub send_mail {
75 my ($class, $r, $hash) = @_;
76 my $email = $r->dir_config("OneTimeEmail") || $r->server_admin();
77 my $referrer = $r->header_in( 'Referer' );
78 my $msg = new Mail::Send To => $email,
79 Subject => 'One-time URL reused';
80 my $fh = $msg->open;
81 print $fh <<EOF;
82  
83 Key issued at @{[ scalar localtime $hash->{created} ]}
84 with comments @{[ $hash->{comments} ]}
85  
86 Reused at @{[ scalar localtime ]}
87 by @{[ $r->get_remote_host ]} ( @{[ $r->get_remote_logname ]} )
88  
89 EOF
90  
91 if ($referrer) { print $fh "Accessed via $referrer\n\n" }
92 $fh->close;
93 }
94  
95 1;
96  
97 __END__