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