function fboauth_create_user in Facebook OAuth (FBOAuth) 6
Same name and namespace in other branches
- 7.2 includes/fboauth.fboauth.inc \fboauth_create_user()
- 7 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 150 - 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 = '%s'";
$uid = db_result(db_query($query, $username));
$i = 0;
while ($uid) {
$i++;
$uid = db_result(db_query($query, $username . $i));
}
if ($i > 0) {
$username = $username . $i;
}
// Initialize basic properties that are unlikely to need changing.
$edit = array(
'name' => $username,
'mail' => !empty($fbuser->email) ? $fbuser->email : '',
// If user_register is "1", then no approval required.
'status' => $options['status'],
'fboauth' => TRUE,
// Signify this is being imported by Facebook OAuth.
'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);
}
// 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);
}
$account = user_save(NULL, $edit);
// Retrieve the user's picture from Facebook and save it locally.
if ($account->uid && $options['picture'] === 'picture') {
$path = file_create_path('pictures');
file_check_directory($path, FILE_CREATE_DIRECTORY);
$picture_result = drupal_http_request('https://graph.facebook.com/' . $fbuser->id . '/picture?type=large');
$picture_path = $path . '/picture-' . $account->uid . '.jpg';
$file = fopen($picture_path, 'w');
fwrite($file, $picture_result->data);
fclose($file);
// Check to make sure the picture isn't too large for the site settings.
$picture_info = image_get_info($picture_path);
list($max_dimensions['width'], $max_dimensions['height']) = explode('x', variable_get('user_picture_dimensions', '85x85'));
if (image_get_toolkit() && $picture_info['width'] > $max_dimensions['width'] || $picture_info['height'] > $max_dimensions['height']) {
image_scale($picture_path, $picture_path, $max_dimensions['width'], $max_dimensions['height']);
}
// Update the database record.
db_query("UPDATE {users} SET picture = '%s' WHERE uid = %d", $picture_path, $account->uid);
}
// 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;
}