2 package BackupPC::SearchLib;
5 use BackupPC::CGI::Lib qw(:all);
6 use BackupPC::Attrib qw(:all);
13 my $dbh = DBI->connect( "dbi:SQLite:dbname=${TopDir}/$Conf{SearchDB}",
14 "", "", { RaiseError => 1, AutoCommit => 1 } );
17 " SELECT shares.ID AS ID, shares.share AS name FROM shares;");
19 push (@ret, { 'ID' => '', 'name' => '-'});
20 while ( $tmp = $st->fetchrow_hashref() ) {
21 push( @ret, { 'ID' => $tmp->{'ID'}, 'name' => $tmp->{'name'} } );
32 my ($name,$suffix) = @_;
34 my $yyyy = $param->{ $name . '_year_' . $suffix} || return;
35 my $mm .= $param->{ $name . '_month_' . $suffix} ||
36 ( $suffix eq 'from' ? 1 : 12);
37 my $dd .= $param->{ $name . '_day_' . $suffix} ||
38 ( $suffix eq 'from' ? 1 : 31);
39 return sprintf("%04d-%02d-%02d", $yyyy, $mm, $dd);
42 my $backup_from = mk_iso_date('search_backup', 'from');
43 push @conditions, qq{ date(backups.date, 'unixepoch','localtime') >= '$backup_from' } if ($backup_from);
44 my $backup_to = mk_iso_date('search_backup', 'to');
45 push @conditions, qq{ date(backups.date, 'unixepoch','localtime') <= '$backup_to' } if ($backup_to);
47 my $files_from = mk_iso_date('search', 'from');
48 push @conditions, qq{ date(files.date, 'unixepoch','localtime') >= '$files_from' } if ($files_from);
49 my $files_to = mk_iso_date('search', 'to');
50 push @conditions, qq{ date(files.date, 'unixepoch','localtime') <= '$files_to' } if ($files_to);
52 print STDERR "backup: $backup_from - $backup_to files: $files_from - $files_to cond:",join(" | ",@conditions);
54 push( @conditions, ' backups.hostID = ' . $param->{'search_host'} ) if ($param->{'search_host'});
56 push (@conditions, " files.name LIKE '".$param->{'search_filename'}."%'") if ($param->{'search_filename'});
59 join(" and ", @conditions),
60 $files_from, $files_to,
61 $backup_from, $backup_to
68 my ($where, $offset) = @_;
71 my $dbh = DBI->connect( "dbi:SQLite:dbname=${TopDir}/$Conf{SearchDB}",
72 "", "", { RaiseError => 1, AutoCommit => 1 } );
75 SELECT files.id AS fid,
78 shares.share AS sharename,
79 files.backupNum AS backupNum,
80 files.name AS filename,
81 files.path AS filepath,
82 shares.share||files.fullpath AS networkPath,
83 date(files.date, 'unixepoch', 'localtime') AS date,
84 files.type AS filetype,
88 INNER JOIN shares ON files.shareID=shares.ID
89 INNER JOIN hosts ON hosts.ID = shares.hostID
90 INNER JOIN backups ON backups.num = files.backupNum and backups.hostID = hosts.ID
91 LEFT JOIN dvds ON dvds.ID = files.dvdid
94 if (defined($where) && $where ne "")
96 $sql .= " WHERE ". $where;
108 my $st = $dbh->prepare(
111 if (!defined($offset) && $offset ne "")
113 $st->bind_param(1, $offset);
117 $st->bind_param(1,0);
124 while ($tmp = $st->fetchrow_hashref())
127 'hname' => $tmp->{'hname'},
128 'sname' => $tmp->{'sname'},
129 'sharename' => $tmp->{'sharename'},
130 'backupno' => $tmp->{'backupNum'},
131 'fname' => $tmp->{'filename'},
132 'fpath' => $tmp->{'filepath'},
133 'networkpath' => $tmp->{'networkPath'},
134 'date' => $tmp->{'date'},
135 'type' => $tmp->{'filetype'},
136 'size' => $tmp->{'size'},
137 'id' => $tmp->{'fid'},
138 'dvd' => $tmp->{'dvd'}
149 sub getBackupsNotBurned()
151 my $dbh = DBI->connect( "dbi:SQLite:dbname=${TopDir}/$Conf{SearchDB}",
152 "", "", { RaiseError => 1, AutoCommit => 1 } );
157 backups.num AS backupno,
158 backups.type AS type,
160 FROM backups, shares, files, hosts
162 backups.num = files.backupNum AND
163 shares.ID = files.shareID AND
164 backups.hostID = shares.hostID AND
165 hosts.ID = backups.hostID AND
168 backups.hostID, backups.num
170 my $st = $dbh -> prepare( $sql );
174 while ( my $tmp = $st -> fetchrow_hashref() )
177 'host' => $tmp->{'host'},
178 'hostid' => $tmp->{'hostID'},
179 'backupno' => $tmp->{'backupno'},
180 'type' => $tmp->{'type'},
181 'date' => $tmp->{'date'}
189 sub displayBackupsGrid()
198 <script language="javascript" type="text/javascript">
201 function checkAll(location)
203 for (var i=0;i<document.forma.elements.length;i++)
205 var e = document.forma.elements[i];
206 if ((e.checked || !e.checked) && e.name != \'all\') {
207 if (eval("document.forma."+location+".checked")) {
218 $retHTML .= q{<form name="forma" method="POST" action="}."$MyURL"."?action=burn\"";
219 $retHTML.= q{<input type="hidden" value="burn" name="action">};
220 $retHTML .= q{<input type="hidden" value="results" name="search_results">};
222 $retHTML .= "<table style=\"fview\">";
226 $retHTML .= "<td class=\"tableheader\"><input type=\"checkbox\" name=\"allFiles\" onClick=\"checkAll('allFiles');\"></td>";
228 $retHTML .= "<td class=\"tableheader\">Host</td> <td class=\"tableheader\">Backup no</td> <td class=\"tableheader\">Type</td> <td class=\"tableheader\">date</td></tr>";
229 my @backups = getBackupsNotBurned();
235 $retHTML .= "<td colspan=7 style=\"tableheader\">";
236 $retHTML .= "<input type=\"submit\" value=\"Burn selected backups on medium\" name=\"submitBurner\">";
241 foreach $backup(@backups)
248 $retHTML .= "<td class=\"fview\"> <input type=\"checkbox\" name=\"fcb"
249 .$backup->{'hostid'}."_".$backup->{'backupno'}
250 ."\" value=\"".$backup->{'hostid'}."_".$backup->{'backupno'}."\"> </td>";
253 $retHTML .= "<td class=\"fviewborder\">" . $backup->{'host'} . "</td>";
254 $retHTML .= "<td class=\"fviewborder\">" . $backup->{'backupno'} . "</td>";
255 $retHTML .= "<td class=\"fviewborder\">" . $backup->{'type'} . "</td>";
256 $retHTML .= "<td class=\"fviewborder\">" . $backup->{'date'} . "<td>";
259 $retHTML .= "</table>";
262 $retHTML .= "</form>";
270 sub displayGrid($$$$) {
271 my ($where, $addForm, $offset, $hilite) = @_;
275 $retHTML .= qq{<form name="forma" method="POST" action="}.$MyURL.qq{?action=search">};
276 $retHTML.= qq{<input type="hidden" value="search" name="action">};
277 $retHTML .= qq{<input type="hidden" value="results" name="search_results">};
280 <table style="fview" width="100%">
282 <td class="tableheader">Host</td>
283 <td class="tableheader">Name</td>
284 <td class="tableheader">Type</td>
285 <td class="tableheader">#</td>
286 <td class="tableheader">Size</td>
287 <td class="tableheader">Date</td>
288 <td class="tableheader">Media</td>
291 my @files = getFiles($where, $offset);
294 sub hilite_html($$) {
295 my ($html, $search) = @_;
296 $html =~ s#($search)#<b>$1</b>#gis;
300 foreach $file (@files) {
301 my $typeStr = BackupPC::Attrib::fileType2Text(undef, $file->{'type'});
306 qq{<img src="$Conf{CgiImageDirURL}/icon-$typeStr.gif" align="center"> } . hilite_html( $file->{'fpath'}, $hilite ),
313 $retHTML .= qq{<td class="fviewborder">$v</td>};
318 $retHTML .= "</table>";
323 $retHTML .= "<INPUT TYPE=\"hidden\" VALUE=\"\" NAME=\"offset\">";
324 for (my $ii = 1; $ii <= $#files; $ii++) {
325 $retHTML .= "<a href = \"#\" onclick=\"document.forma.offset.value=$ii;document.forma.submit();\">$ii</a>";
331 $retHTML .= "</form>" if ($addForm);