You are here

function fboauth_create_user in Facebook OAuth (FBOAuth) 7

Same name and namespace in other branches
  1. 6 includes/fboauth.fboauth.inc \fboauth_create_user()
  2. 7.2 includes/fboauth.fboauth.inc \fboauth_create_user()

Given a Facebook user object, associate or save a Drupal user account.

1 call to fboauth_create_user()
fboauth_action_connect in includes/fboauth.fboauth.inc
Facebook OAuth callback for initiating a Facebook connection.

File

includes/fboauth.fboauth.inc, line 244
Provides functions used during Facebook login processes.

Code

function fboauth_create_user($fbuser, $options = array()) {

  // Set default options.
  $defaults = array(
    'username' => variable_get('fboauth_user_username', 'username'),
    'picture' => variable_get('user_pictures', 0) && variable_get('fboauth_user_picture', 'picture') ? 'picture' : '',
    'status' => variable_get('user_register', 1) == 1 ? 1 : 0,
  );
  $options += $defaults;

  // Use their Facebook user name (if defined), otherwise their real name.
  // If an account already exists with that name, increment until the namespace
  // is available.
  if ($options['username'] === 'username' && !empty($fbuser->username)) {
    $username = $fbuser->username;
  }
  else {
    $username = $fbuser->name;
  }
  $query = "SELECT uid FROM {users} WHERE name = :name";
  $uid = db_query($query, array(
    ':name' => $username,
  ))
    ->fetchField();
  $i = 0;
  while ($uid) {
    $i++;
    $uid = db_query($query, array(
      ':name' => $username . $i,
    ))
      ->fetchField();
  }
  if ($i > 0) {
    $username = $username . $i;
  }

  // get the facebook e-mail address in case the main e-mail address isn't available from facebook.
  // this still will fail if the Facebook user hasn't set a Facebook username, since per
  // https://www.drupal.org/node/2149661#comment-9623017 #6  the fbid with @facebook.com isn't a valid email.
  $facebook_mail = isset($fbuser->username) ? $fbuser->username . '@facebook.com' : '';

  // Initialize basic properties that are unlikely to need changing.
  $edit = array(
    'name' => $username,
    'mail' => !empty($fbuser->email) ? $fbuser->email : $facebook_mail,
    'init' => !empty($fbuser->email) ? $fbuser->email : $facebook_mail,
    // If user_register is "1", then no approval required.
    'status' => $options['status'],
    'timezone' => variable_get('date_default_timezone'),
    // TODO: is this appropriate in sites with no default?
    'fboauth' => TRUE,
    // Signify this is being imported by Facebook OAuth.
    // So that other modules can load the account.
    'fboauth_fbid' => $fbuser->id,
  );

  // Profile module support.
  if (module_exists('profile')) {
    module_load_include('inc', 'fboauth', 'includes/fboauth.profile');
    fboauth_profile_create_user($edit, $fbuser);
  }

  // Field module support.
  module_load_include('inc', 'fboauth', 'includes/fboauth.field');
  fboauth_field_create_user($edit, $fbuser);

  // Allow other modules to manipulate the user information before save.
  foreach (module_implements('fboauth_user_presave') as $module) {
    $function = $module . '_fboauth_user_presave';
    $function($edit, $fbuser);
  }

  // check for a valid email address and username and if they don't exist return NULL
  if (!isset($edit) || empty($edit)) {
    watchdog('fboauth', 'Account information not provided.');
    return NULL;
  }
  if (!isset($edit['mail']) || empty($edit['mail'])) {
    watchdog('fboauth', 'Email address not provided by Facebook.');
    return NULL;
  }
  if (!isset($edit['name']) || empty($edit['name'])) {
    watchdog('fboauth', 'Username not able to be retrieved from Facebook or be generated.');
    return NULL;
  }
  $account = user_save(NULL, $edit);

  // Retrieve the user's picture from Facebook and save it locally.
  if ($account->uid && $options['picture'] === 'picture') {
    $picture_directory = file_default_scheme() . '://' . variable_get('user_picture_path', 'pictures');
    if (file_prepare_directory($picture_directory, FILE_CREATE_DIRECTORY)) {
      $picture_result = drupal_http_request('https://graph.facebook.com/v2.2/' . $fbuser->id . '/picture?type=large');
      $picture_path = file_stream_wrapper_uri_normalize($picture_directory . '/picture-' . $account->uid . '-' . REQUEST_TIME . '.jpg');
      $picture_file = file_save_data($picture_result->data, $picture_path, FILE_EXISTS_REPLACE);

      // Check to make sure the picture isn't too large for the site settings.
      $max_dimensions = variable_get('user_picture_dimensions', '85x85');
      file_validate_image_resolution($picture_file, $max_dimensions);

      // Update the user record.
      $picture_file->uid = $account->uid;
      $picture_file = file_save($picture_file);
      file_usage_add($picture_file, 'user', 'user', $account->uid);
      db_update('users')
        ->fields(array(
        'picture' => $picture_file->fid,
      ))
        ->condition('uid', $account->uid)
        ->execute();
    }
  }

  // Allow other modules to manipulate the user information after save.
  foreach (module_implements('fboauth_user_save') as $module) {
    $function = $module . '_fboauth_user_save';
    $function($account, $fbuser);
  }
  return $account;
}