You are here

function fb_settings_url_rewrite in Drupal for Facebook 5

Same name and namespace in other branches
  1. 5.2 fb_settings.inc \fb_settings_url_rewrite()

Rewrite URLs for facebook canvas pages.

Ideally, this function would live in fb_canvas.module and only be set when serving canvas pages. However, it gets called before modules are loaded. So it must live here.

1 call to fb_settings_url_rewrite()
fb_settings.inc in ./fb_settings.inc

File

./fb_settings.inc, line 69

Code

function fb_settings_url_rewrite($type, $path, $original) {

  //dpm(func_get_args(), 'fb_canvas_url_rewrite');
  $prefixes = array(
    FB_SETTINGS_APP_NID,
    FB_SETTINGS_PAGE_TYPE,
  );
  if ($type == 'source') {

    // See if this is a request for us.
    if (strpos($path, FB_SETTINGS_APP_NID . '/') === 0) {

      // Too soon for arg() function.
      $args = explode('/', $path);
      while (count($args) && in_array($args[0], $prefixes)) {
        $key = array_shift($args);
        $value = array_shift($args);
        fb_settings($key, $value);

        // Store for use later.
      }
      if ($app_nid = fb_settings(FB_SETTINGS_APP_NID)) {
        if (count($args)) {
          $path = implode('/', $args);

          // remaining args
          $path = drupal_get_normal_path($path);
        }
        else {

          // frontpage
          $path = drupal_get_normal_path(variable_get('site_frontpage', 'node'));
        }
      }
    }
  }
  else {
    if ($type == 'alias') {
      if (function_exists('fb_canvas_is_fbml')) {
        $pre = '';
        if (fb_canvas_is_iframe()) {

          // Rewrite for iframes
          foreach ($prefixes as $prefix) {
            if ($value = fb_settings($prefix)) {
              $pre .= $prefix . '/' . fb_settings($prefix) . '/';
            }
          }
          $path = $pre . $path;
        }
        else {
          if (fb_canvas_is_fbml()) {

            // Rewrite for FBML

            //$path = $fb_app->canvas . '/' . $path; // old way

            // For now, same as for iframes, but may be different one day
            foreach ($prefixes as $prefix) {
              if ($value = fb_settings($prefix)) {
                $pre .= $prefix . '/' . fb_settings($prefix) . '/';
              }
            }
            $path = $pre . $path;
          }
        }
      }
    }
  }
  return $path;
}