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