PluginProbe
Patchstack – WordPress & Plugins Security / 2.2.1
Patchstack – WordPress & Plugins Security v2.2.1
2.3.7 trunk 2.1.0 2.1.1 2.1.10 2.1.11 2.1.12 2.1.13 2.1.14 2.1.15 2.1.16 2.1.17 2.1.18 2.1.19 2.1.2 2.1.20 2.1.21 2.1.22 2.1.23 2.1.24 2.1.25 2.1.3 2.1.4 2.1.5 2.1.6 All 49 releases
patchstack / includes / hide-login.php

hide-login.php in Patchstack – WordPress & Plugins Security 2.2.1, at includes/hide-login.php

144 lines 4.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 // Do not allow the file to be called directly.
4 if ( ! defined( 'ABSPATH' ) ) {
5 exit;
6 }
7
8 /**
9 * This class is used to hide the login page, if it's enabled.
10 */
11 class P_Hide_Login extends P_Core {
12 /**
13 * Add the actions required to hide the login page.
14 *
15 * @param Patchstack $core
16 * @return void
17 */
18 public function __construct( $core ) {
19 parent::__construct( $core );
20
21 if ( $this->get_option( 'patchstack_license_free', 0 ) == 1 || $this->is_community() ) {
22 return;
23 }
24
25 // Update the renamed login page if it's set to our hardcoded one.
26 if ( get_site_option( 'patchstack_mv_wp_login' ) == 0 && get_site_option( 'patchstack_rename_wp_login' ) == 'swlogin' ) {
27 update_site_option( 'patchstack_rename_wp_login', md5( wp_generate_password( 32, true, true ) ) );
28 }
29
30 // No need to continue if it is not enabled.
31 if ( ! get_site_option( 'patchstack_mv_wp_login' ) || ! get_site_option( 'patchstack_rename_wp_login' ) ) {
32 return;
33 }
34
35 // Register the filters and actions for the functionality.
36 add_action( 'init', [ $this, 'init' ] );
37 add_action( 'wp_logout', [ $this, 'wp_logout' ] );
38 }
39
40 /**
41 * Deny access to wp-login.php if the login page rename feature is enabled.
42 *
43 * @return void
44 */
45 public function init() {
46 // Do not block the user if they are already logged in as that would block a logout.
47 if ( is_user_logged_in() ) {
48 return;
49 }
50
51 // Determine if the user is whitelisted.
52 if ( ( stripos( $_SERVER['REQUEST_URI'], 'wp-login.php' ) !== false || $GLOBALS['pagenow'] === 'wp-login.php' || $_SERVER['PHP_SELF'] === '/wp-login.php' ) && ! $this->is_whitelisted() ) {
53 if ( isset( $_REQUEST['action'] ) && in_array( $_REQUEST['action'], ['confirm_admin_email', 'postpass', 'lostpassword', 'retrievepassword', 'resetpass', 'rp', 'register', 'checkemail', 'confirmaction'] ) ) {
54 return;
55 }
56
57 $this->plugin->firewall_base->display_error_page( 'login' );
58 }
59
60 // If the current page is the renamed login page we give the user access for 10 minutes to the login page.
61 if ( strpos( $_SERVER['REQUEST_URI'], get_site_option( 'patchstack_rename_wp_login' ) ) !== false ) {
62 $this->whitelist_ip();
63 wp_safe_redirect( 'wp-login.php' );
64 exit;
65 }
66 }
67
68 /**
69 * If the user is logging out, whitelist them again so they don't see a blocked page.
70 *
71 * @return void
72 */
73 public function wp_logout() {
74 $this->whitelist_ip();
75 }
76
77 /**
78 * Determine if the IP address is whitelisted.
79 *
80 * @return boolean
81 */
82 private function is_whitelisted() {
83 // Process the whitelist, and remove old ones.
84 $whitelist = get_site_option( 'patchstack_rename_wp_login_whitelist', [] );
85 $new_whitelist = [];
86 $allow = false;
87
88 // Only continue if there are actually any whitelist entries.
89 if ( is_array( $whitelist ) && count( $whitelist ) != 0 ){
90 $ip = $this->get_ip();
91 foreach ( $whitelist as $entry ) {
92
93 // Determine if the whitelist entry is still valid.
94 if ( ( time() - $entry[1] ) <= 600 ) {
95 array_push( $new_whitelist, $entry );
96
97 // Determine if the IP address matches.
98 if ( $ip === $entry[0] ) {
99 $allow = true;
100 }
101 }
102 }
103
104 update_site_option( 'patchstack_rename_wp_login_whitelist', $new_whitelist );
105 }
106
107 return $allow;
108 }
109
110 /**
111 * Whitelist the current IP address, or extend the time.
112 *
113 * @return void
114 */
115 private function whitelist_ip() {
116 $whitelist = get_site_option( 'patchstack_rename_wp_login_whitelist', [] );
117 $new_whitelist = [];
118
119 // If the IP address is already whitelisted, reset the timestamp.
120 if ( is_array( $whitelist ) && count( $whitelist ) != 0 ) {
121 $ip = $this->get_ip();
122 $whitelisted = false;
123 foreach ( $whitelist as $entry ) {
124 // Determine if we should extend the whitelist time or ignore if already whitelisted.
125 if ( $ip === $entry[0] ) {
126 $new_whitelist[] = [ $ip, time() ];
127 $whitelisted = true;
128 } else {
129 $new_whitelist[] = $entry;
130 }
131 }
132
133 // Whitelist the IP address.
134 if ( ! $whitelisted ) {
135 $new_whitelist[] = [ $ip, time() ];
136 }
137
138 update_site_option( 'patchstack_rename_wp_login_whitelist', $new_whitelist );
139 } else {
140 update_site_option( 'patchstack_rename_wp_login_whitelist', [ [ $this->get_ip(), time() ] ] );
141 }
142 }
143 }
144