| 1 |
<?php |
| 2 |
// @codingStandardsIgnoreFile |
| 3 |
/** |
| 4 |
* Login checks. |
| 5 |
* |
| 6 |
* Copyright: © 2009-2011 |
| 7 |
* {@link http://websharks-inc.com/ WebSharks, Inc.} |
| 8 |
* (coded in the USA) |
| 9 |
* |
| 10 |
* Released under the terms of the GNU General Public License. |
| 11 |
* You should have received a copy of the GNU General Public License, |
| 12 |
* along with this software. In the main directory, see: /licensing/ |
| 13 |
* If not, see: {@link http://www.gnu.org/licenses/}. |
| 14 |
* |
| 15 |
* @package s2Member\Login_Checks |
| 16 |
* @since 131025 |
| 17 |
*/ |
| 18 |
if(!defined('WPINC')) // MUST have WordPress. |
| 19 |
exit ('Do not access this file directly.'); |
| 20 |
|
| 21 |
if(!class_exists('c_ws_plugin__s2member_login_checks')) |
| 22 |
{ |
| 23 |
/** |
| 24 |
* Login checks. |
| 25 |
* |
| 26 |
* @package s2Member\Login_Checks |
| 27 |
* @since 131025 |
| 28 |
*/ |
| 29 |
class c_ws_plugin__s2member_login_checks |
| 30 |
{ |
| 31 |
/** |
| 32 |
* Assists in multisite User authentication. |
| 33 |
* |
| 34 |
* @package s2Member\Login_Checks |
| 35 |
* @since 131025 |
| 36 |
* |
| 37 |
* @attaches-to ``add_filter('wp_authenticate_user');`` |
| 38 |
* |
| 39 |
* @param WP_User|WP_Error Expects either a WP_User or WP_Error object passed in by the Filter. |
| 40 |
* |
| 41 |
* @return WP_User|WP_Error WP_User or WP_Error object (if there was a problem). |
| 42 |
*/ |
| 43 |
public static function ms_wp_authenticate_user($user_or_wp_error) |
| 44 |
{ |
| 45 |
if(!is_multisite()) return $user_or_wp_error; |
| 46 |
|
| 47 |
foreach(array_keys(get_defined_vars()) as $__v) $__refs[$__v] =& $$__v; |
| 48 |
do_action('ws_plugin__s2member_before_ms_wp_authenticate_user', get_defined_vars()); |
| 49 |
unset($__refs, $__v); |
| 50 |
|
| 51 |
if(is_a($user_or_wp_error, 'WP_User') && ($user = $user_or_wp_error) && $user->ID && !is_super_admin($user->ID) && !in_array(get_current_blog_id(), array_keys(get_blogs_of_user($user->ID)), TRUE)) |
| 52 |
$user_or_wp_error = new WP_Error('invalid_username', _x('<strong>ERROR</strong>: Invalid username for this site.', 's2member-front', 's2member')); |
| 53 |
|
| 54 |
return apply_filters('ws_plugin__s2member_ms_wp_authenticate_user', $user_or_wp_error, get_defined_vars()); |
| 55 |
} |
| 56 |
|
| 57 |
/** |
| 58 |
* Assists in User authentication (stops max simultaneous logins). |
| 59 |
* |
| 60 |
* @package s2Member\Login_Checks |
| 61 |
* @since 131025 |
| 62 |
* |
| 63 |
* @attaches-to ``add_filter('wp_authenticate_user');`` |
| 64 |
* |
| 65 |
* @param WP_User|WP_Error Expects either a WP_User or WP_Error object passed in by the Filter. |
| 66 |
* |
| 67 |
* @return WP_User|WP_Error WP_User or WP_Error object (if there was a problem). |
| 68 |
*/ |
| 69 |
public static function stop_simultaneous_logins($user_or_wp_error) |
| 70 |
{ |
| 71 |
if(!($max = $GLOBALS['WS_PLUGIN__']['s2member']['o']['max_simultaneous_logins'])) |
| 72 |
return $user_or_wp_error; // Simultaneous login monitoring not enabled here. |
| 73 |
|
| 74 |
foreach(array_keys(get_defined_vars()) as $__v) $__refs[$__v] =& $$__v; |
| 75 |
do_action('ws_plugin__s2member_before_stop_simultaneous_logins', get_defined_vars()); |
| 76 |
unset($__refs, $__v); |
| 77 |
|
| 78 |
if(is_a($user_or_wp_error, 'WP_User') && ($user = $user_or_wp_error) && $user->ID && !is_super_admin($user->ID) && c_ws_plugin__s2member_login_checks::get_simultaneous_logins($user->user_login) + 1 > $max) |
| 79 |
$user_or_wp_error = new WP_Error('max_simultaneous_logins', sprintf(_x('<strong>ERROR</strong>: Max simultaneous logins for username: %1$s. Please wait %2$s and try again.', 's2member-front', 's2member'), $user->user_login, $GLOBALS['WS_PLUGIN__']['s2member']['o']['max_simultaneous_logins_timeout'])); |
| 80 |
|
| 81 |
return apply_filters('ws_plugin__s2member_stop_simultaneous_logins', $user_or_wp_error, get_defined_vars()); |
| 82 |
} |
| 83 |
|
| 84 |
/** |
| 85 |
* Monitors simultaneous logins (updates timer on each page view). |
| 86 |
* |
| 87 |
* @package s2Member\Login_Checks |
| 88 |
* @since 131025 |
| 89 |
* |
| 90 |
* @attaches-to ``add_action('init');`` |
| 91 |
* |
| 92 |
* @param WP_User|WP_Error Expects either a WP_User or WP_Error object passed in by the Filter. |
| 93 |
* |
| 94 |
* @return WP_User|WP_Error WP_User or WP_Error object (if there was a problem). |
| 95 |
*/ |
| 96 |
public static function monitor_simultaneous_logins() |
| 97 |
{ |
| 98 |
if(!is_user_logged_in() || is_super_admin()) |
| 99 |
return; // Nothing to do here. |
| 100 |
|
| 101 |
if(!($max = $GLOBALS['WS_PLUGIN__']['s2member']['o']['max_simultaneous_logins'])) |
| 102 |
return; // Simultaneous login monitoring not enabled here. |
| 103 |
|
| 104 |
foreach(array_keys(get_defined_vars()) as $__v) $__refs[$__v] =& $$__v; |
| 105 |
do_action('ws_plugin__s2member_before_monitor_simultaneous_logins', get_defined_vars()); |
| 106 |
unset($__refs, $__v); |
| 107 |
|
| 108 |
$user = wp_get_current_user(); |
| 109 |
$username = $user->user_login; // The username. |
| 110 |
c_ws_plugin__s2member_login_checks::update_simultaneous_logins($username, $user, 'timer'); |
| 111 |
} |
| 112 |
|
| 113 |
/** |
| 114 |
* Handles simultaneous logouts. |
| 115 |
* |
| 116 |
* @package s2Member\Login_Checks |
| 117 |
* @since 131025 |
| 118 |
* |
| 119 |
* @attaches-to ``add_action('clear_auth_cookie');`` |
| 120 |
*/ |
| 121 |
public static function simultaneous_logout() |
| 122 |
{ |
| 123 |
if(!is_user_logged_in() || is_super_admin()) |
| 124 |
return; // Nothing to do here. |
| 125 |
|
| 126 |
if(!($max = $GLOBALS['WS_PLUGIN__']['s2member']['o']['max_simultaneous_logins'])) |
| 127 |
return; // Simultaneous login monitoring not enabled here. |
| 128 |
|
| 129 |
foreach(array_keys(get_defined_vars()) as $__v) $__refs[$__v] =& $$__v; |
| 130 |
do_action('ws_plugin__s2member_before_simultaneous_logout', get_defined_vars()); |
| 131 |
unset($__refs, $__v); |
| 132 |
|
| 133 |
$user = wp_get_current_user(); |
| 134 |
$username = $user->user_login; // The username. |
| 135 |
c_ws_plugin__s2member_login_checks::update_simultaneous_logins($username, $user, 'decrement'); |
| 136 |
} |
| 137 |
|
| 138 |
/** |
| 139 |
* Get simultaneous logins for a particular username. |
| 140 |
* |
| 141 |
* @package s2Member\Login_Checks |
| 142 |
* @since 131025 |
| 143 |
* |
| 144 |
* @param string $username Expects a username (e.g., a `user_login` value). |
| 145 |
* |
| 146 |
* @return integer Current number of simultaneous logins. |
| 147 |
*/ |
| 148 |
public static function get_simultaneous_logins($username) |
| 149 |
{ |
| 150 |
if(!$username) return 0; // Nothing to get. |
| 151 |
|
| 152 |
foreach(array_keys(get_defined_vars()) as $__v) $__refs[$__v] =& $$__v; |
| 153 |
do_action('ws_plugin__s2member_before_get_simultaneous_logins', get_defined_vars()); |
| 154 |
unset($__refs, $__v); |
| 155 |
|
| 156 |
$prefix = 's2m_slm_'; // s2Member Transient prefix for all simultaneous login monitoring. |
| 157 |
$transient_entries = $prefix.md5('s2member_simultaneous_login_entries_for_'.strtolower((string)$username)); |
| 158 |
|
| 159 |
$timeout = $GLOBALS['WS_PLUGIN__']['s2member']['o']['max_simultaneous_logins_timeout']; |
| 160 |
$timeout_ago = strtotime('-'.$timeout); // e.g., 30 minutes ago. |
| 161 |
|
| 162 |
$entries = (is_array($entries = get_transient($transient_entries))) ? $entries : array(); |
| 163 |
foreach($entries as $_entry => $_time /* Auto-expire entries, based on time. */) |
| 164 |
if($_time < $timeout_ago) unset($entries[$_entry]); |
| 165 |
|
| 166 |
return apply_filters('ws_plugin__s2member_get_simultaneous_logins', count($entries), get_defined_vars()); |
| 167 |
} |
| 168 |
|
| 169 |
/** |
| 170 |
* Update simultaneous logins for a particular username. |
| 171 |
* |
| 172 |
* @package s2Member\Login_Checks |
| 173 |
* @since 131025 |
| 174 |
* |
| 175 |
* @attaches-to ``add_action('wp_login');`` |
| 176 |
* |
| 177 |
* @param string $username Expects a username (e.g., a `user_login` value). |
| 178 |
* @param WP_User|null $user When fired against `wp_login` this receives a WP_User object also. |
| 179 |
* @param string $action Default action is to increment the counter. This can be set to `decrement` or NULL to do nothing. |
| 180 |
*/ |
| 181 |
public static function update_simultaneous_logins($username, $user = NULL, $action = 'increment') |
| 182 |
{ |
| 183 |
if(!$username) return; // Nothing to do. |
| 184 |
|
| 185 |
foreach(array_keys(get_defined_vars()) as $__v) $__refs[$__v] =& $$__v; |
| 186 |
do_action('ws_plugin__s2member_before_update_simultaneous_logins', get_defined_vars()); |
| 187 |
unset($__refs, $__v); |
| 188 |
|
| 189 |
$prefix = 's2m_slm_'; // s2Member Transient prefix for all simultaneous login monitoring. |
| 190 |
$transient_entries = $prefix.md5('s2member_simultaneous_login_entries_for_'.strtolower((string)$username)); |
| 191 |
|
| 192 |
$timeout = $GLOBALS['WS_PLUGIN__']['s2member']['o']['max_simultaneous_logins_timeout']; |
| 193 |
$timeout_ago = strtotime('-'.$timeout); // e.g., 30 minutes ago. |
| 194 |
|
| 195 |
$entries = (is_array($entries = get_transient($transient_entries))) ? $entries : array(); |
| 196 |
foreach($entries as $_entry => $_time /* Auto-expire entries, based on time. */) |
| 197 |
if($_time < $timeout_ago) unset($entries[$_entry]); |
| 198 |
|
| 199 |
$total_entries = count($entries); // May need this below. |
| 200 |
|
| 201 |
if($action === 'increment') $entries[] = time(); // New entry. |
| 202 |
else if($action === 'decrement') array_pop($entries); // Remove last entry. |
| 203 |
else if($action === 'timer') // Update time on last entry; or add a new entry. |
| 204 |
$entries[(($total_entries) ? $total_entries - 1 : 0)] = time(); |
| 205 |
|
| 206 |
set_transient($transient_entries, $entries, strtotime('+'.$timeout) - time()); |
| 207 |
|
| 208 |
foreach(array_keys(get_defined_vars()) as $__v) $__refs[$__v] =& $$__v; |
| 209 |
do_action('ws_plugin__s2member_after_update_simultaneous_logins', get_defined_vars()); |
| 210 |
unset($__refs, $__v); |
| 211 |
} |
| 212 |
} |
| 213 |
} |
| 214 |
|