You are here

function fb_process in Drupal for Facebook 7.4

Same name and namespace in other branches
  1. 6.3 fb.process.inc \fb_process()
  2. 7.3 fb.process.inc \fb_process()

This function uses regular expressions to convert links on canvas pages to URLs that begin http://apps.facebook.com/...

This is a relatively expensive operation. In the past, this had to be run on all FBML canvas pages. Now with iframe canvas pages, this is not strictly needed, but remains useful. If processed, links on the page will change the parent frame (the one with the URL shown in the browser). If not processed, the links will change only the iframe.

Parameters

$output is the page (or iframe block) about to be returned.:

$options - 'add_target' will cause target=_top to be added: when producing an iframe. 'absolute_links' will change hrefs with absolute URLs to refer to canvas pages.

1 call to fb_process()
fb_canvas_post_render_page in ./fb_canvas.module

File

./fb.process.inc, line 31
Helper functions for processing links.

Code

function fb_process($output, $options = array()) {
  global $base_url;

  // Defaults
  $options += array(
    'relative_links' => TRUE,
    'to_canvas' => FALSE,
    'to_page' => FALSE,
    'add_target' => '_top',
  );
  $to_canvas = $options['to_canvas'];
  $to_page = $options['to_page'];
  $patterns = array();
  $replacements = array();
  $base_path = base_path();
  $targets_added = FALSE;
  if (function_exists('fb_url_outbound_alter')) {
    $rewrite_options = array();
    $base_rewrite = '';
    fb_url_outbound_alter($base_rewrite, $rewrite_options, '');
    $base = $base_path . $base_rewrite;

    // short URL with rewrite applied.
  }
  else {

    // If no url_alter, use normal base_path.
    $base = $base_path;
  }
  if ($options['relative_links']) {
    if ($to_canvas) {

      // Make relative links point to canvas pages.
      $patterns[] = "|<a([^>]*)href=\"{$base}|";
      $replacements[] = "<a \$1 href=\"//apps.facebook.com/{$to_canvas}/";
    }
    elseif ($to_page) {

      // Make relative links point to page tab.
      // Note that url parameters will be lost on page tabs.  Facebook will not pass them on to our iframe.
      $patterns[] = "|<a([^>]*)href=\"{$base}([^\"\\?]*)\\?|";
      $replacements[] = "<a \$1 href=\"{$to_page}" . "&app_data=\$2&";

      // Pattern without URL params.
      $patterns[] = "|<a([^>]*)href=\"{$base}|";
      $replacements[] = "<a \$1 href=\"{$to_page}" . "&app_data=";
    }
    else {

      // Make relative links point to website.
      $site_base = url('<front>', array(
        'absolute' => TRUE,
        'fb_url_alter' => FALSE,
      ));
      $patterns[] = "|<a([^>]*)href=\"{$base}|";
      $replacements[] = "<a \$1 href=\"{$site_base}";
    }

    // Add target=_top so that entire pages do not appear within an iframe.
    // TODO: make these pattern replacements more sophisticated, detect whether target is already set.
    if (isset($options['add_target']) && ($target = $options['add_target'])) {

      // Add target=_top to all links
      $patterns[] = "|<a ([^>]*)href=\"|";
      $replacements[] = "<a \$1 target=\"{$target}\" href=\"";

      // Do not change local forms, but do change external ones
      $patterns[] = "|<form([^>]*)action=\"([^:\"]*):|";
      $replacements[] = "<form target=\"{$target}\" \$1 action=\"\$2:";
      $targets_added = TRUE;
    }
  }
  if (!$targets_added) {

    // Add target=_top to only external links
    $patterns[] = "|<a([^>]*)href=\"([^:\"]*):|";
    $replacements[] = "<a target=\"_top\" \$1 href=\"\$2:";
    $patterns[] = "|<form([^>]*)action=\"([^:\"]*):|";
    $replacements[] = "<form target=\"_top\" \$1 action=\"\$2:";
  }
  if (isset($options['absolute_links']) && $options['absolute_links']) {

    // Make absolute links point to canvas pages.
    $absolute_base = url('<front>', array(
      'absolute' => TRUE,
    ));
    $patterns[] = "|<a([^>]*)href=\"{$absolute_base}|";
    $replacements[] = "<a \$1 href=\"//apps.facebook.com/{$to_canvas}/";
  }
  if (count($patterns)) {
    $count = 0;
    $return = preg_replace($patterns, $replacements, $output, -1, $count);

    //print ("fb_process replaced $count.\n\n"); // debug
    return $return;
  }
  else {
    return $output;
  }
}