Translated the five new strings.
[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.0beta2, released 11 Nov 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 ( $type eq "config" ) {
82         # Note: only works for Storage::Text
83         $file = $bpc->{storage}->ConfigPath($host);
84     } elsif ( $type eq "hosts" ) {
85         # Note: only works for Storage::Text
86         $file = $bpc->ConfDir() . "/hosts";
87         $linkHosts = 1;
88     } elsif ( $type eq "docs" ) {
89         $file = "$BinDir/../doc/BackupPC.html";
90     } elsif ( $host ne "" ) {
91         if ( !defined($In{num}) ) {
92             # get the latest LOG file
93             $file = ($bpc->sortedPCLogFiles($host))[0];
94         } else {
95             $file = "$TopDir/pc/$host/LOG$ext";
96         }
97         $linkHosts = 1;
98     } else {
99         $file = "$LogDir/LOG$ext";
100         $linkHosts = 1;
101     }
102     if ( $type ne "docs" && !$Privileged ) {
103         ErrorExit($Lang->{Only_privileged_users_can_view_log_or_config_files});
104     }
105     if ( !-f $file && -f "$file.z" ) {
106         $file .= ".z";
107         $compress = 1;
108     }
109     my($contentPre, $contentSub, $contentPost);
110     $contentPre .= eval("qq{$Lang->{Log_File__file__comment}}");
111     if ( $file ne ""
112             && defined($fh = BackupPC::FileZIO->open($file, 0, $compress)) ) {
113
114         $fh->utf8(1);
115         my $mtimeStr = $bpc->timeStamp((stat($file))[9], 1);
116
117         $contentPre .= eval("qq{$Lang->{Contents_of_log_file}}");
118
119         $contentPre .= "<pre>";
120         if ( $type eq "XferErr" || $type eq "XferErrbad"
121                                 || $type eq "RestoreErr"
122                                 || $type eq "ArchiveErr" ) {
123             $contentSub = sub {
124                 #
125                 # Because the content might be large, we use
126                 # a sub to return the data in 64K chunks.
127                 #
128                 my($skipped, $c, $s);
129                 while ( length($c) < 65536 ) {
130                     $s = $fh->readLine();
131                     if ( $s eq "" ) {
132                         $c .= eval("qq{$Lang->{skipped__skipped_lines}}")
133                                                         if ( $skipped );
134                         last;
135                     }
136                     $s =~ s/[\n\r]+//g;
137                     if ( $s =~ /smb: \\>/
138                             || $s =~ /^\s*(\d+) \(\s*\d+\.\d kb\/s\) (.*)$/
139                             || $s =~ /^tar: dumped \d+ files/
140                             || $s =~ /^\s*added interface/i
141                             || $s =~ /^\s*restore tar file /i
142                             || $s =~ /^\s*restore directory /i
143                             || $s =~ /^\s*tarmode is now/i
144                             || $s =~ /^\s*Total bytes written/i
145                             || $s =~ /^\s*Domain=/i
146                             || $s =~ /^\s*Getting files newer than/i
147                             || $s =~ /^\s*Output is \/dev\/null/
148                             || $s =~ /^\s*\([\d.,]* kb\/s\) \(average [\d\.]* kb\/s\)$/
149                             || $s =~ /^\s+directory \\/
150                             || $s =~ /^\s*Timezone is/
151                             || $s =~ /^\s*creating lame (up|low)case table/i
152                             || $s =~ /^\.\//
153                             || $s =~ /^  / ) {
154                         $skipped++;
155                         next;
156                     }
157                     $c .= eval("qq{$Lang->{skipped__skipped_lines}}")
158                                                          if ( $skipped );
159                     $skipped = 0;
160                     $c .= ${EscHTML($s)} . "\n";
161                 }
162                 return $c;
163             };
164         } elsif ( $linkHosts ) {
165             #
166             # Because the content might be large, we use
167             # a sub to return the data in 64K chunks.
168             #
169             $contentSub = sub {
170                 my($c, $s);
171                 while ( length($c) < 65536 ) {
172                     $s = $fh->readLine();
173                     last if ( $s eq "" );
174                     $s =~ s/[\n\r]+//g;
175                     $s = ${EscHTML($s)};
176                     $s =~ s/\b([\w-]+)\b/defined($Hosts->{$1})
177                                             ? ${HostLink($1)} : $1/eg;
178                     $c .= $s . "\n";
179                 }
180                 return $c;
181             };
182         } elsif ( $type eq "config" ) {
183             #
184             # Because the content might be large, we use
185             # a sub to return the data in 64K chunks.
186             #
187             $contentSub = sub {
188                 my($c, $s);
189                 while ( length($c) < 65536 ) {
190                     $s = $fh->readLine();
191                     last if ( $s eq "" );
192                     $s =~ s/[\n\r]+//g;
193                     # remove any passwords and user names
194                     $s =~ s/(SmbSharePasswd.*=.*['"]).*(['"])/$1****$2/ig;
195                     $s =~ s/(SmbShareUserName.*=.*['"]).*(['"])/$1****$2/ig;
196                     $s =~ s/(RsyncdPasswd.*=.*['"]).*(['"])/$1****$2/ig;
197                     $s =~ s/(ServerMesgSecret.*=.*['"]).*(['"])/$1****$2/ig;
198                     $s = ${EscHTML($s)};
199                     $s =~ s[(\$Conf\{.*?\})][
200                         my $c = $1;
201                         my $s = lc($c);
202                         $s =~ s{(\W)}{sprintf("%%%02x", ord($1) )}gxe;
203                         "<a href=\"?action=view&type=docs#item_$s\"><tt>$c</tt></a>"
204                     ]eg;
205                     $c .= $s . "\n";
206                 }
207                 return $c;
208             };
209         } elsif ( $type eq "docs" ) {
210             #
211             # Because the content might be large, we use
212             # a sub to return the data in 64K chunks.
213             #
214             $contentSub = sub {
215                 my($c, $s);
216                 while ( length($c) < 65536 ) {
217                     $s = $fh->readLine();
218                     last if ( $s eq "" );
219                     $c .= $s;
220                 }
221                 return $c;
222             };
223             #
224             # Documentation has a different header and no pre or post text,
225             # so just handle it here
226             #
227             Header($Lang->{BackupPC__Documentation}, "", 0, $contentSub);
228             Trailer();
229             return;
230         } else {
231             #
232             # Because the content might be large, we use
233             # a sub to return the data in 64K chunks.
234             #
235             $contentSub = sub {
236                 my($c, $s);
237                 while ( length($c) < 65536 ) {
238                     $s = $fh->readLine();
239                     last if ( $s eq "" );
240                     $s =~ s/[\n\r]+//g;
241                     $s = ${EscHTML($s)};
242                     $c .= $s . "\n";
243                 }
244                 return $c;
245             };
246         }
247     } else {
248         if ( $type eq "docs" ) {
249             ErrorExit(eval("qq{$Lang->{Unable_to_open__file__configuration_problem}}"));
250         }
251         $contentPre .= eval("qq{$Lang->{_pre___Can_t_open_log_file__file}}");
252     }
253     $contentPost .= "</pre>\n" if ( $type ne "docs" );
254     Header(eval("qq{$Lang->{Backup_PC__Log_File__file}}"),
255                     $contentPre, !-f "$TopDir/pc/$host/backups",
256                     $contentSub, $contentPost);
257     Trailer();
258     $fh->close() if ( defined($fh) );
259 }
260
261 1;