function fb_init in Drupal for Facebook 7.3
Same name and namespace in other branches
- 5.2 fb.module \fb_init()
- 5 fb.module \fb_init()
- 6.3 fb.module \fb_init()
- 6.2 fb.module \fb_init()
- 7.4 fb.module \fb_init()
Implements hook_init().
Initializes facebook's javascript. 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). We invoke the hook again to let interested modules know the sdk is initialized.
File
- ./
fb.module, line 188 - This is the core required module of Drupal for Facebook.
Code
function fb_init() {
// Globals provided for internal use and convenience to third-parties.
global $_fb;
global $_fb_app;
// When the site is in maintenance mode, hook_custom_theme won't be invoked so
// we call our implementation manually.
if (!function_exists('fb_settings')) {
fb_custom_theme();
}
// Javascript settings needed by fb.js.
fb_js_settings('base_url', trim(url('', array(
'absolute' => TRUE,
)), '/'));
fb_js_settings('ajax_event_url', url(FB_PATH_AJAX_EVENT, array(
'absolute' => TRUE,
)));
fb_js_settings('is_anonymous', !user_is_logged_in());
// Data structure to pass to FB.init();
$fb_init_settings = array(
'xfbml' => FALSE,
'status' => FALSE,
// We will call getLoginStatus() instead.
'oauth' => variable_get(FB_VAR_JS_OAUTH, TRUE),
);
if (variable_get(FB_VAR_USE_COOKIE, TRUE)) {
$fb_init_settings['cookie'] = TRUE;
}
if ($_fb_app) {
// An App is configured.
// Javascript settings needed by fb.js and/or other modules.
fb_js_settings('label', $_fb_app->label);
fb_js_settings('namespace', $_fb_app->canvas);
fb_js_settings('page_type', fb_settings(FB_SETTINGS_TYPE));
// canvas or connect.
// Add perms to settings, for calling FB.login().
$perms = array();
drupal_alter('fb_required_perms', $perms);
fb_js_settings('perms', implode(',', $perms));
//$fb_init_settings['apiKey'] = $_fb_app->apikey;
$fb_init_settings['appId'] = $_fb_app->id;
if ($_fb) {
// @TODO: test if this is still true: Sometimes when canvas page is open in one tab, and user logs out of
// facebook in another, the canvas page has bogus session info when
// refreshed. Here we attempt to detect and cleanup.
// Give other modules a chance to initialize.
fb_invoke(FB_OP_INITIALIZE, array(
'fb_app' => $_fb_app,
'fb' => $_fb,
));
// See if the facebook user id is known
if ($fbu = $_fb
->getUser()) {
fb_invoke(FB_OP_APP_IS_AUTHORIZED, array(
'fb_app' => $_fb_app,
'fb' => $_fb,
'fbu' => $fbu,
));
fb_js_settings('fbu', $fbu);
}
if (!empty($_REQUEST['fb_reload'])) {
// Tell javascript not to reload indefinately.
fb_js_settings('fb_reloading', $_REQUEST['fb_reload']);
}
// When not using cookies, or third-party cookies disabled, we can pass all the auth details in javascript settings.
if (variable_get(FB_VAR_JS_USE_SESSION, TRUE)) {
if (!empty($_REQUEST['fb_login_status'])) {
// Remember for duration of session whether test failed.
$_SESSION['fb_get_login_status_test'] = $_REQUEST['fb_login_status'];
}
if ($fb_uid = $_fb
->getUser()) {
if (!empty($_SESSION['fb_get_login_status_test']) && $_SESSION['fb_get_login_status_test'] == 'false') {
$fb_token = $_fb
->getAccessToken();
// This uses a data structure not documented by facebook. May not continue to work.
$js_sr = array(
'accessToken' => $fb_token,
'userID' => $fb_uid,
);
$fb_init_settings['authResponse'] = (object) $js_sr;
}
}
else {
fb_js_settings('get_login_status_test', TRUE);
}
}
}
else {
unset($_fb_app);
watchdog('fb', "Could not initialize Facebook API.", array(), WATCHDOG_ERROR);
}
if (isset($_REQUEST['destination'])) {
$destination = $_REQUEST['destination'];
}
elseif (current_path()) {
$destination = current_path();
}
else {
$destination = '<front>';
}
if (fb_is_canvas()) {
$destination = fb_scrub_urls($destination);
// Needed?
}
//Stripping the fragment out to be tacked on during the javascript redirect
if (strpos($destination, '#') !== FALSE) {
list($destination, $fragment) = explode('#', $destination, 2);
fb_js_settings('reload_url_fragment', $fragment);
}
fb_js_settings('reload_url', url($destination, array(
'absolute' => TRUE,
'fb_canvas' => fb_is_canvas(),
'language' => (object) array(
'prefix' => NULL,
'language' => NULL,
),
)));
}
if ($channel = variable_get(FB_VAR_JS_CHANNEL, TRUE)) {
if (!is_string($channel)) {
$channel = url('fb/channel', array(
'absolute' => TRUE,
'fb_url_alter' => FALSE,
));
}
$fb_init_settings['channelUrl'] = $channel;
}
fb_js_settings('fb_init_settings', $fb_init_settings);
// Allow third-parties to act, even if we did not initialize $_fb.
fb_invoke(FB_OP_POST_INIT, array(
'fb_app' => $_fb_app,
'fb' => $_fb,
));
fb_js_settings('test_login_status', variable_get(FB_VAR_JS_TEST_LOGIN_STATUS, TRUE));
fb_js_settings('get_login_status', variable_get(FB_VAR_JS_GET_LOGIN_STATUS, TRUE));
fb_js_settings('controls', implode(',', fb_controls()));
if (!fb_js_settings('js_sdk_url')) {
if (isset($_SESSION['fb_locale']) && variable_get(FB_VAR_LANGUAGE_OVERRIDE, 'override')) {
// @TODO - get locale from signed request. It appears to contain it now.
$fb_lang = $_SESSION['fb_locale'];
}
else {
global $language;
$fb_lang = variable_get('fb_language_' . $language->language, 'en_US');
}
$js_sdk = fb_protocol() . "://connect.facebook.net/{$fb_lang}/all.js";
fb_js_settings('js_sdk_url', variable_get(FB_VAR_JS_SDK, $js_sdk));
}
// Add our module's javascript.
drupal_add_js(drupal_get_path('module', 'fb') . '/fb.js', array(
'type' => 'file',
'scope' => 'header',
'group' => JS_LIBRARY,
));
// See also fb_page_alter(), where we initialize facebook's SDK.
}