2ba2aff37e398e7d647108a67d04f99e4ae1ad3b
[BackupPC.git] / lib / BackupPC / CGI / DirHistory.pm
1 #============================================================= -*-perl-*-
2 #
3 # BackupPC::CGI::DirHistory package
4 #
5 # DESCRIPTION
6 #
7 #   This module implements the DirHistory action for the CGI interface.
8 #
9 # AUTHOR
10 #   Craig Barratt  <cbarratt@users.sourceforge.net>
11 #
12 # COPYRIGHT
13 #   Copyright (C) 2003-2007  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::DirHistory;
38
39 use strict;
40 use BackupPC::CGI::Lib qw(:all);
41 use BackupPC::View;
42 use BackupPC::Attrib qw(:all);
43 use Encode;
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 $share  = $In{share};
56     my $dir    = $In{dir};
57     my $dirURI = $dir;
58     my $shareURI = $share;
59     $dirURI    =~ s/([^\w.\/-])/uc sprintf("%%%02x", ord($1))/eg;
60     $shareURI  =~ s/([^\w.\/-])/uc sprintf("%%%02x", ord($1))/eg;
61
62     ErrorExit($Lang->{Empty_host_name}) if ( $host eq "" );
63
64     my @Backups = $bpc->BackupInfoRead($host);
65     my $view = BackupPC::View->new($bpc, $host, \@Backups, {inode => 1});
66     my $hist = $view->dirHistory($share, $dir);
67     my($backupNumStr, $backupTimeStr, $fileStr);
68
69     $dir = "/$dir" if ( $dir !~ /^\// );
70
71     if ( "/$host/$share/$dir/" =~ m{/\.\./} ) {
72         ErrorExit($Lang->{Nice_try__but_you_can_t_put});
73     }
74
75     my @backupList = $view->backupList($share, $dir);
76     foreach $i ( @backupList ) {
77         my $backupTime  = timeStamp2($Backups[$i]{startTime});
78         my $num = $Backups[$i]{num};
79         $backupNumStr  .= "<td align=center><a href=\"$MyURL?action=browse"
80                         . "&host=${EscURI($host)}&num=$num&share=$shareURI"
81                         . "&dir=$dirURI\">$num</a></td>";
82         $backupTimeStr .= "<td align=center>$backupTime</td>";
83     }
84
85     foreach my $f ( sort {uc($a) cmp uc($b)} keys(%$hist) ) {
86         my %inode2name;
87         my $nameCnt = 0;
88         (my $fDisp  = "${EscHTML($f)}") =~ s/ /&nbsp;/g;
89         $fDisp      = decode_utf8($fDisp);
90         $fileStr   .= "<tr><td align=\"left\"  class=\"histView\">$fDisp</td>";
91         my($colSpan, $url, $inode, $type);
92         my $tdClass = ' class="histView"';
93         foreach $i ( @backupList ) {
94             my($path);
95             if ( $colSpan > 0 ) {
96                 #
97                 # The file is the same if it also size==0 (inode == -1)
98                 # or if it is a directory and the previous one is (inode == -2)
99                 # or if the inodes agree and the types are the same.
100                 #
101                 if ( defined($hist->{$f}[$i])
102                     && $hist->{$f}[$i]{type} == $type
103                     && (($hist->{$f}[$i]{size} == 0 && $inode == -1)
104                      || ($hist->{$f}[$i]{type} == BPC_FTYPE_DIR && $inode == -2)
105                      || $hist->{$f}[$i]{inode} == $inode) ) {
106                     $colSpan++;
107                     next;
108                 }
109                 #
110                 # Also handle the case of a sequence of missing files
111                 #
112                 if ( !defined($hist->{$f}[$i]) && $inode == -3 ) {
113                     $colSpan++;
114                     next;
115                 }
116                 $fileStr .= "<td align=center colspan=$colSpan$tdClass>"
117                           . "$url</td>";
118                 $colSpan = 0;
119                 $tdClass = ' class="histView"';
120             }
121             if ( !defined($hist->{$f}[$i]) ) {
122                 $colSpan = 1;
123                 $url     = "&nbsp;";
124                 $inode   = -3;                  # special value for missing
125                 $tdClass = ' class="histViewMis"';
126                 next;
127             }
128             if ( $dir eq "" ) {
129                 $path = "/$f";
130             } else {
131                 ($path = "$dir/$f") =~ s{//+}{/}g;
132             }
133             $path =~ s{^/+}{/};
134             $path =~ s/([^\w.\/-])/uc sprintf("%%%02X", ord($1))/eg;
135             my $num = $hist->{$f}[$i]{backupNum};
136             if ( $hist->{$f}[$i]{type} == BPC_FTYPE_DIR ) {
137                 $inode = -2;                    # special value for dir
138                 $type  = $hist->{$f}[$i]{type};
139                 $url   = <<EOF;
140 <a href="$MyURL?action=dirHistory&host=${EscURI($host)}&num=$num&share=$shareURI&dir=$path">$Lang->{DirHistory_dirLink}</a>
141 EOF
142             } else {
143                 $inode = $hist->{$f}[$i]{inode};
144                 $type  = $hist->{$f}[$i]{type};
145                 #
146                 # special value for empty file
147                 #
148                 $inode = -1 if ( $hist->{$f}[$i]{size} == 0 );
149                 if ( !defined($inode2name{$inode}) ) {
150                     $inode2name{$inode}
151                                 = "$Lang->{DirHistory_fileLink}$nameCnt";
152                     $nameCnt++;
153                 }
154                 $url = <<EOF;
155 <a href="$MyURL?action=RestoreFile&host=${EscURI($host)}&num=$num&share=$shareURI&dir=$path">$inode2name{$inode}</a>
156 EOF
157             }
158             $colSpan = 1;
159         }
160         if ( $colSpan > 0 ) {
161             $fileStr .= "<td align=center colspan=$colSpan$tdClass>$url</td>";
162             $colSpan = 0;
163         }
164         $fileStr .= "</tr>\n";
165     }
166
167     my $dirDisplay = decode_utf8("$share/$dir");
168     $dirDisplay =~ s{//+}{/}g;
169     $dirDisplay =~ s{/+$}{}g;
170     $dirDisplay = "/" if ( $dirDisplay eq "" );
171     my $content = eval("qq{$Lang->{DirHistory_for__host}}");
172     Header(eval("qq{$Lang->{DirHistory_backup_for__host}}"), $content);
173     Trailer();
174 }
175
176 1;