我这里有一套PHP代码,有些地方看得不太懂,哪位一起来看看,共同演学习? ( 积分: 20 )

  • 主题发起人 主题发起人 七步子
  • 开始时间 开始时间

七步子

Unregistered / Unconfirmed
GUEST, unregistred user!
是一套搜索引擎的代码~~
search_results.tpl
你看看这个文件,好像它是最终当这个是一个模板,最终把里边的##title##等,替换了的,但我不知道它在哪里替换的~
search_results.tpl文件在在templates文件夹下!
所以我到现在还是看不懂它是怎么样搜索的,哪位看懂了请告诉我!
保持联糸,QQ:53663796,注明:搜索
 
是一套搜索引擎的代码~~
search_results.tpl
你看看这个文件,好像它是最终当这个是一个模板,最终把里边的##title##等,替换了的,但我不知道它在哪里替换的~
search_results.tpl文件在在templates文件夹下!
所以我到现在还是看不懂它是怎么样搜索的,哪位看懂了请告诉我!
保持联糸,QQ:53663796,注明:搜索
 
下载地址是:http://www.365y.cn/K-Search.rar
 
用 EditPlus 把所有PHP文件打开,搜一下 ##title## (在打开的文件中)
你就可以看到在哪里替换的了。
但如果它用了 Smarty 或 PHPLib 之类的模板引擎,你是看不出来的。
哪就搜 search_results.tpl
 
楼上的,按你的方法搜了。
搜到这个文件,do_search.php
但我还是看不懂它是怎么样搜内容的,请高抬贵鼠指教,无限感尽!谢谢!
内容如下:

<?
/* K-Search
(C) Rinalds Uzkalns, 2004. All Rights Reserved
http://turn-k.net
Version: 1.01 (12.08.2004)
*/
?>
<?
//time measurement function
function micro() {
return time()+microtime();
}
//wait for certain search engine to retrieve results
function wait_for($id,$key) {
global $times,$en;
while (!file_exists('cache/'.$id.'-'.$key)) {
$i++;
if (!$en['notimeout'])
if ((micro() - $times[$key]) > search_timeout) {return false;} //break in case of timeout
}
}
//format search term to be search engine friendly
function format_term($term) {
$term = str_replace('-','~~',$term);
return str_replace(' ','-',$term);
}
//clean term
$en['term'] = stripslashes($en['term']);
$en['term'] = str_replace("'",'"',$en['term']);
$en['term'] = str_replace("+",' ',$en['term']);
$en['term'] = str_replace(chr(92),' ',$en['term']);
$en['term'] = str_replace('/',' ',$en['term']);
while (substr($en['term'],0,1) == ' ') $en['term'] = substr($en['term'],1);
while (strpos($en['term'],' ') !== false) $en['term'] = str_replace(" ",' ',$en['term']);
while (substr($en['term'],strlen($en['term'])-1,1) == ' ') $en['term'] = substr($en['term'],-1);
//no search source - source = 1
if (!$en['source']) $en['source'] = 1;
//first page = search engine friendly
echo "<br>".$_GET['req'];
echo "<br>".$_GET['cache'];
echo "<br>".first_friendly;
exit;
if (first_friendly == 'Yes' &amp;&amp;
$_GET['req'] != 'xsearch' &amp;&amp;
$en['cache'] != 'no') {
ob_clean();
header('Location: '.constant('dir').'search/'.format_term($en['term']).'/'.$en['source'].'-1.html');
exit;
}
//page title = query
define('page_title',$en['term']);
//filter keywords
if (safe_search == 'Yes') {
$keys = $filtered = array();
$kres = mysql_query("SELECT * FROM $safetable");
while ($line = mysql_fetch_assoc($kres)) $keys[] = $line['s_word'];
$words = explode(' ',$en['term']);
foreach ($words as $key => $value)
if (array_search(strtolower($value),$keys) !== false) {
unset($words[$key]);
$filtered[] = $value;
}
$en['filtered'] = implode('</b>, <b>',$filtered);
$en['term'] = implode(' ',$words);
}
//spellcheck
if (use_spellcheck == 'Yes' &amp;&amp;
function_exists('pspell_new')) {
pspell_config_create("en");
$int = pspell_new("en", "", "", "",PSPELL_FAST);
$q = query_parse($en['term']);
//not for advanced search
if (count($q) < 2) {
$words = $wrds = preg_split('/[/W]+?/',$en['term']);
$misspelled = $return = array();
foreach ($words as $key => $value)
if (!pspell_check($int, $value))
$misspelled[$key] = $value;
foreach ($misspelled as $key => $value) {
$ret = pspell_suggest($int, $value);
$words[$key] = '<i><b>'.$ret[0].'</b></i>';
if ($ret[0] == '' || strtolower($ret[0]) == strtolower($value)) unset($misspelled[$key]);
$wrds[$key] = $ret[0];
$en['is_spellcheck'] = 1;
}
if (count($misspelled) > 0) {
$msp = implode(' ',$words);
$msp = str_replace('</b></i> <i><b>',' ',$msp);
$que = implode(' ',$wrds);
$en['spell_corrected'] = '<a href="'.constant('dir').'index.php?req=search&amp;source='.$en['source'].'&amp;term='.urlencode($que).'">'.$msp.'</a>';
}

}
}

//get search type and result page number
if (isset($en['offs']) &amp;&amp;
!is_numeric($en['offs'])) list($en['source'],$en['offs']) = explode('-',$en['offs']);
if ($en['offs'] == 0) $en['offs'] = 1;
if ($en['source'] == '') $en['source'] = 1;
//search requirements
$times = $rez = array();
$options['keys'] = urlencode($en['term']);
$options['limit'] = result_count;
//load results from cache?
if (cache_results == 'Yes' &amp;&amp;
$en['cache'] != 'no') {
$res = mysql_query("SELECT * FROM $cachetable WHERE c_query='$en[term]'"
." AND c_source='$en[source]'"
) or die(mysql_error());
if (mysql_num_rows($res) > 0)
$cache = mysql_fetch_array($res);
}

//leave source search engines only
foreach ($sites as $key => $value)
if (@constant($key.'_cat') != $en['source'] &amp;&amp;
!$value['spons'])
unset($sites[$key]);
//get results
$search_st = micro();
if (!isset($cache)) {
$queries = queries($options);
if (cache_results == 'Yes' &amp;&amp;
$en['cache'] != 'no') {
mysql_query("INSERT INTO $cachetable (c_query, c_time"
.",c_source"
.") VALUES ('$en[term]', ".time()
.",$en[source]"
.")") or die(mysql_error());
$id = mysql_insert_id();
} else
{
$id = rand(0,100000000);
}
function execInBackground($path, $exe, $args = "") {
global $conf,$par;
$pth = getcwd();
@chdir($path);
if (substr(php_uname(), 0, 7) == "Windows"){
$p = "start /"Title/" /"".path_to_php."/" /"" . $exe . "/" " . $args;
pclose(popen($p, "r"));
} else
{
$q = path_to_php." '".$exe."' '".$par."' >&amp;- <&amp;- >/dev/null &amp;";
exec($q);
}
chdir($pth);
}
//exec search threads
foreach ($sites as $key => $value) {
$times[$key] = micro();
$params = array();
$params['query'] = $queries[$key];
$params['cache'] = "cache/".$id.'-'.$key;
$par = base64_encode(serialize($params));
execinbackground(dirname(path_to_php),getcwd().'/get.php', $par);
}
//wait for results
foreach ($sites as $key => $s) {
wait_for($id,$key);
}
//get retrieved results + save in cache
foreach ($sites as $key => $s) {
$file = "cache/".$id.'-'.$key;
$f = @fopen($file,'r');
if (!$f) { continue;
}
$s = '';
while (!feof($f)) $s .= fgets($f);
fclose($f);
unset($f);
unlink($file);
if (cache_results == 'Yes' &amp;&amp;
$en['cache'] != 'no')
mysql_query("UPDATE $cachetable SET c_{$key}='".mysql_escape_string($s)."' WHERE c_id=$id") or die(mysql_error());
$cache['c_'.$key] = $s;
}
}
//get detailed times needed for each engine
$grr = @fopen('cache/'.$id,'r');
if ($grr) {
$s = fread($grr,4096);
fclose($grr);
$en['search_detail_times'] = str_replace(', ',"/n",$s);
$en['search_time'] = round(micro()-$search_st,4);
@unlink('cache/'.$id);
}
//parse results
foreach ($sites as $key => $value)
$rez[$key]['res'] = check_ranking($key,$options);
//query for related queries
$wrd = $en['term'];
while (strpos($wrd,' ') !== false) $wrd = str_replace(' ',' ',$wrd);
$wrd = str_replace('"','',$wrd);
$words = explode(' ',$wrd);
$en['rel'] = '';
foreach ($words as $c => $word)
if (substr($word,0,1) != '-') {
$en['rel'] .= "AND (q_query REGEXP '(^|[^a-zA-Z])".$word."([^a-zA-Z]|$)')";
$terms[] = $word;
}
$en['rel'] = substr($en['rel'],4);
if ($en['rel'] != '') {
$en['spons'] = str_replace('AND','OR',$en['rel']);
$en['rel'] = '('.$en['rel'].") AND (q_query <> '$wrd')";
}

global $relateds;
$relateds = array();
//suggestions based on previous searches
if (related_ext == 'Yes') {
//find the best available suggestions
$maxq = 0;
foreach ($rez as $key => $value)
if ($value['res']['related_q'] > $maxq) {
$maxq = $value['res']['related_q'];
$rel = $value['res']['related'];
}
}
if (related_ext != 'Yes' || $maxq == 0) {
$res = mysql_query("SELECT * FROM $querytable WHERE ".$en['rel']." ORDER BY q_times DESC LIMIT 20");
while ($line = mysql_fetch_assoc($res)) {
$rel[] = $line['q_query'];
}
}
//prepare suggestions for displaying
if (is_array($rel))
foreach ($rel as $c => $value) {
$relateds[]['show'] = $value;
if (count($relateds) > 20) break;
}
//chop slashes off the end of URL's
foreach ($rez as $key => $value)
if (is_array($value['res']) &amp;&amp;
!($sites[$key]['spons']))
foreach ($value['res'] as $r => $s)
if (substr($s['site'],-1) == '/') $rez[$key]['res'][$r]['site'] = substr($s['site'],0,-1);
//process search results
$out = array();
foreach ($rez as $key => $value) {
if (is_array($value['res']) &amp;&amp;
!($sites[$key]['spons']))
foreach ($value['res'] as $r => $s)
if (is_numeric($r)) {
$pr = $s['site'];
//remove www
$s['site'] = str_replace('http://www.','http://',$s['site']);
if ($s['site'] != $pr) {$www[$s['site']] = true;}
//add points
$out[$s['site']] += ($options['limit']+1-$r);
//register the rank within search engine
$engines_[$s['site']][$key] = $r;
//assign title and description
$s['title'] = strip_tags($s['title']);
if ($s['des']) $des[$s['site']] = $s['des'];
$title[$s['site']] = $s['title'];
//assign rank
$found[$s['site']][$key] = $r;
}
}
arsort($out);

//register queries if not advanced search
$reg = true;
$t = strtolower($en['term']);
if (strpos($t,'"') || strpos($t,' or ') || strpos($t,' and ') || strpos($t,' not ') || strpos($t,' -')) $reg = false;
if ($en['req'] != 'do_advanced' &amp;&amp;
$reg) {
if (count($out) > 0) {
mysql_query("UPDATE $querytable SET q_times=q_times+1 WHERE q_query='$en[term]'") or die(mysql_error());
if (mysql_affected_rows() == 0)
mysql_query("INSERT INTO $querytable (q_query) VALUES ('$en[term]')") or die(mysql_error());
}
}
//max number of points
$sitecnt = 0;
foreach ($rez as $key => $value) {
unset($rez[$key]['res']['title']);
if (is_array($value['res']) &amp;&amp;
!($sites[$key]['spons']))
$sitecnt++;
}
$max = $sitecnt * $options['limit'];
if ($max == 0) $max = 1;
//sponsored results set
foreach ($sites as $key => $value)
if ($value['spons'])
if (is_array($rez[$key]['res'])) {
foreach ($rez[$key]['res'] as $k => $v)
$en['sprez']['res'][] = $v;
}
//prepare results set
$rez = array();
foreach ($out as $key => $value) {
$p = count($rez)+1;
$rez[$p]['url'] = $key;
$rez[$p]['points'] = round(100 * ($value/$max));
$rez[$p]['title'] = $title[$key];
$rez[$p]['des'] = $des[$key];
$rez[$p]['found'] = $found[$key];
}
$en['count'] = count($rez);
//determine count of results per page
if (!defined('def_res_pp')) define('def_res_pp',10);
if (isset($en['pp'])) {
$en['pp'] = $en['pp'];
if ($en['pp'] != def_res_pp) $ugly = true;
}
else
if (isset($_COOKIE['pp'])) {
$en['pp'] = $_COOKIE['pp'];
} else
{
$en['pp'] = def_res_pp;
}

//delete empty cached set
if (cache_results == 'Yes' &amp;&amp;
count($rez) == 0) {
mysql_query("DELETE FROM $cachetable WHERE c_query='$en[term]' AND c_source='$en[source]'") or die(mysql_error());
}
//open result links in new window?
$neww = ($_COOKIE['newwind'] == 'tru');
$en['newwind'] = ($neww ? ' target=blank_' : ' ');
//hide search summaries
$hide = ($_COOKIE['nosum'] == 'tru');
$en['hidesum'] = ($hide ? 'tru' : ' ');
//number of pages
$en['pages'] = ceil($en['count'] / $en['pp']);
//format search term to use for search engine friendly pages
$en['xsrc'] = format_term($en['term']);
//bounds of results set
$en['to'] = $en['offs'] * $en['pp'];
$en['from'] = $en['to'] - $en['pp'] + 1;
//links to result pages
for ($k = 1;
$k <= $en['pages'];
$k++)
$en['rpg'] .= '&amp;nbsp;'.($k == $en['offs'] ? '<b>' : ($ugly ? "<a href='".constant('dir')."index.php?req=xsearch&amp;term=".$en['xsrc'].'&amp;pp='.$en['pp'].'&amp;offs='.$en['source'].($en['source'] != '' ? '-' : '').$k."'>" : "<a href='".constant('dir')."search/".$en['xsrc'].'/'.$en['source'].($en['source'] != '' ? '-' : '').$k.".html'>")).$k.($k == $en['offs'] ? '</b>' : '</a>');
if ($en['offs'] > 1)
$en['rpg'] = '&amp;nbsp;'.($ugly ? "<a href='".constant('dir')."index.php?req=xsearch&amp;term=".$en['xsrc'].'&amp;pp='.$en['pp'].'&amp;offs='.$en['source'].($en['source'] != '' ? '-' : '').($en['offs'] - 1)."'>" : "<a href='".constant('dir')."search/".$en['xsrc'].'/'.$en['source'].($en['source'] != '' ? '-' : '').($en['offs'] - 1).".html'>").'&amp;lt;&amp;lt;'.'</a>' . $en['rpg'];
if ($en['offs'] < $en['pages'])
$en['rpg'] .= '&amp;nbsp;'.($ugly ? "<a href='".constant('dir')."index.php?req=xsearch&amp;term=".$en['xsrc'].'&amp;pp='.$en['pp'].'&amp;offs='.$en['source'].($en['source'] != '' ? '-' : '').($en['offs'] + 1)."'>" : "<a href='".constant('dir')."search/".$en['xsrc'].'/'.$en['source'].($en['source'] != '' ? '-' : '').($en['offs'] + 1).".html'>").'&amp;gt;&amp;gt;'.'</a>';
//highlight search term
foreach ($rez as $key => $value) {
if ($www[$value['url']]) $rez[$key]['url'] = str_replace('http://','http://www.',$rez[$key]['url']);
$rez[$key]['title'] = highlight($rez[$key]['title'],$terms);
$rez[$key]['des'] = highlight($rez[$key]['des'],$terms);
$rez[$key]['url'] = highlight($rez[$key]['url'],$terms);
}
//show results
$en['rez'] = $rez;
if ($xmlsearch) {
//xml results
include 'includes/xml_output.php';
} else
{
//web results
load_template('templates/search_results.tpl');
}
?>
 
没人回答了吗?
 
这样看我也看不出来,只是感觉这个PHP文件很怪,好象只是一个大文件的一小部分。
看看load_template 这个函数呢?
 
看以下代码,能告诉我数组$sites是从哪里来的?也就是数组是在哪里被赋值的?
<div class=header>Manage Search Categories</div>
<?
//move up?
if ($en['act'] == 'moveup' || $en['act'] == 'movedown') {
$sorting = ($en['act'] == 'moveup' ? 'DESC' : 'ASC');
$comp = ($en['act'] == 'moveup' ? '<' : '>');
$l = mysql_fetch_assoc(mysql_query("SELECT * FROM $catstable WHERE c_id=$en[id]"));
$o = mysql_fetch_assoc(mysql_query("SELECT * FROM $catstable WHERE c_order $comp $l[c_order] ORDER BY c_order $sorting"));
mysql_query("UPDATE $catstable SET c_order=$l[c_order] WHERE c_id=$o[c_id]") or die(mysql_error());
mysql_query("UPDATE $catstable SET c_order=$o[c_order] WHERE c_id=$l[c_id]") or die(mysql_error());
}
//delete
if ($en['act'] == 'del' &amp;&amp;
$_SERVER['PHP_AUTH_USER'] != 'demo')
mysql_query("DELETE FROM $catstable WHERE c_id = '$en[id]'");
//save
if (isset($en['sm']) &amp;&amp;
$_SERVER['PHP_AUTH_USER'] != 'demo') {
//new category?
if ($en['new'] != '') {
$l = mysql_fetch_assoc(mysql_query("SELECT MAX(c_order) AS cnt FROM $catstable"));
mysql_query("INSERT INTO $catstable (c_constant,c_order)
VALUES ('$en[new]',".($l['cnt'] + 10).')') or die(mysql_error());
}
//save category names
if (is_array($_POST['catname']))
foreach ($_POST['catname'] as $key => $value)
mysql_query("UPDATE $catstable SET c_constant='$value' WHERE c_id=$key") or die(mysql_error());

//need to move engines?
if ($en['moveto'] != '' &amp;&amp;
is_array($_POST['ch']))
foreach ($_POST['ch'] as $key => $value)
if ($value == 'ON')
if (mysql_num_rows(mysql_query("SELECT * FROM $configtable WHERE c_key='{$key}_cat'")) > 0)
{
mysql_query("UPDATE $configtable SET c_value='$en[moveto]' WHERE c_key='{$key}_cat'") or die(mysql_error());
} else
{
mysql_query("INSERT INTO $configtable VALUES('{$key}_cat','$en[moveto]')") or die(mysql_error());
}
die('<b>Changes saved!</b><br><Br><a href="index.php?req=manage_cats">Return</a> to categories page.');
}
?>
<table>
<form action=index.php method=post>
<input type=hidden name=req value=manage_cats>
<?
$res = mysql_query("SELECT * FROM $catstable ORDER BY c_order ASC");
$eng = array();
echo "gao:".mysql_fetch_assoc($res);
while ($line = mysql_fetch_assoc($res)) {
echo "<Tr><td><br><input type=text name=catname[".$line['c_id']."] value='$line[c_constant]' size=20> <a href='index.php?req=manage_cats&amp;act=moveup&amp;id=$line[c_id]'>Move Up</a> | <a href='index.php?req=manage_cats&amp;act=movedown&amp;id=$line[c_id]'>Movedo
wn</a> | <a href='index.php?req=manage_cats&amp;act=del&amp;id=$line[c_id]'>Delete</a><br><Br>";

foreach ($sites as $key => $value)
if (@constant($key.'_cat') == $line['c_id'] &amp;&amp;
!$value['spons']) {
echo "<input type=checkbox name=ch[$key] value=ON id=ch_{$key}> <label for=ch_{$key}>$value[name]</label><br>";
$sites[$key]['shown'] = true;
}
echo "</td></tr>";
$eng[$line['c_id']] = $line['c_constant'];
}
foreach ($sites as $key => $value)
if (!$value['shown'] &amp;&amp;
!$value['spons']) {
if (++$noshown == 1) echo '<Tr><td><b>No category selected:</b><Br>';
echo "<input type=checkbox name=ch[$key] value=ON id=ch_{$key}> <label for=ch_{$key}>$value[name]</label><br>";
$sites[$key]['shown'] = true;
}
echo '</td></Tr>';
?>
</table>
<br>
<b>Move Selected Engines To:</b> <select name=moveto>
<option value=''>Do not move</option>
<? foreach ($eng as $key => $value) echo "<option value='$key'>$value</option>";
?>
</select><Br>
<Br>
<b>Add New Category:</b> <input type=text name=new size=20> (enter constant name) <Br><br>
<input type=submit name=sm value='Save Changes'>
</form>
 
欢迎大家来讨论学习,我觉得这套代码,很有学习研究的价值,老外写的东西!
 
多人接受答案了。
 

Similar threads

回复
0
查看
1K
不得闲
D
回复
0
查看
872
DelphiTeacher的专栏
D
D
回复
0
查看
840
DelphiTeacher的专栏
D
D
回复
0
查看
790
DelphiTeacher的专栏
D
后退
顶部