You are here

function fb_api_init in Drupal for Facebook 5

Same name and namespace in other branches
  1. 5.2 fb.module \fb_api_init()
  2. 6.3 fb.module \fb_api_init()
  3. 6.2 fb.module \fb_api_init()
  4. 7.3 fb.module \fb_api_init()

Include the necessary facebook-platform code and initialize the API object.

Parameters

fbu To log into facebook as a particular user, pass the facebook id.: This is useful during cron jobs, for example, but rarely if ever needed on a canvas page. If no valid session key is known for the user, this call will still return a facebook reference.

If FB_FBU_INFINITE_SESSION is passed, we attempt to log into an infinite session we've configured.

If FB_FBU_ANY is passed in, we will log in as the canvas page user if already logged in. Otherwise we try the infinite session, if configured.

Future calls into the the facebook api could fail for various reasons. For one, we may fail to log in as the specified user. This call does not actually contact facebook to test the connection, it just sets things up so that when the connection is needed, it might work. Even if the connection is established, the user may not have sufficient permission to whatever you are asking facebook to do.

16 calls to fb_api_init()
action_fb_cron_per_user in ./fb_cron.module
action_fb_set_profile_fbml in ./fb_actions.module
Implementation of an Action. See Actions module.
action_fb_set_ref_fbml in ./fb_actions.module
Implementation of an Action.
action_fb_write_minifeed in ./fb_actions.module
Implementation of an Action. See Actions module.
fb_actions_cron_per_user in ./fb_actions.module

... See full list

File

./fb.module, line 126

Code

function fb_api_init($fb_app, $fbu) {

  //dpm(func_get_args(), "fb_api_init");

  //$trace = debug_backtrace();

  //dpm($trace, "fb_api_init call stack");
  static $cache = array();
  if (!count($cache)) {

    // Include FB API code.
    if (substr(phpversion(), 0, 1) == '4' || FB_USE_PHP4_API) {
      require_once 'facebook-platform/php4client/facebook.php';
    }
    else {
      require_once 'facebook-platform/client/facebook.php';

      // Ideally, we'd check that drupal has not already specified an exception handler.
      set_exception_handler('fb_report_exception');
    }
  }
  global $facebook_config;
  $facebook_config['debug'] = FALSE;

  // set TRUE for debug output from FB API
  if (!$cache[$fb_app->apikey]) {
    $cache[$fb_app->apikey] = array();
  }
  $fbu_orig = $fbu;

  // Determine the actual facebook user id to use.
  if ($GLOBALS['fb'] && $GLOBALS['fb']
    ->validate_fb_params() && ($fbu == FB_FBU_CURRENT || $fbu == FB_FBU_ANY || $fbu == $GLOBALS['fb']
    ->get_loggedin_user())) {
    return $GLOBALS['fb'];
  }
  else {
    if (($fbu == FB_FBU_CURRENT || $fbu == FB_FBU_ANY) && isset($_SESSION['fb_frame_params']) && !$_REQUEST['fb_sig']) {
      $params = $_SESSION['fb_frame_params'];
      $fbu = $params['user'];
      $session = $params['session_key'];
    }
    else {
      if ($fbu == FB_FBU_CURRENT && $GLOBALS['fb']) {

        // No current user to use, probably anonymous canvas page.
        return $GLOBALS['fb'];
      }
      else {
        if ($fbu == FB_FBU_INFINITE_SESSION || $fbu == FB_FBU_ANY) {

          // Learn the infinite session id and key
          $data = _fb_invoke($fb_app, FB_OP_GET_INFINITE_SESSION, array());
          $fbu = $data[0];
          $session = $data[1];
        }
        else {

          // FB user id passed in.
        }
      }
    }
  }
  if (!$cache[$fb_app->apikey][$fbu]) {

    // We don't have a cached resource for this app/user combo, so we're going to create one.
    $fb = new Facebook($fb_app->apikey, $fb_app->secret);

    // Iframes may link or redirect within the iframe, in which case facebook
    // will not provide all its normal variables.  To prepare for such a case,
    // we cache the facebook parameters in the session.
    // We update the cache whenever possible, to keep it current.
    if ($fb
      ->in_frame() && $fb
      ->validate_fb_params() && $fb->fb_params) {
      $_SESSION['fb_frame_params'] = $fb->fb_params;

      // hacky to use fb's internal data structure
    }

    // If we learned the session, above, use it
    if ($fbu && $session) {
      $fb
        ->set_user($fbu, $session);
    }
    else {
      if ($fbu && $fbu == $fb
        ->get_loggedin_user() && $fb
        ->validate_fb_params()) {

        // Canvas page, current user is logged in already.
        // Nothing to do here.
      }
      else {

        // FB user id passed in.  If we happen to have valid session info for
        // them, we can log in as them.
        $data = _fb_invoke($fb_app, FB_OP_GET_USER_SESSION, array(), array(
          'fbu' => $fbu,
        ));
        if (count($data) && $data[0] && $data[1]) {
          $fb
            ->set_user($data[0], $data[1]);
          $fbu = $data[0];
        }
      }
    }

    // Cache the result, in case we're called again.
    $cache[$fb_app->apikey][$fbu] = $fb;

    // Note that facebook api has not actually logged into facebook yet.
    // We won't really know if our session is valid until later.
    // get_loggedin_user does not really test it.
    if ($fbu_orig != FB_FBU_CURRENT && !$fb
      ->get_loggedin_user()) {

      // An FBU other than CURRENT was specified, but we failed to log in.
      watchdog('fb', t('Failed to log into facebook app %app as user %user', array(
        '%app' => $fb_app->title,
        '%user' => $fbu_orig,
      )), WATCHDOG_ERROR);
    }
  }
  $fb = $cache[$fb_app->apikey][$fbu];
  return $fb;
}