-
Notifications
You must be signed in to change notification settings - Fork 1
/
ajax.php
143 lines (131 loc) · 4.14 KB
/
ajax.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
<?php
/**
* response
* 返回json格式的数据,本页面所有返回都走这个函数
*/
function response($status, $data)
{
echo json_encode(array(
'status' => $status,
'data' => $data,
));
exit;
}
/**
* getMTime
* 获取当前时间(含毫秒)
* @access public
* @return void
*/
function getMTime()
{
list($usec, $sec) = explode(" ", microtime());
return ((float)$usec + (float)$sec);
}
/**
* getTableNameById
* 由于做了分表,这里需要根据sphinx返回的id计算出数据处于哪个表里
*/
function getTableNameById($rowId)
{
return floor($rowId/10000000)+1;
}
/**
* 判断提交数据合法性
*/
if (!isset($_POST['mode']) || !isset($_POST['keyword']) || !isset($_POST['useMd5'])) {
response('error','bad Data Posted');
}
if (!in_array($_POST['useMd5'], array(0,16,32))) {
response('error','post field useMd5 should be one of 0, 16, 32');
}
switch ($_POST['useMd5'])
{
case 0:
$keyToSearch = $_POST['keyword'];
break;
case 16:
$keyToSearch = substr(md5($_POST['keyword']), 8, 16);
break;
case 32:
$keyToSearch = md5($_POST['keyword']);
break;
}
if (isset($_POST['limit'])) {
$searchLimit = (int) $_POST['limit'];
} else {
$searchLimit = 0;
}
/**
* 由于sphinx配置等原因,目前暂时只展示前1000个
*/
if ($searchLimit >979) {
response('error','由于sphinx原因,目前暂时只展示前1000个,你可以换一下关键词!');
}
require('./sphinxapi.php');
$t_start = getMTime();
$cl= new SphinxClient();
$cl->SetServer('localhost', 9312);
$cl->SetArrayResult(true); //设置 显示结果集方式
$cl->SetLimits($searchLimit, 20); //同sql语句中的LIMIT
$cl->SetSortMode(SPH_SORT_RELEVANCE); //设置默认按照相关性排序
$cl->SetMatchMode($_POST['mode']);
$result=$cl->Query($keyToSearch, "*"); //执行搜索
$t_aftSphinx = getMTime();
if ($result !== FALSE) {
/**
* 没有搜到结果
*/
if ($result['total_found'] == 0) {
response('error','没有找到结果!');
} else {
// 返回结果
$data = array(
'resultCount' => $result['total_found'],
'rows' => $result['matches'],
'currentLimit'=> $searchLimit,
);
/**
* 连接 mysql
*/
$con = mysql_connect('localhost', 'root', 'i');
mysql_select_db('shegong');
mysql_set_charset('utf8',$con);
$keyArr = explode(' ', $keyToSearch);
/**
* 从mysql取出对应的记录
*/
foreach ($result['matches'] as $k => $v) {
$tableId = getTableNameById($k);
$sql = 'select * from data_'.$tableId.' where id = '.$k;
$res = mysql_query($sql);
while ($row = mysql_fetch_array($res, MYSQL_NUM)) {
foreach ($row as $kb => $vb) {
$row[$kb] = str_ireplace($keyToSearch, "<font color='#ff0000'>".$keyToSearch.'</font>', $vb);
if (count($keyArr) > 1) {
foreach ($keyArr as $kc => $vc) {
$row[$kb] = str_ireplace($vc, "<font color='#bf0060'>".$vc.'</font>', $vb);
}
}
}
foreach ($row as $kd => $vd) {
if (preg_match('/^[a-zA-Z0-9]{16,32}$/ui', trim($vd))) {
$row[$kd] = '<a target="_blank" href="http://www.xmd5.com">'.$vd.'</a>';
}
}
$data['rows'][$k] = $row;
}
}
mysql_close($con);
/**
* 统计时间
*/
$t_aftMysql = getMTime();
$data['sphinxCost'] = number_format($t_aftSphinx - $t_start, 4, '.', '');
$data['mysqlCost'] = number_format($t_aftMysql - $t_aftSphinx, 4, '.', '');
$data['totalCost'] = number_format($t_aftMysql - $t_start, 4, '.', '');
response('success', $data);
}
} else {
response('error',$cl->GetLastError());
}