4 |
use strict; |
use strict; |
5 |
use BackupPC::CGI::Lib qw(:all); |
use BackupPC::CGI::Lib qw(:all); |
6 |
use BackupPC::Attrib qw(:all); |
use BackupPC::Attrib qw(:all); |
|
use Data::Dumper; |
|
7 |
use DBI; |
use DBI; |
8 |
|
|
9 |
sub getUnits() { |
sub getUnits() { |
24 |
} |
} |
25 |
|
|
26 |
sub getWhere($) { |
sub getWhere($) { |
27 |
my ($param) = @_; |
my ($param) = @_; |
28 |
my $retSQL = ""; |
my @conditions; |
|
my @conditions = (); |
|
|
my $cond; |
|
29 |
|
|
30 |
|
sub mk_iso_date($$) { |
31 |
|
my ($name,$suffix) = @_; |
|
|
|
|
if ( defined( $param->{'search_backup_day_from'} ) && $param->{'search_backup_day_from'} ne "") { |
|
|
push( @conditions, |
|
|
' strftime("%d", datetime(backups.date, "unixepoch","localtime")) >= "' |
|
|
. $param->{'search_backup_day_from'} ."\""); |
|
|
} |
|
|
if ( defined( $param->{'search_backup_day_to'} ) && $param->{'search_backup_day_to'} ne "") { |
|
|
push( @conditions, |
|
|
' strftime("%d", datetime(backups.date, "unixepoch","localtime")) <= "' |
|
|
. $param->{'search_backup_day_from'} ."\""); |
|
|
} |
|
|
if ( defined( $param->{'search_backup_month_from'} ) && $param->{'search_backup_month_from'} ne "") { |
|
|
push( @conditions, |
|
|
' strftime("%m", datetime(backups.date, "unixepoch","localtime")) >= "' |
|
|
. $param->{'search_backup_month_from'} ."\""); |
|
|
} |
|
|
if ( defined( $param->{'search_backup_month_to'} ) && $param->{'search_backup_month_to'} ne "") { |
|
|
push( @conditions, |
|
|
' strftime("%m", datetime(backups.date, "unixepoch","localtime")) <= "' |
|
|
. $param->{'search_backup_month_to'} ."\""); |
|
|
} |
|
|
if ( defined( $param->{'search_backup_year_from'} ) && $param->{'search_backup_year_from'} ne "") { |
|
|
push( @conditions, |
|
|
' strftime("%Y", datetime(backups.date, "unixepoch","localtime")) >= "' |
|
|
. $param->{'search_backup_year_from'} ."\""); |
|
|
} |
|
|
if ( defined( $param->{'search_backup_year_to'} ) && $param->{'search_backup_year_to'} ne "") { |
|
|
push( @conditions, |
|
|
' strftime("%Y", datetime(backups.date, "unixepoch","localtime")) <= "' |
|
|
. $param->{'search_backup_year_to'} ."\""); |
|
|
} |
|
|
|
|
|
if ( defined( $param->{'search_day_from'} ) && $param->{'search_day_from'} ne "" ) { |
|
|
push( @conditions, |
|
|
' strftime("%d", datetime(files.date, "unixepoch","localtime")) >= "' |
|
|
. $param->{'search_day_from'} ."\""); |
|
|
} |
|
|
if ( defined( $param->{'search_month_from'} ) && $param->{'search_month_from'} ne "") { |
|
|
push( @conditions, |
|
|
' strftime("%m", datetime(files.date, "unixepoch","localtime")) >= "' |
|
|
. $param->{'search_month_from'} ."\""); |
|
|
} |
|
|
if ( defined( $param->{'search_year_from'} ) && $param->{'search_year_from'} ne "") { |
|
|
push( @conditions, |
|
|
' strftime("%Y", datetime(files.date, "unixepoch","localtime")) >= "' |
|
|
. $param->{'search_year_from'} ."\""); |
|
|
} |
|
|
if ( defined( $param->{'search_day_to'} ) && $param->{'search_day_to'} ne "" ) { |
|
|
push( @conditions, |
|
|
' strftime("%d", datetime(files.date, "unixepoch","localtime")) <= "' |
|
|
. $param->{'search_day_to'} ."\""); |
|
|
} |
|
|
if ( defined( $param->{'search_month_to'} ) && $param->{'search_month_to'} ne "" ) { |
|
|
push( @conditions, |
|
|
' strftime("%m", datetime(files.date, "unixepoch","localtime")) <= "' |
|
|
. $param->{'search_month_to'} ."\"" ); |
|
|
} |
|
|
if ( defined( $param->{'search_year_to'} )&& $param->{'search_year_to'} ne "" ) { |
|
|
push( @conditions, |
|
|
' strftime("%Y", datetime(files.date, "unixepoch","localtime")) <= "' |
|
|
. $param->{'search_year_to'} ."\""); |
|
|
} |
|
32 |
|
|
33 |
if ( defined( $param->{'search_host'} ) && $param->{'search_host'} ne "") { |
my $yyyy = $param->{ $name . '_year_' . $suffix} || return; |
34 |
push( @conditions, ' backups.hostID = ' . $param->{'search_host'} ); |
my $mm .= $param->{ $name . '_month_' . $suffix} || |
35 |
} |
( $suffix eq 'from' ? 1 : 12); |
36 |
|
my $dd .= $param->{ $name . '_day_' . $suffix} || |
37 |
|
( $suffix eq 'from' ? 1 : 31); |
38 |
|
return sprintf("%04d-%02d-%02d", $yyyy, $mm, $dd); |
39 |
|
} |
40 |
|
|
41 |
|
my $backup_from = mk_iso_date('search_backup', 'from'); |
42 |
|
push @conditions, qq{ date(backups.date, 'unixepoch','localtime') >= '$backup_from' } if ($backup_from); |
43 |
|
my $backup_to = mk_iso_date('search_backup', 'to'); |
44 |
|
push @conditions, qq{ date(backups.date, 'unixepoch','localtime') <= '$backup_to' } if ($backup_to); |
45 |
|
|
46 |
|
my $files_from = mk_iso_date('search', 'from'); |
47 |
|
push @conditions, qq{ date(files.date, 'unixepoch','localtime') >= '$files_from' } if ($files_from); |
48 |
|
my $files_to = mk_iso_date('search', 'to'); |
49 |
|
push @conditions, qq{ date(files.date, 'unixepoch','localtime') <= '$files_to' } if ($files_to); |
50 |
|
|
51 |
if ( defined ($param->{'search_filename'}) && $param->{'search_filename'} ne "") { |
print STDERR "backup: $backup_from - $backup_to files: $files_from - $files_to cond:",join(" | ",@conditions); |
|
push (@conditions, " files.name LIKE '".$param->{'search_filename'}."%'"); |
|
|
} |
|
52 |
|
|
53 |
$retSQL = ""; |
push( @conditions, ' backups.hostID = ' . $param->{'search_host'} ) if ($param->{'search_host'}); |
|
foreach $cond(@conditions) |
|
|
{ |
|
|
if ($retSQL ne "") |
|
|
{ |
|
|
$retSQL .= " AND "; |
|
|
} |
|
|
$retSQL .= $cond; |
|
|
} |
|
54 |
|
|
55 |
|
push (@conditions, " upper(files.name) LIKE upper('%".$param->{'search_filename'}."%')") if ($param->{'search_filename'}); |
56 |
return $retSQL; |
|
57 |
|
return ( |
58 |
|
join(" and ", @conditions), |
59 |
|
$files_from, $files_to, |
60 |
|
$backup_from, $backup_to |
61 |
|
); |
62 |
} |
} |
63 |
|
|
64 |
sub getFiles($) |
|
65 |
|
sub getFiles($$) |
66 |
{ |
{ |
67 |
my ($where) = @_; |
my ($where, $offset) = @_; |
68 |
|
|
69 |
|
|
70 |
my $dbh = DBI->connect( "dbi:SQLite:dbname=${TopDir}/$Conf{SearchDB}", |
my $dbh = DBI->connect( "dbi:SQLite:dbname=${TopDir}/$Conf{SearchDB}", |
71 |
"", "", { RaiseError => 1, AutoCommit => 1 } ); |
"", "", { RaiseError => 1, AutoCommit => 1 } ); |
72 |
my $sql = |
my $sql = |
73 |
q{ |
q{ |
74 |
SELECT files.id AS fid, |
SELECT files.id AS fid, |
75 |
hosts.name AS hname, |
hosts.name AS hname, |
76 |
shares.name AS sname, |
shares.name AS sname, |
77 |
shares.share AS sharename, |
shares.share AS sharename, |
78 |
backups.num AS backupNum, |
files.backupNum AS backupNum, |
79 |
files.name AS filename, |
files.name AS filename, |
80 |
files.path AS filepath, |
files.path AS filepath, |
81 |
shares.share||files.fullpath AS networkPath, |
shares.share||files.fullpath AS networkPath, |
82 |
date(files.date, 'unixepoch', 'localtime') AS date, |
date(files.date, 'unixepoch', 'localtime') AS date, |
83 |
files.type AS filetype, |
files.type AS filetype, |
84 |
files.size AS size, |
files.size AS size, |
85 |
dvds.name AS dvd |
dvds.name AS dvd |
86 |
FROM |
FROM files |
87 |
files |
INNER JOIN shares ON files.shareID=shares.ID |
88 |
INNER JOIN shares ON files.shareID=shares.ID |
INNER JOIN hosts ON hosts.ID = shares.hostID |
89 |
INNER JOIN hosts ON hosts.ID = shares.hostID |
INNER JOIN backups ON backups.num = files.backupNum and backups.hostID = hosts.ID |
90 |
INNER JOIN backups ON backups.hostID = hosts.ID |
LEFT JOIN dvds ON dvds.ID = files.dvdid |
|
LEFT JOIN dvds ON dvds.ID = files.dvdid |
|
|
|
|
91 |
}; |
}; |
92 |
|
|
93 |
if (defined($where) && $where ne "") |
if (defined($where) && $where ne "") |
95 |
$sql .= " WHERE ". $where; |
$sql .= " WHERE ". $where; |
96 |
} |
} |
97 |
|
|
98 |
|
$sql .= |
99 |
|
q{ |
100 |
|
ORDER BY files.id |
101 |
|
LIMIT 100 |
102 |
|
OFFSET ? * 100 + 1 |
103 |
|
}; |
104 |
|
|
105 |
|
|
106 |
|
|
107 |
my $st = $dbh->prepare( |
my $st = $dbh->prepare( |
108 |
$sql |
$sql |
109 |
); |
); |
110 |
|
if (!defined($offset) && $offset ne "") |
111 |
|
{ |
112 |
|
$st->bind_param(1, $offset); |
113 |
|
} |
114 |
|
else |
115 |
|
{ |
116 |
|
$st->bind_param(1,0); |
117 |
|
} |
118 |
$st->execute; |
$st->execute; |
119 |
|
|
120 |
my @ret = (); |
my @ret = (); |
266 |
|
|
267 |
} |
} |
268 |
|
|
269 |
sub displayGrid($$) |
sub displayGrid($$$$) { |
270 |
{ |
my ($where, $addForm, $offset, $hilite) = @_; |
271 |
my ($where, $addForm) = @_; |
my $retHTML = ""; |
272 |
my $retHTML = ""; |
|
273 |
|
if ($addForm) { |
274 |
if ($addForm) |
$retHTML .= qq{<form name="forma" method="POST" action="}.$MyURL.qq{?action=search">}; |
275 |
{ |
$retHTML.= qq{<input type="hidden" value="search" name="action">}; |
276 |
$retHTML .= <<EOF3; |
$retHTML .= qq{<input type="hidden" value="results" name="search_results">}; |
277 |
<script language="javascript" type="text/javascript"> |
} |
278 |
<!-- |
$retHTML .= qq{ |
279 |
|
<table style="fview" width="100%"> |
280 |
function checkAll(location) |
<tr> |
281 |
{ |
<td class="tableheader">Share</td> |
282 |
for (var i=0;i<document.forma.elements.length;i++) |
<td class="tableheader">Name</td> |
283 |
{ |
<td class="tableheader">Type</td> |
284 |
var e = document.forma.elements[i]; |
<td class="tableheader">#</td> |
285 |
if ((e.checked || !e.checked) && e.name != \'all\') { |
<td class="tableheader">Size</td> |
286 |
if (eval("document.forma."+location+".checked")) { |
<td class="tableheader">Date</td> |
287 |
e.checked = true; |
<td class="tableheader">Media</td> |
288 |
} else { |
</tr> |
289 |
e.checked = false; |
}; |
290 |
} |
my @files = getFiles($where, $offset); |
291 |
} |
my $file; |
292 |
} |
|
293 |
} |
sub hilite_html($$) { |
294 |
|
my ($html, $search) = @_; |
295 |
|
$html =~ s#($search)#<b>$1</b>#gis; |
296 |
//--> |
return $html; |
297 |
</script> |
} |
298 |
EOF3 |
|
299 |
$retHTML .= q{<form name="forma" method="POST" action="}."$MyURL"."?action=burn\""; |
sub restore_link($$$$$$) { |
300 |
$retHTML.= q{<input type="hidden" value="burn" name="action">}; |
my $type = shift; |
301 |
$retHTML .= q{<input type="hidden" value="results" name="search_results">}; |
my $action = 'RestoreFile'; |
302 |
|
$action = 'browse' if (lc($type) eq 'dir'); |
303 |
|
return sprintf(qq{<a href="?action=%s&host=%s&num=%d&share=%s&dir=%s">%s</a>}, $action, @_); |
304 |
|
} |
305 |
|
|
306 |
|
foreach $file (@files) { |
307 |
|
my $typeStr = BackupPC::Attrib::fileType2Text(undef, $file->{'type'}); |
308 |
|
$retHTML .= "<tr>"; |
309 |
|
|
310 |
|
foreach my $v (( |
311 |
|
$file->{'sharename'}, |
312 |
|
qq{<img src="$Conf{CgiImageDirURL}/icon-$typeStr.gif" align="center"> } . hilite_html( $file->{'fpath'}, $hilite ), |
313 |
|
$typeStr, |
314 |
|
restore_link( $typeStr, $file->{'hname'}, $file->{'backupno'}, $file->{'sname'}, $file->{'fpath'}, $file->{'backupno'} ), |
315 |
|
$file->{'size'}, |
316 |
|
$file->{'date'}, |
317 |
|
$file->{'dvd'} |
318 |
|
)) { |
319 |
|
$retHTML .= qq{<td class="fviewborder">$v</td>}; |
320 |
|
} |
321 |
|
|
322 |
|
$retHTML .= "</tr>"; |
323 |
|
} |
324 |
|
$retHTML .= "</table>"; |
325 |
|
|
326 |
|
# skip pager |
327 |
|
return $retHTML; |
328 |
|
|
329 |
|
$retHTML .= "<INPUT TYPE=\"hidden\" VALUE=\"\" NAME=\"offset\">"; |
330 |
|
for (my $ii = 1; $ii <= $#files; $ii++) { |
331 |
|
$retHTML .= "<a href = \"#\" onclick=\"document.forma.offset.value=$ii;document.forma.submit();\">$ii</a>"; |
332 |
|
if ($ii < $#files) { |
333 |
|
$retHTML .= " | "; |
334 |
|
} |
335 |
} |
} |
|
$retHTML .= "<table style=\"fview\">"; |
|
|
$retHTML .= "<tr> "; |
|
|
if ($addForm) |
|
|
{ |
|
|
$retHTML .= "<td class=\"tableheader\"><input type=\"checkbox\" name=\"allFiles\" onClick=\"checkAll('allFiles');\"></td>"; |
|
|
} |
|
|
$retHTML .= "<td class=\"tableheader\">Host</td> <td class=\"tableheader\">Name</td> <td class=\"tableheader\">Type</td> <td class=\"tableheader\">backup no.</td> <td class=\"tableheader\">size</td> <td class=\"tableheader\">date</td> <td class=\"tableheader\">Media</td></tr>"; |
|
|
my @files = getFiles($where); |
|
|
my $file; |
|
336 |
|
|
337 |
if ($addForm) |
$retHTML .= "</form>" if ($addForm); |
|
{ |
|
|
$retHTML .= "<tr>"; |
|
|
$retHTML .= "<td colspan=7 style=\"tableheader\">"; |
|
|
$retHTML .= "<input type=\"submit\" value=\"Burn selected files on medium\" name=\"submitBurner\">"; |
|
|
$retHTML .= "</td>"; |
|
|
$retHTML .= "</tr>"; |
|
|
|
|
|
} |
|
|
foreach $file(@files) |
|
|
{ |
|
|
my $ftype = ""; |
|
|
|
|
|
if ($file->{'type'} == BPC_FTYPE_DIR) |
|
|
{ |
|
|
$ftype = "dir"; |
|
|
} |
|
|
else |
|
|
{ |
|
|
$ftype = "file"; |
|
|
} |
|
|
$retHTML .= "<tr>"; |
|
|
if ($addForm) |
|
|
{ |
|
|
$retHTML .= "<td class=\"fview\"> <input type=\"checkbox\" name=\"fcb" |
|
|
.$file->{'id'} |
|
|
."\" value=\"".$file->{'id'}."\"> </td>"; |
|
|
} |
|
|
|
|
|
$retHTML .= "<td class=\"fviewborder\">" . $file->{'hname'} ."</td>"; |
|
|
$retHTML .= "<td class=\"fviewborder\">" . $file->{'fname'} . "</td>"; |
|
|
$retHTML .= "<td class=\"fviewborder\">" . $ftype . "</td>"; |
|
|
$retHTML .= "<td class=\"fviewborder\">" . $file->{'backupno'} . "</td>"; |
|
|
$retHTML .= "<td class=\"fviewborder\">" . $file->{'size'} . "</td>"; |
|
|
$retHTML .= "<td class=\"fviewborder\">" . $file->{'date'} . "</td>"; |
|
|
$retHTML .= "<td class=\"fviewborder\">" . $file->{'dvd'} . "</td>"; |
|
|
$retHTML .= "</tr>"; |
|
|
} |
|
|
$retHTML .= "</table>"; |
|
|
if ($addForm) |
|
|
{ |
|
|
$retHTML .= "</form>"; |
|
|
} |
|
338 |
|
|
339 |
return $retHTML; |
return $retHTML; |
340 |
} |
} |
341 |
|
|
342 |
1; |
1; |