1 #============================================================= -*-perl-*-
3 # BackupPC::CGI::RestoreFile package
7 # This module implements the RestoreFile action for the CGI interface.
10 # Craig Barratt <cbarratt@users.sourceforge.net>
13 # Copyright (C) 2003-2009 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.2.0, released 31 Jul 2010.
33 # See http://backuppc.sourceforge.net.
35 #========================================================================
37 package BackupPC::CGI::RestoreFile;
40 use BackupPC::CGI::Lib qw(:all);
41 use BackupPC::FileZIO;
42 use BackupPC::Attrib qw(:all);
44 use Encode qw/from_to decode_utf8/;
48 restoreFile($In{host}, $In{num}, $In{share}, $In{dir});
53 my($host, $num, $share, $dir, $skipHardLink, $origName) = @_;
54 my($Privileged) = CheckPermission($host);
57 # Some common content (media) types from www.iana.org (via MIME::Types).
59 my $Ext2ContentType = {
60 'asc' => 'text/plain',
61 'avi' => 'video/x-msvideo',
63 'book' => 'application/x-maker',
65 'cpp' => 'text/plain',
66 'csh' => 'application/x-csh',
67 'csv' => 'text/comma-separated-values',
69 'deb' => 'application/x-debian-package',
70 'doc' => 'application/msword',
71 'dot' => 'application/msword',
73 'dvi' => 'application/x-dvi',
74 'eps' => 'application/postscript',
75 'fb' => 'application/x-maker',
76 'fbdoc'=> 'application/x-maker',
77 'fm' => 'application/x-maker',
78 'frame'=> 'application/x-maker',
79 'frm' => 'application/x-maker',
81 'gtar' => 'application/x-gtar',
82 'gz' => 'application/x-gzip',
84 'hpp' => 'text/plain',
86 'html' => 'text/html',
87 'htmlx'=> 'text/html',
89 'iges' => 'model/iges',
90 'igs' => 'model/iges',
91 'jpeg' => 'image/jpeg',
92 'jpe' => 'image/jpeg',
93 'jpg' => 'image/jpeg',
94 'js' => 'application/x-javascript',
95 'latex'=> 'application/x-latex',
96 'maker'=> 'application/x-maker',
97 'mid' => 'audio/midi',
98 'midi' => 'audio/midi',
99 'movie'=> 'video/x-sgi-movie',
100 'mov' => 'video/quicktime',
101 'mp2' => 'audio/mpeg',
102 'mp3' => 'audio/mpeg',
103 'mpeg' => 'video/mpeg',
104 'mpg' => 'video/mpeg',
105 'mpp' => 'application/vnd.ms-project',
106 'pdf' => 'application/pdf',
107 'pgp' => 'application/pgp-signature',
108 'php' => 'application/x-httpd-php',
109 'pht' => 'application/x-httpd-php',
110 'phtml'=> 'application/x-httpd-php',
111 'png' => 'image/png',
112 'ppm' => 'image/x-portable-pixmap',
113 'ppt' => 'application/powerpoint',
114 'ppt' => 'application/vnd.ms-powerpoint',
115 'ps' => 'application/postscript',
116 'qt' => 'video/quicktime',
117 'rgb' => 'image/x-rgb',
118 'rtf' => 'application/rtf',
120 'shar' => 'application/x-shar',
121 'shtml'=> 'text/html',
122 'swf' => 'application/x-shockwave-flash',
123 'tex' => 'application/x-tex',
124 'texi' => 'application/x-texinfo',
125 'texinfo'=> 'application/x-texinfo',
126 'tgz' => 'application/x-gtar',
127 'tiff' => 'image/tiff',
128 'tif' => 'image/tiff',
129 'txt' => 'text/plain',
130 'vcf' => 'text/x-vCard',
131 'vrml' => 'model/vrml',
132 'wav' => 'audio/x-wav',
133 'wmls' => 'text/vnd.wap.wmlscript',
134 'wml' => 'text/vnd.wap.wml',
135 'wrl' => 'model/vrml',
136 'xls' => 'application/vnd.ms-excel',
138 'xwd' => 'image/x-xwindowdump',
139 'z' => 'application/x-compress',
140 'zip' => 'application/zip',
141 %{$Conf{CgiExt2ContentType}}, # add site-specific values
143 if ( !$Privileged ) {
144 ErrorExit(eval("qq{$Lang->{Only_privileged_users_can_restore_backup_files2}}"));
146 $bpc->ConfigRead($host);
147 %Conf = $bpc->Conf();
149 ErrorExit($Lang->{Empty_host_name}) if ( $host eq "" );
151 $dir = "/" if ( $dir eq "" );
152 my @Backups = $bpc->BackupInfoRead($host);
153 my $view = BackupPC::View->new($bpc, $host, \@Backups);
154 my $a = $view->fileAttrib($num, $share, $dir);
155 if ( $dir =~ m{(^|/)\.\.(/|$)} || !defined($a) ) {
156 $dir = decode_utf8($dir);
157 ErrorExit("Can't restore bad file ${EscHTML($dir)} ($num, $share)");
159 my $f = BackupPC::FileZIO->open($a->{fullPath}, 0, $a->{compress});
160 if ( !defined($f) ) {
161 my $fullPath = decode_utf8($a->{fullPath});
162 ErrorExit("Unable to open file ${EscHTML($fullPath)} ($num, $share)");
165 if ( !$skipHardLink && $a->{type} == BPC_FTYPE_HARDLINK ) {
167 # hardlinks should look like the file they point to
170 while ( $f->read(\$data, 65536) > 0 ) {
174 $linkName =~ s/^\.\///;
175 restoreFile($host, $num, $share, $linkName, 1, $dir);
178 $bpc->ServerMesg("log User $User recovered file $host/$num:$share/$dir ($a->{fullPath})");
179 $dir = $origName if ( defined($origName) );
180 my $ext = $1 if ( $dir =~ /\.([^\/\.]+)$/ );
181 my $contentType = $Ext2ContentType->{lc($ext)}
182 || "application/octet-stream";
183 my $fileName = $1 if ( $dir =~ /.*\/(.*)/ );
184 $fileName =~ s/"/\\"/g;
186 print "Content-Type: $contentType\r\n";
187 print "Content-Transfer-Encoding: binary\r\n";
189 if ( $ENV{HTTP_USER_AGENT} =~ /\bmsie\b/i
190 && $ENV{HTTP_USER_AGENT} !~ /\bopera\b/i ) {
192 # Convert to cp1252 for MS IE. TODO: find a way to get IE
193 # to accept UTF8 encoding. Firefox accepts inline encoding
194 # using the "=?UTF-8?B?base64?=" format, but IE doesn't.
196 from_to($fileName, "utf8", "cp1252")
197 if ( $Conf{ClientCharset} ne "" );
199 print "Content-Disposition: attachment; filename=\"$fileName\"\r\n\r\n";
200 while ( $f->read(\$data, 1024 * 1024) > 0 ) {