/search/inc/search.inc.php
PHP | 1776 lines | 1448 code | 236 blank | 92 comment | 372 complexity | 6421fdcb131827d9811f4856a5ec165e MD5 | raw file
Possible License(s): BSD-3-Clause
Large files files are truncated, but you can click here to view the full file
- <?php
-
- /******************************************************************************
- * iSearch2 - website search engine *
- * *
- * Visit the iSearch homepage at http://www.iSearchTheNet.com/isearch *
- * *
- * Copyright (C) 2002-2007 Z-Host. All rights reserved. *
- * *
- ******************************************************************************/
-
- if ( !defined('IN_ISEARCH') )
- {
- die('Hacking attempt');
- }
-
- define('ISEARCH_OP_MUST', 1);
- define('ISEARCH_OP_MAY', 2);
- define('ISEARCH_OP_MUSTNOT', 3);
- define('ISEARCH_LINKS_ID_BASE', 0x40000000);
-
- /* Clear the iSearch search log file */
- function isearch_clearSearchLog()
- {
- global $isearch_table_search_log;
- global $isearch_db;
-
- mysql_query("DELETE FROM $isearch_table_search_log", $isearch_db);
- }
-
-
- /* Return the contents of the spider log */
- function isearch_getSearchLog()
- {
- global $isearch_table_search_log;
- global $isearch_ro_db;
-
- $log = '';
-
- $result = mysql_query("SELECT * FROM $isearch_table_search_log ORDER BY id", $isearch_ro_db);
- if ($result)
- {
- while ($item = mysql_fetch_object($result))
- {
- $log .= date('M d, Y, H:i:s - ', $item->time) . $item->search_term . " (" . $item->matches . " matches)\n";
- }
- }
-
- return $log;
- }
-
-
- /* Save the string in the iSearch log file */
- function isearch_searchLog($search_term, $matches)
- {
- global $isearch_table_search_log;
- global $isearch_config;
- global $isearch_table_info;
- global $isearch_db;
-
- if (($isearch_config['log_searches']) && (isset($isearch_db)))
- {
- $now = time();
- if (!mysql_query("INSERT INTO $isearch_table_search_log (search_term, time, matches) VALUES ('" . isearch_escape_string($search_term) . "', '$now', '$matches')", $isearch_db))
- {
- echo '<p>MySQL Error : ' . mysql_error() . ' File: ' . __FILE__ . ', Line:' . __LINE__. '</p>';
- }
-
- if ($isearch_config['search_log_email_days'] > 0)
- {
- $now = time();
- if ($isearch_config['search_log_last_emailed'] + ($isearch_config['search_log_email_days'] * 60 * 60 * 24) < $now)
- {
- /* Email the search log */
- $mailTo = $isearch_config['admin_email'];
- $mailSubject = "iSearch search log";
- $mailBody = "The following searches have been performed on your site:\n";
- $mailBody .= "\n";
- $mailBody .= isearch_getSearchLog();
- $mailBody .= "\n";
- $mailBody .= "This email is automatically generated by iSearch. To change how often\n";
- $mailBody .= "this email is sent, please use your iSearch admin control panel.\n";
- $mailBody .= "\n";
- $mailBody .= "Visit the iSearch home page at http://www.iSearchTheNet.com/isearch\n";
- $mailHeaders = "From: " . $isearch_config['admin_email'] . "\n";
- if (mail($mailTo, $mailSubject, $mailBody, $mailHeaders))
- {
- mysql_query("UPDATE $isearch_table_info SET search_log_last_emailed='$now'", $isearch_db);
- $isearch_config['search_log_last_emailed'] = $now;
- isearch_clearSearchLog();
- }
- }
- }
- }
- }
-
-
- /* Clean the search string
- * Parameters:
- * $data : Contains the string to clean
- */
- function isearch_cleanSearchString($data)
- {
- global $isearch_config;
-
- if ($isearch_config['char_set_8_bit'])
- {
- /* Convert to lower case, doing accented character conversion correctly */
- $data = strtr($data, 'ABCDEFGHIJKLMNOPQRSTUVWXYZ?ÁÂ?Ä??Ç?É?Ë?ÍÎ????ÓÔ?Ö×??Ú?ÜÝ?' . chr(0x8a) . chr(0x8e) ,
- 'abcdefghijklmnopqrstuvwxyz?áâ?ä??ç?é?ë?íî????óô?ö÷??ú?üý?' . chr(0x9a) . chr(0x9e) );
-
- /* Strip out all characters except numeric, alpha, whitespace or quote */
- $data = preg_replace('/([^-\.,@0-9a-z:\\.,' . chr(0xbf) . '-' . chr(0xff) . chr(0x9a) . chr(0x9e) . '\s"\+\-])/e', '', $data);
- }
- else
- {
-
- $data = isearch_stripslashes($data);
- }
-
-
-
-
-
-
- /* Ensure that quotes, + and - are surrounded by spaces */
- $data = ereg_replace('(["+])', ' \\1 ', $data);
-
- if (($isearch_config['allow_dashes'] == 0) || ($isearch_config['allow_dashes'] == 3))
- {
- /* Strip trailing +/- words */
- $data = ereg_replace('([+-]|[[:space:]])+$', '', $data);
-
- /* Replace repeated +/- words */
- $data = ereg_replace('([+-])([+-]|[[:space:]])+', ' \\1 ', $data);
-
- /* Ensure that dashes are surrounded by spaces */
- $data = ereg_replace('(-)', ' \\1 ', $data);
- }
- else
- {
- /* Strip trailing + words */
- $data = ereg_replace('([+]|[[:space:]])+$', '', $data);
-
- /* Replace repeated + words */
- $data = ereg_replace('([+])([+]|[[:space:]])+', ' \\1 ', $data);
- if ($isearch_config['allow_colons'] == 1)
- {
- /* Allow within words */
- $data = ereg_replace('([^ ])- ', '\\1 ', ereg_replace(' -([^ ])', ' \\1', " $data "));
- }
- }
-
- if ($isearch_config['allow_colons'] == 0)
- {
- /* Replace with spaces */
- $data = str_replace(':', ' ', $data);
- }
- else if ($isearch_config['allow_colons'] == 1)
- {
- /* Allow within words */
- $data = str_replace(' :', ' ', str_replace(': ', ' ', $data));
- }
- else if ($isearch_config['allow_colons'] == 3)
- {
- /* Remove All */
- $data = str_replace(':', '', $data);
- }
-
- if ($isearch_config['allow_dots'] == 0)
- {
- /* Replace with spaces */
- $data = str_replace('.', ' ', $data);
- }
- else if ($isearch_config['allow_dots'] == 1)
- {
- /* Allow within words */
- $data = str_replace(' .', ' ', str_replace('. ', ' ', $data));
- }
- else if ($isearch_config['allow_dots'] == 3)
- {
- /* Remove All */
- $data = str_replace('.', '', $data);
- }
-
- if ($isearch_config['allow_commas'] == 0)
- {
- /* Replace with spaces */
- $data = str_replace(',', ' ', $data);
- }
- else if ($isearch_config['allow_commas'] == 1)
- {
- /* Allow within words */
- $data = str_replace(' ,', ' ', str_replace(', ', ' ', $data));
- }
- else if ($isearch_config['allow_commas'] == 3)
- {
- /* Remove All */
- $data = str_replace(',', '', $data);
- }
-
- if ($isearch_config['allow_underscores'] == 0)
- {
- /* Replace with spaces */
- $data = str_replace('_', ' ', $data);
- }
- else if ($isearch_config['allow_underscores'] == 1)
- {
- /* Allow within words */
- $data = str_replace(' _', ' ', str_replace('_ ', ' ', $data));
- }
- else if ($isearch_config['allow_underscores'] == 3)
- {
- /* Remove All */
- $data = str_replace('_', '', $data);
- }
-
- /* Compact all white space into a single space character */
- $data = ereg_replace('[[:space:]]+', ' ', $data);
-
- /* Strip white space from beginning and end of the string */
- $data = trim($data);
-
- return $data;
- }
-
-
- /* Add a URL to the array of search results.
- */
- function isearch_addResult($id, $ranking)
- {
- global $isearch_resultArray;
- global $isearch_table_urls;
-
- global $isearch_groupQuery;
- global $isearch_ro_db;
- global $isearch_highestRank;
-
- if ($isearch_groupQuery != '')
- {
- $result = mysql_query("SELECT COUNT(id) FROM $isearch_table_urls WHERE id='$id' AND ( " . $isearch_groupQuery . " )", $isearch_ro_db);
- if (!$result)
- {
- echo "<p>MySQL error : " . mysql_error() . ' File: ' . __FILE__ . ', Line:' . __LINE__ . "</p>\n";
- }
- else
- {
- $total = mysql_result($result, 0);
- if ($total == 0)
- {
- /* Do not save this result */
- return;
- }
- }
- }
-
- if ($ranking > $isearch_highestRank)
- {
- $isearch_highestRank = $ranking;
- }
-
-
- $isearch_resultArray[] = array(
- 'id' => $id,
- 'ranking' => $ranking );
- }
-
-
- function isearch_resultArrayCmp($a, $b)
- {
- if ($a['ranking'] < $b['ranking'])
- {
- return 1;
- }
- if ($a['ranking'] > $b['ranking'])
- {
- return -1;
- }
-
- return 0;
- }
-
-
- function isearch_microtime()
- {
- list($usec, $sec) = explode(" ", microtime());
- return ((float)$usec + (float)$sec);
- }
-
-
- function isearch_find_suggestion($s)
- {
-
- global $isearch_suggestions;
- global $isearch_table_alts, $isearch_ro_db;
-
- $result = mysql_query("SELECT alternative, redirect FROM $isearch_table_alts WHERE keyword='".isearch_escape_string($s)."'", $isearch_ro_db);
- if (!$result)
- {
- echo "<p>MySQL error : " . mysql_error() . ' File: ' . __FILE__ . ', Line:' . __LINE__ . "</p>\n";
- }
- else
- {
- while ($item = mysql_fetch_object($result))
- {
- if ($item->redirect)
- {
- $s = $item->alternative;
- }
- else
- {
- $isearch_suggestions[] = array('keyword' => $s, 'alternative' => $item->alternative);
- }
- }
- }
-
-
- return $s;
- }
-
-
- /* Search the site for a string. Returns the number of matches found. */
- function isearch_find($searchString, $groups='', $partial=False, $soundex=False)
- {
- global $isearch_table_words, $isearch_table_urls, $isearch_table_links_words;
- global $isearch_ro_db;
- global $isearch_config;
- global $isearch_searchString;
- global $isearch_searchGroups;
- global $isearch_highlightWords;
- global $isearch_resultArray;
- global $isearch_search_time;
- global $isearch_search_partial;
- global $isearch_highestRank;
- global $isearch_suggestions;
- global $isearch_lang;
-
- global $isearch_groupQuery;
-
-
- $isearch_search_partial = $partial;
-
- $isearch_search_time = 0;
- $isearch_highestRank = 0;
-
- $isearch_searchString = $searchString;
- $isearch_searchGroups = $groups;
- $isearch_highlightWords = array();
- $isearch_suggestions = array();
-
- /* Clear the array of results */
- $isearch_resultArray = array();
-
- $maxResults = $isearch_config['max_pages'] * $isearch_config['results_per_page'];
-
- if ($searchString == '')
- {
- return 0;
- }
-
- $time_start = isearch_microtime();
-
- /* Arrays for storing ids and scores for each of the matches */
- $must = array();
- $may = array();
- $mustNot = array();
- $links = array();
-
- if ($isearch_config['search_all'])
- {
- $firstMust = True;
- $mustFlag = True;
- $defaultOp = ISEARCH_OP_MUST;
- }
- else
- {
- $defaultOp = ISEARCH_OP_MAY;
- $mustFlag = False;
- }
-
-
- $op = $defaultOp;
- $mysqlOp = $partial ? ' REGEXP ' : '=';
-
- $words = explode(' ', isearch_find_suggestion($isearch_searchString));
- $wordsCount = count($words);
-
- for ($i = 0; $i < $wordsCount; $i++)
- {
- if ($words[$i] == '+')
- {
- if (!$isearch_config['search_all'])
- {
- $op = ISEARCH_OP_MUST;
- $firstMust = False;
- if (!$mustFlag)
- {
- $firstMust = True;
- $mustFlag = True;
- }
- }
- }
- else if ($words[$i] == '-')
- {
- $op = ISEARCH_OP_MUSTNOT;
- }
- else if ($words[$i] == '"')
- {
-
- $quotedWords = '';
- for ($j = $i+1; $j < $wordsCount; $j++)
- {
- if ($words[$j] == '"')
- {
- break;
- }
-
- if (($words[$j] != '+') && ($words[$j] != '-'))
- {
- if ($quotedWords != '')
- {
- $quotedWords .= ' ';
- }
- $word = isearch_find_suggestion($words[$j]);
- $quotedWords .= $word;
-
- if ($op != ISEARCH_OP_MUSTNOT)
- {
- if (($word != '') && (!in_array($word, $isearch_config['stop_words'])) && (strlen($word) > $isearch_config['stop_words_length']))
- {
- $isearch_highlightWords[] = $word;
- }
- }
- }
- }
-
- /* If ($j >= $wordsCount) then the quote is not matched, so just ignore it */
- if (($j < $wordsCount) && ($j != ($i + 1)))
- {
- $quotedWords = isearch_find_suggestion($quotedWords);
- $result = mysql_query("SELECT id FROM $isearch_table_urls WHERE words REGEXP ' " . $quotedWords . " '", $isearch_ro_db);
- if (!$result)
- {
- echo "<p>MySQL error : " . mysql_error() . ' File: ' . __FILE__ . ', Line:' . __LINE__ . "</p>\n";
- }
- else
- {
- if ($op == ISEARCH_OP_MUSTNOT)
- {
- while ($item = mysql_fetch_object($result))
- {
- $mustNot[$item->id] = 0;
- }
- }
- else
- {
- $scores = array();
- while ($item = mysql_fetch_object($result))
- {
- $score = 0;
- for ($k = $i + 1; $k < $j; $k++)
- {
- $result2 = mysql_query("SELECT score FROM $isearch_table_words WHERE id='".$item->id."' AND word='" . $words[$k] . "'", $isearch_ro_db);
- if (!$result2)
- {
- echo "<p>MySQL error : " . mysql_error() . ' File: ' . __FILE__ . ', Line:' . __LINE__ . "</p>\n";
- }
- else
- {
- if ($item2 = mysql_fetch_object($result2))
- {
- $score += $item2->score;
- }
- }
- }
-
- $scores[$item->id] = $score;
- }
-
- if ($op == ISEARCH_OP_MUST)
- {
- if ($firstMust)
- {
- foreach (array_keys($scores) as $id)
- {
- $must[$id] = $scores[$id];
- }
- }
- else
- {
- foreach (array_keys($must) as $id)
- {
- /* Remove entries that are not in the scores list */
- if (!isset($scores[$id]))
- {
- unset($must[$id]);
- }
- else
- {
- /* Add the score */
- $must[$id] += $scores[$id];
- }
- }
- }
- }
- else
- {
- /* $op == ISEARCH_OP_MAY */
- foreach (array_keys($scores) as $id)
- {
- if (isset($may[$id]))
- {
- $may[$id] += $scores[$id];
- }
- else
- {
- $may[$id] = $scores[$id];
- }
- }
- }
- }
- }
- $i = $j;
- }
- $op = $defaultOp;
-
- }
- else
- {
- $word = isearch_find_suggestion($words[$i]);
- if ($op != ISEARCH_OP_MUSTNOT)
- {
- $isearch_highlightWords[] = $word;
- }
-
- /* Could optimise this to avoid storing scores for MUST NOT list */
- if ($soundex)
- {
- $result = mysql_query("SELECT id, score FROM $isearch_table_words WHERE SOUNDEX(word)${mysqlOp}SOUNDEX('" . isearch_escape_string($word) . "') ORDER BY score DESC LIMIT $maxResults", $isearch_ro_db);
- }
- else
- {
- $result = mysql_query("SELECT id, score FROM $isearch_table_words WHERE word$mysqlOp'" . isearch_escape_string($word) . "' ORDER BY score DESC LIMIT $maxResults", $isearch_ro_db);
- }
-
- if (!$result)
- {
- echo "<p>MySQL error : " . mysql_error() . ' File: ' . __FILE__ . ', Line:' . __LINE__ . "</p>\n";
- }
- else
- {
- if ($op == ISEARCH_OP_MUSTNOT)
- {
- while ($item = mysql_fetch_object($result))
- {
- $mustNot[$item->id] = 0;
- }
- }
- else if ($op == ISEARCH_OP_MUST)
- {
- if ($firstMust)
- {
- while ($item = mysql_fetch_object($result))
- {
- $must[$item->id] = $item->score;
- }
- }
- else
- {
- $scores = array();
- while ($item = mysql_fetch_object($result))
- {
- $scores[$item->id] = $item->score;
- }
-
- foreach (array_keys($must) as $id)
- {
- /* Remove entries that are not in the scores list */
- if (!isset($scores[$id]))
- {
- unset($must[$id]);
- }
- else
- {
- /* Add the score */
- $must[$id] += $scores[$id];
- }
- }
- }
- }
- else
- {
- /* $op == ISEARCH_OP_MAY */
- if (count($may) == 0)
- {
- while ($item = mysql_fetch_object($result))
- {
- $may[$item->id] = $item->score;
- }
- }
- else
- {
- while ($item = mysql_fetch_object($result))
- {
- if (isset($may[$item->id]))
- {
- $may[$item->id] += $item->score;
- }
- else
- {
- $may[$item->id] = $item->score;
- }
- }
- }
- }
- }
-
-
- /* Now check for links */
- if (($isearch_config['extra_link_display']) && ($op != ISEARCH_OP_MUSTNOT))
- {
- if ($soundex)
- {
- $result = mysql_query("SELECT id FROM $isearch_table_links_words WHERE SOUNDEX(word)${mysqlOp}SOUNDEX('" . isearch_escape_string($word) . "')", $isearch_ro_db);
- }
- else
- {
- $result = mysql_query("SELECT id FROM $isearch_table_links_words WHERE word$mysqlOp'" . isearch_escape_string($word) . "'", $isearch_ro_db);
- }
-
- if (!$result)
- {
- echo "<p>MySQL error : " . mysql_error() . ' File: ' . __FILE__ . ', Line:' . __LINE__ . "</p>\n";
- }
- else
- {
- /* $op == ISEARCH_OP_MAY */
- while ($item = mysql_fetch_object($result))
- {
- $links[] = $item->id;
- }
- }
- }
-
- $op = $defaultOp;
- $firstMust = False;
- }
- }
-
-
-
- $isearch_groupQuery = '';
-
- if ($groups != '')
- {
- $list = explode(',', $groups);
- for ($i = 0; $i < count($isearch_config['groups']); $i += 3)
- {
- if (in_array($isearch_config['groups'][$i], $list))
- {
- if ($isearch_config['groups'][$i+1] != '')
- {
- if ($isearch_groupQuery != '')
- {
- $isearch_groupQuery .= ' OR ';
- }
- $isearch_groupQuery .= "url REGEXP '^" . ereg_replace('\?', '\?', ereg_replace('\+', '\+', ereg_replace('\.', '\.', $isearch_config['groups'][$i+1]))) . "'";
- }
- if ($isearch_config['groups'][$i+2] != '')
- {
- if ($isearch_groupQuery != '')
- {
- $isearch_groupQuery .= ' OR ';
- }
- $isearch_groupQuery .= "url REGEXP '" . $isearch_config['groups'][$i+2] . "'";
- }
- }
- }
- }
-
-
- if ($mustFlag)
- {
- foreach (array_keys($must) as $id)
- {
- /* Ignore entries in the MUST NOT list */
- if (!isset($mustNot[$id]))
- {
- /* Add MAY scores */
- if (isset($may[$id]))
- {
- isearch_addResult($id, $must[$id] + $may[$id]);
- }
- else
- {
- isearch_addResult($id, $must[$id]);
- }
- }
- }
- }
- else
- {
- foreach (array_keys($may) as $id)
- {
- /* Ignore entries in the MUST NOT list */
- if (!isset($mustNot[$id]))
- {
- isearch_addResult($id, $may[$id]);
- }
- }
- }
-
-
- $linksRank = $isearch_highestRank + 1;
- if (count($links) > 0)
- {
- foreach ($links as $id)
- {
- isearch_addResult($id + ISEARCH_LINKS_ID_BASE, $linksRank);
- }
- }
-
-
- /* Sort the result array */
- usort($isearch_resultArray, 'isearch_resultArrayCmp');
-
- $numResults = count($isearch_resultArray);
-
- isearch_searchLog($isearch_searchString, $numResults);
-
- $time_end = isearch_microtime();
- $isearch_search_time = str_replace('.', $isearch_lang['decimal_point'], sprintf("%.3f", $time_end - $time_start));
-
- return $numResults;
- }
-
- function isearch_html_entities($str)
- {
- global $isearch_config;
-
- if ($isearch_config['char_set_8_bit'])
- {
- if (version_compare(PHP_VERSION, '4.1.0', '>='))
- {
- $str = @htmlentities($str, ENT_QUOTES, $isearch_config['char_set']);
- }
- else
- {
- $str = @htmlentities($str);
- }
- }
- else
- {
- if (version_compare(PHP_VERSION, '4.1.0', '>='))
- {
- $str = @htmlspecialchars($str, ENT_QUOTES, $isearch_config['char_set']);
- }
- else
- {
- $str = @htmlspecialchars($str);
- }
- }
-
- return $str;
- }
-
-
- function isearch_googleStyleDescription($resultIndex)
- {
- global $isearch_resultArray;
- global $isearch_table_urls;
- global $isearch_ro_db;
- global $isearch_config;
- global $isearch_highlightWords;
-
- $id = $isearch_resultArray[$resultIndex]['id'];
-
- if (count($isearch_highlightWords) == 0)
- {
- $result = mysql_query("SELECT description FROM $isearch_table_urls WHERE id='$id'", $isearch_ro_db);
- if (($result) && ($resultItem = mysql_fetch_object($result)))
- {
- return $resultItem->description;
- }
-
- /* No description available */
- return '';
- }
-
- $isearch_googleMinBefore = 5;
- $isearch_googleMinAfter = 10;
- $isearch_googleMaxWordCount = 32;
-
- $result = mysql_query("SELECT stripped_body FROM $isearch_table_urls WHERE id='$id'", $isearch_ro_db);
- if (($result) && ($resultItem = mysql_fetch_object($result)))
- {
- $description = $resultItem->stripped_body;
- if ($isearch_config['hide_regexp'] != '')
- {
- $description = ereg_replace($isearch_config['hide_regexp'], '', $description);
- }
-
- if ($isearch_config['replace_regexp'] != '')
- {
- $description = ereg_replace($isearch_config['replace_regexp'], ' ', $description);
- }
-
- $words = explode(' ', $description);
- }
- else
- {
- return '';
- }
-
- $wordsCount = count($words);
-
- $pos = array();
- /* Find the position of each word to highlight */
- for ($j = 0; $j < $wordsCount; $j++)
- {
- for ($i = 0; $i < count($isearch_highlightWords); $i++)
- {
- if (eregi($isearch_highlightWords[$i], $words[$j]))
- {
- $pos[] = $j;
- break; /* Avoid multiple entries in pos[] on same word */
- }
- }
- }
-
- if (count($pos) == 0)
- {
- /* This should never happen ! */
- $pos[] = 0;
- $isearch_googleMinAfter = 999;
- }
- else if (count($pos) == 1)
- {
- /* Increase the number of words displayed before and after the hit word */
- $isearch_googleMinBefore *= 2;
- $isearch_googleMinAfter = 999;
- }
-
- if ($isearch_config['char_set_8_bit'])
- {
- $trans = get_html_translation_table(HTML_ENTITIES);
- }
- else
- {
- }
-
- $wordCount = 0;
- $posCount = 0;
- $description = '';
-
- while (($wordCount < $isearch_googleMaxWordCount) && ($posCount < count($pos)))
- {
- $startPos = $pos[$posCount] - $isearch_googleMinBefore;
- if ($startPos < 0)
- {
- $startPos = 0;
- }
- $endPos = $pos[$posCount] + $isearch_googleMinAfter + 1;
-
- if (($posCount == 0) && ($startPos != 0))
- {
- $description = '... ';
- }
- for ($i = $startPos; $i < $endPos; $i++)
- {
- $wordCount ++;
- if (($wordCount > $isearch_googleMaxWordCount) || ($i >= $wordsCount))
- {
- break;
- }
- $lastWord = $i;
-
- $description .= isearch_html_entities($words[$i]) . ' ';
-
- /* Check whether this is a matched word */
- if (($isearch_config['highlight_results']) && ($posCount < count($pos)) && ($i == $pos[$posCount]))
- {
- $posCount ++;
-
- if ($i + $isearch_googleMinAfter + 1 > $endPos)
- {
- $endPos = $i + $isearch_googleMinAfter + 1;
- }
- }
-
- if (($posCount+1 < count($pos)) && ($i + $isearch_googleMinBefore >= $pos[$posCount+1]))
- {
- if ($pos[$posCount + 1] > $endPos)
- {
- $endPos = $pos[$posCount + 1];
- }
- }
- }
-
- $description .= '... ';
- }
-
- if ($wordCount < $isearch_googleMaxWordCount)
- {
- $i = $lastWord + 1;
- while (($wordCount < $isearch_googleMaxWordCount) && ($i < $wordsCount))
- {
- $wordCount ++;
- $description .= isearch_html_entities($words[$i]) . ' ';
- $i++;
- }
-
- $description .= '... ';
- }
-
- return $description;
- }
-
-
- function isearch_highlight($in)
- {
- global $isearch_highlightWords;
-
- $words = explode(' ', $in);
- $wordsCount = count($words);
-
- /* Search each word, and highlight */
- for ($j = 0; $j < $wordsCount; $j++)
- {
- for ($i = 0; $i < count($isearch_highlightWords); $i++)
- {
- if (eregi($isearch_highlightWords[$i], $words[$j]))
- {
- $words[$j] = '<span class="isearch-highlight">' . $words[$j] . '</span>';
- break;
- }
- }
- }
-
- return implode(' ', $words);
- }
-
-
- /* Truncate a string to a manimum length */
- function isearch_truncate($string, $length)
- {
- if (($length > 0) && (strlen($string) > $length + 3))
- {
- return substr($string, 0, $length) . '...';
- }
- return $string;
- }
-
-
- /* Show results of a site search */
- function isearch_showResults($pageNumber = 1)
- {
- global $isearch_table_urls, $isearch_table_links, $isearch_ro_db;
- global $isearch_searchString;
- global $isearch_searchGroups;
- global $isearch_resultArray;
- global $isearch_config;
- global $isearch_lang;
- global $isearch_version;
- global $isearch_search_time;
- global $isearch_search_partial;
- global $isearch_suggestions;
-
- $resultCount = count($isearch_resultArray);
-
- if ($resultCount > $isearch_config['max_pages'] * $isearch_config['results_per_page'])
- {
- $resultCount = $isearch_config['max_pages'] * $isearch_config['results_per_page'];
- }
-
- $maxPage = ceil($resultCount / $isearch_config['results_per_page']);
- if ($maxPage > $isearch_config['max_pages'])
- {
- $maxPage = $isearch_config['max_pages'];
- }
-
- if ($pageNumber < 1)
- {
- $pageNumber = 1;
- }
-
- if ($pageNumber > $maxPage)
- {
- $pageNumber = $maxPage;
- }
-
- $lastResult = $pageNumber * $isearch_config['results_per_page'];
- $firstResult = $lastResult - $isearch_config['results_per_page'] + 1;
- if ($lastResult > $resultCount)
- {
- $lastResult = $resultCount;
- }
- if ($firstResult < 0)
- {
- $firstResult = 0;
- }
-
- if ($isearch_config['char_set_8_bit'])
- {
- $trans = get_html_translation_table(HTML_ENTITIES);
- }
- else
- {
- $trans = get_html_translation_table(HTML_SPECIALCHARS);
- }
- $htmlSearchString = isearch_html_entities($isearch_searchString);
-
- $patterns = array(
- '/(%s)/',
- '/(%f)/',
- '/(%l)/',
- '/(%t)/',
- '/(%e)/');
-
- $replacements = array(
- '<span class="isearch-search">' . $htmlSearchString . '</span>',
- $firstResult,
- $lastResult,
- $resultCount,
- $isearch_search_time);
-
- foreach (array_keys($isearch_lang) as $key)
- {
- $isearch_lang_converted[$key] = preg_replace($patterns, $replacements, $isearch_lang[$key]);
- }
-
-
-
- $free_pro = 'Professional';
-
-
-
-
- $time = $isearch_config['show_time'] ? (' '.$isearch_lang_converted['results_head4']) : '';
- echo '
- <!-- Search results generated by iSearch2 version '.$isearch_version.' '.$free_pro.' -->
- <!-- Visit http://www.iSearchTheNet.com/isearch -->
- <table class="isearch-head">
- <tr class="isearch-head">
- <td class="isearch-head">'.$isearch_lang_converted['results_head2'].'</td>
- <td class="isearch-head" align="right">'.$isearch_lang_converted['results_head3'].$time.'</td>
- </tr>
- </table>
- <br />
- ';
-
- if (count($isearch_suggestions) > 0)
- {
- echo '
- <span class="isearch-suggest-title">' . $isearch_lang_converted['suggest_title'] . '</span>
- <br />
- ';
- $searchStrings = array();
- foreach ($isearch_suggestions as $suggestion)
- {
- $searchStrings[] = str_replace($suggestion['keyword'], $suggestion['alternative'], $isearch_searchString);
- }
-
- sort($searchStrings);
- $searchStrings = array_unique($searchStrings);
-
- foreach ($searchStrings as $searchString)
- {
- $href = $_SERVER['PHP_SELF'] . '?action=search&s=';
- if (strtolower($isearch_config['char_set']) == 'utf-8')
- {
- $href .= urlencode(utf8_encode($searchString));
- }
- else
- {
- $href .= urlencode($searchString);
- }
- $href .= '&group=' . urlencode($isearch_searchGroups);
- if ($isearch_search_partial)
- {
- $href .= '&partial=1';
- }
- echo '<a class="isearch-suggest" href="' . $href . '">' . $searchString . '</a>
- ';
- }
- echo '<br />
- ';
- }
-
- if ($resultCount > 0)
- {
- if ($isearch_config['prevnext_type'] == 0)
- {
- $prevNext = '';
- }
- else if (($isearch_config['prevnext_type'] > 3) || ($maxPage > 1))
- {
- /* If more than one results page, display "previous" and/or "next" links */
- $href = $_SERVER['PHP_SELF'] . '?action=search&s=';
- if (strtolower($isearch_config['char_set']) == 'utf-8')
- {
- $href .= urlencode(utf8_encode($isearch_searchString));
- }
- else
- {
- $href .= urlencode($isearch_searchString);
- }
- $href .= '&group=' . urlencode($isearch_searchGroups);
- if ($isearch_search_partial)
- {
- $href .= '&partial=1';
- }
- $prevNext = '
- <table class="isearch-prevnext" border="0" width="100%">
- <col width="10%" />
- <col width="80%" />
- <col width="10%" />
- <tr class="isearch-prevnext">
- <td class="isearch-prevnext" style="text-align: left">
- ';
- if ($pageNumber > 1)
- {
- $prevPageNumber = $pageNumber - 1;
- $prevNext .= ' <a class="isearch-prevnext" href="'.$href.'&page='.$prevPageNumber.'">'.$isearch_lang_converted['previous'].'</a>';
- }
-
- $prevNext .= '
- </td>
- <td class="isearch-prevnext" style="text-align: center">
- ';
-
- $startPage = $pageNumber - $isearch_config['prevnext_num'] + 1;
- if ($startPage < 1)
- {
- $startPage = 1;
- }
- $endPage = $pageNumber + $isearch_config['prevnext_num'] - 1;
- if ($endPage > $maxPage)
- {
- $endPage = $maxPage;
- }
-
- if ($isearch_config['prevnext_num'] != 0)
- {
- if ($startPage > 1)
- {
- $prevNext .= '
- <a class="isearch-prevnext" href="'.$href.'&page=1">1</a>
- ';
- if ($startPage > 2)
- {
- $prevNext .= ' ...';
- }
- }
- $prevNext .= ' ';
- for ($i = $startPage; $i <= $endPage; $i++)
- {
- if ($i == $pageNumber)
- {
- $prevNext .= $i.' ';
- }
- else
- {
- $prevNext .= '
- <a class="isearch-prevnext" href="'.$href.'&page='.$i.'">'.$i.'</a>
- ';
- }
- }
- if ($endPage < $maxPage)
- {
- if ($endPage < ($maxPage - 1))
- {
- $prevNext .= '... ';
- }
- $prevNext .= '
- <a class="isearch-prevnext" href="'.$href.'&page='.$maxPage.'">'.$maxPage.'</a>
- ';
- }
- }
- $prevNext .= '
- </td>
- <td class="isearch-prevnext" style="text-align: right">
- ';
-
- if ($pageNumber < $maxPage)
- {
- $nextPageNumber = $pageNumber + 1;
- $prevNext .= '
- <a class="isearch-prevnext" href="'.$href.'&page='.$nextPageNumber.'">'.$isearch_lang_converted['next'].'</a> ';
- }
- $prevNext .= '
- </td>
- </tr>
- </table>
- <br />
- ';
- }
- else
- {
- $prevNext = '
- <table class="isearch-prevnext">
- <tr class="isearch-prevnext">
- <td class="isearch-prevnext"> </td>
- </tr>
- </table>
- <br />
- ';
- }
-
- $prevNextBefore = '';
- $prevNextAfter = '';
- switch ($isearch_config['prevnext_type'])
- {
- case 1:
- case 4:
- $prevNextBefore = $prevNext;
- break;
- case 2:
- case 5:
- $prevNextAfter = $prevNext;
- break;
- case 3:
- case 6:
- $prevNextAfter = $prevNext;
- $prevNextBefore = $prevNext;
- break;
- }
-
- echo $prevNextBefore;
- for ($i = $firstResult-1; $i < $lastResult; $i++)
- {
- $id = $isearch_resultArray[$i]['id'];
-
- $lastIsLink = isset($isLink) ? $isLink : False;
-
- if ($id >= ISEARCH_LINKS_ID_BASE)
- {
- $id -= ISEARCH_LINKS_ID_BASE;
- $result = mysql_query("SELECT url, title, description FROM $isearch_table_links WHERE id='$id'", $isearch_ro_db);
- $isLink = True;
- }
- else
-
- {
- $result = mysql_query("SELECT url, title, description, size FROM $isearch_table_urls WHERE id='$id'", $isearch_ro_db);
- $isLink = False;
- }
-
- if (!$result)
- {
- echo "<p>MySQL error : " . mysql_error() . ' File: ' . __FILE__ . ', Line:' . __LINE__ . "</p>\n";
- }
- else if ($item = mysql_fetch_object($result))
- {
- /* Use google style results if explicitly set, or if there is no
- * description for this page
- */
- if ($isearch_config['description_style'] == 0)
- {
- $description = '';
- }
- else if ($isLink)
- {
- $description = $item->description;
- }
- else if (($isearch_config['description_style'] == 2) || ($item->description == ''))
- {
- /* Use Google style */
- $description = isearch_googleStyleDescription($i);
- }
- else
- {
- /* Either meta (1) or meta then google (3) or google then meta (4) */
-
- /* Process meta description */
- $description = $item->description;
- if ($isearch_config['hide_regexp'] != '')
- {
- $description = ereg_replace($isearch_config['hide_regexp'], '', $description);
- }
-
- if ($isearch_config['replace_regexp'] != '')
- {
- $description = ereg_replace($isearch_config['replace_regexp'], ' ', $description);
- }
-
- if ($isearch_config['description_style'] == 3)
- {
- /* Meta then google */
- $description = isearch_truncate($description, floor($isearch_config['max_displayed_description_length'] / 2));
- $description = $description . "<br />\n" . isearch_googleStyleDescription($i);
- }
- else if ($isearch_config['description_style'] == 4)
- {
- /* google then meta */
- $description = isearch_truncate($description, floor($isearch_config['max_displayed_description_length'] / 2));
- $description = isearch_googleStyleDescription($i) . "<br />\n" . $description;
- }
- }
-
- $description = isearch_truncate($description, $isearch_config['max_displayed_description_length']);
- if ($isearch_config['highlight_results'])
- {
- $description = isearch_highlight($description);
- }
-
-
- if ($isearch_config['extra_link_display'] == 1)
- {
- if (($lastIsLink) && (!$isLink))
- {
- /* Seperate links from non-links */
- echo '</div> ';
- }
- else if ((!$lastIsLink) && ($isLink))
- {
- /* Display before first link on page */
- echo '<div class="isearch-link-all">';
- }
- if ($isLink)
- {
- echo '<div class="isearch-link">';
- }
- }
-
- if ($isearch_config['show_title'])
- {
- $title = isearch_truncate($item->title, $isearch_config['max_displayed_title_length']);
- if ($isearch_config['highlight_results'])
- {
- $title = isearch_highlight($title);
- }
- echo '<a class="isearch-title" href="' . $item->url . '" target="' . $isearch_config['target_frame'] . '">' . $title . '</a>';
- if (($isearch_config['match_score'] == 1) && (($isearch_resultArray[0]['ranking']) > 0))
- {
- /* Show percentage */
- $percent = ceil(($isearch_resultArray[$i]['ranking'] * 100) / $isearch_resultArray[0]['ranking']);
- echo '<span class="isearch-score"> ['.$percent.' %]</span> ';
- }
- else if (($isearch_config['match_score'] == 2) && (($isearch_resultArray[0]['ranking']) > 0))
- {
- /* Show out of ten */
- $outoften = ceil(($isearch_resultArray[$i]['ranking'] * 10) / $isearch_resultArray[0]['ranking']);
- echo '<span class="isearch-score"> ['.$outoften.'/10]</span> ';
- }
- echo "<br />\n";
- }
- if (strlen($description) > 0)
- {
- echo '<span class="isearch-description">' . $description . "</span><br />\n";
- }
- if ($isearch_config['max_displayed_url_length'] > 1)
- {
- if ($isearch_config['display_strip_query'])
- {
- $displayedUrl = ereg_replace('\\?.*$', '', $item->url);
- }
- else
- {
- $displayedUrl = $item->url;
- }
-
- if (strlen($displayedUrl) > $isearch_config['max_displayed_url_length'])
- {
- $displayedUrl = substr($displayedUrl, 0, ($isearch_config['max_displayed_url_length'] / 2) - 3) . '...' .
- substr($displayedUrl, -($isearch_config['max_displayed_url_length'] / 2));
- }
-
- echo '<a class="isearch-url" href="' . $item->url . '" target="' . $isearch_config['target_frame'] . '">' . $displayedUrl . '</a>';
- }
- if (($isearch_config['show_size']) && (isset($item->size)))
- {
- echo '<span class="isearch-size"> - ' . ceil($item->size / 1024) . 'k';
- if (($isearch_config['keep_cache']) && (!$isLink))
- {
- echo ' - ';
- }
- echo '</span> ';
- }
- if (($isearch_config['keep_cache']) && (!$isLink))
- {
- echo '<a class="isearch-viewcache" href="viewcache.php?url=' . urlencode($item->url) . '">' . $isearch_lang_converted['cached'] . '</a>';
- }
- echo '<br />';
- if (($isearch_config['extra_link_display'] == 1) && ($isLink))
- {
- echo '</div> ';
- }
- echo "<br />\n";
- }
- }
- echo "<br />\n";
- echo $prevNextAfter;
- }
- else
- {
- echo '<h2 class="isearch-nomatch">'.$isearch_lang_converted['nomatch'].'</h2><br /><br />';
- }
-
-
- if (! $isearch_config['hide_powered_by'])
-
- {
- echo '<span style="font-size: 80%">Powered by <a href="http://www.iSearchTheNet.com/isearch" target="_blank">iSearch2</a> from <a href="http://www.z-host.com/" target="_blank">Z-Host</a></span><br />';
- }
- }
-
-
- function isearch_getSuggestionsArray()
- {
- global $isearch_searchString;
- global $isearch_suggestions;
-
- $searchStrings = array();
-
- if (count($isearch_suggestions) > 0)
- {
- $searchStrings = array();
- foreach ($isearch_suggestions as $suggestion)
- {
- $searchStrings[] = str_replace($suggestion['keyword'], $suggestion['alternative'], $isearch_searchString);
- }
-
- sort($searchStrings);
- $searchStrings = array_unique($searchStrings);
- }
-
- return $searchStrings;
- }
-
- function isearch_getResultCount()
- {
- global $isearch_resultArray;
-
- return count($isearch_resultArray);
- }
-
- function isearch_getResultArray($firstResult=1, $lastResult=1000)
- {
- global $isearch_table_urls, $isearch_table_links, $isearch_ro_db;
- global $isearch_resultArray;
- global $isearch_config;
-
- $resultCount = count($isearch_resultArray);
-
- if ($lastResult > $resultCount)
- {
- $lastResult = $resultCount;
- }
- if ($firstResult < 0)
- {
- $firstResult = 0;
- }
-
- $resultArray = array();
- for ($i = $firstResult-1; $i < $lastResult; $i++)
- {
- $id = $isearch_resultArray[$i]['id'];
-
- if ($id >= ISEARCH_LINKS_ID_BASE)
- {
- $id -= ISEARCH_LINKS_ID_BASE;
- $result = mysql_query("SELECT url, title, description FROM $isearch_table_links WHERE id='$id'", $isearch_ro_db);
- $isLink = True;
- }
- else
- {
- $result = mysql_query("SELECT url, title, description, size FROM $isearch_table_urls WHERE id='$id'", $isearch_ro_db);
- $isLink = False;
- }
-
- if (!$result)
- {
- echo "<p>MySQL error : " . mysql_error() . ' File: ' . __FILE__ . ', Line:' . __LINE__ . "</p>\n";
- }
- else if ($item = mysql_fetch_object($result))
- {
- /* Use google style results if explicitly set, or if there is no
- * description for this page
- */
- if ($isearch_config['description_style'] == 0)
- {
- $description = '';
- }
- else if ($isLink)
- {
- $description = $item->description;
- }
- else if (($isearch_config['description_style'] == 2) || ($item->description == ''))
- {
- /* Use Google style */
- $description = isearch_googleStyleDescription($i);
- }
- else
- {
- /* Either meta (1) or meta then google (3) or google then meta (4) */
-
- /* Process meta description */
- $description = $item->description;
- if ($isearch_config['hide_regexp'] != '')
- {
- $description = ereg_replace($isearch_config['hide_regexp'], '', $description);
- }
-
- if ($isearch_config['replace_regexp'] != '')
- {
- $description = ereg_replace($isearch_config['replace_regexp'], ' ', $description);
- }
-
- if ($isearch_config['description_style'] == 3)
- {
- /* Meta then google */
- $description = isearch_truncate($description, floor($isearch_config['max_displayed_description_length'] / 2));
- $description = $description . "<br />\n" . isearch_googleStyleD…
Large files files are truncated, but you can click here to view the full file