|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132 |
- <?php
-
-
- namespace yii\validators;
-
- use Yii;
- use yii\base\InvalidConfigException;
- use yii\web\JsExpression;
- use yii\helpers\Json;
-
-
- class EmailValidator extends Validator
- {
-
-
- public $pattern = '/^[a-zA-Z0-9!#$%&\'*+\\/=?^_`{|}~-]+(?:\.[a-zA-Z0-9!#$%&\'*+\\/=?^_`{|}~-]+)*@(?:[a-zA-Z0-9](?:[a-zA-Z0-9-]*[a-zA-Z0-9])?\.)+[a-zA-Z0-9](?:[a-zA-Z0-9-]*[a-zA-Z0-9])?$/';
-
-
- public $fullPattern = '/^[^@]*<[a-zA-Z0-9!#$%&\'*+\\/=?^_`{|}~-]+(?:\.[a-zA-Z0-9!#$%&\'*+\\/=?^_`{|}~-]+)*@(?:[a-zA-Z0-9](?:[a-zA-Z0-9-]*[a-zA-Z0-9])?\.)+[a-zA-Z0-9](?:[a-zA-Z0-9-]*[a-zA-Z0-9])?>$/';
-
-
- public $allowName = false;
-
-
- public $checkDNS = false;
-
-
- public $enableIDN = false;
-
-
-
-
- public function init()
- {
- parent::init();
- if ($this->enableIDN && !function_exists('idn_to_ascii')) {
- throw new InvalidConfigException('In order to use IDN validation intl extension must be installed and enabled.');
- }
- if ($this->message === null) {
- $this->message = Yii::t('yii', '{attribute} is not a valid email address.');
- }
- }
-
-
-
- protected function validateValue($value)
- {
- if (!is_string($value)) {
- $valid = false;
- } elseif (!preg_match('/^(?P<name>(?:"?([^"]*)"?\s)?)(?:\s+)?(?:(?P<open><?)((?P<local>.+)@(?P<domain>[^>]+))(?P<close>>?))$/i', $value, $matches)) {
- $valid = false;
- } else {
- if ($this->enableIDN) {
- $matches['local'] = idn_to_ascii($matches['local']);
- $matches['domain'] = idn_to_ascii($matches['domain']);
- $value = $matches['name'] . $matches['open'] . $matches['local'] . '@' . $matches['domain'] . $matches['close'];
- }
-
- if (strlen($matches['local']) > 64) {
-
-
- $valid = false;
- } elseif (strlen($matches['local'] . '@' . $matches['domain']) > 254) {
-
-
-
-
-
-
- $valid = false;
- } else {
- $valid = preg_match($this->pattern, $value) || $this->allowName && preg_match($this->fullPattern, $value);
- if ($valid && $this->checkDNS) {
- $valid = checkdnsrr($matches['domain'], 'MX') || checkdnsrr($matches['domain'], 'A');
- }
- }
- }
-
- return $valid ? null : [$this->message, []];
- }
-
-
-
- public function clientValidateAttribute($model, $attribute, $view)
- {
- $options = [
- 'pattern' => new JsExpression($this->pattern),
- 'fullPattern' => new JsExpression($this->fullPattern),
- 'allowName' => $this->allowName,
- 'message' => Yii::$app->getI18n()->format($this->message, [
- 'attribute' => $model->getAttributeLabel($attribute),
- ], Yii::$app->language),
- 'enableIDN' => (bool)$this->enableIDN,
- ];
- if ($this->skipOnEmpty) {
- $options['skipOnEmpty'] = 1;
- }
-
- ValidationAsset::register($view);
- if ($this->enableIDN) {
- PunycodeAsset::register($view);
- }
-
- return 'yii.validation.email(value, messages, ' . Json::htmlEncode($options) . ');';
- }
- }
|