PageRenderTime 24ms CodeModel.GetById 13ms RepoModel.GetById 1ms app.codeStats 0ms

/wp-content/plugins/s2member/includes/classes/login-redirects.inc.php

https://gitlab.com/Gashler/dp
PHP | 212 lines | 105 code | 32 blank | 75 comment | 43 complexity | e04c803761d7a9ad6d1fb7ebe9ea1d1e MD5 | raw file
  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. if (!class_exists ("c_ws_plugin__s2member_login_redirects"))
  20. {
  21. /**
  22. * Login redirections.
  23. *
  24. * @package s2Member\Login_Redirects
  25. * @since 3.5
  26. */
  27. class c_ws_plugin__s2member_login_redirects
  28. {
  29. /**
  30. * Assists in multisite User authentication.
  31. *
  32. * @package s2Member\Login_Redirects
  33. * @since 130405
  34. *
  35. * @attaches-to ``add_filter("wp_authenticate_user");``
  36. *
  37. * @param WP_User|WP_Error Expects either a WP_User or WP_Error object passed in by the Filter.
  38. * @return WP_User|WP_Error WP_User or WP_Error object (if there was a problem).
  39. */
  40. public static function ms_wp_authenticate_user ($user_or_wp_error = FALSE)
  41. {
  42. if(!is_multisite()) return $user_or_wp_error;
  43. foreach(array_keys(get_defined_vars())as$__v)$__refs[$__v]=&$$__v;
  44. do_action ("ws_plugin__s2member_before_ms_wp_authenticate_user", get_defined_vars ());
  45. unset /* Unset defined __refs, __v. */ ($__refs, $__v);
  46. 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))
  47. $user_or_wp_error = new WP_Error("invalid_username", _x("<strong>ERROR</strong>: Invalid username for this site.", "s2member-front", "s2member"));
  48. return apply_filters ("ws_plugin__s2member_ms_wp_authenticate_user", $user_or_wp_error, get_defined_vars ());
  49. }
  50. /**
  51. * Handles login redirections.
  52. *
  53. * @package s2Member\Login_Redirects
  54. * @since 3.5
  55. *
  56. * @attaches-to ``add_action("wp_login");``
  57. *
  58. * @param str $username Expects Username.
  59. * @param WP_User $user Expects a WP_User object instance.
  60. * @return null Or exits script execution after a redirection takes place.
  61. */
  62. public static function login_redirect ($username = FALSE, $user = FALSE)
  63. {
  64. foreach(array_keys(get_defined_vars())as$__v)$__refs[$__v]=&$$__v;
  65. do_action ("ws_plugin__s2member_before_login_redirect", get_defined_vars ());
  66. unset /* Unset defined __refs, __v. */ ($__refs, $__v);
  67. if (is_string($username) && $username && is_object ($user) && !empty ($user->ID) && ($user_id = $user->ID))
  68. {
  69. update_user_option ($user_id, "s2member_last_login_time", time());
  70. if /* Have we got this yet? */ (!get_user_option ("s2member_registration_ip", $user_id))
  71. update_user_option ($user_id, "s2member_registration_ip", $_SERVER["REMOTE_ADDR"]);
  72. if (($logins = (int)get_user_option ("s2member_login_counter", $user_id) + 1) >= 1 || ($logins = 1))
  73. update_user_option ($user_id, "s2member_login_counter", $logins);
  74. if /* Nag em? */ ($GLOBALS["WS_PLUGIN__"]["s2member"]["o"]["custom_reg_password"])
  75. delete_user_setting ("default_password_nag") . update_user_option ($user_id, "default_password_nag", false, true);
  76. $disable_login_ip_restrictions = apply_filters ("ws_plugin__s2member_disable_login_ip_restrictions", false, get_defined_vars ());
  77. if (($ok = true) && !is_super_admin ($user_id) && $username !== "demo" && !$disable_login_ip_restrictions)
  78. $ok = c_ws_plugin__s2member_ip_restrictions::ip_restrictions_ok ($_SERVER["REMOTE_ADDR"], $username);
  79. if (($redirect = apply_filters ("ws_plugin__s2member_login_redirect", (($user->has_cap ("edit_posts")) ? false : true), get_defined_vars ())))
  80. {
  81. $obey_redirect_to = apply_filters ("ws_plugin__s2member_obey_login_redirect_to", /* By default, we obey this. */ true, get_defined_vars ());
  82. 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"]))
  83. {
  84. foreach(array_keys(get_defined_vars())as$__v)$__refs[$__v]=&$$__v;
  85. do_action ("ws_plugin__s2member_during_login_redirect", get_defined_vars ());
  86. unset /* Unset defined __refs, __v. */ ($__refs, $__v);
  87. if($redirect && is_string ($redirect)) $redirect = $redirect; // Custom?
  88. else if ($redirection_url = c_ws_plugin__s2member_login_redirects::login_redirection_url ($user))
  89. $redirect = $redirection_url; // Special redirection URL (overrides LWP).
  90. else // Else we use the Login Welcome Page configured for s2Member.
  91. $redirect = get_page_link ($GLOBALS["WS_PLUGIN__"]["s2member"]["o"]["login_welcome_page"]);
  92. if($GLOBALS["WS_PLUGIN__"]["s2member"]["o"]["login_redirection_always_http"])
  93. $redirect = preg_replace("/^https\:\/\//i", "http://", $redirect);
  94. wp_redirect($redirect).exit();
  95. }
  96. }
  97. }
  98. foreach(array_keys(get_defined_vars())as$__v)$__refs[$__v]=&$$__v;
  99. do_action ("ws_plugin__s2member_after_login_redirect", get_defined_vars ());
  100. unset /* Unset defined __refs, __v. */ ($__refs, $__v);
  101. return /* Return for uniformity. */;
  102. }
  103. /**
  104. * Parses a Special Login Redirection URL.
  105. *
  106. * @package s2Member\Login_Redirects
  107. * @since 3.5
  108. *
  109. * @param obj $user Optional. A WP_User object. Defaults to the current User, if logged-in.
  110. * @param bool $root_returns_false Defaults to false. True if the function should return false when a URL is reduced to the site root.
  111. * @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.
  112. */
  113. public static function login_redirection_url ($user = FALSE, $root_returns_false = FALSE)
  114. {
  115. foreach(array_keys(get_defined_vars())as$__v)$__refs[$__v]=&$$__v;
  116. do_action ("ws_plugin__s2member_before_login_redirection_url", get_defined_vars ());
  117. unset /* Unset defined __refs, __v. */ ($__refs, $__v);
  118. $url = $GLOBALS["WS_PLUGIN__"]["s2member"]["o"]["login_redirection_override"];
  119. $url = c_ws_plugin__s2member_login_redirects::fill_login_redirect_rc_vars ($url, $user, $root_returns_false);
  120. return apply_filters ("ws_plugin__s2member_login_redirection_url", $url, get_defined_vars ());
  121. }
  122. /**
  123. * Parses a Special Login Redirection URI.
  124. *
  125. * @package s2Member\Login_Redirects
  126. * @since 3.5
  127. *
  128. * @param obj $user Optional. A WP_User object. Defaults to the current User, if logged-in.
  129. * @param bool $root_returns_false Defaults to false. True if the function should return false when a URI is reduced to the site root.
  130. * @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.
  131. */
  132. public static function login_redirection_uri ($user = FALSE, $root_returns_false = FALSE)
  133. {
  134. foreach(array_keys(get_defined_vars())as$__v)$__refs[$__v]=&$$__v;
  135. do_action ("ws_plugin__s2member_before_login_redirection_uri", get_defined_vars ());
  136. unset /* Unset defined __refs, __v. */ ($__refs, $__v);
  137. if (($url = c_ws_plugin__s2member_login_redirects::login_redirection_url ($user, $root_returns_false)))
  138. $uri = c_ws_plugin__s2member_utils_urls::parse_uri ($url);
  139. return apply_filters ("ws_plugin__s2member_login_redirection_uri", ((!empty ($uri)) ? $uri : false), get_defined_vars ());
  140. }
  141. /**
  142. * Fills Replacement Codes in Special Redirection URLs.
  143. *
  144. * @package s2Member\Login_Redirects
  145. * @since 3.5
  146. *
  147. * @param str $url A URL with possible Replacement Codes in it.
  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 URL is reduced to the site root.
  150. * @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.
  151. */
  152. public static function fill_login_redirect_rc_vars ($url = FALSE, $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_fill_login_redirect_rc_vars", get_defined_vars ());
  156. unset /* Unset defined __refs, __v. */ ($__refs, $__v);
  157. $url = /* Force ``$url`` to a string value. */ (string)$url;
  158. $orig_url = /* Record the original URL that was passed in. */ $url;
  159. $user = ((is_object ($user) || is_object ($user = (is_user_logged_in ()) ? wp_get_current_user () : false)) && !empty ($user->ID)) ? $user : false;
  160. $user_id = ($user) ? (string)$user->ID : "";
  161. $user_login = ($user) ? (string)strtolower ($user->user_login) : "";
  162. $user_level = (string)c_ws_plugin__s2member_user_access::user_access_level ($user);
  163. $user_role = (string)c_ws_plugin__s2member_user_access::user_access_role ($user);
  164. $user_ccaps = (string)implode ("-", c_ws_plugin__s2member_user_access::user_access_ccaps ($user));
  165. $user_logins = ($user) ? (string)(int)get_user_option ("s2member_login_counter", $user_id) : "-1";
  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. 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)))
  173. $url = /* Defaults to Home Page. We don't return invalid URLs produced by empty Replacement Codes ( i.e. with `//` ). */ site_url ("/");
  174. if ($root_returns_false /* Used by s2Member's security gate. */ && c_ws_plugin__s2member_utils_conds::is_site_root ($url))
  175. $url = /* In case we need to return false on root URLs (i.e. don't protect the Home Page inadvertently). */ false;
  176. return apply_filters ("ws_plugin__s2member_fill_login_redirect_rc_vars", $url, get_defined_vars ());
  177. }
  178. }
  179. }
  180. ?>