--- search404.module.orig	2007-12-07 14:38:35.000000000 +0000
+++ search404.module	2007-12-07 16:26:16.000000000 +0000
@@ -95,8 +95,16 @@
  * Beware of messy code
  */
 function search404_page() {
-  $output = '<p>'. t('The page you requested was not found.') .'</p>';
-
+  $output = '<p>'. t('The page you requested was not found.') ;
+  if ( $newhostname = variable_get('search404_oldsite', '')) {
+    $try_url = "http://".$newhostname."/".$_REQUEST['q'];
+    $response = search404_GetResponse ( $try_url );
+    if ( strpos($response, "200") !== FALSE ) {
+       $output .= t("You have followed an outdated link. <strong>YOU MUST UPDATE YOUR BOOKMARKS.</strong> Nevertheless, you can still temporarily view the requested document by following the link below:");
+       $output .= '<p><div class="oldsitelink"><a href="'.$try_url.'" target="_new">'.t("The outdated version of the requested document at %s",array("%s"=>$try_url)).'</a></div>';
+    }
+  }
+  $output .='</p>';
   if (module_exists('search') && user_access('search content')) {
     $keys = "";
     if (variable_get('search404_use_search_engine', false)) {
@@ -180,6 +188,135 @@
     '#description' => t('A search will not be performed for a query ending in the following extensions. Separate extensions with a space, e.g.: "gif jpg jpeg bmp png". Do not include leading dot.'),
     '#default_value' => variable_get('search404_ignore_query', 'gif jpg jpeg bmp png'),
   );
+   $form['search404_oldsite'] = array(
+    '#type' => 'textfield',
+    '#title' => t('Old site domain name'),
+    '#description' => t('If filled in, the module will first try to locate the URL on the alternative sita nad present the link at the top of the search results. Example: "www.myoldsite.org"'),
+    '#default_value' => variable_get('search404_oldsite', ''),
+  );

   return system_settings_form($form);
+}
+
+/************************ Functions for URL checking *******************/
+// Gets urls
+function search404_GetUrls( $html ) {
+  // Finds any links in the HTML
+  $matches = array();
+  preg_match_all("|href\=\"?'?`?([[:alnum:]:?=&@/;, %+._-]+)\"?'?`?|i", $html, & $matches);
+  $links = array();
+  $ret = $matches[1];
+  for($i=0;isset($ret[$i]);$i++) {
+     if(preg_match("|^http://(.*)|i",$ret[ $i])) {
+         $links[] = $ret[$i];
+     } elseif(preg_match("|^/(.*)|i",$ret[$i])) {
+         if ( variable_get( 'linkchecker_fqdn_only', 1 ) == 0 )
+            $links[] = "http://".$_SERVER["SERVER_NAME"]."". $ret[$i];
+     }
+  }
+  return $links;
+}
+
+// Gets Unique Urls
+function search404_GetUniqueUrls( $html ) {
+
+  if ( !$html ){
+    return false;
+  }
+
+  // Gets the list of urls
+  $urls = search404_GetUrls ( $html );
+  $uurls = array();
+  for( $i=0;isset($urls[$i]);$i++ ) {
+
+    // Checks if the url is in the array
+    if(!in_array($urls[$i], $uurls )) {
+
+      // If it's not it adds it
+      $uurls[] = $urls[$i];
+    }
+  }
+  return $uurls;
+}
+
+// Gets headers
+function search404_GetHeaders($url) {
+  // Gets url ready to use
+  $info  = @parse_url( $url );
+
+  // Opens socket
+
+  $fp    = @fsockopen( $info["host"], 80, $errno, $errstr, variable_get('linkchecker_socket_timeout', 3) );
+  // Makes sure the socket is open or returns false
+  if ( !$fp ) {
+     return false;
+  } else {
+
+     // Checks the path is not empty
+     if( empty( $info["path"] ) ) {
+
+        // If it is empty it fills it
+        $info["path"] = "/";
+     }
+     $query = "";
+
+     // Checks if there is a query string in the url
+     if( isset( $info["query"] ) ) {
+
+          // If there is a query string it adds a ? to the front of it
+          $query = "?".$info["query"]."";
+     }
+     $info["path"] = str_replace(" ","%20",$info["path"]);
+     // Sets the headers to send
+
+     $out  = "HEAD ".$info["path"]."".$query." HTTP/1.0\r\n";
+     $out .= "Host: ".$info["host"]."\r\n";
+     $out .= "Connection: close \r\n" ;
+     $out .= "Accept-language: en-us;q=0.7,en;q=0.3 \r\n";
+     $out .= "Accept:   text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5";
+     $out .= "Accept-charset: ISO-8859-2,utf-8;q=0.7,*;q=0.7";
+     $out .= "User-Agent:   Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.8.1.5) Gecko/20061201 Firefox/2.0.0.5 (Ubuntu-feisty) \r\n\r\n";
+     d_("Headers: $out");
+     // writes the headers out
+     fwrite( $fp, $out );
+     $html = '';
+
+     // Reads what gets sent back
+     //  while ( !feof( $fp ) ) { - commented out, no need to read the whole thing
+          $html .= fread( $fp, 8192 );
+     //}
+
+     // Closes socket
+     fclose( $fp );
+  }
+  return $html;
+}
+
+// Gets status code
+function search404_GetStatusCode( $header ) {
+
+  // Splits the headers into an array
+  $headers = explode( "\r\n", $header );
+  unset( $header );
+  for( $i=0;isset( $headers[$i] );$i++ ) {
+
+    // Checks if the header is the status header
+    if( preg_match( "/HTTP\/[0-9A-Za-z +]/i" ,$headers[$i] ) ) {
+
+      // If it is save the status
+      $status = preg_replace( "/http\/[0-9]\.[0-9]/i","",$headers[$i] );
+    }
+  }
+  return $status;
+}
+
+function search404_GetResponse ( $url ) {
+   $headers = search404_GetHeaders ( $url );
+   if(!$headers) {
+     $response = "-1 Unable to connect";
+   } else {
+   // Get status code
+   $response = search404_GetStatusCode( $headers );
+   }
+   return $response;
 }
\ No newline at end of file
