| 1 |
<?php |
| 2 |
// @codingStandardsIgnoreFile |
| 3 |
/** |
| 4 |
* Login redirections. |
| 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_Redirects |
| 16 |
* @since 3.5 |
| 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_redirects')) |
| 22 |
{ |
| 23 |
/** |
| 24 |
* Login redirections. |
| 25 |
* |
| 26 |
* @package s2Member\Login_Redirects |
| 27 |
* @since 3.5 |
| 28 |
*/ |
| 29 |
class c_ws_plugin__s2member_login_redirects |
| 30 |
{ |
| 31 |
/** |
| 32 |
* Handles login redirections. |
| 33 |
* |
| 34 |
* @package s2Member\Login_Redirects |
| 35 |
* @since 3.5 |
| 36 |
* |
| 37 |
* @attaches-to ``add_action('wp_login');`` |
| 38 |
* |
| 39 |
* @param string $username Expects Username. |
| 40 |
* @param WP_User $user Expects a WP_User object instance. |
| 41 |
* |
| 42 |
* @return null Or exits script execution after a redirection takes place. |
| 43 |
*/ |
| 44 |
public static function login_redirect($username = '', $user = NULL) |
| 45 |
{ |
| 46 |
foreach(array_keys(get_defined_vars()) as $__v) $__refs[$__v] =& $$__v; |
| 47 |
do_action('ws_plugin__s2member_before_login_redirect', get_defined_vars()); |
| 48 |
unset($__refs, $__v); // Housekeeping. |
| 49 |
|
| 50 |
$ci = $GLOBALS['WS_PLUGIN__']['s2member']['o']['ruris_case_sensitive'] ? '' : 'i'; |
| 51 |
|
| 52 |
if(is_string($username) && $username && is_object($user) && !empty($user->ID) && ($user_id = $user->ID)) |
| 53 |
{ |
| 54 |
update_user_option($user_id, 's2member_last_login_time', time()); |
| 55 |
|
| 56 |
$logins = (int)get_user_option('s2member_login_counter', $user_id) + 1; |
| 57 |
update_user_option($user_id, 's2member_login_counter', $logins); |
| 58 |
|
| 59 |
if(!get_user_option('s2member_registration_ip', $user_id)) |
| 60 |
update_user_option($user_id, 's2member_registration_ip', c_ws_plugin__s2member_utils_ip::current()); |
| 61 |
|
| 62 |
if($GLOBALS['WS_PLUGIN__']['s2member']['o']['custom_reg_password']) |
| 63 |
{ |
| 64 |
delete_user_setting('default_password_nag'); |
| 65 |
update_user_option($user_id, 'default_password_nag', FALSE, TRUE); |
| 66 |
} |
| 67 |
$ok = TRUE; // Initialize IP restriction being OK here. This is for filters. |
| 68 |
if($username !== 'demo' && !is_super_admin($user_id) // Exclude the `demo` user, super admins, and anyone who can edit posts. |
| 69 |
&& !apply_filters('ws_plugin__s2member_disable_login_ip_restrictions', $user->has_cap('edit_posts') ? TRUE : FALSE, get_defined_vars()) |
| 70 |
) $ok = c_ws_plugin__s2member_ip_restrictions::ip_restrictions_ok(c_ws_plugin__s2member_utils_ip::current(), strtolower($username)); |
| 71 |
|
| 72 |
if(apply_filters('ws_plugin__s2member_login_redirection_always_http', force_ssl_admin() && stripos(get_option('siteurl'), 'https://') !== 0)) |
| 73 |
if(!empty($_REQUEST['redirect_to']) && is_string($_REQUEST['redirect_to']) && strpos($_REQUEST['redirect_to'], 'wp-admin') === FALSE) |
| 74 |
{ |
| 75 |
$_REQUEST['redirect_to'] = preg_replace('/^https\:\/\//i', 'http://', $_REQUEST['redirect_to']); |
| 76 |
if(stripos($_REQUEST['redirect_to'], 'http://') !== 0) // Force an absolute URL in this case. |
| 77 |
{ |
| 78 |
$redirect_uri = $_REQUEST['redirect_to']; // e.g., `/path/with/?query=args` |
| 79 |
$home_path = trim((string)@parse_url(home_url('/'), PHP_URL_PATH), '/'); |
| 80 |
$http_home_base = trim(preg_replace('/\/'.preg_quote($home_path, '/').'\/$/'.$ci, '', home_url('/', 'http')), '/'); |
| 81 |
$_REQUEST['redirect_to'] = $http_home_base.'/'.ltrim($redirect_uri, '/'); |
| 82 |
} |
| 83 |
} |
| 84 |
if(($redirect = apply_filters('ws_plugin__s2member_login_redirect', $user->has_cap('edit_posts') ? FALSE : TRUE, get_defined_vars()))) |
| 85 |
{ |
| 86 |
$obey_redirect_to = apply_filters('ws_plugin__s2member_obey_login_redirect_to', TRUE, get_defined_vars()); |
| 87 |
|
| 88 |
if($obey_redirect_to && (empty($_REQUEST['redirect_to']) || !is_string($_REQUEST['redirect_to']) || $_REQUEST['redirect_to'] === admin_url() || preg_match('/^\/?wp-admin\/?$/'.$ci, $_REQUEST['redirect_to']))) |
| 89 |
$obey_redirect_to = FALSE; // Do not obey default redirect_to locations; like those inside the default admin area. |
| 90 |
|
| 91 |
else if($obey_redirect_to && !empty($_REQUEST['redirect_to_automatic']) && is_string($redirect)) |
| 92 |
$obey_redirect_to = FALSE; // Do not obey automatic redirects when a custom redirection filter applies. |
| 93 |
// ↑ NOTE: this will apply to s2Member Pro's One-Time-Offers (Upon Login) also. |
| 94 |
|
| 95 |
if(!$obey_redirect_to) // Only if we are NOT obeying the `redirect_to` variable. |
| 96 |
{ |
| 97 |
foreach(array_keys(get_defined_vars()) as $__v) $__refs[$__v] =& $$__v; |
| 98 |
do_action('ws_plugin__s2member_during_login_redirect', get_defined_vars()); |
| 99 |
unset($__refs, $__v); // Housekeeping. |
| 100 |
|
| 101 |
$is_lwp = FALSE; // Initialize LWP detection flag. |
| 102 |
|
| 103 |
if($redirect && is_string($redirect)) |
| 104 |
$redirect = $redirect; |
| 105 |
|
| 106 |
else if(($login_redirection_url = c_ws_plugin__s2member_login_redirects::login_redirection_url($user))) |
| 107 |
{ |
| 108 |
$is_lwp = TRUE; // Flag as being a hard-coded LWP URL in this case. |
| 109 |
$redirect = $login_redirection_url; // Special redirection URL. |
| 110 |
} |
| 111 |
else if($GLOBALS['WS_PLUGIN__']['s2member']['o']['login_welcome_page']) |
| 112 |
{ |
| 113 |
$is_lwp = TRUE; // Flag as being a hard-coded LWP URL in this case. |
| 114 |
$redirect = get_page_link($GLOBALS['WS_PLUGIN__']['s2member']['o']['login_welcome_page']); |
| 115 |
} |
| 116 |
else $redirect = home_url('/'); // Default to the home page. |
| 117 |
|
| 118 |
if(apply_filters('ws_plugin__s2member_login_redirection_always_http', force_ssl_admin() && stripos(get_option('siteurl'), 'https://') !== 0)) |
| 119 |
{ |
| 120 |
$redirect = preg_replace('/^https\:\/\//i', 'http://', $redirect); |
| 121 |
if(stripos($redirect, 'http://') !== 0) // Force absolute. |
| 122 |
{ |
| 123 |
$redirect_uri = $redirect; // e.g., `/path/with/?query=args` |
| 124 |
$home_path = trim((string)@parse_url(home_url('/'), PHP_URL_PATH), '/'); |
| 125 |
$http_home_base = trim(preg_replace('/\/'.preg_quote($home_path, '/').'\/$/'.$ci, '', home_url('/', 'http')), '/'); |
| 126 |
$redirect = $http_home_base.'/'.ltrim($redirect_uri, '/'); |
| 127 |
} |
| 128 |
} |
| 129 |
if($is_lwp) // Allow offsite redirection? |
| 130 |
wp_redirect($redirect); // Perhaps an offsite location. |
| 131 |
else wp_safe_redirect($redirect); // Default behavior. |
| 132 |
|
| 133 |
exit(); // Stop here; redirecting now. |
| 134 |
} |
| 135 |
} |
| 136 |
} |
| 137 |
do_action('ws_plugin__s2member_after_login_redirect', get_defined_vars()); |
| 138 |
} |
| 139 |
|
| 140 |
/** |
| 141 |
* Parses a Special Login Redirection URL. |
| 142 |
* |
| 143 |
* @package s2Member\Login_Redirects |
| 144 |
* @since 3.5 |
| 145 |
* |
| 146 |
* @param object $user Optional. A WP_User object. Defaults to the current User, if logged-in. |
| 147 |
* @param bool $root_returns_false Defaults to false. True if the function should return false when a URL is reduced to the site root. |
| 148 |
* |
| 149 |
* @return string|bool A Special Login Redirection URL with Replacement Codes having been parsed, or false if ``$root_returns_false = true`` and the URL is the site root. |
| 150 |
*/ |
| 151 |
public static function login_redirection_url($user = NULL, $root_returns_false = FALSE) |
| 152 |
{ |
| 153 |
foreach(array_keys(get_defined_vars()) as $__v) $__refs[$__v] =& $$__v; |
| 154 |
do_action('ws_plugin__s2member_before_login_redirection_url', get_defined_vars()); |
| 155 |
unset($__refs, $__v); // Housekeeping. |
| 156 |
|
| 157 |
$url = $GLOBALS['WS_PLUGIN__']['s2member']['o']['login_redirection_override']; |
| 158 |
$url = c_ws_plugin__s2member_login_redirects::fill_login_redirect_rc_vars($url, $user, $root_returns_false); |
| 159 |
|
| 160 |
return apply_filters('ws_plugin__s2member_login_redirection_url', $url, get_defined_vars()); |
| 161 |
} |
| 162 |
|
| 163 |
/** |
| 164 |
* Parses a Special Login Redirection URI. |
| 165 |
* |
| 166 |
* @package s2Member\Login_Redirects |
| 167 |
* @since 3.5 |
| 168 |
* |
| 169 |
* @param object $user Optional. A WP_User object. Defaults to the current User, if logged-in. |
| 170 |
* @param bool $root_returns_false Defaults to false. True if the function should return false when a URI is reduced to the site root. |
| 171 |
* |
| 172 |
* @return string|bool A Special Login Redirection URI with Replacement Codes having been parsed, or false if ``$root_returns_false = true`` and the URI is the site root. |
| 173 |
*/ |
| 174 |
public static function login_redirection_uri($user = NULL, $root_returns_false = FALSE) |
| 175 |
{ |
| 176 |
foreach(array_keys(get_defined_vars()) as $__v) $__refs[$__v] =& $$__v; |
| 177 |
do_action('ws_plugin__s2member_before_login_redirection_uri', get_defined_vars()); |
| 178 |
unset($__refs, $__v); // Housekeeping. |
| 179 |
|
| 180 |
if(($url = c_ws_plugin__s2member_login_redirects::login_redirection_url($user, $root_returns_false))) |
| 181 |
$uri = c_ws_plugin__s2member_utils_urls::parse_uri($url); |
| 182 |
|
| 183 |
return apply_filters('ws_plugin__s2member_login_redirection_uri', !empty($uri) ? $uri : FALSE, get_defined_vars()); |
| 184 |
} |
| 185 |
|
| 186 |
/** |
| 187 |
* Fills Replacement Codes in Special Redirection URLs. |
| 188 |
* |
| 189 |
* @package s2Member\Login_Redirects |
| 190 |
* @since 3.5 |
| 191 |
* |
| 192 |
* @param string $url A URL with possible Replacement Codes in it. |
| 193 |
* @param object $user Optional. A `WP_User` object. Defaults to the current User, if logged-in. |
| 194 |
* @param bool $root_returns_false Defaults to false. True if the function should return false when a URL is reduced to the site root. |
| 195 |
* |
| 196 |
* @return string|bool A Special Login Redirection URL with Replacement Codes having been parsed, or false if ``$root_returns_false = true`` and the URL is the site root. |
| 197 |
*/ |
| 198 |
public static function fill_login_redirect_rc_vars($url = '', $user = NULL, $root_returns_false = FALSE) |
| 199 |
{ |
| 200 |
foreach(array_keys(get_defined_vars()) as $__v) $__refs[$__v] =& $$__v; |
| 201 |
do_action('ws_plugin__s2member_before_fill_login_redirect_rc_vars', get_defined_vars()); |
| 202 |
unset($__refs, $__v); // Housekeeping. |
| 203 |
|
| 204 |
$url = (string)$url; // Force ``$url`` to a string value. |
| 205 |
$orig_url = $url; // Record the original URL that was passed in. |
| 206 |
|
| 207 |
$user = (is_object($user) || is_object($user = wp_get_current_user())) |
| 208 |
&& !empty($user->ID) ? $user : NULL; |
| 209 |
|
| 210 |
$user_id = ($user) ? (string)$user->ID : ''; |
| 211 |
$user_login = ($user) ? (string)strtolower($user->user_login) : ''; |
| 212 |
$user_nicename = ($user) ? (string)strtolower($user->user_nicename) : ''; |
| 213 |
|
| 214 |
$user_level = (string)c_ws_plugin__s2member_user_access::user_access_level($user); |
| 215 |
$user_role = (string)c_ws_plugin__s2member_user_access::user_access_role($user); |
| 216 |
$user_ccaps = (string)implode('-', c_ws_plugin__s2member_user_access::user_access_ccaps($user)); |
| 217 |
$user_logins = ($user) ? (string)(int)get_user_option('s2member_login_counter', $user_id) : '-1'; |
| 218 |
|
| 219 |
$url = preg_replace('/%%current_user_login%%/i', c_ws_plugin__s2member_utils_strings::esc_refs(urlencode($user_login)), $url); |
| 220 |
$url = preg_replace('/%%current_user_nicename%%/i', c_ws_plugin__s2member_utils_strings::esc_refs(urlencode($user_nicename)), $url); |
| 221 |
$url = preg_replace('/%%current_user_id%%/i', c_ws_plugin__s2member_utils_strings::esc_refs(urlencode($user_id)), $url); |
| 222 |
$url = preg_replace('/%%current_user_level%%/i', c_ws_plugin__s2member_utils_strings::esc_refs(urlencode($user_level)), $url); |
| 223 |
$url = preg_replace('/%%current_user_role%%/i', c_ws_plugin__s2member_utils_strings::esc_refs(urlencode($user_role)), $url); |
| 224 |
$url = preg_replace('/%%current_user_ccaps%%/i', c_ws_plugin__s2member_utils_strings::esc_refs(urlencode($user_ccaps)), $url); |
| 225 |
$url = preg_replace('/%%current_user_logins%%/i', c_ws_plugin__s2member_utils_strings::esc_refs(urlencode($user_logins)), $url); |
| 226 |
|
| 227 |
if($url !== $orig_url && (!($parse = c_ws_plugin__s2member_utils_urls::parse_url($url, -1, FALSE)) || (!empty($parse['path']) && strpos($parse['path'], '//') !== FALSE))) |
| 228 |
$url = home_url('/'); // Defaults to Home Page. We don't return invalid URLs produced by empty Replacement Codes ( i.e., with `//` ). |
| 229 |
|
| 230 |
if($root_returns_false && c_ws_plugin__s2member_utils_conds::is_site_root($url)) // Used by s2Member's security gate. |
| 231 |
$url = FALSE; // In case we need to return false on root URLs (i.e., don't protect the Home Page inadvertently). |
| 232 |
|
| 233 |
return apply_filters('ws_plugin__s2member_fill_login_redirect_rc_vars', $url, get_defined_vars()); |
| 234 |
} |
| 235 |
} |
| 236 |
} |
| 237 |
|