John Davidson

php - field don't accept Arabic word in nickname

0 comments
Message:


this is a code for guest user ,
he can enter chat with just nickname , in English word work and no problem ,
but in Arabic word get this message ( Error : Invalid Input or Field Empty )
when Enter Arabic word with number 1 or 2 or any number it will done and username 1_randon_string .
where is the error ?
so did need more coded ?
I'm have little known of php .
thanks for all .


 <?php
include 'fns/filters/load.php';
$result = array();
$result['success'] = false;
$result['error_message'] = Registry::load('strings')->invalid_value;
$result['error_key'] = 'invalid_value';
$result['error_variables'] = ['nickname'];
$noerror = true;
$strict_mode = true;
if (Registry::load('settings')->non_latin_usernames !== 'enable') {
$strict_mode = false;
}
if (!isset($data['nickname'])) {
$noerror = false;
} else {
$data['username'] = sanitize_username($data['nickname'], $strict_mode);
if (empty(trim($data['username']))) {
$noerror = false;
}
}
if (Registry::load('settings')->guest_login !== 'enable') {
$result['error_message'] = Registry::load('strings')->went_wrong;
$result['error_key'] = 'something_went_wrong';
$noerror = false;
}

if (!Registry::load('current_user')->logged_in) {

if (isset(Registry::load('settings')->captcha) && Registry::load('settings')->captcha !== 'disable') {
include 'fns/captcha/load.php';
}
if (isset(Registry::load('settings')->captcha) && Registry::load('settings')->captcha === 'google_recaptcha_v2') {
if (!isset($data['g-recaptcha-response']) || empty(trim($data['g-recaptcha-response']))) {
$result['error_message'] = Registry::load('strings')->invalid_captcha;
$result['error_variables'][] = 'captcha';
$noerror = false;
}
else if (!validate_captcha('google_recaptcha_v2', $data['g-recaptcha-response'])) {
$result['error_message'] = Registry::load('strings')->invalid_captcha;
$result['error_variables'][] = 'captcha';
$noerror = false;
}
} else if (isset(Registry::load('settings')->captcha) && Registry::load('settings')-
>captcha === 'hcaptcha') {
if (!isset($data['h-captcha-response']) || empty(trim($data['h-captcha-response']))) {
$result['error_message'] = Registry::load('strings')->invalid_captcha;
$result['error_variables'][] = 'captcha';
$noerror = false;
} else if (!validate_captcha('hcaptcha', $data['h-captcha-response'])) {
$result['error_message'] = Registry::load('strings')->invalid_captcha;
$result['error_variables'][] = 'captcha';
$noerror = false;
}
}
}

if ($noerror) {
$guest_user = [
'add' => 'site_users',
'full_name' => $data['nickname'],
'username' => $data['username'],
'password' => random_string(['length' => 6]),
'signup_page' => true,
'return' => true
];

if (username_exists($data['username'])) {
$guest_user['username'] = $data['username'].'_'.random_string(['length' => 5]);
}

$guest_user['email_address'] = $guest_user['username'].'@'.random_string(['length' => 5]).'.guestuser';

if (isset($data['redirect'])) {
$guest_user['redirect'] = $data['redirect'];
}
$result = add($guest_user, ['force_request' => true, 'exclude_filters_function' => true, 'guest_user' => true]);
}

?>

this is function sanitize_username :


function sanitize_username($username, $strict = false) {
$username = strip_all_tags($username);
$username = remove_accents($username);
$username = preg_replace('|%([a-fA-F0-9][a-fA-F0-9])|', '',
$username);
$username = preg_replace('/&.+?;/', '', $username);

if ($strict) {
$username = preg_replace('|[^a-z0-9 _.\-]|i', '', $username);
}

$username = str_replace(array('\'', '"', ',', '@', ';', '(', ')', '[', ']', '<', '>', '{', '}', '?', '&'), '', $username);
$username = trim($username);
$username = preg_replace('|\s+|', ' ', $username);
$username = preg_replace('/\s+/', '-', $username);
return $username;
}


My Answer:
The issue is with the `sanitize_username` function. The function is removing all non-Latin characters and special characters from the username, which is causing the Arabic word to be considered invalid.

To allow Arabic words in the username, you need to modify the `sanitize_username` function to allow Arabic characters. You can update the regular expression in the function to include Arabic characters as well.

Here is an updated version of the `sanitize_username` function that allows Arabic characters:

php
function sanitize_username($username, $strict = false) {
$username = strip_all_tags($username);
$username = remove_accents($username);
$username = preg_replace('|%([a-fA-F0-9][a-fA-F0-9])|', '', $username);
$username = preg_replace('/&.+?;/', '', $username);

if ($strict) {
$username = preg_replace('/[^a-zA-Z0-9_\x{0621}-\x{064A} _.\-]/u', '', $username);
}

$username = str_replace(array('\'', '"', ',', '@', ';', '(', ')', '[', ']', '<', '>', '{', '}', '?', '&'), '', $username);
$username = trim($username);
$username = preg_replace('/\s+/', ' ', $username);
$username = preg_replace('/\s+/', '-', $username);
return $username;
}


In this updated function, the regular expression `/[^a-zA-Z0-9_\x{0621}-\x{064A} _.\-]/u` allows Arabic characters in addition to Latin characters, numbers, and some special characters.

Replace the existing `sanitize_username` function with this updated version in your code, and it should now allow Arabic words in the username field.

Rate this post

4 of 5 based on 6941 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us