2016-11-02 02:27:42 +05:30
|
|
|
<?php
|
2020-08-23 02:34:26 +05:30
|
|
|
declare(strict_types=1);
|
|
|
|
|
2016-11-02 02:27:42 +05:30
|
|
|
namespace common\validators;
|
|
|
|
|
|
|
|
use common\helpers\Error as E;
|
2018-01-07 21:38:40 +05:30
|
|
|
use common\helpers\StringHelper;
|
2016-11-02 02:27:42 +05:30
|
|
|
use common\models\Account;
|
|
|
|
use Ely\Yii2\TempmailValidator;
|
|
|
|
use yii\base\Model;
|
|
|
|
use yii\db\QueryInterface;
|
|
|
|
use yii\validators;
|
|
|
|
use yii\validators\Validator;
|
|
|
|
|
2023-10-15 10:23:17 +05:30
|
|
|
final class EmailValidator extends Validator {
|
2016-11-02 02:27:42 +05:30
|
|
|
|
|
|
|
/**
|
2024-12-02 15:40:55 +05:30
|
|
|
* @phpstan-var \Closure(): int the function must return the account id for which the current validation is being performed.
|
2020-08-23 02:34:26 +05:30
|
|
|
* Allows you to skip the email uniqueness check for the current account.
|
2016-11-02 02:27:42 +05:30
|
|
|
*/
|
2024-12-02 15:40:55 +05:30
|
|
|
public ?\Closure $accountCallback = null;
|
2016-11-02 02:27:42 +05:30
|
|
|
|
|
|
|
public $skipOnEmpty = false;
|
|
|
|
|
2020-08-23 02:34:26 +05:30
|
|
|
public function validateAttribute($model, $attribute): void {
|
2024-12-02 15:40:55 +05:30
|
|
|
$trim = new validators\FilterValidator(['filter' => StringHelper::trim(...)]);
|
2016-11-02 02:27:42 +05:30
|
|
|
|
|
|
|
$required = new validators\RequiredValidator();
|
|
|
|
$required->message = E::EMAIL_REQUIRED;
|
|
|
|
|
|
|
|
$length = new validators\StringValidator();
|
|
|
|
$length->max = 255;
|
|
|
|
$length->tooLong = E::EMAIL_TOO_LONG;
|
|
|
|
|
|
|
|
$email = new validators\EmailValidator();
|
|
|
|
$email->checkDNS = true;
|
|
|
|
$email->enableIDN = true;
|
|
|
|
$email->message = E::EMAIL_INVALID;
|
|
|
|
|
2023-10-15 10:23:17 +05:30
|
|
|
$additionalEmail = new class extends Validator {
|
|
|
|
protected function validateValue($value): ?array {
|
|
|
|
// Disallow emails starting with slash since Postfix (or someone before?) can't correctly handle it
|
2024-12-02 15:40:55 +05:30
|
|
|
if (str_starts_with((string)$value, '/')) {
|
2023-10-15 10:23:17 +05:30
|
|
|
return [E::EMAIL_INVALID, []];
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2016-11-02 02:27:42 +05:30
|
|
|
$tempmail = new TempmailValidator();
|
|
|
|
$tempmail->message = E::EMAIL_IS_TEMPMAIL;
|
|
|
|
|
2020-08-23 02:34:26 +05:30
|
|
|
$blacklist = new class extends Validator {
|
|
|
|
public $hosts = [
|
|
|
|
'seznam.cz',
|
|
|
|
];
|
|
|
|
|
|
|
|
protected function validateValue($value): ?array {
|
2024-12-02 15:40:55 +05:30
|
|
|
$host = explode('@', (string)$value)[1];
|
2020-08-23 02:34:26 +05:30
|
|
|
if (in_array($host, $this->hosts, true)) {
|
|
|
|
return [E::EMAIL_HOST_IS_NOT_ALLOWED, []];
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-04-21 18:37:09 +05:30
|
|
|
$idnaDomain = new validators\FilterValidator(['filter' => function(string $value): string {
|
|
|
|
[$name, $domain] = explode('@', $value);
|
2023-10-15 10:23:17 +05:30
|
|
|
return idn_to_ascii($name) . '@' . idn_to_ascii($domain);
|
2018-04-21 18:37:09 +05:30
|
|
|
}]);
|
|
|
|
|
2016-11-02 02:27:42 +05:30
|
|
|
$unique = new validators\UniqueValidator();
|
|
|
|
$unique->message = E::EMAIL_NOT_AVAILABLE;
|
|
|
|
$unique->targetClass = Account::class;
|
|
|
|
$unique->targetAttribute = 'email';
|
|
|
|
if ($this->accountCallback !== null) {
|
2020-08-23 02:34:26 +05:30
|
|
|
$unique->filter = function(QueryInterface $query): void {
|
2016-11-02 02:27:42 +05:30
|
|
|
$query->andWhere(['NOT', ['id' => ($this->accountCallback)()]]);
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2024-12-02 15:40:55 +05:30
|
|
|
$this->executeValidation($trim, $model, $attribute)
|
|
|
|
&& $this->executeValidation($required, $model, $attribute)
|
|
|
|
&& $this->executeValidation($length, $model, $attribute)
|
|
|
|
&& $this->executeValidation($email, $model, $attribute)
|
|
|
|
&& $this->executeValidation($additionalEmail, $model, $attribute)
|
|
|
|
&& $this->executeValidation($tempmail, $model, $attribute)
|
|
|
|
&& $this->executeValidation($blacklist, $model, $attribute)
|
|
|
|
&& $this->executeValidation($idnaDomain, $model, $attribute)
|
|
|
|
&& $this->executeValidation($unique, $model, $attribute);
|
2016-11-02 02:27:42 +05:30
|
|
|
}
|
|
|
|
|
2018-04-21 18:37:09 +05:30
|
|
|
private function executeValidation(Validator $validator, Model $model, string $attribute): bool {
|
2016-11-02 02:27:42 +05:30
|
|
|
$validator->validateAttribute($model, $attribute);
|
|
|
|
return !$model->hasErrors($attribute);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|