| 1 |
<?php |
| 2 |
// Exit if accessed directly. |
| 3 |
if ( ! defined( 'ABSPATH' ) ) { |
| 4 |
exit; |
| 5 |
} |
| 6 |
|
| 7 |
/** |
| 8 |
* Helper function to get the normalized custom login slug. |
| 9 |
* |
| 10 |
* Returns the login slug without leading/trailing slashes in lowercase. |
| 11 |
* |
| 12 |
* @return string |
| 13 |
*/ |
| 14 |
function ns_shield_get_custom_slug() { |
| 15 |
$slug = get_option( 'ns_shield_login_url', '/mysecurelogin/' ); |
| 16 |
// Usuń ukośniki z pocz� |
| 17 |
tku i końca oraz zamień na małe litery. |
| 18 |
$slug = strtolower( untrailingslashit( $slug ) ); |
| 19 |
$slug = ltrim( $slug, '/' ); |
| 20 |
return $slug; |
| 21 |
} |
| 22 |
|
| 23 |
/** |
| 24 |
* Adds a rewrite rule for the custom login URL. |
| 25 |
* |
| 26 |
* @return void |
| 27 |
*/ |
| 28 |
function ns_shield_add_rewrite_rule() { |
| 29 |
if ( get_option( 'ns_shield_login_url_enabled', false ) ) { |
| 30 |
$custom_slug = ns_shield_get_custom_slug(); |
| 31 |
add_rewrite_rule( '^' . preg_quote( $custom_slug, '/' ) . '/?$', 'index.php?ns_shield_custom_login=1', 'top' ); |
| 32 |
} |
| 33 |
} |
| 34 |
// Ustawiamy priorytet 1, aby reguła została zarejestrowana jak najwcześniej. |
| 35 |
add_action( 'init', 'ns_shield_add_rewrite_rule', 1 ); |
| 36 |
|
| 37 |
/** |
| 38 |
* Registers the query variable for custom login. |
| 39 |
* |
| 40 |
* @param array $vars Existing query variables. |
| 41 |
* @return array Modified query variables. |
| 42 |
*/ |
| 43 |
function ns_shield_query_vars( $vars ) { |
| 44 |
$vars[] = 'ns_shield_custom_login'; |
| 45 |
return $vars; |
| 46 |
} |
| 47 |
add_filter( 'query_vars', 'ns_shield_query_vars' ); |
| 48 |
|
| 49 |
/** |
| 50 |
* Blocks access to default login URLs and the admin area for unauthenticated users. |
| 51 |
* |
| 52 |
* @return void |
| 53 |
*/ |
| 54 |
function ns_shield_block_default_urls() { |
| 55 |
// Do not block if the request is for the custom login. |
| 56 |
if ( intval( get_query_var( 'ns_shield_custom_login' ) ) === 1 ) { |
| 57 |
return; |
| 58 |
} |
| 59 |
|
| 60 |
// Process GET parameter "action" safely. |
| 61 |
$action_param = isset( $_GET['action'] ) ? sanitize_text_field( wp_unslash( $_GET['action'] ) ) : ''; |
| 62 |
// phpcs:ignore WordPress.Security.NonceVerification.Recommended |
| 63 |
if ( ! intval( get_query_var( 'ns_shield_custom_login' ) ) && ! empty( $action_param ) && in_array( $action_param, array( 'register', 'lostpassword' ), true ) ) { |
| 64 |
status_header( 404 ); |
| 65 |
nocache_headers(); |
| 66 |
$template = get_query_template( '404' ); |
| 67 |
if ( empty( $template ) || ! file_exists( $template ) ) { |
| 68 |
wp_die( esc_html__( '404 Not Found', 'netsensai-shield' ), esc_html__( 'Not Found', 'netsensai-shield' ), array( 'response' => 404 ) ); |
| 69 |
} |
| 70 |
include( $template ); |
| 71 |
exit; |
| 72 |
} |
| 73 |
|
| 74 |
if ( get_option( 'ns_shield_login_url_enabled', false ) && ! is_user_logged_in() && ! defined( 'DOING_AJAX' ) ) { |
| 75 |
// Pobieramy pełny URL ż� |
| 76 |
dania i wyodrębniamy tylko ścieżkę. |
| 77 |
$request_uri_full = isset( $_SERVER['REQUEST_URI'] ) ? sanitize_text_field( wp_unslash( $_SERVER['REQUEST_URI'] ) ) : ''; |
| 78 |
$parsed_url = wp_parse_url( $request_uri_full ); |
| 79 |
$request_path = isset( $parsed_url['path'] ) ? rtrim( $parsed_url['path'], '/' ) : ''; |
| 80 |
|
| 81 |
// Pobieramy niestandardowy slug logowania. |
| 82 |
$custom_slug = '/' . ns_shield_get_custom_slug(); |
| 83 |
|
| 84 |
// Pozwól na dostęp, jeśli ż� |
| 85 |
danie dotyczy niestandardowego adresu logowania. |
| 86 |
if ( $request_path === $custom_slug ) { |
| 87 |
return; |
| 88 |
} |
| 89 |
|
| 90 |
// Lista domyślnych adresów logowania do zablokowania. |
| 91 |
$default_login_urls = array( |
| 92 |
'/wp-login.php', |
| 93 |
'/wp_login.php', |
| 94 |
'/login.php', |
| 95 |
'/wp-login', |
| 96 |
'/wp_login', |
| 97 |
'/login' |
| 98 |
); |
| 99 |
|
| 100 |
// Jeżeli ż� |
| 101 |
dana ścieżka znajduje się na liście domyślnych adresów. |
| 102 |
if ( in_array( $request_path, $default_login_urls, true ) ) { |
| 103 |
// Bezpieczne przetwarzanie GET parametru "loggedout". |
| 104 |
$loggedout = isset( $_GET['loggedout'] ) ? sanitize_text_field( wp_unslash( $_GET['loggedout'] ) ) : ''; |
| 105 |
// phpcs:ignore WordPress.Security.NonceVerification.Recommended |
| 106 |
if ( $loggedout === 'true' ) { |
| 107 |
wp_redirect( home_url( '/' . ns_shield_get_custom_slug() . '?loggedout=true' ) ); |
| 108 |
exit; |
| 109 |
} |
| 110 |
// W przeciwnym razie blokujemy dostęp. |
| 111 |
status_header( 404 ); |
| 112 |
nocache_headers(); |
| 113 |
$template = get_query_template( '404' ); |
| 114 |
if ( empty( $template ) || ! file_exists( $template ) ) { |
| 115 |
wp_die( esc_html__( '404 Not Found', 'netsensai-shield' ), esc_html__( 'Not Found', 'netsensai-shield' ), array( 'response' => 404 ) ); |
| 116 |
} |
| 117 |
include( $template ); |
| 118 |
exit; |
| 119 |
} |
| 120 |
|
| 121 |
// Blokujemy dostęp do /wp-admin dla niezalogowanych użytkowników. |
| 122 |
if ( strpos( $request_path, '/wp-admin' ) === 0 ) { |
| 123 |
status_header( 404 ); |
| 124 |
nocache_headers(); |
| 125 |
$template = get_query_template( '404' ); |
| 126 |
if ( empty( $template ) || ! file_exists( $template ) ) { |
| 127 |
wp_die( esc_html__( '404 Not Found', 'netsensai-shield' ), esc_html__( 'Not Found', 'netsensai-shield' ), array( 'response' => 404 ) ); |
| 128 |
} |
| 129 |
include( $template ); |
| 130 |
exit; |
| 131 |
} |
| 132 |
} |
| 133 |
} |
| 134 |
add_action( 'wp_loaded', 'ns_shield_block_default_urls' ); |
| 135 |
|
| 136 |
/** |
| 137 |
* Handles custom login requests. |
| 138 |
* |
| 139 |
* If the custom login query variable is set, this function loads wp-login.php. |
| 140 |
* |
| 141 |
* @return void |
| 142 |
*/ |
| 143 |
function ns_shield_handle_custom_login_page() { |
| 144 |
if ( intval( get_query_var( 'ns_shield_custom_login' ) ) === 1 ) { |
| 145 |
$custom_slug = ns_shield_get_custom_slug(); |
| 146 |
// Forbidden slugs to avoid conflicts. |
| 147 |
$forbidden_slugs = array( 'login', 'wp-login', 'login.php', 'wp-login.php' ); |
| 148 |
if ( in_array( $custom_slug, $forbidden_slugs, true ) ) { |
| 149 |
status_header( 404 ); |
| 150 |
nocache_headers(); |
| 151 |
$template = get_query_template( '404' ); |
| 152 |
if ( empty( $template ) || ! file_exists( $template ) ) { |
| 153 |
wp_die( esc_html__( '404 Not Found', 'netsensai-shield' ), esc_html__( 'Not Found', 'netsensai-shield' ), array( 'response' => 404 ) ); |
| 154 |
} |
| 155 |
include( $template ); |
| 156 |
exit; |
| 157 |
} |
| 158 |
global $user_login, $error; |
| 159 |
$user_login = ''; |
| 160 |
$error = ''; |
| 161 |
require_once( ABSPATH . 'wp-login.php' ); |
| 162 |
exit; |
| 163 |
} |
| 164 |
} |
| 165 |
add_action( 'template_redirect', 'ns_shield_handle_custom_login_page' ); |
| 166 |
|
| 167 |
/** |
| 168 |
* Modifies the login URL to return the custom login URL set by the user. |
| 169 |
* |
| 170 |
* @param string $login_url The original login URL. |
| 171 |
* @param string $redirect The redirect URL. |
| 172 |
* @return string Modified login URL. |
| 173 |
*/ |
| 174 |
function ns_shield_custom_login_url( $login_url, $redirect ) { |
| 175 |
if ( get_option( 'ns_shield_login_url_enabled', false ) ) { |
| 176 |
return home_url( '/' . ns_shield_get_custom_slug() ); |
| 177 |
} |
| 178 |
return $login_url; |
| 179 |
} |
| 180 |
add_filter( 'login_url', 'ns_shield_custom_login_url', 10, 2 ); |
| 181 |
|
| 182 |
/** |
| 183 |
* Additional filter to modify wp-login.php URLs generated by site_url(). |
| 184 |
* |
| 185 |
* @param string $url URL. |
| 186 |
* @param string $path Path. |
| 187 |
* @param string $orig_scheme Original scheme. |
| 188 |
* @param int $blog_id Blog ID. |
| 189 |
* @return string Modified URL. |
| 190 |
*/ |
| 191 |
function ns_shield_override_wp_login_url( $url, $path, $orig_scheme, $blog_id ) { |
| 192 |
if ( get_option( 'ns_shield_login_url_enabled', false ) && in_array( $path, array( 'wp-login.php', 'wp-login' ) ) ) { |
| 193 |
return home_url( '/' . ns_shield_get_custom_slug() ); |
| 194 |
} |
| 195 |
return $url; |
| 196 |
} |
| 197 |
add_filter( 'site_url', 'ns_shield_override_wp_login_url', 10, 4 ); |
| 198 |
|
| 199 |
/** |
| 200 |
* Modifies the logout URL to redirect to the custom login URL. |
| 201 |
* |
| 202 |
* @param string $redirect_to The URL to redirect to. |
| 203 |
* @param string $requested_redirect_to The requested redirect URL. |
| 204 |
* @param WP_User|WP_Error $user The user object or error. |
| 205 |
* @return string Modified logout URL. |
| 206 |
*/ |
| 207 |
function ns_shield_custom_logout_url( $redirect_to, $requested_redirect_to, $user ) { |
| 208 |
if ( get_option( 'ns_shield_login_url_enabled', false ) ) { |
| 209 |
return home_url( '/' . ns_shield_get_custom_slug() . '?loggedout=true' ); |
| 210 |
} |
| 211 |
return home_url( '/wp-login.php?loggedout=true' ); |
| 212 |
} |
| 213 |
add_filter( 'logout_redirect', 'ns_shield_custom_logout_url', 10, 3 ); |
| 214 |
|
| 215 |
/** |
| 216 |
* Redirects the user to the admin area after a successful login. |
| 217 |
* |
| 218 |
* @param string $redirect_to The redirect URL. |
| 219 |
* @param string $requested_redirect_to The requested redirect URL. |
| 220 |
* @param WP_User|WP_Error $user The user object or error. |
| 221 |
* @return string Modified redirect URL. |
| 222 |
*/ |
| 223 |
function ns_shield_custom_login_redirect( $redirect_to, $requested_redirect_to, $user ) { |
| 224 |
if ( get_option( 'ns_shield_login_url_enabled', false ) && ! is_wp_error( $user ) ) { |
| 225 |
return admin_url(); |
| 226 |
} |
| 227 |
return $redirect_to; |
| 228 |
} |
| 229 |
add_filter( 'login_redirect', 'ns_shield_custom_login_redirect', 10, 3 ); |
| 230 |
|
| 231 |
/** |
| 232 |
* Adds the autocomplete="off" attribute to the login form. |
| 233 |
* |
| 234 |
* @return void |
| 235 |
*/ |
| 236 |
function ns_shield_disable_autocomplete() { |
| 237 |
echo '<script> |
| 238 |
document.addEventListener("DOMContentLoaded", function() { |
| 239 |
var loginForm = document.getElementById("loginform"); |
| 240 |
if ( loginForm ) { |
| 241 |
loginForm.setAttribute("autocomplete", "off"); |
| 242 |
} |
| 243 |
}); |
| 244 |
</script>'; |
| 245 |
} |
| 246 |
add_action( 'login_form', 'ns_shield_disable_autocomplete' ); |
| 247 |
|
| 248 |
/** |
| 249 |
* Adds a nonce field to the login form. |
| 250 |
* |
| 251 |
* @return void |
| 252 |
*/ |
| 253 |
function ns_shield_add_login_nonce() { |
| 254 |
wp_nonce_field( 'login_nonce' ); |
| 255 |
} |
| 256 |
add_action( 'login_form', 'ns_shield_add_login_nonce' ); |
| 257 |
|
| 258 |
/** |
| 259 |
* Changes the action attribute of the login form to the custom login URL. |
| 260 |
* |
| 261 |
* @param string $action The original form action URL. |
| 262 |
* @return string Modified form action URL. |
| 263 |
*/ |
| 264 |
function ns_shield_login_form_action( $action ) { |
| 265 |
if ( get_option( 'ns_shield_login_url_enabled', false ) ) { |
| 266 |
return home_url( '/' . ns_shield_get_custom_slug() ); |
| 267 |
} |
| 268 |
return $action; |
| 269 |
} |
| 270 |
add_filter( 'login_form_action', 'ns_shield_login_form_action' ); |
| 271 |
|
| 272 |
/** |
| 273 |
* Displays the toggle for changing the login URL on the settings page. |
| 274 |
* |
| 275 |
* @return void |
| 276 |
*/ |
| 277 |
function ns_shield_change_login_url() { |
| 278 |
$status = get_option( 'ns_shield_login_url_enabled', false ); |
| 279 |
$login_url = get_option( 'ns_shield_login_url', '/mysecurelogin/' ); |
| 280 |
?> |
| 281 |
<div class="change-url-container"> |
| 282 |
<label class="switch"> |
| 283 |
<input type="checkbox" name="ns_shield_login_url_enabled" id="ns_shield_login_url_enabled" value="1" <?php checked( 1, $status, true ); ?>> |
| 284 |
<span class="slider round"></span> |
| 285 |
</label> |
| 286 |
<div class="tooltip" id="tooltip-change-login-url"> |
| 287 |
<?php echo esc_html( ns_shield_get_login_url_tooltip() ); ?> |
| 288 |
</div> |
| 289 |
<input type="text" name="ns_shield_login_url" id="ns_shield_login_url" value="<?php echo esc_attr( $login_url ); ?>" placeholder="<?php echo esc_attr__( 'Enter new login URL', 'netsensai-shield' ); ?>" class="login-url-input" style="display:<?php echo $status ? 'block' : 'none'; ?>;" /> |
| 290 |
</div> |
| 291 |
<script> |
| 292 |
document.addEventListener("DOMContentLoaded", function() { |
| 293 |
var changeLoginUrlCheckbox = document.getElementById("ns_shield_login_url_enabled"); |
| 294 |
var loginUrlField = document.getElementById("ns_shield_login_url"); |
| 295 |
if ( changeLoginUrlCheckbox && loginUrlField ) { |
| 296 |
loginUrlField.style.display = changeLoginUrlCheckbox.checked ? "block" : "none"; |
| 297 |
changeLoginUrlCheckbox.addEventListener("change", function() { |
| 298 |
loginUrlField.style.display = this.checked ? "block" : "none"; |
| 299 |
}); |
| 300 |
} else { |
| 301 |
console.error("Toggle switch or login URL text field not found!"); |
| 302 |
} |
| 303 |
}); |
| 304 |
</script> |
| 305 |
<?php |
| 306 |
} |