lib/Rapi/Blog/Util.pm

filter: Statementstmt 25.2% 22 / 87 Branchbran 0.0% 0 / 54 Conditioncond 0.0% 0 / 46 MC/DCmcdc 0.0% 0 / 28 Subroutinesub 35.0% 7 / 20 Podpod 0.0% 0 / 11 totaltotal 11.7% 29 / 246 CC 44 SCAR 39.2
CC
44
Cov
12%
recaptcha_verify
110.0
recaptcha_active
90.0
ts_to_dt
72.0
CRAP 50.5
« lib/Rapi/Blog/DB/ResultSet/Post.pm ↑ Summary lib/Rapi/Blog/Util/ppRender.pm »
1 package Rapi::Blog::Util;
2  
3 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 2 ▶use RapidApp::Util ':all';
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 2 ▶use DateTime;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use HTTP::Request::Common;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use LWP::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 ▶use Rapi::Blog::Util::ppRender;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 0 ▶sub _dt_base_opts {(
Subroutine _dt_base_opts: not called CC=1 SCAR=6.9
14 0 time_zone => 'local'
15 )}
16  
17 0 ▶sub now_ts { &dt_to_ts( &now_dt ) }
Subroutine now_ts: not called CC=1 SCAR=6.9
Pod undocumented
18 0 ▶sub now_dt { DateTime->now( &_dt_base_opts ) }
Subroutine now_dt: not called CC=1 SCAR=6.9
Pod undocumented
19  
20 0 ▶sub dt_to_ts {
Subroutine dt_to_ts: not called CC=3 SCAR=24.8
Pod undocumented
21 0 ▶ shift if ($_[0] && $_[0] eq __PACKAGE__);
Branch: 1 branch
BranchTrueFalseTotal
if $_[0] and $_[0] eq __PACKAGE__ 0 0 0
Condition0%0/3
$_[0] and $_[0] eq __PACKAGE__
!ll&&!rl&&r
000
MC/DC0%0/2
$_[0] and $_[0] eq __PACKAGE__
$_[0] $_[0] eq __PACKAGE__
Truth table0%0/3
A and B
A= $_[0]B= $_[0] eq __PACKAGE__
ABresult
0X0
100
111
22 0 my $dt = shift;
23 0 join(' ',$dt->ymd('-'),$dt->hms(':'));
24 }
25  
26 # This is overkill and probably silly; I wrote it to be able to rule out possible time-zone
27 # inflate/deflate conversion issues. As a sanity check, I can always compare apples to
28 # apples with the DateTime/db-date-string conversion funcs in this package
29 0 ▶sub ts_to_dt {
Subroutine ts_to_dt: not called CC=8 SCAR=42.8
Pod undocumented
30 0 ▶ shift if ($_[0] && $_[0] eq __PACKAGE__);
Branch: 1 branch
BranchTrueFalseTotal
if $_[0] and $_[0] eq __PACKAGE__ 0 0 0
Condition0%0/3
$_[0] and $_[0] eq __PACKAGE__
!ll&&!rl&&r
000
MC/DC0%0/2
$_[0] and $_[0] eq __PACKAGE__
$_[0] $_[0] eq __PACKAGE__
Truth table0%0/3
A and B
A= $_[0]B= $_[0] eq __PACKAGE__
ABresult
0X0
100
111
31 0 my $ts = shift;
32 0 ▶ length($ts) == 19 or die "Bad timestamp '$ts' - should be exactly 19 characters long (YYYY-MM-DD hh:mm:ss)";
Branch: 1 branch
BranchTrueFalseTotal
length $ts == 19 or die "Bad timestamp '${ts}' - should be exactly 19 characters long (YYYY-MM-DD hh:mm:ss)" 0 0 0
33
34 0 my ($date,$time) = split(/\s/,$ts,2);
35 0 ▶ length($date) == 10 or die "Bad date part '$date' - should be exactly 10 characters long (YYYY-MM-DD)";
Branch: 1 branch
BranchTrueFalseTotal
length $date == 10 or die "Bad date part '${date}' - should be exactly 10 characters long (YYYY-MM-DD)" 0 0 0
36 0 ▶ length($time) == 8 or die "Bad time part '$time' - should be exactly 8 characters long (hh:mm:ss)";
Branch: 1 branch
BranchTrueFalseTotal
length $time == 8 or die "Bad time part '${time}' - should be exactly 8 characters long (hh:mm:ss)" 0 0 0
37
38 0 my @d = split(/\-/,$date);
39 0 my @t = split(/\:/,$time);
40 0 ▶ scalar(@d) == 3 or die "Bad date part '$date' - didn't split ('-') into exactly 3 items";
Branch: 1 branch
BranchTrueFalseTotal
scalar @d == 3 or die "Bad date part '${date}' - didn't split ('-') into exactly 3 items" 0 0 0
41 0 ▶ scalar(@t) == 3 or die "Bad time part '$time' - didn't split (':') into exactly 3 items";
Branch: 1 branch
BranchTrueFalseTotal
scalar @t == 3 or die "Bad time part '${time}' - didn't split (':') into exactly 3 items" 0 0 0
42
43 0 my %o = ( &_dt_base_opts );
44 0 ($o{year},$o{month},$o{day},$o{hour},$o{minute},$o{second}) = (@d,@t);
45
46 0 DateTime->new(%o)
47 }
48  
49 0 ▶sub get_uid {
Subroutine get_uid: not called CC=5 SCAR=34.0
Pod undocumented
50 0 ▶ if(my $c = RapidApp->active_request_context) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $c = "RapidApp"->active_request_context) 0 0 0
51 0 ▶ return $c->user->linkedRow->id if ($c->can('user') && $c->user && $c->user->linkedRow);
Branch: 1 branch
BranchTrueFalseTotal
if $c->can("user") and $c->user and $c->user->linkedRow 0 0 0
Condition0%0/6
$c->can("user") and $c->user and $c->user->linkedRow
!ll&&!rl&&r
000
$c->can("user") and $c->user
!ll&&!rl&&r
000
MC/DC0%0/3
$c->can("user") and $c->user and $c->user->linkedRow
$c->can("user") $c->user $c->user->linkedRow
Truth table0%0/4
A and B and C
A= $c->can("user")B= $c->userC= $c->user->linkedRow
ABCresult
0XX0
10X0
1100
1111
52 }
53 0 return 0;
54 }
55  
56 0 ▶sub get_User {
Subroutine get_User: not called CC=4 SCAR=30.0
Pod undocumented
57 0 ▶ if(my $c = RapidApp->active_request_context) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $c = "RapidApp"->active_request_context) 0 0 0
58 0 ▶ return $c->user->linkedRow if ($c->can('user') && $c->user);
Branch: 1 branch
BranchTrueFalseTotal
if $c->can("user") and $c->user 0 0 0
Condition0%0/3
$c->can("user") and $c->user
!ll&&!rl&&r
000
MC/DC0%0/2
$c->can("user") and $c->user
$c->can("user") $c->user
Truth table0%0/3
A and B
A= $c->can("user")B= $c->user
ABresult
0X0
100
111
59 }
60 0 return undef;
61 }
62  
63  
64 0 ▶sub get_scaffold_cfg {
Subroutine get_scaffold_cfg: not called CC=2 SCAR=17.9
Pod undocumented
65 0 ▶ if(my $c = RapidApp->active_request_context) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $c = "RapidApp"->active_request_context) 0 0 0
66 0 ▶ return try{$c->template_controller->Access->scaffold_cfg};
Subroutine __ANON__: not called CC=1 SCAR=6.9
67 }
68 0 return undef;
69 }
70  
71  
72 0 ▶sub recaptcha_active {
Subroutine recaptcha_active: not called CC=9 SCAR=45.0
Pod undocumented
73 0 ▶ shift if ($_[0] && $_[0] eq __PACKAGE__);
Branch: 1 branch
BranchTrueFalseTotal
if $_[0] and $_[0] eq __PACKAGE__ 0 0 0
Condition0%0/3
$_[0] and $_[0] eq __PACKAGE__
!ll&&!rl&&r
000
MC/DC0%0/2
$_[0] and $_[0] eq __PACKAGE__
$_[0] $_[0] eq __PACKAGE__
Truth table0%0/3
A and B
A= $_[0]B= $_[0] eq __PACKAGE__
ABresult
0X0
100
111
74 0 ▶ my $c = shift || RapidApp->active_request_context or return 0;
Branch: 1 branch
BranchTrueFalseTotal
my $c = shift() || "RapidApp"->active_request_context or return 0 0 0 0
Condition0%0/3
shift() || 'RapidApp'->active_request_context
l!l&&r!l&&!r
000
MC/DC0%0/2
shift() || 'RapidApp'->active_request_context
shift() 'RapidApp'->active_request_context
Truth table0%0/3
A || B
A= shift()B= 'RapidApp'->active_request_context
ABresult
1X1
011
000
75
76 0 my $cfg = $c->ra_builder->recaptcha_config;
77
78 # When 'strict_mode' is active, we force recaptcha verification in all places it is supported
79 # (i.e. force ->opportunistic_recaptcha_verify to behave the same as ->recaptcha_verify)
80 # This prevents circumventing recaptcha validation by clients constructing their own POST request.
81 # The downside is that if front-side templates fail to properly enable the reCAPTCHA client side
82 # setup, the associated forms will always fail to submit because reCAPTCHA will always fail
83 0 ▶ return 1 if ($cfg->{strict_mode});
Branch: 1 branch
BranchTrueFalseTotal
if $cfg->{"strict_mode"} 0 0 0
84  
85 $cfg->{public_key}
86 && $cfg->{private_key}
87 && $c->req->method eq 'POST'
88 0 ▶ && exists $c->req->params->{'g-recaptcha-response'}
Branch: 1 branch
BranchTrueFalseTotal
$cfg->{"public_key"} and $cfg->{"private_key"} and $c->req->method eq "POST" and exists $c->req->params->{"g-recaptcha-response"} 0 0 0
Condition0%0/6
$cfg->{"public_key"} and $cfg->{"private_key"} and $c->req->method eq "POST"
!ll&&!rl&&r
000
$cfg->{"public_key"} and $cfg->{"private_key"}
!ll&&!rl&&r
000
MC/DC0%0/3
$cfg->{"public_key"} and $cfg->{"private_key"} and $c->req->method eq "POST"
$cfg->{"public_key"} $cfg->{"private_key"} $c->req->method eq "POST"
Truth table0%0/4
A and B and C
A= $cfg->{"public_key"}B= $cfg->{"private_key"}C= $c->req->method eq "POST"
ABCresult
0XX0
10X0
1100
1111
89 }
90  
91 # opportunistic_recaptcha_verify only runs, and possibly fails, if all the needed reCAPTCHA pieces
92 # are active. When 'strict_mode' is turned on, this method behaves the same as recaptcha_verify.
93 # See the POD for more information of 'strict_mode'
94 0 ▶sub opportunistic_recaptcha_verify {
Subroutine opportunistic_recaptcha_verify: not called CC=6 SCAR=37.4
Pod undocumented
95 0 ▶ shift if ($_[0] && $_[0] eq __PACKAGE__);
Branch: 1 branch
BranchTrueFalseTotal
if $_[0] and $_[0] eq __PACKAGE__ 0 0 0
Condition0%0/3
$_[0] and $_[0] eq __PACKAGE__
!ll&&!rl&&r
000
MC/DC0%0/2
$_[0] and $_[0] eq __PACKAGE__
$_[0] $_[0] eq __PACKAGE__
Truth table0%0/3
A and B
A= $_[0]B= $_[0] eq __PACKAGE__
ABresult
0X0
100
111
96 0 ▶ my $c = shift || RapidApp->active_request_context or return 1;
Branch: 1 branch
BranchTrueFalseTotal
my $c = shift() || "RapidApp"->active_request_context or return 1 0 0 0
Condition0%0/3
shift() || 'RapidApp'->active_request_context
l!l&&r!l&&!r
000
MC/DC0%0/2
shift() || 'RapidApp'->active_request_context
shift() 'RapidApp'->active_request_context
Truth table0%0/3
A || B
A= shift()B= 'RapidApp'->active_request_context
ABresult
1X1
011
000
97 0 ▶ &recaptcha_active($c) ? &recaptcha_verify($c) : 1
Branch: 1 branch
BranchTrueFalseTotal
&recaptcha_active($c) ? : 0 0 0
98 }
99  
100  
101 0 ▶sub recaptcha_verify {
Subroutine recaptcha_verify: not called CC=10 SCAR=47.0
Pod undocumented
102 0 ▶ shift if ($_[0] && $_[0] eq __PACKAGE__);
Branch: 1 branch
BranchTrueFalseTotal
if $_[0] and $_[0] eq __PACKAGE__ 0 0 0
Condition0%0/3
$_[0] and $_[0] eq __PACKAGE__
!ll&&!rl&&r
000
MC/DC0%0/2
$_[0] and $_[0] eq __PACKAGE__
$_[0] $_[0] eq __PACKAGE__
Truth table0%0/3
A and B
A= $_[0]B= $_[0] eq __PACKAGE__
ABresult
0X0
100
111
103 0 ▶ my $c = shift || RapidApp->active_request_context;
Condition0%0/3
shift() || 'RapidApp'->active_request_context
l!l&&r!l&&!r
000
MC/DC0%0/2
shift() || 'RapidApp'->active_request_context
shift() 'RapidApp'->active_request_context
Truth table0%0/3
A || B
A= shift()B= 'RapidApp'->active_request_context
ABresult
1X1
011
000
104
105 0 ▶ &recaptcha_active($c) or return 0;
Branch: 1 branch
BranchTrueFalseTotal
&recaptcha_active($c) or return 0 0 0 0
106
107 0 my $cfg = $c->ra_builder->recaptcha_config;
108  
109 my $packet = {
110 secret => $cfg->{private_key},
111 0 response => $c->req->params->{'g-recaptcha-response'},
112 #remoteip => $c->req->address
113 };
114 0 my $content_payload = join('&',map { join('=',$_,$packet->{$_}) } keys %$packet);
115
116 0 ▶ my $url = $cfg->{verify_url} || 'https://www.google.com/recaptcha/api/siteverify';
Condition0%0/2
$cfg->{'verify_url'} || 'https://www.google.com/recaptcha/api/siteverify'
l!l
00
MC/DC0%0/1
$cfg->{'verify_url'} || 'https://www.google.com/recaptcha/api/siteverify'
$cfg->{'verify_url'}
Truth table0%0/2
A
A= $cfg->{'verify_url'}
Aresult
11
00
117
118 # for refernece, this is how to turn of certificate validation, which should not be needed
119 # as long as the remote endpoint is a Google system
120 #local $ENV{'PERL_LWP_SSL_VERIFY_HOSTNAME'} = 0;
121
122 0 my $ua = LWP::UserAgent->new;
123 0 $ua->agent('rapi-blog/' . $Rapi::Blog::VERSION);
124 0 $ua->timeout(30); # 30 seconds
125  
126 0 my $req = HTTP::Request->new( 'POST', $url );
127 0 $req->header( 'Content-Type' => 'application/x-www-form-urlencoded' );
128 0 $req->content( $content_payload );
129  
130 0 $c->log->info('Validating reCAPTCHA: POST -> '.$url);
131 0 my $res = $ua->request($req);
132  
133 0 ▶ if($res->is_success) {
Branch: 1 branch
BranchTrueFalseTotal
if ($res->is_success) { } 0 0 0
134 0 my $data = decode_json_utf8( $res->decoded_content );
135  
136 0 my $success = $data->{success};
137 0 ▶ $success = $$success if (ref($success)||'' eq 'SCALAR');
Branch: 1 branch
BranchTrueFalseTotal
if 0 0 0
Condition0%0/2
ref $success or
l!l
00
MC/DC0%0/1
ref $success or
ref $success
Truth table0%0/2
A
A= ref $success
Aresult
11
00
138
139 0 ▶ return $success ? 1 : 0
Branch: 1 branch
BranchTrueFalseTotal
$success ? : 0 0 0
140 }
141 else {
142 0 $c->log->error('reCAPTCHA validation failed');
143 0 return 0;
144 }
145 }
146  
147 0 ▶sub sanitize_input {
Subroutine sanitize_input: not called CC=5 SCAR=34.0
Pod undocumented
148 0 ▶ shift if ($_[0] && $_[0] eq __PACKAGE__);
Branch: 1 branch
BranchTrueFalseTotal
if $_[0] and $_[0] eq __PACKAGE__ 0 0 0
Condition0%0/3
$_[0] and $_[0] eq __PACKAGE__
!ll&&!rl&&r
000
MC/DC0%0/2
$_[0] and $_[0] eq __PACKAGE__
$_[0] $_[0] eq __PACKAGE__
Truth table0%0/3
A and B
A= $_[0]B= $_[0] eq __PACKAGE__
ABresult
0X0
100
111
149 0 my $content = shift;
150
151 0 ▶ my $c = RapidApp->active_request_context or die join('',
Branch: 1 branch
BranchTrueFalseTotal
my $c = "RapidApp"->active_request_context or die "Failed to obtain request context \$c - sanitize_input() can only be called during a request" 0 0 0
152 'Failed to obtain request context $c - ',
153 'sanitize_input() can only be called during a request'
154 );
155
156 0 ▶ my $Scrubber = $c->ra_builder->input_Scrubber or die "Failed to load HTML::Scrubber object";
Branch: 1 branch
BranchTrueFalseTotal
my $Scrubber = $c->ra_builder->input_Scrubber or die "Failed to load HTML::Scrubber object" 0 0 0
157
158 0 $Scrubber->scrub( $content )
159 }
160  
161 2 1;
« lib/Rapi/Blog/DB/ResultSet/Post.pm ↑ Summary lib/Rapi/Blog/Util/ppRender.pm »