PageRenderTime 42ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

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

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