4f3070ae454b21935a1fa72f40fd5fe016fdb76a
[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 3 Jul 2003.
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             my $skipped;
126             while ( 1 ) {
127                 $_ = $fh->readLine();
128                 if ( $_ eq "" ) {
129                     $content .= (eval ("qq{$Lang->{skipped__skipped_lines}}"))
130                                                     if ( $skipped );
131                     last;
132                 }
133                 if ( /smb: \\>/
134                         || /^\s*(\d+) \(\s*\d+\.\d kb\/s\) (.*)$/
135                         || /^tar: dumped \d+ files/
136                         || /^added interface/i
137                         || /^restore tar file /i
138                         || /^restore directory /i
139                         || /^tarmode is now/i
140                         || /^Total bytes written/i
141                         || /^Domain=/i
142                         || /^Getting files newer than/i
143                         || /^Output is \/dev\/null/
144                         || /^\([\d\.]* kb\/s\) \(average [\d\.]* kb\/s\)$/
145                         || /^\s+directory \\/
146                         || /^Timezone is/
147                         || /^\.\//
148                         || /^  /
149                             ) {
150                     $skipped++;
151                     next;
152                 }
153                 $content .= (eval("qq{$Lang->{skipped__skipped_lines}}"))
154                                                      if ( $skipped );
155                 $skipped = 0;
156                 $content .= ${EscHTML($_)};
157             }
158         } elsif ( $linkHosts ) {
159             while ( 1 ) {
160                 $_ = $fh->readLine();
161                 last if ( $_ eq "" );
162                 my $s = ${EscHTML($_)};
163                 $s =~ s/\b([\w-]+)\b/defined($Hosts->{$1})
164                                         ? ${HostLink($1)} : $1/eg;
165                 $content .= $s;
166             }
167         } elsif ( $type eq "config" ) {
168             while ( 1 ) {
169                 $_ = $fh->readLine();
170                 last if ( $_ eq "" );
171                 # remove any passwords and user names
172                 s/(SmbSharePasswd.*=.*['"]).*(['"])/$1$2/ig;
173                 s/(SmbShareUserName.*=.*['"]).*(['"])/$1$2/ig;
174                 s/(RsyncdPasswd.*=.*['"]).*(['"])/$1$2/ig;
175                 s/(ServerMesgSecret.*=.*['"]).*(['"])/$1$2/ig;
176                 $content .= ${EscHTML($_)};
177             }
178         } else {
179             while ( 1 ) {
180                 $_ = $fh->readLine();
181                 last if ( $_ eq "" );
182                 $content .= ${EscHTML($_)};
183             }
184         }
185         $fh->close();
186     } else {
187         $content .= ( eval("qq{$Lang->{_pre___Can_t_open_log_file__file}}"));
188     }
189     $content .= <<EOF;
190 </pre>
191 EOF
192     Header(eval("qq{$Lang->{Backup_PC__Log_File__file}}"), $content);
193     Trailer();
194 }
195
196 1;