class_membersearch.php 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. <?php
  2. /**
  3. * [Discuz!] (C)2001-2099 Comsenz Inc.
  4. * This is NOT a freeware, use is subject to license terms
  5. *
  6. * $Id: class_membersearch.php 33687 2013-08-02 01:46:22Z nemohou $
  7. */
  8. if(!defined('IN_DISCUZ')) {
  9. exit('Access Denied');
  10. }
  11. class membersearch {
  12. function membersearch(){}
  13. function getfield($fieldid='') {
  14. static $fields = array(
  15. 'uid'=>'member', 'username'=>'member', 'groupid'=>'member', 'medalid'=>'medal','tagid'=>'tag','idtype'=>'tag',
  16. 'email'=>'member', 'credits'=>'member', 'regdate'=>'member',
  17. 'status'=>'member', 'freeze'=>'member', 'emailstatus'=>'member', 'avatarstatus'=>'member','videophotostatus'=>'member',
  18. 'conisbind'=>'member','uin' => 'black','sid'=>'session',
  19. 'extcredits1'=>'count', 'extcredits2'=>'count', 'extcredits3'=>'count', 'extcredits4'=>'count',
  20. 'extcredits5'=>'count', 'extcredits6'=>'count', 'extcredits7'=>'count', 'extcredits8'=>'count',
  21. 'posts'=>'count','friends'=>'count','oltime'=>'count',
  22. 'fid' => 'groupuser', 'level' => 'groupuser',
  23. 'verify1' => 'verify', 'verify2' => 'verify', 'verify3' => 'verify', 'verify4' => 'verify', 'verify5' => 'verify', 'verify6' => 'verify', 'verify7' => 'verify',
  24. 'regip'=>'status', 'lastip'=>'status', 'lastvisit'=>'status', 'lastpost' => 'status', 'realname'=>'profile',
  25. 'birthyear'=>'profile', 'birthmonth'=>'profile', 'birthday'=>'profile', 'gender'=>'profile',
  26. 'constellation'=>'profile', 'zodiac'=>'profile', 'telephone'=>'profile', 'mobile'=>'profile',
  27. 'idcardtype'=>'profile', 'idcard'=>'profile', 'address'=>'profile', 'zipcode'=>'profile', 'nationality'=>'profile',
  28. 'birthprovince'=>'profile', 'birthcity'=>'profile', 'resideprovince'=>'profile',
  29. 'residecity'=>'profile', 'residedist'=>'profile', 'residecommunity'=>'profile',
  30. 'residesuite'=>'profile', 'graduateschool'=>'profile', 'education'=>'profile',
  31. 'occupation'=>'profile', 'company'=>'profile', 'position'=>'profile', 'revenue'=>'profile',
  32. 'affectivestatus'=>'profile', 'lookingfor'=>'profile', 'bloodtype'=>'profile',
  33. 'height'=>'profile', 'weight'=>'profile', 'alipay'=>'profile', 'icq'=>'profile',
  34. 'qq'=>'profile', 'yahoo'=>'profile', 'msn'=>'profile', 'taobao'=>'profile', 'site'=>'profile',
  35. 'bio'=>'profile', 'interest'=>'profile', 'field1'=>'profile', 'field2'=>'profile',
  36. 'field3'=>'profile', 'field4'=>'profile', 'field5'=>'profile', 'field6'=>'profile',
  37. 'field7'=>'profile', 'field8'=>'profile', 'token' => 'token');
  38. return $fieldid ? $fields[$fieldid] : $fields;
  39. }
  40. function gettype($fieldid) {
  41. static $types = array(
  42. 'uid'=>'int', 'groupid'=>'int', 'medalid'=>'int', 'tagid'=>'int', 'credits'=>'int',
  43. 'status'=>'int', 'freeze'=>'int', 'emailstatus'=>'int', 'avatarstatus'=>'int','videophotostatus'=>'int',
  44. 'extcredits1'=>'int', 'extcredits2'=>'int', 'extcredits3'=>'int', 'extcredits4'=>'int',
  45. 'extcredits5'=>'int', 'extcredits6'=>'int', 'extcredits7'=>'int', 'extcredits8'=>'int',
  46. 'posts'=>'int', 'friends'=>'int', 'birthyear'=>'int', 'birthmonth'=>'int', 'birthday'=>'int', 'gender'=>'int',
  47. 'uin'=>'int', 'sid'=>'noempty', 'token' => 'noempty'
  48. );
  49. return $types[$fieldid] ? $types[$fieldid] : 'string';
  50. }
  51. function search($condition, $maxsearch=100, $start=0){
  52. $list = array();
  53. $sql = membersearch::makesql($condition);
  54. if($maxsearch) {
  55. $sql .= " LIMIT $start, $maxsearch";
  56. }
  57. if(isset($condition['token_noempty'])) {
  58. try {
  59. $query = DB::query($sql);
  60. while($value = DB::fetch($query)) {
  61. $list[] = intval($value['uid']);
  62. }
  63. } catch (Exception $e) {}
  64. } else {
  65. $query = DB::query($sql);
  66. while($value = DB::fetch($query)) {
  67. $list[] = intval($value['uid']);
  68. }
  69. }
  70. return $list;
  71. }
  72. function getcount($condition) {
  73. $count = 0;
  74. if(isset($condition['token_noempty'])) {
  75. try {
  76. $count = DB::result_first(membersearch::makesql($condition, true));
  77. } catch (Exception $e) {}
  78. } else {
  79. $count = DB::result_first(membersearch::makesql($condition, true));
  80. }
  81. return intval($count);
  82. }
  83. function filtercondition($condition) {
  84. $tablename = isset($condition['tablename']) ? $condition['tablename'] : '';
  85. unset($condition['tablename']);
  86. $fields = membersearch::getfield();
  87. foreach($condition as $key => $value) {
  88. $rkey = str_replace(array('_low', '_high', '_noempty', '_after', '_before'), '', $key);
  89. if(!(isset($fields[$rkey]) || in_array($key, array('verify', 'fid', 'tagid')))) {
  90. unset($condition[$key]);
  91. }
  92. }
  93. $condition['tablename'] = $tablename;
  94. return $condition;
  95. }
  96. function makesql($condition, $onlyCount=false) {
  97. $tables = $wheres = array();
  98. $isarchive = $condition['tablename'] === 'archive' ? true : false;
  99. if($condition['verify']) {
  100. foreach($condition['verify'] as $key => $value) {
  101. $condition[$value] = 1;
  102. }
  103. unset($condition['verify']);
  104. }
  105. if($condition['fid']) {
  106. $condition['level'] = '1,2,3,4';
  107. }
  108. if($condition['tagid']) {
  109. $condition['idtype'] = 'uid';
  110. }
  111. $fields = membersearch::getfield();
  112. foreach ($fields as $key=>$value) {
  113. $return = array();
  114. if(isset($condition[$key])) {
  115. $return = membersearch::makeset($key, $condition[$key], membersearch::gettype($key));
  116. } elseif(isset($condition[$key.'_low']) || isset($condition[$key.'_high'])) {
  117. $return = membersearch::makerange($key, $condition[$key.'_low'], $condition[$key.'_high'], membersearch::gettype($key));
  118. } elseif(isset($condition[$key.'_noempty'])) {
  119. $return = membersearch::makeset($key, $condition[$key.'_noempty'], membersearch::gettype($key));
  120. } elseif(isset($condition[$key.'_after']) || isset($condition[$key.'_before'])) {
  121. $condition[$key.'_after'] = dmktime($condition[$key.'_after']);
  122. $condition[$key.'_before'] = dmktime($condition[$key.'_before']);
  123. $return = membersearch::makerange($key, $condition[$key.'_after'], $condition[$key.'_before'], membersearch::gettype($key));
  124. }
  125. if($return) {
  126. $tables[$return['table']] = true;
  127. $wheres[] = $return['where'];
  128. }
  129. }
  130. if($tables && $wheres) {
  131. $parts = array();
  132. $table1 = $asuid = '';
  133. $uidfield = 'uid';
  134. foreach ($tables as $key => $value) {
  135. $value = membersearch::gettable($key, $isarchive);
  136. $parts[] = "$value as $key";
  137. if(! $table1) {
  138. $table1 = $key;
  139. if($table1 == 'tag') {
  140. $uidfield = 'itemid';
  141. $asuid = ' as uid';
  142. }
  143. } else {
  144. if($key == 'tag') {
  145. $keyuid = 'itemid';
  146. } else {
  147. $keyuid = 'uid';
  148. }
  149. $wheres[] = $table1.'.'.$uidfield.' = '.$key.'.'.$keyuid;
  150. }
  151. }
  152. $selectsql = $onlyCount ? 'SELECT COUNT(DISTINCT '.$table1.'.'.$uidfield.') as cnt ' : 'SELECT DISTINCT '.$table1.'.'.$uidfield.$asuid;
  153. return $selectsql.' FROM '.implode(', ', $parts).' WHERE '.implode(' AND ', $wheres);
  154. } else {
  155. $selectsql = $onlyCount ? 'SELECT COUNT(uid) as cnt ' : 'SELECT uid';
  156. return $selectsql.' FROM '.DB::table('common_member'.($isarchive ? '_archive' : ''))." WHERE 1";
  157. }
  158. }
  159. function makeset($field, $condition, $type='string') {
  160. $return = $values = array();
  161. $return['table'] = membersearch::getfield($field);
  162. if(! $return['table']){
  163. return array();
  164. }
  165. $field = $return['table'].'.'.$field;
  166. $islikesearch = $noempty = false;
  167. if(!is_array($condition)) {
  168. $condition = explode(',', $condition);
  169. }
  170. foreach ($condition as $value) {
  171. $value = trim($value);
  172. if($type == 'int') {
  173. $value = intval($value);
  174. } elseif($type == 'noempty') {
  175. $noempty = true;
  176. } elseif(!$islikesearch && strexists($value, '*')) {
  177. $islikesearch = true;
  178. }
  179. if($type != 'int') $value = addslashes($value);
  180. if($value !== null) {
  181. $values[] = $value;
  182. }
  183. }
  184. if(!$values) {
  185. return array();
  186. }
  187. if($islikesearch) {
  188. $likes = array();
  189. foreach ($values as $value) {
  190. if(strexists($value, '*')) {
  191. $value = str_replace('*', '%', $value);
  192. $likes[] = "$field LIKE '$value'";
  193. } else {
  194. $likes[] = "$field = '$value'";
  195. }
  196. }
  197. $return['where'] = '('.implode(' OR ', $likes).')';
  198. } elseif($noempty) {
  199. $return['where'] = "$field != ''";
  200. } elseif(count($values) > 1) {
  201. $return['where'] = "$field IN ('".implode("','", $values)."')";
  202. } else {
  203. $return['where'] = "$field = '$values[0]'";
  204. }
  205. return $return;
  206. }
  207. function makerange($field, $range_low=null, $range_high=null, $type='string') {
  208. $return = array();
  209. $return['table'] = membersearch::getfield($field);
  210. if(!$return['table']){
  211. return array();
  212. }
  213. $field = $return['table'].'.'.$field;
  214. if($type == 'int') {
  215. $range_low = intval($range_low);
  216. $range_high = intval($range_high);
  217. } else {
  218. $range_low = addslashes(trim($range_low));
  219. $range_high = addslashes(trim($range_high));
  220. }
  221. $wheres = array();
  222. if($range_low !== null) {
  223. $wheres[] = "$field >= '$range_low'";
  224. }
  225. if($range_high !== null && $range_high > $range_low) {
  226. $wheres[] = "$field <= '$range_high'";
  227. }
  228. if($wheres) {
  229. $return['where'] = implode(' AND ', $wheres);
  230. return $return;
  231. } else {
  232. return array();
  233. }
  234. }
  235. function gettable($alias, $isarchive = false) {
  236. 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');
  237. return DB::table($isarchive && in_array($alias, array('member', 'status', 'profile', 'count')) ? $mapping[$alias].'_archive' : $mapping[$alias]);
  238. }
  239. }
  240. ?>