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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
|
<?php
if ( !class_exists( 'BruteProtect_Admin' ) ) { class BruteProtect_Admin extends BruteProtect {
private $error_reporting_data; public $clef; public $menu_icon;
function __construct() {
$ip = $this->brute_get_ip(); $key = get_site_option( 'bruteprotect_api_key' );
if ( ( $ip == '127.0.0.1' || $ip == '::1' ) && !$key ) { add_action( 'admin_notices', array( &$this, 'bruteprotect_localhost_warning' ) ); }
add_action( 'admin_head', array( &$this, 'set_custom_font_icon' ) ); add_action( 'wp_dashboard_setup', array( &$this, 'bruteprotect_dashboard_widgets' ) ); add_action( 'wp_network_dashboard_setup', array( &$this, 'bruteprotect_dashboard_widgets' ) );
add_filter( 'plugin_action_links', array( &$this, 'bruteprotect_plugin_action_links' ), 10, 2 );
add_action( 'admin_menu', array( &$this, 'bruteprotect_admin_menu_non_multisite' ) ); add_action( 'network_admin_menu', array( &$this, 'bruteprotect_admin_menu' ) );
add_action( 'admin_enqueue_scripts', array( &$this, 'enqueue_bruteprotect_admin' ) );
add_action( 'admin_init', array( &$this, 'activation_redirection' ) ); add_action( 'admin_init', array( &$this, 'dismiss_notice' ) );
}
function dismiss_notice() { if ( isset( $_GET['brute_dismiss_notice'] ) ) { $dismiss = $_GET['brute_dismiss_notice']; $expected = array( '1', '0' ); if ( in_array( $dismiss, $expected ) ) { global $current_user; update_user_meta( $current_user->ID, 'brute_dismiss_jetpack_notice', $dismiss ); } } }
function activation_redirection() { if ( get_site_option( 'bruteprotect_do_activation_redirect', false ) ) { delete_site_option( 'bruteprotect_do_activation_redirect' ); wp_redirect( admin_url( 'admin.php?page=bruteprotect-config' ) ); } }
function set_custom_font_icon() { if ( version_compare( get_bloginfo( 'version' ), '3.7.99', '>' ) ) : ?> <style type="text/css">
/* for top level menu pages replace `{menu-slug}` with the slug name passed to `add_menu_page()` */ #toplevel_page_bruteprotect-config .wp-menu-image:before { font-family: bruteprotect !important; content: "a" !important; }
</style> <?php endif; }
function enqueue_bruteprotect_admin() {
wp_enqueue_style( 'bruteprotect-css', plugins_url( '/admin/bruteprotect-admin.css', __FILE__ ), array(), BRUTEPROTECT_VERSION ); if ( isset( $_GET[ 'page' ] ) && $_GET[ 'page' ] == 'bruteprotect-config' ) { wp_enqueue_style( 'bpdash-css', MYBP_URL . 'assets/stylesheets/app.css', array(), BRUTEPROTECT_VERSION ); wp_enqueue_style( 'fontawesome', plugins_url( '/bruteprotect/admin/fonts/font-awesome/css/font-awesome.min.css' ), array(), BRUTEPROTECT_VERSION ); wp_enqueue_style( 'bpadmin-css', plugins_url( '/bruteprotect/admin/css/admin.css' ), array(), BRUTEPROTECT_VERSION ); wp_register_script( 'modrnizer', plugins_url( '/bruteprotect/admin/js/modrnizer.js' ) ); wp_register_script( 'foundation', plugins_url( '/bruteprotect/admin/js/foundation.min.js' ), array( 'jquery', 'modrnizer' ), BRUTEPROTECT_VERSION ); wp_register_script( 'equalizer', plugins_url( '/bruteprotect/admin/js/foundation.equalizer.js' ), array( 'foundation' ), BRUTEPROTECT_VERSION );
wp_enqueue_script( 'modrnizer' ); wp_enqueue_script( 'foundation' ); wp_enqueue_script( 'equalizer' ); } if ( isset( $_GET[ 'page' ] ) && $_GET[ 'page' ] == 'bruteprotect-debug' ) { wp_enqueue_style('bpadmin-css', plugins_url('/bruteprotect/admin/css/admin.css'), array(), BRUTEPROTECT_VERSION); }
}
function bruteprotect_localhost_warning() { global $bruteprotect_showing_warning; $bruteprotect_showing_warning = true;
echo "<div id='bruteprotect-warning' class='updated fade'><p><strong>" . __( 'BruteProtect not enabled.' ) . "</strong> You have installed BruteProtect, but we have detected that you are running it on a local installation. You can leave BruteProtect turned on, we will prompt you to generate a key when you migrate to a live server.</p></div>"; }
function check_bruteprotect_access() { global $wp_version; $api_url = get_site_option( 'brute_api_url', 'api.bruteprotect.com/' ); $test = wp_remote_get( 'http://' . $api_url . 'api_check.php' );
$report['wp_version'] = $wp_version; $report['connection_status'] = $test; $report['server'] = $_SERVER; $report['bp_version'] = BRUTEPROTECT_VERSION; $this->error_reporting_data = base64_encode( serialize( $report ) );
if ( !is_wp_error( $test ) && $test[ 'body' ] == 'ok' ) : return true; endif;
return false; }
function get_error_reporting_data() { $this->brute_call( 'brute_test' ); $api_url = $this->get_bruteprotect_host(); $error = unserialize( base64_decode( $this->error_reporting_data ) ); $data = "Server Info:\n"; $data .= print_r( $error, true); $data .= "\n\nLast Request to $api_url:\n"; $data .= print_r( $this->last_request, true ); $data .= "\n\nLast Response (raw):\n"; $data .= print_r( $this->last_response_raw, true ); $data .= "\n\nLast Response (pretty):\n"; $data .= print_r( $this->last_response, true ); return $data; }
///////////////////////////////////////////////////////////////////// // Admin Dashboard Widget ///////////////////////////////////////////////////////////////////// function bruteprotect_dashboard_widgets() {
if ( is_multisite() && !is_network_admin() ) { $brute_dashboard_widget_hide = get_site_option( 'brute_dashboard_widget_hide' ); if ( $brute_dashboard_widget_hide == 1 ) { return; } }
$brute_dashboard_widget_admin_only = get_site_option( 'brute_dashboard_widget_admin_only' ); if ( $brute_dashboard_widget_admin_only == 1 && !current_user_can( 'manage_options' ) ) { return; }
global $wp_meta_boxes; wp_add_dashboard_widget( 'bruteprotect_dashboard_widget', 'BruteProtect Stats', array( &$this, 'bruteprotect_dashboard_widget' ) ); }
function bruteprotect_dashboard_widget() {
$response = $this->brute_call( 'check_key' );
if ( !isset( $response[ 'status' ] ) ) { // we cannot connect to the api, lets not show the stats echo '<div style="text-align: center;"><strong>Statistics are currently unavailable.</strong></div>';
return; }
bruteprotect_save_pro_info( $response ); $key = get_site_option( 'bruteprotect_api_key' ); $ckval = get_site_option( 'bruteprotect_ckval' );
$stats = wp_remote_get( $this->get_bruteprotect_host() . "index.php/ui/dashboard/index/" . $key );
if ( is_wp_error( $stats ) ) { echo '<div style="text-align: center;"><strong>Statistics are currently unavailable.</strong></div>';
return; }
print_r( $stats[ 'body' ] ); }
function bruteprotect_plugin_action_links( $links, $file ) { if ( $file == plugin_basename( dirname( __FILE__ ) . '/bruteprotect.php' ) ) { $links[ ] = '<a href="' . esc_url( admin_url( 'admin.php?page=bruteprotect-config' ) ) . '">' . __( 'Settings' ) . '</a>'; }
return $links; }
function bruteprotect_admin_menu_non_multisite() { if ( version_compare( get_bloginfo( 'version' ), '3.7.99', '>' ) ) { $this->menu_icon = ''; // no need for icon .png if in mp6
} else { $this->menu_icon = plugins_url( '/images/menu_icon.png', __FILE__ ); }
// registers a debug page with a null parent so it won't show up in a menu add_submenu_page( null, 'BruteProtect Debug', 'BruteProtect Debu', 'manage_options', 'bruteprotect-debug', array( &$this, 'bruteprotect_debug_page') );
if ( is_multisite() ) { add_menu_page( __( 'BruteProtect' ), __( 'BruteProtect' ), 'manage_options', 'bruteprotect-config', array( &$this, 'bruteprotect_conf_ms_notice' ), $this->menu_icon );
return; } $this->bruteprotect_admin_menu(); }
function bruteprotect_admin_menu() { if ( version_compare( get_bloginfo( 'version' ), '3.7.99', '>' ) ) { $this->menu_icon = ''; // no need for icon .png if in mp6
} else { $this->menu_icon = plugins_url( '/images/menu_icon.png', __FILE__ ); } add_menu_page( __( 'BruteProtect' ), __( 'BruteProtect' ), 'manage_options', 'bruteprotect-config', array( &$this, 'bruteprotect_dashboard' ), $this->menu_icon );
$key = get_site_option( 'bruteprotect_api_key' ); $error = get_site_option( 'bruteprotect_error' );
if ( !$key ) { add_action( 'admin_notices', array( &$this, 'bruteprotect_warning' ) );
return; } elseif ( $error && isset( $_GET[ 'page' ] ) && $_GET[ 'page' ] != 'bruteprotect-config' ) { add_action( 'admin_notices', array( &$this, 'bruteprotect_invalid_key_warning' ) );
return; } }
function send_error_report($error_info) { $nonce = isset( $_POST['bp_debug_nonce'] ) ? $_POST['bp_debug_nonce'] : ''; if( ! wp_verify_nonce( $nonce, 'brute_error_report' ) ) { return false; } $to = 'rocco@hcg.bz'; $subject = 'BruteProtect Connection Report from ' . home_url(); wp_mail( $to, $subject, $error_info ); return true; }
function change_endpoints() { $nonce = isset( $_POST['bp_debug_nonce'] ) ? $_POST['bp_debug_nonce'] : ''; if( ! wp_verify_nonce( $nonce, 'brute_change_endpoints' ) ) { return false; } $brute_endpoint = isset( $_POST['brute_endpoint'] ) ? $_POST['brute_endpoint'] : ''; if( $brute_endpoint == 'testing' ) { update_site_option( 'brute_my_url', 'my.bpdv.co/' ); update_site_option( 'brute_api_url', 'api.bpdv.co/' ); update_site_option( 'brute_endpoint_location', 'testing' ); } else { update_site_option( 'brute_my_url', 'my.bruteprotect.com/' ); update_site_option( 'brute_api_url', 'api.bruteprotect.com/' ); update_site_option( 'brute_endpoint_location', 'live' ); } }
function bruteprotect_debug_page() { $has_access = $this->check_bruteprotect_access(); $error_info = $this->get_error_reporting_data();
if( isset( $_POST['bp_debug_action']) && $_POST['bp_debug_action'] == 'send_error_report' ) { $this->send_error_report($error_info); $debug_notice = 'Error report sent!'; }
if( isset( $_POST['bp_debug_action']) && $_POST['bp_debug_action'] == 'change_endpoints' ) { $this->change_endpoints(); $debug_notice = 'Endpoints changed!'; }
$error_report_nonce = wp_create_nonce( 'brute_error_report' ); $endpoint_nonce = wp_create_nonce( 'brute_change_endpoints' ); $endpoint_location = get_site_option( 'brute_endpoint_location', 'live' ); $live_checked = ( $endpoint_location == 'live' ) ? 'checked="checked"' : ''; $testing_checked = ( $endpoint_location == 'testing' ) ? 'checked="checked"' : '';
include 'admin/inc/debug.php'; }
function bruteprotect_warning() { global $bruteprotect_showing_warning; $bruteprotect_showing_warning = true;
//Don't trigger the warning on the config page if ( isset( $_GET[ 'page' ] ) && 'bruteprotect-config' == $_GET[ 'page' ] ) { return; }
$ip = $this->brute_get_ip(); //Don't trigger the warning on localhost, since we're not going to let them set up the API yet anyway... if ( $ip == '127.0.0.1' || $ip == '::1' ) { return; }
$selfinstalling = get_option( 'bp_selfinstall_attempt' ); if ( $selfinstalling ) { return; }
echo "<div id='bruteprotect-warning' class='error fade'><p><strong>" . __( 'BruteProtect is almost ready.' ) . "</strong> " . sprintf( __( 'You must <a href="%1$s">enter your BruteProtect API key</a> for it to work. <a href="%1$s">Obtain a key for free</a>.' ), esc_url( admin_url( 'admin.php?page=bruteprotect-config' ) ) ) . "</p></div> "; }
function bruteprotect_invalid_key_warning() { if ( isset( $_GET[ 'get_key' ] ) && $_GET[ 'get_key' ] == 'success' ) { return true; } global $bruteprotect_showing_warning; $bruteprotect_showing_warning = true; echo " <div id='bruteprotect-warning' class='error fade'><p><strong>" . __( 'There is a problem with your BruteProtect API key' ) . "</strong> " . sprintf( __( ' <a href="%1$s">Please correct the error</a>, your site will not be protected until you do.' ), esc_url( admin_url( 'admin.php?page=bruteprotect-config' ) ) ) . "</p></div> "; }
function bruteprotect_dashboard() { include 'admin/main.php'; }
/** * Unlinks the current user from my.bruteprotect.com * * if there is a flag to delete_all, all other linked users will also be disconnected, and the api key removed * * @param bool $delete_all */ function unlink_site( $delete_all = false ) { global $current_user; $user_linked = get_user_meta( $current_user->ID, 'bruteprotect_user_linked', true ); if ( !empty( $user_linked ) ) { delete_user_meta( $current_user->ID, 'bruteprotect_user_linked' ); $action = 'unlink_owner_from_site'; $additional_data = array( 'wp_user_id' => strval( $current_user->ID ), ); $sign = true;
$response = $this->brute_call( $action, $additional_data, $sign ); }
$bp_users = get_bruteprotect_users(); if ( empty( $bp_users ) ) { delete_site_option( 'bruteprotect_user_linked' ); } else if ( $delete_all == true ) { foreach ( $bp_users as $u ) { delete_user_meta( $u->ID, 'bruteprotect_user_linked' ); $action = 'unlink_owner_from_site'; $additional_data = array( 'wp_user_id' => strval( $u->ID ), ); $sign = true;
$response = $this->brute_call( $action, $additional_data, $sign ); } delete_site_option( 'bruteprotect_user_linked' ); }
if ( $delete_all === true ) { delete_site_option( 'bruteprotect_api_key' ); }
}
function bruteprotect_conf_ms_notice() { ?> <div class="wrap"> <div id="bruteapi">
<h2 id="header"> <img src="<?php echo BRUTEPROTECT_PLUGIN_URL ?>images/BruteProtect-Logo-Text-Only-40.png" alt="BruteProtect" width="250"> Setup</h2>
<div class="brutecontainer"> <div class="box left clear"> <h3 class="green">BruteProtect is ready to protect your network</h3> <?php if ( current_user_can( 'manage_network' ) ): ?> <p>BruteProtect only needs one API key per network.</strong> <a href="<?php echo network_home_url( '/wp-admin/network/admin.php?page=bruteprotect-config' ) ?>">Manage your key here.</a></p> <?php else: ?> <p>But, only Super-Admins can configure BruteProtect settings. Contact your network administrator to make sure everything is working perfectly</p> <?php endif ?>
</div> </div> </div> </div> <?php }
} }
|