/wp-content/plugins/jetpack/modules/protect/math-fallback.php

https://github.com/livinglab/openlab · PHP · 164 lines · 119 code · 24 blank · 21 comment · 17 complexity · 2a49e6686d9665e1d8c983bb8e4a6acf MD5 · raw file

  1. <?php
  2. if ( ! class_exists( 'Jetpack_Protect_Math_Authenticate' ) ) {
  3. /*
  4. * The math captcha fallback if we can't talk to the Protect API
  5. */
  6. class Jetpack_Protect_Math_Authenticate {
  7. static $loaded;
  8. function __construct() {
  9. if ( self::$loaded ) {
  10. return;
  11. }
  12. self::$loaded = 1;
  13. add_action( 'login_form', array( $this, 'math_form' ) );
  14. if( isset( $_POST[ 'jetpack_protect_process_math_form' ] ) ) {
  15. add_action( 'init', array( $this, 'process_generate_math_page' ) );
  16. }
  17. }
  18. private static function time_window() {
  19. return ceil( time() / ( MINUTE_IN_SECONDS * 2 ) );
  20. }
  21. /**
  22. * Verifies that a user answered the math problem correctly while logging in.
  23. *
  24. * @return bool Returns true if the math is correct
  25. * @throws Error if insuffient $_POST variables are present.
  26. * @throws Error message if the math is wrong
  27. */
  28. static function math_authenticate() {
  29. if( isset( $_COOKIE[ 'jpp_math_pass' ] ) ) {
  30. $jetpack_protect = Jetpack_Protect_Module::instance();
  31. $transient = $jetpack_protect->get_transient( 'jpp_math_pass_' . $_COOKIE[ 'jpp_math_pass' ] );
  32. if( !$transient || $transient < 1 ) {
  33. Jetpack_Protect_Math_Authenticate::generate_math_page();
  34. }
  35. return true;
  36. }
  37. $ans = isset( $_POST['jetpack_protect_num'] ) ? (int) $_POST['jetpack_protect_num'] : '' ;
  38. $correct_ans = isset( $_POST[ 'jetpack_protect_answer' ] ) ? $_POST[ 'jetpack_protect_answer' ] : '' ;
  39. $time_window = Jetpack_Protect_Math_Authenticate::time_window();
  40. $salt = get_site_option( 'jetpack_protect_key' ) . '|' . get_site_option( 'admin_email' ) . '|';
  41. $salted_ans_1 = hash_hmac( 'sha1', $ans, $salt . $time_window );
  42. $salted_ans_2 = hash_hmac( 'sha1', $ans, $salt . ( $time_window - 1 ) );
  43. if ( ! $correct_ans || ! $ans ) {
  44. Jetpack_Protect_Math_Authenticate::generate_math_page();
  45. } elseif ( ! hash_equals( $salted_ans_1, $correct_ans ) && ! hash_equals( $salted_ans_2, $correct_ans ) ) {
  46. wp_die(
  47. wp_kses(
  48. __(
  49. '<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.',
  50. 'jetpack'
  51. ),
  52. array( 'strong' => array() )
  53. ),
  54. '',
  55. array( 'response' => 401 )
  56. );
  57. } else {
  58. return true;
  59. }
  60. }
  61. /**
  62. * Creates an interim page to collect answers to a math captcha
  63. *
  64. * @return none, execution stopped
  65. */
  66. static function generate_math_page( $error = false ) {
  67. ob_start();
  68. ?>
  69. <h2><?php esc_html_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>
  70. <?php if ($error): ?>
  71. <h3><?php esc_html_e( 'Your answer was incorrect, please try again.', 'jetpack' ); ?></h3>
  72. <?php endif ?>
  73. <form action="<?php echo wp_login_url(); ?>" method="post" accept-charset="utf-8">
  74. <?php Jetpack_Protect_Math_Authenticate::math_form(); ?>
  75. <input type="hidden" name="jetpack_protect_process_math_form" value="1" id="jetpack_protect_process_math_form" />
  76. <p><input type="submit" value="<?php esc_attr_e( 'Continue &rarr;', 'jetpack' ); ?>"></p>
  77. </form>
  78. <?php
  79. $mathpage = ob_get_contents();
  80. ob_end_clean();
  81. wp_die(
  82. $mathpage,
  83. '',
  84. array ( 'response' => 401 )
  85. );
  86. }
  87. public function process_generate_math_page() {
  88. $ans = isset( $_POST['jetpack_protect_num'] ) ? (int)$_POST['jetpack_protect_num'] : '';
  89. $correct_ans = isset( $_POST[ 'jetpack_protect_answer' ] ) ? $_POST[ 'jetpack_protect_answer' ] : '' ;
  90. $time_window = Jetpack_Protect_Math_Authenticate::time_window();
  91. $salt = get_site_option( 'jetpack_protect_key' ) . '|' . get_site_option( 'admin_email' ) . '|';
  92. $salted_ans_1 = hash_hmac( 'sha1', $ans, $salt . $time_window );
  93. $salted_ans_2 = hash_hmac( 'sha1', $ans, $salt . ( $time_window - 1 ) );
  94. if ( ! hash_equals( $salted_ans_1, $correct_ans ) && ! hash_equals( $salted_ans_2, $correct_ans ) ) {
  95. Jetpack_Protect_Math_Authenticate::generate_math_page(true);
  96. } else {
  97. $temp_pass = substr( hash_hmac( 'sha1', rand( 1, 100000000 ), get_site_option( 'jetpack_protect_key' ) ), 5, 25 );
  98. $jetpack_protect = Jetpack_Protect_Module::instance();
  99. $jetpack_protect->set_transient( 'jpp_math_pass_' . $temp_pass, 3, DAY_IN_SECONDS );
  100. setcookie('jpp_math_pass', $temp_pass, time() + DAY_IN_SECONDS, COOKIEPATH, COOKIE_DOMAIN, false);
  101. remove_action( 'login_form', array( $this, 'math_form' ) );
  102. return true;
  103. }
  104. }
  105. /**
  106. * Requires a user to solve a simple equation. Added to any WordPress login form.
  107. *
  108. * @return VOID outputs html
  109. */
  110. static function math_form() {
  111. // Check if jpp_math_pass cookie is set and it matches valid transient
  112. if( isset( $_COOKIE[ 'jpp_math_pass' ] ) ) {
  113. $jetpack_protect = Jetpack_Protect_Module::instance();
  114. $transient = $jetpack_protect->get_transient( 'jpp_math_pass_' . $_COOKIE[ 'jpp_math_pass' ] );
  115. if( $transient && $transient > 0 ) {
  116. return '';
  117. }
  118. }
  119. $num1 = rand( 0, 10 );
  120. $num2 = rand( 1, 10 );
  121. $ans = $num1 + $num2;
  122. $time_window = Jetpack_Protect_Math_Authenticate::time_window();
  123. $salt = get_site_option( 'jetpack_protect_key' ) . '|' . get_site_option( 'admin_email' ) . '|';
  124. $salted_ans = hash_hmac( 'sha1', $ans, $salt . $time_window );
  125. ?>
  126. <div style="margin: 5px 0 20px;">
  127. <label for="jetpack_protect_answer">
  128. <?php esc_html_e( 'Prove your humanity', 'jetpack' ); ?>
  129. </label>
  130. <br/>
  131. <span style="vertical-align:super;">
  132. <?php echo esc_html( "$num1 &nbsp; + &nbsp; $num2 &nbsp; = &nbsp;" ); ?>
  133. </span>
  134. <input type="text" id="jetpack_protect_answer" name="jetpack_protect_num" value="" size="2" style="width:30px;height:25px;vertical-align:middle;font-size:13px;" class="input" />
  135. <input type="hidden" name="jetpack_protect_answer" value="<?php echo esc_attr( $salted_ans ); ?>" />
  136. </div>
  137. <?php
  138. }
  139. }
  140. }