Source Code Documentation


s2member\includes\classes\brute-force.inc.php


  1. <?php
  2. /**
  3. * s2Member's Brute Force protection routines.
  4. *
  5. * Copyright: © 2009-2011
  6. * {@link http://www.websharks-inc.com/ WebSharks, Inc.}
  7. * (coded in the USA)
  8. *
  9. * Released under the terms of the GNU General Public License.
  10. * You should have received a copy of the GNU General Public License,
  11. * along with this software. In the main directory, see: /licensing/
  12. * If not, see: {@link http://www.gnu.org/licenses/}.
  13. *
  14. * @package s2Member\Brute_Force
  15. * @since 3.5
  16. */
  17. if (realpath (__FILE__) === realpath ($_SERVER["SCRIPT_FILENAME"]))
  18. exit("Do not access this file directly.");
  19.  
  20. if (!class_exists ("c_ws_plugin__s2member_brute_force"))
  21. {
  22. /**
  23. * s2Member's Brute Force protection routines.
  24. *
  25. * @package s2Member\Brute_Force
  26. * @since 3.5
  27. */
  28. class c_ws_plugin__s2member_brute_force
  29. {
  30. /**
  31. * Tracks failed login attempts.
  32. *
  33. * Prevents an attacker from guessing Usernames/Passwords.
  34. * Allows only 5 failed login attempts every 30 minutes.
  35. *
  36. * @package s2Member\Brute_Force
  37. * @since 3.5
  38. *
  39. * @attaches-to ``add_action("wp_login_failed");``
  40. *
  41. * @param str $username Expects the $username to be passed in through the Hook.
  42. * @return null
  43. */
  44. public static function track_failed_logins ($username = FALSE)
  45. {
  46. foreach(array_keys(get_defined_vars())as$__v)$__refs[$__v]=&$$__v;
  47. do_action ("ws_plugin__s2member_before_track_failed_logins", get_defined_vars ());
  48. unset /* Unset defined __refs, __v. */ ($__refs, $__v);
  49.  
  50. if (($max = $GLOBALS["WS_PLUGIN__"]["s2member"]["o"]["max_failed_login_attempts"]))
  51. {
  52. $exp_secs = strtotime ("+" . apply_filters ("ws_plugin__s2member_track_failed_logins__exp_time", "30 minutes", get_defined_vars ())) - time ();
  53. // If you add Filters to this value, you should use a string that is compatible with PHP's strtotime() function.
  54.  
  55. $transient = "s2m_ipr_" . md5 ("s2member_transient_failed_login_attempts_" . $_SERVER["REMOTE_ADDR"]);
  56. set_transient ($transient, (int)get_transient ($transient) + 1, $exp_secs);
  57. }
  58. do_action ("ws_plugin__s2member_after_track_failed_logins", get_defined_vars ());
  59.  
  60. return /* Return for uniformity. */;
  61. }
  62. /**
  63. * Stops anyone attempting a Brute Force attack.
  64. *
  65. * Prevents an attacker from guessing Usernames/Passwords.
  66. * Allows only 5 failed login attempts every 30 minutes.
  67. *
  68. * @package s2Member\Brute_Force
  69. * @since 3.5
  70. *
  71. * @attaches-to ``add_filter("authenticate");``
  72. *
  73. * @param obj $user Expects a WP_User object, or possibly a null value.
  74. * This parameter value is simply passed through this routine.
  75. * @return obj|null Either null, the ``$user`` obj, or a `WP_Error` obj.
  76. */
  77. public static function stop_brute_force_logins ($user = FALSE)
  78. {
  79. foreach(array_keys(get_defined_vars())as$__v)$__refs[$__v]=&$$__v;
  80. do_action ("ws_plugin__s2member_before_stop_brute_force_logins", get_defined_vars ());
  81. unset /* Unset defined __refs, __v. */ ($__refs, $__v);
  82.  
  83. if (($max = $GLOBALS["WS_PLUGIN__"]["s2member"]["o"]["max_failed_login_attempts"]))
  84. {
  85. $exp_secs = strtotime ("+" . apply_filters ("ws_plugin__s2member_track_failed_logins__exp_time", "30 minutes", get_defined_vars ())) - time ();
  86. // If you add Filters to this value, you should use a string that is compatible with PHP's strtotime() function.
  87.  
  88. $about = c_ws_plugin__s2member_utils_time::approx_time_difference (time (), time () + $exp_secs);
  89.  
  90. if ((int)get_transient ("s2m_ipr_" . md5 ("s2member_transient_failed_login_attempts_" . $_SERVER["REMOTE_ADDR"])) > $max)
  91. {
  92. $errors = new WP_Error ("incorrect_password", sprintf (_x ("Max failed logins. Please wait %s and try again.", "s2member-front", "s2member"), $about));
  93.  
  94. foreach(array_keys(get_defined_vars())as$__v)$__refs[$__v]=&$$__v;
  95. do_action ("ws_plugin__s2member_during_stop_brute_force_logins", get_defined_vars ());
  96. unset /* Unset defined __refs, __v. */ ($__refs, $__v);
  97. }
  98. }
  99. return apply_filters ("ws_plugin__s2member_stop_brute_force_logins", ((!empty ($errors)) ? $errors : $user), get_defined_vars ());
  100. }
  101. }
  102. }
  103. ?>

s2member\includes\classes\brute-force.inc.php

Source Code Documentation