-
Notifications
You must be signed in to change notification settings - Fork 1
/
redirect.php
224 lines (192 loc) · 6.81 KB
/
redirect.php
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
<?php
/**
* Handle request for uploaded content
* @return void
*/
function dxw_members_only_serve_uploads()
{
$req = dmo_strip_query($_SERVER['REQUEST_URI']);
if (
str_contains($req, '/wp-content/uploads/') || str_contains($req, '/wp-content/blogs.dir/')
) {
$upload_dir = wp_upload_dir();
$baseurl = preg_replace('%^https?://[^/]+(/.*)$%', '$1', $upload_dir['baseurl']);
$basedir = $upload_dir['basedir'];
$file = preg_replace("[^{$baseurl}]", $basedir, $req);
$realFilePath = realpath($file);
$realUploadDir = realpath($basedir);
if (is_file($file) && is_readable($file) && \Missing\Strings::startsWith($realFilePath, $realUploadDir.'/')) {
$ims_timestamp = gmdate('D, d M Y H:i:s T', filemtime($file));
if ($_SERVER['HTTP_IF_MODIFIED_SINCE'] && $ims_timestamp === $_SERVER['HTTP_IF_MODIFIED_SINCE']) {
## we don't set Etag so `If-None-Match:` doesn't need checking
http_response_code(304);
header('Last-Modified: ' . $ims_timestamp);
} else {
$mime = wp_check_filetype($file);
$type = 'application/octet-stream';
if ($mime['type'] !== false) {
$type = $mime['type'];
}
header('Accept-Ranges: none');
header('Content-Type: ' . $type);
header('Content-Length: ' . filesize($file));
header('Last-Modified: ' . $ims_timestamp);
header('X-Accel-Buffering: no');
ob_get_flush();
readfile($file);
}
exit;
}
}
}
/**
* Handle redirect for users when not logged in or viewing whitelisted content
*
* @param boolean $root Whether attempting to view root or not
* @return void
*/
function dxw_members_only_redirect($root)
{
// Redirect
if ($root) {
$redirect = get_option('dxw_members_only_redirect_root');
} else {
$redirect = get_option('dxw_members_only_redirect');
}
// %return_path%
$redirect = str_replace('%return_path%', urlencode($_SERVER['REQUEST_URI']), $redirect);
header('HTTP/1.1 303 See Other');
header('x-redirect-by: dxw-members-only');
header('Location: '.$redirect);
die();
}
function dxw_members_only_ip_in_range($ip, $range)
{
$range = trim($range);
# Handle IPv4-mapped IPv6 addresses
if (preg_match('_^::ffff:(.*)$_', $ip, $m)) {
$ip = $m[1];
}
$result = \Dxw\CIDR\IP::contains($range, $ip);
if ($result->isErr()) {
return false;
}
return $result->unwrap();
}
function dxw_members_only_current_ip_in_whitelist()
{
$ip_list = explode("\n", get_option('dxw_members_only_ip_whitelist'));
foreach ($ip_list as $ip) {
$ip = trim($ip);
if (!empty($ip) && dxw_members_only_ip_in_range($_SERVER['REMOTE_ADDR'], $ip)) {
return true;
}
}
return false;
}
function dxw_members_only_referrer_in_allow_list()
{
$referrer_list = explode("\n", get_option('dxw_members_only_referrer_allow_list'));
/*
* If there is no referrer header, or if we have no configured referrers to
* whitelist we can stop here.
*/
if (isset($_SERVER['HTTP_REFERER'])) {
foreach ($referrer_list as $referrer) {
if (!empty($referrer)) {
/*
* Add the site url to the referrer string to ensure that external
* referrers can't be used here.
*/
$whitelisted_referrer = get_site_url() . $referrer;
$referrer_check = strpos($_SERVER['HTTP_REFERER'], $whitelisted_referrer);
/*
* Check that there is a match, and that match is at the start of the referrer string.
* This is to ensure that the referrer being whitelisted can't be fooled by having
* a whitelisted referrer passed in as a parameter on the referrer string.
*/
if ($referrer_check !== false && $referrer_check == 0) {
return true;
}
}
}
}
return false;
}
add_action('init', function () {
// Fix for wp-cli
if (defined('WP_CLI_ROOT')) {
return;
}
$max_age = absint(get_option('dxw_members_only_max_age'));
$max_age_static = absint(get_option('dxw_members_only_max_age_static'));
$max_age_public = absint(get_option('dxw_members_only_max_age_public'));
do_action('dxw_members_only_redirect');
if (
defined('dxw_members_ONLY_PASSTHROUGH') ||
is_user_logged_in() ||
apply_filters('dxw_members_only_redirect', false) === true
) {
header('Cache-Control: private, max-age=' . $max_age);
dxw_members_only_serve_uploads();
return;
}
// Get path component
$path = dmo_strip_query($_SERVER['REQUEST_URI']);
// Always allow /wp-login.php
if (\Missing\Strings::endsWith($path, 'wp-login.php')) {
return;
}
// Always allow POST /wp-admin/admin-ajax.php with action=heartbeat
if (\Missing\Strings::endsWith($path, 'wp-admin/admin-ajax.php') && isset($_POST['action']) && $_POST['action'] === 'heartbeat') {
return;
}
// IP whitelist
if (dxw_members_only_current_ip_in_whitelist()) {
header('Cache-Control: private, max-age=' . $max_age_static);
dxw_members_only_serve_uploads();
return;
}
// Referrer whitelist
if (dxw_members_only_referrer_in_allow_list()) {
header('Cache-Control: private, max-age=' . $max_age_static);
dxw_members_only_serve_uploads();
return;
}
// List
$hit = false;
$list = explode("\n", get_option('dxw_members_only_list_content'));
foreach ($list as $w) {
$w = trim($w);
if (empty($w)) {
continue;
}
# /welcome => /welcome, /welcome/
if ($path === $w || $path === $w . '/') {
$hit = true;
break;
}
# /welcome/ => /welcome
if (\Missing\Strings::endsWith($w, '/') && $path === substr($w, 0, -1)) {
$hit = true;
break;
}
# /welcome/* => /welcome
if (\Missing\Strings::endsWith($w, '/*') && $path === substr($w, 0, -2)) {
$hit = true;
break;
}
# /welcome/* => /welcome/.*
if (\Missing\Strings::endsWith($w, '*') && \Missing\Strings::startsWith($path, substr($w, 0, -1))) {
$hit = true;
break;
}
}
if ($hit) {
header('Cache-Control: public, max-age=' . $max_age_public);
dxw_members_only_serve_uploads();
return;
}
header('Cache-Control: private, max-age=' . $max_age);
dxw_members_only_redirect($path === '/');
}, -99999999999);