-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathemail-checker.php
More file actions
246 lines (202 loc) · 8.96 KB
/
email-checker.php
File metadata and controls
246 lines (202 loc) · 8.96 KB
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
<?php
/*
Plugin Name: Disposable Email Checker (API-Aries)
Plugin URI: https://support.api-aries.com/hc/articles/1/3/3/email-checker
Description: WordPress plugin to check email for disposable emails using the "api-aries" API.
Version: 1.2
Author: API-Aries Team
Author URI: https://api-aries.com/
License: GPL2
*/
// Add a settings page to enter API token
add_action('admin_menu', 'email_checker_plugin_menu');
register_activation_hook(__FILE__, 'email_checker_activate');
add_action('admin_notices', 'email_checker_admin_notice');
function email_checker_plugin_menu() {
add_options_page(
'Disposable Email Checker Plugin Settings',
'Disposable Email Checker (API-Aries)',
'manage_options',
'email_checker_settings',
'email_checker_settings_page'
);
}
function email_checker_activate() {
$api_token = get_option('email_checker_api_token');
if (!$api_token) {
add_option('email_checker_activation_error', 'The Disposable Email Checker plugin requires a valid API token to function. Please enter a valid API token in the plugin settings.');
}
}
function email_checker_admin_notice() {
if ($message = get_option('email_checker_activation_error')) {
echo '<div class="notice notice-error"><p>' . $message . '</p></div>';
delete_option('email_checker_activation_error');
}
}
function email_checker_is_valid_token($api_token) {
$api_url = 'https://api.api-aries.com/v1/checkers/proxy/email/?email=valid@example.com';
$headers = array(
'APITOKEN: ' . $api_token
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $api_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$response = curl_exec($ch);
if (curl_errno($ch)) {
curl_close($ch);
return false;
}
curl_close($ch);
$data = json_decode($response, true);
if (isset($data['error_code']) && $data['error_code'] === 'XR12') {
return false;
}
return true;
}
function email_checker_settings_page() {
// Check if user has necessary permission
if (!current_user_can('manage_options')) {
return;
}
// Save settings on form submission
if (isset($_POST['email_checker_settings_submit'])) {
check_admin_referer('email_checker_save_settings');
$api_token = sanitize_text_field($_POST['email_checker_api_token']);
$enabled = isset($_POST['email_checker_enabled']) ? '1' : '0';
$disposable_email_message = sanitize_textarea_field($_POST['email_checker_disposable_email']);
update_option('email_checker_api_token', $api_token);
update_option('email_checker_enabled', $enabled);
update_option('email_checker_disposable_email_message', $disposable_email_message);
echo '<div class="notice notice-success"><p>Settings saved.</p></div>';
// Validate the API token
if (!email_checker_is_valid_token($api_token)) {
echo '<div class="notice notice-error"><p>Invalid API token. Please enter a valid token. <a href="https://support.api-aries.com/hc/articles/6/7/15/api-error-codes" target="_blank">Learn more</a>.</p></div>';
}
}
$disposable_email_message = get_option('email_checker_disposable_email_message', 'The email address provided is not valid. Please provide a valid email address. Disposable emails are not permitted.');
?>
<div class="wrap">
<h1>Email Checker Plugin Settings</h1>
<p>If you do not already possess a token, you have the option to <a href="https://forums.api-aries.com/" target="_blank">Sign up</a> and obtain one.</p>
<h2>Token Types - info</h2>
<p>Free - learn more - <a href="https://forums.api-aries.com/subscriptions/" target="_blank">Subscriptions</a></p>
<p>Paid - learn more - <a href="https://forums.api-aries.com/subscriptions/" target="_blank">Subscriptions</a></p>
<form method="post" action="">
<?php wp_nonce_field('email_checker_save_settings'); ?>
<table class="form-table">
<tr>
<th scope="row">API Token</th>
<td>
<input type="text" name="email_checker_api_token" value="<?php echo esc_attr(get_option('email_checker_api_token')); ?>" />
</td>
</tr>
<tr>
<th scope="row">Enable Email Checker</th>
<td>
<input type="checkbox" name="email_checker_enabled" value="1" <?php checked(get_option('email_checker_enabled'), '1'); ?> />
</td>
</tr>
<tr>
<th scope="row">Disposable Email Message</th>
<td>
<textarea name="email_checker_disposable_email" rows="5" cols="50"><?php echo esc_textarea($disposable_email_message); ?></textarea>
</td>
</tr>
</table>
<p>Add your API token. This should be found on the <a href="https://dashboard.api-aries.com">Dashboard</a>.</p>
<?php submit_button('Save Settings', 'primary', 'email_checker_settings_submit'); ?>
</form>
</div>
<?php
}
// Hook the function to validate email before registration
add_action('registration_errors', 'email_checker_validate_email', 10, 3);
add_action('user_register', 'email_checker_validate_existing_user_email');
add_action('profile_update', 'email_checker_validate_existing_user_email', 10, 2);
add_filter('preprocess_comment', 'email_checker_validate_comment_email');
function email_checker_validate_email($errors, $sanitized_user_login, $user_email) {
if (get_option('email_checker_enabled') !== '1') {
return $errors;
}
$validation_error = email_checker_check_email($user_email);
if ($validation_error) {
$errors->add('email_invalid', __($validation_error));
}
return $errors;
}
function email_checker_validate_existing_user_email($user_id) {
if (get_option('email_checker_enabled') !== '1') {
return;
}
$user = get_userdata($user_id);
$user_email = $user->user_email;
$validation_error = email_checker_check_email($user_email);
if ($validation_error) {
wp_die(__($validation_error));
}
}
function email_checker_validate_comment_email($commentdata) {
if (get_option('email_checker_enabled') !== '1') {
return $commentdata;
}
$user_email = $commentdata['comment_author_email'];
$validation_error = email_checker_check_email($user_email);
if ($validation_error) {
wp_die(__($validation_error));
}
return $commentdata;
}
function email_checker_check_email($email) {
$api_token = get_option('email_checker_api_token');
$disposable_email_message = get_option('email_checker_disposable_email_message', 'The email address provided is not valid. Please provide a valid email address. Disposable emails are not permitted.');
$default_messages = array(
'rate_limit_exceeded' => 'API rate limit exceeded. https://support.api-aries.com/hc/articles/6/7/15/api-error-codes',
'invalid_token' => 'Invalid API token. Please check your API token and try again.',
'missing_token' => 'API token missing. Please add your API token in the plugin settings.',
'daily_limit_exceeded' => 'Exceeded daily request limit. Please upgrade your plan or try again tomorrow.',
'service_unavailable' => 'Service unavailable. Please try again later.',
'unknown_error' => 'An unknown error occurred. Please try again later.'
);
$api_url = 'https://api.api-aries.com/v1/checkers/proxy/email/?email=' . urlencode($email);
$headers = array(
'APITOKEN: ' . $api_token
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $api_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$response = curl_exec($ch);
if (curl_errno($ch)) {
curl_close($ch);
return $default_messages['unknown_error'];
}
curl_close($ch);
$data = json_decode($response, true);
if (json_last_error() !== JSON_ERROR_NONE) {
return $default_messages['unknown_error'];
}
if (isset($data['error_code'])) {
$error_code = $data['error_code'];
switch ($error_code) {
case 'QR89':
return $default_messages['rate_limit_exceeded'];
case 'XR12':
case '100':
return $default_messages['invalid_token'];
case '101':
return $default_messages['missing_token'];
case '102':
return $default_messages['daily_limit_exceeded'];
case '103':
return $default_messages['service_unavailable'];
default:
return $default_messages['unknown_error'];
}
}
if (isset($data['disposable']) && $data['disposable'] === 'yes') {
return $disposable_email_message;
}
return null;
}
?>