You are here

function fb_canvas_fb in Drupal for Facebook 5.2

Same name and namespace in other branches
  1. 5 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 12
This module provides some app-specific navigation to facebook apps.

Code

function fb_canvas_fb($op, $data, &$return) {
  $fb = $data['fb'];
  $fb_app = $data['fb_app'];

  //watchdog('XXX', "fb_canvas_fb($op) data is " . dprint_r($data, 1));
  if ($op == FB_OP_CURRENT_APP) {
    if ($apikey = $_REQUEST[FB_APP_REQ_API_KEY]) {

      // If facebook has passed the app key, let's use that.
      $fb_app = fb_get_app(array(
        'apikey' => $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';
          }
        }
      }
    }
  }
}