2 package BackupPC::SearchLib;
5 use BackupPC::CGI::Lib qw(:all);
6 use BackupPC::Attrib qw(:all);
8 use vars qw(%In $MyURL);
16 my $dbh = DBI->connect( "dbi:SQLite:dbname=${TopDir}/$Conf{SearchDB}",
17 "", "", { RaiseError => 1, AutoCommit => 1 } );
20 " SELECT shares.ID AS ID, shares.share AS name FROM shares;");
22 push (@ret, { 'ID' => '', 'name' => '-'});
23 while ( $tmp = $st->fetchrow_hashref() ) {
24 push( @ret, { 'ID' => $tmp->{'ID'}, 'name' => $tmp->{'name'} } );
35 my ($name,$suffix) = @_;
37 my $yyyy = $param->{ $name . '_year_' . $suffix} || return;
38 my $mm .= $param->{ $name . '_month_' . $suffix} ||
39 ( $suffix eq 'from' ? 1 : 12);
40 my $dd .= $param->{ $name . '_day_' . $suffix} ||
41 ( $suffix eq 'from' ? 1 : 31);
42 return sprintf("%04d-%02d-%02d", $yyyy, $mm, $dd);
45 my $backup_from = mk_iso_date('search_backup', 'from');
46 push @conditions, qq{ date(backups.date, 'unixepoch','localtime') >= '$backup_from' } if ($backup_from);
47 my $backup_to = mk_iso_date('search_backup', 'to');
48 push @conditions, qq{ date(backups.date, 'unixepoch','localtime') <= '$backup_to' } if ($backup_to);
50 my $files_from = mk_iso_date('search', 'from');
51 push @conditions, qq{ date(files.date, 'unixepoch','localtime') >= '$files_from' } if ($files_from);
52 my $files_to = mk_iso_date('search', 'to');
53 push @conditions, qq{ date(files.date, 'unixepoch','localtime') <= '$files_to' } if ($files_to);
55 print STDERR "backup: $backup_from - $backup_to files: $files_from - $files_to cond:",join(" | ",@conditions);
57 push( @conditions, ' backups.hostID = ' . $param->{'search_host'} ) if ($param->{'search_host'});
59 push (@conditions, " upper(files.name) LIKE upper('%".$param->{'search_filename'}."%')") if ($param->{'search_filename'});
62 join(" and ", @conditions),
63 $files_from, $files_to,
64 $backup_from, $backup_to
70 my ($where, $offset) = @_;
72 my $dbh = DBI->connect( "dbi:SQLite:dbname=${TopDir}/$Conf{SearchDB}",
73 "", "", { RaiseError => 1, AutoCommit => 1 } );
79 shares.share AS sharename,
80 files.backupNum AS backupNum,
81 files.name AS filename,
82 files.path AS filepath,
83 shares.share||files.fullpath AS networkPath,
84 date(files.date, 'unixepoch', 'localtime') AS date,
85 files.type AS filetype,
92 INNER JOIN shares ON files.shareID=shares.ID
93 INNER JOIN hosts ON hosts.ID = shares.hostID
94 INNER JOIN backups ON backups.num = files.backupNum and backups.hostID = hosts.ID
95 LEFT JOIN dvds ON dvds.ID = files.dvdid
99 $sql_where = " WHERE ". $where if ($where);
108 $offset = ($offset * $on_page) + 1;
110 my $sth = $dbh->prepare(qq{ select count(files.id) $sql_from $sql_where });
113 my ($results) = $sth->fetchrow_array();
115 $sth = $dbh->prepare(qq{ select $sql_cols $sql_from $sql_where $sql_order });
116 $sth->execute( $offset );
120 while (my $row = $sth->fetchrow_hashref()) {
122 'hname' => $row->{'hname'},
123 'sname' => $row->{'sname'},
124 'sharename' => $row->{'sharename'},
125 'backupno' => $row->{'backupNum'},
126 'fname' => $row->{'filename'},
127 'fpath' => $row->{'filepath'},
128 'networkpath' => $row->{'networkPath'},
129 'date' => $row->{'date'},
130 'type' => $row->{'filetype'},
131 'size' => $row->{'size'},
132 'id' => $row->{'fid'},
133 'dvd' => $row->{'dvd'}
139 return ($results, \@ret);
142 sub getBackupsNotBurned()
144 my $dbh = DBI->connect( "dbi:SQLite:dbname=${TopDir}/$Conf{SearchDB}",
145 "", "", { RaiseError => 1, AutoCommit => 1 } );
150 backups.num AS backupno,
151 backups.type AS type,
153 FROM backups, shares, files, hosts
155 backups.num = files.backupNum AND
156 shares.ID = files.shareID AND
157 backups.hostID = shares.hostID AND
158 hosts.ID = backups.hostID AND
161 backups.hostID, backups.num
163 my $st = $dbh -> prepare( $sql );
167 while ( my $row = $st -> fetchrow_hashref() )
170 'host' => $row->{'host'},
171 'hostid' => $row->{'hostID'},
172 'backupno' => $row->{'backupno'},
173 'type' => $row->{'type'},
174 'date' => $row->{'date'}
182 sub displayBackupsGrid()
190 <script language="javascript" type="text/javascript">
193 function checkAll(location)
195 for (var i=0;i<document.forma.elements.length;i++)
197 var e = document.forma.elements[i];
198 if ((e.checked || !e.checked) && e.name != \'all\') {
199 if (eval("document.forma."+location+".checked")) {
210 $retHTML .= q{<form name="forma" method="GET" action="}."$MyURL"."?action=burn\"";
211 $retHTML.= q{<input type="hidden" value="burn" name="action">};
212 $retHTML .= q{<input type="hidden" value="results" name="search_results">};
214 $retHTML .= qq{<table style="fview"><tr>};
217 $retHTML .= "<td class=\"tableheader\"><input type=\"checkbox\" name=\"allFiles\" onClick=\"checkAll('allFiles');\"></td>";
219 $retHTML .= qq{<td class="tableheader">Host</td><td class="tableheader">Backup no</td><td class="tableheader">Type</td><td class="tableheader">date</td></tr>};
221 my @backups = getBackupsNotBurned();
225 $retHTML .= qq{<tr><td colspan=7 style="tableheader">
226 <input type="submit" value="Burn selected backups on medium" name="submitBurner">
230 foreach $backup(@backups) {
236 $retHTML .= qq{<td class="fview"><input type="checkbox" name="fcb} .
237 $backup->{'hostid'}."_".$backup->{'backupno'} .
238 qq{" value="} . $backup->{'hostid'}."_".$backup->{'backupno'} .
242 $retHTML .= '<td class="fviewborder">' . $backup->{'host'} . '</td>' .
243 '<td class="fviewborder">' . $backup->{'backupno'} . '</td>' .
244 '<td class="fviewborder">' . $backup->{'type'} . '</td>' .
245 '<td class="fviewborder">' . $backup->{'date'} . '<td>' .
249 $retHTML .= "</table>";
252 $retHTML .= "</form>";
258 sub displayGrid($$$$) {
259 my ($where, $addForm, $offset, $hilite) = @_;
263 $retHTML .= qq{<form name="forma" method="GET" action="$MyURL">};
264 $retHTML.= qq{<input type="hidden" value="search" name="action">};
265 $retHTML .= qq{<input type="hidden" value="results" name="search_results">};
268 my ($results, $files) = getFiles($where, $offset);
270 my ($from, $to) = (($offset * $on_page) + 1, ($offset * $on_page) + $on_page);
273 <br/>Found $results files, showing $from - $to
274 <table style="fview" width="100%">
276 <td class="tableheader">Share</td>
277 <td class="tableheader">Name</td>
278 <td class="tableheader">Type</td>
279 <td class="tableheader">#</td>
280 <td class="tableheader">Size</td>
281 <td class="tableheader">Date</td>
282 <td class="tableheader">Media</td>
288 sub hilite_html($$) {
289 my ($html, $search) = @_;
290 $html =~ s#($search)#<b>$1</b>#gis;
294 sub restore_link($$$$$$) {
296 my $action = 'RestoreFile';
297 $action = 'browse' if (lc($type) eq 'dir');
298 return sprintf(qq{<a href="?action=%s&host=%s&num=%d&share=%s&dir=%s">%s</a>}, $action, @_);
301 foreach $file (@{ $files }) {
302 my $typeStr = BackupPC::Attrib::fileType2Text(undef, $file->{'type'});
306 $file->{'sharename'},
307 qq{<img src="$Conf{CgiImageDirURL}/icon-$typeStr.gif" align="center"> } . hilite_html( $file->{'fpath'}, $hilite ),
309 restore_link( $typeStr, $file->{'hname'}, $file->{'backupno'}, $file->{'sname'}, $file->{'fpath'}, $file->{'backupno'} ),
314 $retHTML .= qq{<td class="fviewborder">$v</td>};
319 $retHTML .= "</table>";
321 # all variables which has to be transfered
322 foreach my $n (qw/search_day_from search_month_from search_year_from search_day_to search_month_to search_year_to search_backup_day_from search_backup_month_from search_backup_year_from search_backup_day_to search_backup_month_to search_backup_year_to search_filename offset/) {
323 $retHTML .= qq{<INPUT TYPE="hidden" NAME="$n" VALUE="$In{$n}">\n};
327 my $max_page = int( $results / $on_page );
330 my $link_fmt = '<a href = "#" onclick="document.forma.offset.value=%d;document.forma.submit();">%s</a>';
332 $retHTML .= '<div style="text-align: center;">';
335 $retHTML .= sprintf($link_fmt, $offset - 1, '<<') . ' ';
338 while ($page <= $max_page) {
339 if ($page == $offset) {
340 $retHTML .= $del . '<b>' . ($page + 1) . '</b>';
342 $retHTML .= $del . sprintf($link_fmt, $page, $page + 1);
345 if ($page < $offset - $pager_pages && $page != 0) {
347 $page = $offset - $pager_pages;
349 } elsif ($page > $offset + $pager_pages && $page != $max_page) {
359 if ($offset < $max_page) {
360 $retHTML .= ' ' . sprintf($link_fmt, $offset + 1, '>>');
363 $retHTML .= "</div>";
365 $retHTML .= "</form>" if ($addForm);