You are here

function fb_init in Drupal for Facebook 6.2

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

Code

function fb_init() {

  // Globals provided for internal use and convenience to third-parties.
  global $_fb;
  global $_fb_app;

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

    // Ensure arg function is defined.
    drupal_bootstrap(DRUPAL_BOOTSTRAP_PATH);
  }

  // Perform sanity check, help users who skip the README.
  if (!function_exists('fb_settings')) {

    // Avoid fatal errors caused by settings not included.
    module_load_include('inc', 'fb', 'fb_settings');
    if (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 details.
  $_fb_app = fb_invoke(FB_OP_CURRENT_APP);

  // DEPRECATED global $fb_app has been renamed to $_fb_app.
  $GLOBALS['fb_app'] = $_fb_app;

  // For compatibility with old third-party modules.
  if ($_fb_app) {

    // we are in a callback
    // For canvas and connect pages, use current user.
    $_fb = fb_api_init($_fb_app, FB_FBU_CURRENT);

    // DEPRECATED global $fb has been renamed to $_fb.
    $GLOBALS['fb'] = $_fb;

    // For compatibility with old third-party modul
    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", array(), WATCHDOG_ERROR);
    }
  }
  fb_invoke(FB_OP_POST_INIT, array(
    'fb_app' => $_fb_app,
    'fb' => $_fb,
  ));
}