You are here

function fb_api_init in Drupal for Facebook 6.2

Same name and namespace in other branches
  1. 5.2 fb.module \fb_api_init()
  2. 5 fb.module \fb_api_init()
  3. 6.3 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_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.

15 calls to fb_api_init()
fb_actions_cron_per_user in contrib/fb_actions.module
Trigger an action several times, emulating a different user each time. Useful for cron jobs in which we update each users profile box, for example.
fb_admin_get_app_properties in ./fb.admin.inc
Get properties from Facebook. Fills in the data that we need to know by querying facebook.
fb_admin_set_app_properties in ./fb.admin.inc
fb_app_set_app_properties in ./fb_app.admin.inc
Sets callback URLs and other properties of a facebook app. Calls the facebook
fb_app_user in ./fb_app.module
Implementation of hook_user.

... See full list

File

./fb.module, line 144

Code

function fb_api_init($fb_app, $fbu = FB_FBU_CURRENT) {

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

  //$trace = debug_backtrace();

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

  // This helps with uncaught exceptions.  However, it should be configurable
  // or at least not overwrite previously declared handler.
  set_exception_handler('fb_handle_exception');
  if (!count($cache) && !class_exists('Facebook')) {
    $filename = variable_get('fb_api_file', 'facebook-platform/php/facebook.php');
    if (!(include $filename)) {
      $message = t('Failed to find the Facebook client libraries at %filename.  Read the !readme and follow the instructions carefully.', array(
        '!drupal_for_facebook' => l(t('Drupal for Facebook'), 'http://drupal.org/project/fb'),
        // This link should work with clean URLs disabled.
        '!readme' => '<a href=' . base_path() . '/' . drupal_get_path('module', 'fb') . '/README.txt>README.txt</a>',
        '%filename' => $filename,
      ));
      drupal_set_message($message, 'error');
      watchdog('fb', $message);
      return NULL;
    }
  }

  // This global comes from facebook's client API.
  $GLOBAL['facebook_config']['debug'] = variable_get('fb_debug', FALSE);

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

  // Avoid E_NOTICE errors and keep code below clean.
  if (isset($GLOBALS['_fb'])) {
    $global_fb = $GLOBALS['_fb'];
  }
  else {
    $global_fb = (object) array(
      'api_key' => NULL,
    );
  }

  // Determine the actual facebook user id to use.
  if ($global_fb->api_key == $fb_app->apikey && ($fbu == FB_FBU_CURRENT || $fbu == FB_FBU_ANY || $fbu == $GLOBALS['_fb']
    ->get_loggedin_user())) {
    return $GLOBALS['_fb'];
  }
  elseif ($global_fb->api_key == $fb_app->apikey && $fbu == FB_FBU_CURRENT) {

    // No current user to use, probably anonymous canvas page.
    return $GLOBALS['_fb'];
  }
  elseif (is_numeric($fbu)) {

    // 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_OP_GET_USER_SESSION, array(
      'fb_app' => $fb_app,
      'fbu' => $fbu,
    ), array());
    if (count($data) && $data[0] && $data[1]) {
      $fbu = $data[0];
      $session = $data[1];
    }
  }
  if (!isset($cache[$fb_app->apikey][$fbu])) {
    try {

      // 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);

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

        // Canvas page or Connect page, current user is logged in already.
        // Nothing to do here.
      }
      elseif ($fbu == FB_FBU_NO_SESSION) {
        $fb
          ->set_user(NULL, NULL);
      }

      // 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_NO_SESSION && $fbu_orig != FB_FBU_CURRENT && !$fb
        ->get_loggedin_user()) {

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