You are here

function shurly_goto in ShURLy 6

Same name and namespace in other branches
  1. 7 shurly.module \shurly_goto()

A heavily modified version of drupal_goto() (which hasn't been bootstrapped during hook_boot()

1 call to shurly_goto()
shurly_boot in ./shurly.module
Implementation of hook_boot()

File

./shurly.module, line 843
description http://www.youtube.com/watch?v=Qo7qoonzTCE

Code

function shurly_goto($row) {
  if (!$row || isset($_GET['redirect']) && $_GET['redirect'] == 'false') {
    return;
  }

  // Allow other modules to implement hook_shurly_redirect_before()
  // to add additional logging information to the database or perform other tasks
  // _before() is probably best to use for altering the $row->destination
  // Remember this is running during hook_boot(). Many Drupal functions are unavailable.
  module_invoke_all('shurly_redirect_before', $row);
  $url = $row->destination;

  // Remove newlines from the URL to avoid header injection attacks.
  $url = str_replace(array(
    "\n",
    "\r",
  ), '', $url);

  // We do not want this while running update.php.
  if (!defined('MAINTENANCE_MODE') || MAINTENANCE_MODE != 'update') {

    // Allow modules to react to the end of the page request before redirecting.
    module_invoke_all('exit', $url);
  }

  // Even though session_write_close() is registered as a shutdown function, we
  // need all session data written to the database before redirecting.
  session_write_close();
  header('Location: ' . $url, TRUE, 301);

  // header has been sent... browser has been redirected
  // now we can do any expensive operations
  // update access information on this row
  db_query('UPDATE {shurly} SET count = count + 1, last_used = %d WHERE rid = %d', time(), $row->rid);

  // note: If possible, other modules should probably insert more information
  // in the database by using hook_db_rewrite_sql() on the above query
  // rather than creating a new db call
  // Allow other modules to implement hook_shurly_redirect_after()
  // _after() happens after the redirect has already been sent to browser.
  // It's probably best for slower operations like additional database logging
  // Remember this is running during hook_boot(). Many Drupal functions are unavailable.
  module_invoke_all('shurly_redirect_after', $row);

  // The "Location" header sends a redirect status code to the HTTP daemon. In
  // some cases this can be wrong, so we make sure none of the code below the
  // drupal_goto() call gets executed upon redirection.
  exit;
}