1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the EmailChecker package. |
5
|
|
|
* |
6
|
|
|
* (c) Matthieu Moquet <[email protected]> |
7
|
|
|
* |
8
|
|
|
* This source file is subject to the MIT license that is bundled |
9
|
|
|
* with this source code in the file LICENSE. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace EmailChecker; |
13
|
|
|
|
14
|
|
|
use EmailChecker\Exception\InvalidEmailException; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Some utilities for email checking. |
18
|
|
|
* |
19
|
|
|
* @author Matthieu Moquet <[email protected]> |
20
|
|
|
*/ |
21
|
|
|
class Utilities |
22
|
|
|
{ |
23
|
|
|
const EMAIL_REGEX_LOCAL = '(?!(?:(?:\\x22?\\x5C[\\x00-\\x7E]\\x22?)|(?:\\x22?[^\\x5C\\x22]\\x22?)){255,})(?!(?:(?:\\x22?\\x5C[\\x00-\\x7E]\\x22?)|(?:\\x22?[^\\x5C\\x22]\\x22?)){65,}@)(?:(?:[\\x21\\x23-\\x27\\x2A\\x2B\\x2D\\x2F-\\x39\\x3D\\x3F\\x5E-\\x7E]+)|(?:\\x22(?:[\\x01-\\x08\\x0B\\x0C\\x0E-\\x1F\\x21\\x23-\\x5B\\x5D-\\x7F]|(?:\\x5C[\\x00-\\x7F]))*\\x22))(?:\\.(?:(?:[\\x21\\x23-\\x27\\x2A\\x2B\\x2D\\x2F-\\x39\\x3D\\x3F\\x5E-\\x7E]+)|(?:\\x22(?:[\\x01-\\x08\\x0B\\x0C\\x0E-\\x1F\\x21\\x23-\\x5B\\x5D-\\x7F]|(?:\\x5C[\\x00-\\x7F]))*\\x22)))*'; |
24
|
|
|
const EMAIL_REGEX_DOMAIN = '(?:(?:(?!.*[^.]{64,})(?:(?:(?:xn--)?[a-z0-9]+(?:-[a-z0-9]+)*\\.){1,126}){1,}(?:(?:[a-z][a-z0-9]*)|(?:(?:xn--)[a-z0-9]+))(?:-[a-z0-9]+)*)|(?:\\[(?:(?:IPv6:(?:(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){7})|(?:(?!(?:.*[a-f0-9][:\\]]){7,})(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){0,5})?::(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){0,5})?)))|(?:(?:IPv6:(?:(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){5}:)|(?:(?!(?:.*[a-f0-9]:){5,})(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){0,3})?::(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){0,3}:)?)))?(?:(?:25[0-5])|(?:2[0-4][0-9])|(?:1[0-9]{2})|(?:[1-9]?[0-9]))(?:\\.(?:(?:25[0-5])|(?:2[0-4][0-9])|(?:1[0-9]{2})|(?:[1-9]?[0-9]))){3}))\\]))'; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Extract parts of an email address. |
28
|
|
|
* |
29
|
|
|
* @param string $email The email address to parse |
30
|
|
|
* |
31
|
|
|
* @return array The parts of the email. First the local part, then the domain |
32
|
|
|
*/ |
33
|
|
|
public static function parseEmailAddress($email) |
34
|
|
|
{ |
35
|
|
|
$pattern = sprintf('/^(?<local>%s)@(?<domain>%s)$/iD', self::EMAIL_REGEX_LOCAL, self::EMAIL_REGEX_DOMAIN); |
36
|
|
|
|
37
|
|
|
if (!preg_match($pattern, $email, $parts)) { |
38
|
|
|
throw new InvalidEmailException(sprintf('"%s" is not a valid email', $email)); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
return array_map('strtolower', [$parts['local'], $parts['domain']]); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Parse content and extract lines. |
46
|
|
|
* |
47
|
|
|
* @param string $content The content to parse |
48
|
|
|
* |
49
|
|
|
* @return array Array of cleaned string |
50
|
|
|
*/ |
51
|
|
|
public static function parseLines($content) |
52
|
|
|
{ |
53
|
|
|
// Split by line |
54
|
|
|
$lines = explode("\n", $content); |
55
|
|
|
|
56
|
|
|
// Trim and convert to lowercase |
57
|
|
|
$lines = array_map('trim', $lines); |
58
|
|
|
$lines = array_map('strtolower', $lines); |
59
|
|
|
|
60
|
|
|
// Remove empty lines and comments |
61
|
|
|
$lines = array_filter($lines, function ($line) { |
62
|
|
|
return (0 === strlen($line) || '#' === $line[0]) ? false : $line; |
63
|
|
|
}); |
64
|
|
|
|
65
|
|
|
return $lines; |
66
|
|
|
} |
67
|
|
|
} |
68
|
|
|
|