- added utf8 to output filter and added encoding of file names
[BackupPC.git] / lib / BackupPC / CGI / View.pm
1 #============================================================= -*-perl-*-
2 #
3 # BackupPC::CGI::View package
4 #
5 # DESCRIPTION
6 #
7 #   This module implements the View 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.0alpha, released 23 Jan 2006.
32 #
33 # See http://backuppc.sourceforge.net.
34 #
35 #========================================================================
36
37 package BackupPC::CGI::View;
38
39 use strict;
40 use BackupPC::CGI::Lib qw(:all);
41 use BackupPC::FileZIO;
42
43 sub action
44 {
45     my $Privileged = CheckPermission($In{host});
46     my $compress = 0;
47     my $fh;
48     my $host = $In{host};
49     my $num  = $In{num};
50     my $type = $In{type};
51     my $linkHosts = 0;
52     my($file, $comment);
53     my $ext = $num ne "" ? ".$num" : "";
54
55     ErrorExit(eval("qq{$Lang->{Invalid_number__num}}"))
56                     if ( $num ne "" && $num !~ /^\d+$/ );
57     if ( $type eq "XferLOG" ) {
58         $file = "$TopDir/pc/$host/SmbLOG$ext";
59         $file = "$TopDir/pc/$host/XferLOG$ext" if ( !-f $file && !-f "$file.z");
60     } elsif ( $type eq "XferLOGbad" ) {
61         $file = "$TopDir/pc/$host/SmbLOG.bad";
62         $file = "$TopDir/pc/$host/XferLOG.bad" if ( !-f $file && !-f "$file.z");
63     } elsif ( $type eq "XferErrbad" ) {
64         $file = "$TopDir/pc/$host/SmbLOG.bad";
65         $file = "$TopDir/pc/$host/XferLOG.bad" if ( !-f $file && !-f "$file.z");
66         $comment = $Lang->{Extracting_only_Errors};
67     } elsif ( $type eq "XferErr" ) {
68         $file = "$TopDir/pc/$host/SmbLOG$ext";
69         $file = "$TopDir/pc/$host/XferLOG$ext" if ( !-f $file && !-f "$file.z");
70         $comment = $Lang->{Extracting_only_Errors};
71     } elsif ( $type eq "RestoreLOG" ) {
72         $file = "$TopDir/pc/$host/RestoreLOG$ext";
73     } elsif ( $type eq "RestoreErr" ) {
74         $file = "$TopDir/pc/$host/RestoreLOG$ext";
75         $comment = $Lang->{Extracting_only_Errors};
76     } elsif ( $type eq "ArchiveLOG" ) {
77         $file = "$TopDir/pc/$host/ArchiveLOG$ext";
78     } elsif ( $type eq "ArchiveErr" ) {
79         $file = "$TopDir/pc/$host/ArchiveLOG$ext";
80         $comment = $Lang->{Extracting_only_Errors};
81     } elsif ( $host ne "" && $type eq "config" ) {
82         $file = "$TopDir/pc/$host/config.pl";
83         $file = "$TopDir/conf/$host.pl"
84                     if ( $host ne "config" && -f "$TopDir/conf/$host.pl"
85                                            && !-f $file );
86     } elsif ( $type eq "docs" ) {
87         $file = "$BinDir/../doc/BackupPC.html";
88     } elsif ( $type eq "config" ) {
89         $file = "$TopDir/conf/config.pl";
90     } elsif ( $type eq "hosts" ) {
91         $file = "$TopDir/conf/hosts";
92         $linkHosts = 1;
93     } elsif ( $host ne "" ) {
94         $file = "$TopDir/pc/$host/LOG$ext";
95         $linkHosts = 1;
96     } else {
97         $file = "$LogDir/LOG$ext";
98         $linkHosts = 1;
99     }
100     if ( $type ne "docs" && !$Privileged ) {
101         ErrorExit($Lang->{Only_privileged_users_can_view_log_or_config_files});
102     }
103     if ( !-f $file && -f "$file.z" ) {
104         $file .= ".z";
105         $compress = 1;
106     }
107     my($contentPre, $contentSub, $contentPost);
108     $contentPre .= eval("qq{$Lang->{Log_File__file__comment}}");
109     if ( defined($fh = BackupPC::FileZIO->open($file, 0, $compress)) ) {
110
111         $fh->utf8(1);
112         my $mtimeStr = $bpc->timeStamp((stat($file))[9], 1);
113
114         $contentPre .= eval("qq{$Lang->{Contents_of_log_file}}");
115
116         $contentPre .= "<pre>";
117         if ( $type eq "XferErr" || $type eq "XferErrbad"
118                                 || $type eq "RestoreErr"
119                                 || $type eq "ArchiveErr" ) {
120             $contentSub = sub {
121                 #
122                 # Because the content might be large, we use
123                 # a sub to return the data in 64K chunks.
124                 #
125                 my($skipped, $c, $s);
126                 while ( length($c) < 65536 ) {
127                     $s = $fh->readLine();
128                     if ( $s eq "" ) {
129                         $c .= eval("qq{$Lang->{skipped__skipped_lines}}")
130                                                         if ( $skipped );
131                         last;
132                     }
133                     $s =~ s/[\n\r]+//g;
134                     if ( $s =~ /smb: \\>/
135                             || $s =~ /^\s*(\d+) \(\s*\d+\.\d kb\/s\) (.*)$/
136                             || $s =~ /^tar: dumped \d+ files/
137                             || $s =~ /^\s*added interface/i
138                             || $s =~ /^\s*restore tar file /i
139                             || $s =~ /^\s*restore directory /i
140                             || $s =~ /^\s*tarmode is now/i
141                             || $s =~ /^\s*Total bytes written/i
142                             || $s =~ /^\s*Domain=/i
143                             || $s =~ /^\s*Getting files newer than/i
144                             || $s =~ /^\s*Output is \/dev\/null/
145                             || $s =~ /^\s*\([\d.,]* kb\/s\) \(average [\d\.]* kb\/s\)$/
146                             || $s =~ /^\s+directory \\/
147                             || $s =~ /^\s*Timezone is/
148                             || $s =~ /^\s*creating lame (up|low)case table/i
149                             || $s =~ /^\.\//
150                             || $s =~ /^  / ) {
151                         $skipped++;
152                         next;
153                     }
154                     $c .= eval("qq{$Lang->{skipped__skipped_lines}}")
155                                                          if ( $skipped );
156                     $skipped = 0;
157                     $c .= ${EscHTML($s)} . "\n";
158                 }
159                 return $c;
160             };
161         } elsif ( $linkHosts ) {
162             #
163             # Because the content might be large, we use
164             # a sub to return the data in 64K chunks.
165             #
166             $contentSub = sub {
167                 my($c, $s);
168                 while ( length($c) < 65536 ) {
169                     $s = $fh->readLine();
170                     last if ( $s eq "" );
171                     $s =~ s/[\n\r]+//g;
172                     $s = ${EscHTML($s)};
173                     $s =~ s/\b([\w-]+)\b/defined($Hosts->{$1})
174                                             ? ${HostLink($1)} : $1/eg;
175                     $c .= $s . "\n";
176                 }
177                 return $c;
178             };
179         } elsif ( $type eq "config" ) {
180             #
181             # Because the content might be large, we use
182             # a sub to return the data in 64K chunks.
183             #
184             $contentSub = sub {
185                 my($c, $s);
186                 while ( length($c) < 65536 ) {
187                     $s = $fh->readLine();
188                     last if ( $s eq "" );
189                     $s =~ s/[\n\r]+//g;
190                     # remove any passwords and user names
191                     $s =~ s/(SmbSharePasswd.*=.*['"]).*(['"])/$1****$2/ig;
192                     $s =~ s/(SmbShareUserName.*=.*['"]).*(['"])/$1****$2/ig;
193                     $s =~ s/(RsyncdPasswd.*=.*['"]).*(['"])/$1****$2/ig;
194                     $s =~ s/(ServerMesgSecret.*=.*['"]).*(['"])/$1****$2/ig;
195                     $s = ${EscHTML($s)};
196                     $s =~ s[(\$Conf\{.*?\})][
197                         my $c = $1;
198                         my $s = lc($c);
199                         $s =~ s{(\W)}{sprintf("%%%02x", ord($1) )}gxe;
200                         "<a href=\"?action=view&type=docs#item_$s\"><tt>$c</tt></a>"
201                     ]eg;
202                     $c .= $s . "\n";
203                 }
204                 return $c;
205             };
206         } elsif ( $type eq "docs" ) {
207             #
208             # Because the content might be large, we use
209             # a sub to return the data in 64K chunks.
210             #
211             $contentSub = sub {
212                 my($c, $s);
213                 while ( length($c) < 65536 ) {
214                     $s = $fh->readLine();
215                     last if ( $s eq "" );
216                     $c .= $s;
217                 }
218                 return $c;
219             };
220             #
221             # Documentation has a different header and no pre or post text,
222             # so just handle it here
223             #
224             Header($Lang->{BackupPC__Documentation}, "", 0, $contentSub);
225             Trailer();
226             return;
227         } else {
228             #
229             # Because the content might be large, we use
230             # a sub to return the data in 64K chunks.
231             #
232             $contentSub = sub {
233                 my($c, $s);
234                 while ( length($c) < 65536 ) {
235                     $s = $fh->readLine();
236                     last if ( $s eq "" );
237                     $s =~ s/[\n\r]+//g;
238                     $s = ${EscHTML($s)};
239                     $c .= $s . "\n";
240                 }
241                 return $c;
242             };
243         }
244     } else {
245         if ( $type eq "docs" ) {
246             ErrorExit(eval("qq{$Lang->{Unable_to_open__file__configuration_problem}}"));
247         }
248         $contentPre .= eval("qq{$Lang->{_pre___Can_t_open_log_file__file}}");
249     }
250     $contentPost .= "</pre>\n" if ( $type ne "docs" );
251     Header(eval("qq{$Lang->{Backup_PC__Log_File__file}}"),
252                     $contentPre, !-f "$TopDir/pc/$host/backups",
253                     $contentSub, $contentPost);
254     Trailer();
255     $fh->close() if ( defined($fh) );
256 }
257
258 1;