You are here

function fb_canvas_fb in Drupal for Facebook 5

Same name and namespace in other branches
  1. 5.2 fb_canvas.module \fb_canvas_fb()
  2. 6.3 fb_canvas.module \fb_canvas_fb()
  3. 6.2 fb_canvas.module \fb_canvas_fb()
  4. 7.3 fb_canvas.module \fb_canvas_fb()

Implementation of hook_fb.

File

./fb_canvas.module, line 13
This module provides some app-specific navigation to facebook apps. This is fairly experimental material. May change a lot in near future.

Code

function fb_canvas_fb($fb, $fb_app, $op, &$return) {
  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'];

    // Set an app-specific theme.
    global $custom_theme;

    // Set by this function.
    if (fb_canvas_is_fbml()) {
      $custom_theme = $fb_canvas_data['theme_fbml'];
    }
    else {
      if (fb_canvas_is_iframe()) {
        $custom_theme = $fb_canvas_data['theme_iframe'];
      }
    }

    // This is for backward compatability (delete eventually)
    if (!$custom_theme) {
      $custom_theme = variable_get('fb_theme', 'fb_fbml');
    }

    // 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();
    }

    //drupal_set_message("fb_canvas_fb" . dpr($_GET, 1) . 'arg(0) = ' . dpr(arg(0), 1) . 'arg(1) = ' . dpr(arg(1), 1) . 'frontpage ' . dpr(variable_get('site_frontpage', 'node'), 1));
    if ($_GET['q'] == drupal_get_normal_path(variable_get('site_frontpage', 'node'))) {
      if ($fb->api_client
        ->users_isAppAdded()) {
        $front = $fb_canvas_data['front_added'];
      }
      else {
        if ($fb
          ->get_loggedin_user()) {
          $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
        }
      }
    }
  }
}