-
Notifications
You must be signed in to change notification settings - Fork 0
/
email_verify.module
224 lines (212 loc) · 7.26 KB
/
email_verify.module
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
<?php
/**
* @file
* Verifies thoroughly that email addresses are correctly entered.
*
* Copyright: Daniel Bonniot <[email protected]>.
* License: GNU GPL v2 or later.
*/
/**
* Implements hook_config_info().
*/
function email_verify_config_info() {
$prefixes['email_verify.settings'] = array(
'label' => t('Email Verify'),
'group' => t('Configuration'),
);
return $prefixes;
}
/**
* Implements hook_permission().
*/
function email_verify_permission() {
return array(
'bypass email verification' => array(
'title' => t('Bypass email verification'),
'description' => t('Allow email verification to be bypassed when performing user operations.'),
),
);
}
/**
* Implements hook_menu().
*/
function email_verify_menu() {
$items = array();
// The route to the system configuration page.
$items['admin/config/system/email_verify'] = array(
'title' => 'Email Verify',
'page callback' => 'backdrop_get_form',
'page arguments' => array('email_verify_admin_settings'),
'access arguments' => array('administer site configuration'),
'description' => "Configure the Email Verify module's administrative settings.",
'file' => 'email_verify.admin.inc',
);
// The route to the user email address check page.
$items['admin/people/email_verify'] = array(
'title' => 'Email Verify',
'page callback' => 'backdrop_get_form',
'page arguments' => array('email_verify_user_check_form'),
'access callback' => 'email_verify_access_people_email_verify',
'type' => MENU_LOCAL_TASK,
'file' => 'email_verify.check.inc',
);
return $items;
}
/**
* Access callback for the page at admin/people/email_verify.
*/
function email_verify_access_people_email_verify() {
if (email_verify_activated() && user_access('administer users')) {
return TRUE;
}
return FALSE;
}
/**
* Implements hook_form_alter().
*/
function email_verify_form_alter(&$form, &$form_state, $form_id) {
if (email_verify_activated() && !user_access('bypass email verification')) {
// Get the list of fields.
$fields = email_verify_get_form_fields($form_id);
if (!empty($fields)) {
// Add the email verification validation callback.
if (empty($form['#validate'])) {
$form['#validate'] = array('email_verify_verify_address');
}
else {
array_unshift($form['#validate'], 'email_verify_verify_address');
}
if (empty($form['submit']['#validate'])) {
$form['submit']['#validate'] = array('email_verify_verify_address');
}
else {
array_unshift($form['submit']['#validate'], 'email_verify_verify_address');
}
}
}
}
/**
* Additional validation for the form to verify the email address.
*/
function email_verify_verify_address($form, &$form_state) {
if (isset($form_state['values']['op']) && isset($form_state['values']['submit'])
&& $form_state['values']['op'] == $form_state['values']['submit']) {
// Get the list of fields.
$fields = email_verify_get_form_fields($form_state['values']['form_id']);
if (!empty($fields)) {
foreach ($fields as $field) {
if (!empty($form_state['values'][$field])) {
// Get the data from the field.
if (is_string($form_state['values'][$field])) {
// Verify the e-mail address.
$results = email_verify_check($form_state['values'][$field]);
if (!empty($results['debugging_text'])) {
// Log and/or display the debugging information.
email_verify_process_debug_information($results['debugging_text']);
}
// Report the error and flag the form field.
if (!empty($results['verification_message'])) {
form_set_error($field, $results['verification_message']);
}
}
elseif (is_array($form_state['values'][$field])) {
foreach ($form_state['values'][$field] as $lang_code) {
foreach ($lang_code as $value) {
$address = reset($value);
if (!empty($address)) {
// Verify the e-mail address.
$results = email_verify_check($address);
if (!empty($results['debugging_text'])) {
// Log and/or display the debugging information.
email_verify_process_debug_information($results['debugging_text']);
}
// Report the error and flag the form field.
if (!empty($results['verification_message'])) {
form_set_error($field, $results['verification_message']);
}
}
}
}
}
}
}
}
}
}
/**
* Retrieves the forms and fields the admin specified to verify.
*
* @param string $form_id
* The ID of the form to retrieve the fields for.
*
* @return array
* An associative array with the key being the form and the
*/
function email_verify_get_form_fields($form_id) {
$verify_form_fields = array();
// Get the list of forms and fields.
$email_verify_forms = config_get('email_verify.settings', 'email_verify_forms');
if (!empty($email_verify_forms)) {
// Convert the text data into an array of strings, each representing one
// form/field combination.
$forms_and_fields = explode("\n", $email_verify_forms);
// Process each form/field pair.
foreach ($forms_and_fields as $form_and_field) {
// Make sure it isn't an empty array element.
if (!empty($form_and_field)) {
$form_and_field = explode(',', $form_and_field);
$form = trim($form_and_field[0]);
$field = trim($form_and_field[1]);
// If the form and field variables are not empty, and the form variable
// matches $form_id, save the field ID.
if (!empty($form) && !empty($field) && $form == $form_id) {
$verify_form_fields[] = $field;
}
}
}
}
return $verify_form_fields;
}
/**
* Verifies whether the given mail address exists.
*
* @param string $mail
* Email address to verify.
*
* @return string
* NULL if the address exists, or an error message if we found a problem with
* the address.
*/
function email_verify_check($mail) {
module_load_include('inc', 'email_verify');
return _email_verify_check($mail);
}
/**
* Determine if Email Verify is activated.
*
* @return bool
* Indicates whether the module is activated or not.
*/
function email_verify_activated() {
return config_get('email_verify.settings', 'email_verify_active');
}
/**
* Displays and/or logs the collected debugging information.
*
* @param array $debug_information
* An array of text strings reported during the debugging process.
*/
function email_verify_process_debug_information(array $debug_information) {
if (!empty($debug_information)) {
// Log and/or display the debugging information.
$message = implode('<br />', $debug_information);
$debug_mode_record_log = config_get('email_verify.settings', 'email_verify_debug_mode_record_log');
if ($debug_mode_record_log) {
watchdog('email_verify debug', $message, array(), WATCHDOG_DEBUG);
}
$debug_mode_display_page = config_get('email_verify.settings', 'email_verify_debug_mode_display_page');
if ($debug_mode_display_page) {
backdrop_set_message($message);
}
}
}