line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package MToken::Util; # $Id: Util.pm 92 2021-10-05 16:43:59Z minus $ |
2
|
4
|
|
|
4
|
|
78903
|
use strict; |
|
4
|
|
|
|
|
20
|
|
|
4
|
|
|
|
|
136
|
|
3
|
4
|
|
|
4
|
|
713
|
use utf8; |
|
4
|
|
|
|
|
24
|
|
|
4
|
|
|
|
|
29
|
|
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 |
26
|
|
|
|
|
|
|
|
27
|
|
|
|
|
|
|
my $servername = cleanServerName( "my.server.com" ); |
28
|
|
|
|
|
|
|
|
29
|
|
|
|
|
|
|
Clening the specified ServerName value |
30
|
|
|
|
|
|
|
|
31
|
|
|
|
|
|
|
=item B |
32
|
|
|
|
|
|
|
|
33
|
|
|
|
|
|
|
my $filename = cleanServerName( "mtoken.12345678" ); |
34
|
|
|
|
|
|
|
|
35
|
|
|
|
|
|
|
Clening the specified FileName value |
36
|
|
|
|
|
|
|
|
37
|
|
|
|
|
|
|
=item B |
38
|
|
|
|
|
|
|
|
39
|
|
|
|
|
|
|
print explain( $object ); |
40
|
|
|
|
|
|
|
|
41
|
|
|
|
|
|
|
Returns Data::Dumper dump |
42
|
|
|
|
|
|
|
|
43
|
|
|
|
|
|
|
=item B |
44
|
|
|
|
|
|
|
|
45
|
|
|
|
|
|
|
my $md5 = md5sum( $file ); |
46
|
|
|
|
|
|
|
|
47
|
|
|
|
|
|
|
See L |
48
|
|
|
|
|
|
|
|
49
|
|
|
|
|
|
|
=item B |
50
|
|
|
|
|
|
|
|
51
|
|
|
|
|
|
|
my $sha1 = sha1sum( $file ); |
52
|
|
|
|
|
|
|
|
53
|
|
|
|
|
|
|
See L |
54
|
|
|
|
|
|
|
|
55
|
|
|
|
|
|
|
=item B |
56
|
|
|
|
|
|
|
|
57
|
|
|
|
|
|
|
my $fsize = filesize( $file ); |
58
|
|
|
|
|
|
|
|
59
|
|
|
|
|
|
|
Returns file size |
60
|
|
|
|
|
|
|
|
61
|
|
|
|
|
|
|
=item B |
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, B, B, B, B, B |
68
|
|
|
|
|
|
|
|
69
|
|
|
|
|
|
|
print cyan("Format %s", "text"); |
70
|
|
|
|
|
|
|
|
71
|
|
|
|
|
|
|
Returns colored string |
72
|
|
|
|
|
|
|
|
73
|
|
|
|
|
|
|
=item B, B, B, B |
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 |
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 |
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 |
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 file |
109
|
|
|
|
|
|
|
|
110
|
|
|
|
|
|
|
=head1 TO DO |
111
|
|
|
|
|
|
|
|
112
|
|
|
|
|
|
|
See C file |
113
|
|
|
|
|
|
|
|
114
|
|
|
|
|
|
|
=head1 BUGS |
115
|
|
|
|
|
|
|
|
116
|
|
|
|
|
|
|
* none noted |
117
|
|
|
|
|
|
|
|
118
|
|
|
|
|
|
|
=head1 SEE ALSO |
119
|
|
|
|
|
|
|
|
120
|
|
|
|
|
|
|
L, L |
121
|
|
|
|
|
|
|
|
122
|
|
|
|
|
|
|
=head1 AUTHOR |
123
|
|
|
|
|
|
|
|
124
|
|
|
|
|
|
|
Serż Minus (Sergey Lepenkov) L Eabalama@cpan.orgE |
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 file and L |
136
|
|
|
|
|
|
|
|
137
|
|
|
|
|
|
|
=cut |
138
|
|
|
|
|
|
|
|
139
|
4
|
|
|
4
|
|
214
|
use vars qw/ $VERSION @EXPORT_OK @EXPORT /; |
|
4
|
|
|
|
|
9
|
|
|
4
|
|
|
|
|
309
|
|
140
|
|
|
|
|
|
|
$VERSION = "1.03"; |
141
|
|
|
|
|
|
|
|
142
|
4
|
|
|
4
|
|
29
|
use Carp; |
|
4
|
|
|
|
|
9
|
|
|
4
|
|
|
|
|
282
|
|
143
|
4
|
|
|
4
|
|
672
|
use CTK::Util qw/bload bsave/; |
|
4
|
|
|
|
|
186442
|
|
|
4
|
|
|
|
|
342
|
|
144
|
4
|
|
|
4
|
|
2070
|
use CTK::Crypt::TCD04; |
|
4
|
|
|
|
|
3920
|
|
|
4
|
|
|
|
|
243
|
|
145
|
4
|
|
|
4
|
|
1834
|
use URI; |
|
4
|
|
|
|
|
14986
|
|
|
4
|
|
|
|
|
143
|
|
146
|
4
|
|
|
4
|
|
28
|
use URI::Escape qw/uri_unescape/; |
|
4
|
|
|
|
|
11
|
|
|
4
|
|
|
|
|
241
|
|
147
|
4
|
|
|
4
|
|
23
|
use Digest::MD5; |
|
4
|
|
|
|
|
10
|
|
|
4
|
|
|
|
|
124
|
|
148
|
4
|
|
|
4
|
|
1277
|
use Digest::SHA; |
|
4
|
|
|
|
|
5906
|
|
|
4
|
|
|
|
|
217
|
|
149
|
4
|
|
|
4
|
|
1497
|
use Data::Dumper; #$Data::Dumper::Deparse = 1; |
|
4
|
|
|
|
|
13753
|
|
|
4
|
|
|
|
|
312
|
|
150
|
4
|
|
|
4
|
|
2249
|
use Term::ANSIColor qw/colored/; |
|
4
|
|
|
|
|
27966
|
|
|
4
|
|
|
|
|
3090
|
|
151
|
|
|
|
|
|
|
|
152
|
4
|
|
|
4
|
|
1198
|
use MToken::Const qw/IS_TTY/; |
|
4
|
|
|
|
|
19
|
|
|
4
|
|
|
|
|
507
|
|
153
|
|
|
|
|
|
|
|
154
|
4
|
|
|
4
|
|
31
|
use base qw/Exporter/; |
|
4
|
|
|
|
|
23
|
|
|
4
|
|
|
|
|
6113
|
|
155
|
|
|
|
|
|
|
@EXPORT = qw( |
156
|
|
|
|
|
|
|
yep nope skip wow |
157
|
|
|
|
|
|
|
blue green red yellow cyan magenta |
158
|
|
|
|
|
|
|
); |
159
|
|
|
|
|
|
|
@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
|
|
|
|
|
|
|
sub cleanServerName { |
168
|
0
|
|
0
|
0
|
1
|
0
|
my $sn = shift // 'localhost'; |
169
|
0
|
|
|
|
|
0
|
$sn =~ s/[^a-z0-9_\-.]//ig; |
170
|
0
|
|
|
|
|
0
|
return $sn; |
171
|
|
|
|
|
|
|
} |
172
|
|
|
|
|
|
|
sub cleanFileName { |
173
|
0
|
|
0
|
0
|
1
|
0
|
my $f = shift // ''; |
174
|
0
|
|
|
|
|
0
|
$f =~ s/[^a-z0-9_\-.]//ig; |
175
|
0
|
|
|
|
|
0
|
return $f; |
176
|
|
|
|
|
|
|
} |
177
|
|
|
|
|
|
|
sub sha1sum { |
178
|
4
|
|
|
4
|
1
|
945
|
my $f = shift; |
179
|
4
|
|
|
|
|
36
|
my $sha1 = Digest::SHA->new; |
180
|
4
|
|
|
|
|
74
|
my $sum = ''; |
181
|
4
|
100
|
|
|
|
125
|
return $sum unless -e $f; |
182
|
1
|
50
|
0
|
|
|
46
|
open( my $sha1_fh, '<', $f) or (carp("Can't open '$f': $!") && return $sum); |
183
|
1
|
50
|
|
|
|
5
|
if ($sha1_fh) { |
184
|
1
|
|
|
|
|
5
|
binmode($sha1_fh); |
185
|
1
|
|
|
|
|
8
|
$sha1->addfile($sha1_fh); |
186
|
1
|
|
|
|
|
217
|
$sum = $sha1->hexdigest; |
187
|
1
|
|
|
|
|
13
|
close($sha1_fh); |
188
|
|
|
|
|
|
|
} |
189
|
1
|
|
|
|
|
12
|
return $sum; |
190
|
|
|
|
|
|
|
} |
191
|
|
|
|
|
|
|
sub md5sum { |
192
|
0
|
|
|
0
|
1
|
0
|
my $f = shift; |
193
|
0
|
|
|
|
|
0
|
my $md5 = Digest::MD5->new; |
194
|
0
|
|
|
|
|
0
|
my $sum = ''; |
195
|
0
|
0
|
|
|
|
0
|
return $sum unless -e $f; |
196
|
0
|
0
|
0
|
|
|
0
|
open( my $md5_fh, '<', $f) or (carp("Can't open '$f': $!") && return $sum); |
197
|
0
|
0
|
|
|
|
0
|
if ($md5_fh) { |
198
|
0
|
|
|
|
|
0
|
binmode($md5_fh); |
199
|
0
|
|
|
|
|
0
|
$md5->addfile($md5_fh); |
200
|
0
|
|
|
|
|
0
|
$sum = $md5->hexdigest; |
201
|
0
|
|
|
|
|
0
|
close($md5_fh); |
202
|
|
|
|
|
|
|
} |
203
|
0
|
|
|
|
|
0
|
return $sum; |
204
|
|
|
|
|
|
|
} |
205
|
|
|
|
|
|
|
sub filesize { |
206
|
0
|
|
|
0
|
1
|
0
|
my $f = shift; |
207
|
0
|
|
|
|
|
0
|
my $filesize = 0; |
208
|
0
|
0
|
|
|
|
0
|
$filesize = (stat $f)[7] if -e $f; |
209
|
0
|
|
|
|
|
0
|
return $filesize; |
210
|
|
|
|
|
|
|
} |
211
|
|
|
|
|
|
|
sub parse_credentials { |
212
|
0
|
|
0
|
0
|
1
|
0
|
my $url = shift || return (); |
213
|
0
|
0
|
|
|
|
0
|
my $uri = (ref($url) eq 'URI') ? $url : URI->new($url); |
214
|
0
|
|
0
|
|
|
0
|
my $info = $uri->userinfo() // ""; |
215
|
0
|
|
|
|
|
0
|
my $user = $info; |
216
|
0
|
|
|
|
|
0
|
my $pass = $info; |
217
|
0
|
|
|
|
|
0
|
$user =~ s/:.*//; |
218
|
0
|
|
|
|
|
0
|
$pass =~ s/^[^:]*://; |
219
|
0
|
|
0
|
|
|
0
|
return (uri_unescape($user // ''), uri_unescape($pass // '')); |
|
|
|
0
|
|
|
|
|
220
|
|
|
|
|
|
|
} |
221
|
|
|
|
|
|
|
sub hide_pasword { |
222
|
0
|
|
0
|
0
|
1
|
0
|
my $url = shift || return ""; |
223
|
0
|
|
0
|
|
|
0
|
my $full = shift || 0; |
224
|
0
|
|
|
|
|
0
|
my $uri = URI->new($url); |
225
|
0
|
|
|
|
|
0
|
my ($u,$p) = parse_credentials($uri); |
226
|
0
|
0
|
0
|
|
|
0
|
return $url unless defined($p) && length($p); |
227
|
0
|
0
|
|
|
|
0
|
$uri->userinfo($full ? undef : sprintf("%s:*****", $u)); |
228
|
0
|
|
|
|
|
0
|
return $uri->canonical->as_string; |
229
|
|
|
|
|
|
|
} |
230
|
|
|
|
|
|
|
sub tcd_save { |
231
|
1
|
|
|
1
|
1
|
1178
|
my $fn = shift; |
232
|
1
|
|
50
|
|
|
5
|
my $text = shift // ''; |
233
|
1
|
50
|
|
|
|
4
|
carp("No file specified") unless $fn; |
234
|
1
|
50
|
|
|
|
4
|
return unless length $text; |
235
|
1
|
50
|
|
|
|
13
|
bsave($fn, CTK::Crypt::TCD04->new()->encrypt($text)) |
236
|
|
|
|
|
|
|
or carp("Can't save file \"$fn\""); |
237
|
1
|
|
|
|
|
544
|
return 1; |
238
|
|
|
|
|
|
|
} |
239
|
|
|
|
|
|
|
sub tcd_load { |
240
|
1
|
|
|
1
|
1
|
3
|
my $fn = shift; |
241
|
1
|
50
|
|
|
|
5
|
carp("No file specified") unless $fn; |
242
|
1
|
50
|
33
|
|
|
36
|
return unless -f $fn and -r _ and -s _; |
|
|
|
33
|
|
|
|
|
243
|
1
|
|
50
|
|
|
9
|
return CTK::Crypt::TCD04->new()->decrypt(bload($fn) // ''); |
244
|
|
|
|
|
|
|
} |
245
|
|
|
|
|
|
|
sub explain { |
246
|
0
|
|
|
0
|
1
|
|
my $dumper = Data::Dumper->new( [shift] ); |
247
|
0
|
|
|
|
|
|
$dumper->Indent(1)->Terse(1); |
248
|
0
|
0
|
|
|
|
|
$dumper->Sortkeys(1) if $dumper->can("Sortkeys"); |
249
|
0
|
|
|
|
|
|
return $dumper->Dump; |
250
|
|
|
|
|
|
|
} |
251
|
|
|
|
|
|
|
|
252
|
|
|
|
|
|
|
################ |
253
|
|
|
|
|
|
|
# Colored says |
254
|
|
|
|
|
|
|
################ |
255
|
|
|
|
|
|
|
sub yep { |
256
|
0
|
|
|
0
|
1
|
|
print(green(sprintf(shift, @_)), "\n"); |
257
|
0
|
|
|
|
|
|
return 1; |
258
|
|
|
|
|
|
|
} |
259
|
|
|
|
|
|
|
sub nope { |
260
|
0
|
|
|
0
|
1
|
|
print(red(sprintf(shift, @_)), "\n"); |
261
|
0
|
|
|
|
|
|
return 0; |
262
|
|
|
|
|
|
|
} |
263
|
|
|
|
|
|
|
sub skip { |
264
|
0
|
|
|
0
|
1
|
|
print(yellow(sprintf(shift, @_)), "\n"); |
265
|
0
|
|
|
|
|
|
return 1; |
266
|
|
|
|
|
|
|
} |
267
|
|
|
|
|
|
|
sub wow { |
268
|
0
|
|
|
0
|
1
|
|
print(blue(sprintf(shift, @_)), "\n"); |
269
|
0
|
|
|
|
|
|
return 1; |
270
|
|
|
|
|
|
|
} |
271
|
|
|
|
|
|
|
|
272
|
|
|
|
|
|
|
# Colored helper functions |
273
|
0
|
|
|
0
|
1
|
|
sub green { IS_TTY ? colored(['bright_green'], sprintf(shift, @_)) : sprintf(shift, @_) } |
274
|
0
|
|
|
0
|
1
|
|
sub red { IS_TTY ? colored(['bright_red'], sprintf(shift, @_)) : sprintf(shift, @_) } |
275
|
0
|
|
|
0
|
1
|
|
sub yellow { IS_TTY ? colored(['bright_yellow'], sprintf(shift, @_)) : sprintf(shift, @_) } |
276
|
0
|
|
|
0
|
1
|
|
sub cyan { IS_TTY ? colored(['bright_cyan'], sprintf(shift, @_)) : sprintf(shift, @_) } |
277
|
0
|
|
|
0
|
1
|
|
sub blue { IS_TTY ? colored(['bright_blue'], sprintf(shift, @_)) : sprintf(shift, @_) } |
278
|
0
|
|
|
0
|
1
|
|
sub magenta {IS_TTY ? colored(['bright_magenta'],sprintf(shift, @_)) : sprintf(shift, @_) } |
279
|
|
|
|
|
|
|
|
280
|
|
|
|
|
|
|
1; |