d60196b3452a641161a1ef85c5afeedb12c86477
[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 2.1.0_CVS, released 8 Feb 2004.
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}}")) if ( $num ne "" && $num !~ /^\d+$/ );
56     if ( $type eq "XferLOG" ) {
57         $file = "$TopDir/pc/$host/SmbLOG$ext";
58         $file = "$TopDir/pc/$host/XferLOG$ext" if ( !-f $file && !-f "$file.z");
59     } elsif ( $type eq "XferLOGbad" ) {
60         $file = "$TopDir/pc/$host/SmbLOG.bad";
61         $file = "$TopDir/pc/$host/XferLOG.bad" if ( !-f $file && !-f "$file.z");
62     } elsif ( $type eq "XferErrbad" ) {
63         $file = "$TopDir/pc/$host/SmbLOG.bad";
64         $file = "$TopDir/pc/$host/XferLOG.bad" if ( !-f $file && !-f "$file.z");
65         $comment = $Lang->{Extracting_only_Errors};
66     } elsif ( $type eq "XferErr" ) {
67         $file = "$TopDir/pc/$host/SmbLOG$ext";
68         $file = "$TopDir/pc/$host/XferLOG$ext" if ( !-f $file && !-f "$file.z");
69         $comment = $Lang->{Extracting_only_Errors};
70     } elsif ( $type eq "RestoreLOG" ) {
71         $file = "$TopDir/pc/$host/RestoreLOG$ext";
72     } elsif ( $type eq "RestoreErr" ) {
73         $file = "$TopDir/pc/$host/RestoreLOG$ext";
74         $comment = $Lang->{Extracting_only_Errors};
75     } elsif ( $type eq "ArchiveLOG" ) {
76         $file = "$TopDir/pc/$host/ArchiveLOG$ext";
77     } elsif ( $type eq "ArchiveErr" ) {
78         $file = "$TopDir/pc/$host/ArchiveLOG$ext";
79         $comment = $Lang->{Extracting_only_Errors};
80     } elsif ( $host ne "" && $type eq "config" ) {
81         $file = "$TopDir/pc/$host/config.pl";
82         $file = "$TopDir/conf/$host.pl"
83                     if ( $host ne "config" && -f "$TopDir/conf/$host.pl"
84                                            && !-f $file );
85     } elsif ( $type eq "docs" ) {
86         $file = "$BinDir/../doc/BackupPC.html";
87         if ( open(LOG, $file) ) {
88             binmode(LOG);
89             my $content;
90             $content .= $_ while ( <LOG> );
91             close(LOG);
92             Header($Lang->{BackupPC__Documentation}, $content);
93             Trailer();
94         } else {
95             ErrorExit(eval("qq{$Lang->{Unable_to_open__file__configuration_problem}}"));
96         }
97         return;
98     } elsif ( $type eq "config" ) {
99         $file = "$TopDir/conf/config.pl";
100     } elsif ( $type eq "hosts" ) {
101         $file = "$TopDir/conf/hosts";
102     } elsif ( $host ne "" ) {
103         $file = "$TopDir/pc/$host/LOG$ext";
104     } else {
105         $file = "$TopDir/log/LOG$ext";
106         $linkHosts = 1;
107     }
108     if ( !$Privileged ) {
109         ErrorExit($Lang->{Only_privileged_users_can_view_log_or_config_files});
110     }
111     if ( !-f $file && -f "$file.z" ) {
112         $file .= ".z";
113         $compress = 1;
114     }
115     my $content;
116     $content .= eval ("qq{$Lang->{Log_File__file__comment}}");
117     if ( defined($fh = BackupPC::FileZIO->open($file, 0, $compress)) ) {
118         my $mtimeStr = $bpc->timeStamp((stat($file))[9], 1);
119
120         $content .= ( eval ("qq{$Lang->{Contents_of_log_file}}"));
121
122         $content .= "<pre>";
123         if ( $type eq "XferErr" || $type eq "XferErrbad"
124                                 || $type eq "RestoreErr"
125                                 || $type eq "ArchiveErr" ) {
126             my $skipped;
127             while ( 1 ) {
128                 $_ = $fh->readLine();
129                 if ( $_ eq "" ) {
130                     $content .= (eval ("qq{$Lang->{skipped__skipped_lines}}"))
131                                                     if ( $skipped );
132                     last;
133                 }
134                 if ( /smb: \\>/
135                         || /^\s*(\d+) \(\s*\d+\.\d kb\/s\) (.*)$/
136                         || /^tar: dumped \d+ files/
137                         || /^\s*added interface/i
138                         || /^\s*restore tar file /i
139                         || /^\s*restore directory /i
140                         || /^\s*tarmode is now/i
141                         || /^\s*Total bytes written/i
142                         || /^\s*Domain=/i
143                         || /^\s*Getting files newer than/i
144                         || /^\s*Output is \/dev\/null/
145                         || /^\s*\([\d.,]* kb\/s\) \(average [\d\.]* kb\/s\)$/
146                         || /^\s+directory \\/
147                         || /^\s*Timezone is/
148                         || /^\s*creating lame (up|low)case table/i
149                         || /^\.\//
150                         || /^  /
151                             ) {
152                     $skipped++;
153                     next;
154                 }
155                 $content .= (eval("qq{$Lang->{skipped__skipped_lines}}"))
156                                                      if ( $skipped );
157                 $skipped = 0;
158                 $content .= ${EscHTML($_)};
159             }
160         } elsif ( $linkHosts ) {
161             while ( 1 ) {
162                 $_ = $fh->readLine();
163                 last if ( $_ eq "" );
164                 my $s = ${EscHTML($_)};
165                 $s =~ s/\b([\w-]+)\b/defined($Hosts->{$1})
166                                         ? ${HostLink($1)} : $1/eg;
167                 $content .= $s;
168             }
169         } elsif ( $type eq "config" ) {
170             while ( 1 ) {
171                 $_ = $fh->readLine();
172                 last if ( $_ eq "" );
173                 # remove any passwords and user names
174                 s/(SmbSharePasswd.*=.*['"]).*(['"])/$1$2/ig;
175                 s/(SmbShareUserName.*=.*['"]).*(['"])/$1$2/ig;
176                 s/(RsyncdPasswd.*=.*['"]).*(['"])/$1$2/ig;
177                 s/(ServerMesgSecret.*=.*['"]).*(['"])/$1$2/ig;
178                 $content .= ${EscHTML($_)};
179             }
180         } else {
181             while ( 1 ) {
182                 $_ = $fh->readLine();
183                 last if ( $_ eq "" );
184                 $content .= ${EscHTML($_)};
185             }
186         }
187         $fh->close();
188     } else {
189         $content .= ( eval("qq{$Lang->{_pre___Can_t_open_log_file__file}}"));
190     }
191     $content .= <<EOF;
192 </pre>
193 EOF
194     Header(eval("qq{$Lang->{Backup_PC__Log_File__file}}"),
195                     $content, !-f "$TopDir/pc/$host/backups" );
196     Trailer();
197 }
198
199 1;