* Copyright date update
[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-2009  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 3.2.0beta0, released 5 April 2009.
32 #
33 # See http://backuppc.sourceforge.net.
34 #
35 #========================================================================
36
37 package BackupPC::CGI::Browse;
38
39 use strict;
40 use Encode qw/decode_utf8/;
41 use BackupPC::CGI::Lib qw(:all);
42 use BackupPC::View;
43 use BackupPC::Attrib qw(:all);
44
45 sub action
46 {
47     my $Privileged = CheckPermission($In{host});
48     my($i, $dirStr, $fileStr, $attr);
49     my $checkBoxCnt = 0;
50
51     if ( !$Privileged ) {
52         ErrorExit(eval("qq{$Lang->{Only_privileged_users_can_browse_backup_files}}"));
53     }
54     my $host  = $In{host};
55     my $num   = $In{num};
56     my $share = $In{share};
57     my $dir   = $In{dir};
58
59     ErrorExit($Lang->{Empty_host_name}) if ( $host eq "" );
60     #
61     # Find the requested backup and the previous filled backup
62     #
63     my @Backups = $bpc->BackupInfoRead($host);
64
65     #
66     # default to the newest backup
67     #
68     if ( !defined($In{num}) && defined(@Backups) && @Backups > 0 ) {
69         $i = @Backups - 1;
70         $num = $Backups[$i]{num};
71     }
72
73     for ( $i = 0 ; $i < @Backups ; $i++ ) {
74         last if ( $Backups[$i]{num} == $num );
75     }
76     if ( $i >= @Backups ) {
77         ErrorExit("Backup number $num for host ${EscHTML($host)} does"
78                 . " not exist.");
79     }
80     my $backupTime = timeStamp2($Backups[$i]{startTime});
81     my $backupAge = sprintf("%.1f", (time - $Backups[$i]{startTime})
82                                     / (24 * 3600));
83     my $view = BackupPC::View->new($bpc, $host, \@Backups, {nlink => 1});
84
85     if ( $dir eq "" || $dir eq "." || $dir eq ".." ) {
86         $attr = $view->dirAttrib($num, "", "");
87         if ( keys(%$attr) > 0 ) {
88             $share = (sort(keys(%$attr)))[0];
89             $dir   = '/';
90         } else {
91             ErrorExit(eval("qq{$Lang->{Directory___EscHTML}}"));
92         }
93     }
94     $dir = "/$dir" if ( $dir !~ /^\// );
95     my $relDir  = $dir;
96     my $currDir = undef;
97     if ( $dir =~ m{(^|/)\.\.(/|$)} ) {
98         ErrorExit($Lang->{Nice_try__but_you_can_t_put});
99     }
100
101     #
102     # Loop up the directory tree until we hit the top.
103     #
104     my(@DirStrPrev);
105     while ( 1 ) {
106         my($fLast, $fLastum, @DirStr);
107
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}}"));
112         }
113
114         my $fileCnt = 0;          # file counter
115         $fLast = $dirStr = "";
116
117         #
118         # Loop over each of the files in this directory
119         #
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 "" ) {
125                 $path = "/$f";
126             } else {
127                 ($path = "$relDir/$f") =~ s{//+}{/}g;
128             }
129             if ( $shareURI eq "" ) {
130                 $shareURI = $f;
131                 $path  = "/";
132             }
133             $path =~ s{^/+}{/};
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 ) {
139                 #
140                 # Display directory if it exists in current backup.
141                 # First find out if there are subdirs
142                 #
143                 my $tdStyle;
144                 my $linkStyle = "fview";
145                 $img |= 1 << 6;
146                 $img |= 1 << 5 if ( $attr->{$f}{nlink} > 2 );
147                 if ( $dirOpen ) {
148                     $linkStyle = "fviewbold";
149                     $img |= 1 << 2;
150                     $img |= 1 << 3 if ( $attr->{$f}{nlink} > 2 );
151                 }
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";
156                 } else {
157                     $tdStyle = "fviewoff";
158                 }
159                 my $dirName = $f;
160                 $dirName =~ s/ /&nbsp;/g;
161                 $dirName = decode_utf8($dirName);
162                 push(@DirStr, {needTick => 1,
163                                tdArgs   => " class=\"$tdStyle\"",
164                                link     => <<EOF});
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">&nbsp;$dirName</a></td></tr>
166 EOF
167                 $fileCnt++;
168                 $gotDir = 1;
169                 if ( $dirOpen ) {
170                     my($lastTick, $doneLastTick);
171                     foreach my $d ( @DirStrPrev ) {
172                         $lastTick = $d if ( $d->{needTick} );
173                     }
174                     $doneLastTick = 1 if ( !defined($lastTick) );
175                     foreach my $d ( @DirStrPrev ) {
176                         $img = 0;
177                         if  ( $d->{needTick} ) {
178                             $img |= 1 << 0;
179                         }
180                         if ( $d == $lastTick ) {
181                             $img |= 1 << 4;
182                             $doneLastTick = 1;
183                         } elsif ( !$doneLastTick ) {
184                             $img |= 1 << 3 | 1 << 4;
185                         }
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}
191                         });
192                     }
193                 }
194             }
195             if ( $relDir eq $dir ) {
196                 #
197                 # This is the selected directory, so display all the files
198                 #
199                 my ($attrStr, $iconStr);
200                 if ( defined($a = $attr->{$f}) ) {
201                     my $mtimeStr = $bpc->timeStamp($a->{mtime});
202                     # UGH -> fix this
203                     my $typeStr  = BackupPC::Attrib::fileType2Text(undef,
204                                                                    $a->{type});
205                     my $modeStr  = sprintf("0%o", $a->{mode} & 07777);
206                     $iconStr = <<EOF;
207 <img src="$Conf{CgiImageDirURL}/icon-$typeStr.png" valign="top">
208 EOF
209                     $attrStr .= <<EOF;
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>
215 </tr>
216 EOF
217                 } else {
218                     $attrStr .= "<td colspan=\"5\" align=\"center\" class=\"fviewborder\"> </td>\n";
219                 }
220                 (my $fDisp = "${EscHTML($f)}") =~ s/ /&nbsp;/g;
221                 $fDisp = decode_utf8($fDisp);
222                 if ( $gotDir ) {
223                     $fileStr .= <<EOF;
224 <tr><td class="fviewborder">
225     <input type="checkbox" name="fcb$checkBoxCnt" value="$path">&nbsp;$iconStr&nbsp;<a href="$MyURL?action=browse&host=${EscURI($host)}&num=$num&share=$shareURI&dir=$path">$fDisp</a>
226 </td>
227 $attrStr
228 </tr>
229 EOF
230                 } else {
231                     $fileStr .= <<EOF;
232 <tr><td class="fviewborder">
233     <input type="checkbox" name="fcb$checkBoxCnt" value="$path">&nbsp;$iconStr&nbsp;<a href="$MyURL?action=RestoreFile&host=${EscURI($host)}&num=$num&share=$shareURI&dir=$path">$fDisp</a>
234 </td>
235 $attrStr
236 </tr>
237 EOF
238                 }
239                 $checkBoxCnt++;
240             }
241         }
242         @DirStrPrev = @DirStr;
243         last if ( $relDir eq "" && $share eq "" );
244         # 
245         # Prune the last directory off $relDir, or at the very end
246         # do the top-level directory.
247         #
248         if ( $relDir eq "" || $relDir eq "/" || $relDir !~ /(.*)\/(.*)/ ) {
249             $currDir = $share;
250             $share = "";
251             $relDir = "";
252         } else {
253             $relDir  = $1;
254             $currDir = $2;
255         }
256     }
257     $share = $currDir;
258     my $dirDisplay = decode_utf8("$share/$dir");
259     $dirDisplay =~ s{//+}{/}g;
260     $dirDisplay =~ s{/+$}{}g;
261     $dirDisplay = "/" if ( $dirDisplay eq "" );
262     my $filledBackup;
263
264     if ( (my @mergeNums = @{$view->mergeNums}) > 1 ) {
265         shift(@mergeNums);
266         my $numF = join(", #", @mergeNums);
267         $filledBackup = eval("qq{$Lang->{This_display_is_merged_with_backup}}");
268     }
269
270     foreach my $d ( @DirStrPrev ) {
271         $dirStr .= "<tr><td$d->{tdArgs}>$d->{link}\n";
272     }
273
274     ### hide checkall button if there are no files
275     my ($topCheckAll, $checkAll, $fileHeader);
276     if ( $fileStr ) {
277         $fileHeader = eval("qq{$Lang->{fileHeader}}");
278
279         $checkAll = $Lang->{checkAll};
280
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;
285         }
286     } else {
287         $fileStr = eval("qq{$Lang->{The_directory_is_empty}}");
288     }
289     my $pathURI  = $dir;
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) ) {
294         my $otherDirs;
295         foreach my $i ( @otherDirs ) {
296             my $selected;
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";
301         }
302         $filledBackup .= eval("qq{$Lang->{Visit_this_directory_in_backup}}");
303     }
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);
308     Trailer();
309 }
310
311 1;