* Split BackupPC_Admin into a set of modules, one for each major action.
[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 ( $host ne "" && $type eq "config" ) {
76         $file = "$TopDir/pc/$host/config.pl";
77         $file = "$TopDir/conf/$host.pl"
78                     if ( $host ne "config" && -f "$TopDir/conf/$host.pl"
79                                            && !-f $file );
80     } elsif ( $type eq "docs" ) {
81         $file = "$BinDir/../doc/BackupPC.html";
82         if ( open(LOG, $file) ) {
83             binmode(LOG);
84             Header($Lang->{BackupPC__Documentation});
85             print while ( <LOG> );
86             close(LOG);
87             Trailer();
88         } else {
89             ErrorExit(eval("qq{$Lang->{Unable_to_open__file__configuration_problem}}"));
90         }
91         return;
92     } elsif ( $type eq "config" ) {
93         $file = "$TopDir/conf/config.pl";
94     } elsif ( $type eq "hosts" ) {
95         $file = "$TopDir/conf/hosts";
96     } elsif ( $host ne "" ) {
97         $file = "$TopDir/pc/$host/LOG$ext";
98     } else {
99         $file = "$TopDir/log/LOG$ext";
100         $linkHosts = 1;
101     }
102     if ( !$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     Header(eval("qq{$Lang->{Backup_PC__Log_File__file}}")  );
110     print( eval ("qq{$Lang->{Log_File__file__comment}}"));
111     if ( defined($fh = BackupPC::FileZIO->open($file, 0, $compress)) ) {
112         my $mtimeStr = $bpc->timeStamp((stat($file))[9], 1);
113
114         print ( eval ("qq{$Lang->{Contents_of_log_file}}"));
115
116         print "<pre>";
117         if ( $type eq "XferErr" || $type eq "XferErrbad"
118                                 || $type eq "RestoreErr" ) {
119             my $skipped;
120             while ( 1 ) {
121                 $_ = $fh->readLine();
122                 if ( $_ eq "" ) {
123                     print(eval ("qq{$Lang->{skipped__skipped_lines}}"))
124                                                     if ( $skipped );
125                     last;
126                 }
127                 if ( /smb: \\>/
128                         || /^\s*(\d+) \(\s*\d+\.\d kb\/s\) (.*)$/
129                         || /^tar: dumped \d+ files/
130                         || /^added interface/i
131                         || /^restore tar file /i
132                         || /^restore directory /i
133                         || /^tarmode is now/i
134                         || /^Total bytes written/i
135                         || /^Domain=/i
136                         || /^Getting files newer than/i
137                         || /^Output is \/dev\/null/
138                         || /^\([\d\.]* kb\/s\) \(average [\d\.]* kb\/s\)$/
139                         || /^\s+directory \\/
140                         || /^Timezone is/
141                         || /^\.\//
142                         || /^  /
143                             ) {
144                     $skipped++;
145                     next;
146                 }
147                 print(eval("qq{$Lang->{skipped__skipped_lines}}"))
148                                                      if ( $skipped );
149                 $skipped = 0;
150                 print ${EscHTML($_)};
151             }
152         } elsif ( $linkHosts ) {
153             while ( 1 ) {
154                 $_ = $fh->readLine();
155                 last if ( $_ eq "" );
156                 my $s = ${EscHTML($_)};
157                 $s =~ s/\b([\w-]+)\b/defined($Hosts->{$1})
158                                         ? ${HostLink($1)} : $1/eg;
159                 print $s;
160             }
161         } elsif ( $type eq "config" ) {
162             while ( 1 ) {
163                 $_ = $fh->readLine();
164                 last if ( $_ eq "" );
165                 # remove any passwords and user names
166                 s/(SmbSharePasswd.*=.*['"]).*(['"])/$1$2/ig;
167                 s/(SmbShareUserName.*=.*['"]).*(['"])/$1$2/ig;
168                 s/(RsyncdPasswd.*=.*['"]).*(['"])/$1$2/ig;
169                 s/(ServerMesgSecret.*=.*['"]).*(['"])/$1$2/ig;
170                 print ${EscHTML($_)};
171             }
172         } else {
173             while ( 1 ) {
174                 $_ = $fh->readLine();
175                 last if ( $_ eq "" );
176                 print ${EscHTML($_)};
177             }
178         }
179         $fh->close();
180     } else {
181         printf( eval("qq{$Lang->{_pre___Can_t_open_log_file__file}}"));
182     }
183     print <<EOF;
184 </pre>
185 EOF
186     Trailer();
187 }
188
189 1;