Memberlist.php 23 KB

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