blib/lib/Apache/Mailtrack.pm

filter: Statementstmt 81.8% 9 / 11 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 4 / 4 Podpod n/a totaltotal 86.6% 13 / 15 CC 1 SCAR 0.7
CC
1
Cov
82%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.1
↑ Summary
1 package Apache::Mailtrack;
2  
3 #
4 # $Id: Mailtrack.pm,v 1.3 2003/02/03 12:14:57 florian Exp $
5 #
6  
7 1 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 0 ▶use Apache::Constants qw/OK REDIRECT/;
Subroutine BEGIN: called CC=1 SCAR=2.6
12 use Apache::Request;
13 use Data::Serializer;
14 use DBI qw/:sql_types/;
15  
16 our $VERSION = '0.03';
17 our %config;
18  
19 my $serializer;
20  
21 sub handler
22 {
23 my $r = Apache::Request->new(shift);
24 my $userdata = $r->param('userdata');
25 my $target = $r->param('target');
26 my $internal;
27 my $data;
28  
29 our %config;
30  
31 #
32 # If we didn't get set up correctly, warn about this and throw a really ugly error.
33 #
34 foreach(qw/db_dsn db_user db_pass db_table/)
35 {
36 error($r, 'Apache::Mailtrack> Please configure me correctly (check my manpage).')
37 unless $config{$_} = $r->dir_config($_);
38 }
39  
40 #
41 # Read out optional configuration settings or set defaults.
42 #
43 $config{'serializer'} = $r->dir_config('serializer') || 'Storable';
44 $config{'secret'} = $r->dir_config('secret') if defined $r->dir_config('secret');
45 $config{'path'} = $r->dir_config('path') || '/images';
46 $config{'defaultfile'} = $r->dir_config('defaultfile') || 'mailtrack_default.jpg';
47 $config{'db_target'} = $r->dir_config('db_target') if defined $r->dir_config('db_target');
48  
49 $serializer ||= Data::Serializer->new(
50 serializer => $config{'serializer'},
51 secret => $config{'secret'} || undef,
52 );
53
54 #
55 # Redirect to the defaultfile if we didn't get the required parameters.
56 #
57 error($r, "Apache::Mailtrack> I didn't get the parameters I want - check your rewrite rule.")
58 unless defined $userdata and defined $target;
59  
60 #
61 # Deserialze userdata.
62 #
63 eval
64 {
65 $data = $serializer->deserialize($userdata);
66 };
67
68 #
69 # If we couldn't deserialize the userdata, redirect to the defaultfile.
70 #
71 error($r, "Apache::Mailtrack> Couldn't deserialize the userdata - maybe you should check your secret?")
72 if $@;
73  
74 #
75 # Let's look, whether we got a URL as target...
76 #
77 if($target =~ m/^url\|(.*)/)
78 {
79 $data->{'target'} = $1 unless exists $data->{'target'};
80 $target = 'http://' . $1;
81 }
82 #
83 # or some file.
84 #
85 else
86 {
87 $target = $config{'path'} . '/' . $target;
88 $internal = 1;
89 }
90
91 #
92 # Log into DB.
93 #
94 eval
95 {
96 my $query;
97 my $dbh;
98 my $sth;
99  
100 #
101 # If we should also log the target, add it to the data hash,
102 #
103 $data->{'target'} = $config{'target'} if exists $config{'target'};
104 $query = sprintf('INSERT INTO %s (%s) VALUES (%s)', $config{'db_table'},
105 join(',', sort keys %$data), '?,' x scalar keys %$data);
106 $query =~ s/,\)/\)/;
107  
108 $dbh = DBI->connect($config{'db_dsn'}, $config{'db_user'}, $config{'db_pass'},
109 { AutoCommit => 0, RaiseError => 1 });
110 $sth = $dbh->prepare($query);
111  
112 $sth->execute(map { $data->{$_} } sort keys %$data);
113 $sth->finish();
114 $dbh->commit();
115 $dbh->disconnect();
116 };
117
118 error($r, $@) if $@;
119  
120 #
121 # If we serve a local resource, generate an internal redirect...
122 #
123 if(defined($internal))
124 {
125 $r->internal_redirect($target);
126 OK;
127 }
128 #
129 # If we should redirect to another URL, do so.
130 #
131 else
132 {
133 $r->headers_out->set(Location => $target);
134 REDIRECT;
135 }
136 }
137  
138  
139 #
140 # Redirect to the defaultfile (used in case of emergency).
141 # Also warns about the possible reason for doing so.
142 #
143 sub error
144 {
145 my($r, $fatal) = @_;
146 our %config;
147  
148 $r->warn($fatal);
149 $r->internal_redirect($config{'path'} . '/' . $config{'defaultfile'});
150 exit OK;
151 }
152  
153  
154 1;
155 __END__
↑ Summary