| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentCart\App\Services; |
| 4 |
|
| 5 |
use FluentCart\App\Models\Customer; |
| 6 |
|
| 7 |
class AuthService |
| 8 |
{ |
| 9 |
|
| 10 |
public static function createUserFromCustomer(Customer $customer, $sendUserEmail = true, $userRole = '') |
| 11 |
{ |
| 12 |
$userName = self::createUserNameFromStrings($customer->email, [$customer->first_name, $customer->last_name]); |
| 13 |
|
| 14 |
return self::registerNewUser( |
| 15 |
$userName, |
| 16 |
$customer->email, |
| 17 |
'', |
| 18 |
[ |
| 19 |
'first_name' => $customer->first_name, |
| 20 |
'last_name' => $customer->last_name, |
| 21 |
'role' => $userRole |
| 22 |
] |
| 23 |
); |
| 24 |
} |
| 25 |
|
| 26 |
public static function registerNewUser($user_login, $user_email, $user_pass = '', $extraData = []) |
| 27 |
{ |
| 28 |
$errors = new \WP_Error(); |
| 29 |
|
| 30 |
$sanitized_user_login = sanitize_user($user_login); |
| 31 |
|
| 32 |
// Check the username. |
| 33 |
if ('' === $sanitized_user_login) { |
| 34 |
$errors->add('empty_username', __('<strong>Error</strong>: Please enter a username.', 'fluent-cart')); |
| 35 |
} elseif (username_exists($sanitized_user_login)) { |
| 36 |
$errors->add('username_exists', __('<strong>Error</strong>: This username is already registered. Please choose another one.', 'fluent-cart')); |
| 37 |
} |
| 38 |
|
| 39 |
// Check the email address. |
| 40 |
if ('' === $user_email) { |
| 41 |
$errors->add('empty_email', __('<strong>Error</strong>: Please type your email address.', 'fluent-cart')); |
| 42 |
} elseif (!is_email($user_email)) { |
| 43 |
$errors->add('invalid_email', __('<strong>Error</strong>: The email address is not correct.', 'fluent-cart')); |
| 44 |
$user_email = ''; |
| 45 |
} elseif (email_exists($user_email)) { |
| 46 |
$errors->add( |
| 47 |
'email_exists', |
| 48 |
__('<strong>Error:</strong> This email address is already registered. Please login or try resetting your password.', 'fluent-cart') |
| 49 |
); |
| 50 |
} |
| 51 |
|
| 52 |
if ($errors->has_errors()) { |
| 53 |
return $errors; |
| 54 |
} |
| 55 |
|
| 56 |
$isGeneratedPassword = false; |
| 57 |
if (!$user_pass) { |
| 58 |
$isGeneratedPassword = true; |
| 59 |
$user_pass = wp_generate_password(8, false); |
| 60 |
} |
| 61 |
|
| 62 |
$data = [ |
| 63 |
'user_login' => wp_slash($sanitized_user_login), |
| 64 |
'user_email' => wp_slash($user_email), |
| 65 |
'user_pass' => $user_pass |
| 66 |
]; |
| 67 |
|
| 68 |
if (!empty($extraData['first_name'])) { |
| 69 |
$data['first_name'] = sanitize_text_field($extraData['first_name']); |
| 70 |
} |
| 71 |
|
| 72 |
if (!empty($extraData['last_name'])) { |
| 73 |
$data['last_name'] = sanitize_text_field($extraData['last_name']); |
| 74 |
} |
| 75 |
|
| 76 |
if (!empty($extraData['full_name']) && empty($extraData['first_name']) && empty($extraData['last_name'])) { |
| 77 |
$extraData['full_name'] = sanitize_text_field($extraData['full_name']); |
| 78 |
// extract the names |
| 79 |
$fullNameArray = explode(' ', $extraData['full_name']); |
| 80 |
$data['first_name'] = array_shift($fullNameArray); |
| 81 |
if ($fullNameArray) { |
| 82 |
$data['last_name'] = implode(' ', $fullNameArray); |
| 83 |
} else { |
| 84 |
$data['last_name'] = ''; |
| 85 |
} |
| 86 |
} |
| 87 |
|
| 88 |
if (!empty($extraData['description'])) { |
| 89 |
$data['description'] = sanitize_textarea_field($extraData['description']); |
| 90 |
} |
| 91 |
|
| 92 |
if (!empty($extraData['user_url']) && filter_var($extraData['user_url'], FILTER_VALIDATE_URL)) { |
| 93 |
$data['user_url'] = sanitize_url($extraData['user_url']); |
| 94 |
} |
| 95 |
|
| 96 |
if (!empty($extraData['role'])) { |
| 97 |
$data['role'] = $extraData['role']; |
| 98 |
} |
| 99 |
|
| 100 |
$user_id = wp_insert_user($data); |
| 101 |
|
| 102 |
if (!$user_id || is_wp_error($user_id)) { |
| 103 |
$errors->add('registerfail', __('<strong>Error</strong>: Could not register you. Please contact the site admin!', 'fluent-cart')); |
| 104 |
return $errors; |
| 105 |
} |
| 106 |
|
| 107 |
if (!empty($_COOKIE['wp_lang'])) { |
| 108 |
$wp_lang = sanitize_text_field(wp_unslash($_COOKIE['wp_lang'])); |
| 109 |
if (in_array($wp_lang, get_available_languages(), true)) { |
| 110 |
update_user_meta($user_id, 'locale', $wp_lang); // Set user locale if defined on registration. |
| 111 |
} |
| 112 |
} |
| 113 |
|
| 114 |
if ($isGeneratedPassword) { |
| 115 |
update_user_meta($user_id, 'default_password_nag', true); // Set up the password change nag. |
| 116 |
} |
| 117 |
|
| 118 |
do_action('fluent_cart/user/after_register', $user_id, [ |
| 119 |
'user_id' => $user_id |
| 120 |
]); |
| 121 |
|
| 122 |
if (apply_filters('fluent_cart/user/after_register/skip_hooks', false, $user_id)) { |
| 123 |
return $user_id; |
| 124 |
} |
| 125 |
|
| 126 |
do_action('register_new_user', $user_id); |
| 127 |
|
| 128 |
return $user_id; |
| 129 |
} |
| 130 |
|
| 131 |
public static function makeLogin($user) |
| 132 |
{ |
| 133 |
wp_clear_auth_cookie(); |
| 134 |
wp_set_current_user($user->ID, $user->user_login); |
| 135 |
wp_set_auth_cookie($user->ID, true, is_ssl()); |
| 136 |
|
| 137 |
$user = get_user_by('ID', $user->ID); |
| 138 |
|
| 139 |
if ($user) { |
| 140 |
// do_action('wp_login', $user->user_login, $user); |
| 141 |
} |
| 142 |
|
| 143 |
return $user; |
| 144 |
} |
| 145 |
|
| 146 |
public static function createUserNameFromStrings($maybeEmail, $fallbacks = []) |
| 147 |
{ |
| 148 |
$emailParts = explode('@', $maybeEmail); |
| 149 |
$userName = $emailParts[0]; |
| 150 |
|
| 151 |
$userName = self::sanitizeUserName($userName); |
| 152 |
|
| 153 |
if (self::isUsernameAvailable($userName)) { |
| 154 |
return $userName; |
| 155 |
} |
| 156 |
|
| 157 |
foreach ($fallbacks as $fallback) { |
| 158 |
// only take alphanumeric characters and _ - |
| 159 |
$fallback = preg_replace('/[^a-zA-Z0-9_-]/', '', $fallback); |
| 160 |
$userName = self::sanitizeUserName($fallback); |
| 161 |
if (self::isUsernameAvailable($userName)) { |
| 162 |
return $userName; |
| 163 |
} |
| 164 |
} |
| 165 |
|
| 166 |
$userName = strtolower($emailParts[0]); |
| 167 |
|
| 168 |
$finalUserName = $userName; |
| 169 |
|
| 170 |
// loop until we find a unique username |
| 171 |
$counter = 2; |
| 172 |
while (!self::isUsernameAvailable($userName)) { |
| 173 |
$userName = $finalUserName . $counter; |
| 174 |
$counter++; |
| 175 |
if ($counter % 100 === 0) { |
| 176 |
$finalUserName = $finalUserName . '-' . time(); |
| 177 |
} |
| 178 |
} |
| 179 |
|
| 180 |
return $userName; |
| 181 |
} |
| 182 |
|
| 183 |
private static function sanitizeUserName($username) |
| 184 |
{ |
| 185 |
$username = strtolower($username); |
| 186 |
|
| 187 |
// check of @ symbol |
| 188 |
if (strpos($username, '@') !== false) { |
| 189 |
$username = explode('@', $username)[0]; |
| 190 |
} |
| 191 |
|
| 192 |
$username = sanitize_user($username); |
| 193 |
$username = preg_replace('/[^a-zA-Z0-9_]/', '', $username); |
| 194 |
return $username; |
| 195 |
} |
| 196 |
|
| 197 |
private static function isUsernameAvailable($userName) |
| 198 |
{ |
| 199 |
$userName = strtolower($userName); |
| 200 |
if (strlen($userName) < 3) { |
| 201 |
return false; |
| 202 |
} |
| 203 |
|
| 204 |
$reservedUserNames = self::getReservedUserNames(); |
| 205 |
if (in_array($userName, $reservedUserNames)) { |
| 206 |
return false; |
| 207 |
} |
| 208 |
|
| 209 |
if (defined('FLUENT_COMMUNITY_PLUGIN_VERSION')) { |
| 210 |
$xProfile = \FluentCommunity\App\Models\XProfile::where('username', $userName) |
| 211 |
->exists(); |
| 212 |
if ($xProfile) { |
| 213 |
return false; |
| 214 |
} |
| 215 |
} |
| 216 |
|
| 217 |
$illegal_user_logins = (array)apply_filters('illegal_user_logins', array()); |
| 218 |
if (in_array($userName, array_map('strtolower', $illegal_user_logins), true)) { |
| 219 |
return false; |
| 220 |
} |
| 221 |
|
| 222 |
if (username_exists($userName)) { |
| 223 |
return false; |
| 224 |
} |
| 225 |
|
| 226 |
return true; |
| 227 |
} |
| 228 |
|
| 229 |
private static function getReservedUserNames() |
| 230 |
{ |
| 231 |
return apply_filters('fluent_community/reserved_usernames', [ |
| 232 |
'admin', 'administrator', 'me', 'moderator', 'mod', 'superuser', 'root', 'system', 'official', 'staff', 'support', 'helpdesk', 'user', 'guest', 'anonymous', 'everyone', 'anybody', 'someone', 'webmaster', 'postmaster', 'hostmaster', 'abuse', 'security', 'ssl', 'firewall', 'no-reply', 'noreply', 'mail', 'email', 'mailer', 'smtp', 'pop', 'imap', 'ftp', 'sftp', 'ssh', 'ceo', 'cfo', 'cto', 'founder', 'cofounder', 'owner', 'president', 'vicepresident', 'director', 'manager', 'supervisor', 'executive', 'info', 'contact', 'sales', 'marketing', 'support', 'billing', 'accounting', 'finance', 'hr', 'humanresources', 'legal', 'compliance', 'it', 'itsupport', 'customerservice', 'customersupport', 'dev', 'developer', 'api', 'sdk', 'app', 'bot', 'chatbot', 'sysadmin', 'devops', 'infosec', 'security', 'test', 'testing', 'beta', 'alpha', 'staging', 'production', 'development', 'home', 'about', 'contact', 'faq', 'help', 'news', 'blog', 'forum', 'community', 'events', 'calendar', 'shop', 'store', 'cart', 'checkout', 'social', 'follow', 'like', 'share', 'tweet', 'post', 'status', 'privacy', 'terms', 'copyright', 'trademark', 'legal', 'policy', 'all', 'none', 'null', 'undefined', 'true', 'false', 'default', 'example', 'sample', 'demo', 'temporary', 'delete', 'remove', 'profanity', 'explicit', 'offensive', 'yourappname', 'yourbrandname', 'yourdomain', |
| 233 |
]); |
| 234 |
} |
| 235 |
|
| 236 |
} |
| 237 |
|