diff --git a/library/ajax/user_register.php b/library/ajax/user_register.php
index 48fe4e95c..81afd72b6 100644
--- a/library/ajax/user_register.php
+++ b/library/ajax/user_register.php
@@ -40,21 +40,12 @@ switch ($mode) {
case 'check_pass':
$pass = (string)$this->request['pass'];
$pass_confirm = (string)$this->request['pass_confirm'];
- if (empty($pass) || empty($pass_confirm)) {
- $html = '
' . $lang['CHOOSE_PASS'] . '';
+
+ if ($err = \TorrentPier\Legacy\Validate::password($pass, $pass_confirm)) {
+ $html = '
' . $err . '';
} else {
- if ($pass != $pass_confirm) {
- $html = '
' . $lang['CHOOSE_PASS_ERR'] . '';
- } else {
- if (mb_strlen($pass, 'UTF-8') > 20) {
- $html = '
' . sprintf($lang['CHOOSE_PASS_ERR_MAX'], 20) . '';
- } elseif (mb_strlen($pass, 'UTF-8') < 5) {
- $html = '
' . sprintf($lang['CHOOSE_PASS_ERR_MIN'], 5) . '';
- } else {
- $text = (IS_GUEST) ? $lang['CHOOSE_PASS_REG_OK'] : $lang['CHOOSE_PASS_OK'];
- $html = '
' . $text . '';
- }
- }
+ $text = (IS_GUEST) ? $lang['CHOOSE_PASS_REG_OK'] : $lang['CHOOSE_PASS_OK'];
+ $html = '
' . $text . '';
}
break;
}
diff --git a/library/includes/ucp/register.php b/library/includes/ucp/register.php
index f620d9582..c96ba98a8 100644
--- a/library/includes/ucp/register.php
+++ b/library/includes/ucp/register.php
@@ -204,13 +204,10 @@ foreach ($profile_fields as $field => $can_edit) {
// пароль для гостя и при смене пароля юзером
if (!empty($new_pass)) {
- if (mb_strlen($new_pass, 'UTF-8') > 20) {
- $errors[] = sprintf($lang['CHOOSE_PASS_ERR_MAX'], 20);
- } elseif (mb_strlen($new_pass, 'UTF-8') < 4) {
- $errors[] = sprintf($lang['CHOOSE_PASS_ERR_MIN'], 4);
- } elseif ($new_pass != $cfm_pass) {
- $errors[] = $lang['CHOOSE_PASS_ERR'];
+ if ($err = \TorrentPier\Legacy\Validate::password($new_pass, $cfm_pass)) {
+ $errors[] = $err;
}
+
$db_data['user_password'] = md5(md5($new_pass));
}
diff --git a/src/Legacy/Validate.php b/src/Legacy/Validate.php
index bb0430191..9caa0be5e 100644
--- a/src/Legacy/Validate.php
+++ b/src/Legacy/Validate.php
@@ -143,4 +143,37 @@ class Validate
return false;
}
+
+ /**
+ * Validate user entered password
+ *
+ * @param string $password
+ * @param string $password_confirm
+ *
+ * @return bool|string
+ */
+ public static function password(string $password, string $password_confirm)
+ {
+ global $lang;
+
+ // Check for empty
+ if (empty($pass) || empty($pass_confirm)) {
+ return $lang['CHOOSE_PASS'];
+ }
+
+ // Check password confirm
+ if ($password_confirm != $password) {
+ return $lang['CHOOSE_PASS_ERR'];
+ }
+
+ // Length
+ if (mb_strlen($password, 'UTF-8') > PASSWORD_MAX_LENGTH) {
+ return sprintf($lang['CHOOSE_PASS_ERR_MAX'], PASSWORD_MAX_LENGTH);
+ }
+ if (mb_strlen($password, 'UTF-8') < PASSWORD_MIN_LENGTH) {
+ return sprintf($lang['CHOOSE_PASS_ERR_MIN'], PASSWORD_MIN_LENGTH);
+ }
+
+ return false;
+ }
}