PageRenderTime 45ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-content/plugins/better-wp-security/core/modules/hide-backend/class-itsec-hide-backend.php

https://gitlab.com/najomie/fit-hippie
PHP | 361 lines | 184 code | 107 blank | 70 comment | 69 complexity | e8d6827891345d4f023d5f4f3dbff518 MD5 | raw file
  1. <?php
  2. class ITSEC_Hide_Backend {
  3. private
  4. $settings,
  5. $auth_cookie_expired;
  6. function run() {
  7. $this->settings = ITSEC_Modules::get_settings( 'hide-backend' );
  8. if ( ! $this->settings['enabled'] ) {
  9. return;
  10. }
  11. add_filter( 'itsec_filter_apache_server_config_modification', array( $this, 'filter_apache_server_config_modification' ) );
  12. add_filter( 'itsec_filter_litespeed_server_config_modification', array( $this, 'filter_apache_server_config_modification' ) );
  13. add_filter( 'itsec_filter_nginx_server_config_modification', array( $this, 'filter_nginx_server_config_modification' ) );
  14. $jetpack_active_modules = get_option( 'jetpack_active_modules' );
  15. if ( is_multisite() && function_exists( 'is_plugin_active_for_network' ) ) { //see if Jetpack is active
  16. $is_jetpack_active = in_array( 'jetpack/jetpack.php', (array) get_option( 'active_plugins', array() ) ) || is_plugin_active_for_network( 'jetpack/jetpack.php' );
  17. } else {
  18. $is_jetpack_active = in_array( 'jetpack/jetpack.php', (array) get_option( 'active_plugins', array() ) );
  19. }
  20. if (
  21. ! (
  22. $is_jetpack_active === true &&
  23. is_array( $jetpack_active_modules ) &&
  24. in_array( 'json-api', $jetpack_active_modules ) &&
  25. isset( $_GET['action'] ) &&
  26. $_GET['action'] == 'jetpack_json_api_authorization'
  27. )
  28. ) {
  29. $this->auth_cookie_expired = false;
  30. add_action( 'auth_cookie_expired', array( $this, 'auth_cookie_expired' ) );
  31. add_action( 'init', array( $this, 'execute_hide_backend' ), 1000 );
  32. add_action( 'login_init', array( $this, 'execute_hide_backend_login' ) );
  33. add_action( 'plugins_loaded', array( $this, 'plugins_loaded' ), 11 );
  34. add_filter( 'body_class', array( $this, 'remove_admin_bar' ) );
  35. add_filter( 'loginout', array( $this, 'filter_loginout' ) );
  36. add_filter( 'wp_redirect', array( $this, 'filter_login_url' ), 10, 2 );
  37. add_filter( 'lostpassword_url', array( $this, 'filter_login_url' ), 10, 2 );
  38. add_filter( 'site_url', array( $this, 'filter_login_url' ), 10, 2 );
  39. add_filter( 'retrieve_password_message', array( $this, 'retrieve_password_message' ) );
  40. add_filter( 'comment_moderation_text', array( $this, 'comment_moderation_text' ) );
  41. remove_action( 'template_redirect', 'wp_redirect_admin_locations', 1000 );
  42. }
  43. }
  44. public function filter_apache_server_config_modification( $modification ) {
  45. require_once( dirname( __FILE__ ) . '/config-generators.php' );
  46. return ITSEC_Hide_Backend_Config_Generators::filter_apache_server_config_modification( $modification, $this->settings );
  47. }
  48. public function filter_nginx_server_config_modification( $modification ) {
  49. require_once( dirname( __FILE__ ) . '/config-generators.php' );
  50. return ITSEC_Hide_Backend_Config_Generators::filter_nginx_server_config_modification( $modification, $this->settings );
  51. }
  52. /**
  53. * Lets the module know that this is a reauthorization
  54. *
  55. * @since 4.1
  56. *
  57. * @return void
  58. */
  59. public function auth_cookie_expired() {
  60. $this->auth_cookie_expired = true;
  61. wp_clear_auth_cookie();
  62. }
  63. /**
  64. * @param $notify_message
  65. *
  66. * @since 4.5
  67. *
  68. * @param sting $notify_message Notification message
  69. *
  70. * @return string Notification message
  71. */
  72. public function comment_moderation_text( $notify_message ) {
  73. preg_match_all( "#(https?:\/\/((.*)wp-admin(.*)))#", $notify_message, $urls );
  74. if ( isset( $urls ) && is_array( $urls ) && isset( $urls[0] ) ) {
  75. foreach ( $urls[0] as $url ) {
  76. $notify_message = str_replace( trim( $url ), wp_login_url( trim( $url ) ), $notify_message );
  77. }
  78. }
  79. return $notify_message;
  80. }
  81. /**
  82. * Execute hide backend functionality
  83. *
  84. * @since 4.0
  85. *
  86. * @return void
  87. */
  88. public
  89. function execute_hide_backend() {
  90. if ( get_site_option( 'users_can_register' ) == 1 && isset( $_SERVER['REQUEST_URI'] ) && $_SERVER['REQUEST_URI'] == ITSEC_Lib::get_home_root() . $this->settings['register'] ) {
  91. wp_redirect( wp_login_url() . '?action=register' );
  92. exit;
  93. }
  94. //redirect wp-admin and wp-register.php to 404 when not logged in
  95. if (
  96. (
  97. (
  98. get_site_option( 'users_can_register' ) == false &&
  99. (
  100. isset( $_SERVER['REQUEST_URI'] ) && strpos( $_SERVER['REQUEST_URI'], 'wp-register.php' ) ||
  101. isset( $_SERVER['REQUEST_URI'] ) && strpos( $_SERVER['REQUEST_URI'], 'wp-signup.php' )
  102. )
  103. ) ||
  104. (
  105. isset( $_SERVER['REQUEST_URI'] ) && strpos( $_SERVER['REQUEST_URI'], 'wp-login.php' ) && is_user_logged_in() !== true
  106. ) ||
  107. ( is_admin() && is_user_logged_in() !== true ) ||
  108. (
  109. $this->settings['register'] != 'wp-register.php' &&
  110. strpos( $_SERVER['REQUEST_URI'], 'wp-register.php' ) !== false ||
  111. strpos( $_SERVER['REQUEST_URI'], 'wp-signup.php' ) !== false ||
  112. (
  113. isset( $_REQUEST['redirect_to'] ) &&
  114. strpos( $_REQUEST['redirect_to'], 'wp-admin/customize.php' ) !== false
  115. )
  116. )
  117. ) &&
  118. strpos( $_SERVER['REQUEST_URI'], 'admin-ajax.php' ) === false
  119. && $this->auth_cookie_expired === false
  120. ) {
  121. global $itsec_is_old_admin;
  122. $itsec_is_old_admin = true;
  123. if ( isset( $this->settings['theme_compat'] ) && $this->settings['theme_compat'] === true ) { //theme compat (process theme and redirect to a 404)
  124. wp_redirect( ITSEC_Lib::get_home_root() . sanitize_title( isset( $this->settings['theme_compat_slug'] ) ? $this->settings['theme_compat_slug'] : 'not_found' ), 302 );
  125. exit;
  126. } else {
  127. // Throw a 403 forbidden
  128. wp_die( __( 'This has been disabled.', 'better-wp-security' ), 403 );
  129. }
  130. }
  131. $url_info = parse_url( $_SERVER['REQUEST_URI'] );
  132. $login_path = site_url( $this->settings['slug'], 'relative' );
  133. $login_path_trailing_slash = site_url( $this->settings['slug'] . '/', 'relative' );
  134. if ( $url_info['path'] === $login_path || $url_info['path'] === $login_path_trailing_slash ) {
  135. if ( ! is_user_logged_in() ) {
  136. //Add the login form
  137. if ( isset( $this->settings['post_logout_slug'] ) && strlen( trim( $this->settings['post_logout_slug'] ) ) > 0 && isset( $_GET['action'] ) && sanitize_text_field( $_GET['action'] ) == trim( $this->settings['post_logout_slug'] ) ) {
  138. do_action( 'itsec_custom_login_slug' ); //add hook here for custom users
  139. }
  140. //suppress error messages due to timing
  141. error_reporting( 0 );
  142. @ini_set( 'display_errors', 0 );
  143. status_header( 200 );
  144. //don't allow domain mapping to redirect
  145. if ( defined( 'DOMAIN_MAPPING' ) && DOMAIN_MAPPING == 1 ) {
  146. remove_action( 'login_head', 'redirect_login_to_orig' );
  147. }
  148. if ( ! function_exists( 'login_header' ) ) {
  149. include( ABSPATH . 'wp-login.php' );
  150. exit;
  151. }
  152. } elseif ( ! isset( $_GET['action'] ) || ( sanitize_text_field( $_GET['action'] ) != 'logout' && sanitize_text_field( $_GET['action'] ) != 'postpass' && ( isset( $this->settings['post_logout_slug'] ) && strlen( trim( $this->settings['post_logout_slug'] ) ) > 0 && sanitize_text_field( $_GET['action'] ) != trim( $this->settings['post_logout_slug'] ) ) ) ) {
  153. //Just redirect them to the dashboard (for logged in users)
  154. if ( $this->auth_cookie_expired === false ) {
  155. wp_redirect( get_admin_url() );
  156. exit();
  157. }
  158. } elseif ( isset( $_GET['action'] ) && ( sanitize_text_field( $_GET['action'] ) == 'postpass' || ( isset( $this->settings['post_logout_slug'] ) && strlen( trim( $this->settings['post_logout_slug'] ) ) > 0 && sanitize_text_field( $_GET['action'] ) == trim( $this->settings['post_logout_slug'] ) ) ) ) {
  159. //handle private posts for
  160. if ( isset( $this->settings['post_logout_slug'] ) && strlen( trim( $this->settings['post_logout_slug'] ) ) > 0 && sanitize_text_field( $_GET['action'] ) == trim( $this->settings['post_logout_slug'] ) ) {
  161. do_action( 'itsec_custom_login_slug' ); //add hook here for custom users
  162. }
  163. //suppress error messages due to timing
  164. error_reporting( 0 );
  165. @ini_set( 'display_errors', 0 );
  166. status_header( 200 ); //its a good login page. make sure we say so
  167. //include the login page where we need it
  168. if ( ! function_exists( 'login_header' ) ) {
  169. include( ABSPATH . '/wp-login.php' );
  170. exit;
  171. }
  172. //Take them back to the page if we need to
  173. if ( isset( $_SERVER['HTTP_REFERRER'] ) ) {
  174. wp_redirect( sanitize_text_field( $_SERVER['HTTP_REFERRER'] ) );
  175. exit();
  176. }
  177. }
  178. }
  179. }
  180. /**
  181. * Filter the old login page out
  182. *
  183. * @return void
  184. */
  185. public function execute_hide_backend_login() {
  186. if ( strpos( $_SERVER['REQUEST_URI'], 'wp-login.php' ) ) { //are we on the login page
  187. global $itsec_is_old_admin;
  188. $itsec_is_old_admin = true;
  189. ITSEC_Lib::set_404();
  190. }
  191. }
  192. /**
  193. * Filters redirects for correct login URL
  194. *
  195. * @since 4.0
  196. *
  197. * @param string $url URL redirecting to
  198. *
  199. * @return string Correct redirect URL
  200. */
  201. public function filter_login_url( $url ) {
  202. $t = str_replace( 'wp-login.php', $this->settings['slug'], $url );
  203. return str_replace( 'wp-login.php', $this->settings['slug'], $url );
  204. }
  205. /**
  206. * Filter meta link
  207. *
  208. * @since 4.2
  209. *
  210. * @param string $link the link
  211. *
  212. * @return string the link
  213. */
  214. public function filter_loginout( $link ) {
  215. return str_replace( 'wp-login.php', $this->settings['slug'], $link );
  216. }
  217. /**
  218. * Actions for plugins loaded.
  219. *
  220. * Makes certain logout is processed on NGINX.
  221. *
  222. * @return void
  223. */
  224. public function plugins_loaded() {
  225. if ( is_user_logged_in() && isset( $_GET['action'] ) && sanitize_text_field( $_GET['action'] ) == 'logout' ) {
  226. check_admin_referer( 'log-out' );
  227. wp_logout();
  228. $redirect_to = ! empty( $_REQUEST['redirect_to'] ) ? $_REQUEST['redirect_to'] : 'wp-login.php?loggedout=true';
  229. wp_safe_redirect( $redirect_to );
  230. exit();
  231. }
  232. }
  233. /**
  234. * Removes the admin bar class from the body tag
  235. *
  236. * @param array $classes body tag classes
  237. *
  238. * @return array body tag classes
  239. */
  240. public function remove_admin_bar( $classes ) {
  241. if ( is_admin() && is_user_logged_in() !== true ) {
  242. foreach ( $classes as $key => $value ) {
  243. if ( $value == 'admin-bar' ) {
  244. unset( $classes[ $key ] );
  245. }
  246. }
  247. }
  248. return $classes;
  249. }
  250. public function retrieve_password_message( $message ) {
  251. return str_replace( 'wp-login.php', $this->settings['slug'], $message );
  252. return $message;
  253. }
  254. }