/
XSendFileContrib.pm
205 lines (159 loc) · 5.8 KB
/
XSendFileContrib.pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
# Module of Foswiki - The Free and Open Source Wiki, http://foswiki.org/
#
# Copyright (C) 2013 Michael Daum http://michaeldaumconsulting.com
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version. For
# more details read LICENSE in the root of this distribution.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
#
# As per the GPL, removal of this notice is prohibited.
package Foswiki::Contrib::XSendFileContrib;
use strict;
use warnings;
use Encode ();
use Foswiki::Sandbox ();
use Foswiki::Func ();
use File::MMagic ();
our $VERSION = '2.10';
our $RELEASE = '2.10';
our $SHORTDESCRIPTION = 'A viewfile replacement to send static files efficiently';
our $mimeTypeInfo;
our $mmagic;
sub xsendfile {
my $session = shift;
my $request = $session->{request};
my $response = $session->{response};
my $web = $session->{webName};
my $topic = $session->{topicName};
my $fileName = $request->param('filename');
my $dispositionMode = $request->param('mode') || 'inline';
unless (defined $fileName) {
my $pathInfo = $request->path_info();
my @path = split(/\/+/, $pathInfo);
shift(@path) unless $path[0];
# work out the web, topic and filename
my @web;
my $pel = Foswiki::Sandbox::untaint($path[0], \&Foswiki::Sandbox::validateWebName);
while ($pel && Foswiki::Func::webExists(join('/', @web, $pel))) {
push(@web, $pel);
shift(@path);
$pel = Foswiki::Sandbox::untaint($path[0], \&Foswiki::Sandbox::validateWebName);
}
$web = join('/', @web);
unless ($web) {
$response->status(404);
$response->print("404 - no web found\n");
return;
}
# Must set the web name, otherwise plugins may barf if
# they try to manipulate the topic context when an oops is generated.
$session->{webName} = $web;
# The next element on the path has to be the topic name
$topic = Foswiki::Sandbox::untaint(shift(@path), \&Foswiki::Sandbox::validateTopicName);
unless ($topic) {
$response->status(404);
$response->print("404 - no topic found\n");
return;
}
# See comment about webName above
$session->{topicName} = $topic;
# What's left in the path is the attachment name.
$fileName = join('/', @path);
}
# not found
unless (defined $fileName) {
$response->status(404);
$response->print("404 - no file found\n");
return;
}
#print STDERR "web=$web, topic=$topic, fileName=$fileName\n";
$fileName = Foswiki::urlDecode($fileName);
$fileName = Encode::decode_utf8($fileName);
$fileName = Foswiki::Sandbox::untaint($fileName, \&Foswiki::Sandbox::validateAttachmentName);
# invalid
unless (defined $fileName) {
$response->status(404);
$response->print("404 - file not valid\n");
return;
}
my $topicObject = Foswiki::Meta->new($session, $web, $topic);
# not found
unless ($topicObject->existsInStore()) {
$response->status(404);
$response->print("404 - topic $web.$topic does not exist\n");
return;
}
# not found
unless ($topicObject->hasAttachment($fileName)) {
$response->status(404);
$response->print("404 - attachment $fileName not found at $web.$topic\n");
return;
}
# unauthorized
unless (checkAccess($topicObject, $fileName, $session->{user})) {
$response->status(401);
$response->print("401 - access denied\n");
return;
}
# construct file path to protected location
my $location = $Foswiki::cfg{XSendFileContrib}{Location} || $Foswiki::cfg{PubDir};
my $filePath = $location.'/'.$web.'/'.$topic.'/'.$fileName;
# ok
my $headerName = $Foswiki::cfg{XSendFileContrib}{Header} || 'X-LIGHTTPD-send-file';
$fileName = Encode::encode_utf8($fileName);
$filePath = Encode::encode_utf8($filePath);
#print STDERR "fileName=$fileName\n";
#print STDERR "filePath=$filePath\n";
$response->header(
-status => 200,
-type => mimeTypeOfFile($filePath),
-content_disposition => "$dispositionMode; filename=\"$fileName\"",
$headerName => $filePath,
);
# $response->print("OK");
return;
}
sub checkAccess {
my ($topicObject, $fileName, $user) = @_;
if (defined $Foswiki::cfg{XSendFileContrib}{AccessRules}) {
my $web = $topicObject->web;
my $topic = $topicObject->topic;
foreach my $rule (@{$Foswiki::cfg{XSendFileContrib}{AccessRules}}) {
#print STDERR "rule: web=$rule->{web}, topic=$rule->{topic}, file=$rule->{file}, requiredAccess=$rule->{requiredAccess}\n";
if ((!defined($rule->{web}) || $web =~ /^$rule->{web}$/) &&
(!defined($rule->{topic}) || $topic =~ /^$rule->{topic}$/) &&
(!defined($rule->{file}) || $fileName =~ /^$rule->{file}$/)) {
return 1 if !defined($rule->{requiredAccess}) || $rule->{requiredAccess} eq "";
return $topicObject->haveAccess($rule->{requiredAccess}, $user);
}
}
}
# fallback
return $topicObject->haveAccess("VIEW", $user);
}
sub mimeTypeOfFile {
my $fileName = shift;
if ($fileName && $fileName =~ /\.([^.]+)$/) {
my $suffix = $1;
$mimeTypeInfo = Foswiki::Func::readFile($Foswiki::cfg{MimeTypesFileName})
unless defined $mimeTypeInfo;
if ($mimeTypeInfo =~ /^([^#]\S*).*?\s$suffix(?:\s|$)/im) {
return $1;
}
}
$mmagic = File::MMagic->new() unless defined $mmagic;
my $mimeType = $mmagic->checktype_filename($fileName);
if (defined $mimeType && $mimeType ne "x-system/x-error") {
#print STDERR "mmagic says $mimeType to $fileName\n";
return $mimeType;
}
#print STDERR "unknown mime type of $fileName\n";
return 'application/octet-stream';
}
1;