You are here

function customerror_check_redirect in Customerror 6

Same name and namespace in other branches
  1. 5 customerror.module \customerror_check_redirect()
  2. 7 customerror.module \customerror_check_redirect()

Check list of redirects.

1 call to customerror_check_redirect()
customerror_page in ./customerror.module
Displays the 403 or 404 error page.

File

./customerror.module, line 278
Enables custom 404 (not found) and 403 (access denied) pages in Drupal.

Code

function customerror_check_redirect() {
  $destination = $_REQUEST['destination'];
  if (empty($destination)) {
    return;
  }
  $redirects = trim(variable_get('customerror_redirect', ''));
  if (empty($redirects)) {
    return;
  }
  $redirect_list = explode("\n", $redirects);
  foreach ($redirect_list as $item) {
    $pair = explode(' ', $item);
    $src = trim($pair[0]);
    if (isset($pair[1])) {
      $dst = trim($pair[1]);
    }
    else {
      $dst = NULL;
    }
    if (empty($src) || empty($dst)) {
      $item = trim($item);

      // Replace sp with nbsp.
      $item = str_replace(' ', ' ', $item);
      drupal_set_message(t('Malformet redirect <code>"@item"</code> in custom error 404 redirect list.', array(
        '@item' => $item,
      )), 'error');
    }
    else {
      $src = str_replace("/", "\\/", $src);

      // In case there are spaces in the URL, we escape them.
      $orig_dst = str_replace(" ", "%20", $destination);
      if (preg_match('/' . $src . '/', $orig_dst)) {
        $_REQUEST['destination'] = $dst;
        drupal_goto($dst);
      }
    }
  }
}