$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;
}
// 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_EXIT) {
$destination = $return;
if (fb_canvas_handling_form() && $fb_app) {
$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.
$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) {
$fb->redirect($destination);
}
}
else {
// 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, 'cache_page', $output, 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('', NULL, NULL, 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';
}
}
/**
* Implementation of hook_form_alter.
*/
function fb_canvas_form_alter($form_id, &$form) {
// Add our settings to the fb_app edit form.
if (is_array($form['fb_app_data'])) {
$node = $form['#node'];
$fb_app_data = fb_app_get_data($node->fb_app);
$fb_canvas_data = $fb_app_data['fb_canvas'];
// defaults
if (!$fb_canvas_data)
$fb_canvas_data = array();
$fb_canvas_data = array_merge(array('theme_fbml' => 'fb_fbml',
'theme_iframe' => 0),
$fb_canvas_data);
$form['fb_app_data']['fb_canvas'] = array('#type' => 'fieldset',
'#collapsible' => TRUE,
'#collapsed' => TRUE,
'#title' => t('Facebook canvas page settings'),
'#description' => t('Allows application-specific front page and navigation links.')
);
$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 users of this application'),
'#description' => t('Leave blank to use the site-wide front page.'),
'#default_value' => $fb_canvas_data['front_added'],
);
// Allow primary links to be different on facebook versus the rest of the
// site. Code from menu_configure() in menu.module.
$root_menus = menu_get_root_menus();
$primary_options = $root_menus;
$primary_options[0] = t('