<!DOCTYPE HTML>
<html>
    <head>
        <title>File Based Local Site Search Engine</title>
        <style>
            body {
                font: normal 10pt Arial, sans-serif;
            }
            .result {
                background:    ivory;
                color:        maroon;
                border:        1px solid #DDD;
                margin:        10px 0;
            }
            .result .link {
                margin:        0;
                padding:    0 0 0 5px;
            }
            .result .snippet {
                margin:        2px 10px;
                padding:    0;
            }
            .result .file_info {
                color:        #333;
                font-size:    smaller;
                margin:        2px 10px;
                padding:    0;
            }
        </style>
    </head>
    <body>
<?php

// decide to use or not use case sensitivity with grep
$cs    = ( isset($_POST['case_sensitive'])    ? ' -i '    ''    );

// decide to search recursivly or only in the current directory
$r    = ( isset($_POST['recursive'])        ? ' -r '    ''    );

// decide to include files that only match displayable text, or the entire file
$ef    = ( isset($_POST['entire_file'])    ? true        false    );

?>
<form method="post">
    <input type="text" name="searchString" value="<?php echo htmlentities(@$_POST['searchString']); ?>" size="40" />&nbsp;<input type="submit" value="Search" /><br />
    <input type="checkbox" name="case_sensitive" value="yes" <?php print (!empty($cs) ? ' checked="checked" ':''); ?> /> Case-Insensitive &nbsp;
    <input type="checkbox" name="recursive" value="yes" <?php print (!empty($r) ? ' checked="checked" ':''); ?> /> Recursive &nbsp;
    <input type="checkbox" name="entire_file" value="yes" <?php print ( $ef ' checked="checked" ':''); ?> /> Entire File &nbsp;
</form>
<hr />
<?php
# Search.php
// Only search in sub-directories from me
$base_path dirname(__FILE__);

// Allow results to be from only these file types
$allowed_ftypes = array('txt''html''php''phps''css''js''def''conf''ini''inc');

// Initialize initial results array
$results = array();

// Initialize Results array
$allowed_results = array();

// Start if searchString was submitted
if ( !empty($_POST['searchString']) ) {

    
// Escape incoming search string
    
$safe_searchString escapeshellarg$_POST['searchString'] );

    
// Start Timer
    
$start_time microtime(true);
    
    
// Run search
    
exec("grep -l {$r} {$cs} {$safe_searchString} *"$results);
    
    
// Loop through results
    
foreach ( $results AS $file ) {
    
        
// Get extension and test file type
        
$ext array_pop(explode('.'$file));
        if ( ( 
count($allowed_ftypes) > && in_array(strtolower($ext), $allowed_ftypes) ) || count($allowed_ftypes) == ) {
        
            
// Grab file contents
            
$contents file_get_contents($file);

            
// Decide whether to include based on entire file or found in HTML restriction
            
if ( !$ef ) {
                
$contents strip_tags($contents);
            }

            
$contents preg_replace('|[\s]+|'' '$contents);

            
// 
            
if ( ( $pos strpos($contents$_POST['searchString']) ) !== false ) {

                
$snippet substr($contentsmax(($pos-200), 0), 400);
        
                
// Add link to result set
                
$allowed_results[] = array(
                                
'link' => "<a href='{$file}'>{$file}</a>",
                                
'snip' => htmlspecialchars($snippet),
                                
'lmod' => date('Y-m-d H:i A'filemtime($file)),
                                
'size' => round(filesize($file)/10241),
                                );

            }
        }
    }
    
// Finish Timer
    
$finish_time microtime(true);
    
    
// Display Results
    
echo "Searching for {$safe_searchString} - ".count($allowed_results)." result".(count($allowed_results)==1?'':'s').' in '.round($finish_time-$start_time2).' seconds.<br /><hr />';
    foreach ( 
$allowed_results AS $v ) {
        echo <<<HTML
<blockquote class="result">
    <h4 class="link">
{$v['link']}</h4>
    <blockquote class="snippet">
{$v['snip']}</blockquote>
    <span class="file_info"><b>Modification Time:</b> 
{$v['lmod']} &nbsp; <b>Size:</b> {$v['size']} kb's</span>
</blockquote>
        
HTML;
    }
}