class_membersearch.php
9.65 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
<?php
/**
* [Discuz!] (C)2001-2099 Comsenz Inc.
* This is NOT a freeware, use is subject to license terms
*
* $Id: class_membersearch.php 33687 2013-08-02 01:46:22Z nemohou $
*/
if(!defined('IN_DISCUZ')) {
exit('Access Denied');
}
class membersearch {
function membersearch(){}
function getfield($fieldid='') {
static $fields = array(
'uid'=>'member', 'username'=>'member', 'groupid'=>'member', 'medalid'=>'medal','tagid'=>'tag','idtype'=>'tag',
'email'=>'member', 'credits'=>'member', 'regdate'=>'member',
'status'=>'member', 'freeze'=>'member', 'emailstatus'=>'member', 'avatarstatus'=>'member','videophotostatus'=>'member',
'conisbind'=>'member','uin' => 'black','sid'=>'session',
'extcredits1'=>'count', 'extcredits2'=>'count', 'extcredits3'=>'count', 'extcredits4'=>'count',
'extcredits5'=>'count', 'extcredits6'=>'count', 'extcredits7'=>'count', 'extcredits8'=>'count',
'posts'=>'count','friends'=>'count','oltime'=>'count',
'fid' => 'groupuser', 'level' => 'groupuser',
'verify1' => 'verify', 'verify2' => 'verify', 'verify3' => 'verify', 'verify4' => 'verify', 'verify5' => 'verify', 'verify6' => 'verify', 'verify7' => 'verify',
'regip'=>'status', 'lastip'=>'status', 'lastvisit'=>'status', 'lastpost' => 'status', 'realname'=>'profile',
'birthyear'=>'profile', 'birthmonth'=>'profile', 'birthday'=>'profile', 'gender'=>'profile',
'constellation'=>'profile', 'zodiac'=>'profile', 'telephone'=>'profile', 'mobile'=>'profile',
'idcardtype'=>'profile', 'idcard'=>'profile', 'address'=>'profile', 'zipcode'=>'profile', 'nationality'=>'profile',
'birthprovince'=>'profile', 'birthcity'=>'profile', 'resideprovince'=>'profile',
'residecity'=>'profile', 'residedist'=>'profile', 'residecommunity'=>'profile',
'residesuite'=>'profile', 'graduateschool'=>'profile', 'education'=>'profile',
'occupation'=>'profile', 'company'=>'profile', 'position'=>'profile', 'revenue'=>'profile',
'affectivestatus'=>'profile', 'lookingfor'=>'profile', 'bloodtype'=>'profile',
'height'=>'profile', 'weight'=>'profile', 'alipay'=>'profile', 'icq'=>'profile',
'qq'=>'profile', 'yahoo'=>'profile', 'msn'=>'profile', 'taobao'=>'profile', 'site'=>'profile',
'bio'=>'profile', 'interest'=>'profile', 'field1'=>'profile', 'field2'=>'profile',
'field3'=>'profile', 'field4'=>'profile', 'field5'=>'profile', 'field6'=>'profile',
'field7'=>'profile', 'field8'=>'profile', 'token' => 'token');
return $fieldid ? $fields[$fieldid] : $fields;
}
function gettype($fieldid) {
static $types = array(
'uid'=>'int', 'groupid'=>'int', 'medalid'=>'int', 'tagid'=>'int', 'credits'=>'int',
'status'=>'int', 'freeze'=>'int', 'emailstatus'=>'int', 'avatarstatus'=>'int','videophotostatus'=>'int',
'extcredits1'=>'int', 'extcredits2'=>'int', 'extcredits3'=>'int', 'extcredits4'=>'int',
'extcredits5'=>'int', 'extcredits6'=>'int', 'extcredits7'=>'int', 'extcredits8'=>'int',
'posts'=>'int', 'friends'=>'int', 'birthyear'=>'int', 'birthmonth'=>'int', 'birthday'=>'int', 'gender'=>'int',
'uin'=>'int', 'sid'=>'noempty', 'token' => 'noempty'
);
return $types[$fieldid] ? $types[$fieldid] : 'string';
}
function search($condition, $maxsearch=100, $start=0){
$list = array();
$sql = membersearch::makesql($condition);
if($maxsearch) {
$sql .= " LIMIT $start, $maxsearch";
}
if(isset($condition['token_noempty'])) {
try {
$query = DB::query($sql);
while($value = DB::fetch($query)) {
$list[] = intval($value['uid']);
}
} catch (Exception $e) {}
} else {
$query = DB::query($sql);
while($value = DB::fetch($query)) {
$list[] = intval($value['uid']);
}
}
return $list;
}
function getcount($condition) {
$count = 0;
if(isset($condition['token_noempty'])) {
try {
$count = DB::result_first(membersearch::makesql($condition, true));
} catch (Exception $e) {}
} else {
$count = DB::result_first(membersearch::makesql($condition, true));
}
return intval($count);
}
function filtercondition($condition) {
$tablename = isset($condition['tablename']) ? $condition['tablename'] : '';
unset($condition['tablename']);
$fields = membersearch::getfield();
foreach($condition as $key => $value) {
$rkey = str_replace(array('_low', '_high', '_noempty', '_after', '_before'), '', $key);
if(!(isset($fields[$rkey]) || in_array($key, array('verify', 'fid', 'tagid')))) {
unset($condition[$key]);
}
}
$condition['tablename'] = $tablename;
return $condition;
}
function makesql($condition, $onlyCount=false) {
$tables = $wheres = array();
$isarchive = $condition['tablename'] === 'archive' ? true : false;
if($condition['verify']) {
foreach($condition['verify'] as $key => $value) {
$condition[$value] = 1;
}
unset($condition['verify']);
}
if($condition['fid']) {
$condition['level'] = '1,2,3,4';
}
if($condition['tagid']) {
$condition['idtype'] = 'uid';
}
$fields = membersearch::getfield();
foreach ($fields as $key=>$value) {
$return = array();
if(isset($condition[$key])) {
$return = membersearch::makeset($key, $condition[$key], membersearch::gettype($key));
} elseif(isset($condition[$key.'_low']) || isset($condition[$key.'_high'])) {
$return = membersearch::makerange($key, $condition[$key.'_low'], $condition[$key.'_high'], membersearch::gettype($key));
} elseif(isset($condition[$key.'_noempty'])) {
$return = membersearch::makeset($key, $condition[$key.'_noempty'], membersearch::gettype($key));
} elseif(isset($condition[$key.'_after']) || isset($condition[$key.'_before'])) {
$condition[$key.'_after'] = dmktime($condition[$key.'_after']);
$condition[$key.'_before'] = dmktime($condition[$key.'_before']);
$return = membersearch::makerange($key, $condition[$key.'_after'], $condition[$key.'_before'], membersearch::gettype($key));
}
if($return) {
$tables[$return['table']] = true;
$wheres[] = $return['where'];
}
}
if($tables && $wheres) {
$parts = array();
$table1 = $asuid = '';
$uidfield = 'uid';
foreach ($tables as $key => $value) {
$value = membersearch::gettable($key, $isarchive);
$parts[] = "$value as $key";
if(! $table1) {
$table1 = $key;
if($table1 == 'tag') {
$uidfield = 'itemid';
$asuid = ' as uid';
}
} else {
if($key == 'tag') {
$keyuid = 'itemid';
} else {
$keyuid = 'uid';
}
$wheres[] = $table1.'.'.$uidfield.' = '.$key.'.'.$keyuid;
}
}
$selectsql = $onlyCount ? 'SELECT COUNT(DISTINCT '.$table1.'.'.$uidfield.') as cnt ' : 'SELECT DISTINCT '.$table1.'.'.$uidfield.$asuid;
return $selectsql.' FROM '.implode(', ', $parts).' WHERE '.implode(' AND ', $wheres);
} else {
$selectsql = $onlyCount ? 'SELECT COUNT(uid) as cnt ' : 'SELECT uid';
return $selectsql.' FROM '.DB::table('common_member'.($isarchive ? '_archive' : ''))." WHERE 1";
}
}
function makeset($field, $condition, $type='string') {
$return = $values = array();
$return['table'] = membersearch::getfield($field);
if(! $return['table']){
return array();
}
$field = $return['table'].'.'.$field;
$islikesearch = $noempty = false;
if(!is_array($condition)) {
$condition = explode(',', $condition);
}
foreach ($condition as $value) {
$value = trim($value);
if($type == 'int') {
$value = intval($value);
} elseif($type == 'noempty') {
$noempty = true;
} elseif(!$islikesearch && strexists($value, '*')) {
$islikesearch = true;
}
if($type != 'int') $value = addslashes($value);
if($value !== null) {
$values[] = $value;
}
}
if(!$values) {
return array();
}
if($islikesearch) {
$likes = array();
foreach ($values as $value) {
if(strexists($value, '*')) {
$value = str_replace('*', '%', $value);
$likes[] = "$field LIKE '$value'";
} else {
$likes[] = "$field = '$value'";
}
}
$return['where'] = '('.implode(' OR ', $likes).')';
} elseif($noempty) {
$return['where'] = "$field != ''";
} elseif(count($values) > 1) {
$return['where'] = "$field IN ('".implode("','", $values)."')";
} else {
$return['where'] = "$field = '$values[0]'";
}
return $return;
}
function makerange($field, $range_low=null, $range_high=null, $type='string') {
$return = array();
$return['table'] = membersearch::getfield($field);
if(!$return['table']){
return array();
}
$field = $return['table'].'.'.$field;
if($type == 'int') {
$range_low = intval($range_low);
$range_high = intval($range_high);
} else {
$range_low = addslashes(trim($range_low));
$range_high = addslashes(trim($range_high));
}
$wheres = array();
if($range_low !== null) {
$wheres[] = "$field >= '$range_low'";
}
if($range_high !== null && $range_high > $range_low) {
$wheres[] = "$field <= '$range_high'";
}
if($wheres) {
$return['where'] = implode(' AND ', $wheres);
return $return;
} else {
return array();
}
}
function gettable($alias, $isarchive = false) {
static $mapping = array('member'=>'common_member', 'status'=>'common_member_status', 'profile'=>'common_member_profile', 'count'=>'common_member_count', 'session'=>'common_session', 'groupuser' => 'forum_groupuser', 'verify' => 'common_member_verify', 'black'=>'common_uin_black', 'medal'=>'common_member_medal', 'tag'=>'common_tagitem', 'token' => 'common_devicetoken');
return DB::table($isarchive && in_array($alias, array('member', 'status', 'profile', 'count')) ? $mapping[$alias].'_archive' : $mapping[$alias]);
}
}
?>