blib/lib/Email/Enkoder.pm

filter: Statementstmt 100% 82 / 82 Branchbran 94.4% 17 / 18 Conditioncond 80.0% 8 / 10 MC/DCmcdc 33.3% 2 / 6 Subroutinesub 100% 12 / 12 Podpod 100% 5 / 5 totaltotal 94.7% 126 / 133 CC 18 SCAR 14.6
CC
18
Cov
97%
enkode
7.0
enkode_mail
6.0
js_wrap_quote
5.0
CRAP 4.3
↑ Summary
1 2 ▶use strictures;
Subroutine BEGIN: called CC=1 SCAR=0.0
2  
3 package Email::Enkoder;
4  
5 2 ▶use 5.010;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use Sub::Exporter::Simple qw' enkode enkode_mail ';
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 2 our $VERSION = '1.120831'; # VERSION
10  
11 # ABSTRACT: obfuscate email or html with randomized javascript
12  
13 #
14 # This file is part of Email-Enkoder
15 #
16 #
17 # Christian Walde has dedicated the work to the Commons by waiving all of his
18 # or her rights to the work worldwide under copyright law and all related or
19 # neighboring legal rights he or she had in the work, to the extent allowable by
20 # law.
21 #
22 # Works under CC0 do not require attribution. When citing the work, you should
23 # not imply endorsement by the author.
24 #
25  
26 my $default_enkoders = [
27 {
28 5 ▶ perl => sub { reverse @_ },
Subroutine __ANON__: called CC=1 SCAR=0.0
29 js => ";kode=kode.split('').reverse().join('')",
30 },
31 {
32 6 ▶ perl => sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
33 6 my ( $string ) = @_;
34 6 for my $i ( 0 .. ( length( $string ) / 2 - 1 ) ) {
35 1139 $i *= 2;
36 1139 my $left = substr $string, $i, 1;
37 1139 my $right = substr $string, $i + 1, 1;
38 1139 substr $string, $i, 1, $right;
39 1139 substr $string, $i + 1, 1, $left;
40 }
41 6 return $string;
42 },
43 js => (
44 ";x='';for(i=0;i<(kode.length-1);i+=2){"
45 . "x+=kode.charAt(i+1)+kode.charAt(i)"
46 . "}kode=x+(i<kode.length?kode.charAt(kode.length-1):'');"
47 )
48 },
49 {
50 9 ▶ perl => sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
51 9 my ( $str ) = @_;
52 9 my @chars = split //, $str;
53 9 for ( @chars ) {
54 2501 my $ord = ord $_;
55 2501 $ord += 3;
56 2501 $_ = chr $ord;
57 }
58 9 my $result = join '', @chars;
59 9 return $result;
60 },
61 2 js => (
62 ";x='';for(i=0;i<kode.length;i++){" #
63 . "c=kode.charCodeAt(i)-3;x+=String.fromCharCode(c)"
64 . "}kode=x"
65 )
66 },
67 ];
68  
69  
70 2 ▶sub enkode_mail {
Subroutine enkode_mail: called CC=6 SCAR=17.9
Pod documented
71 2 my ( $email, $options ) = @_;
72  
73 2 ▶ $options->{link_text} //= $email;
Condition66%2/3
$options->{"link_text"} //= $email
l!l&&r!l&&!r
110
MC/DC0%0/2
$options->{"link_text"} //= $email
$options->{"link_text"} $email
Truth table66%2/3
A //= B
A= $options->{"link_text"}B= $email
ABresult
1X1
011
000
74 2 ▶ $_ = defined $_ ? qq| $_| : '' for $options->{link_attributes};
Branch: 1 branch
BranchTrueFalseTotal
defined $_ ? : 1 1 2
75 2 ▶ $_ = defined $_ ? qq|?subject=$_| : '' for $options->{subject};
Branch: 1 branch
BranchTrueFalseTotal
defined $_ ? : 1 1 2
76  
77 2 $email = qq|<a href="mailto:$email$options->{subject}"$options->{link_attributes}>$options->{link_text}</a>|;
78 2 my $link = enkode( $email, $options );
79  
80 2 return $link;
81 }
82  
83  
84 7 ▶sub enkode {
Subroutine enkode: called CC=7 SCAR=19.5
Pod documented
85 7 my ( $html, $options ) = @_;
86  
87 7 ▶ $options->{enkoders} ||= enkoders();
Condition66%2/3
$options->{"enkoders"} ||= enkoders()
l!l&&r!l&&!r
250
MC/DC0%0/2
$options->{"enkoders"} ||= enkoders()
$options->{"enkoders"} enkoders()
Truth table66%2/3
A ||= B
A= $options->{"enkoders"}B= enkoders()
ABresult
1X1
011
000
88 7 ▶ $options->{max_length} ||= 1024;
Condition100%2/2
$options->{"max_length"} ||= 1024
l!l
52
MC/DC100%1/1
$options->{"max_length"} ||= 1024
$options->{"max_length"}
Truth table100%2/2
A
A= $options->{"max_length"}
Aresult
11
00
89  
90 7 my $js = js_dbl_quote( $html );
91 7 $js = qq|document.write($js);|;
92  
93 7 ▶ $options->{max_length} = 1 + length $js if $options->{max_length} <= length $js;
Branch: 1 branch
BranchTrueFalseTotal
if $options->{"max_length"} <= length $js 5 2 7
94  
95 7 my $script_html;
96  
97 7 ▶ while ( $options->{max_length} > length $js ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($options->{"max_length"} > length $js) 22 0 22
98 22 ▶ my $idx = $options->{enkoder_index} // int rand @{ $options->{enkoders} };
Condition100%2/2
$options->{"enkoder_index"} // int rand @{$options->{"enkoders"};}
l!l
319
MC/DC100%1/1
$options->{"enkoder_index"} // int rand @{$options->{"enkoders"};}
$options->{"enkoder_index"}
Truth table100%2/2
A
A= $options->{"enkoder_index"}
Aresult
11
00
99 22 $js = $options->{enkoders}[$idx]{perl}->( $js );
100 22 $js = js_dbl_quote( $js );
101 22 $js = qq|kode=$js$options->{enkoders}[$idx]{js}|;
102 22 my $js_variable = js_wrap_quote( js_dbl_quote( $js ), 79 );
103  
104 22 $script_html = qq|
105 <script type="text/javascript">
106 /* <![CDATA[ */
107 function perl_enkoder(){var kode=
108 $js_variable
109 ;var i,c,x;while(eval(kode));}perl_enkoder();
110 /* ]]> */
111 </script>
112 |;
113 22 $script_html =~ s/^ +//mg;
114 22 ▶ last if length $script_html > $options->{max_length};
Branch: 1 branch
BranchTrueFalseTotal
if length $script_html > $options->{"max_length"} 7 15 22
115 }
116  
117 7 return $script_html;
118 }
119  
120  
121 6 ▶sub enkoders { $default_enkoders }
Subroutine enkoders: called CC=1 SCAR=0.0
Pod documented
122  
123  
124 23 ▶sub js_wrap_quote {
Subroutine js_wrap_quote: called CC=5 SCAR=16.1
Pod documented
125 23 my ( $str, $max_line_length ) = @_;
126  
127 23 $max_line_length -= 3;
128 23 my $inQ;
129 23 my $esc = 0;
130 23 my $lineLen = 0;
131 23 my $result = '';
132 23 my $chunk = '';
133  
134 23 ▶ while ( length $str ) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $str) 8769 23 8792
135 8769 my $chunk = '';
136 8769 ▶ if ( $str =~ /^\\[0-7]{3}/ ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($str =~ /^\\[0-7]{3}/) { } 1 8768 8769
elsif ($str =~ /^\\./) { } 256 8512 8768
137 1 $chunk = substr $str, 0, 4;
138 1 substr $str, 0, 4, '';
139 }
140 elsif ( $str =~ /^\\./ ) {
141 256 $chunk = substr $str, 0, 2;
142 256 substr $str, 0, 2, '';
143 }
144 else {
145 8512 $chunk = substr $str, 0, 1;
146 8512 substr $str, 0, 1, '';
147 }
148  
149 8769 ▶ if ( ( $lineLen + length $chunk ) >= $max_line_length ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($lineLen + length($chunk) >= $max_line_length) 113 8656 8769
150 113 $result .= qq|"+\n"|;
151 113 $lineLen = 1;
152 }
153  
154 8769 $lineLen += length $chunk;
155 8769 $result .= $chunk;
156 }
157  
158 23 return $result;
159 }
160  
161  
162 51 ▶sub js_dbl_quote {
Subroutine js_dbl_quote: called CC=1 SCAR=0.0
Pod documented
163 51 my ( $in ) = @_;
164 51 $in =~ s/([\\"])/\\$1/g;
165 51 return qq|"$in"|;
166 }
167  
168 2 1;
169  
170 __END__
↑ Summary