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
|
|
|
|
|
|
|
/**
|
2020-08-23 02:34:26 +05:30
|
|
|
* @var callable(): int the function must return the account id for which the current validation is being performed.
|
|
|
|
* Allows you to skip the email uniqueness check for the current account.
|
2016-11-02 02:27:42 +05:30
|
|
|
*/
|
|
|
|
public $accountCallback;
|
|
|
|
|
|
|
|
public $skipOnEmpty = false;
|
|
|
|
|
2020-08-23 02:34:26 +05:30
|
|
|
public function validateAttribute($model, $attribute): void {
|
|
|
|
$trim = new validators\FilterValidator(['filter' => [StringHelper::class, '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
|
|
|
|
if (str_starts_with($value, '/')) {
|
|
|
|
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 {
|
|
|
|
$host = explode('@', $value)[1];
|
|
|
|
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)()]]);
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2020-08-23 02:34:26 +05:30
|
|
|
$this->executeValidation($trim, $model, $attribute) &&
|
2016-11-02 02:27:42 +05:30
|
|
|
$this->executeValidation($required, $model, $attribute) &&
|
|
|
|
$this->executeValidation($length, $model, $attribute) &&
|
|
|
|
$this->executeValidation($email, $model, $attribute) &&
|
2023-10-15 10:23:17 +05:30
|
|
|
$this->executeValidation($additionalEmail, $model, $attribute) &&
|
2016-11-02 02:27:42 +05:30
|
|
|
$this->executeValidation($tempmail, $model, $attribute) &&
|
2020-08-23 02:34:26 +05:30
|
|
|
$this->executeValidation($blacklist, $model, $attribute) &&
|
2018-04-21 18:37:09 +05:30
|
|
|
$this->executeValidation($idnaDomain, $model, $attribute) &&
|
2016-11-02 02:27:42 +05:30
|
|
|
$this->executeValidation($unique, $model, $attribute);
|
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|