$apikey));
}
else {
if (function_exists('fb_settings')) {
// See fb_settings.inc
if ($nid = fb_settings(FB_SETTINGS_APP_NID)) {
// Here if we're in iframe, using our /fb_canvas/nid/ path convention.
$fb_app = fb_get_app(array('nid' => $nid));
}
}
}
if ($fb_app)
$return = $fb_app;
}
else if ($op == FB_OP_INITIALIZE) {
// Get our configuration settings.
$fb_app_data = fb_app_get_data($fb_app);
$fb_canvas_data = $fb_app_data['fb_canvas'];
$is_canvas = FALSE;
// Set an app-specific theme.
global $custom_theme; // Set by this function.
if (fb_canvas_is_fbml()) {
$custom_theme = $fb_canvas_data['theme_fbml'];
$is_canvas = TRUE;
}
else if (fb_canvas_is_iframe()) {
$custom_theme = $fb_canvas_data['theme_iframe'];
$is_canvas = TRUE;
}
if ($is_canvas) {
// We are serving a canvas page.
if ($fb_canvas_data['require_login'] == FB_CANVAS_OPTION_REQUIRE_LOGIN) {
// The application is configured to require login on all canvas pages.
// However, there are exceptions.
if (fb_is_profile_tab()) {
// Redirects are not allowed for the profile tab.
}
else {
// There may be other exceptions, for example some ajax callbacks. Potential todo item.
$fb->require_login();
}
}
// Remember the user id. If fb_user.module changes it, we'll need to refresh the page. See FB_OP_POST_INIT.
$original_uid = $user->uid;
// Hack to init the theme before _drupal_maintenance_theme initializes the wrong one.
if (variable_get('site_offline', FALSE)) {
$dummy = theme('dummy');
}
}
// Special handling for forms, as they are submitted directly to us, not
// to apps.facebook.com/canvas
// we will buffer, and later cache, the results.
if (fb_canvas_handling_form())
ob_start();
if ($is_canvas && $_GET['q'] == drupal_get_normal_path(variable_get('site_frontpage', 'node'))) {
if ($fb->get_loggedin_user()) {
if ($fb->api_client->users_isAppUser())
$front = $fb_canvas_data['front_added'];
else
$front = $fb_canvas_data['front_loggedin'];
}
else
$front = $fb_canvas_data['front_anonymous'];
if ($front)
menu_set_active_item(drupal_get_normal_path($front));
}
}
else if ($op == FB_OP_POST_INIT) {
if (isset($original_uid) &&
$original_uid != $user->uid) {
// The user has changed, presumably fb_user.module recognized the facebookuser. We need to refresh canvas pages.
if(!(arg(0) == 'fb_app' && arg(1) == 'event')) {
// In order to ensure that drupal handles
// permissions properly, the user must make the request all over
// again. Skip this for the profile tab, as facebook does not allow
// redirects (or persistent session) there.
if ((fb_is_fbml_canvas() || fb_is_iframe_canvas()) &&
!fb_is_profile_tab()) {
$url = fb_canvas_fix_url(url(fb_scrub_urls($_REQUEST['q']), array('absolute' => TRUE)), $fb_app);
if (fb_verbose())
watchdog('fb_canvas', "User uid is now {$user->uid} (was {$original_uid}), redirecting canvas to $url to ensure permissions are correct."); // debug
$fb->redirect($url);
}
}
}
// The ?destination=... url param means something to drupal but also to facebook. If ?fb_canvas_destination=... is set, we honor that.
if (isset($_REQUEST['fb_canvas_destination'])) {
$_REQUEST['destination'] = $_REQUEST['fb_canvas_destination'];
}
}
else if ($op == FB_OP_EXIT) {
$destination = $return;
if (fb_canvas_is_fbml()) {
$output = ob_get_contents();
ob_end_clean();
if ($destination) {
// Fully qualified URLs need to be modified to point to facebook app.
// URLs are fully qualified when a form submit handler returns a path,
// or any call to drupal_goto.
$app_destination = fb_canvas_fix_url($destination, $fb_app);
// If here, drupal_goto has been called, but it may not work within a
// canvas page, so we'll use Facebook's method.
// Will this preempt other hook_exits???
if (fb_verbose()) {
watchdog('fb_debug', "FB_OP_EXIT on canvas page redirecting to $app_destination (original destination was $destination).");
$fb->redirect($app_destination);
}
}
else if (fb_canvas_handling_form()) {
// Save the results to show the user later
$token = uniqid('fb_');
$cid = session_id() . "_$token";
watchdog('fb', "Storing cached form page $cid, then redirecting");
cache_set($cid, $output, 'cache_page', time() + (60 * 5), drupal_get_headers()); // (60 * 5) == 5 minutes
$dest = 'http://apps.facebook.com/' . $fb_app->canvas . "/fb/form_cache/$cid";
// $fb->redirect($url); // Does not work!
// Preserve some URL parameters
$query = array();
foreach (array('fb_force_mode') as $key) {
if ($_REQUEST[$key])
$query[] = $key . '=' . $_REQUEST[$key];
}
//drupal_goto honors $_REQUEST['destination'], but we only want that when no errors occurred
if (form_get_errors()) {
unset($_REQUEST['destination']);
if ($_REQUEST['edit'])
unset($_REQUEST['edit']['destination']);
}
drupal_goto($dest, implode('&', $query), NULL, 303); // appears to work
}
}
}
else if ($op == FB_OP_SET_PROPERTIES) {
// Compute properties which we can set automatically.
$callback_url = url('',array('absolute' => TRUE)) . FB_SETTINGS_APP_NID . '/' . $fb_app->nid . '/';
$return['callback_url'] = $callback_url;
}
else if ($op == FB_OP_LIST_PROPERTIES) {
$return[t('Callback URL')] = 'callback_url';
$return[t('Canvas Page Suffix')] = 'canvas_name';
}
}
/**
* Helper returns configuration for this module, on a per-app basis.
*/
function _fb_canvas_get_config($fb_app) {
$fb_app_data = fb_app_get_data($fb_app);
$fb_canvas_data = $fb_app_data['fb_canvas'] ? $fb_app_data['fb_canvas'] : array();
// Merge in defaults
$fb_canvas_data += array(
'require_login' => FB_CANVAS_OPTION_ALLOW_ANON,
'theme_fbml' => 'fb_fbml',
'theme_iframe' => 'fb_fbml',
);
return $fb_canvas_data;
}
/**
* Implementation of hook_form_alter.
*/
function fb_canvas_form_alter(&$form, &$form_state, $form_id) {
// Add our settings to the fb_app edit form.
if (isset($form['fb_app_data']) && is_array($form['fb_app_data'])) {
$node = $form['#node'];
$fb_canvas_data = _fb_canvas_get_config($node->fb_app);
$form['fb_app_data']['fb_canvas'] = array(
'#type' => 'fieldset',
'#collapsible' => TRUE,
'#collapsed' => TRUE,
'#title' => t('Facebook canvas pages'),
'#description' => t('Settings which apply to canvas pages.',
array('!url' => 'http://developers.facebook.com/get_started.php?tab=anatomy#canvas')),
);
$form['fb_app_data']['fb_canvas']['require_login'] = array(
'#type' => 'radios',
'#title' => t('Require authorization'),
'#description' => t('Require authorization if you want Drupal for Facebook to call require_login() on every canvas page.'),
'#options' => array(
FB_CANVAS_OPTION_ALLOW_ANON => t('Allow anonymous visitors'),
FB_CANVAS_OPTION_REQUIRE_LOGIN => t('Require all users to authorize the application'),
),
'#default_value' => $fb_canvas_data['require_login'],
'#required' => TRUE,
);
$form['fb_app_data']['fb_canvas']['front_anonymous'] =
array('#type' => 'textfield',
'#title' => t('Front page when user is not logged in to facebook'),
'#description' => t('Leave blank to use the site-wide front page. See Public Canvas Pages.',
array('!link' => 'http://wiki.developers.facebook.com/index.php/Public_Canvas_Pages')),
'#default_value' => $fb_canvas_data['front_anonymous'],
);
$form['fb_app_data']['fb_canvas']['front_loggedin'] =
array('#type' => 'textfield',
'#title' => t('Front page when user is logged in to facebook, but is not a user of the app'),
'#description' => t('Leave blank to use the site-wide front page.'),
'#default_value' => $fb_canvas_data['front_loggedin'],
);
$form['fb_app_data']['fb_canvas']['front_added'] =
array('#type' => 'textfield',
'#title' => t('Front page for authorized users of this application'),
'#description' => t('Leave blank to use the site-wide front page.'),
'#default_value' => $fb_canvas_data['front_added'],
);
/* XXX menu code here needs updating to D6
// Allow primary links to be different on facebook versus the rest of the
// site. Code from menu_configure() in menu.module.
$primary = variable_get('menu_primary_links_source', 'primary-links');
$primary_options = array_merge($menu_options, array('' => t('