You are here

function sms_user_register_new_user in SMS Framework 7

Same name and namespace in other branches
  1. 6.2 modules/sms_user/sms_user.module \sms_user_register_new_user()

Registers a new user via SMS

Parameters

string $number: The user's mobile number.

string $message: Body of the SMS message.

array $options: An associative array of metadata passed from the incoming gateway.

Return value

object|bool The user object of the created user, false if registration failed.

1 call to sms_user_register_new_user()
sms_user_sms_incoming in modules/sms_user/sms_user.module
Implements hook_sms_incoming().

File

modules/sms_user/sms_user.module, line 1055
Provides integration between the SMS Framework and Drupal users.

Code

function sms_user_register_new_user($number, $message, $options) {
  $edit = array();

  // If we have a from address from the e-mail gateway, use it, otherwise
  // leave the e-mail fields blank.
  $mail = isset($options['sms_email_gateway_from']) ? $options['sms_email_gateway_from'] : '';

  // Pass in sms_user specific data for saving.
  $edit['sms_user'] = array(
    'number' => $number,
    'status' => SMS_USER_SMS_REGISTERED,
    'code' => '',
    'gateway' => array(),
  );
  $edit['timezone'] = '';

  // If by chance there's already a user with the same email address, then use
  // it instead of creating a new user.
  if (!empty($mail) && ($account = array_shift(user_load_multiple(array(), array(
    'mail' => $mail,
  ), TRUE)))) {
    $account = user_save($account, $edit);
  }
  else {
    $edit['mail'] = $edit['init'] = $mail;

    // Add password if enabled.
    if (variable_get('sms_user_allow_password', 0)) {
      $lines = explode("\n", $message);
      $words = explode(" ", $lines[0]);
      foreach ($words as $word) {
        if (trim($word)) {
          $edit['pass'] = preg_replace('/\\s+/', '-', $word);
          break;
        }
      }
    }

    // Auto-generated password.
    if (!isset($edit['pass']) || !$edit['pass']) {
      $edit['pass'] = user_password();
    }

    // Pick a pseudo-random name for the user -- using the email
    // address would be a privacy violation.
    $edit['name'] = substr(md5($number . strval(REQUEST_TIME)), 0, 10);

    // Save the user.
    $edit['status'] = variable_get('user_register', 1) == 1;
    $account = user_save('', $edit);
    watchdog('sms_user', '%name was created using SMS.', array(
      '%name' => $account->name,
    ));
  }

  // Verify that the account was created.
  if (is_object($account)) {
    $user = $account;
    $metadata = array(
      'register' => TRUE,
      'number' => $number,
      'message' => $message,
      'options' => $options,
    );
    sms_user_login_metadata($account->uid, $metadata);
    if ($account->status) {

      // User account is directly enabled.
      _sms_user_switch($account);
    }
    user_module_invoke('login', $edit, $account);
    return $account;
  }
  return FALSE;
}