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-2009 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 3.2.0, released 31 Jul 2010.
33 # See http://backuppc.sourceforge.net.
35 #========================================================================
37 package BackupPC::CGI::Browse;
40 use Encode qw/decode_utf8/;
41 use BackupPC::CGI::Lib qw(:all);
43 use BackupPC::Attrib qw(:all);
47 my $Privileged = CheckPermission($In{host});
48 my($i, $dirStr, $fileStr, $attr);
52 ErrorExit(eval("qq{$Lang->{Only_privileged_users_can_browse_backup_files}}"));
56 my $share = $In{share};
59 ErrorExit($Lang->{Empty_host_name}) if ( $host eq "" );
61 # Find the requested backup and the previous filled backup
63 my @Backups = $bpc->BackupInfoRead($host);
66 # default to the newest backup
68 if ( !defined($In{num}) && defined(@Backups) && @Backups > 0 ) {
70 $num = $Backups[$i]{num};
73 for ( $i = 0 ; $i < @Backups ; $i++ ) {
74 last if ( $Backups[$i]{num} == $num );
76 if ( $i >= @Backups ) {
77 ErrorExit("Backup number $num for host ${EscHTML($host)} does"
80 my $backupTime = timeStamp2($Backups[$i]{startTime});
81 my $backupAge = sprintf("%.1f", (time - $Backups[$i]{startTime})
83 my $view = BackupPC::View->new($bpc, $host, \@Backups, {nlink => 1});
85 if ( $dir eq "" || $dir eq "." || $dir eq ".." ) {
86 $attr = $view->dirAttrib($num, "", "");
87 if ( keys(%$attr) > 0 ) {
88 $share = (sort(keys(%$attr)))[0];
91 ErrorExit(eval("qq{$Lang->{Directory___EscHTML}}"));
94 $dir = "/$dir" if ( $dir !~ /^\// );
97 if ( $dir =~ m{(^|/)\.\.(/|$)} ) {
98 ErrorExit($Lang->{Nice_try__but_you_can_t_put});
102 # Loop up the directory tree until we hit the top.
106 my($fLast, $fLastum, @DirStr);
108 $attr = $view->dirAttrib($num, $share, $relDir);
109 if ( !defined($attr) ) {
110 $relDir = decode_utf8($relDir);
111 ErrorExit(eval("qq{$Lang->{Can_t_browse_bad_directory_name2}}"));
114 my $fileCnt = 0; # file counter
115 $fLast = $dirStr = "";
118 # Loop over each of the files in this directory
120 foreach my $f ( sort {uc($a) cmp uc($b)} keys(%$attr) ) {
121 my($dirOpen, $gotDir, $imgStr, $img, $path);
122 my $fURI = $f; # URI escaped $f
123 my $shareURI = $share; # URI escaped $share
124 if ( $relDir eq "" ) {
127 ($path = "$relDir/$f") =~ s{//+}{/}g;
129 if ( $shareURI eq "" ) {
134 $path =~ s/([^\w.\/-])/uc sprintf("%%%02X", ord($1))/eg;
135 $fURI =~ s/([^\w.\/-])/uc sprintf("%%%02X", ord($1))/eg;
136 $shareURI =~ s/([^\w.\/-])/uc sprintf("%%%02X", ord($1))/eg;
137 $dirOpen = 1 if ( defined($currDir) && $f eq $currDir );
138 if ( $attr->{$f}{type} == BPC_FTYPE_DIR ) {
140 # Display directory if it exists in current backup.
141 # First find out if there are subdirs
144 my $linkStyle = "fview";
146 $img |= 1 << 5 if ( $attr->{$f}{nlink} > 2 );
148 $linkStyle = "fviewbold";
150 $img |= 1 << 3 if ( $attr->{$f}{nlink} > 2 );
152 my $imgFileName = sprintf("%07b.gif", $img);
153 $imgStr = "<img src=\"$Conf{CgiImageDirURL}/$imgFileName\" align=\"absmiddle\" width=\"9\" height=\"19\" border=\"0\">";
154 if ( "$relDir/$f" eq $dir ) {
155 $tdStyle = "fviewon";
157 $tdStyle = "fviewoff";
160 $dirName =~ s/ / /g;
161 $dirName = decode_utf8($dirName);
162 push(@DirStr, {needTick => 1,
163 tdArgs => " class=\"$tdStyle\"",
165 <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>
170 my($lastTick, $doneLastTick);
171 foreach my $d ( @DirStrPrev ) {
172 $lastTick = $d if ( $d->{needTick} );
174 $doneLastTick = 1 if ( !defined($lastTick) );
175 foreach my $d ( @DirStrPrev ) {
177 if ( $d->{needTick} ) {
180 if ( $d == $lastTick ) {
183 } elsif ( !$doneLastTick ) {
184 $img |= 1 << 3 | 1 << 4;
186 my $imgFileName = sprintf("%07b.gif", $img);
187 $imgStr = "<img src=\"$Conf{CgiImageDirURL}/$imgFileName\" align=\"absmiddle\" width=\"9\" height=\"19\" border=\"0\">";
188 push(@DirStr, {needTick => 0,
189 tdArgs => $d->{tdArgs},
190 link => $imgStr . $d->{link}
195 if ( $relDir eq $dir ) {
197 # This is the selected directory, so display all the files
199 my ($attrStr, $iconStr);
200 if ( defined($a = $attr->{$f}) ) {
201 my $mtimeStr = $bpc->timeStamp($a->{mtime});
203 my $typeStr = BackupPC::Attrib::fileType2Text(undef,
205 my $modeStr = sprintf("0%o", $a->{mode} & 07777);
207 <img src="$Conf{CgiImageDirURL}/icon-$typeStr.png" valign="top">
210 <td align="center" class="fviewborder">$typeStr</td>
211 <td align="center" class="fviewborder">$modeStr</td>
212 <td align="center" class="fviewborder">$a->{backupNum}</td>
213 <td align="right" class="fviewborder">$a->{size}</td>
214 <td align="right" class="fviewborder">$mtimeStr</td>
218 $attrStr .= "<td colspan=\"5\" align=\"center\" class=\"fviewborder\"> </td>\n";
220 (my $fDisp = "${EscHTML($f)}") =~ s/ / /g;
221 $fDisp = decode_utf8($fDisp);
224 <tr><td class="fviewborder">
225 <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>
232 <tr><td class="fviewborder">
233 <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>
242 @DirStrPrev = @DirStr;
243 last if ( $relDir eq "" && $share eq "" );
245 # Prune the last directory off $relDir, or at the very end
246 # do the top-level directory.
248 if ( $relDir eq "" || $relDir eq "/" || $relDir !~ /(.*)\/(.*)/ ) {
258 my $dirDisplay = decode_utf8("$share/$dir");
259 $dirDisplay =~ s{//+}{/}g;
260 $dirDisplay =~ s{/+$}{}g;
261 $dirDisplay = "/" if ( $dirDisplay eq "" );
264 if ( (my @mergeNums = @{$view->mergeNums}) > 1 ) {
266 my $numF = join(", #", @mergeNums);
267 $filledBackup = eval("qq{$Lang->{This_display_is_merged_with_backup}}");
270 foreach my $d ( @DirStrPrev ) {
271 $dirStr .= "<tr><td$d->{tdArgs}>$d->{link}\n";
274 ### hide checkall button if there are no files
275 my ($topCheckAll, $checkAll, $fileHeader);
277 $fileHeader = eval("qq{$Lang->{fileHeader}}");
279 $checkAll = $Lang->{checkAll};
281 # and put a checkall box on top if there are at least 20 files
282 if ( $checkBoxCnt >= 20 ) {
283 $topCheckAll = $checkAll;
284 $topCheckAll =~ s{allFiles}{allFilestop}g;
287 $fileStr = eval("qq{$Lang->{The_directory_is_empty}}");
290 my $shareURI = $share;
291 $pathURI =~ s/([^\w.\/-])/uc sprintf("%%%02x", ord($1))/eg;
292 $shareURI =~ s/([^\w.\/-])/uc sprintf("%%%02x", ord($1))/eg;
293 if ( my @otherDirs = $view->backupList($share, $dir) ) {
295 foreach my $i ( @otherDirs ) {
297 my $showDate = timeStamp2($Backups[$i]{startTime});
298 my $backupNum = $Backups[$i]{num};
299 $selected = " selected" if ( $backupNum == $num );
300 $otherDirs .= "<option value=\"$MyURL?action=browse&host=${EscURI($host)}&num=$backupNum&share=$shareURI&dir=$pathURI\"$selected>#$backupNum - ($showDate)</option>\n";
302 $filledBackup .= eval("qq{$Lang->{Visit_this_directory_in_backup}}");
304 $dir = decode_utf8($dir);
305 $share = decode_utf8($share);
306 my $content = eval("qq{$Lang->{Backup_browse_for__host}}");
307 Header(eval("qq{$Lang->{Browse_backup__num_for__host}}"), $content);