| 1 |
<?php |
| 2 |
/** |
| 3 |
* Login redirections. |
| 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\Login_Redirects |
| 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_login_redirects")) |
| 21 |
{ |
| 22 |
/** |
| 23 |
* Login redirections. |
| 24 |
* |
| 25 |
* @package s2Member\Login_Redirects |
| 26 |
* @since 3.5 |
| 27 |
*/ |
| 28 |
class c_ws_plugin__s2member_login_redirects |
| 29 |
{ |
| 30 |
/** |
| 31 |
* Handles login redirections. |
| 32 |
* |
| 33 |
* @package s2Member\Login_Redirects |
| 34 |
* @since 3.5 |
| 35 |
* |
| 36 |
* @attaches-to: ``add_action("wp_login");`` |
| 37 |
* |
| 38 |
* @param str $username Expects Username to be passed in by the Action Hook. |
| 39 |
* @return null Or exits script execution after a redirection takes place. |
| 40 |
*/ |
| 41 |
public static function login_redirect ($username = FALSE) |
| 42 |
{ |
| 43 |
eval('foreach(array_keys(get_defined_vars())as$__v)$__refs[$__v]=&$$__v;'); |
| 44 |
do_action ("ws_plugin__s2member_before_login_redirect", get_defined_vars ()); |
| 45 |
unset ($__refs, $__v); /* Unset defined __refs, __v. */ |
| 46 |
/**/ |
| 47 |
$username = (!$username && is_object ($user = wp_get_current_user ())) ? strtolower ($user->user_login) : strtolower ($username); |
| 48 |
/**/ |
| 49 |
if ($username && (is_object ($user) || is_object ($user = new WP_User ($username))) && ($user_id = $user->ID)) |
| 50 |
{ |
| 51 |
if (!get_user_option ("s2member_registration_ip", $user_id)) /* Have we got this yet? */ |
| 52 |
update_user_option ($user_id, "s2member_registration_ip", $_SERVER["REMOTE_ADDR"]); |
| 53 |
/**/ |
| 54 |
if (($logins = (int)get_user_option ("s2member_login_counter", $user_id) + 1) >= 1 || ($logins = 1)) |
| 55 |
update_user_option ($user_id, "s2member_login_counter", $logins); |
| 56 |
/**/ |
| 57 |
if ($GLOBALS["WS_PLUGIN__"]["s2member"]["o"]["custom_reg_password"]) /* Nag em? */ |
| 58 |
delete_user_setting("default_password_nag") . update_user_option ($user_id, "default_password_nag", false, true); |
| 59 |
/**/ |
| 60 |
$disable_login_ip_restrictions = apply_filters ("ws_plugin__s2member_disable_login_ip_restrictions", false, get_defined_vars ()); |
| 61 |
/**/ |
| 62 |
if (($ok = true) && !is_super_admin ($user_id) && $username !== "demo" && !$disable_login_ip_restrictions) |
| 63 |
$ok = c_ws_plugin__s2member_ip_restrictions::ip_restrictions_ok ($_SERVER["REMOTE_ADDR"], $username); |
| 64 |
/**/ |
| 65 |
if (($redirect = apply_filters ("ws_plugin__s2member_login_redirect", (($user->has_cap ("edit_posts")) ? false : true), get_defined_vars ()))) |
| 66 |
{ |
| 67 |
$obey_redirect_to = apply_filters ("ws_plugin__s2member_obey_login_redirect_to", /* By default, we obey this. */ true, get_defined_vars ()); |
| 68 |
/**/ |
| 69 |
if (!$obey_redirect_to || empty ($_REQUEST["redirect_to"]) || $_REQUEST["redirect_to"] === "wp-admin/" || $_REQUEST["redirect_to"] === admin_url ()) |
| 70 |
{ |
| 71 |
eval('foreach(array_keys(get_defined_vars())as$__v)$__refs[$__v]=&$$__v;'); |
| 72 |
do_action ("ws_plugin__s2member_during_login_redirect", get_defined_vars ()); |
| 73 |
unset ($__refs, $__v); /* Unset defined __refs, __v. */ |
| 74 |
/**/ |
| 75 |
if ($redirect && is_string ($redirect)) /* Is this a string? */ |
| 76 |
wp_redirect($redirect); /* Dynamic URL introduced by a Filter. */ |
| 77 |
/**/ |
| 78 |
else if ($redirection_url = c_ws_plugin__s2member_login_redirects::login_redirection_url ($user)) |
| 79 |
wp_redirect($redirection_url); /* Special Redirection URL configured with s2Member. */ |
| 80 |
/**/ |
| 81 |
else /* Else we use the Login Welcome Page configured for s2Member. */ |
| 82 |
wp_redirect(get_page_link ($GLOBALS["WS_PLUGIN__"]["s2member"]["o"]["login_welcome_page"])); |
| 83 |
/**/ |
| 84 |
exit (); /* Clean exit. */ |
| 85 |
} |
| 86 |
} |
| 87 |
} |
| 88 |
/**/ |
| 89 |
eval('foreach(array_keys(get_defined_vars())as$__v)$__refs[$__v]=&$$__v;'); |
| 90 |
do_action ("ws_plugin__s2member_after_login_redirect", get_defined_vars ()); |
| 91 |
unset ($__refs, $__v); /* Unset defined __refs, __v. */ |
| 92 |
/**/ |
| 93 |
return; /* Return for uniformity. */ |
| 94 |
} |
| 95 |
/** |
| 96 |
* Parses a Special Login Redirection URL. |
| 97 |
* |
| 98 |
* @package s2Member\Login_Redirects |
| 99 |
* @since 3.5 |
| 100 |
* |
| 101 |
* @param obj $user Optional. A WP_User object. Defaults to the current User, if logged-in. |
| 102 |
* @param bool $root_returns_false Defaults to false. True if the function should return false when a URL is reduced to the site root. |
| 103 |
* @return str|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. |
| 104 |
*/ |
| 105 |
public static function login_redirection_url ($user = FALSE, $root_returns_false = FALSE) |
| 106 |
{ |
| 107 |
eval('foreach(array_keys(get_defined_vars())as$__v)$__refs[$__v]=&$$__v;'); |
| 108 |
do_action ("ws_plugin__s2member_before_login_redirection_url", get_defined_vars ()); |
| 109 |
unset ($__refs, $__v); /* Unset defined __refs, __v. */ |
| 110 |
/**/ |
| 111 |
$url = $GLOBALS["WS_PLUGIN__"]["s2member"]["o"]["login_redirection_override"]; |
| 112 |
$url = c_ws_plugin__s2member_login_redirects::fill_login_redirect_rc_vars ($url, $user, $root_returns_false); |
| 113 |
/**/ |
| 114 |
return apply_filters ("ws_plugin__s2member_login_redirection_url", $url, get_defined_vars ()); |
| 115 |
} |
| 116 |
/** |
| 117 |
* Parses a Special Login Redirection URI. |
| 118 |
* |
| 119 |
* @package s2Member\Login_Redirects |
| 120 |
* @since 3.5 |
| 121 |
* |
| 122 |
* @param obj $user Optional. A WP_User object. Defaults to the current User, if logged-in. |
| 123 |
* @param bool $root_returns_false Defaults to false. True if the function should return false when a URI is reduced to the site root. |
| 124 |
* @return str|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. |
| 125 |
*/ |
| 126 |
public static function login_redirection_uri ($user = FALSE, $root_returns_false = FALSE) |
| 127 |
{ |
| 128 |
eval('foreach(array_keys(get_defined_vars())as$__v)$__refs[$__v]=&$$__v;'); |
| 129 |
do_action ("ws_plugin__s2member_before_login_redirection_uri", get_defined_vars ()); |
| 130 |
unset ($__refs, $__v); /* Unset defined __refs, __v. */ |
| 131 |
/**/ |
| 132 |
if (($url = c_ws_plugin__s2member_login_redirects::login_redirection_url ($user, $root_returns_false))) |
| 133 |
$uri = c_ws_plugin__s2member_utils_urls::parse_uri ($url); |
| 134 |
/**/ |
| 135 |
return apply_filters ("ws_plugin__s2member_login_redirection_uri", ((!empty ($uri)) ? $uri : false), get_defined_vars ()); |
| 136 |
} |
| 137 |
/** |
| 138 |
* Fills Replacement Codes in Special Redirection URLs. |
| 139 |
* |
| 140 |
* @package s2Member\Login_Redirects |
| 141 |
* @since 3.5 |
| 142 |
* |
| 143 |
* @param str $url A URL with possible Replacement Codes in it. |
| 144 |
* @param obj $user Optional. A WP_User object. Defaults to the current User, if logged-in. |
| 145 |
* @param bool $root_returns_false Defaults to false. True if the function should return false when a URL is reduced to the site root. |
| 146 |
* @return str|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. |
| 147 |
*/ |
| 148 |
public static function fill_login_redirect_rc_vars ($url = FALSE, $user = FALSE, $root_returns_false = FALSE) |
| 149 |
{ |
| 150 |
eval('foreach(array_keys(get_defined_vars())as$__v)$__refs[$__v]=&$$__v;'); |
| 151 |
do_action ("ws_plugin__s2member_before_fill_login_redirect_rc_vars", get_defined_vars ()); |
| 152 |
unset ($__refs, $__v); /* Unset defined __refs, __v. */ |
| 153 |
/**/ |
| 154 |
$orig_url = $url; /* Record the original URL that was passed in. */ |
| 155 |
/**/ |
| 156 |
$user = ((is_object ($user) || is_object ($user = (is_user_logged_in ()) ? wp_get_current_user () : false)) && !empty ($user->ID)) ? $user : false; |
| 157 |
/**/ |
| 158 |
$user_login = ($user) ? (string)strtolower ($user->user_login) : ""; |
| 159 |
$user_id = ($user) ? (string)$user->ID : ""; |
| 160 |
/**/ |
| 161 |
$user_level = (string)c_ws_plugin__s2member_user_access::user_access_level ($user); |
| 162 |
$user_role = (string)c_ws_plugin__s2member_user_access::user_access_role ($user); |
| 163 |
$user_ccaps = (string)implode ("-", c_ws_plugin__s2member_user_access::user_access_ccaps ($user)); |
| 164 |
$user_logins = ($user) ? (string)(int)get_user_option ("s2member_login_counter", $user_id) : "-1"; |
| 165 |
/**/ |
| 166 |
$url = preg_replace ("/%%current_user_login%%/i", c_ws_plugin__s2member_utils_strings::esc_ds ($user_login), $url); |
| 167 |
$url = preg_replace ("/%%current_user_id%%/i", c_ws_plugin__s2member_utils_strings::esc_ds ($user_id), $url); |
| 168 |
$url = preg_replace ("/%%current_user_level%%/i", c_ws_plugin__s2member_utils_strings::esc_ds ($user_level), $url); |
| 169 |
$url = preg_replace ("/%%current_user_role%%/i", c_ws_plugin__s2member_utils_strings::esc_ds ($user_role), $url); |
| 170 |
$url = preg_replace ("/%%current_user_ccaps%%/i", c_ws_plugin__s2member_utils_strings::esc_ds ($user_ccaps), $url); |
| 171 |
$url = preg_replace ("/%%current_user_logins%%/i", c_ws_plugin__s2member_utils_strings::esc_ds ($user_logins), $url); |
| 172 |
/**/ |
| 173 |
if ( /* Only if s2Member's fault » */$url !== $orig_url && (!($parse = @parse_url ($url)) || strpos ($parse["path"], "//") !== false)) |
| 174 |
$url = site_url ("/"); /* Defaults to home page. We don't return invalid URLs produced by empty Replacement Codes ( i.e. // ). */ |
| 175 |
/**/ |
| 176 |
if ($root_returns_false /* Used by s2Member's security gate. */ && c_ws_plugin__s2member_utils_conds::is_site_root ($url)) |
| 177 |
$url = false; /* In case we need to return false on root URLs ( i.e. don't protect the Home Page inadvertently ). */ |
| 178 |
/**/ |
| 179 |
return apply_filters ("ws_plugin__s2member_fill_login_redirect_rc_vars", $url, get_defined_vars ()); |
| 180 |
} |
| 181 |
} |
| 182 |
} |
| 183 |
?> |