View source
<?php
class TfaBasicSms extends TfaBasePlugin implements TfaValidationPluginInterface, TfaSendPluginInterface {
protected $client;
protected $twilioNumber;
protected $mobileNumber;
public function __construct(array $context, $mobile_number, $twilio_client) {
parent::__construct($context);
if (!empty($context['validate_context']) && !empty($context['validate_context']['code'])) {
$this->code = $context['validate_context']['code'];
}
$this->client = $twilio_client;
$this->twilioNumber = variable_get('tfa_basic_twilio_account_number', '');
$this->codeLength = 6;
$this->messageText = variable_get('tfa_basic_twilio_message_text', 'Verification code: !code');
$this->mobileNumber = $mobile_number;
if (!empty($context['mobile_number'])) {
$this->mobileNumber = $context['mobile_number'];
}
}
public function begin() {
if (!$this->code) {
$this->code = $this
->generate();
if (!$this
->sendCode($this->code)) {
drupal_set_message(t('Unable to deliver the code. Please contact support.'), 'error');
}
}
}
public function getForm(array $form, array &$form_state) {
$form['code'] = array(
'#type' => 'textfield',
'#title' => t('Verification Code'),
'#required' => TRUE,
'#description' => t('Enter @length-character code sent to your device.', array(
'@length' => $this->codeLength,
)),
);
if (module_exists('elements')) {
$form['code']['#type'] = 'numberfield';
}
$form['actions']['#type'] = 'actions';
$form['actions']['login'] = array(
'#type' => 'submit',
'#value' => t('Verify'),
);
$form['actions']['resend'] = array(
'#type' => 'submit',
'#value' => t('Resend'),
'#submit' => array(
'tfa_form_submit',
),
'#limit_validation_errors' => array(),
);
return $form;
}
public function validateForm(array $form, array &$form_state) {
if ($form_state['values']['op'] === $form_state['values']['resend']) {
return TRUE;
}
elseif (!parent::validate($form_state['values']['code'])) {
$this->errorMessages['code'] = t('Invalid code.');
return FALSE;
}
else {
return TRUE;
}
}
public function submitForm(array $form, array &$form_state) {
if ($form_state['values']['op'] === $form_state['values']['resend']) {
$this->code = $this
->generate();
if (!$this
->sendCode($this->code)) {
drupal_set_message(t('Unable to deliver the code. Please contact support.'), 'error');
}
else {
drupal_set_message(t('Code resent'));
}
return FALSE;
}
else {
return parent::submitForm($form, $form_state);
}
}
public function getPluginContext() {
return array(
'code' => $this->code,
);
}
protected function generate() {
$allowable_characters = '0123456789';
$len = strlen($allowable_characters) - 1;
$string = '';
for ($i = 0; $i < $this->codeLength; $i++) {
do {
$index = ord(drupal_random_bytes(1));
} while ($index > $len);
$string .= $allowable_characters[$index];
}
return $string;
}
protected function getAccountNumber() {
return $this->mobileNumber;
}
protected function sendCode($code) {
$to = $this
->getAccountNumber();
try {
$message = $this->client->account->messages
->sendMessage($this->twilioNumber, $to, t($this->messageText, array(
'!code' => $code,
)));
watchdog('tfa_basic', 'Message !id sent to user !uid on @sent', array(
'@sent' => $message->date_sent,
'!id' => $message->sid,
'!uid' => $this->context['uid'],
), WATCHDOG_INFO);
return TRUE;
} catch (Services_Twilio_RestException $e) {
watchdog('tfa_basic', 'Twilio send message error to user !uid. Status code: @code, message: @message, link: @link', array(
'!uid' => $this->context['uid'],
'@code' => $e
->getStatus(),
'@message' => $e
->getMessage(),
'@link' => $e
->getInfo(),
), WATCHDOG_ERROR);
return FALSE;
}
}
public function ready() {
if (!empty($this->mobileNumber)) {
$user_data = tfa_basic_get_tfa_data(user_load($this->context['uid']));
if (!empty($user_data['status'])) {
return TRUE;
}
}
return FALSE;
}
}
class TfaBasicSmsSetup extends TfaBasicSms implements TfaSetupPluginInterface {
public function __construct(array $context, $mobile_number, $client) {
parent::__construct($context, $mobile_number, $client);
}
public function begin() {
if (empty($this->code)) {
$this->code = $this
->generate();
if (!$this
->sendCode($this->code)) {
$this->errorMessages[''] = t('Unable to deliver code to that number.');
}
}
}
public function getSetupForm(array $form, array &$form_state) {
$form['sms_code'] = array(
'#type' => 'textfield',
'#title' => t('Verification Code'),
'#required' => TRUE,
'#description' => t('Enter @length-character code sent to your device.', array(
'@length' => $this->codeLength,
)),
);
if (module_exists('elements')) {
$form['sms_code']['#type'] = 'numberfield';
}
$form['actions']['verify'] = array(
'#type' => 'submit',
'#value' => t('Verify and save'),
);
return $form;
}
public function validateSetupForm(array $form, array &$form_state) {
if (!$this
->validate($form_state['values']['sms_code'])) {
$this->errorMessages['sms_code'] = t('Invalid code. Please try again.');
return FALSE;
}
else {
return TRUE;
}
}
public function submitSetupForm(array $form, array &$form_state) {
return TRUE;
}
}