-
Notifications
You must be signed in to change notification settings - Fork 0
/
email_verify.admin.inc
435 lines (417 loc) · 17 KB
/
email_verify.admin.inc
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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
<?php
/**
* @file
* This is for the administrative settings for this module.
*/
/**
* Email Verify administration settings form.
*
* @return array
* The admin settings form.
*/
function email_verify_admin_settings($form, &$form_state) {
$config = config('email_verify.settings');
$form['#config'] = 'email_verify.settings';
$form['email_verify_activate'] = array(
'#type' => 'fieldset',
'#title' => t('Activate module'),
'#collapsible' => TRUE,
);
$form['email_verify_activate']['email_verify_active'] = array(
'#type' => 'checkbox',
'#title' => t('Activate this module to verify email adresses'),
'#default_value' => email_verify_activated(),
'#description' => t('When activated, Email Verify will check email addresses for validity.'),
);
$form['email_verify_activate']['test_options'] = array(
'#type' => 'fieldset',
'#title' => t('Testing options'),
'#collapsible' => TRUE,
'#collapsed' => TRUE,
'#description' => t("If the test fails when checking whether this module will work on your system or not, you can try changing the options below to see if they will work better for you."),
);
$form['email_verify_activate']['test_options']['test_options_host_name'] = array(
'#type' => 'textfield',
'#title' => t("Host name"),
'#default_value' => $config->get('test_options_host_name'),
'#description' => t('The name of the host to test with. The default is "backdropcms.org".'),
);
// The test is to see if port 25 is open, so offering to change the port
// number will not give the correct results. However, there might be some
// valid use case for being able to set the port number, so this is being left
// here, but commented out, for now.
// $form['email_verify_enable']['test_options']['test_options_port_number'] = array(
// '#type' => 'textfield',
// '#title' => t("Port number"),
// '#default_value' => $config->get('test_options_port_number'),
// '#description' => t('The number of the port to test with. The default is "25".'),
// );
$form['email_verify_activate']['test_options']['test_options_timeout'] = array(
'#type' => 'textfield',
'#title' => t("Timeout"),
'#default_value' => $config->get('test_options_timeout'),
'#description' => t('The connection timeout, in seconds. The default is "15".'),
);
$form['email_verify_debug_mode'] = array(
'#type' => 'fieldset',
'#title' => t('Debug mode'),
'#collapsible' => TRUE,
);
$form['email_verify_debug_mode']['debug_mode'] = array(
'#type' => 'checkbox',
'#title' => t('Enable'),
'#default_value' => $config->get('debug_mode'),
'#description' => t('Check this box to get verbose information about each email address verification made.'),
);
$form['email_verify_debug_mode']['debug_mode_record_log'] = array(
'#type' => 'checkbox',
'#title' => t('Record debugging information in the database log'),
'#default_value' => $config->get('debug_mode_record_log'),
'#states' => array('enabled' => array(':input[name="email_verify_debug_mode"]' => array('checked' => TRUE))),
);
$form['email_verify_debug_mode']['debug_mode_display_page'] = array(
'#type' => 'checkbox',
'#title' => t('Display debugging information on the page'),
'#default_value' => $config->get('debug_mode_display_page'),
'#states' => array('enabled' => array(':input[name="email_verify_debug_mode"]' => array('checked' => TRUE))),
);
$date_types = system_get_date_formats();
$options = array();
foreach ($date_types as $name => $date_type) {
if (is_array($date_type) && isset($date_type['label'])) {
$options[$name] = $date_type['label'];
}
}
$form['email_verify_debug_mode']['debug_mode_date_format'] = array(
'#type' => 'select',
'#title' => t('Date/time format'),
'#default_value' => $config->get('debug_mode_date_format'),
'#options' => $options,
'#description' => t('Select the date format to use when displaying the dates and times in the debug information.<br />
Date types and formats can be modified in the system !datetime settings pages.', array('!datetime' => l(t('Date and time'), 'admin/config/regional/date-time'))),
'#states' => array('enabled' => array(':input[name="email_verify_debug_mode"]' => array('checked' => TRUE))),
);
$form['email_verify_forms_to_check'] = array(
'#type' => 'fieldset',
'#title' => t('Forms to check'),
'#collapsible' => TRUE,
);
$form['email_verify_forms_to_check']['email_verify_forms'] = array(
'#type' => 'textarea',
'#description' => t('
Enter a list of form IDs and field IDs of the forms and corresponding
email address fields on which to verify the email addresses. Each
form/field combination should be on a separate line and the form ID and
field ID should be separated with a comma and should look something like
this: <br />user_register_form, mail<br />user_profile_form, mail
'),
'#default_value' => $config->get('email_verify_forms'),
);
$form['email_verify_methods'] = array(
'#type' => 'fieldset',
'#title' => t('Methods to use'),
'#collapsible' => TRUE,
'#description' => t("Check the boxes for the various methods to use when verifying email addresses. If you find you're getting lots of false positives and/or false negatives, try changing which options are enabled."),
);
$form['email_verify_methods']['methods_checkdnsrr'] = array(
'#type' => 'checkbox',
'#title' => t("Check for any DNS records"),
'#default_value' => $config->get('methods_checkdnsrr'),
'#description' => t("Use PHP's checkdnsrr() function to see if there are any DNS records associated with the email address' domain name."),
);
$form['email_verify_methods']['methods_gethostbyname'] = array(
'#type' => 'checkbox',
'#title' => t("Check for a valid IPv4 address"),
'#default_value' => $config->get('methods_gethostbyname'),
'#description' => t("Use PHP's gethostbyname() function to see if a valid IPv4 address is associated with the email address' domain name."),
);
$form['email_verify_methods']['methods_add_dot'] = array(
'#type' => 'checkbox',
'#title' => t("Add a dot to the domain"),
'#default_value' => $config->get('methods_add_dot'),
'#description' => t("For hosts that add their own domain to the end of the domain in the email address, this adds an additional '.' to the end of the email address domain, so that the check will not fail at the wrong time."),
);
// $form['email_verify_methods']['methods_extra_chars'] = array(
// '#type' => 'checkbox',
// '#title' => t("Allow non-basic ASCII characters"),
// '#default_value' => $config->get('methods_extra_chars'),
// '#description' => t("Check email addresses with domains with characters that are not in the basic ASCII set: http://php.net/manual/en/function.idn-to-ascii.php."),
// );
$form['#submit'] = array('email_verify_admin_settings_submit');
return system_settings_form($form);
}
/**
* Form submit function.
*
* This runs the module verification check, if activating, and logs whether the
* module was activated or inactivated.
*/
function email_verify_admin_settings_submit($form, &$form_state) {
// Enable/disable mail sending subsystem.
if ($form_state['values']['email_verify_active']) {
if (!email_verify_activated()) {
$debugging_text = email_verify_enable_module();
watchdog('email_verify', 'The Email Verify module was activated.');
if (!empty($debugging_text)) {
// Log and/or display the debugging information.
email_verify_process_debug_information($debugging_text);
}
}
}
elseif (email_verify_activated()) {
watchdog('email_verify', 'The Email Verify module was inactivated.');
}
}
/**
* Checks the system for the capability to use this module.
*
* @todo
* This function works, but it needs some thought and potential rework, now
* that it is not in email_verify.install.
*/
function email_verify_enable_module() {
$config = config('email_verify.settings');
$debugging_mode = $config->get('debug_mode');
$date_time_format = $config->get('debug_mode_date_format');
$debugging_text = array();
if ($debugging_mode) {
$debugging_text[] = t(
'Beginning the system capability checks for the Email Verify module (!date_time).',
array('!date_time' => format_date(time(), $date_time_format))
);
}
// Check that fsockopen() works on port 25.
// @see: http://backdropcms.org/node/147883
//
// The following is duplicated code from email_verify_check(). The reason it
// exists is because in its original use case, it was being called on install,
// and the code was not being reliable loaded in all cases. That situation no
// longer exists, but before the code is just wholesale replaced by that
// function call, it needs to be compared and tested, as some of it was
// modified to work in this situation.
//
// If a previous enable found port 25 closed or fsockopen() disabled, don't
// test it again. Testing can cause a long delay on module enable. Completely
// uninstall and then re-install this module to re-test.
if ($debugging_mode) {
$debugging_text[] = t(
'Checking to see if the system capability checks have already been run (!date_time).',
array('!date_time' => format_date(time(), $date_time_format))
);
}
if ($config->get('skip_mailbox')) {
if ($debugging_mode) {
$debugging_text[] = t(
'The system capability checks have already been run, so the system capability check is stopping (!date_time).',
array('!date_time' => format_date(time(), $date_time_format))
);
}
return $debugging_text;
}
if ($debugging_mode) {
$debugging_text[] = t(
'The system capability checks have not been run, so the system capability check is continuing (!date_time).',
array('!date_time' => format_date(time(), $date_time_format))
);
}
// Check if fsockopen() is disabled.
if ($debugging_mode) {
$debugging_text[] = t(
'Checking to see if fsockopen() is enabled or disabled (!date_time).',
array('!date_time' => format_date(time(), $date_time_format))
);
}
if (!function_exists('fsockopen')) {
if ($debugging_mode) {
$debugging_text[] = t(
'The fsockopen() function does not exist, so the system capability check is stopping (!date_time).',
array('!date_time' => format_date(time(), $date_time_format))
);
}
$message = t('Email Verify will test email domains but not mailboxes because the fsockopen() function has been disabled.');
$config->set('skip_mailbox', TRUE);
$config->save();
backdrop_set_message($message, 'warning');
return $debugging_text;
}
if ($debugging_mode) {
$debugging_text[] = t(
'The fsockopen() function exists, so the system capability check is continuing (!date_time).',
array('!date_time' => format_date(time(), $date_time_format))
);
}
$host = $config->get('test_options_host_name');
if ($debugging_mode) {
$debugging_text[] = t(
'The host that will be used for checking the system capability is "%host" (!date_time).',
array(
'%host' => $host,
'!date_time' => format_date(time(), $date_time_format),
)
);
}
if ($debugging_mode) {
$debugging_text[] = t(
'Checking to see if Microsoft Windows compatible functions are needed (!date_time).',
array('!date_time' => format_date(time(), $date_time_format))
);
}
if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {
if ($debugging_mode) {
$debugging_text[] = t(
'Microsoft Windows was detected, so the compatible functions are being loaded (!date_time).',
array('!date_time' => format_date(time(), $date_time_format))
);
}
module_load_include('inc', 'email_verify', 'windows_compat');
}
else {
if ($debugging_mode) {
$debugging_text[] = t(
'Microsoft Windows was not detected, so the compatible functions are not being loaded (!date_time).',
array('!date_time' => format_date(time(), $date_time_format))
);
}
}
// What SMTP servers should we contact?
if ($debugging_mode) {
$debugging_text[] = t(
'Retrieving any MX records corresponding to the specified host "%host" (!date_time).',
array(
'%host' => $host,
'!date_time' => format_date(time(), $date_time_format),
)
);
}
$mx_hosts = array();
if (!getmxrr($host, $mx_hosts)) {
// When there is no MX record, the host itself should be used.
if ($debugging_mode) {
$debugging_text[] = t(
'No MX records were found, so the host itself will be used to check the system capability (!date_time).',
array('!date_time' => format_date(time(), $date_time_format))
);
}
$mx_hosts[] = $host;
}
else {
if ($debugging_mode) {
$debugging_text[] = t(
'MX records were found, so they will be used to check the system capability (!date_time): !mx_hosts',
array(
'!date_time' => format_date(time(), $date_time_format),
'!mx_hosts' => '<pre>' . print_r($mx_hosts, TRUE) . '</pre>',
)
);
}
}
$timeout = $config->get('test_options_timeout');
if ($debugging_mode) {
$debugging_text[] = t(
'The timeout setting for checking the system capability is "%timeout" seconds (!date_time).',
array(
'%timeout' => $timeout,
'!date_time' => format_date(time(), $date_time_format),
)
);
}
// Try to connect to one SMTP server.
if ($debugging_mode) {
$debugging_text[] = t(
'Checking the host(s) to see if a connection can be made to any of them (!date_time).',
array('!date_time' => format_date(time(), $date_time_format))
);
}
foreach ($mx_hosts as $smtp) {
$connect = @fsockopen($smtp, 25, $errno, $errstr, $timeout);
if ($debugging_mode) {
if ($connect === FALSE) {
$debugging_text[] = t(
'The attempt to connect to host "%smtp" failed. If provided, the error number was "%errno", and the error string was "%errstr" (!date_time).',
array(
'%smtp' => $smtp,
'%errno' => $errno,
'%errstr' => $errstr,
'!date_time' => format_date(time(), $date_time_format),
)
);
}
else {
$debugging_text[] = t(
'The attempt to connect to host "%smtp" succeeded. If provided, the error number was "%errno", and the error string was "%errstr" (!date_time).',
array(
'%smtp' => $smtp,
'%errno' => $errno,
'%errstr' => $errstr,
'!date_time' => format_date(time(), $date_time_format),
)
);
}
}
if (!$connect) {
if ($debugging_mode) {
$debugging_text[] = t(
'The system could not connect to "%smtp" and is continuing to the next host in the list (!date_time).',
array(
'%smtp' => $smtp,
'!date_time' => format_date(time(), $date_time_format),
)
);
}
if ($connect === FALSE && $errno === 0) {
if ($debugging_mode) {
$debugging_text[] = t(
'The result of fsockopen() was FALSE, and the error number was 0, which indicates a potential problem initializing the socket. This is the error string: "%errstr" (!date_time).',
array(
'%errstr' => $errstr,
'!date_time' => format_date(time(), $date_time_format),
)
);
}
watchdog('email_verify', 'There was a potential problem initializing the socket when attempting to check an email address.', array(), WATCHDOG_WARNING);
}
continue;
}
if (preg_match("/^220/", fgets($connect, 1024))) {
// OK, we have a SMTP connection.
if ($debugging_mode) {
$debugging_text[] = t(
'A connection was made to "%smtp", and so the system capability check is continuing (!date_time).',
array(
'%smtp' => $smtp,
'!date_time' => format_date(time(), $date_time_format),
)
);
}
break;
}
}
if (!$connect) {
if ($debugging_mode) {
$debugging_text[] = t(
'No connection could be made to any host. Domains will be checked for validity, but mailboxes cannot be checked (!date_time).',
array('!date_time' => format_date(time(), $date_time_format))
);
}
$config->set('skip_mailbox', TRUE);
$config->save();
$message = t("Email Verify will test email domains but not mailboxes because port 25 is closed on your host's firewall for security.");
watchdog('email_verify', $message, array(), WATCHDOG_WARNING);
backdrop_set_message($message, 'warning');
}
else {
if ($debugging_mode) {
$debugging_text[] = t(
'A connection was made to a host, so the system has passed the capability check (!date_time).',
array('!date_time' => format_date(time(), $date_time_format))
);
}
}
if ($debugging_mode) {
$debugging_text[] = t(
'Ending the system capability checks for the Email Verify module (!date_time).',
array('!date_time' => format_date(time(), $date_time_format))
);
}
return $debugging_text;
}