1 #============================================================= -*-perl-*-
3 # BackupPC::CGI::View package
7 # This module implements the View action for the CGI interface.
10 # Craig Barratt <cbarratt@users.sourceforge.net>
13 # Copyright (C) 2003 Craig Barratt
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.
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.
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
29 #========================================================================
31 # Version 3.0.0alpha, released 23 Jan 2006.
33 # See http://backuppc.sourceforge.net.
35 #========================================================================
37 package BackupPC::CGI::View;
40 use BackupPC::CGI::Lib qw(:all);
41 use BackupPC::FileZIO;
45 my $Privileged = CheckPermission($In{host});
53 my $ext = $num ne "" ? ".$num" : "";
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";
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];
95 $file = "$TopDir/pc/$host/LOG$ext";
99 $file = "$LogDir/LOG$ext";
102 if ( $type ne "docs" && !$Privileged ) {
103 ErrorExit($Lang->{Only_privileged_users_can_view_log_or_config_files});
105 if ( !-f $file && -f "$file.z" ) {
109 my($contentPre, $contentSub, $contentPost);
110 $contentPre .= eval("qq{$Lang->{Log_File__file__comment}}");
112 && defined($fh = BackupPC::FileZIO->open($file, 0, $compress)) ) {
115 my $mtimeStr = $bpc->timeStamp((stat($file))[9], 1);
117 $contentPre .= eval("qq{$Lang->{Contents_of_log_file}}");
119 $contentPre .= "<pre>";
120 if ( $type eq "XferErr" || $type eq "XferErrbad"
121 || $type eq "RestoreErr"
122 || $type eq "ArchiveErr" ) {
125 # Because the content might be large, we use
126 # a sub to return the data in 64K chunks.
128 my($skipped, $c, $s);
129 while ( length($c) < 65536 ) {
130 $s = $fh->readLine();
132 $c .= eval("qq{$Lang->{skipped__skipped_lines}}")
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
157 $c .= eval("qq{$Lang->{skipped__skipped_lines}}")
160 $c .= ${EscHTML($s)} . "\n";
164 } elsif ( $linkHosts ) {
166 # Because the content might be large, we use
167 # a sub to return the data in 64K chunks.
171 while ( length($c) < 65536 ) {
172 $s = $fh->readLine();
173 last if ( $s eq "" );
176 $s =~ s/\b([\w-]+)\b/defined($Hosts->{$1})
177 ? ${HostLink($1)} : $1/eg;
182 } elsif ( $type eq "config" ) {
184 # Because the content might be large, we use
185 # a sub to return the data in 64K chunks.
189 while ( length($c) < 65536 ) {
190 $s = $fh->readLine();
191 last if ( $s eq "" );
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;
199 $s =~ s[(\$Conf\{.*?\})][
202 $s =~ s{(\W)}{sprintf("%%%02x", ord($1) )}gxe;
203 "<a href=\"?action=view&type=docs#item_$s\"><tt>$c</tt></a>"
209 } elsif ( $type eq "docs" ) {
211 # Because the content might be large, we use
212 # a sub to return the data in 64K chunks.
216 while ( length($c) < 65536 ) {
217 $s = $fh->readLine();
218 last if ( $s eq "" );
224 # Documentation has a different header and no pre or post text,
225 # so just handle it here
227 Header($Lang->{BackupPC__Documentation}, "", 0, $contentSub);
232 # Because the content might be large, we use
233 # a sub to return the data in 64K chunks.
237 while ( length($c) < 65536 ) {
238 $s = $fh->readLine();
239 last if ( $s eq "" );
248 if ( $type eq "docs" ) {
249 ErrorExit(eval("qq{$Lang->{Unable_to_open__file__configuration_problem}}"));
251 $contentPre .= eval("qq{$Lang->{_pre___Can_t_open_log_file__file}}");
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);
258 $fh->close() if ( defined($fh) );