math-fallback.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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. /**
  19. * Verifies that a user answered the math problem correctly while logging in.
  20. *
  21. * @return bool Returns true if the math is correct
  22. * @throws Error if insuffient $_POST variables are present.
  23. * @throws Error message if the math is wrong
  24. */
  25. static function math_authenticate() {
  26. $salt = get_site_option( 'jetpack_protect_key' ) . get_site_option( 'admin_email' );
  27. $ans = isset( $_POST['jetpack_protect_num'] ) ? (int) $_POST['jetpack_protect_num'] : '' ;
  28. $salted_ans = sha1( $salt . $ans );
  29. $correct_ans = isset( $_POST[ 'jetpack_protect_answer' ] ) ? $_POST[ 'jetpack_protect_answer' ] : '' ;
  30. if( isset( $_COOKIE[ 'jpp_math_pass' ] ) ) {
  31. $jetpack_protect = Jetpack_Protect_Module::instance();
  32. $transient = $jetpack_protect->get_transient( 'jpp_math_pass_' . $_COOKIE[ 'jpp_math_pass' ] );
  33. if( !$transient || $transient < 1 ) {
  34. Jetpack_Protect_Math_Authenticate::generate_math_page();
  35. }
  36. return true;
  37. }
  38. if ( ! $correct_ans || !$_POST['jetpack_protect_num'] ) {
  39. Jetpack_Protect_Math_Authenticate::generate_math_page();
  40. } elseif ( $salted_ans != $correct_ans ) {
  41. wp_die(
  42. __( '<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' ),
  43. '',
  44. array ( 'response' => 401 )
  45. );
  46. } else {
  47. return true;
  48. }
  49. }
  50. /**
  51. * Creates an interim page to collect answers to a math captcha
  52. *
  53. * @return none, execution stopped
  54. */
  55. static function generate_math_page( $error = false ) {
  56. $salt = get_site_option( 'jetpack_protect_key' ) . get_site_option( 'admin_email' );
  57. $num1 = rand( 0, 10 );
  58. $num2 = rand( 1, 10 );
  59. $sum = $num1 + $num2;
  60. $ans = sha1( $salt . $sum );
  61. ob_start();
  62. ?>
  63. <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>
  64. <?php if ($error): ?>
  65. <h3><?php _e( 'Your answer was incorrect, please try again.', 'jetpack' ); ?></h3>
  66. <?php endif ?>
  67. <form action="<?php echo wp_login_url(); ?>" method="post" accept-charset="utf-8">
  68. <?php Jetpack_Protect_Math_Authenticate::math_form(); ?>
  69. <input type="hidden" name="jetpack_protect_process_math_form" value="1" id="jetpack_protect_process_math_form" />
  70. <p><input type="submit" value="<?php esc_html_e( 'Continue &rarr;', 'jetpack' ); ?>"></p>
  71. </form>
  72. <?php
  73. $mathpage = ob_get_contents();
  74. ob_end_clean();
  75. wp_die(
  76. $mathpage,
  77. '',
  78. array ( 'response' => 401 )
  79. );
  80. }
  81. public function process_generate_math_page() {
  82. $salt = get_site_option( 'jetpack_protect_key' ) . get_site_option( 'admin_email' );
  83. $ans = (int)$_POST['jetpack_protect_num'];
  84. $salted_ans = sha1( $salt . $ans );
  85. $correct_ans = $_POST[ 'jetpack_protect_answer' ];
  86. if ( $salted_ans != $correct_ans ) {
  87. Jetpack_Protect_Math_Authenticate::generate_math_page(true);
  88. } else {
  89. $temp_pass = substr( sha1( rand( 1, 100000000 ) . get_site_option( 'jetpack_protect_key' ) ), 5, 25 );
  90. $jetpack_protect = Jetpack_Protect_Module::instance();
  91. $jetpack_protect->set_transient( 'jpp_math_pass_' . $temp_pass, 3, DAY_IN_SECONDS );
  92. setcookie('jpp_math_pass', $temp_pass, time() + DAY_IN_SECONDS, COOKIEPATH, COOKIE_DOMAIN, false);
  93. return true;
  94. }
  95. }
  96. /**
  97. * Requires a user to solve a simple equation. Added to any WordPress login form.
  98. *
  99. * @return VOID outputs html
  100. */
  101. static function math_form() {
  102. $salt = get_site_option( 'jetpack_protect_key' ) . get_site_option( 'admin_email' );
  103. $num1 = rand( 0, 10 );
  104. $num2 = rand( 1, 10 );
  105. $sum = $num1 + $num2;
  106. $ans = sha1( $salt . $sum );
  107. ?>
  108. <div style="margin: 5px 0 20px;">
  109. <strong><?php esc_html_e( 'Prove your humanity:', 'jetpack' ); ?> </strong>
  110. <?php echo $num1 ?> &nbsp; + &nbsp; <?php echo $num2 ?> &nbsp; = &nbsp;
  111. <input type="input" name="jetpack_protect_num" value="" size="2" />
  112. <input type="hidden" name="jetpack_protect_answer" value="<?php echo $ans; ?>" />
  113. </div>
  114. <?php
  115. }
  116. }
  117. }