You are here

function fb_user_fb in Drupal for Facebook 5

Same name and namespace in other branches
  1. 5.2 fb_user.module \fb_user_fb()
  2. 6.3 fb_user.module \fb_user_fb()
  3. 6.2 fb_user.module \fb_user_fb()
  4. 7.4 fb_user.module \fb_user_fb()
  5. 7.3 fb_user.module \fb_user_fb()

Implementation of hook_fb.

File

./fb_user.module, line 393
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_fb($fb, $fb_app, $op, &$return, $data) {
  if ($op == FB_OP_INITIALIZE) {
    global $user;
    $fb_app_data = fb_app_get_data($fb_app);
    $fb_user_data = $fb_app_data['fb_user'];

    // our configuration
    // Here we ask facebook to prompt the user to login or add the app.
    if ($fb_user_data['require_login'] == FB_USER_OPTION_REQUIRE_LOGIN) {
      $fb
        ->require_login();
    }
    else {
      if ($fb_user_data['require_login'] == FB_USER_OPTION_REQUIRE_ADD) {
        $fb
          ->require_add();
      }
    }

    // If we know the user's fbu, try to load the corresponding local account.
    $fbu = fb_facebook_user();
    if ($fbu) {

      // Remember the original uid (probably 0 for anonymous)
      $original_uid = $user->uid;
      if ($user->fbu != $fbu) {

        // Try the application-specific account.
        $account = user_external_load("{$fbu}-{$fb_app->apikey}@facebook.com");
        if (!$account) {

          // Try the cross-application account.
          $account = user_external_load("{$fbu}@facebook.com");
        }
        if ($account) {
          $account->fbu = $fbu;
          $user = $account;

          // change the global user
        }
      }

      // Later, if we do not create an account, we'll load the default user
      // specified in the app config.
      // Check if we need to create a local account for this user.
      if ($fb_user_data['create_account'] == FB_USER_OPTION_CREATE_ADD && $fb->api_client
        ->users_isAppAdded() || $fb_user_data['create_account'] == FB_USER_OPTION_CREATE_LOGIN) {

        // Check if the local account is already made.
        if ($user->fbu != fb_facebook_user() && !_fb_user_special_page()) {

          // We need to make a local account for this facebook user.
          $user = fb_user_create_local_user($fb, $fb_app, fb_facebook_user(), array(
            'app_specific' => $fb_user_data['unique_account'],
            'roles' => array(
              $fb_user_data['new_user_rid'] => TRUE,
            ),
          ));
          watchdog('fb_user', t("Created new user !username for application %app", array(
            '!username' => theme('username', $user),
            '%app' => $fb_app->label,
          )));
        }
      }

      // It's possible the user was already created by another app.
      // In this case we need to add our role.
      if ($user->fbu == fb_facebook_user() && $fb_user_data['new_user_rid'] && !$user->roles[$fb_user_data['new_user_rid']]) {

        // there should be an API for this...
        db_query('INSERT INTO {users_roles} (uid, rid) VALUES (%d, %d)', $user->uid, $fb_user_data['new_user_rid']);
        watchdog('fb_user', t("Added role %role to existing user !username for application %app", array(
          '!username' => theme('username', $user),
          '%app' => $fb_app->label,
          '%role' => $fb_user_data['new_user_rid'],
        )));
      }
      if ($user->uid != $original_uid) {

        // We've changed the user.  In order to ensure that drupal handles permissions properly, the user must make the request all over again.
        if (function_exists('fb_canvas_fix_url')) {

          // Redirect to a canvas page
          $url = fb_canvas_fix_url(url(fb_scrub_urls($_REQUEST['q']), NULL, NULL, TRUE), $fb_app);
          if (fb_verbose()) {
            watchdog('fb_debug', "User uid is now {$user->uid} (was {$original_uid}), redirecting to {$url} to ensure permissions are correct.");
          }

          // debug
          $fb
            ->redirect($url);
        }
      }

      // Keep a record of when user accesses app, and whether they have added it.
      _fb_user_track($fb, $fb_app, $user);
    }

    // Don't mess with the user info if the user is visiting the login pages or submitting a form (i.e. the login form).
    if (!$user->uid && !_fb_user_special_page() && !$_REQUEST['form_id']) {
      if ($fbu = fb_facebook_user()) {
        $uid = $fb_app_data['fb_user']['logged_in_uid'];
      }
      else {
        $uid = $fb_app_data['fb_user']['not_logged_in_uid'];
      }
      if ($uid) {
        $user = user_load(array(
          'uid' => $uid,
        ));

        /* too verbose
           watchdog('fb_user', t('Treating a facebook user as local user !user',
                                 array('!user' => $user->name,
                                 )));
           */
      }
    }

    // We don't want user's who are not logged in (in the facebook sense) to
    // login locally.  So let's make sure they've added the app before doing
    // anything related to Drupal accounts.
    if (strpos($_GET['q'], 'user/login') === 0) {

      // Have to check idAppAdded in case of iframe.
      if (!$fb->api_client
        ->users_isAppAdded()) {
        $fb
          ->require_add();
      }
    }
    else {
      if (strpos($_GET['q'], 'user/register') === 0) {
        if (!$fb->api_client
          ->users_isAppAdded()) {
          $fb
            ->require_add();
        }
      }
    }

    // Now do I need a goto or some such???
    // debug

    /*
    drupal_set_message("To Drupal, you are " . theme('username', $user));
    drupal_set_message("Facebook user id is " . fb_facebook_user());
    drupal_set_message("Facebook logged in is " . $fb->get_loggedin_user());
    */
  }
  else {
    if ($op == FB_OP_GET_FBU) {

      // This is a request to learn the user's FB id.
      $return = _fb_user_get_fbu($data['uid'], $fb_app);
    }
    else {
      if ($op == FB_OP_GET_USER_SESSION) {

        // The fb module is asking for session login information.  For example, to
        // log in as the user when not on a canvas page.  This module may be able
        // to provide it, depending on whether the user has logged in, and whether
        // the session has expired.
        $fbu = $data['fbu'];
        $result = db_query("SELECT * FROM {fb_user_app} WHERE apikey = '%s' and fbu = %d", $fb_app->apikey, $fbu);
        $data = db_fetch_object($result);
        if ($data && $data->session_key) {

          // Return array with FB id and apikey.
          $return = array(
            $data->fbu,
            $data->session_key,
          );
        }
      }
    }
  }
}