* Major changes from Ryan Kucera to add style sheets to the CGI
[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
64     #
65     # default to the newest backup
66     #
67     if ( !defined($In{num}) && defined(@Backups) ) {
68         $i = @Backups - 1;
69         $num = $Backups[$i]{num};
70     }
71
72     for ( $i = 0 ; $i < @Backups ; $i++ ) {
73         last if ( $Backups[$i]{num} == $num );
74     }
75     if ( $i >= @Backups ) {
76         ErrorExit("Backup number $num for host ${EscHTML($host)} does"
77                 . " not exist.");
78     }
79     my $backupTime = timeStamp2($Backups[$i]{startTime});
80     my $backupAge = sprintf("%.1f", (time - $Backups[$i]{startTime})
81                                     / (24 * 3600));
82     my $view = BackupPC::View->new($bpc, $host, \@Backups);
83
84     if ( $dir eq "" || $dir eq "." || $dir eq ".." ) {
85         $attr = $view->dirAttrib($num, "", "");
86         if ( keys(%$attr) > 0 ) {
87             $share = (sort(keys(%$attr)))[0];
88             $dir   = '/';
89         } else {
90             ErrorExit(eval("qq{$Lang->{Directory___EscHTML}}"));
91         }
92     }
93     $dir = "/$dir" if ( $dir !~ /^\// );
94     my $relDir  = $dir;
95     my $currDir = undef;
96     if ( $dir =~ m{(^|/)\.\.(/|$)} ) {
97         ErrorExit($Lang->{Nice_try__but_you_can_t_put});
98     }
99
100     #
101     # Loop up the directory tree until we hit the top.
102     #
103     my(@DirStrPrev);
104     while ( 1 ) {
105         my($fLast, $fLastum, @DirStr);
106
107         $attr = $view->dirAttrib($num, $share, $relDir);
108         if ( !defined($attr) ) {
109             ErrorExit(eval("qq{$Lang->{Can_t_browse_bad_directory_name2}}"));
110         }
111
112         my $fileCnt = 0;          # file counter
113         $fLast = $dirStr = "";
114
115         #
116         # Loop over each of the files in this directory
117         #
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 "" ) {
123                 $path = "/$f";
124             } else {
125                 ($path = "$relDir/$f") =~ s{//+}{/}g;
126             }
127             if ( $shareURI eq "" ) {
128                 $shareURI = $f;
129                 $path  = "/";
130             }
131             $path =~ s{^/+}{/};
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 ) {
137                 #
138                 # Display directory if it exists in current backup.
139                 # First find out if there are subdirs
140                 #
141                 my $tdStyle;
142                 my $linkStyle = "fview";
143                 $img |= 1 << 6;
144                 $img |= 1 << 5 if ( $attr->{$f}{nlink} > 2 );
145                 if ( $dirOpen ) {
146                     $linkStyle = "fviewbold";
147                     $img |= 1 << 2;
148                     $img |= 1 << 3 if ( $attr->{$f}{nlink} > 2 );
149                 }
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";
154                 } else {
155                     $tdStyle = "fviewoff";
156                 }
157                 my $dirName = $f;
158                 $dirName =~ s/ /&nbsp;/g;
159                 push(@DirStr, {needTick => 1,
160                                tdArgs   => " class=\"$tdStyle\"",
161                                link     => <<EOF});
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">&nbsp;$dirName</a></td></tr>
163 EOF
164                 $fileCnt++;
165                 $gotDir = 1;
166                 if ( $dirOpen ) {
167                     my($lastTick, $doneLastTick);
168                     foreach my $d ( @DirStrPrev ) {
169                         $lastTick = $d if ( $d->{needTick} );
170                     }
171                     $doneLastTick = 1 if ( !defined($lastTick) );
172                     foreach my $d ( @DirStrPrev ) {
173                         $img = 0;
174                         if  ( $d->{needTick} ) {
175                             $img |= 1 << 0;
176                         }
177                         if ( $d == $lastTick ) {
178                             $img |= 1 << 4;
179                             $doneLastTick = 1;
180                         } elsif ( !$doneLastTick ) {
181                             $img |= 1 << 3 | 1 << 4;
182                         }
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}
188                         });
189                     }
190                 }
191             }
192             if ( $relDir eq $dir ) {
193                 #
194                 # This is the selected directory, so display all the files
195                 #
196                 my ($attrStr, $iconStr);
197                 if ( defined($a = $attr->{$f}) ) {
198                     my $mtimeStr = $bpc->timeStamp($a->{mtime});
199                     # UGH -> fix this
200                     my $typeStr  = BackupPC::Attrib::fileType2Text(undef,
201                                                                    $a->{type});
202                     my $modeStr  = sprintf("0%o", $a->{mode} & 07777);
203                     $iconStr = <<EOF;
204 <img src="$Conf{CgiImageDirURL}/icon-$typeStr.gif" align="center">
205 EOF
206                     $attrStr .= <<EOF;
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>
212 </tr>
213 EOF
214                 } else {
215                     $attrStr .= "<td colspan=\"5\" align=\"center\" class=\"fviewborder\"> </td>\n";
216                 }
217                 (my $fDisp = "${EscHTML($f)}") =~ s/ /&nbsp;/g;
218                 if ( $gotDir ) {
219                     $fileStr .= <<EOF;
220 <tr><td class="fviewborder">
221     <table cellpadding="0" cellspacing="0" border="0"><tr><td>
222     <input type="checkbox" name="fcb$checkBoxCnt" value="$path">&nbsp;$iconStr
223     </td><td>
224         &nbsp;<a href="$MyURL?action=browse&host=${EscURI($host)}&num=$num&share=$shareURI&dir=$path">$fDisp</a>
225     </td></tr></table>
226 </td>
227 $attrStr
228 </tr>
229 EOF
230                 } else {
231                     $fileStr .= <<EOF;
232 <tr><td class="fviewborder">
233     <table cellpadding="0" cellspacing="0" border="0"><tr><td>
234     <input type="checkbox" name="fcb$checkBoxCnt" value="$path">&nbsp;$iconStr
235     </td><td>
236         &nbsp;<a href="$MyURL?action=RestoreFile&host=${EscURI($host)}&num=$num&share=$shareURI&dir=$path">$fDisp</a>
237     </td></tr></table>
238 </td>
239 $attrStr
240 </tr>
241 EOF
242                 }
243                 $checkBoxCnt++;
244             }
245         }
246         @DirStrPrev = @DirStr;
247         last if ( $relDir eq "" && $share eq "" );
248         # 
249         # Prune the last directory off $relDir, or at the very end
250         # do the top-level directory.
251         #
252         if ( $relDir eq "" || $relDir eq "/" || $relDir !~ /(.*)\/(.*)/ ) {
253             $currDir = $share;
254             $share = "";
255             $relDir = "";
256         } else {
257             $relDir  = $1;
258             $currDir = $2;
259         }
260     }
261     $share = $currDir;
262     my $dirDisplay = "$share/$dir";
263     $dirDisplay =~ s{//+}{/}g;
264     $dirDisplay =~ s{/+$}{}g;
265     $dirDisplay = "/" if ( $dirDisplay eq "" );
266     my $filledBackup;
267
268     if ( (my @mergeNums = @{$view->mergeNums}) > 1 ) {
269         shift(@mergeNums);
270         my $numF = join(", #", @mergeNums);
271         $filledBackup = eval("qq{$Lang->{This_display_is_merged_with_backup}}");
272     }
273
274     foreach my $d ( @DirStrPrev ) {
275         $dirStr .= "<tr><td$d->{tdArgs}>$d->{link}\n";
276     }
277
278     ### hide checkall button if there are no files
279     my ($topCheckAll, $checkAll, $fileHeader);
280     if ( $fileStr ) {
281         $fileHeader = eval("qq{$Lang->{fileHeader}}");
282
283         $checkAll = $Lang->{checkAll};
284
285         # and put a checkall box on top if there are at least 20 files
286         if ( $checkBoxCnt >= 20 ) {
287             $topCheckAll = $checkAll;
288             $topCheckAll =~ s{allFiles}{allFilestop}g;
289         }
290     } else {
291         $fileStr = eval("qq{$Lang->{The_directory_is_empty}}");
292     }
293     my @otherDirs;
294     my $pathURI  = $dir;
295     my $shareURI = $share;
296     $pathURI  =~ s/([^\w.\/-])/uc sprintf("%%%02x", ord($1))/eg;
297     $shareURI =~ s/([^\w.\/-])/uc sprintf("%%%02x", ord($1))/eg;
298     foreach my $i ( $view->backupList($share, $dir) ) {
299         push(@otherDirs, $i);
300     }
301     if ( @otherDirs ) {
302         my $otherDirs  = join(",\n", @otherDirs);
303         my $inc = 0;
304         foreach my $value (@otherDirs) {
305             my $selected = undef;
306             my $showDate = timeStamp2($Backups[$inc]{startTime});
307             $selected = " selected" if ($value == $num);
308             $otherDirs .= "<option value=\"$MyURL?action=browse&host=${EscURI($host)}&num=$value\"$selected>#$value - ($showDate)</option>\n";
309             $inc++;
310         }
311         $filledBackup .= eval("qq{$Lang->{Visit_this_directory_in_backup}}");
312     }
313     my $content = eval("qq{$Lang->{Backup_browse_for__host}}");
314     Header(eval("qq{$Lang->{Browse_backup__num_for__host}}"), $content);
315     Trailer();
316 }
317
318 1;