blib/lib/Lemonldap/NG/Portal/Lib/SMTP.pm

filter: Statementstmt 23.8% 41 / 172 Branchbran 0.0% 0 / 62 Conditioncond 0.0% 0 / 18 MC/DCmcdc 0.0% 0 / 10 Subroutinesub 50.0% 12 / 24 Podpod 0.0% 0 / 7 totaltotal 18.0% 53 / 293 CC 44 SCAR 42.2
CC
44
Cov
16%
sendEmail
182.0
send_mail
110.0
translate
72.0
CRAP 67.8
« blib/lib/Lemonldap/NG/Portal/Auth/_WebForm.pm ↑ Summary blib/lib/Lemonldap/NG/Portal/Auth/Facebook.pm »
1 ##@file
2 # SMTP common functions
3  
4 ##@class
5 # SMTP common functions
6 package Lemonldap::NG::Portal::Lib::SMTP;
7  
8 132 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 132 ▶use Mouse;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 132 ▶use JSON qw(from_json);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 132 ▶use MIME::Entity;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 132 ▶use Lemonldap::NG::Common::EmailAddress qw(format_email);
Subroutine BEGIN: called CC=1 SCAR=0.0
13 132 ▶use Email::Sender::Simple qw(sendmail);
Subroutine BEGIN: called CC=1 SCAR=0.0
14 132 ▶use Email::Date::Format qw(email_date);
Subroutine BEGIN: called CC=1 SCAR=0.0
15 132 ▶use HTML::FormatText::WithLinks;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 132 ▶use Lemonldap::NG::Common::EmailTransport;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 132 ▶use MIME::Base64;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 132 ▶use Encode;
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 132 our $VERSION = '2.21.0';
21  
22 132 our $transport;
23  
24 # PROPERTIES
25  
26 has random => (
27 is => 'rw',
28 default => sub {
29 171 ▶ return Lemonldap::NG::Common::Crypto::srandom();
Subroutine __ANON__: called CC=1 SCAR=0.0
30 }
31 132 );
32 has charset => (
33 is => 'rw',
34 lazy => 1,
35 0 ▶ default => sub { return $_[0]->{conf}->{mailCharset} || 'utf-8' }
Subroutine __ANON__: not called CC=2 SCAR=17.9
Condition0%0/2
$_[0]{'conf'}{'mailCharset'} || 'utf-8'
l!l
00
MC/DC0%0/1
$_[0]{'conf'}{'mailCharset'} || 'utf-8'
$_[0]{'conf'}{'mailCharset'}
Truth table0%0/2
A
A= $_[0]{'conf'}{'mailCharset'}
Aresult
11
00
36 132 );
37 has transport => (
38 is => 'rw',
39 lazy => 1,
40 default => sub {
41 0 ▶ return $transport if $transport;
Subroutine __ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
if $Lemonldap::NG::Portal::Lib::SMTP::transport 0 0 0
42 0 my $conf = $_[0]->{conf};
43 0 $transport = Lemonldap::NG::Common::EmailTransport->new($conf);
44 0 return $transport;
45 },
46 132 );
47 has htmlParser => (
48 is => 'ro',
49 lazy => 1,
50 default => sub {
51 0 ▶ return HTML::FormatText::WithLinks->new;
Subroutine __ANON__: not called CC=1 SCAR=6.9
52 },
53 132 );
54  
55 sub loadMailTemplate {
56 0 ▶ my ( $self, $req, $name, %prm ) = @_;
Subroutine loadMailTemplate: not called CC=2 SCAR=17.9
Pod undocumented
57  
58 # HTML::Template cache interferes with email translation (#1897)
59 0 ▶ $prm{cache} = 0 unless defined $prm{cache};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $prm{"cache"} 0 0 0
60 0 $prm{params}->{STATIC_PREFIX} = $self->p->staticPrefix;
61 0 $prm{params}->{MAIN_LOGO} = $self->conf->{portalMainLogo};
62  
63 0 return $self->loadTemplate( $req, $name, %prm );
64 }
65  
66 sub translate {
67 0 ▶ my ( $self, $req ) = @_;
Subroutine translate: not called CC=8 SCAR=42.8
Pod undocumented
68  
69 0 my $lang_code = $self->p->getLanguage($req);
70 0 my $json = $self->conf->{templateDir} . "/common/mail/$lang_code.json";
71 0 ▶ $json = $self->conf->{templateDir} . '/common/mail/en.json'
Branch: 1 branch
BranchTrueFalseTotal
unless -f $json 0 0 0
72 unless ( -f $json );
73 0 ▶ open F, '<', $json
Branch: 1 branch
BranchTrueFalseTotal
open F, "<", $json or die 'Installation error: ' . $! . qq[ ($self->{"conf"}{"templateDir"}/$lang_code.json or $self->{"conf"}{"templateDir"}/common/mail/en.json)] 0 0 0
74 or die 'Installation error: '
75 . $!
76 . " ($self->{conf}->{templateDir}/$lang_code.json or $self->{conf}->{templateDir}/common/mail/en.json)";
77 0 $json = join '', <F>;
78 0 close F;
79 0 my $lang = from_json( $json, { allow_nonref => 1 } );
80 0 my $langOver =
81 from_json( $self->p->getTrOver($req), { allow_nonref => 1 } );
82  
83 0 ▶ if ($langOver) {
Branch: 1 branch
BranchTrueFalseTotal
if ($langOver) 0 0 0
84 0 ▶ for my $k ( keys %{ $langOver->{all} || {} } ) {
Branch: 1 branch
BranchTrueFalseTotal
$langOver->{"all"} or {} 0 0 0
85 0 $lang->{$k} = $langOver->{all}->{$k};
86 }
87 0 ▶ for my $k ( keys %{ $langOver->{$lang_code} || {} } ) {
Branch: 1 branch
BranchTrueFalseTotal
$langOver->{$lang_code} or {} 0 0 0
88 0 $lang->{$k} = $langOver->{$lang_code}->{$k};
89 }
90 }
91 return sub {
92 0 ▶ ($_) = @_;
Subroutine __ANON__: not called CC=3 SCAR=24.8
93 0 ▶ $$_ =~ s/\s+trspan="(\w+?)"(.*?)>.*?</"$2>".($lang->{$1}||$1).'<'/gse;
Condition0%0/3
$lang->{$1} || $1
l!l&&r!l&&!r
000
MC/DC0%0/2
$lang->{$1} || $1
$lang->{$1} $1
Truth table0%0/3
A || B
A= $lang->{$1}B= $1
ABresult
1X1
011
000
94 0 ▶ $$_ =~ s/^(\w+)$/$lang->{$1}||$1/es;
Branch: 1 branch
BranchTrueFalseTotal
$lang->{$1} or $1 0 0 0
95 0 };
96 }
97  
98 # Generate a complex password based on a regular expression
99 # @param regexp regular expression
100 sub gen_password {
101 0 ▶ my ( $self, $regexp ) = @_;
Subroutine gen_password: not called CC=1 SCAR=6.9
Pod undocumented
102 my $password = join '',
103 0 sort { int( rand(3) ) - 1 } split //,
104 $self->random->randregex($regexp);
105 0 return $password;
106 }
107  
108 # Send mail (legacy API, using sendEmail is recommended instead)
109 # @param mail recipient address
110 # @param subject mail subject
111 # @param body mail body
112 # @param html optional set content type to HTML
113 # @return boolean result
114 sub send_mail {
115 0 ▶ my ( $self, $mail, $subject, $body, $html ) = @_;
Subroutine send_mail: not called CC=10 SCAR=47.0
Pod undocumented
116  
117 0 ▶ if ( $mail =~ /^\S+\@\S+$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($mail =~ /^\S+\@\S+$/) 0 0 0
118 0 $mail = format_email( $mail, $mail );
119 }
120  
121 0 $self->logger->info("send_mail called to send \"$subject\" to $mail");
122  
123 # Encode the body with the given charset
124 0 $body = encode( $self->charset, decode( 'utf-8', $body ) );
125 0 $subject = encode( $self->charset, decode( 'utf-8', $subject ) );
126  
127 # Debug messages
128 0 $self->logger->debug( "SMTP From " . $self->conf->{mailFrom} );
129 0 $self->logger->debug( "SMTP To " . $mail );
130 0 $self->logger->debug( "SMTP Subject " . $subject );
131 0 $self->logger->debug( "SMTP Body " . $body );
132 0 ▶ $self->logger->debug( "SMTP HTML flag " . ( $html ? "on" : "off" ) );
Branch: 1 branch
BranchTrueFalseTotal
$html ? : 0 0 0
133 $self->logger->debug( "SMTP Reply-To " . $self->conf->{mailReplyTo} )
134 0 ▶ if $self->conf->{mailReplyTo};
Branch: 1 branch
BranchTrueFalseTotal
if $self->conf->{"mailReplyTo"} 0 0 0
135  
136 # Encode the subject
137 0 $subject = encode_base64( $subject, '' );
138 0 $subject =~ s/\s//gs;
139 0 $subject = '=?' . $self->charset . "?B?$subject?=";
140  
141 # Detect included images (cid)
142 0 my %cid = ( $body =~ m/"cid:([^:]+):([^"]+)"/g );
143  
144 # Replace cid in body
145 0 $body =~ s/"cid:([^:]+):([^"]+)"/"cid:$1"/g;
146  
147 0 eval {
148  
149 # Create message
150 0 my $message;
151  
152 # HTML case
153 0 ▶ if ($html) {
Branch: 1 branch
BranchTrueFalseTotal
if ($html) { } 0 0 0
154 $message = MIME::Entity->build(
155 From => $self->conf->{mailFrom},
156 To => $mail,
157 (
158 $self->conf->{mailReplyTo}
159 ? ( "Reply-To" => $self->conf->{mailReplyTo} )
160 0 ▶ : ()
Branch: 1 branch
BranchTrueFalseTotal
$self->conf->{'mailReplyTo'} ? : 0 0 0
161 ),
162 Subject => $subject,
163 Type => 'multipart/mixed',
164 Date => email_date,
165 );
166  
167 0 my $alternative =
168 $message->attach( Type => 'multipart/alternative' );
169  
170 # Text message
171 0 $alternative->attach(
172 Type => 'text/plain; charset=' . $self->charset,
173 Data => $self->htmlParser->parse($body),
174 );
175  
176 0 my $related = $alternative->attach( Type => 'multipart/related' );
177  
178 # Attach HTML message
179 0 $related->attach(
180 Type => 'text/html; charset=' . $self->charset,
181 Data => $body,
182 );
183  
184 # Attach included images
185 0 foreach ( keys %cid ) {
186 $related->attach(
187 Type => "image/" . ( $cid{$_} =~ m/\.(\w+)/ )[0],
188 Id => $_,
189 0 Path => $self->_search_attachment( $cid{$_} ),
190 );
191 }
192 }
193  
194 # Plain text case
195 else {
196 $message = MIME::Entity->build(
197 From => $self->conf->{mailFrom},
198 To => $mail,
199 "Reply-To" => $self->conf->{mailReplyTo},
200 0 Subject => $subject,
201 Type => 'TEXT',
202 Data => $body,
203 Type => 'text/plain',
204 Charset => $self->charset,
205 Date => email_date,
206 );
207 }
208  
209 # Send the mail
210 0 ▶ sendmail( $message->stringify,
Branch: 1 branch
BranchTrueFalseTotal
$self->transport ? : 0 0 0
211 ( $self->transport ? { transport => $self->transport } : () ) );
212 };
213 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
214 0 ▶ $self->logger->error( "Send message failed: "
Branch: 1 branch
BranchTrueFalseTotal
$@->isa('Throwable::Error') ? : 0 0 0
215 . ( $@->isa('Throwable::Error') ? $@->message : $@ ) );
216 0 return 0;
217 }
218  
219 0 return 1;
220 }
221  
222 sub _search_attachment {
223 0 ▶ my ( $self, $filename ) = @_;
Subroutine _search_attachment: not called CC=2 SCAR=17.9
224  
225 my $from_skin =
226 0 $self->p->getSkinTplDir( $self->conf->{portalSkin} ) . "/" . $filename;
227  
228 0 ▶ if ( -e $from_skin ) {
Branch: 1 branch
BranchTrueFalseTotal
if (-e $from_skin) { } 0 0 0
229 0 $self->logger->debug("found attachment at $from_skin");
230 0 return $from_skin;
231 }
232 else {
233 my $from_bootstrap =
234 0 $self->conf->{templateDir} . "/bootstrap/" . $filename;
235 0 $self->logger->debug("returning attachment at $from_bootstrap");
236 0 return $from_bootstrap;
237 }
238 }
239  
240 ## @method string getMailSession(string user)
241 # Check if a mail session exists
242 # @param user the value of the user key in session
243 # @return the first session id found or nothing if no session
244 sub getMailSession {
245 0 ▶ my ( $self, $user ) = @_;
Subroutine getMailSession: not called CC=5 SCAR=34.0
Pod undocumented
246  
247 0 ▶ my $moduleOptions = $self->conf->{globalStorageOptions} || {};
Condition0%0/2
$self->conf->{'globalStorageOptions'} || {}
l!l
00
MC/DC0%0/1
$self->conf->{'globalStorageOptions'} || {}
$self->conf->{'globalStorageOptions'}
Truth table0%0/2
A
A= $self->conf->{'globalStorageOptions'}
Aresult
11
00
248 0 $moduleOptions->{backend} = $self->conf->{globalStorage};
249 0 my $module = "Lemonldap::NG::Common::Apache::Session";
250  
251 # Search on mail sessions
252 0 my $sessions = $module->searchOn( $moduleOptions, "user", $user );
253  
254 # Browse found sessions to check if it's a mail session
255 0 foreach my $id ( keys %$sessions ) {
256 0 my $mailSession =
257 $self->p->getApacheSession( $id, kind => "TOKEN", hashStore => 0, );
258 0 ▶ next unless ($mailSession);
Branch: 1 branch
BranchTrueFalseTotal
unless $mailSession 0 0 0
259 0 ▶ return $mailSession if ( $mailSession->data->{_type} =~ /^mail$/ );
Branch: 1 branch
BranchTrueFalseTotal
if $mailSession->data->{"_type"} =~ /^mail$/ 0 0 0
260 }
261  
262 # No mail session found, return empty string
263 0 return "";
264 }
265  
266 ## @method string getRegisterSession(string mail)
267 # Check if a register session exists
268 # @param mail the value of the mail key in session
269 # @return the first session id found or nothing if no session
270 sub getRegisterSession {
271 0 ▶ my ( $self, $mail ) = @_;
Subroutine getRegisterSession: not called CC=6 SCAR=37.4
Pod undocumented
272  
273 0 ▶ my $moduleOptions = $self->conf->{globalStorageOptions} || {};
Condition0%0/2
$self->conf->{'globalStorageOptions'} || {}
l!l
00
MC/DC0%0/1
$self->conf->{'globalStorageOptions'} || {}
$self->conf->{'globalStorageOptions'}
Truth table0%0/2
A
A= $self->conf->{'globalStorageOptions'}
Aresult
11
00
274 0 $moduleOptions->{backend} = $self->conf->{globalStorage};
275 0 my $module = "Lemonldap::NG::Common::Apache::Session";
276  
277 # Search on register sessions
278 0 my $sessions = $module->searchOn( $moduleOptions, "mail", $mail );
279  
280 # Browse found sessions to check if it's a register session
281 0 foreach my $id ( keys %$sessions ) {
282 0 my $registerSession =
283 $self->p->getApacheSession( $id, kind => "TOKEN", hashStore => 0, );
284 0 ▶ next unless ($registerSession);
Branch: 1 branch
BranchTrueFalseTotal
unless $registerSession 0 0 0
285 return $id
286 if ( $registerSession->data->{_type}
287 0 ▶ and $registerSession->data->{_type} =~ /^register$/ );
Branch: 1 branch
BranchTrueFalseTotal
if $registerSession->data->{"_type"} and $registerSession->data->{"_type"} =~ /^register$/ 0 0 0
Condition0%0/3
$registerSession->data->{"_type"} and $registerSession->data->{"_type"} =~ /^register$/
!ll&&!rl&&r
000
MC/DC0%0/2
$registerSession->data->{"_type"} and $registerSession->data->{"_type"} =~ /^register$/
$registerSession->data->{"_type"} $registerSession->data->{"_type"} =~ /^register$/
Truth table0%0/3
A and B
A= $registerSession->data->{"_type"}B= $registerSession->data->{"_type"} =~ /^register$/
ABresult
0X0
100
111
288 }
289  
290 # No register session found, return empty string
291 0 return "";
292 }
293  
294 ## @method string sendEmail(string mail)
295 # High-level method for sending a templated email
296 # Takes a hash of parameters
297 # @param dest the destination email address
298 # @param subject the plaintext subject, will not be translated
299 # @param subject_trmsg the translation key that will be used as subject
300 # @param body the plaintext body, may contain $variable placeholders
301 # @param body_template the name of the template file to render as body
302 # @param params a hashref of parameters
303 # @return success status as a boolean
304 sub sendEmail {
305 0 ▶ my ( $self, $req, %params ) = @_;
Subroutine sendEmail: not called CC=13 SCAR=52.0
Pod undocumented
306  
307 0 my $subject_trmsg = $params{subject_trmsg};
308 0 my $subject = $params{subject};
309 0 my $body = $params{body};
310 0 my $body_template = $params{body_template};
311 0 ▶ my %variables = %{ $params{params} || {} };
Branch: 1 branch
BranchTrueFalseTotal
$params{"params"} or {} 0 0 0
312 0 my $dest = $params{dest};
313  
314 # Build mail content
315 0 my $tr = $self->translate($req);
316 0 ▶ unless ($subject) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($subject) 0 0 0
317 0 $subject = $subject_trmsg;
318 0 $tr->( \$subject );
319 }
320 0 my $html;
321 0 ▶ if ($body) {
Branch: 1 branch
BranchTrueFalseTotal
if ($body) { } 0 0 0
322  
323 # Replace supplied variables in body, and also handle session variables
324 0 ▶ while ( my ( $k, $v ) = each %variables ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %variables) 0 0 0
325 0 $body =~ s/\$\Q$k\E\b/$v/g;
326 }
327 0 ▶ $body =~ s/\$(\w+)/$req->{sessionInfo}->{$1} || ''/ge;
Branch: 1 branch
BranchTrueFalseTotal
$req->{"sessionInfo"}{$1} or "" 0 0 0
328 }
329 else {
330  
331 # Use HTML template
332 0 $body = $self->loadMailTemplate(
333 $req,
334 $body_template,
335 filter => $tr,
336 params => \%variables
337 );
338 0 $html = 1;
339 }
340 0 ▶ if ( $dest && $subject && $body ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dest and $subject and $body) { } 0 0 0
Condition0%0/6
$dest and $subject and $body
!ll&&!rl&&r
000
$dest and $subject
!ll&&!rl&&r
000
MC/DC0%0/3
$dest and $subject and $body
$dest $subject $body
Truth table0%0/4
A and B and C
A= $destB= $subjectC= $body
ABCresult
0XX0
10X0
1100
1111
341 0 ▶ if ( $self->send_mail( $dest, $subject, $body, $html ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->send_mail($dest, $subject, $body, $html)) { } 0 0 0
342 0 $self->auditLog(
343 $req,
344 message => "Email ($body_template) sent "
345 . "to $dest with subject '$subject'",
346 code => "EMAIL_SENT",
347 body_template => $body_template,
348 subject => $subject,
349 subject_trmsg => $subject_trmsg,
350 dest => $dest,
351 );
352 0 return 1;
353 }
354 else {
355 0 return 0;
356 }
357 }
358 else {
359 0 my $message = "Could not send email:";
360 0 ▶ $message .= " missing destination," unless $dest;
Branch: 1 branch
BranchTrueFalseTotal
unless $dest 0 0 0
361 0 ▶ $message .= " missing subject," unless $subject;
Branch: 1 branch
BranchTrueFalseTotal
unless $subject 0 0 0
362 0 ▶ $message .= " missing body," unless $body;
Branch: 1 branch
BranchTrueFalseTotal
unless $body 0 0 0
363 0 chop($message);
364 0 $message .= ".";
365 0 $self->logger->warn($message);
366 0 return 0;
367 }
368 }
369  
370 132 1;
« blib/lib/Lemonldap/NG/Portal/Auth/_WebForm.pm ↑ Summary blib/lib/Lemonldap/NG/Portal/Auth/Facebook.pm »