MToken/Util.pm

filter: Statementstmt 55.3% 67 / 121 Branchbran 28.1% 9 / 32 Conditioncond 12.8% 5 / 39 MC/DCmcdc 0.0% 0 / 19 Subroutinesub 50.0% 17 / 34 Podpod 100% 20 / 20 totaltotal 44.5% 118 / 265 CC 32 SCAR 25.7
CC
32
Cov
42%
parse_credentials
42.0
hide_pasword
42.0
md5sum
30.0
CRAP 13.0
« MToken/Server/Alpha.pm ↑ Summary MToken/Const.pm »
1 package MToken::Util; # $Id: Util.pm 92 2021-10-05 16:43:59Z minus $
2 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 4 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 =encoding utf-8
6  
7 =head1 NAME
8  
9 MToken::Util - Exported utility functions
10  
11 =head1 VERSION
12  
13 Version 1.03
14  
15 =head1 SYNOPSIS
16  
17 use MToken::Util;
18  
19 =head1 DESCRIPTION
20  
21 Exported utility functions
22  
23 =over
24  
25 =item B<cleanServerName>
26  
27 my $servername = cleanServerName( "my.server.com" );
28  
29 Clening the specified ServerName value
30  
31 =item B<cleanFileName>
32  
33 my $filename = cleanServerName( "mtoken.12345678" );
34  
35 Clening the specified FileName value
36  
37 =item B<explain>
38  
39 print explain( $object );
40  
41 Returns Data::Dumper dump
42  
43 =item B<md5sum>
44  
45 my $md5 = md5sum( $file );
46  
47 See L<Digest::MD5>
48  
49 =item B<sha1sum>
50  
51 my $sha1 = sha1sum( $file );
52  
53 See L<Digest::SHA1>
54  
55 =item B<filesize>
56  
57 my $fsize = filesize( $file );
58  
59 Returns file size
60  
61 =item B<hide_pasword>
62  
63 print hide_pasword('http://user:password@example.com'); # 'http://user:*****@example.com'
64  
65 Returns specified URL but without password
66  
67 =item B<blue>, B<cyan>, B<green>, B<red>, B<yellow>, B<magenta>
68  
69 print cyan("Format %s", "text");
70  
71 Returns colored string
72  
73 =item B<nope>, B<skip>, B<wow>, B<yep>
74  
75 my $status = nope("Format %s", "text");
76  
77 Prints status message and returns status.
78  
79 For nope returns - 0; for skip, wow, yep - 1
80  
81 =item B<parse_credentials>
82  
83 my ($user, $password) = parse_credentials( 'http://user:password@example.com' );
84 my ($user, $password) = parse_credentials( new URI('http://user:password@example.com') );
85  
86 Returns credentials pair by URL or URI object
87  
88 =item B<tcd_load>
89  
90 if (my $text = tcd_load("/my/file.tcd")) {
91 print $text; # Blah-Blah-Blah
92 } else {
93 or die("Oops");
94 }
95  
96 Load text data from TCD04 file
97  
98 =item B<tcd_save>
99  
100 tcd_save("/my/file.tcd", "Blah-Blah-Blah") or die("Oops");
101  
102 Save text data to TCD04 file
103  
104 =back
105  
106 =head1 HISTORY
107  
108 See C<Changes> file
109  
110 =head1 TO DO
111  
112 See C<TODO> file
113  
114 =head1 BUGS
115  
116 * none noted
117  
118 =head1 SEE ALSO
119  
120 L<Digest::MD5>, L<Digest::SHA1>
121  
122 =head1 AUTHOR
123  
124 Serż Minus (Sergey Lepenkov) L<http://www.serzik.com> E<lt>abalama@cpan.orgE<gt>
125  
126 =head1 COPYRIGHT
127  
128 Copyright (C) 1998-2021 D&D Corporation. All Rights Reserved
129  
130 =head1 LICENSE
131  
132 This program is free software; you can redistribute it and/or
133 modify it under the same terms as Perl itself.
134  
135 See C<LICENSE> file and L<https://dev.perl.org/licenses/>
136  
137 =cut
138  
139 4 ▶use vars qw/ $VERSION @EXPORT_OK @EXPORT /;
Subroutine BEGIN: called CC=1 SCAR=0.0
140 4 $VERSION = "1.03";
141  
142 4 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
143 4 ▶use CTK::Util qw/bload bsave/;
Subroutine BEGIN: called CC=1 SCAR=0.0
144 4 ▶use CTK::Crypt::TCD04;
Subroutine BEGIN: called CC=1 SCAR=0.0
145 4 ▶use URI;
Subroutine BEGIN: called CC=1 SCAR=0.0
146 4 ▶use URI::Escape qw/uri_unescape/;
Subroutine BEGIN: called CC=1 SCAR=0.0
147 4 ▶use Digest::MD5;
Subroutine BEGIN: called CC=1 SCAR=0.0
148 4 ▶use Digest::SHA;
Subroutine BEGIN: called CC=1 SCAR=0.0
149 4 ▶use Data::Dumper; #$Data::Dumper::Deparse = 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
150 4 ▶use Term::ANSIColor qw/colored/;
Subroutine BEGIN: called CC=1 SCAR=0.0
151  
152 4 ▶use MToken::Const qw/IS_TTY/;
Subroutine BEGIN: called CC=1 SCAR=0.0
153  
154 4 ▶use base qw/Exporter/;
Subroutine BEGIN: called CC=1 SCAR=0.0
155 4 @EXPORT = qw(
156 yep nope skip wow
157 blue green red yellow cyan magenta
158 );
159 4 @EXPORT_OK = (qw(
160 cleanServerName cleanFileName
161 filesize md5sum sha1sum
162 parse_credentials hide_pasword
163 tcd_save tcd_load
164 explain
165 ), @EXPORT);
166  
167 0 ▶sub cleanServerName {
Subroutine cleanServerName: not called CC=2 SCAR=17.9
Pod documented
168 0 ▶ my $sn = shift // 'localhost';
Condition0%0/2
shift() // "localhost"
l!l
00
MC/DC0%0/1
shift() // "localhost"
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
169 0 $sn =~ s/[^a-z0-9_\-.]//ig;
170 0 return $sn;
171 }
172 0 ▶sub cleanFileName {
Subroutine cleanFileName: not called CC=2 SCAR=17.9
Pod documented
173 0 ▶ my $f = shift // '';
Condition0%0/2
shift() // ""
l!l
00
MC/DC0%0/1
shift() // ""
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
174 0 $f =~ s/[^a-z0-9_\-.]//ig;
175 0 return $f;
176 }
177 4 ▶sub sha1sum {
Subroutine sha1sum: called CC=5 SCAR=17.1
Pod documented
178 4 my $f = shift;
179 4 my $sha1 = Digest::SHA->new;
180 4 my $sum = '';
181 4 ▶ return $sum unless -e $f;
Branch: 1 branch
BranchTrueFalseTotal
unless -e $f 3 1 4
182 1 ▶ open( my $sha1_fh, '<', $f) or (carp("Can't open '$f': $!") && return $sum);
Branch: 1 branch
BranchTrueFalseTotal
open my $sha1_fh, "<", $f or carp("Can't open '${f}': $!") and return $sum 1 0 1
Condition20%1/5
open(my $sha1_fh, '<', $f) || carp("Can't open '${f}': $!") && (return $sum)
l!l&&r!l&&!r
100
carp("Can't open '${f}': $!") and return $sum
!ll
00
MC/DC0%0/2
open(my $sha1_fh, '<', $f) || carp("Can't open '${f}': $!") && (return $sum)
open(my $sha1_fh, '<', $f) carp("Can't open '${f}': $!")
Truth table33%1/3
A || B
A= open(my $sha1_fh, '<', $f)B= carp("Can't open '${f}': $!")
ABresult
1X1
011
000
183 1 ▶ if ($sha1_fh) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sha1_fh) 1 0 1
184 1 binmode($sha1_fh);
185 1 $sha1->addfile($sha1_fh);
186 1 $sum = $sha1->hexdigest;
187 1 close($sha1_fh);
188 }
189 1 return $sum;
190 }
191 0 ▶sub md5sum {
Subroutine md5sum: not called CC=5 SCAR=34.0
Pod documented
192 0 my $f = shift;
193 0 my $md5 = Digest::MD5->new;
194 0 my $sum = '';
195 0 ▶ return $sum unless -e $f;
Branch: 1 branch
BranchTrueFalseTotal
unless -e $f 0 0 0
196 0 ▶ open( my $md5_fh, '<', $f) or (carp("Can't open '$f': $!") && return $sum);
Branch: 1 branch
BranchTrueFalseTotal
open my $md5_fh, "<", $f or carp("Can't open '${f}': $!") and return $sum 0 0 0
Condition0%0/5
open(my $md5_fh, '<', $f) || carp("Can't open '${f}': $!") && (return $sum)
l!l&&r!l&&!r
000
carp("Can't open '${f}': $!") and return $sum
!ll
00
MC/DC0%0/2
open(my $md5_fh, '<', $f) || carp("Can't open '${f}': $!") && (return $sum)
open(my $md5_fh, '<', $f) carp("Can't open '${f}': $!")
Truth table0%0/3
A || B
A= open(my $md5_fh, '<', $f)B= carp("Can't open '${f}': $!")
ABresult
1X1
011
000
197 0 ▶ if ($md5_fh) {
Branch: 1 branch
BranchTrueFalseTotal
if ($md5_fh) 0 0 0
198 0 binmode($md5_fh);
199 0 $md5->addfile($md5_fh);
200 0 $sum = $md5->hexdigest;
201 0 close($md5_fh);
202 }
203 0 return $sum;
204 }
205 0 ▶sub filesize {
Subroutine filesize: not called CC=2 SCAR=17.9
Pod documented
206 0 my $f = shift;
207 0 my $filesize = 0;
208 0 ▶ $filesize = (stat $f)[7] if -e $f;
Branch: 1 branch
BranchTrueFalseTotal
if -e $f 0 0 0
209 0 return $filesize;
210 }
211 0 ▶sub parse_credentials {
Subroutine parse_credentials: not called CC=6 SCAR=37.4
Pod documented
212 0 ▶ my $url = shift || return ();
Condition0%0/2
shift() || (return ())
l!l
00
MC/DC0%0/1
shift() || (return ())
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
213 0 ▶ my $uri = (ref($url) eq 'URI') ? $url : URI->new($url);
Branch: 1 branch
BranchTrueFalseTotal
ref $url eq 'URI' ? : 0 0 0
214 0 ▶ my $info = $uri->userinfo() // "";
Condition0%0/2
$uri->userinfo // ""
l!l
00
MC/DC0%0/1
$uri->userinfo // ""
$uri->userinfo
Truth table0%0/2
A
A= $uri->userinfo
Aresult
11
00
215 0 my $user = $info;
216 0 my $pass = $info;
217 0 $user =~ s/:.*//;
218 0 $pass =~ s/^[^:]*://;
219 0 ▶ return (uri_unescape($user // ''), uri_unescape($pass // ''));
Condition0%0/4
$user // ""
l!l
00
$pass // ""
l!l
00
MC/DC0%0/1
$user // ""
$user
MC/DC0%0/1
$pass // ""
$pass
Truth table0%0/2
A
A= $user
Aresult
11
00
Truth table0%0/2
A
A= $pass
Aresult
11
00
220 }
221 0 ▶sub hide_pasword {
Subroutine hide_pasword: not called CC=6 SCAR=37.4
Pod documented
222 0 ▶ my $url = shift || return "";
Condition0%0/2
shift() || (return '')
l!l
00
MC/DC0%0/1
shift() || (return '')
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
223 0 ▶ my $full = shift || 0;
Condition0%0/2
shift() || 0
l!l
00
MC/DC0%0/1
shift() || 0
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
224 0 my $uri = URI->new($url);
225 0 my ($u,$p) = parse_credentials($uri);
226 0 ▶ return $url unless defined($p) && length($p);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $p and length $p 0 0 0
Condition0%0/3
defined $p and length $p
!ll&&!rl&&r
000
MC/DC0%0/2
defined $p and length $p
defined $p length $p
Truth table0%0/3
A and B
A= defined $pB= length $p
ABresult
0X0
100
111
227 0 ▶ $uri->userinfo($full ? undef : sprintf("%s:*****", $u));
Branch: 1 branch
BranchTrueFalseTotal
$full ? : 0 0 0
228 0 return $uri->canonical->as_string;
229 }
230 1 ▶sub tcd_save {
Subroutine tcd_save: called CC=5 SCAR=17.2
Pod documented
231 1 my $fn = shift;
232 1 ▶ my $text = shift // '';
Condition50%1/2
shift() // ""
l!l
10
MC/DC0%0/1
shift() // ""
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
233 1 ▶ carp("No file specified") unless $fn;
Branch: 1 branch
BranchTrueFalseTotal
unless $fn 0 1 1
234 1 ▶ return unless length $text;
Branch: 1 branch
BranchTrueFalseTotal
unless length $text 0 1 1
235 1 ▶ bsave($fn, CTK::Crypt::TCD04->new()->encrypt($text))
Branch: 1 branch
BranchTrueFalseTotal
bsave($fn, "CTK::Crypt::TCD04"->new->encrypt($text)) or carp(qq[Can't save file "$fn"]) 1 0 1
236 or carp("Can't save file \"$fn\"");
237 1 return 1;
238 }
239 1 ▶sub tcd_load {
Subroutine tcd_load: called CC=6 SCAR=22.0
Pod documented
240 1 my $fn = shift;
241 1 ▶ carp("No file specified") unless $fn;
Branch: 1 branch
BranchTrueFalseTotal
unless $fn 0 1 1
242 1 ▶ return unless -f $fn and -r _ and -s _;
Branch: 1 branch
BranchTrueFalseTotal
unless -f $fn and -r _ and -s _ 0 1 1
Condition33%2/6
-f $fn and -r _ and -s _
!ll&&!rl&&r
001
-f $fn and -r _
!ll&&!rl&&r
001
MC/DC0%0/3
-f $fn and -r _ and -s _
-f $fn -r _ -s _
Truth table25%1/4
A and B and C
A= -f $fnB= -r _C= -s _
ABCresult
0XX0
10X0
1100
1111
243 1 ▶ return CTK::Crypt::TCD04->new()->decrypt(bload($fn) // '');
Condition50%1/2
bload($fn) // ""
l!l
10
MC/DC0%0/1
bload($fn) // ""
bload($fn)
Truth table50%1/2
A
A= bload($fn)
Aresult
11
00
244 }
245 0 ▶sub explain {
Subroutine explain: not called CC=2 SCAR=17.9
Pod documented
246 0 my $dumper = Data::Dumper->new( [shift] );
247 0 $dumper->Indent(1)->Terse(1);
248 0 ▶ $dumper->Sortkeys(1) if $dumper->can("Sortkeys");
Branch: 1 branch
BranchTrueFalseTotal
if $dumper->can("Sortkeys") 0 0 0
249 0 return $dumper->Dump;
250 }
251  
252 ################
253 # Colored says
254 ################
255 0 ▶sub yep {
Subroutine yep: not called CC=1 SCAR=6.9
Pod documented
256 0 print(green(sprintf(shift, @_)), "\n");
257 0 return 1;
258 }
259 0 ▶sub nope {
Subroutine nope: not called CC=1 SCAR=6.9
Pod documented
260 0 print(red(sprintf(shift, @_)), "\n");
261 0 return 0;
262 }
263 0 ▶sub skip {
Subroutine skip: not called CC=1 SCAR=6.9
Pod documented
264 0 print(yellow(sprintf(shift, @_)), "\n");
265 0 return 1;
266 }
267 0 ▶sub wow {
Subroutine wow: not called CC=1 SCAR=6.9
Pod documented
268 0 print(blue(sprintf(shift, @_)), "\n");
269 0 return 1;
270 }
271  
272 # Colored helper functions
273 0 ▶sub green { IS_TTY ? colored(['bright_green'], sprintf(shift, @_)) : sprintf(shift, @_) }
Subroutine green: not called CC=1 SCAR=6.9
Pod documented
274 0 ▶sub red { IS_TTY ? colored(['bright_red'], sprintf(shift, @_)) : sprintf(shift, @_) }
Subroutine red: not called CC=1 SCAR=6.9
Pod documented
275 0 ▶sub yellow { IS_TTY ? colored(['bright_yellow'], sprintf(shift, @_)) : sprintf(shift, @_) }
Subroutine yellow: not called CC=1 SCAR=6.9
Pod documented
276 0 ▶sub cyan { IS_TTY ? colored(['bright_cyan'], sprintf(shift, @_)) : sprintf(shift, @_) }
Subroutine cyan: not called CC=1 SCAR=6.9
Pod documented
277 0 ▶sub blue { IS_TTY ? colored(['bright_blue'], sprintf(shift, @_)) : sprintf(shift, @_) }
Subroutine blue: not called CC=1 SCAR=6.9
Pod documented
278 0 ▶sub magenta {IS_TTY ? colored(['bright_magenta'],sprintf(shift, @_)) : sprintf(shift, @_) }
Subroutine magenta: not called CC=1 SCAR=6.9
Pod documented
279  
280 4 1;
« MToken/Server/Alpha.pm ↑ Summary MToken/Const.pm »