📄 search_function.php
字号:
<?php/*----------------------------------------------------------------------------------PhpDig Version 1.8.x - See the config file for the full version number.This program is provided WITHOUT warranty under the GNU/GPL license.See the LICENSE file for more information about the GNU/GPL license.Contributors are listed in the CREDITS and CHANGELOG files in this package.Developer from inception to and including PhpDig v.1.6.2: Antoine BajoletDeveloper from PhpDig v.1.6.3 to and including current version: CharterCopyright (C) 2001 - 2003, Antoine Bajolet, http://www.toiletoine.net/Copyright (C) 2003 - current, Charter, http://www.phpdig.net/Contributors hold Copyright (C) to their code submissions.Do NOT edit or remove this copyright or licence information upon redistribution.If you modify code and redistribute, you may ADD your copyright to this notice.----------------------------------------------------------------------------------*///===============================================// do the search and display the results// can be called in any pagefunction phpdigSearch($id_connect, $query_string, $option='start', $refine=0, $refine_url='', $lim_start=0, $limite=10, $browse=0, $site=0, $path='', $relative_script_path = '.', $template='', $adlog_flag=0, $rssdf='', $template_demo=''){// check input// $id_connect set in connect.php file// $query_string cleaned in $query_to_parse in search_function.php fileif (($option != "start") && ($option != "any") && ($option != "exact")) { $option = SEARCH_DEFAULT_MODE; }if (($refine != 0) && ($refine != 1)) { $refine = 0; }// $refine_url set in search_function.php file// $lim_start set in search_function.php filesettype($limite,'integer');if (($limite != 10) && ($limite != 30) && ($limite != 100)) { $limite = SEARCH_DEFAULT_LIMIT; }settype($limit_start,'integer');if (isset($limit_start)) { $limit_start = $limite * floor($limit_start / $limite); }if (($browse != 0) && ($browse != 1)) { $browse = 0; }if (eregi("^[0-9]+[,]",$site)) { $tempbust = explode(",",$site); $site = $tempbust[0]; $path = $tempbust[1]; }settype($site,'integer'); // now set to integersettype($path,'string'); // make sure set to stringif (!get_magic_quotes_gpc()) { $my_path = addslashes($path); }else { $my_path = addslashes(stripslashes($path)); $path = stripslashes($path); }if (empty($site) && empty($path)) { $refine = 0; } else { $refine = 1; }if ($path == "-###-") { $site = 0; $path = ""; $refine = 0; }// $relative_script_path set in search.php file// $template set in config.php file// $adlog_flag set in search.php file// $rssdf set in search.php file// $template_demo set in config.php$timer = new phpdigTimer('html');$timer->start('All');// init variablesglobal $phpdig_words_chars;settype($maxweight,'integer');$ignore = '';$ignore_common = '';$ignore_message = '';$ignore_commess = '';$wheresite = '';$wherepath = '';$table_results = '';$final_result = '';$search_time = 0;$strings = '';$num_tot = 0;$leven_final = "";$exclude = array();$nav_bar = '';$pages_bar = '';$mtime = explode(' ',microtime());$start_time = $mtime[0]+$mtime[1];$timer->start('All backend');$timer->start('parsing strings');if (!$option) { $option = SEARCH_DEFAULT_MODE;}if (!in_array($option,array('start','any','exact'))) { return 0;}// the query was filledif ($query_string) {$common_words = phpdigComWords("$relative_script_path/includes/common_words.txt");$like_start = array( "start" => "", // is empty "any" => "", // was a percent "exact" => "" // is empty );$like_end = array( "start" => "%", // is a percent "any" => "%", // is a percent "exact" => "%" // was empty );$like_operator = array( "start" => "like", // is a like "any" => "like", // is a like "exact" => "like" // was an = );if ($refine) { $wheresite = "AND spider.site_id = $site "; if (($path) && (strlen($path) > 0)) { $wherepath = "AND spider.path like '$my_path' "; } $refine_url = "&refine=1&site=$site&path=".urlencode($path);}else { $refine_url = "";}settype ($lim_start,"integer");if ($lim_start < 0) { $lim_start = 0;}$n_words = count(explode(" ",$query_string));$ncrit = 0;$tin = "0";if (!get_magic_quotes_gpc()) { $query_to_parse = addslashes($query_string);}else { $query_to_parse = $query_string;}$my_query_string_link = stripslashes($query_to_parse);$query_to_parse = str_replace('_','\_',$query_to_parse); // avoid '_' in the query$query_to_parse = str_replace('%','\%',$query_to_parse); // avoid '%' in the query$query_to_parse = str_replace('\"',' ',$query_to_parse); // avoid '"' in the query$query_to_parse = phpdigStripAccents(strtolower($query_to_parse)); //made all lowercase// RH query_chars = "[^".$phpdig_words_chars[PHPDIG_ENCODING]." \'.\_~@#$:&\%/;,=-]+"; // epure chars \'._~@#$:&%/;,=-$what_query_chars = "[^".$phpdig_words_chars[PHPDIG_ENCODING]." \'._~@#$&%/=-]+"; // epure chars \'._~@#$&%/=-if (eregi($what_query_chars,$query_to_parse)) { $query_to_parse = eregi_replace($what_query_chars," ",$query_to_parse);}$query_to_parse = ereg_replace('(['.$phpdig_words_chars[PHPDIG_ENCODING].'])[\'.\_~@#$:&\%/;,=-]+($|[[:space:]]$|[[:space:]]['.$phpdig_words_chars[PHPDIG_ENCODING].'])','\1 \2',$query_to_parse);$query_to_parse = trim(ereg_replace(" +"," ",$query_to_parse)); // no more than 1 blank$query_for_strings = $query_to_parse;$query_for_phrase = $query_to_parse;$test_short = $query_to_parse;$query_to_parse2 = explode(" ",$query_to_parse);usort($query_to_parse2, "phpdigByLength");$query_to_parse = implode(" ",$query_to_parse2);if (isset($query_to_parse2)) { unset($query_to_parse2); }if (SMALL_WORDS_SIZE >= 1) {$ignore_short_flag = 0;$test_short_counter = 0;$test_short2 = explode(" ",$test_short);for ($i=0; $i<count($test_short2); $i++) { $test_short2[$i] = trim($test_short2[$i]);}$test_short2 = array_unique($test_short2);sort($test_short2);$test_short3 = array();for ($i=0; $i<count($test_short2); $i++) { if ((strlen($test_short2[$i]) <= SMALL_WORDS_SIZE) && (strlen($test_short2[$i]) > 0)) { $test_short2[$i].=" "; $test_short_counter++; $test_short3[] = $test_short2[$i]; }}$test_short = implode(" ",$test_short3);if (isset($test_short2)) { unset($test_short2); }if (isset($test_short3)) { unset($test_short3); } $regs = array(); // for use with ereg() while (ereg('( [^ ]{1,'.SMALL_WORDS_SIZE.'} )|( [^ ]{1,'.SMALL_WORDS_SIZE.'})$|^([^ ]{1,'.SMALL_WORDS_SIZE.'} )',$test_short,$regs)) { for ($n=1; $n<=3; $n++) { if (($regs[$n]) || ($regs[$n] == 0)) { $ignore_short_flag++; if (!eregi("\"".trim(stripslashes($regs[$n]))."\", ",$ignore)) { $ignore .= "\"".trim(stripslashes($regs[$n]))."\", "; } $test_short = trim(str_replace($regs[$n],"",$test_short)); } } } if (strlen($test_short) <= SMALL_WORDS_SIZE) { if (!eregi("\"".$test_short."\", ",$ignore)) { $ignore_short_flag++; $ignore .= "\"".stripslashes($test_short)."\", "; } $test_short = trim(str_replace($test_short,"",$test_short)); }}$ignore = str_replace("\"\", ","",$ignore);if ($option != "exact") { if (($ignore) && ($ignore_short_flag > 1) && ($test_short_counter > 1)) { $ignore_message = $ignore.' '.phpdigMsg('w_short_plur'); } elseif ($ignore) { $ignore_message = $ignore.' '.phpdigMsg('w_short_sing'); }}$ignore_common_flag = 0;while (ereg("(-)?([^ ]{".(SMALL_WORDS_SIZE+1).",}).*",$query_for_strings,$regs)) { $query_for_strings = trim(str_replace($regs[2],"",$query_for_strings)); if (!isset($common_words[stripslashes($regs[2])])) { if ($regs[1] == '-') { $exclude[$ncrit] = $regs[2]; $query_for_phrase = trim(str_replace("-".$regs[2],"",$query_for_phrase)); } else { $strings[$ncrit] = $regs[2]; } $kconds[$ncrit] = ''; if ($option != 'any') { $kconds[$ncrit] .= " AND k.twoletters = '".addslashes(substr(str_replace('\\','',$regs[2]),0,2))."' "; } $kconds[$ncrit] .= " AND k.keyword ".$like_operator[$option]." '".$like_start[$option].$regs[2].$like_end[$option]."' "; $ncrit++; } else { $ignore_common_flag++; $ignore_common .= "\"".stripslashes($regs[2])."\", "; }}if ($option != "exact") { if (($ignore_common) && ($ignore_common_flag > 1)) { $ignore_commess = $ignore_common.' '.phpdigMsg('w_common_plur'); } elseif ($ignore_common) { $ignore_commess = $ignore_common.' '.phpdigMsg('w_common_sing'); }}$timer->stop('parsing strings');if ($ncrit && is_array($strings)) { $query = "SET OPTION SQL_BIG_SELECTS = 1"; mysql_query($query,$id_connect); $my_spider2site_array = array(); $my_sitecount_array = array(); for ($n = 0; $n < $ncrit; $n++) { $timer->start('spider queries'); $query = "SELECT spider.spider_id,sum(weight) as weight, spider.site_id FROM ".PHPDIG_DB_PREFIX."keywords as k,".PHPDIG_DB_PREFIX."engine as engine, ".PHPDIG_DB_PREFIX."spider as spider WHERE engine.key_id = k.key_id ".$kconds[$n]." AND engine.spider_id = spider.spider_id $wheresite $wherepath GROUP BY spider.spider_id,spider.site_id "; $result = mysql_query($query,$id_connect); $num_res_temp = mysql_num_rows($result); $timer->stop('spider queries'); $timer->start('spider fills'); if ($num_res_temp > 0) { if (!isset($exclude[$n])) { $num_res[$n] = $num_res_temp; while (list($spider_id,$weight,$site_id) = mysql_fetch_array($result)) { $s_weight[$n][$spider_id] = $weight; $my_spider2site_array[$spider_id] = $site_id; $my_sitecount_array[$site_id] = 0; } } else { $num_exclude[$n] = $num_res_temp; while (list($spider_id,$weight) = mysql_fetch_array($result)) { $s_exclude[$n][$spider_id] = 1; } mysql_free_result($result); } } elseif (!isset($exclude[$n])) { $num_res[$n] = 0; $s_weight[$n][0] = 0; } $timer->stop('spider fills'); } $timer->start('reorder results'); if ($option != "any") { if (is_array($num_res)) { asort ($num_res); list($id_most) = each($num_res); reset ($s_weight[$id_most]); while (list($spider_id,$weight) = each($s_weight[$id_most])) { $weight_tot = 1; reset ($num_res); while(list($n) = each($num_res)) { settype($s_weight[$n][$spider_id],'integer'); $weight_tot *= sqrt($s_weight[$n][$spider_id]); } if ($weight_tot > 0) { $final_result[$spider_id]=$weight_tot; } } } } else { if (is_array($num_res)) { asort ($num_res); while (list($spider_id,$site_id) = each($my_spider2site_array)) { $weight_tot = 0; reset ($num_res); while(list($n) = each($num_res)) { settype($s_weight[$n][$spider_id],'integer'); $weight_tot += sqrt($s_weight[$n][$spider_id]); } if ($weight_tot > 0) { $final_result[$spider_id]=$weight_tot; } } } } if (isset($num_exclude) && is_array($num_exclude)) { while (list($id) = each($num_exclude)) { while(list($spider_id) = each($s_exclude[$id])) { if (isset($final_result[$spider_id])) { unset($final_result[$spider_id]); } } } } if ($option == "exact") { if ((is_array($final_result)) && (count($final_result) > 0)) { $exact_phrase_flag = 0; arsort($final_result); reset($final_result); $query_for_phrase_array = explode(" ",$query_for_phrase); $reg_strings = str_replace('@#@',' ',phpdigPregQuotes(str_replace('\\','',implode('@#@',$query_for_phrase_array)))); $stop_regs = "[][(){}[:blank:]=&?!&#%\$
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -