<?php
	include("/home/mercadal/public_html/common/stdhead.php");
?>

<h2>Search the Derpsite</h2>
<hr>
<img src="common/talkie.png" height="66" width="66" hspace="5" align="right">

<p>
	Find all the goodness at the Derpsite!
</p>


<p>&nbsp;</p>

<?php
	$newkey = preg_replace ("/[^A-Za-z0-9^ ]/","",$_POST["keywords"]);


	if (!empty($newkey)) {
?> 

<p>Searching for <b><?php echo ($_POST["keywords"]); ?></b>


<?php
	/* function to retrieve time information */
	function getmicrotime()
	{ 
		list($usec, $sec) = explode(" ",microtime()); 
		return ((float)$usec + (float)$sec); 
	} 

	function scroll($current, $terms, $total) 
	{
		$handle = opendir($current);	/* open directory $current */
		
		while ($file = readdir($handle)) {
			$firstLine = ""; 					/* initialize first match */
			$fcount = 0; 						/* initialize number of matches per file */
			$found = false;						/* found flag - true or false */	
			$file_p = $current . "/" . $file;	/* create the path to the file for the a href */
			
			if (($file != ".") && ($file != "..")) {	/* ignore directory files */
			
				/* This next if statement will return true if the file is really a 
				 * regular file, and if it contains the strings contained in the stristr directives.
				 * NB:  Searching for, example, .htm will return true for any file with .htm in 
				 * the name, including .html
				 */
				if (is_file($file_p) && ((strpos($file, ".htm")) || (strpos($file, ".php")) || (strpos($file, ".txt")))) {
					$file_b = fopen($file_p, "r");
					
					while (!feof($file_b)) {
						$line = fgetss($file_b, 2000);	/* remove tags, get line */
						$line = trim($line);
						
						
						if (!empty($line)) {
							if (eregi($terms, $line, $regs)) {
								$found = true;
								$fcount++;
								
								if ($firstLine == "") {
									$firstLine = $line;	/* set first line term was found in */
								}
							}	
						}					
					}				
			
					if ($found == true) {
						$len = strlen($file_p);
						$total++;
						printf("<p><a href=\"%s\">%s</a> contains %d match%s.\n", $file_p, substr($file_p, 2, $len-2), $fcount, ($fcount ==1) ? "" : "es");
						printf ("<br>First match: <i>\n%s</i>", $firstLine);
					}
				}
				
				if (is_dir($file_p)) {
					$new_dir = $current . "/" . $file;
					scroll ($new_dir, $terms,  &$total);	/* recurse into directory */
				}			
			}
		}
	
		closedir($handle);	/* close directory handle */
	}


	$time_start = getmicrotime();
	$tcount = 0;	/* initialize file count */
 
	scroll(".", $newkey, &$tcount);	/* initial call to scroll */

	if ($tcount < 5) {
		for($qcount = 0; $qcount < (5 - $tcount); $qcount++) {
			echo("<p>&nbsp;</p> <p>&nbsp;</p>");
		}
	}

	if ($tcount > 0) {
		$time_end = getmicrotime();
		$timetotal = $time_end - $time_start;
		
		printf("<p>Found %d total match%s in %.3f seconds.</p>", $tcount, ($tcount == 1) ? "" : "es", $timetotal);
	} else {
		print ("<p>No matches. Search for something better!");
	}

} else {	/* keywords are empty */
	echo("Try actually searching for some <i>words</i> or something.");
	echo("<p>&nbsp;</p><p>&nbsp;</p> <p>&nbsp;</p><p>&nbsp;</p> <p>&nbsp;</p><p>&nbsp;</p> <p>&nbsp;</p><p>&nbsp;</p> <p>&nbsp;</p><p>&nbsp;</p> <p>&nbsp;</p>");
}
?>


<?php
	include("/home/mercadal/public_html/common/stdfoot.php");
?>