<?php

/*
 * Custom 404.php handler in PHP which looks at the URL the request was for
 * and allows you to send them to the right page.
 *
 * To use place the following line in your httpd.conf or VirtualHost entry:
 * ErrorDocument 404 /404.php
 *
 * Paul Gregg <pgregg@pgregg.com>, (c) 2003
 *
 */


// Define the a substring to match in the 404ed request and the location
// that you want the browser to be directed to.

$catch_patterns = array(
  
'personal/blog/archives/1_Welcome_to_my_blog'    => '/forums/viewtopic.php?id=1',  // Changed my blog
  
'personal/blog/archives/2_War_What_war'          => '/forums/viewtopic.php?id=2',  // so I redirect
  
'personal/blog/archives/3_Music_sales_on_the_up' => '/forums/viewtopic.php?id=3',  // old urls to the
  
'personal/blog/archives/4_The_Tech_Vote'         => '/forums/viewtopic.php?id=4',  // new locations
  
'personal/blog/archives/5_Tech_Support_drones'   => '/forums/viewtopic.php?id=5',
  
'siged'        => '/projects/siged/',
  
'qmail'        => '/projects/qmail/',
  
'radiussplit'  => '/projects/radiussplit/',
  
'radiusreport' => '/projects/radiusreport/',
  
'singleuid'    => '/projects/qmail/singleuid/',
  
'BMAS'         => '/projects/radiusreport/BMAS-preparser.php',
  
'blog'         => '/forums/viewforum.php?id=1',
  
'telnet://nyx' => 'http://nyx10.nyx.net:8002/',    // Stupid browsers that don't understand telnet://
  
'formmail'     => '/nohack.php',
  
'default.ida'  => '/nohack.php',
  
'cmd.exe'      => '/nohack.php',
  
'scripts/'     => '/nohack.php',
  
'vti_bin/'     => '/nohack.php',
  
'cltreq.asp/'  => '/nohack.php',
  
'\.\.'         => '/nohack.php',
);
$catch_regex '#((' implode('|'array_keys($catch_patterns)) . '))#i';

$req htmlspecialchars($_SERVER['REQUEST_URI']);

if ( 
preg_match($catch_regexurldecode($_SERVER['REQUEST_URI']), $caught) ) {
  
$domail 0;
  
$redir $catch_patterns[$caught[1]];
  if (
$redir == '/nohack.php') { // Display nice J00 ar3 4 L4M3R page
    
include $_SERVER['DOCUMENT_ROOT'] . '/nohack.php';
  } else {
    
Header("Location: $redir"false301);
    print 
"<p>The page: <b>$req</b> that you requested
        was not found. The page you are looking for may (perhaps) be
    found at <a href=\"
$redir\">$redir</a>\n";
  }
  exit;
}

if ( 
preg_match("/[\.,>]$/"$_SERVER['REQUEST_URI']) ) {
  
$redir preg_replace("/[\.,>]$/"''$_SERVER['REQUEST_URI']);
  
Header("Location: $redir");
  
$domail 0;
  print 
"<p>The page: <b>$req</b> that you requested
        is probably incorrect as it has a trailing character. The page you are
        looking for may (perhaps) be found at <a
        href=\"
$redir\">$redir</a>\n";
  exit;
}

print <<<EO404
<h1>Page&nbsp;Not&nbsp;Found</h1>

<p>The page: <b>
$req</b> that you requested was not
    found on this server.</p><p>Please visit <a
    href=\"http://www.pgregg.com\">http://www.pgregg.com/</a> where the
    page you are looking for may be found.</p>\n
    <p>Please also note that I monitor these failed requests and, if
    appropriate, I may either restore the original page, or redirect the
    request to a more suitable location at this site. So please check back
    in a day or so and the page may be here.</p>

EO404;

?>