Maslosoft Mangan API
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156
<?php
/**
* This software package is licensed under AGPL or Commercial license.
*
* @package maslosoft/mangan
* @licence AGPL or Commercial
* @copyright Copyright (c) Piotr Masełkowski <pmaselkowski@gmail.com>
* @copyright Copyright (c) Maslosoft
* @copyright Copyright (c) Others as mentioned in code
* @link https://maslosoft.com/mangan/
*/
namespace Maslosoft\Mangan\Validators\BuiltIn;
use Maslosoft\Addendum\Interfaces\AnnotatedInterface;
use Maslosoft\Mangan\Interfaces\Validators\ValidatorInterface;
use Maslosoft\Mangan\Meta\ManganMeta;
use Maslosoft\Mangan\Validators\Traits\AllowEmpty;
use Maslosoft\Mangan\Validators\Traits\Messages;
use Maslosoft\Mangan\Validators\Traits\OnScenario;
use Maslosoft\Mangan\Validators\Traits\Safe;
use Maslosoft\Mangan\Validators\Traits\SkipOnError;
/**
* EmailValidator
*
* @author Piotr Maselkowski <pmaselkowski at gmail.com>
*/
class EmailValidator implements ValidatorInterface
{
use AllowEmpty,
Messages,
OnScenario,
Safe,
SkipOnError;
const EmailPattern = '/^[\p{L}0-9!#$%&\'*+\\/=?^_`{|}~-]+(?:\.[\p{L}0-9!#$%&\'*+\\/=?^_`{|}~-]+)*@(?:[\p{L}0-9](?:[\p{L}0-9-]*[\p{L}0-9])?\.)+[\p{L}0-9](?:[\p{L}0-9-]*[\p{L}0-9])?$/u';
const FullEmailPattern = '/^[^@]*<[\p{L}0-9!#$%&\'*+\\/=?^_`{|}~-]+(?:\\.[\p{L}0-9!#$%&\'*+\\/=?^_`{|}~-]+)*@(?:[\p{L}0-9](?:[\p{L}0-9-]*[\p{L}0-9])?\\.)+[\p{L}0-9](?:[\p{L}0-9-]*[\p{L}0-9])?>$/u';
/**
* @var string the regular expression used to validate the attribute value.
* @see http://www.regular-expressions.info/email.html
*/
public $pattern = self::EmailPattern;
/**
* @var boolean whether to check the MX record for the email address.
* Defaults to false. To enable it, you need to make sure the PHP function 'checkdnsrr'
* exists in your PHP installation.
* Please note that this check may fail due to temporary problems even if email is deliverable.
*/
public $checkMX = false;
/**
* @var boolean whether to check port 25 for the email address.
* Defaults to false. To enable it, ensure that the PHP functions 'dns_get_record' and
* 'fsockopen' are available in your PHP installation.
* Please note that this check may fail due to temporary problems even if email is deliverable.
*/
public $checkPort = false;
/**
* @Label('{attribute} must be valid email address')
* @var string
*/
public $msgValid = '';
/**
* @Label('Email domain "{domain}" does not exists')
* @var string
*/
public $msgDomain = '';
/**
* @Label('Email service does not seem to be running at "{domain}"')
* @var string
*/
public $msgPort = '';
public function isValid(AnnotatedInterface $model, $attribute)
{
if ($this->allowEmpty && empty($model->$attribute))
{
return true;
}
$label = ManganMeta::create($model)->field($attribute)->label;
if (!is_scalar($model->$attribute))
{
$this->addError('msgValid', ['{attribute}' => $label]);
return false;
}
if (!preg_match($this->pattern, $model->$attribute))
{
$this->addError('msgValid', ['{attribute}' => $label]);
return false;
}
$domain = rtrim(substr($model->$attribute, strpos($model->$attribute, '@') + 1), '>');
if ($this->checkMX)
{
if (function_exists('checkdnsrr'))
{
if (!checkdnsrr($domain, 'MX'))
{
$this->addError('msgDomain', ['{domain}' => $domain]);
return false;
}
}
}
if ($this->checkPort)
{
if ($this->checkMxPorts($domain))
{
$this->addError('msgPort', ['{domain}' => $domain]);
return false;
}
}
return true;
}
/**
* Retrieves the list of MX records for $domain and checks if port 25
* is opened on any of these.
* @param string $domain domain to be checked
* @return boolean true if a reachable MX server has been found
*/
protected function checkMxPorts($domain)
{
$records = dns_get_record($domain, DNS_MX);
if ($records === false || empty($records))
{
return false;
}
$sort = function ($a, $b)
{
return $a['pri'] - $b['pri'];
};
usort($records, $sort);
foreach ($records as $record)
{
$errno = null;
$errstr = null;
$handle = @fsockopen($record['target'], 25, $errno, $errstr, 3);
if ($handle !== false)
{
fclose($handle);
return true;
}
}
return false;
}
}
API documentation generated by ApiGen