You are here

function _hybridauth_save_profile_picture in HybridAuth Social Login 7

Downloads user picture from the 3rd party and links it to the user account.

Returns user account.

1 call to _hybridauth_save_profile_picture()
hybridauth_user_register_submit in ./hybridauth.module
Custom submit handler for the standard user_register form.

File

./hybridauth.module, line 322

Code

function _hybridauth_save_profile_picture(&$account) {

  // Should we bother?
  if (!variable_get('hybridauth_profile_import_photo', FALSE) || !variable_get('user_pictures', 0) || !isset($account->data['hybridauth_data']['profile']['photoURL'])) {
    return $account;
  }
  $photo_url = $account->data['hybridauth_data']['profile']['photoURL'];

  // We need to have the file locally
  $tmp_photo = drupal_tempnam('temporary://', 'drupal_hybridauth-');
  $tmp_photo_realpath = drupal_realpath($tmp_photo);
  copy($photo_url, $tmp_photo_realpath);

  // We'll need a file object to work with the file
  $info = image_get_info($tmp_photo_realpath);
  $file = new stdClass();
  $file->uid = $account->uid;
  $file->status = 0;

  // mark the file as temporary
  $file->filename = basename($tmp_photo_realpath);
  $file->uri = $tmp_photo;
  $file->filemime = $info['mime_type'];
  $file->filesize = $info['file_size'];

  // The file should be an image
  $errors = array();
  $errors += file_validate_is_image($file);
  $errors += file_validate_image_resolution($file, variable_get('user_picture_dimensions', '85x85'));
  $errors += file_validate_size($file, variable_get('user_picture_file_size', '30') * 1024);

  // Make sure file extension is a valid image
  if (!in_array(strtolower($info['extension']), array(
    'jpg',
    'png',
    'gif',
  ))) {
    $errors[] = ' invalid image file extension.';
  }
  if (count($errors)) {
    drupal_set_message(t('Profile Image Import:') . ' ' . $errors[0], 'warning');

    // Clean up (set fid to avoid error messages)
    $file->fid = 0;
    file_delete($file);
  }
  else {

    // We'll need a valid file id on the file object; file_save() will give us one
    $file = file_save($file);

    // Update user account (fid is not empty, status is temporary -- image
    // will be moved to proper directory and assigned to the user)
    $fields['picture'] = $file;
    $account = user_save($account, $fields);
  }
  return $account;
}