| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentSupport\App\Http\Controllers; |
| 4 |
|
| 5 |
use FluentSupport\App\Services\Helper; |
| 6 |
use FluentSupport\Framework\Support\Arr; |
| 7 |
use FluentSupport\Framework\Request\Request; |
| 8 |
use FluentSupport\App\Hooks\Handlers\AuthHandler; |
| 9 |
|
| 10 |
class AuthController extends Controller |
| 11 |
{ |
| 12 |
public function signup(Request $request) |
| 13 |
{ |
| 14 |
|
| 15 |
if (!wp_verify_nonce($request->get('_fsupport_signup_nonce'), 'fluent_support_signup_nonce')) { |
| 16 |
$this->sendError([ |
| 17 |
'message' => __('Security verification failed. Please try again', 'fluent-support') |
| 18 |
]); |
| 19 |
} |
| 20 |
|
| 21 |
$fields = AuthHandler::getSignupFields(); |
| 22 |
|
| 23 |
$rules = $this->getRules($fields); |
| 24 |
|
| 25 |
$messages = $this->getMessages($rules); |
| 26 |
|
| 27 |
$formData = apply_filters('fluent_support/signup_form_data', $request->all()); |
| 28 |
|
| 29 |
do_action('fluent_support/before_signup_validation', []); |
| 30 |
|
| 31 |
$this->validate($formData, $rules, $messages); |
| 32 |
|
| 33 |
do_action('fluent_support/after_signup_validation', $formData); |
| 34 |
|
| 35 |
$userId = $this->createUser($formData); |
| 36 |
|
| 37 |
if (is_wp_error($userId)) { |
| 38 |
return $this->response( |
| 39 |
apply_filters( |
| 40 |
'fluent_support/signup_create_user_error', |
| 41 |
['error' => $userId->get_error_message()] |
| 42 |
), 423); |
| 43 |
} |
| 44 |
|
| 45 |
do_action('fluent_support/after_creating_user'); |
| 46 |
|
| 47 |
$this->maybeUpdateUser($userId, $formData); |
| 48 |
$this->assignRole($userId); |
| 49 |
$this->login($userId); |
| 50 |
|
| 51 |
return $this->response( |
| 52 |
apply_filters('fluent_support/signup_complete_response', [ |
| 53 |
'message' => __('Successfully registered to the site.', 'fluent-support'), |
| 54 |
'redirect' => Arr::get($formData, '__redirect_to', Helper::getPortalBaseUrl()) |
| 55 |
]) |
| 56 |
); |
| 57 |
} |
| 58 |
|
| 59 |
public function handleLogin(Request $request) |
| 60 |
{ |
| 61 |
if (!wp_verify_nonce($request->get('_support_login_nonce'), 'fsupport_login_nonce')) { |
| 62 |
return $this->response([ |
| 63 |
'message' => __('Security verification failed', 'fluent-support') |
| 64 |
], 403); |
| 65 |
} |
| 66 |
|
| 67 |
$data = $request->all(); |
| 68 |
|
| 69 |
if (empty($data['pwd']) || empty($data['log'])) { |
| 70 |
return $this->response([ |
| 71 |
'message' => __('Email and Password is required', 'fluent-support') |
| 72 |
], 403); |
| 73 |
} |
| 74 |
|
| 75 |
$redirectUrl = Helper::getPortalBaseUrl(); |
| 76 |
|
| 77 |
if (get_current_user_id()) { // user already registered |
| 78 |
return $this->sendSuccess([ |
| 79 |
'redirect' => $redirectUrl |
| 80 |
]); |
| 81 |
} |
| 82 |
|
| 83 |
$email = $data['log']; |
| 84 |
$password = $data['pwd']; |
| 85 |
|
| 86 |
if (is_email($email)) { |
| 87 |
$user = get_user_by('email', $email); |
| 88 |
} else { |
| 89 |
$user = get_user_by('login', $email); |
| 90 |
} |
| 91 |
|
| 92 |
if (!$user) { |
| 93 |
return $this->response([ |
| 94 |
'message' => __('Email or Password is not valid. Please try again', 'fluent-support') |
| 95 |
], 403); |
| 96 |
} |
| 97 |
|
| 98 |
$info = array( |
| 99 |
'user_login' => sanitize_text_field(trim($_POST['log'])), |
| 100 |
'user_password' => trim($_POST['pwd']), |
| 101 |
'remember' => isset($_POST['rememberme']), |
| 102 |
); |
| 103 |
|
| 104 |
if (apply_filters('fluent_support_use_native_login', true)) { |
| 105 |
return $this->nativeLoginHandler($user, $info, $redirectUrl); |
| 106 |
} |
| 107 |
|
| 108 |
if (wp_check_password($password, $user->user_pass, $user->ID)) { |
| 109 |
$this->login($user->ID); |
| 110 |
return $this->sendSuccess([ |
| 111 |
'redirect' => $redirectUrl |
| 112 |
]); |
| 113 |
} |
| 114 |
|
| 115 |
return $this->response([ |
| 116 |
'message' => __('Email or Password is not valid. Please try again', 'fluent-support') |
| 117 |
], 403); |
| 118 |
} |
| 119 |
|
| 120 |
private function nativeLoginHandler($user, $info, $redirectUrl = '') |
| 121 |
{ |
| 122 |
if (!$redirectUrl) { |
| 123 |
$redirectUrl = Helper::getPortalBaseUrl(); |
| 124 |
} |
| 125 |
|
| 126 |
$secure_cookie = is_ssl(); |
| 127 |
if (!$secure_cookie && !force_ssl_admin()) { |
| 128 |
if (get_user_option('use_ssl', $user->ID)) { |
| 129 |
$secure_cookie = true; |
| 130 |
force_ssl_admin(true); |
| 131 |
} |
| 132 |
} |
| 133 |
|
| 134 |
if (class_exists('\Limit_Login_Attempts')) { |
| 135 |
global $limit_login_attempts_obj; |
| 136 |
$limit_login_attempts_try = $limit_login_attempts_obj->wp_authenticate_user($user, false); |
| 137 |
if (is_wp_error($limit_login_attempts_try)) { |
| 138 |
return $this->response([ |
| 139 |
'message' => implode('<br/>', $limit_login_attempts_try->get_error_messages()) |
| 140 |
], 403); |
| 141 |
} |
| 142 |
} |
| 143 |
|
| 144 |
$user_signon = wp_signon($info, $secure_cookie); |
| 145 |
|
| 146 |
if (!is_wp_error($user_signon) && empty($_COOKIE[LOGGED_IN_COOKIE])) { |
| 147 |
if (headers_sent()) { |
| 148 |
return $this->response([ |
| 149 |
'message' => sprintf(__('<strong>ERROR</strong>: Cookies are blocked due to unexpected output. For help, please see <a href="%1$s">this documentation</a> or try the <a href="%2$s">support forums</a>.'), |
| 150 |
__('https://codex.wordpress.org/Cookies'), __('https://wordpress.org/support/')) |
| 151 |
], 403); |
| 152 |
} |
| 153 |
} |
| 154 |
|
| 155 |
if (is_wp_error($user_signon)) { |
| 156 |
$errorMessage = __('Email or Password is not valid. Please try again', 'fluent-support'); |
| 157 |
|
| 158 |
if (class_exists('Limit_Login_Attempts')) { |
| 159 |
global $limit_login_attempts_obj; |
| 160 |
if ($limit_login_attempts_obj) { |
| 161 |
$limit_login_attempts_obj->limit_login_failed($user->user_login); |
| 162 |
$msg = $limit_login_attempts_obj->get_message(); |
| 163 |
if ($msg) { |
| 164 |
$errorMessage = $msg; |
| 165 |
} |
| 166 |
} |
| 167 |
} |
| 168 |
|
| 169 |
return $this->response([ |
| 170 |
'message' => $errorMessage |
| 171 |
], 403); |
| 172 |
} |
| 173 |
|
| 174 |
// WP Last Login plugin compatibility |
| 175 |
if (class_exists('\Obenland_Wp_Last_Login')) { |
| 176 |
update_user_meta($user_signon->ID, 'wp-last-login', time()); |
| 177 |
} |
| 178 |
|
| 179 |
return $this->sendSuccess([ |
| 180 |
'redirect' => $redirectUrl |
| 181 |
]); |
| 182 |
} |
| 183 |
|
| 184 |
protected function getRules($fields = []) |
| 185 |
{ |
| 186 |
$rules = []; |
| 187 |
|
| 188 |
foreach ($fields as $fieldName => $field) { |
| 189 |
if (array_key_exists('required', $field)) { |
| 190 |
$rules[$fieldName] = 'required'; |
| 191 |
} |
| 192 |
|
| 193 |
$pipe = array_key_exists($fieldName, $rules) ? '|' : ''; |
| 194 |
|
| 195 |
if ($field['type'] === 'email') { |
| 196 |
$rules[$fieldName] = $rules[$fieldName] . $pipe . 'email'; |
| 197 |
} elseif ($field['type'] === 'password') { |
| 198 |
$rules[$fieldName] = $rules[$fieldName] . $pipe . 'min:8'; |
| 199 |
} |
| 200 |
} |
| 201 |
|
| 202 |
return apply_filters('fluent_support/signup_validation_rules', $rules); |
| 203 |
} |
| 204 |
|
| 205 |
protected function getMessages($rules = []) |
| 206 |
{ |
| 207 |
return apply_filters('fluent_support/signup_validation_messages', [], $rules); |
| 208 |
} |
| 209 |
|
| 210 |
public function createUser($formData = []) |
| 211 |
{ |
| 212 |
$email = apply_filters('fluent_support/signup_email', Arr::get($formData, 'email')); |
| 213 |
$userName = apply_filters('fluent_support/signup_username', Arr::get($formData, 'username')); |
| 214 |
|
| 215 |
if (empty($formData['password'])) { |
| 216 |
$password = wp_generate_password(8); |
| 217 |
} else { |
| 218 |
$password = $formData['password']; |
| 219 |
} |
| 220 |
|
| 221 |
$password = apply_filters('fluent_support/signup_password', $password); |
| 222 |
|
| 223 |
do_action('fluent_support/before_creating_user', $userName, $password, $email); |
| 224 |
|
| 225 |
return wp_create_user($userName, $password, $email); |
| 226 |
} |
| 227 |
|
| 228 |
public function maybeUpdateUser($userId, $formData) |
| 229 |
{ |
| 230 |
$name = trim(Arr::get($formData, 'first_name') . ' ' . Arr::get($formData, 'last_name')); |
| 231 |
|
| 232 |
$data = array_filter([ |
| 233 |
'ID' => $userId, |
| 234 |
'user_nicename' => $name, |
| 235 |
'display_name' => $name, |
| 236 |
'first_name' => Arr::get($formData, 'first_name'), |
| 237 |
'last_name' => Arr::get($formData, 'last_name'), |
| 238 |
]); |
| 239 |
|
| 240 |
if ($name) { |
| 241 |
do_action('fluent_support/before_updating_user', $data); |
| 242 |
|
| 243 |
wp_update_user(apply_filters('fluent_support/update_user_data', $data)); |
| 244 |
|
| 245 |
do_action('fluent_support/after_updating_user', $data); |
| 246 |
} |
| 247 |
} |
| 248 |
|
| 249 |
protected function assignRole($userId) |
| 250 |
{ |
| 251 |
$user = new \WP_User($userId); |
| 252 |
|
| 253 |
do_action('fluent_support/before_assigning_role', $user); |
| 254 |
|
| 255 |
$user->set_role(apply_filters('fluent_support/user_role', 'subscriber')); |
| 256 |
|
| 257 |
do_action('fluent_support/after_assigning_role', $user); |
| 258 |
} |
| 259 |
|
| 260 |
protected function login($userId) |
| 261 |
{ |
| 262 |
do_action('fluent_support/before_logging_in_user', $userId); |
| 263 |
|
| 264 |
wp_clear_auth_cookie(); |
| 265 |
wp_set_current_user($userId); |
| 266 |
wp_set_auth_cookie($userId); |
| 267 |
|
| 268 |
do_action('fluent_support/after_logging_in_user', $userId); |
| 269 |
} |
| 270 |
} |
| 271 |
|