<?
$search_dirs = array("/home/site.com/public_html/klasor" => "http://www.site.com/klasor/");
// End Specify Directories to search.
function Get_Filenames_To_Search($file_dir)
{
//Put File Directory Into An Array
$dir = opendir($file_dir);
while ($file = readdir($dir)) {
if ($file != "." && $file != "..")
$retVal[count($retVal)] = $file;
}
//Close Directory and Sort Results
closedir($dir);
sort($retVal);
return $retVal;
}
Function Search_Files_For_Keywords($filenames,$keywords)
{
for($f = 0; $f < count($filenames); $f++) {
$filename = $filenames[$f];
$match = 0;
$fd = fopen($filename, "r");
$document = fread($fd, filesize ($filename));
fclose($fd);
//Remove HTML Tags, Strip Javascript, Strip White Space Tags before searching
$search = array ("'<script[^>]*?>.*?</script>'si", // Strip out Javascript
"'<[\/\!]*?[^<>]*?>'si", // Strip out html tags
"'([\r\n])[\s]+'", // Strip out White Space
"'&(quot|#34);'i", // Replace HTML entities
"'&(amp|#38);'i",
"'&(lt|#60);'i",
"'&(gt|#62);'i",
"'&(nbsp|#160);'i",
"'&(iexcl|#161);'i",
"'&(cent|#162);'i",
"'&(pound|#163);'i",
"'&(copy|#169);'i",
"'&#(\d+);'e"); // evaluate as PHP
$replace = array ("",
" ",
"\\1",
"\"",
"&",
"<",
">",
" ",
chr(161),
chr(162),
chr(163),
chr(169),
"chr(\\1)");
$document = preg_replace ($search, $replace, $document);
$document = preg_replace ("/\W/", " ", $document);
$document = preg_replace ("/\s+/", " ", $document);
//Seperate Each Word into an Array Element and Compare to Keywords
$document = explode(" ", $document);
$k = 0;
for($k = 0; $k < count($keywords); $k++) {
for($d = 0; $d < count($document); $d++) {
//compare contents with each keyword
if (!strcasecmp ($document[$d], $keywords[$k])) {
$match++;
break;
}
}
}
if ($match == count($keywords))
$retVal[count($retVal)] = $filename;
}
return $retVal;
}
if ($keyword != "") { // This IF statement checks to see if keyword textbox is blank
if ( isset($keyword) ) {
$keywords = explode(" ", $keyword);
$pages = array();
while (list ($key, $val) = each ($search_dirs)) {
$file_dir = $key;
chdir($file_dir) or die("Directory (<B>$file_dir</B>) Not found");
$filenames = Get_Filenames_To_Search($file_dir);
$found = Search_Files_For_Keywords($filenames,$keywords);
//add any pages with keywords in current directory to array
for($i = 0;$i < count($found); $i++) {
$add = "$val$found[$i]";
$pages[count($pages)] = $add;
}
}
}
}
?>
<table border='0' cellspacing='0' cellpadding='0' width='600' bgcolor='#ffffff'>
<td width='500' valign='center' align='left'>
<font size='2' color='#000000'>
<h4><font color='#0000ff'>Site Search</font></h4>
<!-- Place Code Here -->
<form>
<b>Enter Search Keywords:</b><br>
<input type=text name=keyword value='Search Keywords' size='30' maxlength='20'><br><br>
<input type=Submit value='Search Site'>
<input type=Reset value='Reset'>
<?PHP
if ( isset($keyword) )
{
$numberfound = count($pages);
echo "<br><hr>\n";
echo "(<b>$numberfound</b>) <font color='#0000ff'>pages were found on your site search for</font> (<b>$keyword</b>).\n";
echo "<hr><br>\n\n";
echo "<font color='#0000ff'><b>Search Results</b></font><br>\n\n";
for ($i = 0; $i < count($pages); $i++)
echo "<a href='$pages[$i]'>$pages[$i]</a><br>\n";
}
?>
</form>
<!-- End Code -->
</td>
</tr></table>