forked from vboctor/disposable_email_checker
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathdisposable.php
251 lines (219 loc) · 9.97 KB
/
disposable.php
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
<?php
# Disposable Email Checker - a static php based check for spam emails
# Copyright (C) 2007-2016 Victor Boctor
# This program is distributed under the terms and conditions of the MIT
# See the README and LICENSE files for details
/**
* A class that checks an email address and provides some facts about whether
* it is a disposable, free web mail, etc. The data that is used to make
* such decision is static as part of the class implementation, hence
* avoiding a round trip to a remote service. This makes the class much
* more efficient in scenarios where performance is an issue.
*/
class DisposableEmailChecker
{
private static $forwarding_domains_array = null;
private static $trash_domains_array = null;
private static $shredder_domains_array = null;
private static $time_bound_domains_array = null;
private static $open_domains_array = null;
/**
* Determines if the email address is disposable.
*
* @param string $p_email The email address to validate.
* @return boolean true: disposable, false: non-disposable.
*/
public static function is_disposable_email( $p_email ) {
return (
DisposableEmailChecker::is_forwarding_email( $p_email ) ||
DisposableEmailChecker::is_trash_email( $p_email ) ||
DisposableEmailChecker::is_time_bound_email( $p_email ) ||
DisposableEmailChecker::is_shredder_email( $p_email ) );
}
/**
* Determines if the email address is disposable email that forwards to
* users' email address. This is one of the best kind of disposable
* addresses since emails end up in the user's inbox unless the user
* cancel the address.
*
* @param string $p_email The email address to check.
* @return boolean true: disposable forwarding, false: otherwise.
*/
public static function is_forwarding_email( $p_email ) {
$t_domain = DisposableEmailChecker::_get_domain_from_address( $p_email );
if ( DisposableEmailChecker::$forwarding_domains_array === null ) {
DisposableEmailChecker::$forwarding_domains_array = DisposableEmailChecker::_load_file( 'forwarding_domains' );
}
return in_array( $t_domain, DisposableEmailChecker::$forwarding_domains_array );
}
/**
* Determines if the email address is trash email that doesn't forward to
* user's email address. This kind of address can be checked using a
* web page and no password is required for such check. Hence, data sent
* to such address is not protected. Typically users use these addresses
* to signup for a service, and then they never check it again.
*
* @param string $p_email The email address to check.
* @return boolean true: disposable trash mail, false: otherwise.
*/
public static function is_trash_email( $p_email ) {
$t_domain = DisposableEmailChecker::_get_domain_from_address( $p_email );
if ( DisposableEmailChecker::$trash_domains_array === null ) {
DisposableEmailChecker::$trash_domains_array = DisposableEmailChecker::_load_file( 'trash_domains' );
}
return in_array( $t_domain, DisposableEmailChecker::$trash_domains_array );
}
/**
* Determines if the email address is a shredder email address. Shredder
* email address delete all received emails without forwarding them or
* making them available for a user to check.
*
* @param string $p_email The email address to check.
* @return boolean true: shredded disposable email, false: otherwise.
*/
public static function is_shredder_email( $p_email ) {
$t_domain = DisposableEmailChecker::_get_domain_from_address( $p_email );
if ( DisposableEmailChecker::$shredder_domains_array === null ) {
DisposableEmailChecker::$shredder_domains_array = DisposableEmailChecker::_load_file( 'shredder_domains' );
}
return in_array( $t_domain, DisposableEmailChecker::$shredder_domains_array );
}
/**
* Determines whether a given email address is subaddressed or not.
* Subaddressed email addresses, also known as plus addresses or tagged
* addresses, have the form [email protected].
*
* @param string $address An email address to test.
* @return boolean true: subaddressed email, false: otherwise.
*
* @see https://en.wikipedia.org/wiki/Email_address#Sub-addressing
*/
public static function is_subaddressed_email($address) {
// A subaddressed email address must contain a username and a plus sign.
// Match any string that begins with one or more characters other than
// an at sign (@), followed by a plus sign (+).
return (preg_match('/^[^@]+\+/', $address) == 1);
}
/**
* Determines if the email address is time bound, these are the disposable
* addresses that auto expire after a pre-configured time. For example,
* 10 minutes, 1 hour, 2 hours, 1 day, 1 month, etc. These address can
* also be trash emails or forwarding emails.
*
* @param string $p_email The email address to check.
* @return boolean true: time bound disposable email, false: otherwise.
*/
public static function is_time_bound_email( $p_email ) {
$t_domain = DisposableEmailChecker::_get_domain_from_address( $p_email );
if ( DisposableEmailChecker::$time_bound_domains_array === null ) {
DisposableEmailChecker::$time_bound_domains_array = DisposableEmailChecker::_load_file( 'time_bound_domains' );
}
return in_array( $t_domain, DisposableEmailChecker::$time_bound_domains_array );
}
/**
* See is_open_email() for details.
* @param string $p_email The email address to check.
* @return boolean true: free domain email, false: otherwise.
*/
public static function is_free_email( $p_email ) {
return DisposableEmailChecker::is_open_email( $p_email );
}
/**
* Determines if the email address is an email address in an open domain. These are
* addresses that users can sign up for, typically free. They then has to login to
* these address to get the emails. These are not considered to be
* disposable emails, however, if the application is providing a free
* trial for an expensive server, then users can signup for more accounts
* to get further trials.
*
* If applications are to block these addresses, it is important to be aware
* that some users use open webmail as their primary email and that such
* service providers include hotmail, gmail, and yahoo.
*
* @param string $p_email The email address to check.
* @return boolean true: open domain email, false: otherwise.
*/
public static function is_open_email( $p_email ) {
$t_domain = DisposableEmailChecker::_get_domain_from_address( $p_email );
if ( DisposableEmailChecker::$open_domains_array === null ) {
DisposableEmailChecker::$open_domains_array = DisposableEmailChecker::_load_file( 'open_domains' );
}
return in_array( $t_domain, DisposableEmailChecker::$open_domains_array );
}
/**
* A debugging function that takes in an email address and dumps out the
* details for such email.
*
* @param string $p_email The email address to echo results for. This must be a
* safe script (i.e. no javascript, etc).
*/
public static function echo_results( $p_email ) {
echo 'email address = ', htmlspecialchars( $p_email ), '<br />';
echo 'is_disposable_email = ', DisposableEmailChecker::is_disposable_email( $p_email ) ? 'true' : 'false', '<br />';
echo 'is_forwarding_email = ', DisposableEmailChecker::is_forwarding_email( $p_email ) ? 'true' : 'false', '<br />';
echo 'is_trash_email = ', DisposableEmailChecker::is_trash_email( $p_email ) ? 'true' : 'false', '<br />';
echo 'is_time_bound_email = ', DisposableEmailChecker::is_time_bound_email( $p_email ) ? 'true' : 'false', '<br />';
echo 'is_shredder_email = ', DisposableEmailChecker::is_shredder_email( $p_email ) ? 'true' : 'false', '<br />';
echo 'is_free_email = ', DisposableEmailChecker::is_free_email( $p_email ) ? 'true' : 'false', '<br />';
}
/**
* A debugging function that outputs some statistics about the number of domains in
* each category.
*/
public static function echo_stats() {
// Trigger loading of all domains
$domain = 'example.com';
DisposableEmailChecker::is_forwarding_email( $domain );
DisposableEmailChecker::is_open_email( $domain );
DisposableEmailChecker::is_free_email( $domain );
DisposableEmailChecker::is_time_bound_email( $domain );
DisposableEmailChecker::is_shredder_email( $domain );
DisposableEmailChecker::is_trash_email( $domain );
DisposableEmailChecker::is_forwarding_email( $domain );
echo 'Forwarding Domains: ' . count( DisposableEmailChecker::$forwarding_domains_array ) . '<br />';
echo 'Free Domains: ' . count( DisposableEmailChecker::$open_domains_array ) . '<br />';
echo 'Shredded Domains: ' . count( DisposableEmailChecker::$shredder_domains_array ) . '<br />';
echo 'Time Bound: ' . count( DisposableEmailChecker::$time_bound_domains_array ) . '<br />';
echo 'Trash Domains: ' . count( DisposableEmailChecker::$trash_domains_array ) . '<br />';
}
//
// Private functions, shouldn't be called from outside the class
//
/**
* Load the specified file given its name.
*
* @param string $p_type The name of the file not including the path or extension (e.g. open_domains).
* @return array An array of domains matching the specified file name.
*/
private static function _load_file( $p_type ) {
$t_array = file( dirname( __FILE__ ) . '/' . $p_type . '.txt' );
$t_result_array = array();
foreach ( $t_array as $t_line ) {
$t_entry = trim( $t_line );
if ( empty( $t_entry ) ) {
continue;
}
# Exclude commented lines
if ( strpos( $t_entry, '#' ) === 0 ) {
continue;
}
$t_result_array[] = strtolower( $t_entry );
}
return $t_result_array;
}
/**
* A helper function that takes in an email address and returns a lower case
* domain.
*
* @param string $p_email The email address to extra the domain from.
* @return string The lower case domain or empty string if email not valid.
*/
private static function _get_domain_from_address( $p_email ) {
$t_domain_pos = strpos( $p_email, '@' );
// If no @ sign, assume domain was passed in and return as is.
if ( $t_domain_pos === false ) {
return $p_email;
}
return strtolower( substr( $p_email, $t_domain_pos + 1 ) );
}
}