Source Code Documentation


s2member\includes\classes\login-redirects.inc.php


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

s2member\includes\classes\login-redirects.inc.php

Source Code Documentation