forked from yellowtree/geoip-detect
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadmin-ui.php
143 lines (111 loc) · 4.41 KB
/
admin-ui.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
<?php
use YellowTree\GeoipDetect\DataSources\DataSourceRegistry;
function geoip_detect_menu() {
require_once ABSPATH . '/wp-admin/admin.php';
add_submenu_page('tools.php', __('GeoIP Detection Lookup', 'geoip-detect'), __('GeoIP Lookup', 'geoip-detect'), 'activate_plugins', GEOIP_PLUGIN_BASENAME, 'geoip_detect_lookup_page');
add_options_page(__('GeoIP Detection', 'geoip-detect'), __('GeoIP Detection', 'geoip-detect'), 'manage_options', GEOIP_PLUGIN_BASENAME, 'geoip_detect_option_page');
}
add_action('admin_menu', 'geoip_detect_menu');
function geoip_detect_add_settings_link( $links ) {
$link = '<a href="tools.php?page=' . GEOIP_PLUGIN_BASENAME . '">' . __('Lookup', 'geoip-detect') . '</a>';
array_push( $links, $link );
$link = '<a href="options-general.php?page=' . GEOIP_PLUGIN_BASENAME . '">' . __('Options', 'geoip-detect') . '</a>';
array_push( $links, $link );
return $links;
}
add_filter( "plugin_action_links_" . GEOIP_PLUGIN_BASENAME, 'geoip_detect_add_settings_link' );
// ------------- Admin GUI --------------------
function geoip_detect_verify_nonce() {
$nonce = @$_POST['_wpnonce'];
return wp_verify_nonce( $nonce, 'geoip_detect_' . @$_POST['action'] );
}
function geoip_detect_lookup_page()
{
$ip_lookup_result = false;
$message = '';
if (geoip_detect_verify_nonce()) {
switch(@$_POST['action']) {
case 'lookup':
if (isset($_POST['ip']))
{
$request_ip = $_POST['ip'];
$request_skipCache = !empty($_POST['skip_cache']);
$options = array('skipCache' => $request_skipCache);
$request_locales = null;
if (!empty($_POST['locales']))
$request_locales = explode(',', $_POST['locales']);
$start = microtime(true);
$ip_lookup_result = geoip_detect2_get_info_from_ip($request_ip, $request_locales, $options);
$end = microtime(true);
$ip_lookup_duration = $end - $start;
}
break;
}
}
include_once(GEOIP_PLUGIN_DIR . '/views/lookup.php');
}
function geoip_detect_option_page() {
if (!current_user_can('manage_options'))
return;
$registry = DataSourceRegistry::getInstance();
$sources = $registry->getAllSources();
$currentSource = $registry->getCurrentSource();
$message = '';
$numeric_options = array('set_css_country', 'has_reverse_proxy', 'disable_pagecache');
$text_options = array('external_ip');
$option_names = array_merge($numeric_options, $text_options);
if (geoip_detect_verify_nonce()) {
switch(@$_POST['action'])
{
case 'update':
$registry->setCurrentSource('auto');
$s = new \YellowTree\GeoipDetect\DataSources\Auto\AutoDataSource();
$ret = $s->maxmindUpdate();
if ($ret === true)
$message .= __('Updated successfully.', 'geoip-detect');
else
$message .= __('Update failed.', 'geoip-detect') .' '. $ret;
break;
case 'choose':
$registry->setCurrentSource($_POST['options']['source']);
$currentSource = $registry->getCurrentSource();
break;
case 'options-source':
$messages = array();
foreach ($sources as $s) {
$ret = $s->saveParameters($_POST);
if (is_string($ret) && $ret) {
$messages[] = $ret;
}
}
if ($messages)
$message .= implode('<br />', $messages);
break;
case 'options':
// Empty IP Cache
delete_transient('geoip_detect_external_ip');
if (!empty($_POST['options']['external_ip'])) {
if (!geoip_detect_is_ip($_POST['options']['external_ip'])) {
$message .= 'The external IP "' . esc_html($_POST['options']['external_ip']) . '" is not a valid IP.';
unset($_POST['options']['external_ip']);
} else if (!geoip_detect_is_public_ip($_POST['options']['external_ip'])) {
$message .= 'Warning: The external IP "' . esc_html($_POST['options']['external_ip']) . '" is not a public internet IP, so it will probably not work.';
}
}
foreach ($option_names as $opt_name) {
if (in_array($opt_name, $numeric_options))
$opt_value = isset($_POST['options'][$opt_name]) ? (int) $_POST['options'][$opt_name] : 0;
else
$opt_value = isset($_POST['options'][$opt_name]) ? $_POST['options'][$opt_name] : '';
update_option('geoip-detect-' . $opt_name, $opt_value);
}
break;
}
}
$wp_options = array();
foreach ($option_names as $opt_name) {
$wp_options[$opt_name] = get_option('geoip-detect-'. $opt_name);
}
$ipv6_supported = GEOIP_DETECT_IPV6_SUPPORTED;
include_once(GEOIP_PLUGIN_DIR . '/views/options.php');
}