-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtwilio.user.inc
194 lines (171 loc) · 5.34 KB
/
twilio.user.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
<?php
/**
* @file
* Twilio user account and registration related functionality
*/
/**
* Implements hook_user_categories().
*/
function twilio_user_categories() {
return array(
array(
'name' => 'twilio',
'title' => t('Mobile SMS'),
'weight' => 3,
),
);
}
/**
* Implements hook_user_load().
*/
function twilio_user_load($users) {
// Load data from the {twilio_user} table for the user account.
$result = db_select('twilio_user', 'u')
->fields('u', array('uid', 'country', 'number', 'status', 'code'))
->condition('uid', array_keys($users), 'IN')
->execute();
foreach ($result as $record) {
if (!empty($record->uid)) {
// Assign the twilio data to the user object.
$users[$record->uid]->twilio_user = (array) $record;
}
}
}
/**
* Implements hook_user_insert().
*/
function twilio_user_insert(&$edit, $account, $category) {
twilio_user_save($edit, $account, $category);
}
/**
* Implements hook_user_update().
*/
function twilio_user_update(&$edit, $account, $category) {
twilio_user_save($edit, $account, $category);
}
/**
* Implements hook_user_delete().
*/
function twilio_user_delete($account) {
db_delete('twilio_user')
->condition('uid', $account->uid)
->execute();
}
/**
* Saves mobile number data to the {twilio_user} table in the database.
* 8/17/mcl: modify call, only one param in backdrop hook_user_save
*/
//function twilio_user_save(&$edit, $account, $category) {
function twilio_user_save($account) {
if (isset($account->twilio_user['status'])) {
$number = $account->twilio_user;
$number->uid = $account->uid;
$primary_keys = array();
if (isset($account->twilio_user['status'])) {
if ($account->twilio_user['status'] == TWILIO_USER_CONFIRMED) {
$primary_keys = array('uid');
}
}
drupal_write_record('twilio_user', $number, $primary_keys);
}
}
/**
* Implements hook_user_login().
*/
function twilio_user_login(&$edit, $account) {
// If the users mobile number is in the verification state let them know they
// need to enter their verification code and link to their settings page.
if (twilio_edit_access($account) && !empty($account->twilio_user) && $account->twilio_user['status'] == 1) {
$account_link = l(t("account settings page"), 'user/' . $account->uid . '/edit/twilio');
drupal_set_message(t("You must confirm your phone number by entering the verification code sent to you via SMS. Go to the !link to enter your verification code.", array('!link' => $account_link)), 'warning');
}
}
/**
* Implements hook_FORMID_form_alter().
*/
function twilio_form_user_register_form_alter(&$form, &$form_state, $form_id) {
if ($twilio_registration_form = variable_get('twilio_registration_form', 0)) {
if ($twilio_registration_form == 2) {
$required = TRUE;
}
else {
$required = FALSE;
}
$form['account']['countrycode'] = array(
"#type" => 'select',
'#options' => twilio_country_codes(),
'#title' => t('Country code'),
);
$form['account']['number'] = array(
'#type' => 'textfield',
'#title' => t('Phone number'),
'#required' => $required,
);
$form['#validate'][] = 'twilio_register_validate';
$form['#submit'][] = 'twilio_register_submit';
}
}
/**
* Custom validation function for phone numbers during registration.
*/
function twilio_register_validate($form, &$form_state) {
$value = $form_state['values']['number'];
// Phone number is not required and not entered.
if (empty($value) && empty($form['account']['number']['#required'])) {
return;
}
// Something has been entered but is non numeric.
if (!is_numeric($value)) {
form_set_error('number', t('You must enter a valid phone number'));
}
// Something that looks like a number, verify it's not already taken.
elseif (twilio_verify_number($value)) {
form_set_error('number', t('This number is already in use and cannot be assigned to more than one account'));
}
}
/**
* Custom submit handler for phone numbers during registration.
*/
function twilio_register_submit($form, &$form_state) {
$value = $form_state['values']['number'];
// Phone number is not required and not entered.
if (empty($value) && empty($form['account']['number']['#required'])) {
return;
}
else {
$account = user_load($form_state['values']['uid']);
twilio_user_send_confirmation($account, $form_state['values']['number'], $form_state['values']['countrycode']);
}
}
/**
* Send confirmation message.
*
* @param object $account
* The user object of the account to message
*
* @param string $number
* The phone number to send the message
*
* @param string $country
* The country code for the number
*
* @todo Please document this function.
* @see http://drupal.org/node/1354
*/
function twilio_user_send_confirmation($account, $number, $country) {
$code = rand(1000, 9999);
$data = array(
'uid' => $account->uid,
'number' => $number,
'country' => $country,
'status' => TWILIO_USER_PENDING,
'code' => $code,
);
// 8/17/mcl: update array before call to user_save
$account->twilio_user = $data;
//$account = user_save($account, array('twilio' => $data), 'twilio');
twilio_user_save($account); // diff hook for backdrop
$message = "Confirmation code: $code";
twilio_send($number, $message, $country);
return $account;
}