1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
if ( ! class_exists( 'Jetpack_Protect_Math_Authenticate' ) ) { |
4
|
|
|
/* |
5
|
|
|
* The math captcha fallback if we can't talk to the Protect API |
6
|
|
|
*/ |
7
|
|
|
class Jetpack_Protect_Math_Authenticate { |
8
|
|
|
|
9
|
|
|
static $loaded; |
|
|
|
|
10
|
|
|
|
11
|
|
|
function __construct() { |
12
|
|
|
|
13
|
|
|
if ( self::$loaded ) { |
14
|
|
|
return; |
15
|
|
|
} |
16
|
|
|
|
17
|
|
|
self::$loaded = 1; |
18
|
|
|
|
19
|
|
|
add_action( 'login_form', array( $this, 'math_form' ) ); |
20
|
|
|
|
21
|
|
|
if( isset( $_POST[ 'jetpack_protect_process_math_form' ] ) ) { |
22
|
|
|
add_action( 'init', array( $this, 'process_generate_math_page' ) ); |
23
|
|
|
} |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Verifies that a user answered the math problem correctly while logging in. |
28
|
|
|
* |
29
|
|
|
* @return bool Returns true if the math is correct |
30
|
|
|
* @throws Error if insuffient $_POST variables are present. |
31
|
|
|
* @throws Error message if the math is wrong |
32
|
|
|
*/ |
33
|
|
|
static function math_authenticate() { |
34
|
|
|
$salt = get_site_option( 'jetpack_protect_key' ) . get_site_option( 'admin_email' ); |
35
|
|
|
$ans = isset( $_POST['jetpack_protect_num'] ) ? (int) $_POST['jetpack_protect_num'] : '' ; |
36
|
|
|
$salted_ans = sha1( $salt . $ans ); |
37
|
|
|
$correct_ans = isset( $_POST[ 'jetpack_protect_answer' ] ) ? $_POST[ 'jetpack_protect_answer' ] : '' ; |
38
|
|
|
|
39
|
|
|
if( isset( $_COOKIE[ 'jpp_math_pass' ] ) ) { |
40
|
|
|
$transient = Jetpack_Protect_Module::get_transient( 'jpp_math_pass_' . $_COOKIE[ 'jpp_math_pass' ] ); |
41
|
|
|
if( !$transient || $transient < 1 ) { |
42
|
|
|
Jetpack_Protect_Math_Authenticate::generate_math_page(); |
43
|
|
|
} |
44
|
|
|
return true; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
if ( ! $correct_ans || !$_POST['jetpack_protect_num'] ) { |
48
|
|
|
Jetpack_Protect_Math_Authenticate::generate_math_page(); |
49
|
|
|
} elseif ( $salted_ans != $correct_ans ) { |
50
|
|
|
wp_die( |
51
|
|
|
__( '<strong>You failed to correctly answer the math problem.</strong> This is used to combat spam when the Protect API is unavailable. Please use your browser\'s back button to return to the login form, press the "refresh" button to generate a new math problem, and try to log in again.', 'jetpack' ), |
52
|
|
|
'', |
53
|
|
|
401 |
54
|
|
|
); |
55
|
|
|
} else { |
56
|
|
|
return true; |
57
|
|
|
} |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Creates an interim page to collect answers to a math captcha |
62
|
|
|
* |
63
|
|
|
* @return none, execution stopped |
|
|
|
|
64
|
|
|
*/ |
65
|
|
|
static function generate_math_page( $error = false ) { |
66
|
|
|
$salt = get_site_option( 'jetpack_protect_key' ) . get_site_option( 'admin_email' ); |
67
|
|
|
$num1 = rand( 0, 10 ); |
68
|
|
|
$num2 = rand( 1, 10 ); |
69
|
|
|
$sum = $num1 + $num2; |
70
|
|
|
$ans = sha1( $salt . $sum ); |
|
|
|
|
71
|
|
|
ob_start(); |
72
|
|
|
?> |
73
|
|
|
<h2><?php _e( 'Please solve this math problem to prove that you are not a bot. Once you solve it, you will need to log in again.', 'jetpack' ); ?></h2> |
74
|
|
|
<?php if ($error): ?> |
75
|
|
|
<h3><?php _e( 'Your answer was incorrect, please try again.', 'jetpack' ); ?></h3> |
76
|
|
|
<?php endif ?> |
77
|
|
|
|
78
|
|
|
<form action="<?php echo wp_login_url(); ?>" method="post" accept-charset="utf-8"> |
79
|
|
|
<?php Jetpack_Protect_Math_Authenticate::math_form(); ?> |
80
|
|
|
<input type="hidden" name="jetpack_protect_process_math_form" value="1" id="jetpack_protect_process_math_form" /> |
81
|
|
|
<p><input type="submit" value="<?php esc_html_e( 'Continue →', 'jetpack' ); ?>"></p> |
82
|
|
|
</form> |
83
|
|
|
<?php |
84
|
|
|
$mathpage = ob_get_contents(); |
85
|
|
|
ob_end_clean(); |
86
|
|
|
wp_die( |
87
|
|
|
$mathpage, |
88
|
|
|
'', |
89
|
|
|
'401' |
90
|
|
|
); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
public function process_generate_math_page() { |
94
|
|
|
$salt = get_site_option( 'jetpack_protect_key' ) . get_site_option( 'admin_email' ); |
95
|
|
|
$ans = (int)$_POST['jetpack_protect_num']; |
96
|
|
|
$salted_ans = sha1( $salt . $ans ); |
97
|
|
|
$correct_ans = $_POST[ 'jetpack_protect_answer' ]; |
98
|
|
|
|
99
|
|
|
if ( $salted_ans != $correct_ans ) { |
100
|
|
|
Jetpack_Protect_Math_Authenticate::generate_math_page(true); |
101
|
|
|
} else { |
102
|
|
|
$temp_pass = substr( sha1( rand( 1, 100000000 ) . get_site_option( 'jetpack_protect_key' ) ), 5, 25 ); |
103
|
|
|
Jetpack_Protect_Module::set_transient( 'jpp_math_pass_' . $temp_pass, 3, DAY_IN_SECONDS ); |
104
|
|
|
setcookie('jpp_math_pass', $temp_pass, time() + DAY_IN_SECONDS, COOKIEPATH, COOKIE_DOMAIN, false); |
105
|
|
|
return true; |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* Requires a user to solve a simple equation. Added to any WordPress login form. |
111
|
|
|
* |
112
|
|
|
* @return VOID outputs html |
113
|
|
|
*/ |
114
|
|
|
static function math_form() { |
115
|
|
|
$salt = get_site_option( 'jetpack_protect_key' ) . get_site_option( 'admin_email' ); |
116
|
|
|
$num1 = rand( 0, 10 ); |
117
|
|
|
$num2 = rand( 1, 10 ); |
118
|
|
|
$sum = $num1 + $num2; |
119
|
|
|
$ans = sha1( $salt . $sum ); |
120
|
|
|
?> |
121
|
|
|
<div style="margin: 5px 0 20px;"> |
122
|
|
|
<strong><?php esc_html_e( 'Prove your humanity:', 'jetpack' ); ?> </strong> |
123
|
|
|
<?php echo $num1 ?> + <?php echo $num2 ?> = |
124
|
|
|
<input type="input" name="jetpack_protect_num" value="" size="2" /> |
125
|
|
|
<input type="hidden" name="jetpack_protect_answer" value="<?php echo $ans; ?>" /> |
126
|
|
|
</div> |
127
|
|
|
<?php |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
} |
131
|
|
|
} |
132
|
|
|
|
The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using
the property is implicitly global.
To learn more about the PSR-2, please see the PHP-FIG site on the PSR-2.