Memberlist.php 22 KB

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