blib/lib/CGI/Application/MailPage.pm

filter: Statementstmt 82.1% 318 / 387 Branchbran 63.2% 124 / 196 Conditioncond 62.9% 34 / 54 MC/DCmcdc 43.7% 14 / 32 Subroutinesub 93.3% 28 / 30 Podpod 14.2% 1 / 7 totaltotal 73.5% 519 / 706 CC 127 SCAR 41.1
CC
127
Cov
75%
send_mail
128.8
show_thanks
20.0
__ANON__
20.0
CRAP 61.1
↑ Summary
1 package CGI::Application::MailPage;
2 1 ▶use base 'CGI::Application';
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use CGI::Application;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use HTML::Template;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use HTML::TreeBuilder;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use HTTP::Date;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use MIME::Entity;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use Mail::Header;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use Mail::Internet;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use Email::Valid;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use Net::SMTP;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 1 ▶use Text::Format;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 1 ▶use URI;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 1 ▶use Data::FormValidator;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 1 ▶use Fcntl qw(:flock);
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 1 $CGI::Application::MailPage::VERSION = '1.7';
20  
21 25 ▶sub setup {
Subroutine setup: called CC=5 SCAR=16.8
Pod documented
22 25 my $self = shift;
23 25 $self->start_mode('show_form');
24 25 $self->mode_param('rm');
25 25 $self->run_modes(
26 show_form => \&show_form,
27 send_mail => \&send_mail,
28 );
29  
30 # make sure we have required params
31 25 ▶ die "You must set either 'document_root' or 'remote_fetch' in PARAMS!"
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->param("document_root") or $self->param("remote_fetch") 0 25 25
Condition33%1/3
defined $self->param("document_root") or $self->param("remote_fetch")
l!l&&r!l&&!r
2500
MC/DC0%0/2
defined $self->param("document_root") or $self->param("remote_fetch")
defined $self->param("document_root") $self->param("remote_fetch")
Truth table33%1/3
A or B
A= defined $self->param("document_root")B= $self->param("remote_fetch")
ABresult
1X1
011
000
32 unless defined $self->param('document_root') || $self->param('remote_fetch');
33  
34 25 ▶ die "You must set 'your.smtp.server' in PARAMS!"
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->param("smtp_server") 0 25 25
35 unless defined $self->param('smtp_server');
36  
37 # default custom validation_profile is an empty hash
38 25 ▶ $self->param(validation_profile => {} )
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->param("validation_profile") 24 1 25
39 unless defined($self->param('validation_profile'));
40 }
41  
42 5 ▶sub show_form {
Subroutine show_form: called CC=12 SCAR=28.5
Pod undocumented
43 5 my ($self, $err_msgs) = @_;
44 5 my $query = $self->query;
45  
46 5 my $page = $query->param('page');
47 5 ▶ if (not defined $page) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $page) 0 5 5
48 0 ▶ unless($self->param('use_page_param')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->param('use_page_param')) { } 0 0 0
49 0 $page = $query->referer();
50 0 ▶ return $self->error(
Branch: 1 branch
BranchTrueFalseTotal
unless defined $page 0 0 0
51 "Sorry, I can't tell what page you want to send. " .
52 "You need to be using either Netscape 4 or Internet Explorer 4 (or newer) " .
53 "to use this feature. Please upgrade your browser and try again!"
54 )
55 unless defined $page;
56 } else {
57 0 ▶ return $self->error("no value for page param!")
Branch: 1 branch
BranchTrueFalseTotal
unless defined $page 0 0 0
58 unless defined $page;
59 }
60 }
61  
62 5 my $template;
63 5 ▶ if ($self->param('form_template')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->param('form_template')) { } 0 5 5
64 0 $template = $self->load_tmpl($self->param('form_template'),
65 die_on_bad_params => 0,
66 cache => 1,
67 associate => $query
68 );
69
70 } else {
71 5 my @path = $self->tmpl_path;
72 0 ▶ @path = @{ $self->tmpl_path} if(ref($path[0]) eq 'ARRAY');
Branch: 1 branch
BranchTrueFalseTotal
if ref $path[0] eq "ARRAY" 0 5 5
73 5 $template = $self->load_tmpl('CGI/Application/MailPage/templates/form.tmpl',
74 die_on_bad_params => 0,
75 path => [@path, @INC],
76 cache => 1,
77 associate => $query,
78 );
79 }
80  
81 5 my %formats = (
82 both_attachment => 'Both Text and Full HTML as Attachments',
83 html => 'Full HTML',
84 html_attachment => 'Full HTML as an Attachment',
85 text => 'Plain Text',
86 text_attachment => 'Plain Text as an Attachment',
87 url => 'Just A Link',
88 );
89 #create the default dropdown menu
90 5 $template->param(FORMAT_SELECTOR =>
91 $query->popup_menu(-name => 'format',
92 '-values' => [ sort(keys %formats) ],
93 -labels => \%formats,
94 -default => 'both_attachment',
95 )
96 );
97 #create a loop that the user can use as they wish
98 5 my @format_loop = ();
99 5 foreach my $key (sort(keys(%formats))) {
100 30 push(@format_loop, { value => $key, label => $formats{$key}});
101 }
102 5 $template->param(FORMAT_OPTIONS => \@format_loop);
103
104 # set the default 'subject' as the email subject
105 5 ▶ $query->param('subject' => $self->param('email_subject'))
Branch: 1 branch
BranchTrueFalseTotal
unless $query->param("subject") 1 4 5
106 unless($query->param('subject'));
107 # if we have any alerts or error messages
108 5 ▶ if( $err_msgs && ref $err_msgs eq 'HASH' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($err_msgs and ref $err_msgs eq "HASH") 4 1 5
Condition66%2/3
$err_msgs and ref $err_msgs eq "HASH"
!ll&&!rl&&r
104
MC/DC50%1/2
$err_msgs and ref $err_msgs eq "HASH"
$err_msgs ref $err_msgs eq "HASH"
Truth table66%2/3
A and B
A= $err_msgsB= ref $err_msgs eq "HASH"
ABresult
0X0
100
111
109 4 $template->param(%$err_msgs);
110 }
111 5 ▶ $template->param(%{$self->param('extra_tmpl_params')})
Branch: 1 branch
BranchTrueFalseTotal
if $self->param("extra_tmpl_params") 1 4 5
112 if($self->param('extra_tmpl_params'));
113 5 return $template->output();
114 }
115  
116 24 ▶sub send_mail {
Subroutine send_mail: called CC=68 SCAR=48.6
Pod undocumented
117 24 my $self = shift;
118 24 my $query = $self->query;
119  
120 # the default validation profile
121 my %validation_profile = (
122 required => [qw(
123 name from_email to_emails format page subject
124 )],
125 optional => [qw(note)],
126 constraints => {
127 name => qr/^[\w '-\(\),\.]{1,50}$/,
128 from_email => 'email',
129 23 ▶ to_emails => sub {
Subroutine __ANON__: called CC=5 SCAR=16.1
130 23 my @emails = split(/\s*,\s*|\s+/, shift);
131 # make sure there aren't too many
132 # if we have 'max_emails_per_request'
133 23 ▶ if(
Branch: 1 branch
BranchTrueFalseTotal
if ($self->param("max_emails_per_request") and scalar @emails > $self->param("max_emails_per_request")) 1 22 23
Condition100%3/3
$self->param("max_emails_per_request") and scalar @emails > $self->param("max_emails_per_request")
!ll&&!rl&&r
2111
MC/DC100%2/2
$self->param("max_emails_per_request") and scalar @emails > $self->param("max_emails_per_request")
$self->param("max_emails_per_request") scalar @emails > $self->param("max_emails_per_request")
Truth table100%3/3
A and B
A= $self->param("max_emails_per_request")B= scalar @emails > $self->param("max_emails_per_request")
ABresult
0X0
100
111
134 $self->param('max_emails_per_request')
135 && scalar @emails > $self->param('max_emails_per_request')
136 ) {
137 1 return;
138 }
139 # check for valid email addresses
140 22 foreach my $email (@emails) {
141 42 ▶ if(! Email::Valid->address($email) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ("Email::Valid"->address($email)) 2 40 42
142 2 return;
143 }
144 }
145 20 return \@emails;
146 },
147 subject => qr/^[\w '-\(\),\.\?\!]{1,50}$/,
148 note => qr/^[^\0]{1,250}$/,
149 22 ▶ format => sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
150 22 my $val = shift;
151 22 my @valid_formats = qw(
152 both_attachment html html_attachment
153 text text_attachment url
154 );
155 22 ▶ if( grep { $val eq $_ } @valid_formats ) {
Branch: 1 branch
BranchTrueFalseTotal
if (grep {$val eq $_;} @valid_formats) 21 1 22
156 21 return $val;
157 }
158 1 return;
159 },
160 24 page => qr/^[^\n\0]{1,256}$/, # TODO - what should this be?
161 },
162 untaint_all_constraints => 1,
163 );
164  
165 # merge this default with the custom profile
166 # first merge the 'constraints'
167 24 ▶ if( $self->param('validation_profile')->{constraints} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->param("validation_profile")->{"constraints"}) 1 23 24
168 $validation_profile{constraints} = {
169 1 %{$validation_profile{constraints}},
170 1 %{ $self->param('validation_profile')->{constraints} }
171 };
172 }
173 24 delete $self->param('validation_profile')->{constraints};
174 # now merge the rest
175 %validation_profile = (
176 %validation_profile,
177 24 %{ $self->param('validation_profile') },
178 );
179  
180 # now validate the data
181 24 my $results = Data::FormValidator->check($query, \%validation_profile);
182  
183 # create any error messages if necessary.
184 24 ▶ if( $results->has_invalid || $results->has_missing ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($results->has_invalid or $results->has_missing) 4 20 24
Condition66%2/3
$results->has_invalid or $results->has_missing
l!l&&r!l&&!r
4020
MC/DC50%1/2
$results->has_invalid or $results->has_missing
$results->has_invalid $results->has_missing
Truth table66%2/3
A or B
A= $results->has_invalidB= $results->has_missing
ABresult
1X1
011
000
185 4 my %err_msgs = ();
186 # look at each invalid
187 4 foreach my $invalid ($results->invalid) {
188 13 $err_msgs{"error_$invalid"} = 1;
189 13 $err_msgs{"invalid_$invalid"} = 1;
190 13 $err_msgs{"any_errors"} = 1;
191 13 $err_msgs{"any_invalid"} = 1;
192 }
193 # look at each missing
194 4 foreach my $missing ($results->missing) {
195 5 $err_msgs{"error_$missing"} = 1;
196 5 $err_msgs{"missing_$missing"} = 1;
197 5 $err_msgs{"any_errors"} = 1;
198 5 $err_msgs{"any_missing"} = 1;
199 }
200  
201 # for backwards compatability, add an 'alert' parameter
202 # for older templates that hold's the first error message we encounter
203 4 ▶ if( $err_msgs{error_name} ) {
Branch: 6 branches
BranchTrueFalseTotal
if ($err_msgs{'error_name'}) { } 3 1 4
elsif ($err_msgs{'invalid_from_email'}) { } 0 1 1
elsif ($err_msgs{'missing_from_email'}) { } 0 1 1
elsif ($err_msgs{'invalid_to_emails'}) { } 1 0 1
elsif ($err_msgs{'missing_to_emails'}) { } 0 0 0
elsif ($err_msgs{'error_subject'}) { } 0 0 0
204 3 $err_msgs{alert} = "Please fill in your name in the form below.";
205 } elsif( $err_msgs{invalid_from_email} ) {
206 0 $err_msgs{alert} = "Your email address is invalid - it should look like name\@host.com.";
207 } elsif( $err_msgs{missing_from_email} ) {
208 0 $err_msgs{alert} = "Please fill in your email address in the form below.";
209 } elsif( $err_msgs{invalid_to_emails} ) {
210 1 $err_msgs{alert} = "One of your friend's email addresses is invalid - it should look like name\@host.com.";
211 } elsif( $err_msgs{missing_to_emails} ) {
212 0 $err_msgs{alert} = "Please fill in your friends' email addresses in the form below.";
213 } elsif( $err_msgs{error_subject} ) {
214 0 $err_msgs{alert} = "Please enter a Subject for the email in the form below.";
215 }
216  
217 # show these errors
218 4 return $self->show_form(\%err_msgs);
219 }
220  
221 # get the valid data
222 20 my $valid_data = $results->valid();
223 my ($to_emails, $from_email, $name, $subject, $page, $note, $format) =
224 20 map { $valid_data->{$_} } qw(to_emails from_email name subject page note format);
225
226 #make sure this page is either relative or within the acceptable_domains
227 20 ▶ if(
Branch: 1 branch
BranchTrueFalseTotal
if ($self->param("acceptable_domains") and ref $self->param("acceptable_domains") eq "ARRAY" and $page =~ m[^https?://([^/:]+)]) 2 18 20
Condition66%4/6
$self->param("acceptable_domains") and ref $self->param("acceptable_domains") eq "ARRAY" and $page =~ m[^https?://([^/:]+)]
!ll&&!rl&&r
1802
$self->param("acceptable_domains") and ref $self->param("acceptable_domains") eq "ARRAY"
!ll&&!rl&&r
1802
MC/DC33%1/3
$self->param("acceptable_domains") and ref $self->param("acceptable_domains") eq "ARRAY" and $page =~ m[^https?://([^/:]+)]
$self->param("acceptable_domains") ref $self->param("acceptable_domains") eq "ARRAY" $page =~ m[^https?://([^/:]+)]
Truth table50%2/4
A and B and C
A= $self->param("acceptable_domains")B= ref $self->param("acceptable_domains") eq "ARRAY"C= $page =~ m[^https?://([^/:]+)]
ABCresult
0XX0
10X0
1100
1111
228 $self->param('acceptable_domains') #if we have any domains
229 && (ref($self->param('acceptable_domains')) eq 'ARRAY') #if it's an array ref
230 && $page =~ m#^https?://([^/:]+)# #if the path's not relative
231 )
232 {
233 2 my $domain = $1;
234 return $self->error("The domain for that desired page is not acceptable!")
235 2 ▶ unless( grep { lc($domain) eq lc($_) } @{$self->param('acceptable_domains')});
Branch: 1 branch
BranchTrueFalseTotal
unless grep {lc $domain eq lc $_;} @{$self->param("acceptable_domains");} 1 1 2
236 }
237
238 # make sure we haven't exceeded our hourly limit
239 19 ▶ if( $self->param('max_emails_per_hour') ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->param("max_emails_per_hour")) 4 15 19
240 4 my $file = $self->param('max_emails_per_hour_file');
241 4 ▶ unless( $file ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($file) 0 4 4
242 0 die "max_emails_per_hour_file ($file) must exist and be writable"
243 . " in order to use max_emails_per_hour!";
244 }
245 4 my ($error, $count, $last_time);
246 4 my $current_time = time();
247 # if already exists then open it and read the data
248 4 ▶ if( -e $file ) {
Branch: 1 branch
BranchTrueFalseTotal
if (-e $file) { } 4 0 4
249 4 ▶ open(my $IN, $file) or die "Could not open $file for reading! $!";
Branch: 1 branch
BranchTrueFalseTotal
open my $IN, $file or die "Could not open $file for reading! $!" 4 0 4
250 4 ($last_time, $count) = split(qr/:/, <$IN>);
251 4 ▶ close($IN) or die "Could not close $file! $!";
Branch: 1 branch
BranchTrueFalseTotal
close $IN or die "Could not close $file! $!" 4 0 4
252 4 ▶ $last_time ||= 0;
Condition100%2/2
$last_time ||= 0
l!l
31
MC/DC100%1/1
$last_time ||= 0
$last_time
Truth table100%2/2
A
A= $last_time
Aresult
11
00
253 4 ▶ $count ||= 0;
Condition100%2/2
$count ||= 0
l!l
31
MC/DC100%1/1
$count ||= 0
$count
Truth table100%2/2
A
A= $count
Aresult
11
00
254 # find out if we've done this within the hour
255 # if the difference is less than 1 hour, increase the count
256 # and make sure it's less than the hourly total
257 4 ▶ if( $current_time - $last_time < ( 60 * 60 ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($current_time - $last_time < 3600) { } 2 2 4
258 2 $count += scalar(@$to_emails);
259 2 ▶ if( $count > $self->param('max_emails_per_hour') ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($count > $self->param("max_emails_per_hour")) 2 0 2
260 2 $error = "Hourly limit on emails exceeded!";
261 }
262 # keep the last recorded time.
263 2 $current_time = $last_time;
264 } else {
265 2 $count = scalar(@$to_emails);
266 }
267 # else the file doesn't exist
268 } else {
269 0 $count = scalar(@$to_emails);
270 }
271  
272 # now save the time and count
273 4 ▶ open(my $OUT, ">", $file) or die "Could not open $file for writing! $!";
Branch: 1 branch
BranchTrueFalseTotal
open my $OUT, ">", $file or die "Could not open $file for writing! $!" 4 0 4
274 4 ▶ flock($OUT, LOCK_EX) or die "Could not obtain lock on $file! $!";
Branch: 1 branch
BranchTrueFalseTotal
flock $OUT, 2 or die "Could not obtain lock on $file! $!" 4 0 4
275 4 print $OUT "$current_time:$count";
276 4 ▶ close($OUT) or die "Could not close $file! $!";
Branch: 1 branch
BranchTrueFalseTotal
close $OUT or die "Could not close $file! $!" 4 0 4
277  
278 # if we have an error then return it
279 4 ▶ return $self->error($error) if( $error );
Branch: 1 branch
BranchTrueFalseTotal
if $error 2 2 4
280 }
281  
282 # find the HTML file to open (if it's not a remote fetch)
283 17 my ($filename, $base_url, $base);
284 17 ▶ unless( $self->param('remote_fetch') && ($page =~ m!^https?://!) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->param('remote_fetch') and $page =~ m[^https?://]) { } 7 10 17
Condition66%2/3
$self->param("remote_fetch") and $page =~ m[^https?://]
!ll&&!rl&&r
1007
MC/DC50%1/2
$self->param("remote_fetch") and $page =~ m[^https?://]
$self->param("remote_fetch") $page =~ m[^https?://]
Truth table66%2/3
A and B
A= $self->param("remote_fetch")B= $page =~ m[^https?://]
ABresult
0X0
100
111
285 10 $filename = $self->_find_html_file($page);
286 10 ▶ return $self->error("Unable to find file $filename for page $page (might be empty or unreadable): $!")
Branch: 1 branch
BranchTrueFalseTotal
unless -e $filename and -r _ and -s _ 3 7 10
Condition66%4/6
-e $filename and -r _ and -s _
!ll&&!rl&&r
307
-e $filename and -r _
!ll&&!rl&&r
307
MC/DC33%1/3
-e $filename and -r _ and -s _
-e $filename -r _ -s _
Truth table50%2/4
A and B and C
A= -e $filenameB= -r _C= -s _
ABCresult
0XX0
10X0
1100
1111
287 unless -e $filename and -r _ and -s _;
288 7 my ($vol, $dir, $file) = File::Spec->splitpath($filename);
289  
290 7 $base_url = $page;
291 7 $base_url =~ s/\Q$file\E//;
292
293 # if file is empty, assume index.html
294 7 ▶ if (not defined $file or not length $file) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $file and length $file) 0 7 7
Condition33%1/3
defined $file and length $file
!ll&&!rl&&r
007
MC/DC0%0/2
defined $file and length $file
defined $file length $file
Truth table33%1/3
A and B
A= defined $fileB= length $file
ABresult
0X0
100
111
295 0 $file = 'index.html';
296 0 $filename .= '/index.html';
297 }
298
299 7 my $ext;
300 7 ($base, $ext) = $file =~ /(.*)\.([^\.]+)$/;
301 } else {
302 7 $base_url = URI->new($page);
303 7 $base_url = $base_url->scheme . '://' . $base_url->authority . '/' . $base_url->path;
304 }
305  
306 # open the email template
307 14 my $template;
308 14 ▶ if ($self->param('email_template')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->param('email_template')) { } 0 14 14
309 0 $template = $self->load_tmpl($self->param('email_template'),
310 die_on_bad_params => 0,
311 cache => 1,
312 );
313 } else {
314 14 my @path = $self->tmpl_path;
315 0 ▶ @path = @{ $self->tmpl_path} if(ref($path[0]) eq 'ARRAY');
Branch: 1 branch
BranchTrueFalseTotal
if ref $path[0] eq "ARRAY" 0 14 14
316 14 $template = $self->load_tmpl('CGI/Application/MailPage/templates/email.tmpl',
317 die_on_bad_params => 0,
318 path => [@path, @INC],
319 cache => 1,
320 );
321 }
322 14 $template->param(%$valid_data);
323 0 ▶ $template->param(%{$self->param('extra_tmpl_params')})
Branch: 1 branch
BranchTrueFalseTotal
if $self->param("extra_tmpl_params") 0 14 14
324 if($self->param('extra_tmpl_params'));
325  
326 # get the IP address of the original sender
327 14 ▶ my $sender_ip = $ENV{HTTP_X_FORWARDED_FOR} || $ENV{REMOTE_ADDR} || '';
Condition40%2/5
$ENV{'HTTP_X_FORWARDED_FOR'} || $ENV{'REMOTE_ADDR'} || ''
l!l
140
$ENV{"HTTP_X_FORWARDED_FOR"} or $ENV{"REMOTE_ADDR"}
l!l&&r!l&&!r
0140
MC/DC0%0/2
$ENV{'HTTP_X_FORWARDED_FOR'} || $ENV{'REMOTE_ADDR'} || ''
$ENV{"HTTP_X_FORWARDED_FOR"} $ENV{"REMOTE_ADDR"}
Truth table33%1/3
A or B
A= $ENV{"HTTP_X_FORWARDED_FOR"}B= $ENV{"REMOTE_ADDR"}
ABresult
1X1
011
000
328 # $msg will end up with either a Mail::Internet or MIME::Entity object.
329 14 my $msg;
330  
331 # are we doing attachments?
332 14 ▶ if (index($format, '_attachment') != -1) {
Branch: 1 branch
BranchTrueFalseTotal
if (index($format, '_attachment') != -1) { } 7 7 14
333 # open up a MIME::Entity for our msg
334 7 $msg = MIME::Entity->build(
335 'Type' => "multipart/mixed",
336 'From' => "$name <$from_email>",
337 'Reply-To' => "$name <$from_email>",
338 'To' => $to_emails,
339 'Subject' => $subject,
340 'Date' => HTTP::Date::time2str(time()),
341 'X-Originating-Ip' => $sender_ip,
342 );
343  
344 7 $msg->attach(Data => $template->output);
345  
346 # attach the straight HTML if requested
347 7 ▶ if ($format =~ /^(both|html)/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($format =~ /^(both|html)/) 5 2 7
348 5 my $buffer = "";
349 5 ▶ if ($self->param('read_file_callback')) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->param('read_file_callback')) { } 0 5 5
elsif ($self->param('remote_fetch') and $page =~ m[^https?://]) { } 3 2 5
350 0 my $callback = $self->param('read_file_callback');
351 0 $buffer = $callback->($filename);
352 ▶ } elsif( $self->param('remote_fetch') && ($page =~ /^https?:\/\//) ) {
Condition66%2/3
$self->param("remote_fetch") and $page =~ m[^https?://]
!ll&&!rl&&r
203
MC/DC50%1/2
$self->param("remote_fetch") and $page =~ m[^https?://]
$self->param("remote_fetch") $page =~ m[^https?://]
Truth table66%2/3
A and B
A= $self->param("remote_fetch")B= $page =~ m[^https?://]
ABresult
0X0
100
111
353 #fetch this page with LWP
354 3 require LWP::UserAgent;
355 3 require HTTP::Request;
356 3 my $agent = LWP::UserAgent->new();
357 3 my $response = $agent->request(HTTP::Request->new(GET => $page));
358 3 ▶ if( $response->is_success ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($response->is_success) { } 2 1 3
359 2 $buffer = $response->content();
360 } else {
361 1 return $self->error("Unable to retrieve remote page $page");
362 }
363 } else {
364 2 ▶ open(HTML, $filename) or return $self->error("Can't open $filename : $!");
Branch: 1 branch
BranchTrueFalseTotal
open HTML, $filename or return $self->error("Can't open $filename : $!") 2 0 2
365 2 ▶ while(read(HTML, $buffer, 10240, length($buffer))) {}
Branch: 1 branch
BranchTrueFalseTotal
if (read HTML, $buffer, 10240, length $buffer) 2 2 4
366 2 close(HTML);
367 }
368
369 # add <BASE> tag in <HEAD>
370 4 ▶ $buffer =~ s/(<\s*[Hh][Ee][Aa][Dd].*?>)/$1\n<base href=$base_url>\n/
Branch: 1 branch
BranchTrueFalseTotal
if $base_url 4 0 4
371 if( $base_url );
372
373 4 ▶ my $attached_filename = $base ? "$base.html" : $page;
Branch: 1 branch
BranchTrueFalseTotal
$base ? : 2 2 4
374 4 $msg->attach(
375 Data => $buffer,
376 Type => 'text/html',
377 Filename => $attached_filename,
378 );
379 }
380  
381 # attach text translation
382 6 ▶ if ($format =~ /^(both|text)/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($format =~ /^(both|text)/) 4 2 6
383 4 ▶ my $new_filename = $base ? "$base.txt" : "$page.txt";
Branch: 1 branch
BranchTrueFalseTotal
$base ? : 2 2 4
384 4 $msg->attach(
385 Data => $self->_html2text($filename, $page),
386 Type => 'text/plain',
387 Filename => $new_filename,
388 );
389 }
390  
391 } else {
392 # non attachment mail
393 7 my $header = Mail::Header->new();
394 7 $header->add(From => "$name <$from_email>");
395 7 $header->add('Reply-To' => "$name <$from_email>");
396 7 $header->add(To => join(', ', @$to_emails));
397 7 $header->add(Subject => $subject);
398 7 $header->add(Date => HTTP::Date::time2str(time()));
399 7 ▶ $header->add('X-Originating-Ip' => $sender_ip)
Branch: 1 branch
BranchTrueFalseTotal
if $sender_ip 7 0 7
400 if( $sender_ip );
401  
402 7 my @lines;
403 7 push(@lines, $template->output());
404  
405 7 ▶ if ($format =~ /^(both|text)/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($format =~ /^(both|text)/) 2 5 7
406 2 push(@lines, "\n---\n\n");
407 2 push(@lines, $self->_html2text($filename, $page));
408 }
409
410 7 ▶ if ($format =~ /^(both|html)/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($format =~ /^(both|html)/) 2 5 7
411 2 push(@lines, "\n---\n\n");
412 2 ▶ if ($self->param('read_file_callback')) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->param('read_file_callback')) { } 0 2 2
elsif ($self->param('remote_fetch') and $page =~ m[^https?://]) { } 1 1 2
413 0 my $callback = $self->param('read_file_callback');
414 0 my $buffer = $callback->($filename);
415 0 push(@lines, split("\n", $buffer));
416 ▶ } elsif( $self->param('remote_fetch') && ($page =~ /^https?:\/\//) ) {
Condition66%2/3
$self->param("remote_fetch") and $page =~ m[^https?://]
!ll&&!rl&&r
101
MC/DC50%1/2
$self->param("remote_fetch") and $page =~ m[^https?://]
$self->param("remote_fetch") $page =~ m[^https?://]
Truth table66%2/3
A and B
A= $self->param("remote_fetch")B= $page =~ m[^https?://]
ABresult
0X0
100
111
417 #fetch this page with LWP
418 1 require LWP::UserAgent;
419 1 require HTTP::Request;
420 1 my $agent = LWP::UserAgent->new();
421 1 my $response = $agent->request(HTTP::Request->new(GET => $page));
422 1 ▶ if( $response->is_success ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($response->is_success) { } 1 0 1
423 1 my $buffer = $response->content();
424 1 @lines = split(/\r?\n/, $buffer);
425 } else {
426 0 return $self->error("Unable to retrieve remote page $page");
427 }
428 } else {
429 1 ▶ open(HTML, $filename) or return $self->error("Can't open $filename : $!");
Branch: 1 branch
BranchTrueFalseTotal
open HTML, $filename or return $self->error("Can't open $filename : $!") 1 0 1
430 1 push(@lines, <HTML>);
431 1 close(HTML);
432 }
433 }
434  
435 7 ▶ if ($format =~ /url/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($format =~ /url/) 3 4 7
436 3 push(@lines, "\n$page");
437 }
438  
439 7 $msg = Mail::Internet->new([], Header => $header, Body => \@lines);
440 7 ▶ return $self->error("Unable to create Mail::Internet object!")
Branch: 1 branch
BranchTrueFalseTotal
unless defined $msg 0 7 7
441 unless defined $msg;
442 }
443
444 # send the message using SMTP - other methods can be added later
445 13 ▶ unless($self->param('dump_mail')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->param('dump_mail')) { } 13 0 13
446 0 my $smtp = Net::SMTP->new($self->param('smtp_server'));
447 0 ▶ return $self->error("Unable to connect to SMTP server ".$self->param('smtp_server')." : $!")
Branch: 1 branch
BranchTrueFalseTotal
unless defined $smtp and UNIVERSAL::isa($smtp, "Net::SMTP") 0 0 0
Condition0%0/3
defined $smtp and UNIVERSAL::isa($smtp, "Net::SMTP")
!ll&&!rl&&r
000
MC/DC0%0/2
defined $smtp and UNIVERSAL::isa($smtp, "Net::SMTP")
defined $smtp UNIVERSAL::isa($smtp, "Net::SMTP")
Truth table0%0/3
A and B
A= defined $smtpB= UNIVERSAL::isa($smtp, "Net::SMTP")
ABresult
0X0
100
111
448 unless defined $smtp and UNIVERSAL::isa($smtp,'Net::SMTP');
449 0 ▶ $smtp->debug(1) if $self->param('smtp_debug');
Branch: 1 branch
BranchTrueFalseTotal
if $self->param("smtp_debug") 0 0 0
450
451 0 $smtp->mail("$name <$from_email>");
452 0 foreach (@$to_emails) {
453 0 $smtp->to($_);
454 }
455 0 $smtp->data();
456 0 $smtp->datasend($msg->as_string());
457 0 $smtp->dataend();
458 0 $smtp->quit();
459  
460 } else {
461 # debuging hook for test.pl
462 13 my $mailref = $self->param('dump_mail');
463 13 $$mailref = $msg->as_string();
464 13 return $self->error("Mail Dumped");
465 }
466  
467 # all done
468 0 return $self->show_thanks;
469 }
470  
471 0 ▶sub show_thanks {
Subroutine show_thanks: not called CC=4 SCAR=30.0
Pod undocumented
472 0 my $self = shift;
473 0 my $query = $self->query;
474 0 my $page = $query->param('page');
475  
476 0 my $template;
477 0 ▶ if ($self->param('thanks_template')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->param('thanks_template')) { } 0 0 0
478 0 $template = $self->load_tmpl($self->param('thanks_template'),
479 die_on_bad_params => 0,
480 cache => 1,
481 );
482 } else {
483 0 my @path = $self->tmpl_path;
484 0 ▶ @path = @{ $self->tmpl_path} if(ref($path[0]) eq 'ARRAY');
Branch: 1 branch
BranchTrueFalseTotal
if ref $path[0] eq "ARRAY" 0 0 0
485 0 $template = $self->load_tmpl('CGI/Application/MailPage/templates/thanks.tmpl',
486 die_on_bad_params => 0,
487 path => [@path, @INC],
488 cache => 1,
489 );
490 }
491  
492 0 $template->param(PAGE => $page);
493 0 ▶ $template->param(%{$self->param('extra_tmpl_params')})
Branch: 1 branch
BranchTrueFalseTotal
if $self->param("extra_tmpl_params") 0 0 0
494 if($self->param('extra_tmpl_params'));
495 0 return $template->output();
496 }
497  
498  
499 20 ▶sub error {
Subroutine error: called CC=4 SCAR=15.2
Pod undocumented
500 20 my ($self, $msg) = @_;
501 20 my $template;
502  
503 20 ▶ if($self->param('error_template')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->param('error_template')) { } 0 20 20
504 0 $template = $self->load_tmpl( $self->param('error_template'),
505 die_on_bad_params => 0,
506 cache => 1,
507 );
508 } else {
509 20 my @path = $self->tmpl_path;
510 0 ▶ @path = @{ $self->tmpl_path} if(ref($path[0]) eq 'ARRAY');
Branch: 1 branch
BranchTrueFalseTotal
if ref $path[0] eq "ARRAY" 0 20 20
511 20 $template = $self->load_tmpl( 'CGI/Application/MailPage/templates/error.tmpl',
512 die_on_bad_params => 0,
513 path => [@path, @INC],
514 cache => 1,
515 );
516 }
517  
518 20 $template->param(error => $msg);
519 0 ▶ $template->param(%{$self->param('extra_tmpl_params')})
Branch: 1 branch
BranchTrueFalseTotal
if $self->param("extra_tmpl_params") 0 20 20
520 if($self->param('extra_tmpl_params'));
521 20 return $template->output();
522 }
523  
524 10 ▶sub _find_html_file {
Subroutine _find_html_file: called CC=3 SCAR=11.0
525 10 my $self = shift;
526 10 my $url = shift;
527 10 my $path;
528  
529 # if it doesn't start with http, its relative to web root
530 10 ▶ if($url =~ m!^https?://([-\w\.:]+)/(.*)!) {
Branch: 1 branch
BranchTrueFalseTotal
if ($url =~ m[^https?://([-\w\.:]+)/(.*)]) { } 7 3 10
531 7 my $host = $1;
532 7 $path = $2;
533 # if the path starts with a ~user thing, remove it
534 7 $path =~ s!~[^/]+/!!;
535 } else {
536 3 ▶ $path = ($url =~ /^\//) ? $url : "/$url"; #make sure it has a preceding path
Branch: 1 branch
BranchTrueFalseTotal
$url =~ m[^/] ? : 0 3 3
537 }
538  
539 # now make sure we don't allow any '../' sections to try and hack the server
540 10 $path =~ s/\.\.\///g;
541 # append it to document_root and return it
542 10 return File::Spec->join($self->param('document_root'), $path);
543 }
544
545 # takes an html file and returns text. This code was taken and
546 # modified from html2text.pl by Ave Wrigley. I don't really
547 # understand most of it, but it seems to work well.
548  
549 #--------------------------------------------------------------------------
550 #
551 # prefixes to convert tags into - some are converted back to Text::Format
552 # formatting later
553 #
554 #--------------------------------------------------------------------------
555  
556 1 my %prefix = (
557 'li' => '* ',
558 'dt' => '+ ',
559 'dd' => '- ',
560 );
561  
562 1 my %underline = (
563 'h1' => '=',
564 'h2' => '-',
565 'h3' => '-',
566 'h4' => '-',
567 'h5' => '-',
568 'h6' => '-',
569 );
570  
571 1 my @heading_number = ( 0, 0, 0, 0, 0, 0 );
572  
573 6 ▶sub _html2text {
Subroutine _html2text: called CC=6 SCAR=18.9
574 6 my $self = shift;
575 6 my $filename = shift;
576 6 my $page = shift;
577  
578 6 my $html_tree = new HTML::TreeBuilder;
579 6 my $text_formatter = new Text::Format;
580 6 $text_formatter->firstIndent( 0 );
581  
582 6 my $result = "";
583  
584 #----------------------------------------------------------------------
585 #
586 # get_text - get all the text under a node
587 #
588 #----------------------------------------------------------------------
589  
590 sub get_text
591 3 ▶ {
Subroutine get_text: called CC=5 SCAR=23.2
Pod undocumented
592 3 my $this = shift;
593 3 my $text = '';
594
595 # iterate though my children ...
596 3 ▶ return unless defined $this->content;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $this->content 0 3 3
597 3 for my $child ( @{ $this->content } )
598 {
599 # if the child is also non-text ...
600 3 ▶ if ( ref( $child ) )
Branch: 1 branch
BranchTrueFalseTotal
if (ref $child) { } 0 3 3
601 {
602 # traverse it ...
603 $child->traverse(
604 # traveral callback
605 0 ▶ sub {
Subroutine __ANON__: not called CC=4 SCAR=30.0
606 0 my( $node, $startflag, $depth ) = @_;
607 # only visit once
608 0 ▶ return 0 unless $startflag;
Branch: 1 branch
BranchTrueFalseTotal
unless $startflag 0 0 0
609 # if it is non-text ...
610 0 ▶ if ( ref( $node ) )
Branch: 1 branch
BranchTrueFalseTotal
if (ref $node) { } 0 0 0
611 {
612 # recurse get_text
613 0 $text .= get_text( $node );
614 }
615 # if it is text
616 else
617 {
618 # add it to $text
619 0 ▶ $text .= $node if $node =~ /\S/;
Branch: 1 branch
BranchTrueFalseTotal
if $node =~ /\S/ 0 0 0
620 }
621 0 return 0;
622 },
623 0 0
624 );
625 }
626 # if it is text
627 else
628 {
629 # add it to $text
630 3 ▶ $text .= $child if $child =~ /\S/;
Branch: 1 branch
BranchTrueFalseTotal
if $child =~ /\S/ 3 0 3
631 }
632 }
633 3 return $text;
634 }
635
636 #--------------------------------------------------------------------------
637 #
638 # get_paragraphs - routine for generating an array of paras from a given node
639 #
640 #--------------------------------------------------------------------------
641
642 sub get_paragraphs
643 3 ▶ {
Subroutine get_paragraphs: called CC=4 SCAR=14.3
Pod undocumented
644 3 my $this = shift;
645
646 # array to save paragraphs in
647 3 my @paras = ();
648 # avoid -w warning for .= operation on undefined
649 3 $paras[ 0 ] = '';
650
651 # iterate though my children ...
652 3 for my $child ( @{ $this->content } )
653 {
654 # if the child is also non-text ...
655 12 ▶ if ( ref( $child ) )
Branch: 1 branch
BranchTrueFalseTotal
if (ref $child) { } 6 6 12
656 {
657 # traverse it ...
658 $child->traverse(
659 # traveral callback
660 18 ▶ sub {
Subroutine __ANON__: called CC=5 SCAR=17.6
661 18 my( $node, $startflag, $depth ) = @_;
662 # only visit once
663 18 ▶ return 0 unless $startflag;
Branch: 1 branch
BranchTrueFalseTotal
unless $startflag 6 12 18
664 # if it is non-text ...
665 12 ▶ if ( ref( $node ) )
Branch: 1 branch
BranchTrueFalseTotal
if (ref $node) { } 6 6 12
666 {
667 # if it is a list element ...
668 6 ▶ if ( $node->tag =~ /^(?:li|dd|dt)$/ )
Branch: 1 branch
BranchTrueFalseTotal
if ($node->tag =~ /^(?:li|dd|dt)$/) { } 0 6 6
669 {
670 # recurse get_paragraphs
671 0 my @new_paras = get_paragraphs( $node );
672 # pre-pend appropriate prefix for list
673 $new_paras[ 0 ] =
674 0 $prefix{ $node->tag } . $new_paras[ 0 ]
675 ;
676 # and update the @paras array
677 0 @paras = ( @paras, @new_paras );
678 # and traverse no more
679 0 return 0;
680 }
681 else
682 {
683 # any other element, just traverse
684 6 return 1;
685 }
686 }
687 else
688 {
689 # add text to the current paragraph ...
690 6 ▶ $paras[ $#paras ] =
Branch: 1 branch
BranchTrueFalseTotal
if $node =~ /\S/ 6 0 6
691 join( ' ', $paras[ $#paras ], $node )
692 if $node =~ /\S/
693 ;
694 # and recurse no more
695 6 return 0;
696 }
697 },
698 6 0
699 );
700 }
701 else
702 {
703 # add test to current paragraph ...
704 6 ▶ $paras[ $#paras ] = join( ' ', $paras[ $#paras ], $child )
Branch: 1 branch
BranchTrueFalseTotal
if $child =~ /\S/ 6 0 6
705 if $child =~ /\S/
706 ;
707 }
708 }
709 3 return @paras;
710 }
711
712 #--------------------------------------------------------------------------
713 #
714 # Main
715 #
716 #--------------------------------------------------------------------------
717
718 # parse the HTML file
719 6 ▶ if ($self->param('read_file_callback')) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->param('read_file_callback')) { } 0 6 6
elsif ($self->param('remote_fetch') and $page =~ m[^https?://]) { } 3 3 6
720 0 my $callback = $self->param('read_file_callback');
721 0 $html_tree->parse( $callback->($filename) );
722 ▶ } elsif( $self->param('remote_fetch') && ($page =~ /^https?:\/\//) ) {
Condition66%2/3
$self->param("remote_fetch") and $page =~ m[^https?://]
!ll&&!rl&&r
303
MC/DC50%1/2
$self->param("remote_fetch") and $page =~ m[^https?://]
$self->param("remote_fetch") $page =~ m[^https?://]
Truth table66%2/3
A and B
A= $self->param("remote_fetch")B= $page =~ m[^https?://]
ABresult
0X0
100
111
723 #fetch this page with LWP
724 3 require LWP::UserAgent;
725 3 require HTTP::Request;
726 3 my $agent = LWP::UserAgent->new();
727 3 my $response = $agent->request(HTTP::Request->new(GET => $page));
728 3 ▶ if( $response->is_success ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($response->is_success) { } 3 0 3
729 3 my $buffer = $response->content();
730 3 $html_tree->parse($buffer);
731 } else {
732 0 return $self->error("Unable to retrieve remote page $page");
733 }
734 } else {
735 3 ▶ open(HTML, $filename) or return $self->error("Can't open $filename : $!");
Branch: 1 branch
BranchTrueFalseTotal
open HTML, $filename or return $self->error("Can't open $filename : $!") 3 0 3
736 3 $html_tree->parse( join( '', <HTML> ) );
737 3 close(HTML);
738 }
739  
740 # main tree traversal routine
741
742 $html_tree->traverse(
743 414 ▶ sub {
Subroutine __ANON__: called CC=13 SCAR=26.1
744 414 my( $node, $startflag, $depth ) = @_;
745 # ignore what's in the <HEAD>
746 414 ▶ return 0 if ref( $node ) and $node->tag eq 'head';
Branch: 1 branch
BranchTrueFalseTotal
if ref $node and $node->tag eq "head" 6 408 414
Condition100%3/3
ref $node and $node->tag eq "head"
!ll&&!rl&&r
483606
MC/DC100%2/2
ref $node and $node->tag eq "head"
ref $node $node->tag eq "head"
Truth table100%3/3
A and B
A= ref $nodeB= $node->tag eq "head"
ABresult
0X0
100
111
747 # only visit nodes once
748 408 ▶ return 0 unless $startflag;
Branch: 1 branch
BranchTrueFalseTotal
unless $startflag 153 255 408
749 # if this node is non-text ...
750 255 ▶ if ( ref $node )
Branch: 1 branch
BranchTrueFalseTotal
if (ref $node) { } 207 48 255
751 {
752 # if this is a para ...
753 207 ▶ if ( $node->tag eq 'p' )
Branch: 2 branches
BranchTrueFalseTotal
if ($node->tag eq 'p') { } 3 204 207
elsif ($node->tag =~ /^h(\d)/) { } 3 201 204
754 {
755 # iterate sub-paragraphs (including lists) ...
756 3 for ( get_paragraphs( $node ) )
757 {
758 # if it is a <LI> ...
759 3 ▶ if ( /^\* / )
Branch: 3 branches
BranchTrueFalseTotal
if (/^\* /) { } 0 3 3
elsif (s/^\+ //) { } 0 3 3
elsif (s/^- //) { } 0 3 3
760 {
761 # indent first line by 4, rest by 6
762 0 $text_formatter->firstIndent( 4 );
763 0 $text_formatter->bodyIndent( 6 );
764 }
765 # if it is a <DT> ...
766 elsif ( s/^\+ // )
767 {
768 # set left margin to 4
769 0 $text_formatter->leftMargin( 4 );
770 }
771 # if it is a <DD> ...
772 elsif ( s/^- // )
773 {
774 # set left margin to 8
775 0 $text_formatter->leftMargin( 8 );
776 }
777 # print formatted paragraphs ...
778 3 $result .= $text_formatter->paragraphs( $_ );
779 # and reset formatter defaults
780 3 $text_formatter->leftMargin( 0 );
781 3 $text_formatter->firstIndent( 0 );
782 3 $text_formatter->bodyIndent( 0 );
783 }
784 3 $result .= "\n";
785 3 return 0;
786 }
787 # if this is a heading ...
788 elsif ( $node->tag =~ /^h(\d)/ )
789 {
790 # get the heading level ...
791 3 my $level = $1;
792 # increment the number for this level ...
793 3 $heading_number[ $level ]++;
794 # reset lower level heading numbers ...
795 3 for ( $level+1 .. $#heading_number )
796 {
797 12 $heading_number[ $_ ] = 0;
798 }
799 # create heading number string
800 3 my $heading_number = join(
801 '.',
802 @heading_number[ 1 .. $level ]
803 );
804 # generate heading from number string and heading text ...
805 # my $text = "$heading_number " . get_text( $node );
806 3 my $text = get_text( $node );
807 # underline it with the appropriate underline character ...
808 3 $text =~ s{
809 (.*)
810 }
811 {
812 6 "$1\n" . $underline{ $node->tag } x length( $1 )
813 }gex
814 ;
815 3 $result .= $text;
816 3 return 0;
817 } else {
818 201 return 1;
819 }
820 }
821 # if it is text ...
822 else
823 {
824 48 ▶ return 0 unless $node =~ /\S/;
Branch: 1 branch
BranchTrueFalseTotal
unless $node =~ /\S/ 0 48 48
825 48 $result .= $text_formatter->format( $node );
826 48 return 0;
827 }
828 },
829 6 0
830 );
831  
832 # filter out comments
833 6 $result =~ s/<!--.*?-->//gs;
834  
835 6 return $result;
836 }
837
838  
839 1 1;
840 __END__
↑ Summary