Skip to content

Commit afdadfc

Browse files
Benjamin PickBenjamin Pick
Benjamin Pick
authored and
Benjamin Pick
committed
manual reload
1 parent a37fc28 commit afdadfc

File tree

4 files changed

+125
-8
lines changed

4 files changed

+125
-8
lines changed

admin-ui.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,33 @@ function geoip_detect_option_page() {
252252
}
253253

254254
function geoip_detect_option_client_ip_page() {
255+
if (!is_admin() || !current_user_can('manage_options'))
256+
return;
257+
258+
$last_update = get_option('geoip_detect2_dynamic-reverse-proxies_last_updated');
259+
260+
$successMessage = '';
261+
$errorMessage = '';
262+
$action = isset($_POST['action']) ? sanitize_key($_POST['action']) : '';
263+
if (geoip_detect_verify_nonce($action)) {
264+
switch($action)
265+
{
266+
case 'reload-proxies':
267+
$manager = \YellowTree\GeoipDetect\DynamicReverseProxies\getDataManager();
268+
if ($manager) {
269+
$success = $manager->reload(true);
270+
if ($success) {
271+
$successMessage = 'Dynamic Reverse Proxy list was reloaded successfully.';
272+
} else {
273+
$errorMessage = 'There was an error reloading the dynamic reverse proxy list.';
274+
}
275+
} else {
276+
$errorMessage = 'No DataManager found.';
277+
}
278+
break;
279+
}
280+
}
281+
255282
include_once(GEOIP_PLUGIN_DIR . '/views/client-ip.php');
256283
}
257284

lib/dynamic-reverse-proxies/abstract.php

Lines changed: 73 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,34 +2,43 @@
22

33
namespace YellowTree\GeoipDetect\DynamicReverseProxies;
44

5-
require_once(__DIR__ . '/aws.php');
6-
require_once(__DIR__ . '/cloudflare.php');
7-
85
interface DataProvider {
96
function getIps() : array;
107
}
118

12-
function init() : void {
9+
require_once(__DIR__ . '/aws.php');
10+
require_once(__DIR__ . '/cloudflare.php');
11+
12+
13+
function initFilters() : void {
1314
$enabled = get_option('geoip-detect-dynamic_reverse_proxies', 0);
1415
if (!$enabled) return;
1516

1617
add_filter('geoip_detect2_client_ip_whitelist', __NAMESPACE__ . '\addDynamicIps');
1718
add_filter('geoip_detect2_client_ip_use_whitelist', '__return_true');
1819
}
1920
add_filter('plugins_loaded', function() {
20-
init();
21+
initFilters();
2122
});
2223

24+
2325
function addDynamicIps($ipList = []) : array {
24-
$type = get_option('geoip-detect-dynamic_reverse_proxy_type', '');
25-
if (!$type) return $ipList;
26+
$manager = getDataManager();
27+
if (!$manager) return $ipList;
2628

27-
$manager = new DataManager($type);
2829
$ipList = array_merge($ipList, $manager->getIpsFromCache());
2930

3031
return $ipList;
3132
}
3233

34+
35+
function getDataManager() : ?DataManager {
36+
$type = get_option('geoip-detect-dynamic_reverse_proxy_type', '');
37+
if (!$type) return null;
38+
39+
return new DataManager($type);
40+
}
41+
3342
class DataManager {
3443
protected $name;
3544

@@ -61,6 +70,7 @@ public function reload($forceSave = false) : bool {
6170
return false;
6271
}
6372
update_option(self::CACHE_OPTION_NAME, $this->name . '|' . $ip_list);
73+
update_option('geoip_detect2_dynamic-reverse-proxies_last_updated', time());
6474

6575
return true;
6676
}
@@ -81,3 +91,58 @@ public function getIpsFromCache() : array {
8191
}
8292
}
8393

94+
95+
class UpdateDynamicReverseProxiesCron {
96+
public function addFilter() {
97+
add_action('geoipdetectdynamicproxiesupdate', [ $this, 'hook_cron', 10, 1 ]);
98+
}
99+
100+
public function hook_cron() {
101+
/**
102+
* Filter:
103+
* Cron has fired.
104+
* Find out if dynamic reverse proxy data should be updated now.
105+
*
106+
* @param $do_it
107+
*/
108+
$do_it = apply_filters('geoip_detect2_dynamic-reverse-proxies_do_automatic_update', true);
109+
110+
$this->schedule();
111+
112+
if ($do_it) {
113+
$this->run();
114+
}
115+
}
116+
117+
public function run() {
118+
$last = get_option('geoip_detect2_dynamic-reverse-proxies_last_updated');
119+
$now = time();
120+
if( $now - $last < HOUR_IN_SECONDS) {
121+
return false;
122+
}
123+
124+
$manager = getDataManager();
125+
if ($manager) {
126+
$manager->reload();
127+
}
128+
}
129+
130+
public function schedule($forceReschedule = false) {
131+
$next = wp_next_scheduled('geoipdetectdynamicproxiesupdate');
132+
133+
if (!$next || $forceReschedule) {
134+
$this->schedule_next_cron_run();
135+
}
136+
137+
if ($forceReschedule) {
138+
$this->run();
139+
}
140+
}
141+
142+
protected function schedule_next_cron_run() {
143+
$next = time() + DAY_IN_SECONDS;
144+
$next += mt_rand(1, HOUR_IN_SECONDS);
145+
wp_schedule_single_event($next, 'geoipdetectdynamicproxiesupdate');
146+
}
147+
}
148+
(new UpdateDynamicReverseProxiesCron)->addFilter();

views/client-ip.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,16 @@
11
<div class="wrap geoip-detect-wrap">
2+
3+
<?php if (!empty($errorMessage)): ?>
4+
<p class="geoip_detect_error">
5+
<?php echo $errorMessage; ?>
6+
</p>
7+
<?php endif; ?>
8+
<?php if (!empty($successMessage)): ?>
9+
<p class="geoip_detect_error geoip_detect_success">
10+
<?php echo $successMessage; ?>
11+
</p>
12+
<?php endif; ?>
13+
214
<!-- This page cannot be translated yet, as I am not sure how it will look like in the long-term.
315
The final goal would be to have a step-by-step wizard helping to set all the relevant options semi-automatically. -->
416
<h1><?php _e('Geolocation IP Detection', 'geoip-detect');?> - Client IP Debug Panel</h1>
@@ -45,6 +57,15 @@
4557
<li>Add known proxies of a cloud provider enabled: <b><?php echo get_option('geoip-detect-dynamic_reverse_proxies') ? 'Yes, ' . ucfirst(get_option('geoip-detect-dynamic_reverse_proxy_type', '')) : 'No'; ?></b>
4658
<span class="detail-box">If your site is hosted by CloudFlare or AWS, this should probably be enabled. It will automatically retrieve the many IP adresses that a reverse proxy of this provider can have, and update the list daily.</span>
4759
<span class="detail-box">Here is the current list of IP adresses: <b><?= implode(', ', \YellowTree\GeoipDetect\DynamicReverseProxies\addDynamicIps()) ?: '(Empty)' ?></b></span>
60+
<span class="detail-box">
61+
Last updated: <b><?= geoip_detect_format_localtime($last_update); ?></b><br>
62+
Next update: <b><?= geoip_detect_format_localtime(wp_next_scheduled('geoipdetectdynamicproxiesupdate')); ?></b>
63+
<form method="POST">
64+
<?php wp_nonce_field( 'geoip_detect_reload-proxies' ); ?>
65+
<input type="hidden" name="action" value="reload-proxies" />
66+
<input type="submit" class="button button-primary" value="Reload now" />
67+
</form>
68+
</span>
4869
</li>
4970
</ul>
5071

views/footer.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,10 @@
3535
padding-top: 11px;
3636
text-align: left;
3737
}
38+
.geoip_detect_success {
39+
border-left: #00a32a solid 4px;
40+
}
41+
3842
.detail-box {
3943
display: block;
4044
margin-left: 50px;

0 commit comments

Comments
 (0)