You are here

function sms_user_register_new_user in SMS Framework 6.2

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

Registers a new user via SMS

Parameters

$number: The user's mobile number.

$message: Body of the SMS message.

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

Return value

The user object of the created user.

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

File

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

Code

function sms_user_register_new_user($number, $message, $options) {
  global $user;
  $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' => 0,
    'code' => '',
    'gateway' => '',
  );

  // If by chance there's already a user with the same email address, then use
  // it instead of creating a new user.
  if ($mail && ($account = user_load(array(
    'mail' => $mail,
  )))) {
    $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 (!$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(time())), 0, 10);

    // Save the user.
    $edit['status'] = variable_get('user_register', 1) == 1;
    $account = user_save('', $edit);
  }

  // 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);
    $edit = array();
    user_module_invoke('login', $edit, $account);
    return $account;
  }
  return FALSE;
}