You are here

function fb_init in Drupal for Facebook 5.2

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

Implementation of hook_init

Determines whether we are servicing a Facebook App request.

We invoke our hook, first to determine which application is being invoked. (Because we support more than one in the same Drupal instance.) Then, we notify interested modules in various events.

File

./fb.module, line 50

Code

function fb_init() {
  global $fb, $fb_app;

  // Set by this function.
  // http://drupal.org/node/329810
  if (!function_exists('arg')) {

    // Ensure arg function is defined.
    drupal_bootstrap(DRUPAL_BOOTSTRAP_PATH);
  }
  if (arg(0) != 'admin') {

    // Session sanity check.  This is relevant on iframe pages when user once
    // had the app added, but has since removed it.  This is a bit of a hack
    // because we rely on the fb_... params passed by facebook.
    if (isset($_SESSION['fb_frame_params']) && $_REQUEST['fb_sig']) {
      if ($_REQUEST['fb_sig_session_key'] != $_SESSION['fb_frame_params']['session_key']) {

        // The session key from facebook has changed.  Treat this as a new session.
        $req = array();
        foreach ($_REQUEST as $key => $val) {
          if (strpos($key, 'fb_') === 0) {
            $req[] = $key . '=' . $val;
          }
        }
        $url = url($_REQUEST['q'], implode('&', $req), NULL, TRUE);
        if (fb_verbose()) {
          watchdog('fb_debug', t('Facebook session key was %old, now %new.  Destroying session and sending user to %url.', array(
            '%old' => $_SESSION['fb_frame_params']['session_key'],
            '%new' => $_REQUEST['fb_sig_session_key'],
            '%url' => $url,
          )));
        }
        session_destroy();
        drupal_goto($url);
      }
    }
  }

  // Purge out-of-date session info
  if (isset($_SESSION['fb_frame_params']) && $_SESSION['fb_frame_params']['expires'] && $_SESSION['fb_frame_params']['expires'] < time()) {
    unset($_SESSION['fb_frame_params']);
  }

  // Perform sanity check, help users who skip the README.
  if (!variable_get('fb_settings_check', FALSE) && user_access('access administration pages')) {
    drupal_set_message(t('!drupal_for_facebook has been enabled, but not properly installed.  Please read the !readme.', 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>',
    )), 'error');
  }

  // Ask other modules for app detail
  $fb_app = fb_invoke(FB_OP_CURRENT_APP);
  if ($fb_app) {

    // we are in a callback
    // For canvas pages, use current user, never infinite session.
    $fb = fb_api_init($fb_app, FB_FBU_CURRENT);
    if ($fb) {

      // Give other modules a chance to initialize, require login, etc...
      fb_invoke(FB_OP_INITIALIZE, array(
        'fb_app' => $fb_app,
        'fb' => $fb,
      ));

      // See if the facebook user id is known
      if ($fbu = $fb
        ->get_loggedin_user()) {
        fb_invoke(FB_OP_APP_IS_AUTHORIZED, array(
          'fb_app' => $fb_app,
          'fb' => $fb,
          'fbu' => $fbu,
        ));
      }
    }
    else {
      watchdog('fb', "URL indicates a facebook app, but could not initialize Facebook", WATCHDOG_ERROR);
    }
  }
  fb_invoke(FB_OP_POST_INIT, array(
    'fb_app' => $fb_app,
    'fb' => $fb,
  ));
}