You are here

function fb_user_sync_cb in Drupal for Facebook 5

The sync callback is invoked first on a canvas page, in which case we require the user to add the application. Later the user will be redirected to this callback on the locale server, with an token that allows us to write the necessary row to the authmap table.

1 string reference to 'fb_user_sync_cb'
fb_user_menu in ./fb_user.module

File

./fb_user.module, line 256
This module allows Drupal user records to be associated with Facebook user ids. It can create local user accounts when Facebook users visit an application's canvas pages.

Code

function fb_user_sync_cb() {
  global $user;
  global $fb, $fb_app;

  // TODO: ensure that user does not already have a mapping to some other facebook id.
  // On canvas pages, require user to add the app...
  if ($fb) {
    if (fb_verbose() && function_exists('dprint_r')) {
      watchdog('debug', 'fb_user_sync_cb request ' . dprint_r($_REQUEST, 1));
    }
    $fb
      ->require_add();

    // The user has already added the app.
    // Redirect to the local page where the authmap will be generated.
    _fb_user_sync_redirect(FB_USER_SYNC_PATH);
  }
  else {

    // Double check user is logged in.  No point syncing the anonymous account.
    if (!$user->uid) {
      drupal_access_denied();
      exit;
    }
    $output = '';

    // On non canvas pages, we have returned to the local server.  Hopefully
    // the user has added the app and we can now sync the two accounts.
    if ($_REQUEST['sync_token']) {
      $key = $_REQUEST['sync_token'];
      $cache = cache_get($key);
      cache_clear_all($key, 'cache');

      // So noone else uses this key
      $data = unserialize($cache->data);
      if (!$data) {
        drupal_set_message(t('Unable to locate Facebook account information.'), 'error');
        drupal_access_denied();
        exit;
      }
      watchdog('debug', 'got the data ' . dprint_r($data, 1));
      $fbu = $data['fbu'];
      $fb_app = $data['fb_app'];
      drupal_set_title(t('Added %appname Application', array(
        '%appname' => $fb_app->title,
      )));
      if ($fbu && $fb_app) {
        list($module, $authname) = _fb_user_get_authmap($fb_app, $fbu);

        // Check if the authname is already in use.
        $account = user_external_load($authname);
        if ($account && $account->uid != $user->uid) {
          watchdog('fb_user', t('Re-syncing facebook account.  The authname %authname currently refers to !old_user_link.  Updating to point to !new_user_link.', array(
            '%authname' => $authname,
            '%old_username' => $account->name,
            '%old_uid' => $account->uid,
            '!old_user_link' => theme('username', $account),
            '!new_user_link' => theme('username', $user),
          )));

          // This will delete the old authmap entry
          user_set_authmaps($account, array(
            $module => NULL,
          ));
        }
        if (fb_verbose()) {
          watchdog('fb_user', t('Syncing local user %uid with facebook user %fbu via authmap entry %authname', array(
            '%fbu' => $fbu,
            '%uid' => $user->uid,
            '%authname' => $authname,
          )));
        }

        // Write the authmap
        user_set_authmaps($user, array(
          $module => $authname,
        ));
        drupal_set_message(t('Your <a href="!localurl">local account</a> is linked to your <a href="!facebookurl">Facebook profile</a>.', array(
          '!localurl' => url('user/' . $user->uid),
          '!facebookurl' => url('http://www.facebook.com/profile.php', 'id=' . $fbu),
        )));

        // Give the user feedback that the sync has been successful.
        // Query facebook to learn more about their facebook account.
        $fb = fb_api_init($fb_app, FB_FBU_ANY);
        if ($fb) {
          $info = $fb->api_client
            ->users_getInfo(array(
            $fbu,
          ), array(
            'about_me',
            'affiliations',
            'name',
            'is_app_user',
            'pic_big',
            'profile_update_time',
            'status',
          ));
          if (count($info)) {
            $output .= theme('fb_app_user_info', $fb_app, $info[0]);
          }
        }
      }
    }
  }

  // TODO: allow modules a way to customize the output of this function.
  return $output;
}