* A failed full dump is now saved as a partial (incomplete) dump,
[BackupPC.git] / lib / BackupPC / CGI / Browse.pm
1 #============================================================= -*-perl-*-
2 #
3 # BackupPC::CGI::Browse package
4 #
5 # DESCRIPTION
6 #
7 #   This module implements the Browse action for the CGI interface.
8 #
9 # AUTHOR
10 #   Craig Barratt  <cbarratt@users.sourceforge.net>
11 #
12 # COPYRIGHT
13 #   Copyright (C) 2003  Craig Barratt
14 #
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.
19 #
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.
24 #
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
28 #
29 #========================================================================
30 #
31 # Version 2.1.0_CVS, released 3 Jul 2003.
32 #
33 # See http://backuppc.sourceforge.net.
34 #
35 #========================================================================
36
37 package BackupPC::CGI::Browse;
38
39 use strict;
40 use BackupPC::CGI::Lib qw(:all);
41 use BackupPC::View;
42 use BackupPC::Attrib qw(:all);
43
44 sub action
45 {
46     my $Privileged = CheckPermission($In{host});
47     my($i, $dirStr, $fileStr, $attr);
48     my $checkBoxCnt = 0;
49
50     if ( !$Privileged ) {
51         ErrorExit(eval("qq{$Lang->{Only_privileged_users_can_browse_backup_files}}"));
52     }
53     my $host  = $In{host};
54     my $num   = $In{num};
55     my $share = $In{share};
56     my $dir   = $In{dir};
57
58     ErrorExit($Lang->{Empty_host_name}) if ( $host eq "" );
59     #
60     # Find the requested backup and the previous filled backup
61     #
62     my @Backups = $bpc->BackupInfoRead($host);
63     for ( $i = 0 ; $i < @Backups ; $i++ ) {
64         last if ( $Backups[$i]{num} == $num );
65     }
66     if ( $i >= @Backups ) {
67         ErrorExit("Backup number $num for host ${EscHTML($host)} does"
68                 . " not exist.");
69     }
70     my $backupTime = timeStamp2($Backups[$i]{startTime});
71     my $backupAge = sprintf("%.1f", (time - $Backups[$i]{startTime})
72                                     / (24 * 3600));
73     my $view = BackupPC::View->new($bpc, $host, \@Backups);
74
75     if ( $dir eq "" || $dir eq "." || $dir eq ".." ) {
76         $attr = $view->dirAttrib($num, "", "");
77         if ( keys(%$attr) > 0 ) {
78             $share = (sort(keys(%$attr)))[0];
79             $dir   = '/';
80         } else {
81             ErrorExit(eval("qq{$Lang->{Directory___EscHTML}}"));
82         }
83     }
84     $dir = "/$dir" if ( $dir !~ /^\// );
85     my $relDir  = $dir;
86     my $currDir = undef;
87     if ( $dir =~ m{(^|/)\.\.(/|$)} ) {
88         ErrorExit($Lang->{Nice_try__but_you_can_t_put});
89     }
90
91     #
92     # Loop up the directory tree until we hit the top.
93     #
94     my(@DirStrPrev);
95     while ( 1 ) {
96         my($fLast, $fLastum, @DirStr);
97
98         $attr = $view->dirAttrib($num, $share, $relDir);
99         if ( !defined($attr) ) {
100             ErrorExit(eval("qq{$Lang->{Can_t_browse_bad_directory_name2}}"));
101         }
102
103         my $fileCnt = 0;          # file counter
104         $fLast = $dirStr = "";
105
106         #
107         # Loop over each of the files in this directory
108         #
109         foreach my $f ( sort {uc($a) cmp uc($b)} keys(%$attr) ) {
110             my($dirOpen, $gotDir, $imgStr, $img, $path);
111             my $fURI = $f;                             # URI escaped $f
112             my $shareURI = $share;                     # URI escaped $share
113             if ( $relDir eq "" ) {
114                 $path = "/$f";
115             } else {
116                 ($path = "$relDir/$f") =~ s{//+}{/}g;
117             }
118             if ( $shareURI eq "" ) {
119                 $shareURI = $f;
120                 $path  = "/";
121             }
122             $path =~ s{^/+}{/};
123             $path     =~ s/([^\w.\/-])/uc sprintf("%%%02X", ord($1))/eg;
124             $fURI     =~ s/([^\w.\/-])/uc sprintf("%%%02X", ord($1))/eg;
125             $shareURI =~ s/([^\w.\/-])/uc sprintf("%%%02X", ord($1))/eg;
126             $dirOpen  = 1 if ( defined($currDir) && $f eq $currDir );
127             if ( $attr->{$f}{type} == BPC_FTYPE_DIR ) {
128                 #
129                 # Display directory if it exists in current backup.
130                 # First find out if there are subdirs
131                 #
132                 my($bold, $unbold, $BGcolor);
133                 $img |= 1 << 6;
134                 $img |= 1 << 5 if ( $attr->{$f}{nlink} > 2 );
135                 if ( $dirOpen ) {
136                     $bold = "<b>";
137                     $unbold = "</b>";
138                     $img |= 1 << 2;
139                     $img |= 1 << 3 if ( $attr->{$f}{nlink} > 2 );
140                 }
141                 my $imgFileName = sprintf("%07b.gif", $img);
142                 $imgStr = "<img src=\"$Conf{CgiImageDirURL}/$imgFileName\" align=\"absmiddle\" width=\"9\" height=\"19\" border=\"0\">";
143                 if ( "$relDir/$f" eq $dir ) {
144                     $BGcolor = " bgcolor=\"$Conf{CgiHeaderBgColor}\"";
145                 } else {
146                     $BGcolor = "";
147                 }
148                 my $dirName = $f;
149                 $dirName =~ s/ /&nbsp;/g;
150                 push(@DirStr, {needTick => 1,
151                                tdArgs   => $BGcolor,
152                                link     => <<EOF});
153 <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" style="font-size:13px;font-family:arial;text-decoration:none;line-height:15px">&nbsp;$bold$dirName$unbold</a></td></tr>
154 EOF
155                 $fileCnt++;
156                 $gotDir = 1;
157                 if ( $dirOpen ) {
158                     my($lastTick, $doneLastTick);
159                     foreach my $d ( @DirStrPrev ) {
160                         $lastTick = $d if ( $d->{needTick} );
161                     }
162                     $doneLastTick = 1 if ( !defined($lastTick) );
163                     foreach my $d ( @DirStrPrev ) {
164                         $img = 0;
165                         if  ( $d->{needTick} ) {
166                             $img |= 1 << 0;
167                         }
168                         if ( $d == $lastTick ) {
169                             $img |= 1 << 4;
170                             $doneLastTick = 1;
171                         } elsif ( !$doneLastTick ) {
172                             $img |= 1 << 3 | 1 << 4;
173                         }
174                         my $imgFileName = sprintf("%07b.gif", $img);
175                         $imgStr = "<img src=\"$Conf{CgiImageDirURL}/$imgFileName\" align=\"absmiddle\" width=\"9\" height=\"19\" border=\"0\">";
176                         push(@DirStr, {needTick => 0,
177                                        tdArgs   => $d->{tdArgs},
178                                        link     => $imgStr . $d->{link}
179                         });
180                     }
181                 }
182             }
183             if ( $relDir eq $dir ) {
184                 #
185                 # This is the selected directory, so display all the files
186                 #
187                 my $attrStr;
188                 if ( defined($a = $attr->{$f}) ) {
189                     my $mtimeStr = $bpc->timeStamp($a->{mtime});
190                     # UGH -> fix this
191                     my $typeStr  = BackupPC::Attrib::fileType2Text(undef,
192                                                                    $a->{type});
193                     my $modeStr  = sprintf("0%o", $a->{mode} & 07777);
194                     $attrStr .= <<EOF;
195     <td align="center">$typeStr</td>
196     <td align="center">$modeStr</td>
197     <td align="center">$a->{backupNum}</td>
198     <td align="right">$a->{size}</td>
199     <td align="right">$mtimeStr</td>
200 </tr>
201 EOF
202                 } else {
203                     $attrStr .= "<td colspan=\"5\" align=\"center\"> </td>\n";
204                 }
205                 (my $fDisp = "${EscHTML($f)}") =~ s/ /&nbsp;/g;
206                 if ( $gotDir ) {
207                     $fileStr .= <<EOF;
208 <tr bgcolor="#ffffcc"><td><input type="checkbox" name="fcb$checkBoxCnt" value="$path">&nbsp;<a href="$MyURL?action=browse&host=${EscURI($host)}&num=$num&share=$shareURI&dir=$path">$fDisp</a></td>
209 $attrStr
210 </tr>
211 EOF
212                 } else {
213                     $fileStr .= <<EOF;
214 <tr bgcolor="#ffffcc"><td><input type="checkbox" name="fcb$checkBoxCnt" value="$path">&nbsp;<a href="$MyURL?action=RestoreFile&host=${EscURI($host)}&num=$num&share=$shareURI&dir=$path">$fDisp</a></td>
215 $attrStr
216 </tr>
217 EOF
218                 }
219                 $checkBoxCnt++;
220             }
221         }
222         @DirStrPrev = @DirStr;
223         last if ( $relDir eq "" && $share eq "" );
224         # 
225         # Prune the last directory off $relDir, or at the very end
226         # do the top-level directory.
227         #
228         if ( $relDir eq "" || $relDir eq "/" || $relDir !~ /(.*)\/(.*)/ ) {
229             $currDir = $share;
230             $share = "";
231             $relDir = "";
232         } else {
233             $relDir  = $1;
234             $currDir = $2;
235         }
236     }
237     $share = $currDir;
238     my $dirDisplay = "$share/$dir";
239     $dirDisplay =~ s{//+}{/}g;
240     $dirDisplay =~ s{/+$}{}g;
241     $dirDisplay = "/" if ( $dirDisplay eq "" );
242     my $filledBackup;
243
244     if ( (my @mergeNums = @{$view->mergeNums}) > 1 ) {
245         shift(@mergeNums);
246         my $numF = join(", #", @mergeNums);
247         $filledBackup = eval("qq{$Lang->{This_display_is_merged_with_backup}}");
248     }
249     Header(eval("qq{$Lang->{Browse_backup__num_for__host}}"));
250
251     foreach my $d ( @DirStrPrev ) {
252         $dirStr .= "<tr><td$d->{tdArgs}>$d->{link}\n";
253     }
254
255     ### hide checkall button if there are no files
256     my ($topCheckAll, $checkAll, $fileHeader);
257     if ( $fileStr ) {
258         $fileHeader = eval("qq{$Lang->{fileHeader}}");
259
260         $checkAll = $Lang->{checkAll};
261
262         # and put a checkall box on top if there are at least 20 files
263         if ( $checkBoxCnt >= 20 ) {
264             $topCheckAll = $checkAll;
265             $topCheckAll =~ s{allFiles}{allFilestop}g;
266         }
267     } else {
268         $fileStr = eval("qq{$Lang->{The_directory_is_empty}}");
269     }
270     my @otherDirs;
271     my $pathURI  = $dir;
272     my $shareURI = $share;
273     $pathURI  =~ s/([^\w.\/-])/uc sprintf("%%%02x", ord($1))/eg;
274     $shareURI =~ s/([^\w.\/-])/uc sprintf("%%%02x", ord($1))/eg;
275     foreach my $i ( $view->backupList($share, $dir) ) {
276         push(@otherDirs, "<a href=\"$MyURL?action=browse&host=${EscURI($host)}"
277                        . "&num=$i&share=$shareURI&dir=$pathURI\">$i</a>");
278     }
279     if ( @otherDirs ) {
280         my $otherDirs  = join(",\n", @otherDirs);
281         $filledBackup .= eval("qq{$Lang->{Visit_this_directory_in_backup}}");
282     }
283     print (eval("qq{$Lang->{Backup_browse_for__host}}"));
284     Trailer();
285 }
286
287 1;