#native_company# #native_desc#
#native_cta#

recursive site search

By Kimberlee Jensen
on August 24, 2001

Version: 3 +

Type: Full Script

Category: Other

License: GNU General Public License

Description: This script contains the form and the code to search a web site recursively through all subdirectories. This script works best in the top directory of your web site and uses an exact match as the search criteria. It works in php3 and php4.

<html>
<head>
<title>Search our site</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<!-- Copyright 2001 Isis Eye Media -->
<body bgcolor="#FFFFFF">
<form method="post" action="results.php3" name="search">
  <p><font face="Verdana, Arial, Helvetica, sans-serif" size="2"> Please enter 
    text to search </font></p>
  <p> 
    <input type="text" name="keywords">
  </p>
  <p>
    <input type="submit" name="Submit" value="Submit">
  </p>
</form>
<?if (!empty($keywords))
{
 ?> 
<p><font face="Verdana, Arial, Helvetica, sans-serif" size="2"> You were searching for <b><? print ($keywords); ?></b>


<?
/* this script must reside in your root directory and will search recursively down through your web site

this script is written to work with php3 - if you are running php4, simply remove the 3 from the file name reference
 make the same modification in the form tag on our search page

$key = escapeshellcmd ($keywords); // let's make sure any metacharacters get escaped so as not to wreak havoc with our host*/



$tcount = 0; // initialize our file count



function scroll($current, $terms, $total) 
	{

	$handle=opendir($current);		// open the directory passed as the first argument	
	while (($file = readdir($handle))!=false) // keep cycling through all the files
			{$first = ""; // initialize first occurance of the match
			$fcount = 0; // initialize number of matches per file
			$found = false;	// initialize flag - true or false		
			$file_p = $current . "/" . $file;	// create the path to the file for the a href		
			if (($file != ".")  && ($file != "..")) // ignore . and .. files
						
				{
				if (is_file($file_p) && ((substr(strrev($file),0,1) == "3") || (substr(strrev($file),0,1) == "l") || (substr(strrev($file),0,1) == "m") ||(substr(strrev($file),0,1) == "p") ||(substr(strrev($file),0,1) == "t"))) // limit our search to
				// php3, php, html, htm and txt files
				
					{
					
					
					$file_b = fopen($file_p, "r"); // open the file for reading
					
					while (!feof($file_b)) // continue to end of line
						{$line = fgetss($file_b, 2000); // try and remove all data between <> when reading
						$line = trim($line); // trim any leading or trailing white space
						
						
					
						if (!empty($line))
							{
								
							if (eregi($terms, $line, $regs)) // check that line for a match, using the keywords, the line, and returning an array of the matches
							
								{$found = true; // set flag
								$fcount++; //increment total found for this file
								
								if ($first == "")
									{$first = $line; // set the variable to display the first line where the match occurred
									}
								}	
						
							}
						
											
						}					
			
						if ($found == true)
							{$len = strlen($file_p); // this allows us to print the link, ignoring the starting /
							$total++; // increment total number of file matches
							print ("<p><a href ='" . $file_p . "'>" . substr($file_p, 2, $len-2) . "</a> contains ". $fcount . " match(es).");
							print ("<br>First line matched: <i>" . $first . "</i>"); // display the first line in which the match was found
							}
						
					}
				
				if (is_dir($file_p)) // if the file is a directory
				
				
					{
					
					$new_dir = $current . "/" . $file;
					scroll ($new_dir, $terms,  &$total); // call the function recursively when it is a directory
					}
				
				// end of if (($file != ".")  && ($file != ".."))			
				}
		
			// end of while (($file = readdir($handle))!=false)
			}
	
	closedir($handle); // close the directory
	
	// end of function
	 }
 
 scroll(".", $key, &$tcount); // initial call to the function, passing it the current directory as the start, the cleaned up search terms and the total number of files found - this is passed by reference so it can be incremeneted script-level
                         

if ($tcount > 0)
{print ("<p>We found " . $tcount . " total match(es). Care to <a href = "search.html">search again?</a>");}
else
{print ("<p>We found failed to find a match, please <a href = "search.html">search again.</a>");}

} // end of keywords not empty
?>


  </font>

<p><font face="Verdana, Arial, Helvetica, sans-serif" size="2"><? print ("written by <a href = "http:/www.isiseyemedia.com">Isis Eye Media</a>");?> </font></p>
</body>
</html>