PageRenderTime 50ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-content/plugins/jetpack/modules/sso/class.jetpack-sso-helpers.php

https://gitlab.com/chernushov881/charity-fund
PHP | 372 lines | 134 code | 40 blank | 198 comment | 24 complexity | 58d5571a282c8e3fe02f5896431a40db MD5 | raw file
  1. <?php // phpcs:ignore WordPress.Files.FileName.InvalidClassFileName
  2. /**
  3. * A collection of helper functions used in the SSO module.
  4. *
  5. * @package automattic/jetpack
  6. */
  7. if ( ! class_exists( 'Jetpack_SSO_Helpers' ) ) :
  8. /**
  9. * A collection of helper functions used in the SSO module.
  10. *
  11. * @since 4.1.0
  12. */
  13. class Jetpack_SSO_Helpers {
  14. /**
  15. * Determine if the login form should be hidden or not
  16. *
  17. * @return bool
  18. **/
  19. public static function should_hide_login_form() {
  20. /**
  21. * Remove the default log in form, only leave the WordPress.com log in button.
  22. *
  23. * @module sso
  24. *
  25. * @since 3.1.0
  26. *
  27. * @param bool get_option( 'jetpack_sso_remove_login_form', false ) Should the default log in form be removed. Default to false.
  28. */
  29. return (bool) apply_filters( 'jetpack_remove_login_form', get_option( 'jetpack_sso_remove_login_form', false ) );
  30. }
  31. /**
  32. * Returns a boolean value for whether logging in by matching the WordPress.com user email to a
  33. * Jetpack site user's email is allowed.
  34. *
  35. * @return bool
  36. */
  37. public static function match_by_email() {
  38. $match_by_email = ( 1 === (int) get_option( 'jetpack_sso_match_by_email', true ) ) ? true : false;
  39. $match_by_email = defined( 'WPCC_MATCH_BY_EMAIL' ) ? WPCC_MATCH_BY_EMAIL : $match_by_email;
  40. /**
  41. * Link the local account to an account on WordPress.com using the same email address.
  42. *
  43. * @module sso
  44. *
  45. * @since 2.6.0
  46. *
  47. * @param bool $match_by_email Should we link the local account to an account on WordPress.com using the same email address. Default to false.
  48. */
  49. return (bool) apply_filters( 'jetpack_sso_match_by_email', $match_by_email );
  50. }
  51. /**
  52. * Returns a boolean for whether users are allowed to register on the Jetpack site with SSO,
  53. * even though the site disallows normal registrations.
  54. *
  55. * @param object|null $user_data WordPress.com user information.
  56. * @return bool
  57. */
  58. public static function new_user_override( $user_data = null ) {
  59. $new_user_override = defined( 'WPCC_NEW_USER_OVERRIDE' ) ? WPCC_NEW_USER_OVERRIDE : false;
  60. /**
  61. * Allow users to register on your site with a WordPress.com account, even though you disallow normal registrations.
  62. * If you return a string that corresponds to a user role, the user will be given that role.
  63. *
  64. * @module sso
  65. *
  66. * @since 2.6.0
  67. * @since 4.6 $user_data object is now passed to the jetpack_sso_new_user_override filter
  68. *
  69. * @param bool $new_user_override Allow users to register on your site with a WordPress.com account. Default to false.
  70. * @param object|null $user_data An object containing the user data returned from WordPress.com.
  71. */
  72. $role = apply_filters( 'jetpack_sso_new_user_override', $new_user_override, $user_data );
  73. if ( $role ) {
  74. if ( is_string( $role ) && get_role( $role ) ) {
  75. return $role;
  76. } else {
  77. return get_option( 'default_role' );
  78. }
  79. }
  80. return false;
  81. }
  82. /**
  83. * Returns a boolean value for whether two-step authentication is required for SSO.
  84. *
  85. * @since 4.1.0
  86. *
  87. * @return bool
  88. */
  89. public static function is_two_step_required() {
  90. /**
  91. * Is it required to have 2-step authentication enabled on WordPress.com to use SSO?
  92. *
  93. * @module sso
  94. *
  95. * @since 2.8.0
  96. *
  97. * @param bool get_option( 'jetpack_sso_require_two_step' ) Does SSO require 2-step authentication?
  98. */
  99. return (bool) apply_filters( 'jetpack_sso_require_two_step', get_option( 'jetpack_sso_require_two_step', false ) );
  100. }
  101. /**
  102. * Returns a boolean for whether a user that is attempting to log in will be automatically
  103. * redirected to WordPress.com to begin the SSO flow.
  104. *
  105. * @return bool
  106. */
  107. public static function bypass_login_forward_wpcom() {
  108. /**
  109. * Redirect the site's log in form to WordPress.com's log in form.
  110. *
  111. * @module sso
  112. *
  113. * @since 3.1.0
  114. *
  115. * @param bool false Should the site's log in form be automatically forwarded to WordPress.com's log in form.
  116. */
  117. return (bool) apply_filters( 'jetpack_sso_bypass_login_forward_wpcom', false );
  118. }
  119. /**
  120. * Returns a boolean for whether the SSO login form should be displayed as the default
  121. * when both the default and SSO login form allowed.
  122. *
  123. * @since 4.1.0
  124. *
  125. * @return bool
  126. */
  127. public static function show_sso_login() {
  128. if ( self::should_hide_login_form() ) {
  129. return true;
  130. }
  131. /**
  132. * Display the SSO login form as the default when both the default and SSO login forms are enabled.
  133. *
  134. * @module sso
  135. *
  136. * @since 4.1.0
  137. *
  138. * @param bool true Should the SSO login form be displayed by default when the default login form is also enabled?
  139. */
  140. return (bool) apply_filters( 'jetpack_sso_default_to_sso_login', true );
  141. }
  142. /**
  143. * Returns a boolean for whether the two step required checkbox, displayed on the Jetpack admin page, should be disabled.
  144. *
  145. * @since 4.1.0
  146. *
  147. * @return bool
  148. */
  149. public static function is_require_two_step_checkbox_disabled() {
  150. return (bool) has_filter( 'jetpack_sso_require_two_step' );
  151. }
  152. /**
  153. * Returns a boolean for whether the match by email checkbox, displayed on the Jetpack admin page, should be disabled.
  154. *
  155. * @since 4.1.0
  156. *
  157. * @return bool
  158. */
  159. public static function is_match_by_email_checkbox_disabled() {
  160. return defined( 'WPCC_MATCH_BY_EMAIL' ) || has_filter( 'jetpack_sso_match_by_email' );
  161. }
  162. /**
  163. * Returns an array of hosts that SSO will redirect to.
  164. *
  165. * Instead of accessing JETPACK__API_BASE within the method directly, we set it as the
  166. * default for $api_base due to restrictions with testing constants in our tests.
  167. *
  168. * @since 4.3.0
  169. * @since 4.6.0 Added public-api.wordpress.com as an allowed redirect
  170. *
  171. * @param array $hosts Allowed redirect hosts.
  172. * @param string $api_base Base API URL.
  173. *
  174. * @return array
  175. */
  176. public static function allowed_redirect_hosts( $hosts, $api_base = JETPACK__API_BASE ) {
  177. if ( empty( $hosts ) ) {
  178. $hosts = array();
  179. }
  180. $hosts[] = 'wordpress.com';
  181. $hosts[] = 'jetpack.wordpress.com';
  182. $hosts[] = 'public-api.wordpress.com';
  183. $hosts[] = 'jetpack.com';
  184. if ( false === strpos( $api_base, 'jetpack.wordpress.com/jetpack' ) ) {
  185. $base_url_parts = wp_parse_url( esc_url_raw( $api_base ) );
  186. if ( $base_url_parts && ! empty( $base_url_parts['host'] ) ) {
  187. $hosts[] = $base_url_parts['host'];
  188. }
  189. }
  190. return array_unique( $hosts );
  191. }
  192. /**
  193. * Generate a new user from a SSO attempt.
  194. *
  195. * @param object $user_data WordPress.com user information.
  196. */
  197. public static function generate_user( $user_data ) {
  198. $username = $user_data->login;
  199. /**
  200. * Determines how many times the SSO module can attempt to randomly generate a user.
  201. *
  202. * @module sso
  203. *
  204. * @since 4.3.2
  205. *
  206. * @param int 5 By default, SSO will attempt to random generate a user up to 5 times.
  207. */
  208. $num_tries = (int) apply_filters( 'jetpack_sso_allowed_username_generate_retries', 5 );
  209. $exists = username_exists( $username );
  210. $tries = 0;
  211. while ( $exists && $tries++ < $num_tries ) {
  212. $username = $user_data->login . '_' . $user_data->ID . '_' . wp_rand();
  213. $exists = username_exists( $username );
  214. }
  215. if ( $exists ) {
  216. return false;
  217. }
  218. $user = (object) array();
  219. $user->user_pass = wp_generate_password( 20 );
  220. $user->user_login = wp_slash( $username );
  221. $user->user_email = wp_slash( $user_data->email );
  222. $user->display_name = $user_data->display_name;
  223. $user->first_name = $user_data->first_name;
  224. $user->last_name = $user_data->last_name;
  225. $user->url = $user_data->url;
  226. $user->description = $user_data->description;
  227. if ( isset( $user_data->role ) && $user_data->role ) {
  228. $user->role = $user_data->role;
  229. }
  230. $created_user_id = wp_insert_user( $user );
  231. update_user_meta( $created_user_id, 'wpcom_user_id', $user_data->ID );
  232. return get_userdata( $created_user_id );
  233. }
  234. /**
  235. * Determines how long the auth cookie is valid for when a user logs in with SSO.
  236. *
  237. * @return int result of the jetpack_sso_auth_cookie_expiration filter.
  238. */
  239. public static function extend_auth_cookie_expiration_for_sso() {
  240. /**
  241. * Determines how long the auth cookie is valid for when a user logs in with SSO.
  242. *
  243. * @module sso
  244. *
  245. * @since 4.4.0
  246. * @since 6.1.0 Fixed a typo. Filter was previously jetpack_sso_auth_cookie_expirtation.
  247. *
  248. * @param int YEAR_IN_SECONDS
  249. */
  250. return (int) apply_filters( 'jetpack_sso_auth_cookie_expiration', YEAR_IN_SECONDS );
  251. }
  252. /**
  253. * Determines if the SSO form should be displayed for the current action.
  254. *
  255. * @since 4.6.0
  256. *
  257. * @param string $action SSO action being performed.
  258. *
  259. * @return bool Is SSO allowed for the current action?
  260. */
  261. public static function display_sso_form_for_action( $action ) {
  262. /**
  263. * Allows plugins the ability to overwrite actions where the SSO form is allowed to be used.
  264. *
  265. * @module sso
  266. *
  267. * @since 4.6.0
  268. *
  269. * @param array $allowed_actions_for_sso
  270. */
  271. $allowed_actions_for_sso = (array) apply_filters(
  272. 'jetpack_sso_allowed_actions',
  273. array(
  274. 'login',
  275. 'jetpack-sso',
  276. 'jetpack_json_api_authorization',
  277. )
  278. );
  279. return in_array( $action, $allowed_actions_for_sso, true );
  280. }
  281. /**
  282. * This method returns an environment array that is meant to simulate `$_REQUEST` when the initial
  283. * JSON API auth request was made.
  284. *
  285. * @since 4.6.0
  286. *
  287. * @return array|bool
  288. */
  289. public static function get_json_api_auth_environment() {
  290. if ( empty( $_COOKIE['jetpack_sso_original_request'] ) ) {
  291. return false;
  292. }
  293. $original_request = esc_url_raw( $_COOKIE['jetpack_sso_original_request'] );
  294. $parsed_url = wp_parse_url( $original_request );
  295. if ( empty( $parsed_url ) || empty( $parsed_url['query'] ) ) {
  296. return false;
  297. }
  298. $args = array();
  299. wp_parse_str( $parsed_url['query'], $args );
  300. if ( empty( $args ) || empty( $args['action'] ) ) {
  301. return false;
  302. }
  303. if ( 'jetpack_json_api_authorization' !== $args['action'] ) {
  304. return false;
  305. }
  306. return array_merge(
  307. $args,
  308. array( 'jetpack_json_api_original_query' => $original_request )
  309. );
  310. }
  311. /**
  312. * Check if the site has a custom login page URL, and return it.
  313. * If default login page URL is used (`wp-login.php`), `null` will be returned.
  314. *
  315. * @return string|null
  316. */
  317. public static function get_custom_login_url() {
  318. $login_url = wp_login_url();
  319. if ( 'wp-login.php' === substr( $login_url, -12 ) ) {
  320. // No custom URL found.
  321. return null;
  322. }
  323. $site_url = trailingslashit( site_url() );
  324. if ( 0 !== strpos( $login_url, $site_url ) ) {
  325. // Something went wrong, we can't properly extract the custom URL.
  326. return null;
  327. }
  328. // Extracting the "path" part of the URL, because we don't need the `site_url` part.
  329. return str_ireplace( $site_url, '', $login_url );
  330. }
  331. }
  332. endif;