1 |
#!/usr/bin/perl |
2 |
package BackupPC::SearchLib; |
3 |
|
4 |
use strict; |
5 |
use BackupPC::CGI::Lib qw(:all); |
6 |
use BackupPC::Attrib qw(:all); |
7 |
use DBI; |
8 |
|
9 |
sub getUnits() { |
10 |
my @ret = (); |
11 |
my $tmp; |
12 |
my $dbh = DBI->connect( "dbi:SQLite:dbname=${TopDir}/$Conf{SearchDB}", |
13 |
"", "", { RaiseError => 1, AutoCommit => 1 } ); |
14 |
my $st = |
15 |
$dbh->prepare( |
16 |
" SELECT shares.ID AS ID, shares.share AS name FROM shares;"); |
17 |
$st->execute(); |
18 |
push (@ret, { 'ID' => '', 'name' => '-'}); |
19 |
while ( $tmp = $st->fetchrow_hashref() ) { |
20 |
push( @ret, { 'ID' => $tmp->{'ID'}, 'name' => $tmp->{'name'} } ); |
21 |
} |
22 |
$dbh->disconnect(); |
23 |
return @ret; |
24 |
} |
25 |
|
26 |
sub getWhere($) { |
27 |
my ($param) = @_; |
28 |
my @conditions; |
29 |
|
30 |
sub mk_iso_date($$) { |
31 |
my ($name,$suffix) = @_; |
32 |
|
33 |
my $yyyy = $param->{ $name . '_year_' . $suffix} || return; |
34 |
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 |
print STDERR "backup: $backup_from - $backup_to files: $files_from - $files_to cond:",join(" | ",@conditions); |
52 |
|
53 |
push( @conditions, ' backups.hostID = ' . $param->{'search_host'} ) if ($param->{'search_host'}); |
54 |
|
55 |
push (@conditions, " upper(files.name) LIKE upper('%".$param->{'search_filename'}."%')") if ($param->{'search_filename'}); |
56 |
|
57 |
return ( |
58 |
join(" and ", @conditions), |
59 |
$files_from, $files_to, |
60 |
$backup_from, $backup_to |
61 |
); |
62 |
} |
63 |
|
64 |
|
65 |
sub getFiles($$) |
66 |
{ |
67 |
my ($where, $offset) = @_; |
68 |
|
69 |
|
70 |
my $dbh = DBI->connect( "dbi:SQLite:dbname=${TopDir}/$Conf{SearchDB}", |
71 |
"", "", { RaiseError => 1, AutoCommit => 1 } ); |
72 |
my $sql = |
73 |
q{ |
74 |
SELECT files.id AS fid, |
75 |
hosts.name AS hname, |
76 |
shares.name AS sname, |
77 |
shares.share AS sharename, |
78 |
files.backupNum AS backupNum, |
79 |
files.name AS filename, |
80 |
files.path AS filepath, |
81 |
shares.share||files.fullpath AS networkPath, |
82 |
date(files.date, 'unixepoch', 'localtime') AS date, |
83 |
files.type AS filetype, |
84 |
files.size AS size, |
85 |
dvds.name AS dvd |
86 |
FROM files |
87 |
INNER JOIN shares ON files.shareID=shares.ID |
88 |
INNER JOIN hosts ON hosts.ID = shares.hostID |
89 |
INNER JOIN backups ON backups.num = files.backupNum and backups.hostID = hosts.ID |
90 |
LEFT JOIN dvds ON dvds.ID = files.dvdid |
91 |
}; |
92 |
|
93 |
if (defined($where) && $where ne "") |
94 |
{ |
95 |
$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( |
108 |
$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; |
119 |
|
120 |
my @ret = (); |
121 |
my $tmp; |
122 |
|
123 |
while ($tmp = $st->fetchrow_hashref()) |
124 |
{ |
125 |
push(@ret, { |
126 |
'hname' => $tmp->{'hname'}, |
127 |
'sname' => $tmp->{'sname'}, |
128 |
'sharename' => $tmp->{'sharename'}, |
129 |
'backupno' => $tmp->{'backupNum'}, |
130 |
'fname' => $tmp->{'filename'}, |
131 |
'fpath' => $tmp->{'filepath'}, |
132 |
'networkpath' => $tmp->{'networkPath'}, |
133 |
'date' => $tmp->{'date'}, |
134 |
'type' => $tmp->{'filetype'}, |
135 |
'size' => $tmp->{'size'}, |
136 |
'id' => $tmp->{'fid'}, |
137 |
'dvd' => $tmp->{'dvd'} |
138 |
} |
139 |
); |
140 |
|
141 |
} |
142 |
|
143 |
$st->finish(); |
144 |
$dbh->disconnect(); |
145 |
return @ret; |
146 |
} |
147 |
|
148 |
sub getBackupsNotBurned() |
149 |
{ |
150 |
my $dbh = DBI->connect( "dbi:SQLite:dbname=${TopDir}/$Conf{SearchDB}", |
151 |
"", "", { RaiseError => 1, AutoCommit => 1 } ); |
152 |
my $sql = q{ |
153 |
SELECT |
154 |
hosts.ID AS hostID, |
155 |
hosts.name AS host, |
156 |
backups.num AS backupno, |
157 |
backups.type AS type, |
158 |
backups.date AS date |
159 |
FROM backups, shares, files, hosts |
160 |
WHERE |
161 |
backups.num = files.backupNum AND |
162 |
shares.ID = files.shareID AND |
163 |
backups.hostID = shares.hostID AND |
164 |
hosts.ID = backups.hostID AND |
165 |
files.dvdid IS NULL |
166 |
GROUP BY |
167 |
backups.hostID, backups.num |
168 |
}; |
169 |
my $st = $dbh -> prepare( $sql ); |
170 |
my @ret = (); |
171 |
$st -> execute(); |
172 |
|
173 |
while ( my $tmp = $st -> fetchrow_hashref() ) |
174 |
{ |
175 |
push(@ret, { |
176 |
'host' => $tmp->{'host'}, |
177 |
'hostid' => $tmp->{'hostID'}, |
178 |
'backupno' => $tmp->{'backupno'}, |
179 |
'type' => $tmp->{'type'}, |
180 |
'date' => $tmp->{'date'} |
181 |
} |
182 |
); |
183 |
} |
184 |
|
185 |
return @ret; |
186 |
} |
187 |
|
188 |
sub displayBackupsGrid() |
189 |
{ |
190 |
my $retHTML = ""; |
191 |
my $addForm = 1; |
192 |
|
193 |
if ($addForm) |
194 |
{ |
195 |
|
196 |
$retHTML .= <<EOF3; |
197 |
<script language="javascript" type="text/javascript"> |
198 |
<!-- |
199 |
|
200 |
function checkAll(location) |
201 |
{ |
202 |
for (var i=0;i<document.forma.elements.length;i++) |
203 |
{ |
204 |
var e = document.forma.elements[i]; |
205 |
if ((e.checked || !e.checked) && e.name != \'all\') { |
206 |
if (eval("document.forma."+location+".checked")) { |
207 |
e.checked = true; |
208 |
} else { |
209 |
e.checked = false; |
210 |
} |
211 |
} |
212 |
} |
213 |
} |
214 |
//--> |
215 |
</script> |
216 |
EOF3 |
217 |
$retHTML .= q{<form name="forma" method="POST" action="}."$MyURL"."?action=burn\""; |
218 |
$retHTML.= q{<input type="hidden" value="burn" name="action">}; |
219 |
$retHTML .= q{<input type="hidden" value="results" name="search_results">}; |
220 |
} |
221 |
$retHTML .= "<table style=\"fview\">"; |
222 |
$retHTML .= "<tr> "; |
223 |
if ($addForm) |
224 |
{ |
225 |
$retHTML .= "<td class=\"tableheader\"><input type=\"checkbox\" name=\"allFiles\" onClick=\"checkAll('allFiles');\"></td>"; |
226 |
} |
227 |
$retHTML .= "<td class=\"tableheader\">Host</td> <td class=\"tableheader\">Backup no</td> <td class=\"tableheader\">Type</td> <td class=\"tableheader\">date</td></tr>"; |
228 |
my @backups = getBackupsNotBurned(); |
229 |
my $backup; |
230 |
|
231 |
if ($addForm) |
232 |
{ |
233 |
$retHTML .= "<tr>"; |
234 |
$retHTML .= "<td colspan=7 style=\"tableheader\">"; |
235 |
$retHTML .= "<input type=\"submit\" value=\"Burn selected backups on medium\" name=\"submitBurner\">"; |
236 |
$retHTML .= "</td>"; |
237 |
$retHTML .= "</tr>"; |
238 |
|
239 |
} |
240 |
foreach $backup(@backups) |
241 |
{ |
242 |
my $ftype = ""; |
243 |
|
244 |
$retHTML .= "<tr>"; |
245 |
if ($addForm) |
246 |
{ |
247 |
$retHTML .= "<td class=\"fview\"> <input type=\"checkbox\" name=\"fcb" |
248 |
.$backup->{'hostid'}."_".$backup->{'backupno'} |
249 |
."\" value=\"".$backup->{'hostid'}."_".$backup->{'backupno'}."\"> </td>"; |
250 |
} |
251 |
|
252 |
$retHTML .= "<td class=\"fviewborder\">" . $backup->{'host'} . "</td>"; |
253 |
$retHTML .= "<td class=\"fviewborder\">" . $backup->{'backupno'} . "</td>"; |
254 |
$retHTML .= "<td class=\"fviewborder\">" . $backup->{'type'} . "</td>"; |
255 |
$retHTML .= "<td class=\"fviewborder\">" . $backup->{'date'} . "<td>"; |
256 |
$retHTML .= "</tr>"; |
257 |
} |
258 |
$retHTML .= "</table>"; |
259 |
if ($addForm) |
260 |
{ |
261 |
$retHTML .= "</form>"; |
262 |
} |
263 |
|
264 |
return $retHTML; |
265 |
|
266 |
|
267 |
} |
268 |
|
269 |
sub displayGrid($$$$) { |
270 |
my ($where, $addForm, $offset, $hilite) = @_; |
271 |
my $retHTML = ""; |
272 |
|
273 |
if ($addForm) { |
274 |
$retHTML .= qq{<form name="forma" method="POST" action="}.$MyURL.qq{?action=search">}; |
275 |
$retHTML.= qq{<input type="hidden" value="search" name="action">}; |
276 |
$retHTML .= qq{<input type="hidden" value="results" name="search_results">}; |
277 |
} |
278 |
$retHTML .= qq{ |
279 |
<table style="fview" width="100%"> |
280 |
<tr> |
281 |
<td class="tableheader">Share</td> |
282 |
<td class="tableheader">Name</td> |
283 |
<td class="tableheader">Type</td> |
284 |
<td class="tableheader">#</td> |
285 |
<td class="tableheader">Size</td> |
286 |
<td class="tableheader">Date</td> |
287 |
<td class="tableheader">Media</td> |
288 |
</tr> |
289 |
}; |
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 |
} |
298 |
|
299 |
sub restore_link($$$$$$) { |
300 |
my $type = shift; |
301 |
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 |
} |
336 |
|
337 |
$retHTML .= "</form>" if ($addForm); |
338 |
|
339 |
return $retHTML; |
340 |
} |
341 |
|
342 |
1; |