PluginProbe
FlyWP Helper – Page Cache, Page Optimization, Emails for FlyWP Server Control Panel / 0.3.2
FlyWP Helper – Page Cache, Page Optimization, Emails for FlyWP Server Control Panel v0.3.2
1.7.1 1.7.0 1.6.0 1.5.2 trunk 0.1 0.2.0 0.2.1 0.3 0.3.1 0.3.2 0.3.3 0.3.4 0.4 0.4.1 0.4.2 0.4.3 1.0 1.1 1.2 1.3.1 1.4.0 1.4.1 1.5.0 1.5.1 All 26 releases
flywp / includes / Frontend / MagicLogin.php

MagicLogin.php in FlyWP Helper – Page Cache, Page Optimization, Emails for FlyWP Server Control Panel 0.3.2, at includes/Frontend/MagicLogin.php

90 lines 2.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace FlyWP\Frontend;
4
5 /**
6 * Magic Login.
7 *
8 * @since 1.0.0
9 */
10 class MagicLogin {
11
12 /**
13 * Plugin Constructor.
14 *
15 * @return void
16 */
17 public function __construct() {
18 add_action( 'setup_theme', [ $this, 'login_user' ] );
19 }
20
21 /**
22 * Check if the request is valid.
23 *
24 * @return bool
25 */
26 private function is_valid_request() {
27 return isset( $_SERVER['REQUEST_URI'] ) && isset( $_SERVER['REQUEST_METHOD'] ) && $_SERVER['REQUEST_URI'] === '/flywp-magic-login' && $_SERVER['REQUEST_METHOD'] === 'POST';
28 }
29
30 /**
31 * Redirect to home.
32 *
33 * @return void
34 */
35 public function redirect_to_home() {
36 wp_safe_redirect( site_url() );
37 exit;
38 }
39
40 /**
41 * Redirect to admin.
42 *
43 * @return void
44 */
45 public function redirect_to_admin() {
46 wp_safe_redirect( admin_url() );
47 exit;
48 }
49
50 /**
51 * Login an user.
52 *
53 * @return void
54 */
55 public function login_user() {
56 if ( ! $this->is_valid_request() ) {
57 return;
58 }
59
60 // phpcs:disable WordPress.Security.NonceVerification.Missing
61 $api_key = isset( $_POST['api_key'] ) ? sanitize_text_field( wp_unslash( $_POST['api_key'] ) ) : '';
62 $username = isset( $_POST['username'] ) ? sanitize_text_field( wp_unslash( $_POST['username'] ) ) : '';
63 // phpcs:enable WordPress.Security.NonceVerification.Missing
64
65 if ( ! $api_key || ! $username ) {
66 $this->redirect_to_home();
67 }
68
69 if ( $api_key !== flywp()->get_key() ) {
70 $this->redirect_to_home();
71 }
72
73 if ( is_user_logged_in() ) {
74 $this->redirect_to_admin();
75 }
76
77 $user = get_user_by( 'login', $username );
78
79 if ( ! $user ) {
80 $this->redirect_to_admin();
81 }
82
83 wp_set_current_user( $user->ID, $user->user_login );
84 wp_set_auth_cookie( $user->ID );
85
86 // redirect to admin
87 $this->redirect_to_admin();
88 }
89 }
90