-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsite-lock.php
More file actions
214 lines (214 loc) · 6.77 KB
/
site-lock.php
File metadata and controls
214 lines (214 loc) · 6.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
<?php
/*
Plugin Name: Site Lock (Simple)
Description: Password-protects the frontend. Toggle from Settings → Site Lock.
Version: 1.0
Author: Kris+Kelly
*/
add_action('admin_menu', function () {
add_options_page('Site Lock', 'Site Lock', 'manage_options', 'site-lock', 'site_lock_settings_page');
});
function site_lock_settings_page() {
if (!current_user_can('manage_options')) return;
if (isset($_POST['site_lock_nonce']) && wp_verify_nonce($_POST['site_lock_nonce'], 'site_lock_save')) {
update_option('pp_lock_enabled', isset($_POST['pp_lock_enabled']) ? 1 : 0);
if (!empty($_POST['pp_new_pass'])) {
$hash = hash('sha256', $_POST['pp_new_pass'] . (defined('AUTH_SALT') ? AUTH_SALT : 'salt'));
update_option('pp_lock_hash', $hash);
}
echo '<div class="updated"><p>Saved.</p></div>';
}
$enabled = (int) get_option('pp_lock_enabled', 1);
?>
<div class="wrap">
<h1>Site Lock</h1>
<form method="post">
<?php wp_nonce_field('site_lock_save', 'site_lock_nonce'); ?>
<p><label><input type="checkbox" name="pp_lock_enabled" <?php checked($enabled,1); ?>> Enable lock</label></p>
<p>
<label>Set/Change password:<br>
<input type="password" name="pp_new_pass" style="min-width:320px"></label><br>
<small>Leave empty to keep current password.</small>
</p>
<p><button class="button button-primary">Save</button></p>
</form>
<p><small>Logged-in users and /wp-admin/ are never blocked.</small></p>
</div>
<?php
}
add_action('template_redirect', function () {
if (is_user_logged_in() || is_admin() || php_sapi_name()==='cli') return;
$enabled = (int) get_option('pp_lock_enabled', 1);
if (!$enabled) return;
$uri = $_SERVER['REQUEST_URI'] ?? '';
$allow = ['/wp-login.php','/wp-admin/admin-ajax.php','/wp-cron.php','/wp-json','/robots.txt','/sitemap','/.well-known/acme-challenge'];
foreach ($allow as $p) { if (strpos($uri,$p)!==false) return; }
$hash = get_option('pp_lock_hash');
$cookie = 'pp_auth';
if ($hash && isset($_COOKIE[$cookie]) && hash_equals($_COOKIE[$cookie], $hash)) return;
if (!empty($_POST['pp_pass'])) {
$try = hash('sha256', $_POST['pp_pass'] . (defined('AUTH_SALT') ? AUTH_SALT : 'salt'));
if ($hash && hash_equals($hash, $try)) {
setcookie($cookie, $hash, time()+86400, '/', COOKIE_DOMAIN, is_ssl(), true);
wp_safe_redirect(home_url(add_query_arg([])));
exit;
} else {
$error = 'Wrong password';
}
}
status_header(401);
nocache_headers();
echo '<!doctype html><html lang="en"><head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="robots" content="noindex,nofollow">
<title>Site Protected</title>
<style>
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
min-height: 100vh;
display: flex;
align-items: center;
justify-content: center;
line-height: 1.6;
}
.container {
background: rgba(255, 255, 255, 0.95);
backdrop-filter: blur(10px);
border-radius: 20px;
padding: 2.5rem;
box-shadow: 0 20px 40px rgba(0, 0, 0, 0.1);
max-width: 400px;
width: 90%;
text-align: center;
border: 1px solid rgba(255, 255, 255, 0.2);
}
.lock-icon {
width: 60px;
height: 60px;
background: linear-gradient(135deg, #667eea, #764ba2);
border-radius: 50%;
display: inline-flex;
align-items: center;
justify-content: center;
margin-bottom: 1.5rem;
box-shadow: 0 8px 16px rgba(102, 126, 234, 0.3);
}
.lock-icon::before {
content: "🔒";
font-size: 24px;
filter: grayscale(1) brightness(0) invert(1);
}
h1 {
color: #2d3748;
font-size: 1.8rem;
font-weight: 600;
margin-bottom: 0.5rem;
}
p {
color: #718096;
margin-bottom: 2rem;
font-size: 1rem;
}
.form-group {
margin-bottom: 1.5rem;
text-align: left;
}
input[type="password"] {
width: 100%;
padding: 1rem;
border: 2px solid #e2e8f0;
border-radius: 12px;
font-size: 1rem;
transition: all 0.3s ease;
background: #f7fafc;
outline: none;
}
input[type="password"]:focus {
border-color: #667eea;
background: #fff;
box-shadow: 0 0 0 3px rgba(102, 126, 234, 0.1);
transform: translateY(-1px);
}
button {
width: 100%;
padding: 1rem;
background: linear-gradient(135deg, #667eea, #764ba2);
color: white;
border: none;
border-radius: 12px;
font-size: 1rem;
font-weight: 600;
cursor: pointer;
transition: all 0.3s ease;
text-transform: uppercase;
letter-spacing: 0.5px;
}
button:hover {
transform: translateY(-2px);
box-shadow: 0 12px 24px rgba(102, 126, 234, 0.4);
}
button:active {
transform: translateY(0);
}
.error {
background: linear-gradient(135deg, #fc8181, #f56565);
color: white;
padding: 0.75rem;
border-radius: 8px;
margin-top: 1rem;
font-size: 0.9rem;
animation: shake 0.5s ease-in-out;
}
@keyframes shake {
0%, 100% { transform: translateX(0); }
25% { transform: translateX(-5px); }
75% { transform: translateX(5px); }
}
.fade-in {
animation: fadeIn 0.8s ease-out;
}
@keyframes fadeIn {
from {
opacity: 0;
transform: translateY(30px);
}
to {
opacity: 1;
transform: translateY(0);
}
}
@media (max-width: 480px) {
.container {
padding: 2rem;
margin: 1rem;
}
h1 {
font-size: 1.5rem;
}
}
</style>
</head>
<body>
<div class="container fade-in">
<div class="lock-icon"></div>
<h1>PROTECTED AREA</h1>
<p>Enter the password to access this site</p>
<form method="post">
<div class="form-group">
<input type="password" name="pp_pass" placeholder="Password here" autofocus required>
</div>
<button type="submit">Unlock Site</button>
</form>'.
(!empty($error) ? '<div class="error">'.$error.'</div>' : '') .
'</div>
</body>
</html>';
exit;
});