-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmediafirewall2.php
More file actions
152 lines (128 loc) · 4.48 KB
/
mediafirewall2.php
File metadata and controls
152 lines (128 loc) · 4.48 KB
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
<?php
// Media Firewall - Serves media images, after checking access
//
// webtrees: Web based Family History software
// Copyright (C) 2014 webtrees development team.
//
// Derived from PhpGedView
// Copyright (C) 2002 to 2009 PGV Development Team.
//
// 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.
//
// 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. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
use Fisharebest\Webtrees\Auth;
use Fisharebest\Webtrees\Filter;
use Fisharebest\Webtrees\Site;
use Fisharebest\Webtrees\Tree;
use Fisharebest\Webtrees\I18N;
define('WT_SCRIPT_NAME', 'mediafirewall2.php');
require './includes/session.php';
$murl = Filter::get('file');
// Send a “Not found” error
function send404AndExit() {
header('HTTP/1.0 404 Not Found');
header('Status: 404 Not Found');
header('Content-Type: text/html');
echo I18N::translate('The file was not found');
exit;
}
// Redirect to login page
function send403AndExit() {
header('Location: ' . WT_LOGIN_URL . '?url=' . rawurlencode(WT_SCRIPT_NAME . (isset($_SERVER['QUERY_STRING']) ? '?' . $_SERVER['QUERY_STRING'] : '')), true, 301);
exit;
}
function _detectFileMimeType($file)
{
$type = null;
// First try with fileinfo functions
if (function_exists('finfo_open')) {
$_fileInfoDb = @finfo_open(FILEINFO_MIME);
if ($_fileInfoDb) {
$type = finfo_file($_fileInfoDb, $file);
}
} elseif (function_exists('mime_content_type')) {
$type = mime_content_type($file);
}
// Fallback to the default application/octet-stream
if (! $type) {
$type = 'application/octet-stream';
}
return $type;
}
// Only registered users may view files
if (!Auth::check()) {
send403AndExit();
}
$MEDIA_DIRECTORY = Tree::findByName(Site::getPreference('DEFAULT_GEDCOM'))->getPreference('MEDIA_DIRECTORY');
//only published files should be accessible
require WT_DATA_DIR.'/media_config.ini.php';
$exists = false;
foreach ($media_special_trees as $file) {
if ($file->Filename == $murl) {
$exists = true;
break;
}
}
if (!$exists) {
send404AndExit();
}
$serverFilename = WT_DATA_DIR . $MEDIA_DIRECTORY . $murl;
if (!file_exists($serverFilename)) {
send404AndExit();
}
$mimetype = _detectFileMimeType($serverFilename);
$protocol = $_SERVER['SERVER_PROTOCOL']; // determine if we are using HTTP/1.0 or HTTP/1.1
$filetime = @filemtime($serverFilename);
$filetimeHeader = gmdate('D, d M Y H:i:s', $filetime) . ' GMT';
$expireOffset = 3600 * 24; // tell browser to cache this image for 24 hours
$expireHeader = gmdate('D, d M Y H:i:s', WT_TIMESTAMP + $expireOffset) . ' GMT';
// parse IF_MODIFIED_SINCE header from client
$if_modified_since = 'x';
if (isset($_SERVER['HTTP_IF_MODIFIED_SINCE'])) {
$if_modified_since = preg_replace('/;.*$/', '', $_SERVER['HTTP_IF_MODIFIED_SINCE']);
}
// parse IF_NONE_MATCH header from client
$if_none_match = 'x';
if (isset($_SERVER['HTTP_IF_NONE_MATCH'])) {
$if_none_match = str_replace('"', '', $_SERVER['HTTP_IF_NONE_MATCH']);
}
// add caching headers. allow browser to cache file, but not proxy
header('Last-Modified: ' . $filetimeHeader);
header('Expires: ' . $expireHeader);
header('Cache-Control: max-age=' . $expireOffset . ', s-maxage=0, proxy-revalidate');
// if this file is already in the user’s cache, don’t resend it
if (($if_modified_since == $filetimeHeader)) {
header($protocol . ' 304 Not Modified');
exit;
}
// send headers for the image
header('Content-Type: ' . $mimetype);
header('Content-Disposition: filename="' . addslashes(basename($murl)) . '"');
// determine filesize of image (could be original or watermarked version)
$filesize = filesize($serverFilename);
// set content-length header, send file
header('Content-Length: ' . $filesize);
// Some servers disable fpassthru() and readfile()
if (function_exists('readfile')) {
readfile($serverFilename);
} else {
$fp = fopen($serverFilename, 'rb');
if (function_exists('fpassthru')) {
fpassthru($fp);
} else {
while (!feof($fp)) {
echo fread($fp, 65536);
}
}
fclose($fp);
}