Skip to content

Commit 3ffecbb

Browse files
authored
Merge pull request #25 from dxw/feature/add-http-referrer-allow-list-option
Feature/add http referrer allow list option
2 parents b8b10ae + f733765 commit 3ffecbb

6 files changed

+59
-63
lines changed

composer.json

+1-2
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,7 @@
55
},
66
"require-dev": {
77
"dxw/phar-install": "^1.1",
8-
"friendsofphp/php-cs-fixer": "^2.0",
9-
"kahlan/kahlan": "^4.7"
8+
"friendsofphp/php-cs-fixer": "^2.0"
109
},
1110
"scripts": {
1211
"post-update-cmd": "vendor/bin/phar-install"

composer.lock

+1-59
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dxw-members-only.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* Plugin Name: dxw Members Only
44
* Plugin URI: http://dxw.com
55
* Description: Make your WordPress site visible to signed-in users only with the added ability to whitelist specific content for access by all users.
6-
* Version: 4.0.4
6+
* Version: 4.1.0
77
* Author: dxw
88
* Author URI: http://dxw.com
99
* Text Domain: dxwmembersonly

redirect.php

+37
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,36 @@ function dxw_members_only_current_ip_in_whitelist()
8383
return false;
8484
}
8585

86+
function dxw_members_only_referrer_in_allow_list()
87+
{
88+
$referrer_list = explode("\n", get_option('dxw_members_only_referrer_allow_list'));
89+
/*
90+
* If there is no referrer header, or if we have no configured referrers to
91+
* whitelist we can stop here.
92+
*/
93+
if (isset($_SERVER['HTTP_REFERER'])) {
94+
foreach ($referrer_list as $referrer) {
95+
if (!empty($referrer)) {
96+
/*
97+
* Add the site url to the referrer string to ensure that external
98+
* referrers can't be used here.
99+
*/
100+
$whitelisted_referrer = get_site_url() . $referrer;
101+
$referrer_check = strpos($_SERVER['HTTP_REFERER'], $whitelisted_referrer);
102+
/*
103+
* Check that there is a match, and that match is at the start of the referrer string.
104+
* This is to ensure that the referrer being whitelisted can't be fooled by having
105+
* a whitelisted referrer passed in as a parameter on the referrer string.
106+
*/
107+
if ($referrer_check !== false && $referrer_check == 0) {
108+
return true;
109+
}
110+
}
111+
}
112+
}
113+
return false;
114+
}
115+
86116
add_action('init', function () {
87117
// Fix for wp-cli
88118
if (defined('WP_CLI_ROOT')) {
@@ -122,6 +152,13 @@ function dxw_members_only_current_ip_in_whitelist()
122152
return;
123153
}
124154

155+
// Referrer whitelist
156+
if (dxw_members_only_referrer_in_allow_list()) {
157+
header('Cache-Control: private, max-age=' . $max_age);
158+
dxw_members_only_serve_uploads();
159+
return;
160+
}
161+
125162
// List
126163
$hit = false;
127164
$list = explode("\n", get_option('dxw_members_only_list_content'));

settings.php

+19-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
function dxw_members_only_metasettings()
66
{
77
$ms = new dmometasettings(__FILE__, 'dxw_members_only');
8-
$ms->add_settings(__('dxw Members Only', 'dxwmembersonly'), ['list_type', 'list_content', 'ip_whitelist', 'redirect', 'redirect_root', 'upload_default', 'max_age'], 'dxw_members_only_options_page');
8+
$ms->add_settings(__('dxw Members Only', 'dxwmembersonly'), ['list_type', 'list_content', 'ip_whitelist', 'referrer_allow_list', 'redirect', 'redirect_root', 'upload_default', 'max_age'], 'dxw_members_only_options_page');
99
}
1010

1111
/**
@@ -53,6 +53,24 @@ function dxw_members_only_options_page()
5353

5454
</table>
5555

56+
<h3><?php _e('Referrer Allow list') ?></h3>
57+
<p><?php _e('Enter a list of internal referrers to whitelist.', 'dxwmembersonly') ?></p>
58+
<p><?php _e('This is for enabling certain plugins such as Nelio AB to function correctly, do not use unless required', 'dxwmembersonly') ?></p>
59+
60+
<table class="form-table">
61+
62+
<tr valign="top">
63+
<th scope="row"><label for="dxw_members_only_referrer_allow_list"><?php _e('List of referrers', 'dxwmembersonly') ?></label></th>
64+
<td>
65+
<textarea cols="30" rows="5" name="dxw_members_only_referrer_allow_list" id="dxw_members_only_referrer_allow_list" class="large-text code"><?php echo esc_html(get_option('dxw_members_only_referrer_allow_list')) ?></textarea>
66+
<br>
67+
<span class="description"><?php _e('One address per line, do not include the domain (eg /admin.php?page=test)', 'dxwmembersonly') ?></span>
68+
</td>
69+
</tr>
70+
71+
</table>
72+
<?php echo get_option('dxw_members_only_referrer_whitelist'); ?>
73+
5674
<h3><?php _e('Redirection', 'dxwmembersonly') ?></h3>
5775
<p><?php _e('In both the following options, <code>%return_path%</code> will be converted to the URL that was originally visited. i.e. <code>/wp-login.php?redirect_to=http://example.com/private-page</code>', 'dxwmembersonly') ?></p>
5876

vendor.phar

228 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)