'); /*******************************************************\ * ALL VARIALBES BELOW THIS POINT CAN REMAIN AS THEY ARE * * AND THE SCRIPT WILL STILL FUNCTION CORRECTLY * \*******************************************************/ // Set to true if you wish the time taken to be displayed when searching, false otherwise: $show_time_search = true; // Displays time taken when indexing if true: $show_time_index = true; // The array file file extentions to index. The extentions must be readable (no exe, jpg, gif) $include_extentions = array('html','htm','shtml','shtm', 'php', 'php3', 'phtml', 'php4', 'txt',); // Message variables: $err_no_results = "No results were found. Please search again."; $err_no_search_db = "Could not find search database
Contact the administrator of this site."; $err_index = "The index file could not be opened.
Check the path and permissions and try again."; $index_complete = "Your site has been indexed."; $index_cleared = "Your site index has been cleared.
No search results will be found until you re-index your site."; $no_files = "No files in the specified directory were available for indexing."; $no_dir = "The directory you specified cannot be found."; $err_index_empty = "The index file was found, but it contained no data."; // Uncomment this line to debug the script. //DEFINE("DEBUG", true); // Sets the version. $version = "1.8a"; // Sets start time. $timeparts = explode(" ",microtime()); $starttime = $timeparts[1].substr($timeparts[0],1); // Execute either search or admin funtion if (isset($query)) s_control($query); elseif (substr($_SERVER['QUERY_STRING'],0,5) == "admin") i_control(); // Search control function function s_control($q) { $orig = $q; $result_arr = s_search($q); $result_count = sizeof($result_arr); if ($result_count < 1) { echo "

$GLOBALS[err_no_results]

"; } else { echo "

Search Results

Results: 1 - $result_count for \"$orig\"

"; } } // Funtion to query the index file and return the results as an array function s_search($query) { // Brings in the search string $query = trim(strtolower(c_strip_chars($query))); // Load in index file $search_data = @file($GLOBALS[index_file]) or die("

$GLOBALS[err_no_search_db]

"); $pages_found = " "; // Loop though each line of index file foreach ($search_data as $search_page) { // Explode index line into keyword array $page_arr = explode("|", $search_page); $found_count = 0; // Split search query into array $qry_array = split('[, ]+',trim(strtolower($query))); // Loop though each query word foreach ($qry_array as $qry) { // Check to see if query word in index keyword array if (in_array($qry, $page_arr)) { // Add one to found count int ++$found_count; //echo $found_count . ' '; // Add page url to end of string $pages_found .= $page_arr[0] . " "; } } // This makes sure all of the query words are matched and not just one for example if ($found_count == count($qry_array)) $result_arr[] = $page_arr[0] . '|' . $page_arr[1] . '|' . $page_arr[2]; } return $result_arr; } // Formats and returns each search result function s_print_title_desc($result) { // Splits result into address, snippet and page title from index $result_arr = explode("|", $result); $file_n = $result_arr[0]; $file_snippet = $result_arr[1]; $title = $result_arr[2]; // Formats HTML to be rendered echo "
  • "; if (isset($title)&&strlen($title)>0) echo "$title
    - $file_n"; else echo "$file_n"; echo "
    $file_snippet...\n "; } // Controls the admin section function i_control() { // Grabs global variables global $action, $username, $password, $user, $pass; // Checks login details if (($user == $username) && ($pass == $password)) { // Checks for a admin action if ($action == "clear_index") i_clear_index(); elseif ($action == "view_index") i_view_index(); elseif ($action == "index_site") i_index_site(); i_print_options(); } else { if (($username == "") && ($username == "")) i_print_logon(""); else i_print_logon("Invalid username and/or password."); } } function i_index_site() { // Indexes the site & writes it to file. if (!isset($_POST[s])) { echo "

    You have selected to index your site.

    You can index your site using meta tag \"keywords\" or you can perform a \"full\" index.
    Which action do you wish to perform?

    "; } else { if (is_dir($GLOBALS[file_root])) { $file_array = i_get_files($GLOBALS[file_root]); $file_array = i_strip_extentions($file_array); $file_array = i_strip_files($file_array); if(is_array($file_array)) { set_time_limit(0); $fd = @fopen($GLOBALS[index_file], "w") or die("

    $GLOBALS[err_index]

    "); foreach ($file_array as $file) { if (($_POST[s] == "submit_full") || (substr($file, -3) == "txt")) $line = i_full_index_file($file); elseif ($_POST[s] == "submit_meta")$line = i_meta_index_file($file); if (substr_count($line, "|") > 1) fputs($fd, "$line\n"); } fclose($fd); echo "

    $GLOBALS[index_complete]

    "; } else { echo "

    $GLOBALS[no_files]

    "; } } else { echo "

    $GLOBALS[no_dir]

    "; } } } function i_full_index_file($file_name) { // Retrieves a list of keywords from a file. global $http_root, $file_root, $exclude_nav; $file_contents = @file($file_name); if ($file_contents) { $URL = str_replace($file_root, $http_root , $file_name); $keywords = "$URL|"; $file_contents = implode(" ", $file_contents); eregi("(.*)", $file_contents, $out); $title = trim($out[1]); $file_contents = substr($file_contents, strpos($file_contents, '')+7); $file_contents = strip_tags($file_contents); $file_contents = str_replace(" ", " ", $file_contents); $file_contents = eregi_replace("[[:space:]]+", " ", $file_contents); $file_contents = trim($file_contents); echo "

    File Name: " . $file_name . "

    \n"; echo "

    Before Replace

    \n"; echo "\n"; foreach ($exclude_nav as $exc) $file_contents = str_replace($exc, " ", $file_contents); $file_contents = eregi_replace("[[:space:]]+", " ", $file_contents); $file_contents = trim($file_contents); echo "

    After Replace

    \n"; echo "\n"; echo "

    \n"; $file_snippet = trim(substr($file_contents, 2500, 3000)); $file_contents = strtolower($file_contents); $file_contents = c_strip_chars($file_contents); $file_contents = explode(" ", $file_contents); $keywords .= "$file_snippet|$title|"; foreach($file_contents as $word) { $word = trim($word); if ($word != "") { $keywords .= "$word|"; } } } $complete = str_replace("|||", "|", $keywords); $complete = str_replace("||", "|", $complete); $complete = i_strip_words($complete); return $complete; } function i_meta_index_file($file) { // Indexes a page by it's meta tags. global $index_file, $http_root, $file_root; $URL = str_replace($file_root, $http_root , $file); $mt = @get_meta_tags($file); $line = $mt[keywords]; if ($line) { $line = explode(",", $line); foreach ($line as $word) { $word = trim($word); if ($word != "") { $keywords .= "$word|"; } } $keyword = str_replace(",", "", $keywords); $keywords = c_strip_chars($keywords); $keywords = i_strip_words($keywords); $keywords = "$URL|$keywords"; return $keywords; } else { return "$URL|"; } } function i_get_files($dirname) { // Navigates through the directories recurrsively and retrieves a listing in an array. // File permission bit by Abhay Jain if($dirname[strlen($dirname)-1] != "/") $dirname.="/"; static $result_array = array(); $mode = fileperms($dirname); //if(($mode & 0x4000) == 0x4000 && ($mode & 0x00004) == 0x00004) { chdir($dirname); $handle = @opendir($dirname); //} if(isset($handle)) { while ($file = readdir($handle)) { if($file=='.'||$file=='..') continue; if(is_dir($dirname.$file)) i_get_files($dirname.$file.'/'); else $result_array[] = $dirname.$file; } closedir($handle); } return $result_array; } function i_strip_extentions($array) { // Runs through the extention array and // returns all files with the selected extentions. global $include_extentions; if(is_array($array)) { foreach ($include_extentions as $ext) { $str_len = strlen($ext); foreach ($array as $file) { if (substr($file, -$str_len) == $ext) $result_array[] = $file; } } return $result_array; } else return $array; } function i_strip_files($array) { // Reads the exclude file removed unwanted file form the array. // Filtering and regex added by: // Timo Haberkern 10/10/01 // Bug: 18/10: Array check. // Bug: 21/10/01: Type error, eregi returns int not bool. Causes probs on some systems. global $file_root, $exclude_files; if(is_array($array)) { $exclude = @file($exclude_files); if ($exclude) { // Create the filter lists foreach($exclude as $exc_file) { $exc_file = trim($exc_file); // Is it a filter? if ($exc_file[0] == "/") { $file[] = $exc_file; } else { $filter[] = $exc_file; } } // Do the filtering foreach ($array as $act_file) { $act_file = str_replace($file_root, "", $act_file); $bMatchedFilter = false; $bFoundInExcludingList = false; // Test the filters first if(is_array($filter)) { foreach ($filter as $curFilter) { if (eregi($curFilter, $act_file)) { $bMatchedFilter = true; break; } } } // Test for excluding if ($bMatchedFilter == false) { // Test only if the file list is not empty if (sizeof($file) != 0) { if (in_array($act_file, $file)) { $bFoundInExcludingList = true; break; } } } if (!$bFoundInExcludingList AND !$bMatchedFilter) $result_array[] = "$file_root$act_file"; } return $result_array; } else return $array; } else return $array; } function i_strip_words($line) { $file = @file($GLOBALS['exclude_words']); if ($file) { foreach ($file as $word) { $word = trim($word); $word = "|$word|"; $line = str_replace("$word", "|", $line); } } return $line; } function i_clear_index() { // Checks for a confirmation and then clears the index file. global $username, $password; if ($_POST['s'] == "submit") { $fd = @fopen($GLOBALS[index_file], "w") or die("

    $GLOBALS[err_index]

    "); fclose($fd); echo "

    $GLOBALS[index_cleared]

    "; } else { echo "
    You have selected to clear your site index.
    No search results will be found until you re-index your site.
    Are you sure?
    "; } } function i_view_index() { // Displays the index file in a table. if(file_exists($GLOBALS[index_file])) { $file = @file($GLOBALS[index_file]); if(is_array($file)) { echo "

    Your index file:

    "; foreach ($file as $key => $line) { $exp_line = explode("|", $line); $key = $key + 1; echo " \n"; foreach ($exp_line as $key => $word) { if ($key == 0) { echo " \n "; } else { echo ""; } } echo " \n\n"; } echo "
    # URL Keywords
    $key$word$word
    "; } else { echo "

    $GLOBALS[err_index_empty]

    "; } } else { echo "

    $GLOBALS[err_index]

    "; } } function i_print_options() { // Prints the indexer options. global $username, $password; echo "

    Indexer Options:
    Create Site Index
    View Site Index
    Clear Site Index
    "; } function i_print_logon($msg) { // Prints the indexer logon. echo "
    $msg

    Logon:
    Username:
    Password:
    "; } function c_strip_chars($line) { // Strips various characters from $line. // $line = str_replace(".", " ", $line); $line = str_replace("\"", " ", $line); $line = str_replace("'", "", $line); $line = str_replace("+", " ", $line); $line = str_replace("-", " ", $line); $line = str_replace("*", " ", $line); $line = str_replace("/", " ", $line); $line = str_replace("!", " ", $line); $line = str_replace("%", " ", $line); $line = str_replace(">", " ", $line); $line = str_replace("<", " ", $line); $line = str_replace("^", " ", $line); $line = str_replace("(", " ", $line); $line = str_replace(")", " ", $line); $line = str_replace("[", " ", $line); $line = str_replace("]", " ", $line); $line = str_replace("{", " ", $line); $line = str_replace("}", " ", $line); $line = str_replace("\\", " ", $line); $line = str_replace("=", " ", $line); $line = str_replace("$", " ", $line); $line = str_replace("#", " ", $line); $line = str_replace("?", " ", $line); $line = str_replace("~", " ", $line); $line = str_replace(":", " ", $line); $line = str_replace("_", " ", $line); $line = str_replace(" ", " ", $line); $line = str_replace("&", " ", $line); $line = str_replace("©", " ", $line); $line = str_replace(" ", " ", $line); $line = str_replace(""", " ", $line); $line = str_replace("ü", "ü", $line); $line = str_replace("Ü", "Ü", $line); $line = str_replace("&", " ", $line); $line = str_replace(";", " ", $line); $line = str_replace("\n", " ", $line); return $line; } // Display time taken. //if(isset($query)) { // if($show_time_search) { // $timeparts = explode(" ",microtime()); // $total_time = ($timeparts[1].substr($timeparts[0],1)) - $starttime; // echo "
    In: ".substr($total_time,0,4)." secs.
    "; // } //} //elseif (substr($QUERY_STRING,0,5) == "index") { // if($show_time_index) { // $timeparts = explode(" ",microtime()); // $total_time = ($timeparts[1].substr($timeparts[0],1)) - $starttime; // echo "
    In: ".substr($total_time,0,4)." secs.
    "; // } //} if(DEFINED("DEBUG")) { echo "\n\n
    \n

    Debug:

    Variables

    VariableValue
    HTTP_HOST: $HTTP_HOST
    SCRIPT_FILENAME: $SCRIPT_FILENAME
    SCRIPT_NAME: $SCRIPT_NAME
    PHP_SELF: $PHP_SELF
    file_root: $file_root
    http_root: $http_root
    index_file: $index_file
    exclude_words: $exclude_words
    exclude_files: $exclude_files

    Extra Checks

    Red = Error, see solution at end of line.
    Green = No problem.


    "; if(is_dir($file_root)) echo "$file_root is a directory."; else echo "$file_root is not a directory. Solution: Check \$file_root variable."; if(file_exists($index_file)) { echo "
    $index_file exists."; if(is_readable($index_file)) echo "
  • It is readable."; else echo "
  • It is not readable. Solution: Check that $index_file is readable by the webserver."; if(is_writable($index_file)) echo "
  • It is writable."; else echo "
  • It is not writable. Solution: Check that $index_file is writable by the webserver."; } else echo "
    $index_file does not exist. Solution: Check that \$index_file variable."; if(file_exists($exclude_words)) { echo "
    $$exclude_words exists."; if(is_readable($exclude_words)) echo "
  • It is readable."; else echo "
  • It is not readable. Solution: Check that $exclude_words is readable by the webserver."; } else echo "
    $exclude_words does not exist. Solution: Check that \$exclude_words variable"; if(file_exists($exclude_files)) { echo "
    $exclude_files exists."; if(is_readable($exclude_files)) echo "
  • It is readable."; else echo "
  • It is not readable. Solution: Check that $exclude_files is readable by the webserver."; } else echo "
    $exclude_files does not exist. Solution: Check \$exclude_files variable."; echo "
  • "; } ?>