You are here

function fb_init in Drupal for Facebook 5

Same name and namespace in other branches
  1. 5.2 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 42

Code

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

  // Set by this function.
  // 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 ($_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);
    }
  }

  // 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(NULL, FB_OP_GET_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) {
      if ($fb
        ->in_fb_canvas() && FALSE) {

        // XXX deprecated
        fb_init_path($fb_app);
      }

      // Give other modules a chance to initialize, require login, etc...
      _fb_invoke($fb_app, FB_OP_INITIALIZE);
    }
    else {
      watchdog('fb', "URL indicates a facebook app, but could not initialize Facebook");
    }
  }
  _fb_invoke($fb_app, FB_OP_POST_INIT);
}