You are here

function boost_redirect_handler in Boost 6

Grabs drupal_goto requests via boost_exit and looks for redirects.

Looks at the current page and the destination, seeing if the internal name is the same; node/8 == node/8.

Parameters

$destination: URL that user will be sent to soon.

1 call to boost_redirect_handler()
boost_exit in ./boost.module
Implementation of hook_exit(). Performs cleanup tasks.

File

./boost.module, line 613
Provides static file caching for Drupal text output. Pages, Feeds, ect...

Code

function boost_redirect_handler($destination) {
  global $base_path, $base_root;
  if (empty($destination)) {
    return;
  }
  $source = $base_root . request_uri();

  // Parse the URLs
  $new_parts = parse_url($destination);
  $current_parts = parse_url($source);

  // Get paths
  $current_path = ltrim($current_parts['path'], $base_path);
  $current_path_system = $_GET['q'];
  $new_path = ltrim($new_parts['path'], $base_path);
  $new_path_system = drupal_get_normal_path($new_path);
  if (empty($new_path)) {
    $new_path_system = variable_get('site_frontpage', 'node');
  }

  // Build alt source url
  $alt_parts = $current_parts;
  $alt_parts['path'] = $current_path_system;
  $alt_src = boost_glue_url($alt_parts);
  $urls = array(
    $alt_src,
    $source,
  );
  $debug = array(
    'destination' => $destination,
    'source' => $source,
    'alt_src' => $alt_src,
    'current_path' => $current_path,
    'current_path_system' => $current_path_system,
    'new_path' => $new_path,
    'new_path_system' => $new_path_system,
  );

  // Handle domain alias redirects
  if (module_exists('domain_alias') && isset($_domain['redirect']) && $_domain['redirect'] == TRUE) {
    boost_cache_kill_url($urls);
    return;
  }
  elseif (strcmp($new_parts['host'], $current_parts['host']) != 0) {

    //watchdog('boost-redirect', 'redirect is not to the same domain' . str_replace('    ', '    ', nl2br(htmlentities(print_r(array($debug), TRUE)))));
    boost_cache_kill_url($urls);
    return;
  }

  // Check for globalredirect internal to alias redirect
  if (strcmp($current_path, $new_path_system) == 0) {
    boost_cache_kill_url($urls);
    return;
  }

  // Check for globalredirect alias to alias redirect (deslashing)
  // Also grabs not clean to clean redirects
  if (strcmp($current_path, $_REQUEST['q']) != 0) {
    if (strcmp($current_path_system, $new_path_system) == 0) {
      boost_cache_kill_url($urls);
      return;
    }
  }
  if (module_exists('path_redirect')) {

    // Check for normal path_redirect alias to alias redirect
    $path_redirects = boost_path_redirect_load(array(
      'source' => $current_path,
    ));
    if (isset($path_redirects)) {
      foreach ($path_redirects as $path_redirect) {
        $current_path_system = $path_redirect['redirect'];
        $debug['new_current_path_system'] = $current_path_system;
        break;
      }
    }
    if (strcmp($current_path_system, $new_path_system) == 0) {
      boost_cache_kill_url($urls);
      return;
    }

    // Check for alt path_redirect alias to alias redirect
    $path_redirects = boost_path_redirect_load(array(
      'source' => $current_path_system,
    ));
    if (isset($path_redirects)) {
      foreach ($path_redirects as $path_redirect) {
        $current_path_system = $path_redirect['redirect'];
        $debug['new_current_path_system'] = $current_path_system;
        break;
      }
    }
    if (strcmp($current_path_system, $new_path_system) == 0) {
      boost_cache_kill_url($urls);
      return;
    }
  }

  // Last attempt of getting a "match" for this redirect
  $result = db_query("SELECT page_callback, page_type, page_id FROM {boost_cache} WHERE expire = 0 AND (hash_url = '%s' OR hash_url = '%s')", md5($source), md5($alt_src));
  while ($row = db_fetch_array($result)) {

    // Handle node redirects
    if ($row['page_callback'] == 'node') {
      $current_path_system = $row['page_callback'] . '/' . $row['page_id'];
      if (strcmp($current_path_system, $new_path_system) == 0) {
        boost_cache_kill_url($urls);
        return;
      }
    }
  }

  //watchdog('boost-redirect', 'Nothing Done' . str_replace('    ', '    ', nl2br(htmlentities(print_r($debug, TRUE)))));
}