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, released 20 Jun 2004. |
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/ / /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"> $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/ / /g; |
218 |
if ( $gotDir ) { |
219 |
$fileStr .= <<EOF; |
220 |
<tr><td class="fviewborder"> |
221 |
<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> |
222 |
</td> |
223 |
$attrStr |
224 |
</tr> |
225 |
EOF |
226 |
} else { |
227 |
$fileStr .= <<EOF; |
228 |
<tr><td class="fviewborder"> |
229 |
<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> |
230 |
</td> |
231 |
$attrStr |
232 |
</tr> |
233 |
EOF |
234 |
} |
235 |
$checkBoxCnt++; |
236 |
} |
237 |
} |
238 |
@DirStrPrev = @DirStr; |
239 |
last if ( $relDir eq "" && $share eq "" ); |
240 |
# |
241 |
# Prune the last directory off $relDir, or at the very end |
242 |
# do the top-level directory. |
243 |
# |
244 |
if ( $relDir eq "" || $relDir eq "/" || $relDir !~ /(.*)\/(.*)/ ) { |
245 |
$currDir = $share; |
246 |
$share = ""; |
247 |
$relDir = ""; |
248 |
} else { |
249 |
$relDir = $1; |
250 |
$currDir = $2; |
251 |
} |
252 |
} |
253 |
$share = $currDir; |
254 |
my $dirDisplay = "$share/$dir"; |
255 |
$dirDisplay =~ s{//+}{/}g; |
256 |
$dirDisplay =~ s{/+$}{}g; |
257 |
$dirDisplay = "/" if ( $dirDisplay eq "" ); |
258 |
my $filledBackup; |
259 |
|
260 |
if ( (my @mergeNums = @{$view->mergeNums}) > 1 ) { |
261 |
shift(@mergeNums); |
262 |
my $numF = join(", #", @mergeNums); |
263 |
$filledBackup = eval("qq{$Lang->{This_display_is_merged_with_backup}}"); |
264 |
} |
265 |
|
266 |
foreach my $d ( @DirStrPrev ) { |
267 |
$dirStr .= "<tr><td$d->{tdArgs}>$d->{link}\n"; |
268 |
} |
269 |
|
270 |
### hide checkall button if there are no files |
271 |
my ($topCheckAll, $checkAll, $fileHeader); |
272 |
if ( $fileStr ) { |
273 |
$fileHeader = eval("qq{$Lang->{fileHeader}}"); |
274 |
|
275 |
$checkAll = $Lang->{checkAll}; |
276 |
|
277 |
# and put a checkall box on top if there are at least 20 files |
278 |
if ( $checkBoxCnt >= 20 ) { |
279 |
$topCheckAll = $checkAll; |
280 |
$topCheckAll =~ s{allFiles}{allFilestop}g; |
281 |
} |
282 |
} else { |
283 |
$fileStr = eval("qq{$Lang->{The_directory_is_empty}}"); |
284 |
} |
285 |
my $pathURI = $dir; |
286 |
my $shareURI = $share; |
287 |
$pathURI =~ s/([^\w.\/-])/uc sprintf("%%%02x", ord($1))/eg; |
288 |
$shareURI =~ s/([^\w.\/-])/uc sprintf("%%%02x", ord($1))/eg; |
289 |
if ( my @otherDirs = $view->backupList($share, $dir) ) { |
290 |
my $otherDirs; |
291 |
foreach my $i ( @otherDirs ) { |
292 |
my $selected; |
293 |
my $showDate = timeStamp2($Backups[$i]{startTime}); |
294 |
my $backupNum = $Backups[$i]{num}; |
295 |
$selected = " selected" if ( $backupNum == $num ); |
296 |
$otherDirs .= "<option value=\"$MyURL?action=browse&host=${EscURI($host)}&num=$backupNum&share=$shareURI&dir=$pathURI\"$selected>#$backupNum - ($showDate)</option>\n"; |
297 |
} |
298 |
$filledBackup .= eval("qq{$Lang->{Visit_this_directory_in_backup}}"); |
299 |
} |
300 |
my $content = eval("qq{$Lang->{Backup_browse_for__host}}"); |
301 |
Header(eval("qq{$Lang->{Browse_backup__num_for__host}}"), $content); |
302 |
Trailer(); |
303 |
} |
304 |
|
305 |
1; |