schallfun.php
8.04 KB
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
<?php
//區位碼
function SearchReturnQwm($t){
return sprintf("%02d%02d",ord($t[0])-160,ord($t[1])-160);
}
//轉換字符串
function SearchReturnSaveStr($str){
//所有漢字後添加ASCII的0字符,此法是為了排除特殊中文拆分錯誤的問題
$str=preg_replace("/[\x80-\xff]{2}/","\\0".chr(0x00),$str);
//拆分的分割符
$search = array(",", "/", "\\", ".", ";", ":", "\"", "!", "~", "`", "^", "(", ")", "?", "-", "\t", "\n", "'", "<", ">", "\r", "\r\n", "$", "&", "%", "#", "@", "+", "=", "{", "}", "[", "]", ":", ")", "(", ".", "。", ",", "!", ";", "「", "」", "『", "』", "〔", "〕", "、", "—", " ", "《", "》", "-", "…", "【", "】",);
//替換所有的分割符為空格
$str = str_replace($search,' ',$str);
//用正則匹配半角單個字符或者全角單個字符,存入數組$ar
preg_match_all("/[\x80-\xff]?./",$str,$ar);$ar=$ar[0];
//去掉$ar中ASCII為0字符的項目
for($i=0;$i<count($ar);$i++)
{
if($ar[$i]!=chr(0x00))
{
$ar_new[]=$ar[$i];
}
}
$ar=$ar_new;
unset($ar_new);
$oldsw=0;
//把連續的半角存成一個數組下標,或者全角的每2個字符存成一個數組的下標
for($ar_str='',$i=0;$i<count($ar);$i++)
{
$sw=strlen($ar[$i]);
if($i>0 and $sw!=$oldsw)
{
$ar_str.=" ";
}
if($sw==1)
{
$ar_str.=$ar[$i];
}
else
{
if(strlen($ar[$i+1])==2)
{
$ar_str.=SearchReturnQwm($ar[$i]).SearchReturnQwm($ar[$i+1]).' ';
}
elseif($oldsw==1 or $oldsw==0)
{
$ar_str.=SearchReturnQwm($ar[$i]);
}
}
$oldsw=$sw;
}
//去掉連續的空格
$ar_str=trim(preg_replace("# {1,}#i"," ",$ar_str));
//返回拆分後的結果
return $ar_str;
}
//全站搜索去除html
function ClearSearchAllHtml($value){
$value=str_replace(array("\r\n"," ","[!--empirenews.page--]","[/!--empirenews.page--]"),array("","","",""),$value);
$value=strip_tags($value);
$value=trim($value,"\r\n");
$value=SearchAllChangeChar($value);//轉編碼
return $value;
}
//轉換編碼
function SearchAllChangeChar($value){
global $ecms_config,$char,$targetchar,$iconv;
if($ecms_config['sets']['pagechar']!='gb2312')
{
$value=$iconv->Convert($char,$targetchar,$value);
}
return $value;
}
//導入數據
function LoadSearchAll($lid,$start,$userid,$username){
global $empire,$dbtbpre,$class_r,$fun_r,$public_r,$emod_r;
$lid=(int)$lid;
if(empty($lid))
{
printerror('ErrorUrl','');
}
$lr=$empire->fetch1("select tbname,titlefield,infotextfield,loadnum,lastid from {$dbtbpre}enewssearchall_load where lid='$lid'");
if(empty($lr['tbname']))
{
printerror('ErrorUrl','');
}
//不導入欄目
$pr=$empire->fetch1("select schallnotcid from {$dbtbpre}enewspublic limit 1");
$line=$lr['loadnum'];
if(empty($line))
{
$line=300;
}
$start=(int)$start;
if($start<$lr['lastid'])
{
$start=$lr['lastid'];
}
//字段
$selectdtf='';
$selectf='';
$savetxtf='';
$fsql=$empire->query("select tid,f,savetxt,tbdataf from {$dbtbpre}enewsf where (f='$lr[titlefield]' or f='$lr[infotextfield]') and tbname='$lr[tbname]' limit 2");
while($fr=$empire->fetch($fsql))
{
if($fr['tbdataf'])
{
$selectdtf.=','.$fr[f];
}
else
{
$selectf.=','.$fr[f];
}
if($fr['savetxt'])
{
$savetxtf=$fr[f];
}
}
$b=0;
$sql=$empire->query("select id,stb,classid,isurl,newstime".$selectf." from {$dbtbpre}ecms_".$lr['tbname']." where id>$start order by id limit ".$line);
while($r=$empire->fetch($sql))
{
$b=1;
$newstart=$r['id'];
if($r['isurl'])
{
continue;
}
if(empty($class_r[$r[classid]]['tbname']))
{
continue;
}
if(strstr($pr['schallnotcid'],','.$r[classid].','))
{
continue;
}
//重複
$havenum=$empire->gettotal("select count(*) as total from {$dbtbpre}enewssearchall where id='$r[id]' and classid='$r[classid]' limit 1");
if($havenum)
{
continue;
}
//副表
if($selectdtf)
{
$finfor=$empire->fetch1("select id".$selectdtf." from {$dbtbpre}ecms_".$lr['tbname']."_data_".$r[stb]." where id='$r[id]'");
$r=array_merge($r,$finfor);
}
//存文本
if($savetxtf)
{
$r[$savetxtf]=GetTxtFieldText($r[$savetxtf]);
}
$infotext=$r[$lr[infotextfield]];
$title=$r[$lr[titlefield]];
$infotime=$r[newstime];
$title=SearchReturnSaveStr(ClearSearchAllHtml(stripSlashes($title)));
$infotext=SearchReturnSaveStr(ClearSearchAllHtml(stripSlashes($infotext)));
$empire->query("insert into {$dbtbpre}enewssearchall(sid,id,classid,title,infotime,infotext) values(NULL,'$r[id]','$r[classid]','".addslashes($title)."','$infotime','".addslashes($infotext)."');");
}
if(empty($b))
{
$lasttime=time();
if(empty($newstart))
{
$newstart=$start;
}
$empire->query("update {$dbtbpre}enewssearchall_load set lasttime='$lasttime',lastid='$newstart' where lid='$lid'");
echo "<link rel=\"stylesheet\" href=\"../../data/images/css.css\" type=\"text/css\"><center><b>".$lr['tbname'].$fun_r[LoadSearchAllIsOK]."</b></center>";
db_close();
$empire=null;
exit();
}
echo"<link rel=\"stylesheet\" href=\"../../data/images/css.css\" type=\"text/css\"><meta http-equiv=\"refresh\" content=\"0;url=LoadSearchAll.php?enews=LoadSearchAll&lid=$lid&start=$newstart".hReturnEcmsHashStrHref(0)."\">".$fun_r[OneLoadSearchAllSuccess]."(ID:<font color=red><b>".$newstart."</b></font>)";
exit();
}
//返回搜索SQL
function ReturnSearchAllSql($add){
global $public_r,$class_r;
//關閉
if(empty($public_r['openschall']))
{
printerror("SchallClose",'',1);
}
//關鍵字
$keyboard=RepPostVar2($add['keyboard']);
if(!trim($keyboard))
{
printerror('EmptySchallKeyboard','',1);
}
$strlen=strlen($keyboard);
if($strlen<$public_r['schallminlen']||$strlen>$public_r['schallmaxlen'])
{
printerror('SchallMinKeyboard','',1);
}
$returnr['keyboard']=ehtmlspecialchars($keyboard);
$returnr['search']="&keyboard=".$keyboard;
//字段
$field=(int)$add['field'];
if($field)
{
$returnr['search'].="&field=".$field;
}
if($field==1)//標題和全文
{
if($public_r['schallfield']!=1)
{
printerror('SchallNotOpenTitleText','',1);
}
$sf="title,infotext";
}
elseif($field==2)//標題
{
if($public_r['schallfield']==3)
{
printerror('SchallNotOpenTitle','',1);
}
$sf="title";
}
elseif($field==3)//全文
{
if($public_r['schallfield']==2)
{
printerror('SchallNotOpenText','',1);
}
$sf="infotext";
}
else
{
$sf=ReturnSearchAllField(0);
}
$where='';
//欄目
$classid=RepPostVar($add['classid']);
if($classid)
{
$returnr['search'].="&classid=".$classid;
if(strstr($classid,","))//多欄目
{
$son_r=sys_ReturnMoreClass($classid,1);
$where.='('.$son_r[1].') and ';
}
else
{
$classid=(int)$classid;
$where.=$class_r[$classid][islast]?"classid='$classid' and ":ReturnClass($class_r[$classid][sonclass]).' and ';
}
}
//關鍵字
if(strstr($keyboard,' '))
{
$andkey='';
$keyr=explode(' ',$keyboard);
$kcount=count($keyr);
for($i=0;$i<$kcount;$i++)
{
if(strlen($keyr[$i])<$public_r['schallminlen'])
{
continue;
}
$kb=SearchAllChangeChar($keyr[$i]);//轉碼
$kb=SearchReturnSaveStr($kb);
$kb=RepPostVar2($kb);
if(!trim($kb))
{
continue;
}
$where.=$andkey."MATCH(".$sf.") AGAINST('".$kb."' IN BOOLEAN MODE)";
$andkey=' and ';
}
if(empty($where))
{
printerror('SchallMinKeyboard','',1);
}
}
else
{
$keyboard=SearchAllChangeChar($keyboard);//轉碼
$keyboard=SearchReturnSaveStr($keyboard);
$keyboard=RepPostVar2($keyboard);
if(!trim($keyboard))
{
printerror('EmptySchallKeyboard','',1);
}
$where.="MATCH(".$sf.") AGAINST('".$keyboard."' IN BOOLEAN MODE)";
}
$returnr['where']=$where;
return $returnr;
}
//返回搜索字段
function ReturnSearchAllField($field){
global $public_r;
if($public_r['schallfield']==1)
{
$sf="title,infotext";
}
elseif($public_r['schallfield']==3)
{
$sf="infotext";
}
else
{
$sf="title";
}
return $sf;
}
?>