use
Fatal
qw( open close )
;
our
$VERSION
=
'0.03'
;
our
$matches
=
undef
;
sub
init {
my
$self
=
shift
;
undef
$matches
;
return
SUCCESS;
}
sub
add {
my
$self
=
shift
;
my
(
$h_file
,
$func
,
$cert
,
$args
,
$msg
)
=
@_
or
return
SUCCESS;
my
$key
=
$h_file
->{
'path'
};
my
$new_h_file
= {
'name'
=>
$h_file
->{
'name'
},
'size'
=>
$h_file
->{
'size'
},
'mtime'
=>
$h_file
->{
'mtime'
},
};
$matches
->{
$key
} =
$new_h_file
if
( not
exists
$matches
->{
$key
} );
push
@{
$matches
->{
$key
}->{
'matches'
}}, {
'func'
=>
$func
,
'cert'
=>
$cert
,
'args'
=>
$args
,
'msg'
=>
$msg
,
};
return
SUCCESS;
}
sub
get_matches {
my
$var
=
$_
[0] .
"::matches"
;
my
$fmt
=
$_
[1];
no
strict
'refs'
;
if
( not
defined
$fmt
) {
return
$$var
;
}
else
{
my
$func
= __PACKAGE__ .
"::__"
.
$fmt
;
if
(
defined
(
&$func
) ) {
return
&$func
(
$$var
);
}
else
{
croak
"can't find function $func in "
. __PACKAGE__;
}
}
}
sub
__txt {
my
$result
=
shift
;
my
$ret
=
undef
;
if
(
defined
$result
) {
$ret
=
''
;
my
$num
= 1;
foreach
my
$path
(
sort
keys
%$result
) {
my
(
$matches
,
$mtime
,
$name
,
$size
)
= @{
$result
->{
$path
}}{
qw/matches mtime name size/
};
my
$mtime_stamp
=
localtime
(
$mtime
);
$ret
.=
sprintf
(
"%-16s %-50s\n"
,
"Matches \#\($num\):"
,
$path
);
$ret
.=
sprintf
(
"%-16s %-20s %-10s %-20s\n"
,
""
,
"name:$name"
,
"size:$size"
,
"mtime:$mtime_stamp"
);
if
(
defined
$matches
) {
my
$num_of_match
= 1;
foreach
my
$match
(
@$matches
) {
$ret
.=
sprintf
(
"%-20s %-5s %-5s %-20s %-20s\n"
,
""
,
"\<$num_of_match\>\."
,
$match
->{
'cert'
} .
'%'
,
$match
->{
'func'
},
$match
->{
'args'
} );
$ret
.=
" "
x 24 .
"$match->{'msg'}\n"
;
$num_of_match
++;
}
}
$num
++;
}
}
return
$ret
;
}
sub
__html {
my
$result
=
shift
;
my
$ret
=
undef
;
if
(
defined
$result
) {
my
$num
= 1;
$ret
=
''
;
foreach
my
$path
(
sort
keys
%$result
) {
my
(
$matches
,
$mtime
,
$name
,
$size
)
= @{
$result
->{
$path
}}{
qw/matches mtime name size/
};
my
$mtime_stamp
=
localtime
(
$mtime
);
$ret
.=
"<table id='scan_result_table' cellspacing='0'>\n"
;
$ret
.=
"<tr><th width='700px'>(#$num) $path</th><th width='50px'>$size</th><th width='250px'>$mtime_stamp</th></tr>\n"
;
if
(
defined
$matches
) {
my
$num_of_match
= 1;
foreach
my
$match
(
@$matches
) {
$ret
.=
"<tr><table>\n"
.
"<tr><td width='50px'>\<$num_of_match\></td>\n"
.
"<td width='50px'>$match->{'cert'}\%</td>\n"
.
"<td width='200px'>$match->{'func'}</td>\n"
.
"<td width='300px'>$match->{'args'}</td>\n"
.
"<td width='400px'>$match->{'msg'}</td>\n"
.
"</tr></table></tr>\n"
;
$num_of_match
++;
}
}
$num
++;
}
$ret
.=
"</table>\n"
;
my
$css
=
join
(
q()
, <DATA>);
$ret
=
$css
.
"\n"
.
$ret
;
}
return
$ret
;
}
sub
__json {
my
$result
=
shift
;
my
$ret
=
undef
;
$ret
= JSON::to_json(
$result
, {
pretty
=> 1 } )
if
(
defined
$result
);
return
$ret
;
}
1;