You are here

function fb_api_init in Drupal for Facebook 6.3

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

Include and initialize Facebook's PHP SDK.

20 calls to fb_api_init()
fb_admin_app_page in ./fb.admin.inc
fb_admin_get_app_info in ./fb.admin.inc
fb_admin_set_properties_form_submit in ./fb.admin.inc
Confirm form submit function. We don't use fb_app_set_app_properties, because fb_app.module may not be enabled.
fb_ajax_event in ./fb.module
Ajax callback handles an event from facebook's javascript sdk.
fb_app_event_cb in ./fb_app.module
Callback for FB_APP_PATH_EVENT.

... See full list

File

./fb.module, line 361
This is the core required module of Drupal for Facebook.

Code

function fb_api_init($fb_app) {
  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 (isset($cache[$fb_app->id])) {
    return $cache[$fb_app->id];
  }

  // Find Facebook's PHP SDK.  Use libraries API if enabled.
  $fb_lib_path = function_exists('libraries_get_path') ? libraries_get_path('facebook-php-sdk') : 'sites/all/libraries/facebook-php-sdk';
  $fb_platform = variable_get(FB_VAR_API_FILE, $fb_lib_path . '/src/facebook.php');
  try {
    if (!class_exists('Facebook') && !@(include $fb_platform)) {
      $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' => $fb_platform,
      ));
      drupal_set_message($message, 'error');
      watchdog('fb', $message);
      return NULL;
    }
    if (Facebook::VERSION < "3") {
      $message = 'This version of modules/fb is compatible with Facebook PHP SDK version 3.x.y, but %version was found (%fb_platform).';
      $args = array(
        '%fb_platform' => $fb_platform,
        '%version' => Facebook::VERSION,
      );
      if (user_access('access administration pages')) {
        drupal_set_message(t($message, $args));
      }
      watchdog('fb', $message, $args, WATCHDOG_ERROR);
      return NULL;
    }

    // Hack.  In case third-party cookies disabled, put signed request where facebook's SDK will find it.
    if (variable_get(FB_VAR_USE_SESSION, TRUE) && !isset($_REQUEST['signed_request']) && !isset($_COOKIE['fbsr_' . $fb_app->id]) && isset($_SESSION['_fb_' . $fb_app->id])) {
      $_REQUEST['signed_request'] = $_SESSION['_fb_' . $fb_app->id];
    }

    // We don't have a cached resource for this app, so we're going to create one.
    $fb = new Facebook(array(
      'appId' => $fb_app->id,
      'secret' => isset($fb_app->secret) ? $fb_app->secret : NULL,
      'cookie' => variable_get(FB_VAR_USE_COOKIE, TRUE),
    ));

    // Hack in case third-party cookies disabled, find access token in session.
    // This comes into play when oauth is not used in JS.
    if (variable_get(FB_VAR_USE_SESSION, TRUE) && !isset($_REQUEST['signed_request']) && isset($_SESSION['_fb_token_' . $fb_app->id])) {
      $fb
        ->setAccessToken($_SESSION['_fb_token_' . $fb_app->id]);
    }

    // Some servers need these settings.
    if (variable_get(FB_VAR_CURL_NOVERIFY, TRUE)) {
      Facebook::$CURL_OPTS[CURLOPT_SSL_VERIFYPEER] = FALSE;
      Facebook::$CURL_OPTS[CURLOPT_SSL_VERIFYHOST] = FALSE;

      //Facebook::$CURL_OPTS[CURLOPT_VERBOSE] = 1; // debug
    }

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

    // Tell Drupal not to store the current page in the cache when user is logged into facebook.
    if (!user_is_logged_in() && fb_facebook_user($fb)) {

      //dpm("Disabling cache for connected user.", __FUNCTION__);
      $GLOBALS['conf']['cache'] = CACHE_DISABLED;
    }
    return $fb;
  } catch (Exception $e) {
    fb_log_exception($e, t('Failed to construct Facebook client API.'));
  }
}