PluginProbe ʕ •ᴥ•ʔ
WP STAGING – WordPress Backups, Restore, Migration & Clone / 4.11.0
WP STAGING – WordPress Backups, Restore, Migration & Clone v4.11.0
4.11.0 4.10.0 4.9.5 4.9.4 4.9.3 4.9.2 4.9.1 4.9.0 4.8.1 trunk 3.0.0 3.0.1 3.0.2 3.0.3 3.0.4 3.0.5 3.0.6 3.1.0 3.1.1 3.1.2 3.1.3 3.1.4 3.10.0 3.2.0 3.3.1 3.3.2 3.3.3 3.4.1 3.4.3 3.5.0 3.6.0 3.7.1 3.8.0 3.8.1 3.8.2 3.8.3 3.8.4 3.8.5 3.8.6 3.8.7 3.9.0 3.9.1 3.9.2 3.9.3 3.9.4 4.0.0 4.1.0 4.1.1 4.1.2 4.1.3 4.1.4 4.2.0 4.2.1 4.3.0 4.3.1 4.3.2 4.4.0 4.5.0 4.6.0 4.7.0 4.7.1 4.7.2 4.7.3 4.8.0
wp-staging / Frontend / LoginForm.php
wp-staging / Frontend Last commit date
Frontend.php 1 day ago FrontendServiceProvider.php 1 day ago LoginAfterRestore.php 1 day ago LoginForm.php 1 day ago LoginNotice.php 1 day ago
LoginForm.php
232 lines
1 <?php
2
3 namespace WPStaging\Frontend;
4
5 use WPStaging\Core\WPStaging;
6 use WPStaging\Framework\Utils\Sanitize;
7
8 class LoginForm
9 {
10
11 private $args = [];
12
13
14
15
16
17
18
19 private $error = '';
20
21
22 private $sanitize;
23
24 public function __construct()
25 {
26 $this->sanitize = WPStaging::make(Sanitize::class);
27 $this->login();
28 }
29
30
31
32
33 private function login(): bool
34 {
35 if (is_user_logged_in()) {
36 return false;
37 }
38
39 if (!isset($_POST['wpstg-username']) || !isset($_POST['wpstg-pass'])) {
40 return false;
41 }
42
43
44 if (isset($_POST['wpstg-submit']) && (empty($_POST['wpstg-username']) || empty($_POST['wpstg-pass']))) {
45 $this->error = 'No username or password given!';
46 return false;
47 }
48
49 $username = $this->sanitize->sanitizeString($_POST['wpstg-username']);
50
51 $user_data = get_user_by('login', $username);
52
53
54 if (!$user_data) {
55 $user_data = get_user_by('email', $username);
56 }
57
58 $guideLink = esc_url('https://wp-staging.com/docs/can-not-login-to-staging-website/#Disable_WP_STAGING_Login_Form_or_Allow_Specific_Users_to_Pass_it');
59 if (!$user_data) {
60 $msg = sprintf(__('Incorrect credentials! Only administrators can access this page. Please try the default <a target="_blank" href="%s">login</a> form or read this <a target="_blank" href="%s">guide</a>.', 'wp-staging'), wp_login_url(), $guideLink);
61
62 if (defined('WPSTGPRO_VERSION')) {
63 $msg = sprintf(__('Incorrect credentials! Only administrators or explicitly authorized users can access this page. Please try the default <a target="_blank" href="%s">login</a> form or read this <a target="_blank" href="%s">guide</a>.', 'wp-staging'), wp_login_url(), $guideLink);
64 }
65
66 $this->error = $msg;
67 return false;
68 }
69
70
71 $password = isset($_POST['wpstg-pass']) ? $this->sanitize->sanitizePassword($_POST['wpstg-pass']) : '';
72 if (wp_check_password($password, $user_data->user_pass, $user_data->ID)) {
73 $rememberme = isset($_POST['rememberme']) ? true : false;
74
75 wp_set_auth_cookie($user_data->ID, $rememberme);
76 wp_set_current_user($user_data->ID, $username);
77 do_action('wp_login', $username, get_userdata($user_data->ID));
78
79 if (!empty($_POST['redirect_to'])) {
80 $redirectUrl = $this->sanitize->sanitizeUrl($_POST['redirect_to']);
81 }
82
83 set_transient('wpstg_user_logged_in_status', true, 5);
84
85 header('Location:' . $redirectUrl);
86 } else {
87 $msg = sprintf(__('Login not possible! Only administrators can access this page. Please try the default <a target="_blank" href="%s">login</a> form or read this <a target="_blank" href="%s">guide</a>.', 'wp-staging'), wp_login_url(), $guideLink);
88
89 if (defined('WPSTGPRO_VERSION')) {
90 $msg = sprintf(__('Login not possible! Only administrators or explicitly authorized users can access this page. Please try the default <a target="_blank" href="%s">login</a> form or read this <a target="_blank" href="%s">guide</a>.', 'wp-staging'), wp_login_url(), $guideLink);
91 }
92
93 $this->error = $msg;
94 }
95
96 return false;
97 }
98
99
100
101
102
103 public function renderForm(array $args = [])
104 {
105 $this->args = $args;
106 $this->getHeader();
107 $this->getLoginForm();
108 $this->getFooter();
109 }
110
111
112
113
114 private function getHeader()
115 {
116 require_once WPSTG_VIEWS_DIR . 'frontend/header.php';
117 }
118
119
120
121
122
123 private function getFooter()
124 {
125 require_once WPSTG_VIEWS_DIR . 'frontend/footer.php';
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 private function getLoginForm()
160 {
161 $args = empty($this->args) ? $this->getDefaultArguments() : $this->args;
162
163
164 $notice = __('Enter your administrator credentials to access this site. (This message will be displayed only once!)', 'wp-staging');
165 $showNotice = (new LoginNotice())->isLoginNoticeActive();
166
167
168 $isCustomLogin2faEnabled = class_exists('wordfence', false) && get_option('wordfenceActivated');
169
170 $loginFileView = WPSTG_VIEWS_DIR . 'frontend/loginForm.php';
171
172 if ($args['echo']) {
173 require($loginFileView);
174 } else {
175 ob_start();
176 require($loginFileView);
177 return ob_get_clean();
178 }
179 }
180
181
182
183
184
185
186 public function setError(string $error)
187 {
188 $this->error = $error;
189 }
190
191
192
193
194
195
196
197
198
199
200 public function getDefaultArguments(array $overrides = []): array
201 {
202
203 $httpHost = !empty($_SERVER['HTTP_HOST']) ? $this->sanitize->sanitizeString($_SERVER['HTTP_HOST']) : '';
204 $requestURI = !empty($_SERVER['REQUEST_URI']) ? $this->sanitize->sanitizeString($_SERVER['REQUEST_URI']) : '';
205 $redirect = $this->sanitize->sanitizeUrl((is_ssl() ? 'https://' : 'http://') . $httpHost . $requestURI);
206 $lostPasswordUrl = wp_lostpassword_url($redirect);
207 $arguments = wp_parse_args(
208 $overrides,
209 [
210 'echo' => true,
211 'redirect' => $redirect,
212 'lost_password_url' => $lostPasswordUrl,
213 'form_id' => 'loginform',
214 'label_username' => __('Username', 'wp-staging'),
215 'label_password' => __('Password', 'wp-staging'),
216 'label_remember' => __('Remember Me', 'wp-staging'),
217 'label_log_in' => __('Log In', 'wp-staging'),
218 'id_username' => 'user_login',
219 'id_password' => 'user_pass',
220 'id_remember' => 'rememberme',
221 'id_submit' => 'wp-submit',
222 'remember' => true,
223 'value_username' => '',
224
225 'value_remember' => false,
226 ]
227 );
228
229 return $arguments;
230 }
231 }
232