ZofCMS/Plugin/FloodControl.pm

filter: Statementstmt 36.8% 14 / 38 Branchbran 0.0% 0 / 22 Conditioncond 0.0% 0 / 8 MC/DCmcdc 0.0% 0 / 5 Subroutinesub 57.1% 4 / 7 Podpod n/a totaltotal 22.5% 18 / 80 CC 15 SCAR 51.5
CC
15
Cov
21%
_do
240.0
_key
2.0
_defaults
2.0
CRAP 171.8
« ZofCMS/Plugin/DBIPPT.pm ↑ Summary ZofCMS/Plugin/UserLogin.pm »
1 package App::ZofCMS::Plugin::FloodControl;
2  
3 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 1 our $VERSION = '1.001010'; # VERSION
7  
8 1 ▶use DBI;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use base 'App::ZofCMS::Plugin::Base';
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 0 ▶sub _key { 'plug_flood_control' }
Subroutine _key: not called CC=1 SCAR=6.9
12 0 ▶sub _defaults {
Subroutine _defaults: not called CC=1 SCAR=6.9
13 return (
14 # dsn => "DBI:mysql:database=test;host=localhost",
15 # user => 'test',
16 # pass => 'test',
17 0 create_table => 0,
18 opt => { RaiseError => 1, AutoCommit => 1 },
19 limit => 2,
20 run => 1,
21 timeout => 600,
22 table => 'flood_control',
23 trigger => 'plug_flood',
24 t_key => 'plug_flood',
25 cell => 'q',
26 flood_id => 'flood',
27 );
28 }
29 0 ▶sub _do {
Subroutine _do: not called CC=15 SCAR=54.8
30 0 my ( $self, $conf, $template, $query, $config ) = @_;
31  
32 0 ▶ unless ( $conf->{run} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($conf->{"run"}) 0 0 0
33 0 my ( $cell, $trigger ) = @$conf{ qw/cell trigger/ };
34 0 ▶ if ( $cell eq 'q' ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($cell eq 'q') { } 0 0 0
elsif ($cell eq 't' or $cell eq 'd') { } 0 0 0
elsif ($cell eq '') { } 0 0 0
35 return
36 unless defined $query->{ $trigger }
37 0 ▶ and length $query->{ $trigger };
Branch: 1 branch
BranchTrueFalseTotal
unless defined $query->{$trigger} and length $query->{$trigger} 0 0 0
Condition0%0/3
defined $query->{$trigger} and length $query->{$trigger}
!ll&&!rl&&r
000
MC/DC0%0/2
defined $query->{$trigger} and length $query->{$trigger}
defined $query->{$trigger} length $query->{$trigger}
Truth table0%0/3
A and B
A= defined $query->{$trigger}B= length $query->{$trigger}
ABresult
0X0
100
111
38 }
39 ▶ elsif ( $cell eq 't' or $cell eq 'd' ) {
Condition0%0/3
$cell eq "t" or $cell eq "d"
l!l&&r!l&&!r
000
MC/DC0%0/2
$cell eq "t" or $cell eq "d"
$cell eq "t" $cell eq "d"
Truth table0%0/3
A or B
A= $cell eq "t"B= $cell eq "d"
ABresult
1X1
011
000
40 0 ▶ return unless $template->{ $cell }{ $trigger };
Branch: 1 branch
BranchTrueFalseTotal
unless $template->{$cell}{$trigger} 0 0 0
41 }
42 elsif ( $cell eq '' ) {
43 0 ▶ return unless $template->{ $trigger };
Branch: 1 branch
BranchTrueFalseTotal
unless $template->{$trigger} 0 0 0
44 }
45 }
46  
47 0 my $host = $config->cgi->remote_host;
48 0 $host = substr $host, 0, 250;
49  
50 my $dbh = DBI->connect_cached(
51 0 @$conf{ qw/dsn user pass opt/ }
52 );
53  
54 0 ▶ if ( $conf->{create_table} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($conf->{"create_table"}) 0 0 0
55 0 $dbh->do(
56 "CREATE TABLE $conf->{table} (host TEXT, time VARCHAR(10), id VARCHAR(5));",
57 );
58 }
59  
60 $dbh->do(
61 "DELETE FROM $conf->{table} WHERE time < ? AND id = ?;",
62 undef,
63 time() - $conf->{timeout},
64 $conf->{flood_id},
65 0 );
66  
67 my $entries = $dbh->selectall_arrayref(
68 "SELECT * FROM $conf->{table} WHERE host = ? AND id = ?;",
69 { Slice => {} },
70 $host,
71 $conf->{flood_id},
72 0 );
73  
74 0 ▶ if ( @{ $entries || [] } >= $conf->{limit} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$entries or [];} >= $conf->{'limit'}) { } 0 0 0
Condition0%0/2
$entries || []
l!l
00
MC/DC0%0/1
$entries || []
$entries
Truth table0%0/2
A
A= $entries
Aresult
11
00
75 0 $template->{t}{ $conf->{t_key} } = 1;
76 0 ▶ if ( $conf->{flood_code} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($conf->{"flood_code"}) 0 0 0
77 0 $conf->{flood_code}( $template, $query, $config );
78 }
79 }
80 else {
81 $dbh->do(
82 "INSERT INTO $conf->{table} VALUES(?, ?, ?);",
83 undef,
84 $host,
85 time(),
86 $conf->{flood_id},
87 0 );
88 0 ▶ if ( $conf->{no_flood_code} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($conf->{"no_flood_code"}) 0 0 0
89 0 $conf->{no_flood_code}( $template, $query, $config );
90 }
91 }
92 }
93  
94 1 1;
95 __END__
« ZofCMS/Plugin/DBIPPT.pm ↑ Summary ZofCMS/Plugin/UserLogin.pm »