* Split BackupPC_Admin into a set of modules, one for each major action.
[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
88     #
89     # Loop up the directory tree until we hit the top.
90     #
91     my(@DirStrPrev);
92     while ( 1 ) {
93         my($fLast, $fLastum, @DirStr);
94
95         $attr = $view->dirAttrib($num, $share, $relDir);
96         if ( !defined($attr) ) {
97             ErrorExit(eval("qq{$Lang->{Can_t_browse_bad_directory_name2}}"));
98         }
99
100         my $fileCnt = 0;          # file counter
101         $fLast = $dirStr = "";
102
103         #
104         # Loop over each of the files in this directory
105         #
106         foreach my $f ( sort {uc($a) cmp uc($b)} keys(%$attr) ) {
107             my($dirOpen, $gotDir, $imgStr, $img, $path);
108             my $fURI = $f;                             # URI escaped $f
109             my $shareURI = $share;                     # URI escaped $share
110             if ( $relDir eq "" ) {
111                 $path = "/$f";
112             } else {
113                 ($path = "$relDir/$f") =~ s{//+}{/}g;
114             }
115             if ( $shareURI eq "" ) {
116                 $shareURI = $f;
117                 $path  = "/";
118             }
119             $path =~ s{^/+}{/};
120             $path     =~ s/([^\w.\/-])/uc sprintf("%%%02X", ord($1))/eg;
121             $fURI     =~ s/([^\w.\/-])/uc sprintf("%%%02X", ord($1))/eg;
122             $shareURI =~ s/([^\w.\/-])/uc sprintf("%%%02X", ord($1))/eg;
123             $dirOpen  = 1 if ( defined($currDir) && $f eq $currDir );
124             if ( $attr->{$f}{type} == BPC_FTYPE_DIR ) {
125                 #
126                 # Display directory if it exists in current backup.
127                 # First find out if there are subdirs
128                 #
129                 my($bold, $unbold, $BGcolor);
130                 $img |= 1 << 6;
131                 $img |= 1 << 5 if ( $attr->{$f}{nlink} > 2 );
132                 if ( $dirOpen ) {
133                     $bold = "<b>";
134                     $unbold = "</b>";
135                     $img |= 1 << 2;
136                     $img |= 1 << 3 if ( $attr->{$f}{nlink} > 2 );
137                 }
138                 my $imgFileName = sprintf("%07b.gif", $img);
139                 $imgStr = "<img src=\"$Conf{CgiImageDirURL}/$imgFileName\" align=\"absmiddle\" width=\"9\" height=\"19\" border=\"0\">";
140                 if ( "$relDir/$f" eq $dir ) {
141                     $BGcolor = " bgcolor=\"$Conf{CgiHeaderBgColor}\"";
142                 } else {
143                     $BGcolor = "";
144                 }
145                 my $dirName = $f;
146                 $dirName =~ s/ /&nbsp;/g;
147                 push(@DirStr, {needTick => 1,
148                                tdArgs   => $BGcolor,
149                                link     => <<EOF});
150 <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>
151 EOF
152                 $fileCnt++;
153                 $gotDir = 1;
154                 if ( $dirOpen ) {
155                     my($lastTick, $doneLastTick);
156                     foreach my $d ( @DirStrPrev ) {
157                         $lastTick = $d if ( $d->{needTick} );
158                     }
159                     $doneLastTick = 1 if ( !defined($lastTick) );
160                     foreach my $d ( @DirStrPrev ) {
161                         $img = 0;
162                         if  ( $d->{needTick} ) {
163                             $img |= 1 << 0;
164                         }
165                         if ( $d == $lastTick ) {
166                             $img |= 1 << 4;
167                             $doneLastTick = 1;
168                         } elsif ( !$doneLastTick ) {
169                             $img |= 1 << 3 | 1 << 4;
170                         }
171                         my $imgFileName = sprintf("%07b.gif", $img);
172                         $imgStr = "<img src=\"$Conf{CgiImageDirURL}/$imgFileName\" align=\"absmiddle\" width=\"9\" height=\"19\" border=\"0\">";
173                         push(@DirStr, {needTick => 0,
174                                        tdArgs   => $d->{tdArgs},
175                                        link     => $imgStr . $d->{link}
176                         });
177                     }
178                 }
179             }
180             if ( $relDir eq $dir ) {
181                 #
182                 # This is the selected directory, so display all the files
183                 #
184                 my $attrStr;
185                 if ( defined($a = $attr->{$f}) ) {
186                     my $mtimeStr = $bpc->timeStamp($a->{mtime});
187                     # UGH -> fix this
188                     my $typeStr  = BackupPC::Attrib::fileType2Text(undef,
189                                                                    $a->{type});
190                     my $modeStr  = sprintf("0%o", $a->{mode} & 07777);
191                     $attrStr .= <<EOF;
192     <td align="center">$typeStr</td>
193     <td align="center">$modeStr</td>
194     <td align="center">$a->{backupNum}</td>
195     <td align="right">$a->{size}</td>
196     <td align="right">$mtimeStr</td>
197 </tr>
198 EOF
199                 } else {
200                     $attrStr .= "<td colspan=\"5\" align=\"center\"> </td>\n";
201                 }
202                 (my $fDisp = "${EscHTML($f)}") =~ s/ /&nbsp;/g;
203                 if ( $gotDir ) {
204                     $fileStr .= <<EOF;
205 <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>
206 $attrStr
207 </tr>
208 EOF
209                 } else {
210                     $fileStr .= <<EOF;
211 <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>
212 $attrStr
213 </tr>
214 EOF
215                 }
216                 $checkBoxCnt++;
217             }
218         }
219         @DirStrPrev = @DirStr;
220         last if ( $relDir eq "" && $share eq "" );
221         # 
222         # Prune the last directory off $relDir, or at the very end
223         # do the top-level directory.
224         #
225         if ( $relDir eq "" || $relDir eq "/" || $relDir !~ /(.*)\/(.*)/ ) {
226             $currDir = $share;
227             $share = "";
228             $relDir = "";
229         } else {
230             $relDir  = $1;
231             $currDir = $2;
232         }
233     }
234     $share = $currDir;
235     my $dirDisplay = "$share/$dir";
236     $dirDisplay =~ s{//+}{/}g;
237     $dirDisplay =~ s{/+$}{}g;
238     $dirDisplay = "/" if ( $dirDisplay eq "" );
239     my $filledBackup;
240
241     if ( (my @mergeNums = @{$view->mergeNums}) > 1 ) {
242         shift(@mergeNums);
243         my $numF = join(", #", @mergeNums);
244         $filledBackup = eval("qq{$Lang->{This_display_is_merged_with_backup}}");
245     }
246     Header(eval("qq{$Lang->{Browse_backup__num_for__host}}"));
247
248     foreach my $d ( @DirStrPrev ) {
249         $dirStr .= "<tr><td$d->{tdArgs}>$d->{link}\n";
250     }
251
252     ### hide checkall button if there are no files
253     my ($topCheckAll, $checkAll, $fileHeader);
254     if ( $fileStr ) {
255         $fileHeader = eval("qq{$Lang->{fileHeader}}");
256
257         $checkAll = $Lang->{checkAll};
258
259         # and put a checkall box on top if there are at least 20 files
260         if ( $checkBoxCnt >= 20 ) {
261             $topCheckAll = $checkAll;
262             $topCheckAll =~ s{allFiles}{allFilestop}g;
263         }
264     } else {
265         $fileStr = eval("qq{$Lang->{The_directory_is_empty}}");
266     }
267     my @otherDirs;
268     my $pathURI  = $dir;
269     my $shareURI = $share;
270     $pathURI  =~ s/([^\w.\/-])/uc sprintf("%%%02x", ord($1))/eg;
271     $shareURI =~ s/([^\w.\/-])/uc sprintf("%%%02x", ord($1))/eg;
272     foreach my $i ( $view->backupList($share, $dir) ) {
273         push(@otherDirs, "<a href=\"$MyURL?action=browse&host=${EscURI($host)}"
274                        . "&num=$i&share=$shareURI&dir=$pathURI\">$i</a>");
275     }
276     if ( @otherDirs ) {
277         my $otherDirs  = join(",\n", @otherDirs);
278         $filledBackup .= eval("qq{$Lang->{Visit_this_directory_in_backup}}");
279     }
280     print (eval("qq{$Lang->{Backup_browse_for__host}}"));
281     Trailer();
282 }
283
284 1;