You are here

function fb_tab_fb in Drupal for Facebook 7.3

Same name and namespace in other branches
  1. 6.3 fb_tab.module \fb_tab_fb()

Implements hook_fb

Parameters

$op:

$data -- data payload:

$return:

File

./fb_tab.module, line 103
This module provides support for "Profile Tabs" that can be added to facebook pages (no longer allowed for user profiles).

Code

function fb_tab_fb($op, $data, &$return) {
  $fb = isset($data['fb']) ? $data['fb'] : NULL;
  $fb_app = isset($data['fb_app']) ? $data['fb_app'] : NULL;
  if ($op == FB_OP_POST_INIT) {

    // Include our admin hooks.
    if (fb_is_fb_admin_page()) {
      require drupal_get_path('module', 'fb_tab') . '/fb_tab.admin.inc';
    }
    if (fb_is_tab()) {

      // Include our javascript.
      drupal_add_js(array(
        'fb_tab' => array(
          'fbu' => fb_facebook_user(),
          'uid' => $GLOBALS['user']->uid,
          'canvas' => $fb_app->canvas,
        ),
      ), 'setting');
      drupal_add_js(drupal_get_path('module', 'fb_tab') . '/fb_tab.js');
    }
  }
  elseif ($op == FB_OP_CURRENT_APP && fb_is_tab()) {
    if ($id = fb_settings(FB_SETTINGS_CB)) {

      // Using fb_url_rewrite.
      $fb_app = fb_get_app(array(
        'id' => $id,
      ));
      if (!$fb_app) {

        // DEPRECATED.  For backward compatibility, accept apikey in FB_SETTINGS_CB
        $fb_app = fb_get_app(array(
          'apikey' => $id,
        ));
      }
    }
    elseif ($id = fb_settings(FB_SETTINGS_ID)) {

      // New SDK includes ID when session is present.
      $fb_app = fb_get_app(array(
        'id' => $id,
      ));
    }
    elseif (isset($_REQUEST['fb_sig_api_key'])) {
      $return = fb_get_app(array(
        'apikey' => $_REQUEST['fb_sig_api_key'],
      ));
    }
    if ($fb_app) {
      $return = $fb_app;
    }
  }
  elseif ($op == FB_OP_INITIALIZE) {
    if (fb_is_tab()) {
      $config = _fb_tab_get_app_config($fb_app);

      // custom_theme handling moved to fb_tab_custom_theme().
      if (fb_settings(FB_SETTINGS_TYPE) == FB_SETTINGS_TYPE_PAGE_TAB && (variable_get(FB_TAB_VAR_PROCESS_IFRAME, TRUE) || variable_get(FB_TAB_VAR_PROCESS_ABSOLUTE, TRUE))) {

        // Process iframe
        $use_ob = TRUE;
      }
      else {
        $use_ob = FALSE;
      }

      // Hack to init the theme before _drupal_maintenance_theme initializes the wrong one.
      if (variable_get('maintenance_mode', FALSE)) {
        $dummy = theme('dummy');
      }

      // Store entire page in output buffer. Will post-process on exit.
      if ($use_ob) {
        ob_start();
        $GLOBALS['fb_tab_post_process'] = TRUE;
      }

      // If path is fb_tab/view, we may actually show some other menu item.
      if (arg(0) == 'fb_tab' && arg(1) == 'view') {

        // Do not override fb_tab/config.
        $sr = $fb
          ->getSignedRequest();
        if (!empty($sr['app_data']) && !empty($config['app_data_is_path'])) {
          $path = $sr['app_data'];
        }
        elseif ($sr['page'] && $sr['page']['liked'] && $config['profile_tab_url_liked']) {
          $path = $config['profile_tab_url_liked'];
        }
        else {
          $path = $config['profile_tab_url'];
        }

        // Enable token replacement from signed request.
        $path = token_replace($path, array(), array(
          'clear' => TRUE,
        ));

        // <front> is a special path
        $path = str_replace('<front>', variable_get('site_frontpage', 'node'), $path);
        if ($path && $path != FB_TAB_PATH_VIEW) {

          // Tell Drupal what to really render.
          menu_set_active_item(drupal_get_normal_path($path));
        }
      }
    }
  }
  elseif ($op == FB_OP_EXIT && fb_is_tab()) {
    if (isset($GLOBALS['fb_tab_post_process']) && $GLOBALS['fb_tab_post_process']) {
      $output = ob_get_contents();
      ob_end_clean();
      include_once drupal_get_path('module', 'fb') . '/fb.process.inc';
      $process = variable_get(FB_TAB_VAR_PROCESS_IFRAME, FB_TAB_PROCESS_IFRAME_TO_CANVAS);
      if ($process == FB_TAB_PROCESS_IFRAME_TO_CANVAS) {
        $to_canvas = $fb_app->canvas;
      }
      else {
        $to_canvas = FALSE;
      }
      $to_page = FALSE;
      $page = NULL;
      if ($process == FB_TAB_PROCESS_IFRAME_TO_TAB_VIA_APP_DATA) {
        if ($page_id = fb_settings(FB_SETTINGS_PAGE_ID)) {
          $page = fb_graph($page_id);
          if (!empty($page['link'])) {

            // @TODO: $page['link'] may change protocol from https to http.
            $to_page = $page['link'] . "?v=app_" . fb_settings(FB_SETTINGS_ID);
          }
        }
      }
      if (fb_settings(FB_SETTINGS_TYPE) == FB_SETTINGS_TYPE_PAGE_TAB) {

        // Process iframe
        $output = fb_process($output, array(
          'add_target' => '_top',
          'absolute_links' => variable_get(FB_TAB_VAR_PROCESS_ABSOLUTE, TRUE),
          'relative_links' => variable_get(FB_TAB_VAR_PROCESS_IFRAME, TRUE),
          'to_canvas' => $to_canvas,
          'to_page' => $to_page,
        ));
      }
      elseif (fb_settings(FB_SETTINGS_TYPE) == FB_SETTINGS_TYPE_PROFILE) {

        // Process FBML (deprecated)
        $output = fb_process($output, array(
          'add_target' => FALSE,
          'absolute_links' => TRUE,
          'to_canvas' => $to_canvas,
        ));
      }
      if (isset($output)) {
        print $output;
      }
    }
    $destination = $return;
    if ($destination) {

      // A normal redirect will affect only our iframe.
      // We must instead send the user to our page on facebook.com.
      if ($process == FB_TAB_PROCESS_IFRAME_TO_TAB_VIA_APP_DATA) {
        if (!empty($to_page)) {
          $url = fb_iframe_fix_url($destination, $to_page . '&app_data=');

          // It's not ideal to call this here, as doing so prevents other
          // hook_exits from running.  However there's no later
          // opportunity to change where Drupal would send us.
          fb_iframe_redirect($url);
        }
      }
    }
  }
}