|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Copyright (c) 2014-2016 Ryan Parman. |
|
4
|
|
|
* |
|
5
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy |
|
6
|
|
|
* of this software and associated documentation files (the "Software"), to deal |
|
7
|
|
|
* in the Software without restriction, including without limitation the rights |
|
8
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
|
9
|
|
|
* copies of the Software, and to permit persons to whom the Software is |
|
10
|
|
|
* furnished to do so, subject to the following conditions: |
|
11
|
|
|
* |
|
12
|
|
|
* The above copyright notice and this permission notice shall be included in |
|
13
|
|
|
* all copies or substantial portions of the Software. |
|
14
|
|
|
* |
|
15
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
|
16
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
|
17
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
|
18
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
|
19
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
|
20
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
|
21
|
|
|
* THE SOFTWARE. |
|
22
|
|
|
* |
|
23
|
|
|
* http://opensource.org/licenses/MIT |
|
24
|
|
|
*/ |
|
25
|
|
|
|
|
26
|
|
|
namespace Skyzyx\StrongTypes\StringType; |
|
27
|
|
|
|
|
28
|
|
|
use Exception; |
|
29
|
|
|
use UnexpectedValueException; |
|
30
|
|
|
use Skyzyx\StrongTypes\StringType; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* Not RFC-valid, but that's on purpose. In general, this validates e-mail addresses against the syntax in |
|
34
|
|
|
* RFC 822 (obsolete), with the exceptions that comments and whitespace folding are not supported. Also, IP address |
|
35
|
|
|
* hosts are explicitly disallowed. |
|
36
|
|
|
* |
|
37
|
|
|
* @see http://tools.ietf.org/html/rfc822 |
|
38
|
|
|
* @see http://tools.ietf.org/html/rfc2822 |
|
39
|
|
|
* @see http://tools.ietf.org/html/rfc5322 |
|
40
|
|
|
* @see http://markonphp.com/properly-validate-email-address-php/ |
|
41
|
|
|
*/ |
|
42
|
|
|
class Email extends StringType |
|
43
|
|
|
{ |
|
44
|
|
|
/** |
|
45
|
|
|
* {@inheritdoc} |
|
46
|
|
|
*/ |
|
47
|
|
|
public function validate() |
|
48
|
|
|
{ |
|
49
|
|
|
// @codeCoverageIgnoreStart |
|
50
|
|
|
if (!function_exists('idn_to_ascii') && !class_exists('\idna_convert')) { |
|
51
|
|
|
throw new Exception('Unable to convert punycode. Either install the PHP Intl extension, or the mabrahamde/idna-converter Composer package.'); |
|
|
|
|
|
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
// Use PHP Intl first |
|
55
|
|
|
if (function_exists('idn_to_ascii')) { |
|
56
|
|
|
$email = idn_to_ascii($this->value); |
|
57
|
|
|
|
|
58
|
|
|
// Fall back to the idna_convert class |
|
59
|
|
|
} elseif (class_exists('\idna_convert')) { |
|
60
|
|
|
$idn = new \idna_convert([ |
|
61
|
|
|
'idn_version' => 2008 |
|
62
|
|
|
]); |
|
63
|
|
|
|
|
64
|
|
|
$email = $idn->encode($this->value); |
|
65
|
|
|
|
|
66
|
|
|
// Fall back to no conversion |
|
67
|
|
|
} else { |
|
68
|
|
|
$email = $this->value; |
|
69
|
|
|
} |
|
70
|
|
|
// @codeCoverageIgnoreEnd |
|
71
|
|
|
|
|
72
|
|
|
if (!( |
|
73
|
|
|
filter_var($email, FILTER_VALIDATE_EMAIL) && // RFC 822 (obsolete). Exceptions: no comments, no whitespace |
|
74
|
|
|
preg_match('/@.+\./', $email) && // Standard email formatting |
|
|
|
|
|
|
75
|
|
|
!preg_match('/@\[/', $email) && // Disallow IPv6 domains |
|
|
|
|
|
|
76
|
|
|
!preg_match('/".+@/', $email) && // Disallow quotes |
|
|
|
|
|
|
77
|
|
|
!preg_match('/=.+@/', $email) && // Disallow equals |
|
|
|
|
|
|
78
|
|
|
!preg_match('/localhost/i', $email) && // Disallow localhost |
|
|
|
|
|
|
79
|
|
|
!preg_match('/localdomain/i', $email) // Disallow localdomain |
|
80
|
|
|
)) { |
|
81
|
|
|
throw new UnexpectedValueException( |
|
82
|
|
|
sprintf('The value "%s" is not a valid email address.', $this->value) |
|
83
|
|
|
); |
|
84
|
|
|
} |
|
85
|
|
|
} |
|
86
|
|
|
} |
|
87
|
|
|
|
Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.