1 #============================================================= -*-perl-*-
3 # BackupPC::CGI::Browse package
7 # This module implements the Browse action for the CGI interface.
10 # Craig Barratt <cbarratt@users.sourceforge.net>
13 # Copyright (C) 2003 Craig Barratt
15 # This program is free software; you can redistribute it and/or modify
16 # it under the terms of the GNU General Public License as published by
17 # the Free Software Foundation; either version 2 of the License, or
18 # (at your option) any later version.
20 # This program is distributed in the hope that it will be useful,
21 # but WITHOUT ANY WARRANTY; without even the implied warranty of
22 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 # GNU General Public License for more details.
25 # You should have received a copy of the GNU General Public License
26 # along with this program; if not, write to the Free Software
27 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
29 #========================================================================
31 # Version 2.1.0_CVS, released 3 Jul 2003.
33 # See http://backuppc.sourceforge.net.
35 #========================================================================
37 package BackupPC::CGI::Browse;
40 use BackupPC::CGI::Lib qw(:all);
42 use BackupPC::Attrib qw(:all);
46 my $Privileged = CheckPermission($In{host});
47 my($i, $dirStr, $fileStr, $attr);
51 ErrorExit(eval("qq{$Lang->{Only_privileged_users_can_browse_backup_files}}"));
55 my $share = $In{share};
58 ErrorExit($Lang->{Empty_host_name}) if ( $host eq "" );
60 # Find the requested backup and the previous filled backup
62 my @Backups = $bpc->BackupInfoRead($host);
65 # default to the newest backup
67 if ( !defined($In{num}) && defined(@Backups) ) {
69 $num = $Backups[$i]{num};
72 for ( $i = 0 ; $i < @Backups ; $i++ ) {
73 last if ( $Backups[$i]{num} == $num );
75 if ( $i >= @Backups ) {
76 ErrorExit("Backup number $num for host ${EscHTML($host)} does"
79 my $backupTime = timeStamp2($Backups[$i]{startTime});
80 my $backupAge = sprintf("%.1f", (time - $Backups[$i]{startTime})
82 my $view = BackupPC::View->new($bpc, $host, \@Backups);
84 if ( $dir eq "" || $dir eq "." || $dir eq ".." ) {
85 $attr = $view->dirAttrib($num, "", "");
86 if ( keys(%$attr) > 0 ) {
87 $share = (sort(keys(%$attr)))[0];
90 ErrorExit(eval("qq{$Lang->{Directory___EscHTML}}"));
93 $dir = "/$dir" if ( $dir !~ /^\// );
96 if ( $dir =~ m{(^|/)\.\.(/|$)} ) {
97 ErrorExit($Lang->{Nice_try__but_you_can_t_put});
101 # Loop up the directory tree until we hit the top.
105 my($fLast, $fLastum, @DirStr);
107 $attr = $view->dirAttrib($num, $share, $relDir);
108 if ( !defined($attr) ) {
109 ErrorExit(eval("qq{$Lang->{Can_t_browse_bad_directory_name2}}"));
112 my $fileCnt = 0; # file counter
113 $fLast = $dirStr = "";
116 # Loop over each of the files in this directory
118 foreach my $f ( sort {uc($a) cmp uc($b)} keys(%$attr) ) {
119 my($dirOpen, $gotDir, $imgStr, $img, $path);
120 my $fURI = $f; # URI escaped $f
121 my $shareURI = $share; # URI escaped $share
122 if ( $relDir eq "" ) {
125 ($path = "$relDir/$f") =~ s{//+}{/}g;
127 if ( $shareURI eq "" ) {
132 $path =~ s/([^\w.\/-])/uc sprintf("%%%02X", ord($1))/eg;
133 $fURI =~ s/([^\w.\/-])/uc sprintf("%%%02X", ord($1))/eg;
134 $shareURI =~ s/([^\w.\/-])/uc sprintf("%%%02X", ord($1))/eg;
135 $dirOpen = 1 if ( defined($currDir) && $f eq $currDir );
136 if ( $attr->{$f}{type} == BPC_FTYPE_DIR ) {
138 # Display directory if it exists in current backup.
139 # First find out if there are subdirs
142 my $linkStyle = "fview";
144 $img |= 1 << 5 if ( $attr->{$f}{nlink} > 2 );
146 $linkStyle = "fviewbold";
148 $img |= 1 << 3 if ( $attr->{$f}{nlink} > 2 );
150 my $imgFileName = sprintf("%07b.gif", $img);
151 $imgStr = "<img src=\"$Conf{CgiImageDirURL}/$imgFileName\" align=\"absmiddle\" width=\"9\" height=\"19\" border=\"0\">";
152 if ( "$relDir/$f" eq $dir ) {
153 $tdStyle = "fviewon";
155 $tdStyle = "fviewoff";
158 $dirName =~ s/ / /g;
159 push(@DirStr, {needTick => 1,
160 tdArgs => " class=\"$tdStyle\"",
162 <a href="$MyURL?action=browse&host=${EscURI($host)}&num=$num&share=$shareURI&dir=$path">$imgStr</a><a href="$MyURL?action=browse&host=${EscURI($host)}&num=$num&share=$shareURI&dir=$path" class="$linkStyle"> $dirName</a></td></tr>
167 my($lastTick, $doneLastTick);
168 foreach my $d ( @DirStrPrev ) {
169 $lastTick = $d if ( $d->{needTick} );
171 $doneLastTick = 1 if ( !defined($lastTick) );
172 foreach my $d ( @DirStrPrev ) {
174 if ( $d->{needTick} ) {
177 if ( $d == $lastTick ) {
180 } elsif ( !$doneLastTick ) {
181 $img |= 1 << 3 | 1 << 4;
183 my $imgFileName = sprintf("%07b.gif", $img);
184 $imgStr = "<img src=\"$Conf{CgiImageDirURL}/$imgFileName\" align=\"absmiddle\" width=\"9\" height=\"19\" border=\"0\">";
185 push(@DirStr, {needTick => 0,
186 tdArgs => $d->{tdArgs},
187 link => $imgStr . $d->{link}
192 if ( $relDir eq $dir ) {
194 # This is the selected directory, so display all the files
196 my ($attrStr, $iconStr);
197 if ( defined($a = $attr->{$f}) ) {
198 my $mtimeStr = $bpc->timeStamp($a->{mtime});
200 my $typeStr = BackupPC::Attrib::fileType2Text(undef,
202 my $modeStr = sprintf("0%o", $a->{mode} & 07777);
204 <img src="$Conf{CgiImageDirURL}/icon-$typeStr.gif" align="center">
207 <td align="center" class="fviewborder">$typeStr</td>
208 <td align="center" class="fviewborder">$modeStr</td>
209 <td align="center" class="fviewborder">$a->{backupNum}</td>
210 <td align="right" class="fviewborder">$a->{size}</td>
211 <td align="right" class="fviewborder">$mtimeStr</td>
215 $attrStr .= "<td colspan=\"5\" align=\"center\" class=\"fviewborder\"> </td>\n";
217 (my $fDisp = "${EscHTML($f)}") =~ s/ / /g;
220 <tr><td class="fviewborder">
221 <input type="checkbox" name="fcb$checkBoxCnt" value="$path"> $iconStr <a href="$MyURL?action=browse&host=${EscURI($host)}&num=$num&share=$shareURI&dir=$path">$fDisp</a>
228 <tr><td class="fviewborder">
229 <input type="checkbox" name="fcb$checkBoxCnt" value="$path"> $iconStr <a href="$MyURL?action=RestoreFile&host=${EscURI($host)}&num=$num&share=$shareURI&dir=$path">$fDisp</a>
238 @DirStrPrev = @DirStr;
239 last if ( $relDir eq "" && $share eq "" );
241 # Prune the last directory off $relDir, or at the very end
242 # do the top-level directory.
244 if ( $relDir eq "" || $relDir eq "/" || $relDir !~ /(.*)\/(.*)/ ) {
254 my $dirDisplay = "$share/$dir";
255 $dirDisplay =~ s{//+}{/}g;
256 $dirDisplay =~ s{/+$}{}g;
257 $dirDisplay = "/" if ( $dirDisplay eq "" );
260 if ( (my @mergeNums = @{$view->mergeNums}) > 1 ) {
262 my $numF = join(", #", @mergeNums);
263 $filledBackup = eval("qq{$Lang->{This_display_is_merged_with_backup}}");
266 foreach my $d ( @DirStrPrev ) {
267 $dirStr .= "<tr><td$d->{tdArgs}>$d->{link}\n";
270 ### hide checkall button if there are no files
271 my ($topCheckAll, $checkAll, $fileHeader);
273 $fileHeader = eval("qq{$Lang->{fileHeader}}");
275 $checkAll = $Lang->{checkAll};
277 # and put a checkall box on top if there are at least 20 files
278 if ( $checkBoxCnt >= 20 ) {
279 $topCheckAll = $checkAll;
280 $topCheckAll =~ s{allFiles}{allFilestop}g;
283 $fileStr = eval("qq{$Lang->{The_directory_is_empty}}");
286 my $shareURI = $share;
287 $pathURI =~ s/([^\w.\/-])/uc sprintf("%%%02x", ord($1))/eg;
288 $shareURI =~ s/([^\w.\/-])/uc sprintf("%%%02x", ord($1))/eg;
289 if ( my @otherDirs = $view->backupList($share, $dir) ) {
291 foreach my $i ( @otherDirs ) {
293 my $showDate = timeStamp2($Backups[$i]{startTime});
294 my $backupNum = $Backups[$i]{num};
295 $selected = " selected" if ( $backupNum == $num );
296 $otherDirs .= "<option value=\"$MyURL?action=browse&host=${EscURI($host)}&num=$backupNum&share=$shareURI&dir=$pathURI\"$selected>#$backupNum - ($showDate)</option>\n";
298 $filledBackup .= eval("qq{$Lang->{Visit_this_directory_in_backup}}");
300 my $content = eval("qq{$Lang->{Backup_browse_for__host}}");
301 Header(eval("qq{$Lang->{Browse_backup__num_for__host}}"), $content);