Memberlist.php 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627
  1. <?php
  2. /**
  3. * This file contains the functions for displaying and searching in the
  4. * members list.
  5. *
  6. * Simple Machines Forum (SMF)
  7. *
  8. * @package SMF
  9. * @author Simple Machines http://www.simplemachines.org
  10. * @copyright 2014 Simple Machines and individual contributors
  11. * @license http://www.simplemachines.org/about/smf/license.php BSD
  12. *
  13. * @version 2.1 Alpha 1
  14. */
  15. if (!defined('SMF'))
  16. die('No direct access...');
  17. /**
  18. * Shows a listing of registered members.
  19. * - If a subaction is not specified, lists all registered members.
  20. * - It allows searching for members with the 'search' sub action.
  21. * - It calls MLAll or MLSearch depending on the sub action.
  22. * - Requires the view_mlist permission.
  23. * - Accessed via ?action=mlist.
  24. *
  25. * @uses Memberlist template, main sub template.
  26. */
  27. function Memberlist()
  28. {
  29. global $scripturl, $txt, $modSettings, $context;
  30. // Make sure they can view the memberlist.
  31. isAllowedTo('view_mlist');
  32. loadTemplate('Memberlist');
  33. $context['listing_by'] = !empty($_GET['sa']) ? $_GET['sa'] : 'all';
  34. // $subActions array format:
  35. // 'subaction' => array('label', 'function', 'is_selected')
  36. $subActions = array(
  37. 'all' => array($txt['view_all_members'], 'MLAll', $context['listing_by'] == 'all'),
  38. 'search' => array($txt['mlist_search'], 'MLSearch', $context['listing_by'] == 'search'),
  39. );
  40. // Set up the sort links.
  41. $context['sort_links'] = array();
  42. foreach ($subActions as $act => $text)
  43. {
  44. $context['sort_links'][] = array(
  45. 'label' => $text[0],
  46. 'action' => $act,
  47. 'selected' => $text[2],
  48. );
  49. }
  50. $context['num_members'] = $modSettings['totalMembers'];
  51. // Set up the columns...
  52. $context['columns'] = array(
  53. 'is_online' => array(
  54. 'label' => $txt['status'],
  55. 'class' => 'first_th',
  56. 'sort' => array(
  57. 'down' => allowedTo('moderate_forum') ? 'IFNULL(lo.log_time, 1) ASC, real_name ASC' : 'CASE WHEN mem.show_online THEN IFNULL(lo.log_time, 1) ELSE 1 END ASC, real_name ASC',
  58. 'up' => allowedTo('moderate_forum') ? 'IFNULL(lo.log_time, 1) DESC, real_name DESC' : 'CASE WHEN mem.show_online THEN IFNULL(lo.log_time, 1) ELSE 1 END DESC, real_name DESC'
  59. ),
  60. ),
  61. 'real_name' => array(
  62. 'label' => $txt['name'],
  63. 'sort' => array(
  64. 'down' => 'mem.real_name DESC',
  65. 'up' => 'mem.real_name ASC'
  66. ),
  67. ),
  68. 'email_address' => array(
  69. 'label' => $txt['email'],
  70. 'sort' => array(
  71. 'down' => allowedTo('moderate_forum') ? 'mem.email_address DESC' : 'mem.hide_email DESC, mem.email_address DESC',
  72. 'up' => allowedTo('moderate_forum') ? 'mem.email_address ASC' : 'mem.hide_email ASC, mem.email_address ASC'
  73. ),
  74. ),
  75. 'website_url' => array(
  76. 'label' => $txt['website'],
  77. 'link_with' => 'website',
  78. 'sort' => array(
  79. 'down' => 'LENGTH(mem.website_url) > 0 ASC, IFNULL(mem.website_url, 1=1) DESC, mem.website_url DESC',
  80. 'up' => 'LENGTH(mem.website_url) > 0 DESC, IFNULL(mem.website_url, 1=1) ASC, mem.website_url ASC'
  81. ),
  82. ),
  83. 'icq' => array(
  84. 'label' => $txt['icq'],
  85. 'sort' => array(
  86. 'down' => 'LENGTH(mem.icq) > 0 ASC, mem.icq = 0 DESC, mem.icq DESC',
  87. 'up' => 'LENGTH(mem.icq) > 0 DESC, mem.icq = 0 ASC, mem.icq ASC'
  88. ),
  89. ),
  90. 'aim' => array(
  91. 'label' => $txt['aim'],
  92. 'sort' => array(
  93. 'down' => 'LENGTH(mem.aim) > 0 ASC, IFNULL(mem.aim, 1=1) DESC, mem.aim DESC',
  94. 'up' => 'LENGTH(mem.aim) > 0 DESC, IFNULL(mem.aim, 1=1) ASC, mem.aim ASC'
  95. ),
  96. ),
  97. 'yim' => array(
  98. 'label' => $txt['yim'],
  99. 'sort' => array(
  100. 'down' => 'LENGTH(mem.yim) > 0 ASC, IFNULL(mem.yim, 1=1) DESC, mem.yim DESC',
  101. 'up' => 'LENGTH(mem.yim) > 0 DESC, IFNULL(mem.yim, 1=1) ASC, mem.yim ASC'
  102. ),
  103. ),
  104. 'skype' => array(
  105. 'label' => $txt['skype'],
  106. 'sort' => array(
  107. 'down' => 'LENGTH(mem.skype) > 0 ASC, IFNULL(mem.skype, 1=1) DESC, mem.skype DESC',
  108. 'up' => 'LENGTH(mem.skype) > 0 DESC, IFNULL(mem.skype, 1=1) ASC, mem.skype ASC',
  109. ),
  110. ),
  111. 'id_group' => array(
  112. 'label' => $txt['position'],
  113. 'sort' => array(
  114. 'down' => 'IFNULL(mg.group_name, 1=1) DESC, mg.group_name DESC',
  115. 'up' => 'IFNULL(mg.group_name, 1=1) ASC, mg.group_name ASC'
  116. ),
  117. ),
  118. 'registered' => array(
  119. 'label' => $txt['date_registered'],
  120. 'sort' => array(
  121. 'down' => 'mem.date_registered DESC',
  122. 'up' => 'mem.date_registered ASC'
  123. ),
  124. ),
  125. 'posts' => array(
  126. 'label' => $txt['posts'],
  127. 'colspan' => 2,
  128. 'default_sort_rev' => true,
  129. 'sort' => array(
  130. 'down' => 'mem.posts DESC',
  131. 'up' => 'mem.posts ASC'
  132. ),
  133. )
  134. );
  135. $context['colspan'] = 0;
  136. $context['disabled_fields'] = isset($modSettings['disabled_profile_fields']) ? array_flip(explode(',', $modSettings['disabled_profile_fields'])) : array();
  137. foreach ($context['columns'] as $key => $column)
  138. {
  139. if (isset($context['disabled_fields'][$key]) || (isset($column['link_with']) && isset($context['disabled_fields'][$column['link_with']])))
  140. {
  141. unset($context['columns'][$key]);
  142. continue;
  143. }
  144. $context['colspan'] += isset($column['colspan']) ? $column['colspan'] : 1;
  145. }
  146. // Aesthetic stuff.
  147. end($context['columns']);
  148. $context['columns'][key($context['columns'])]['class'] = 'last_th';
  149. $context['linktree'][] = array(
  150. 'url' => $scripturl . '?action=mlist',
  151. 'name' => $txt['members_list']
  152. );
  153. $context['can_send_pm'] = allowedTo('pm_send');
  154. $context['can_send_email'] = allowedTo('send_email_to_members');
  155. // Build the memberlist button array.
  156. $context['memberlist_buttons'] = array(
  157. 'view_all_members' => array('text' => 'view_all_members', 'image' => 'mlist.png', 'lang' => true, 'url' => $scripturl . '?action=mlist' . ';sa=all', 'active'=> true),
  158. 'mlist_search' => array('text' => 'mlist_search', 'image' => 'mlist.png', 'lang' => true, 'url' => $scripturl . '?action=mlist' . ';sa=search'),
  159. );
  160. // Allow mods to add additional buttons here
  161. call_integration_hook('integrate_memberlist_buttons');
  162. // Jump to the sub action.
  163. if (isset($subActions[$context['listing_by']]))
  164. $subActions[$context['listing_by']][1]();
  165. else
  166. $subActions['all'][1]();
  167. }
  168. /**
  169. * List all members, page by page, with sorting.
  170. * Called from MemberList().
  171. * Can be passed a sort parameter, to order the display of members.
  172. * Calls printMemberListRows to retrieve the results of the query.
  173. */
  174. function MLAll()
  175. {
  176. global $txt, $scripturl;
  177. global $modSettings, $context, $smcFunc;
  178. // The chunk size for the cached index.
  179. $cache_step_size = 500;
  180. // Only use caching if:
  181. // 1. there are at least 2k members,
  182. // 2. the default sorting method (real_name) is being used,
  183. // 3. the page shown is high enough to make a DB filesort unprofitable.
  184. $use_cache = $modSettings['totalMembers'] > 2000 && (!isset($_REQUEST['sort']) || $_REQUEST['sort'] === 'real_name') && isset($_REQUEST['start']) && $_REQUEST['start'] > $cache_step_size;
  185. if ($use_cache)
  186. {
  187. // Maybe there's something cached already.
  188. if (!empty($modSettings['memberlist_cache']))
  189. $memberlist_cache = @unserialize($modSettings['memberlist_cache']);
  190. // The chunk size for the cached index.
  191. $cache_step_size = 500;
  192. // Only update the cache if something changed or no cache existed yet.
  193. if (empty($memberlist_cache) || empty($modSettings['memberlist_updated']) || $memberlist_cache['last_update'] < $modSettings['memberlist_updated'])
  194. {
  195. $request = $smcFunc['db_query']('', '
  196. SELECT real_name
  197. FROM {db_prefix}members
  198. WHERE is_activated = {int:is_activated}
  199. ORDER BY real_name',
  200. array(
  201. 'is_activated' => 1,
  202. )
  203. );
  204. $memberlist_cache = array(
  205. 'last_update' => time(),
  206. 'num_members' => $smcFunc['db_num_rows']($request),
  207. 'index' => array(),
  208. );
  209. for ($i = 0, $n = $smcFunc['db_num_rows']($request); $i < $n; $i += $cache_step_size)
  210. {
  211. $smcFunc['db_data_seek']($request, $i);
  212. list($memberlist_cache['index'][$i]) = $smcFunc['db_fetch_row']($request);
  213. }
  214. $smcFunc['db_data_seek']($request, $memberlist_cache['num_members'] - 1);
  215. list ($memberlist_cache['index'][$i]) = $smcFunc['db_fetch_row']($request);
  216. $smcFunc['db_free_result']($request);
  217. // Now we've got the cache...store it.
  218. updateSettings(array('memberlist_cache' => serialize($memberlist_cache)));
  219. }
  220. $context['num_members'] = $memberlist_cache['num_members'];
  221. }
  222. // Without cache we need an extra query to get the amount of members.
  223. else
  224. {
  225. $request = $smcFunc['db_query']('', '
  226. SELECT COUNT(*)
  227. FROM {db_prefix}members
  228. WHERE is_activated = {int:is_activated}',
  229. array(
  230. 'is_activated' => 1,
  231. )
  232. );
  233. list ($context['num_members']) = $smcFunc['db_fetch_row']($request);
  234. $smcFunc['db_free_result']($request);
  235. }
  236. // Set defaults for sort (real_name) and start. (0)
  237. if (!isset($_REQUEST['sort']) || !isset($context['columns'][$_REQUEST['sort']]))
  238. $_REQUEST['sort'] = 'real_name';
  239. if (!is_numeric($_REQUEST['start']))
  240. {
  241. if (preg_match('~^[^\'\\\\/]~' . ($context['utf8'] ? 'u' : ''), $smcFunc['strtolower']($_REQUEST['start']), $match) === 0)
  242. fatal_error('Hacker?', false);
  243. $_REQUEST['start'] = $match[0];
  244. $request = $smcFunc['db_query']('substring', '
  245. SELECT COUNT(*)
  246. FROM {db_prefix}members
  247. WHERE LOWER(SUBSTRING(real_name, 1, 1)) < {string:first_letter}
  248. AND is_activated = {int:is_activated}',
  249. array(
  250. 'is_activated' => 1,
  251. 'first_letter' => $_REQUEST['start'],
  252. )
  253. );
  254. list ($_REQUEST['start']) = $smcFunc['db_fetch_row']($request);
  255. $smcFunc['db_free_result']($request);
  256. }
  257. $context['letter_links'] = '';
  258. for ($i = 97; $i < 123; $i++)
  259. $context['letter_links'] .= '<a href="' . $scripturl . '?action=mlist;sa=all;start=' . chr($i) . '#letter' . chr($i) . '">' . strtoupper(chr($i)) . '</a> ';
  260. // Sort out the column information.
  261. foreach ($context['columns'] as $col => $column_details)
  262. {
  263. $context['columns'][$col]['href'] = $scripturl . '?action=mlist;sort=' . $col . ';start=0';
  264. if ((!isset($_REQUEST['desc']) && $col == $_REQUEST['sort']) || ($col != $_REQUEST['sort'] && !empty($column_details['default_sort_rev'])))
  265. $context['columns'][$col]['href'] .= ';desc';
  266. $context['columns'][$col]['link'] = '<a href="' . $context['columns'][$col]['href'] . '" rel="nofollow">' . $context['columns'][$col]['label'] . '</a>';
  267. $context['columns'][$col]['selected'] = $_REQUEST['sort'] == $col;
  268. }
  269. // Are we sorting the results
  270. $context['sort_by'] = $_REQUEST['sort'];
  271. $context['sort_direction'] = !isset($_REQUEST['desc']) ? 'up' : 'down';
  272. // Construct the page index.
  273. $context['page_index'] = constructPageIndex($scripturl . '?action=mlist;sort=' . $_REQUEST['sort'] . (isset($_REQUEST['desc']) ? ';desc' : ''), $_REQUEST['start'], $context['num_members'], $modSettings['defaultMaxMembers']);
  274. // Send the data to the template.
  275. $context['start'] = $_REQUEST['start'] + 1;
  276. $context['end'] = min($_REQUEST['start'] + $modSettings['defaultMaxMembers'], $context['num_members']);
  277. $context['can_moderate_forum'] = allowedTo('moderate_forum');
  278. $context['page_title'] = sprintf($txt['viewing_members'], $context['start'], $context['end']);
  279. $context['linktree'][] = array(
  280. 'url' => $scripturl . '?action=mlist;sort=' . $_REQUEST['sort'] . ';start=' . $_REQUEST['start'],
  281. 'name' => &$context['page_title'],
  282. 'extra_after' => '(' . sprintf($txt['of_total_members'], $context['num_members']) . ')'
  283. );
  284. $limit = $_REQUEST['start'];
  285. $query_parameters = array(
  286. 'regular_id_group' => 0,
  287. 'is_activated' => 1,
  288. 'sort' => $context['columns'][$_REQUEST['sort']]['sort'][$context['sort_direction']],
  289. );
  290. // Using cache allows to narrow down the list to be retrieved.
  291. if ($use_cache && $_REQUEST['sort'] === 'real_name' && !isset($_REQUEST['desc']))
  292. {
  293. $first_offset = $_REQUEST['start'] - ($_REQUEST['start'] % $cache_step_size);
  294. $second_offset = ceil(($_REQUEST['start'] + $modSettings['defaultMaxMembers']) / $cache_step_size) * $cache_step_size;
  295. $where = 'mem.real_name BETWEEN {string:real_name_low} AND {string:real_name_high}';
  296. $query_parameters['real_name_low'] = $memberlist_cache['index'][$first_offset];
  297. $query_parameters['real_name_high'] = $memberlist_cache['index'][$second_offset];
  298. $limit -= $first_offset;
  299. }
  300. // Reverse sorting is a bit more complicated...
  301. elseif ($use_cache && $_REQUEST['sort'] === 'real_name')
  302. {
  303. $first_offset = floor(($memberlist_cache['num_members'] - $modSettings['defaultMaxMembers'] - $_REQUEST['start']) / $cache_step_size) * $cache_step_size;
  304. if ($first_offset < 0)
  305. $first_offset = 0;
  306. $second_offset = ceil(($memberlist_cache['num_members'] - $_REQUEST['start']) / $cache_step_size) * $cache_step_size;
  307. $where = 'mem.real_name BETWEEN {string:real_name_low} AND {string:real_name_high}';
  308. $query_parameters['real_name_low'] = $memberlist_cache['index'][$first_offset];
  309. $query_parameters['real_name_high'] = $memberlist_cache['index'][$second_offset];
  310. $limit = $second_offset - ($memberlist_cache['num_members'] - $_REQUEST['start']) - ($second_offset > $memberlist_cache['num_members'] ? $cache_step_size - ($memberlist_cache['num_members'] % $cache_step_size) : 0);
  311. }
  312. // Select the members from the database.
  313. $request = $smcFunc['db_query']('', '
  314. SELECT mem.id_member
  315. FROM {db_prefix}members AS mem' . ($_REQUEST['sort'] === 'is_online' ? '
  316. LEFT JOIN {db_prefix}log_online AS lo ON (lo.id_member = mem.id_member)' : '') . ($_REQUEST['sort'] === 'id_group' ? '
  317. LEFT JOIN {db_prefix}membergroups AS mg ON (mg.id_group = CASE WHEN mem.id_group = {int:regular_id_group} THEN mem.id_post_group ELSE mem.id_group END)' : '') . '
  318. WHERE mem.is_activated = {int:is_activated}' . (empty($where) ? '' : '
  319. AND ' . $where) . '
  320. ORDER BY {raw:sort}
  321. LIMIT ' . $limit . ', ' . $modSettings['defaultMaxMembers'],
  322. $query_parameters
  323. );
  324. printMemberListRows($request);
  325. $smcFunc['db_free_result']($request);
  326. // Add anchors at the start of each letter.
  327. if ($_REQUEST['sort'] == 'real_name')
  328. {
  329. $last_letter = '';
  330. foreach ($context['members'] as $i => $dummy)
  331. {
  332. $this_letter = $smcFunc['strtolower']($smcFunc['substr']($context['members'][$i]['name'], 0, 1));
  333. if ($this_letter != $last_letter && preg_match('~[a-z]~', $this_letter) === 1)
  334. {
  335. $context['members'][$i]['sort_letter'] = $smcFunc['htmlspecialchars']($this_letter);
  336. $last_letter = $this_letter;
  337. }
  338. }
  339. }
  340. }
  341. /**
  342. * Search for members, or display search results.
  343. * - Called by MemberList().
  344. * - If variable 'search' is empty displays search dialog box, using the search sub template.
  345. * - Calls printMemberListRows to retrieve the results of the query.
  346. */
  347. function MLSearch()
  348. {
  349. global $txt, $scripturl, $context, $user_info, $modSettings, $smcFunc;
  350. $context['page_title'] = $txt['mlist_search'];
  351. $context['can_moderate_forum'] = allowedTo('moderate_forum');
  352. // Can they search custom fields?
  353. $request = $smcFunc['db_query']('', '
  354. SELECT col_name, field_name, field_desc
  355. FROM {db_prefix}custom_fields
  356. WHERE active = {int:active}
  357. ' . (allowedTo('admin_forum') ? '' : ' AND private < {int:private_level}') . '
  358. AND can_search = {int:can_search}
  359. AND (field_type = {string:field_type_text} OR field_type = {string:field_type_textarea})',
  360. array(
  361. 'active' => 1,
  362. 'can_search' => 1,
  363. 'private_level' => 2,
  364. 'field_type_text' => 'text',
  365. 'field_type_textarea' => 'textarea',
  366. )
  367. );
  368. $context['custom_search_fields'] = array();
  369. while ($row = $smcFunc['db_fetch_assoc']($request))
  370. $context['custom_search_fields'][$row['col_name']] = array(
  371. 'colname' => $row['col_name'],
  372. 'name' => $row['field_name'],
  373. 'desc' => $row['field_desc'],
  374. );
  375. $smcFunc['db_free_result']($request);
  376. // They're searching..
  377. if (isset($_REQUEST['search']) && isset($_REQUEST['fields']))
  378. {
  379. $_POST['search'] = trim(isset($_GET['search']) ? $_GET['search'] : $_POST['search']);
  380. $_POST['fields'] = isset($_GET['fields']) ? explode(',', $_GET['fields']) : $_POST['fields'];
  381. $context['old_search'] = $_REQUEST['search'];
  382. $context['old_search_value'] = urlencode($_REQUEST['search']);
  383. // No fields? Use default...
  384. if (empty($_POST['fields']))
  385. $_POST['fields'] = array('name');
  386. // Set defaults for how the results are sorted
  387. if (!isset($_REQUEST['sort']) || !isset($context['columns'][$_REQUEST['sort']]))
  388. $_REQUEST['sort'] = 'real_name';
  389. // Build the column link / sort information.
  390. foreach ($context['columns'] as $col => $column_details)
  391. {
  392. $context['columns'][$col]['href'] = $scripturl . '?action=mlist;sa=search;start=0;sort=' . $col;
  393. if ((!isset($_REQUEST['desc']) && $col == $_REQUEST['sort']) || ($col != $_REQUEST['sort'] && !empty($column_details['default_sort_rev'])))
  394. $context['columns'][$col]['href'] .= ';desc';
  395. if (isset($_POST['search']) && isset($_POST['fields']))
  396. $context['columns'][$col]['href'] .= ';search=' . $_POST['search'] . ';fields=' . implode(',', $_POST['fields']);
  397. $context['columns'][$col]['link'] = '<a href="' . $context['columns'][$col]['href'] . '" rel="nofollow">' . $context['columns'][$col]['label'] . '</a>';
  398. $context['columns'][$col]['selected'] = $_REQUEST['sort'] == $col;
  399. }
  400. // set up some things for use in the template
  401. $context['sort_direction'] = !isset($_REQUEST['desc']) ? 'up' : 'down';
  402. $context['sort_by'] = $_REQUEST['sort'];
  403. $query_parameters = array(
  404. 'regular_id_group' => 0,
  405. 'is_activated' => 1,
  406. 'blank_string' => '',
  407. 'search' => '%' . strtr($smcFunc['htmlspecialchars']($_POST['search'], ENT_QUOTES), array('_' => '\\_', '%' => '\\%', '*' => '%')) . '%',
  408. 'sort' => $context['columns'][$_REQUEST['sort']]['sort'][$context['sort_direction']],
  409. );
  410. // Search for a name
  411. if (in_array('name', $_POST['fields']))
  412. $fields = allowedTo('moderate_forum') ? array('member_name', 'real_name') : array('real_name');
  413. else
  414. $fields = array();
  415. // Search for messengers...
  416. if (in_array('messenger', $_POST['fields']) && !$user_info['is_guest'])
  417. $fields += array(3 => 'aim', 'icq', 'yim', 'skype');
  418. // Search for websites.
  419. if (in_array('website', $_POST['fields']))
  420. $fields += array(7 => 'website_title', 'website_url');
  421. // Search for groups.
  422. if (in_array('group', $_POST['fields']))
  423. $fields += array(9 => 'IFNULL(group_name, {string:blank_string})');
  424. // Search for an email address?
  425. if (in_array('email', $_POST['fields']))
  426. {
  427. $fields += array(2 => allowedTo('moderate_forum') ? 'email_address' : '(hide_email = 0 AND email_address');
  428. $condition = allowedTo('moderate_forum') ? '' : ')';
  429. }
  430. else
  431. $condition = '';
  432. if ($smcFunc['db_case_sensitive'])
  433. foreach ($fields as $key => $field)
  434. $fields[$key] = 'LOWER(' . $field . ')';
  435. $customJoin = array();
  436. $customCount = 10;
  437. // Any custom fields to search for - these being tricky?
  438. foreach ($_POST['fields'] as $field)
  439. {
  440. $curField = substr($field, 5);
  441. if (substr($field, 0, 5) == 'cust_' && isset($context['custom_search_fields'][$curField]))
  442. {
  443. $customJoin[] = 'LEFT JOIN {db_prefix}themes AS t' . $curField . ' ON (t' . $curField . '.variable = {string:t' . $curField . '} AND t' . $curField . '.id_theme = 1 AND t' . $curField . '.id_member = mem.id_member)';
  444. $query_parameters['t' . $curField] = $curField;
  445. $fields += array($customCount++ => 'IFNULL(t' . $curField . '.value, {string:blank_string})');
  446. }
  447. }
  448. $query = $_POST['search'] == '' ? '= {string:blank_string}' : ($smcFunc['db_case_sensitive'] ? 'LIKE LOWER({string:search})' : 'LIKE {string:search}');
  449. $request = $smcFunc['db_query']('', '
  450. SELECT COUNT(*)
  451. FROM {db_prefix}members AS mem
  452. LEFT JOIN {db_prefix}membergroups AS mg ON (mg.id_group = CASE WHEN mem.id_group = {int:regular_id_group} THEN mem.id_post_group ELSE mem.id_group END)' .
  453. (empty($customJoin) ? '' : implode('
  454. ', $customJoin)) . '
  455. WHERE (' . implode( ' ' . $query . ' OR ', $fields) . ' ' . $query . $condition . ')
  456. AND mem.is_activated = {int:is_activated}',
  457. $query_parameters
  458. );
  459. list ($numResults) = $smcFunc['db_fetch_row']($request);
  460. $smcFunc['db_free_result']($request);
  461. $context['page_index'] = constructPageIndex($scripturl . '?action=mlist;sa=search;search=' . $_POST['search'] . ';fields=' . implode(',', $_POST['fields']), $_REQUEST['start'], $numResults, $modSettings['defaultMaxMembers']);
  462. // Find the members from the database.
  463. $request = $smcFunc['db_query']('', '
  464. SELECT mem.id_member
  465. FROM {db_prefix}members AS mem
  466. LEFT JOIN {db_prefix}log_online AS lo ON (lo.id_member = mem.id_member)
  467. LEFT JOIN {db_prefix}membergroups AS mg ON (mg.id_group = CASE WHEN mem.id_group = {int:regular_id_group} THEN mem.id_post_group ELSE mem.id_group END)' .
  468. (empty($customJoin) ? '' : implode('
  469. ', $customJoin)) . '
  470. WHERE (' . implode( ' ' . $query . ' OR ', $fields) . ' ' . $query . $condition . ')
  471. AND mem.is_activated = {int:is_activated}
  472. ORDER BY {raw:sort}
  473. LIMIT ' . $_REQUEST['start'] . ', ' . $modSettings['defaultMaxMembers'],
  474. $query_parameters
  475. );
  476. printMemberListRows($request);
  477. $smcFunc['db_free_result']($request);
  478. }
  479. else
  480. {
  481. // These are all the possible fields.
  482. $context['search_fields'] = array(
  483. 'name' => $txt['mlist_search_name'],
  484. 'email' => $txt['mlist_search_email'],
  485. 'messenger' => $txt['mlist_search_messenger'],
  486. 'website' => $txt['mlist_search_website'],
  487. 'group' => $txt['mlist_search_group'],
  488. );
  489. foreach ($context['custom_search_fields'] as $field)
  490. $context['search_fields']['cust_' . $field['colname']] = sprintf($txt['mlist_search_by'], $field['name']);
  491. // What do we search for by default?
  492. $context['search_defaults'] = array('name', 'email');
  493. $context['sub_template'] = 'search';
  494. $context['old_search'] = isset($_GET['search']) ? $_GET['search'] : (isset($_POST['search']) ? $smcFunc['htmlspecialchars']($_POST['search']) : '');
  495. // Since we're nice we also want to default focus on to the search field.
  496. addInlineJavascript('
  497. $(\'input[name="search"]\').focus();', true);
  498. }
  499. $context['linktree'][] = array(
  500. 'url' => $scripturl . '?action=mlist;sa=search',
  501. 'name' => &$context['page_title']
  502. );
  503. // Highlight the correct button, too!
  504. unset($context['memberlist_buttons']['view_all_members']['active']);
  505. $context['memberlist_buttons']['mlist_search']['active'] = true;
  506. }
  507. /**
  508. * Retrieves results of the request passed to it
  509. * Puts results of request into the context for the sub template.
  510. *
  511. * @param resource $request
  512. */
  513. function printMemberListRows($request)
  514. {
  515. global $context, $memberContext, $smcFunc;
  516. // Get the most posts.
  517. $result = $smcFunc['db_query']('', '
  518. SELECT MAX(posts)
  519. FROM {db_prefix}members',
  520. array(
  521. )
  522. );
  523. list ($most_posts) = $smcFunc['db_fetch_row']($result);
  524. $smcFunc['db_free_result']($result);
  525. // Avoid division by zero...
  526. if ($most_posts == 0)
  527. $most_posts = 1;
  528. $members = array();
  529. while ($row = $smcFunc['db_fetch_assoc']($request))
  530. $members[] = $row['id_member'];
  531. // Load all the members for display.
  532. loadMemberData($members);
  533. $context['members'] = array();
  534. foreach ($members as $member)
  535. {
  536. if (!loadMemberContext($member))
  537. continue;
  538. $context['members'][$member] = $memberContext[$member];
  539. $context['members'][$member]['post_percent'] = round(($context['members'][$member]['real_posts'] * 100) / $most_posts);
  540. $context['members'][$member]['registered_date'] = strftime('%Y-%m-%d', $context['members'][$member]['registered_timestamp']);
  541. }
  542. }
  543. ?>