You are here

function fboauth_save in Facebook OAuth (FBOAuth) 7

Same name and namespace in other branches
  1. 6 fboauth.module \fboauth_save()
  2. 7.2 fboauth.module \fboauth_save()

Save a Drupal User ID to Facebook ID pairing.

Passing in NULL for $fbid can also be used to delete a UID to FBID pairing.

6 calls to fboauth_save()
fboauth_action_connect in includes/fboauth.fboauth.inc
Facebook OAuth callback for initiating a Facebook connection.
fboauth_action_deauth in includes/fboauth.fboauth.inc
Facebook OAuth callback for deauthorizing the site from Facebook.
fboauth_deauthorize in includes/fboauth.fboauth.inc
Process a deauthorization request from Facebook.
fboauth_user_delete in ./fboauth.module
Implements hook_user_delete().
fboauth_user_insert in ./fboauth.module
Implements hook_user_insert().

... See full list

File

./fboauth.module, line 259

Code

function fboauth_save($uid, $fbid) {

  // Delete the existing Facebook ID if present for this Drupal user.
  $delete_query = 'DELETE FROM {fboauth_users} WHERE uid = :uid';

  // If setting a new Facebook ID for an account, also make sure no other
  // Drupal account is connected with this Facebook ID.
  if (isset($fbid)) {
    $delete_query .= ' OR fbid = :fbid';
    db_query($delete_query, array(
      ':uid' => $uid,
      ':fbid' => $fbid,
    ));
  }
  else {
    db_query($delete_query, array(
      ':uid' => $uid,
    ));
  }
  if (!empty($fbid)) {
    $id = db_insert('fboauth_users')
      ->fields(array(
      'uid' => $uid,
      'fbid' => $fbid,
    ))
      ->execute();
  }
  $loggedin_account = user_load($uid);

  //load user, all user fields & fbuid

  // Check if the logged in user has a picture saved & has linked their FB account through fboauth, but only if
  // the site is configured to do this.
  if (variable_get('fboauth_user_picture', 'picture') == 'picture' && is_null($loggedin_account->picture) && !empty($fbid)) {
    $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/' . $fbid . '/picture?type=large');
      $picture_path = file_stream_wrapper_uri_normalize($picture_directory . '/picture-' . $loggedin_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 = $loggedin_account->uid;
      $picture_file = file_save($picture_file);
      file_usage_add($picture_file, 'user', 'user', $loggedin_account->uid);
      db_update('users')
        ->fields(array(
        'picture' => $picture_file->fid,
      ))
        ->condition('uid', $loggedin_account->uid)
        ->execute();
    }
  }
}