You are here

function fb_canvas_fb in Drupal for Facebook 7.3

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

Implements hook_fb().

File

./fb_canvas.module, line 44
This module provides support for Canvas page applications. Use Drupal to power traditional Facebook Apps.

Code

function fb_canvas_fb($op, $data, &$return) {
  $original_uid =& drupal_static(__FUNCTION__);
  global $user;
  $fb = isset($data['fb']) ? $data['fb'] : NULL;
  $fb_app = isset($data['fb_app']) ? $data['fb_app'] : NULL;
  if ($op == FB_OP_CURRENT_APP) {
    if (function_exists('fb_settings')) {
      if (fb_settings(FB_SETTINGS_TYPE) == FB_SETTINGS_TYPE_CANVAS) {

        // fb_settings.inc has determined this is a canvas page.
        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' => $app_key,
            ));
          }
          if (!$fb_app) {

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

          // New SDK includes ID when session is present.
          $fb_app = fb_get_app(array(
            'id' => $id,
          ));
        }
        elseif ($apikey = fb_settings(FB_SETTINGS_APIKEY)) {

          // Old SDK tells us APIKEY.  Deprecated.
          $fb_app = fb_get_app(array(
            'apikey' => $apikey,
          ));
        }
      }
    }
    if ($fb_app) {
      $return = $fb_app;
    }
  }
  elseif ($op == FB_OP_INITIALIZE) {

    // Get our configuration settings.
    $fb_canvas_data = _fb_canvas_get_config($fb_app);
    $is_canvas = FALSE;
    $use_ob = FALSE;
    if (fb_canvas_is_iframe()) {

      // Use buffer to process iframe markup.
      $is_canvas = TRUE;
      $use_ob = variable_get(FB_CANVAS_VAR_PROCESS_IFRAME, TRUE);
    }

    // Store entire page in output buffer.  Will post-process on exit.
    if ($use_ob) {
      ob_start();
      $GLOBALS['fb_canvas_post_process'] = TRUE;
    }
    if ($is_canvas && current_path() == variable_get('site_frontpage', 'node')) {
      if ($fb
        ->getUser()) {
        $front = $fb_canvas_data['front_added'];
      }
      else {
        $front = $fb_canvas_data['front_anonymous'];
      }
      if ($front) {
        menu_set_active_item(drupal_get_normal_path($front));
      }
    }
  }
  elseif ($op == FB_OP_POST_INIT) {
    if (fb_canvas_is_iframe()) {

      // The ?destination=... url param means something to drupal but something
      // else to facebook.  If ?fb_canvas_destination=... is set, we honor that.
      if (isset($_REQUEST['fb_canvas_destination'])) {
        $_REQUEST['destination'] = $_REQUEST['fb_canvas_destination'];
      }

      // Include our javascript.
      drupal_add_js(array(
        'fb_canvas' => array(
          'fbu' => fb_facebook_user(),
          'uid' => $GLOBALS['user']->uid,
          'canvas' => $fb_app->canvas,
        ),
      ), 'setting');
      drupal_add_js(drupal_get_path('module', 'fb_canvas') . '/fb_canvas.js');
    }

    // Include our admin hooks.
    if (fb_is_fb_admin_page()) {
      require drupal_get_path('module', 'fb_canvas') . '/fb_canvas.admin.inc';
    }
  }
  elseif ($op == FB_OP_EXIT) {

    /* We do some unpleasant stuff in this hook... on FBML canvas
           pages we might use $fb->redirect(), in which case other
           modules' hook_exit() might not be called.

           In other cases we call drupal_goto(), in which case other
           modules' hook_exit() might be called twice.  I hate to do this
           but so far have not figured another way.  And so far no
           problems... if problems arise, please post to issue queue.
        */
    $destination = $return;
    if (isset($GLOBALS['fb_canvas_post_process']) && $GLOBALS['fb_canvas_post_process']) {
      $output = ob_get_contents();
      ob_end_clean();
      if (fb_canvas_is_iframe()) {
        include_once drupal_get_path('module', 'fb') . '/fb.process.inc';
        $output = fb_process($output, array(
          'add_target' => '_top',
          'absolute_links' => variable_get(FB_CANVAS_VAR_PROCESS_ABSOLUTE, TRUE),
          'to_canvas' => $fb_app->canvas,
        ));
      }
    }
    if (fb_canvas_is_iframe() && !isset($GLOBALS['_fb_canvas_goto'])) {
      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.
        // Unfortunately, other modules' hook_exit() may not be called.
        if (fb_verbose()) {
          watchdog('fb_debug', "FB_OP_EXIT on canvas page redirecting to {$app_destination} (original destination was {$destination}).");
        }
        fb_iframe_redirect($app_destination);
      }
    }
    if (isset($output)) {
      print $output;
    }
  }
}