kasl/RegisterValidator.php

142 lines
3.9 KiB
PHP
Raw Normal View History

2024-04-24 12:32:43 +00:00
<?php
class RegisterValidator
{
private $login = '';
private $email = '';
private $password = '';
private $birthday = '';
private $sex = 0;
2024-04-24 15:37:37 +00:00
private $disallowedLogins = ['admin', 'moderator',];
private $loginLength = ['min' => 2, 'max' => 16];
private $passwordLength = 3;
public function setDisallowedLogins(array $disallowedLogins)
{
$this->disallowedLogins = $disallowedLogins;
}
public function setPasswordLength($max)
{
$max = (int)$max;
if ($max > 0) {
$this->passwordLength = $max;
}
}
public function setLoginLength($max, $min = 2)
{
$max = (int)$max;
$min = (int)$min;
if ($max > 0 && $min > 0 && $max > $min) {
$this->loginLength = ['min' => $min, 'max' => $max];
}
}
2024-04-24 12:32:43 +00:00
public function setLogin($login)
{
$login = preg_replace('!\s+!', ' ', $login); // remove inner spaces
$login = trim($login); // remove outer spaces
2024-04-24 15:08:49 +00:00
if (
$this->loginIsAllowed($login) &&
!$this->loginIsMixed($login) &&
2024-04-24 15:37:37 +00:00
mb_strlen($login <= $this->loginLength['min']) &&
mb_strlen($login) >= $this->loginLength['max'] &&
2024-04-24 15:08:49 +00:00
!strpos("!@#$%^&*()\+|/'\"", $login)
) {
2024-04-24 12:32:43 +00:00
$this->login = $login;
}
return $this;
}
private function loginIsAllowed($login)
{
2024-04-24 15:37:37 +00:00
$d = implode('|', $this->disallowedLogins);
2024-04-24 12:32:43 +00:00
$pattern = "/\b($d)\b/iu";
return !preg_match($pattern, $login);
}
private function loginIsMixed($login)
{
$en = preg_match("/^(([0-9A-z -])+)$/iu", $login);
$ru = preg_match("/^([а-яёіїє\s\d]*)$/iu", $login);
return ($ru && $en) || (!$ru && !$en);
}
public function setEmail($email)
{
if (filter_var($email, FILTER_VALIDATE_EMAIL)) {
$this->email = $email;
}
return $this;
}
public function setPassword($password, $passwordVerify)
{
2024-04-24 15:37:37 +00:00
if ($this->password === $passwordVerify && mb_strlen($password) >= $this->passwordLength) {
2024-04-24 12:32:43 +00:00
$this->password = md5($password);
}
return $this;
}
public function setBirthday($birthday)
{
$bdate = DateTime::createFromFormat('Y-m-d', $birthday);
if ($bdate) {
$this->birthday = $bdate->format('d.m.Y');
}
return $this;
}
public function setSex($sex)
{
if ((int)$sex > 0) {
$this->sex = $sex;
}
return $this;
}
2024-04-24 15:08:49 +00:00
public function get()
2024-04-24 12:32:43 +00:00
{
2024-04-24 15:08:49 +00:00
return [
'login' => $this->login,
'email' => $this->email,
'password' => $this->password,
'birthday' => $this->birthday,
'sex' => $this->sex,
];
2024-04-24 12:32:43 +00:00
}
}
2024-04-24 15:37:37 +00:00
## написано под PHP 5.6
## использование:
require_once 'path/to/RegisterValidator.php';
$rv = new RegisterValidator();
# обязательно
$rv->setDisallowedLogins([
'Мироздатель',
'Мусорщик',
'Комментатор',
]);
# необязательный параметр, по умолчанию 16.
$rv->setLoginLength(16);
# необязательный параметр, по умолчанию 3.
$rv->setPasswordLength(3);
# проверяем введённые значения и записываем результат в массив если всё ок, или пустую строку, если не ок.
# [login->string, email->string, password->md5(string), birthday->dd.mm.YYYY, sex->int]
# birthday заточен под <input type=date>, если надо в том формате, что сейчас, надо записывать в него:
# $birthday = "$yy-$mm-$dd";
$values = $rv
->setLogin($login)
->setPassword($pass1, $pass2)
->setEmail($email)
->setBirthday($birthday)
->setSex($sex)
->get();