You are here

function fb_canvas_process in Drupal for Facebook 5

Same name and namespace in other branches
  1. 5.2 fb_canvas.module \fb_canvas_process()
  2. 6.2 fb_canvas.module \fb_canvas_process()

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

Call this method from themes when producing either FBML or iframe canvas pages. This is a relatively expensive operation. Its unfortunate that we must do it on every page request. However to the best of my knowledge, Drupal provides no better way.

Parameters

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

$add_target will cause target=_top to be added when producing an: iframe.

1 call to fb_canvas_process()
fb_fbml_page in themes/fb_fbml/template.php
1 string reference to 'fb_canvas_process'
fb_fbml_page in themes/fb_fbml/template.php

File

./fb_canvas.module, line 357
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_process($output, $add_target = TRUE) {
  global $base_path, $base_url;
  global $fb, $fb_app;
  $patterns = array();
  $replacements = array();
  if ($fb) {
    $page_type = fb_settings(FB_SETTINGS_PAGE_TYPE);
    $nid = $fb_app->nid;
    $base = url();

    // short URL with rewrite applied.
    if (fb_canvas_is_fbml()) {

      //dpm($output, "before fb_canvas_process");

      // We're producing FBML for a canvas page
      // Change links to use canvas on Facebook
      // Links ending in #something:
      $patterns[] = "|=\"{$base}([^\"]*#)|";
      $replacements[] = "=\"/{$fb_app->canvas}/\$1app{$fb_app->id}_";

      // Other links
      $patterns[] = "|=\"{$base}|";
      $replacements[] = "=\"/{$fb_app->canvas}/";

      // Workaround Drupal does not let us rewrite the frontpage url

      /* Not needed thanks to: http://drupal.org/node/241878
      	  $patterns[] = "|=\"{$base_path}\"|";
      	  $replacements[] = "=\"/{$fb_app->canvas}/\"";
      	  */

      // Change paths to files to fully qualified URLs. This matches relative
      // URLs that do not include the canvas (that is, not matched by previous
      // patterns).
      $patterns[] = '|="' . $base_path . "(?!{$fb_app->canvas})|";
      $replacements[] = '="' . $base_url . '/';

      // Experimental!  Change 1234@facebook to an <fb:name> tag.  This is our
      // default user name convention when creating new users.  Ideally, this
      // would be accomplished with something like:
      // http://drupal.org/node/102679.  In the meantime, this may help for
      // canvas pages only.
      $patterns[] = '|([\\d]*)@facebook|';
      $replacements[] = '<fb:name uid=$1 linked=false ifcantsee="$1@facebook" useyou=false />';
    }
    else {

      // In iframe
      // 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 ($add_target) {

        // Add target=_top to all links
        $patterns[] = "|<a |";
        $replacements[] = "<a target=\"_top\" ";

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

        // Make internal links point to canvas pages
        $patterns[] = "|<a([^>]*)href=\"{$base}|";
        $replacements[] = "<a \$1 href=\"http://apps.facebook.com/{$fb_app->canvas}/";
      }
      else {

        // 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:";
      }

      // Workaround Drupal does not let us rewrite the frontpage url

      //$patterns[] = "|=\"{$base_path}\"|";

      //$replacements[] = "=\"{$base_path}fb_canvas/{$page_type}/{$nid}/\"";	 // XXX
    }
  }
  if (count($patterns)) {
    $return = preg_replace($patterns, $replacements, $output);
    return $return;
  }
  else {
    return $output;
  }
}