Memberlist.php 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666
  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 2011 Simple Machines
  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('Hacking attempt...');
  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, $settings, $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. $context['sort_links'][] = array(
  44. 'label' => $text[0],
  45. 'action' => $act,
  46. 'selected' => $text[2],
  47. );
  48. $context['num_members'] = $modSettings['totalMembers'];
  49. // Set up the columns...
  50. $context['columns'] = array(
  51. 'is_online' => array(
  52. 'label' => $txt['status'],
  53. 'width' => '60',
  54. 'class' => 'first_th',
  55. ),
  56. 'real_name' => array(
  57. 'label' => $txt['username']
  58. ),
  59. 'email_address' => array(
  60. 'label' => $txt['email'],
  61. 'width' => '25'
  62. ),
  63. 'website_url' => array(
  64. 'label' => $txt['website'],
  65. 'width' => '70',
  66. 'link_with' => 'website',
  67. ),
  68. 'icq' => array(
  69. 'label' => $txt['icq'],
  70. 'width' => '30'
  71. ),
  72. 'aim' => array(
  73. 'label' => $txt['aim'],
  74. 'width' => '30'
  75. ),
  76. 'yim' => array(
  77. 'label' => $txt['yim'],
  78. 'width' => '30'
  79. ),
  80. 'msn' => array(
  81. 'label' => $txt['msn'],
  82. 'width' => '30'
  83. ),
  84. 'id_group' => array(
  85. 'label' => $txt['position']
  86. ),
  87. 'registered' => array(
  88. 'label' => $txt['date_registered']
  89. ),
  90. 'posts' => array(
  91. 'label' => $txt['posts'],
  92. 'width' => '115',
  93. 'colspan' => '2',
  94. 'default_sort_rev' => true,
  95. )
  96. );
  97. $context['colspan'] = 0;
  98. $context['disabled_fields'] = isset($modSettings['disabled_profile_fields']) ? array_flip(explode(',', $modSettings['disabled_profile_fields'])) : array();
  99. foreach ($context['columns'] as $key => $column)
  100. {
  101. if (isset($context['disabled_fields'][$key]) || (isset($column['link_with']) && isset($context['disabled_fields'][$column['link_with']])))
  102. {
  103. unset($context['columns'][$key]);
  104. continue;
  105. }
  106. $context['colspan'] += isset($column['colspan']) ? $column['colspan'] : 1;
  107. }
  108. // Aesthetic stuff.
  109. end($context['columns']);
  110. $context['columns'][key($context['columns'])]['class'] = 'last_th';
  111. $context['linktree'][] = array(
  112. 'url' => $scripturl . '?action=mlist',
  113. 'name' => $txt['members_list']
  114. );
  115. $context['can_send_pm'] = allowedTo('pm_send');
  116. // Jump to the sub action.
  117. if (isset($subActions[$context['listing_by']]))
  118. $subActions[$context['listing_by']][1]();
  119. else
  120. $subActions['all'][1]();
  121. }
  122. /**
  123. * List all members, page by page, with sorting.
  124. * Called from MemberList().
  125. * Can be passed a sort parameter, to order the display of members.
  126. * Calls printMemberListRows to retrieve the results of the query.
  127. */
  128. function MLAll()
  129. {
  130. global $txt, $scripturl, $user_info;
  131. global $modSettings, $context, $smcFunc;
  132. // The chunk size for the cached index.
  133. $cache_step_size = 500;
  134. // Only use caching if:
  135. // 1. there are at least 2k members,
  136. // 2. the default sorting method (real_name) is being used,
  137. // 3. the page shown is high enough to make a DB filesort unprofitable.
  138. $use_cache = $modSettings['totalMembers'] > 2000 && (!isset($_REQUEST['sort']) || $_REQUEST['sort'] === 'real_name') && isset($_REQUEST['start']) && $_REQUEST['start'] > $cache_step_size;
  139. if ($use_cache)
  140. {
  141. // Maybe there's something cached already.
  142. if (!empty($modSettings['memberlist_cache']))
  143. $memberlist_cache = @unserialize($modSettings['memberlist_cache']);
  144. // The chunk size for the cached index.
  145. $cache_step_size = 500;
  146. // Only update the cache if something changed or no cache existed yet.
  147. if (empty($memberlist_cache) || empty($modSettings['memberlist_updated']) || $memberlist_cache['last_update'] < $modSettings['memberlist_updated'])
  148. {
  149. $request = $smcFunc['db_query']('', '
  150. SELECT real_name
  151. FROM {db_prefix}members
  152. WHERE is_activated = {int:is_activated}
  153. ORDER BY real_name',
  154. array(
  155. 'is_activated' => 1,
  156. )
  157. );
  158. $memberlist_cache = array(
  159. 'last_update' => time(),
  160. 'num_members' => $smcFunc['db_num_rows']($request),
  161. 'index' => array(),
  162. );
  163. for ($i = 0, $n = $smcFunc['db_num_rows']($request); $i < $n; $i += $cache_step_size)
  164. {
  165. $smcFunc['db_data_seek']($request, $i);
  166. list($memberlist_cache['index'][$i]) = $smcFunc['db_fetch_row']($request);
  167. }
  168. $smcFunc['db_data_seek']($request, $memberlist_cache['num_members'] - 1);
  169. list ($memberlist_cache['index'][$i]) = $smcFunc['db_fetch_row']($request);
  170. $smcFunc['db_free_result']($request);
  171. // Now we've got the cache...store it.
  172. updateSettings(array('memberlist_cache' => serialize($memberlist_cache)));
  173. }
  174. $context['num_members'] = $memberlist_cache['num_members'];
  175. }
  176. // Without cache we need an extra query to get the amount of members.
  177. else
  178. {
  179. $request = $smcFunc['db_query']('', '
  180. SELECT COUNT(*)
  181. FROM {db_prefix}members
  182. WHERE is_activated = {int:is_activated}',
  183. array(
  184. 'is_activated' => 1,
  185. )
  186. );
  187. list ($context['num_members']) = $smcFunc['db_fetch_row']($request);
  188. $smcFunc['db_free_result']($request);
  189. }
  190. // Set defaults for sort (real_name) and start. (0)
  191. if (!isset($_REQUEST['sort']) || !isset($context['columns'][$_REQUEST['sort']]))
  192. $_REQUEST['sort'] = 'real_name';
  193. if (!is_numeric($_REQUEST['start']))
  194. {
  195. if (preg_match('~^[^\'\\\\/]~' . ($context['utf8'] ? 'u' : ''), $smcFunc['strtolower']($_REQUEST['start']), $match) === 0)
  196. fatal_error('Hacker?', false);
  197. $_REQUEST['start'] = $match[0];
  198. $request = $smcFunc['db_query']('substring', '
  199. SELECT COUNT(*)
  200. FROM {db_prefix}members
  201. WHERE LOWER(SUBSTRING(real_name, 1, 1)) < {string:first_letter}
  202. AND is_activated = {int:is_activated}',
  203. array(
  204. 'is_activated' => 1,
  205. 'first_letter' => $_REQUEST['start'],
  206. )
  207. );
  208. list ($_REQUEST['start']) = $smcFunc['db_fetch_row']($request);
  209. $smcFunc['db_free_result']($request);
  210. }
  211. $context['letter_links'] = '';
  212. for ($i = 97; $i < 123; $i++)
  213. $context['letter_links'] .= '<a href="' . $scripturl . '?action=mlist;sa=all;start=' . chr($i) . '#letter' . chr($i) . '">' . strtoupper(chr($i)) . '</a> ';
  214. // Sort out the column information.
  215. foreach ($context['columns'] as $col => $column_details)
  216. {
  217. $context['columns'][$col]['href'] = $scripturl . '?action=mlist;sort=' . $col . ';start=0';
  218. if ((!isset($_REQUEST['desc']) && $col == $_REQUEST['sort']) || ($col != $_REQUEST['sort'] && !empty($column_details['default_sort_rev'])))
  219. $context['columns'][$col]['href'] .= ';desc';
  220. $context['columns'][$col]['link'] = '<a href="' . $context['columns'][$col]['href'] . '" rel="nofollow">' . $context['columns'][$col]['label'] . '</a>';
  221. $context['columns'][$col]['selected'] = $_REQUEST['sort'] == $col;
  222. }
  223. $context['sort_by'] = $_REQUEST['sort'];
  224. $context['sort_direction'] = !isset($_REQUEST['desc']) ? 'up' : 'down';
  225. // Construct the page index.
  226. $context['page_index'] = constructPageIndex($scripturl . '?action=mlist;sort=' . $_REQUEST['sort'] . (isset($_REQUEST['desc']) ? ';desc' : ''), $_REQUEST['start'], $context['num_members'], $modSettings['defaultMaxMembers']);
  227. // Send the data to the template.
  228. $context['start'] = $_REQUEST['start'] + 1;
  229. $context['end'] = min($_REQUEST['start'] + $modSettings['defaultMaxMembers'], $context['num_members']);
  230. $context['can_moderate_forum'] = allowedTo('moderate_forum');
  231. $context['page_title'] = sprintf($txt['viewing_members'], $context['start'], $context['end']);
  232. $context['linktree'][] = array(
  233. 'url' => $scripturl . '?action=mlist;sort=' . $_REQUEST['sort'] . ';start=' . $_REQUEST['start'],
  234. 'name' => &$context['page_title'],
  235. 'extra_after' => ' (' . sprintf($txt['of_total_members'], $context['num_members']) . ')'
  236. );
  237. // List out the different sorting methods...
  238. $sort_methods = array(
  239. 'is_online' => array(
  240. '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',
  241. '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'
  242. ),
  243. 'real_name' => array(
  244. 'down' => 'mem.real_name DESC',
  245. 'up' => 'mem.real_name ASC'
  246. ),
  247. 'email_address' => array(
  248. 'down' => allowedTo('moderate_forum') ? 'mem.email_address DESC' : 'mem.hide_email DESC, mem.email_address DESC',
  249. 'up' => allowedTo('moderate_forum') ? 'mem.email_address ASC' : 'mem.hide_email ASC, mem.email_address ASC'
  250. ),
  251. 'website_url' => array(
  252. 'down' => 'LENGTH(mem.website_url) > 0 ASC, IFNULL(mem.website_url, 1=1) DESC, mem.website_url DESC',
  253. 'up' => 'LENGTH(mem.website_url) > 0 DESC, IFNULL(mem.website_url, 1=1) ASC, mem.website_url ASC'
  254. ),
  255. 'icq' => array(
  256. 'down' => 'LENGTH(mem.icq) > 0 ASC, mem.icq = 0 DESC, mem.icq DESC',
  257. 'up' => 'LENGTH(mem.icq) > 0 DESC, mem.icq = 0 ASC, mem.icq ASC'
  258. ),
  259. 'aim' => array(
  260. 'down' => 'LENGTH(mem.aim) > 0 ASC, IFNULL(mem.aim, 1=1) DESC, mem.aim DESC',
  261. 'up' => 'LENGTH(mem.aim) > 0 DESC, IFNULL(mem.aim, 1=1) ASC, mem.aim ASC'
  262. ),
  263. 'yim' => array(
  264. 'down' => 'LENGTH(mem.yim) > 0 ASC, IFNULL(mem.yim, 1=1) DESC, mem.yim DESC',
  265. 'up' => 'LENGTH(mem.yim) > 0 DESC, IFNULL(mem.yim, 1=1) ASC, mem.yim ASC'
  266. ),
  267. 'msn' => array(
  268. 'down' => 'LENGTH(mem.msn) > 0 ASC, IFNULL(mem.msn, 1=1) DESC, mem.msn DESC',
  269. 'up' => 'LENGTH(mem.msn) > 0 DESC, IFNULL(mem.msn, 1=1) ASC, mem.msn ASC'
  270. ),
  271. 'registered' => array(
  272. 'down' => 'mem.date_registered DESC',
  273. 'up' => 'mem.date_registered ASC'
  274. ),
  275. 'id_group' => array(
  276. 'down' => 'IFNULL(mg.group_name, 1=1) DESC, mg.group_name DESC',
  277. 'up' => 'IFNULL(mg.group_name, 1=1) ASC, mg.group_name ASC'
  278. ),
  279. 'posts' => array(
  280. 'down' => 'mem.posts DESC',
  281. 'up' => 'mem.posts ASC'
  282. )
  283. );
  284. $limit = $_REQUEST['start'];
  285. $query_parameters = array(
  286. 'regular_id_group' => 0,
  287. 'is_activated' => 1,
  288. 'sort' => $sort_methods[$_REQUEST['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'] = 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
  345. * search sub template.
  346. * Calls printMemberListRows to retrieve the results of the query.
  347. */
  348. function MLSearch()
  349. {
  350. global $txt, $scripturl, $context, $user_info, $modSettings, $smcFunc;
  351. $context['page_title'] = $txt['mlist_search'];
  352. $context['can_moderate_forum'] = allowedTo('moderate_forum');
  353. // Can they search custom fields?
  354. $request = $smcFunc['db_query']('', '
  355. SELECT col_name, field_name, field_desc
  356. FROM {db_prefix}custom_fields
  357. WHERE active = {int:active}
  358. ' . (allowedTo('admin_forum') ? '' : ' AND private < {int:private_level}') . '
  359. AND can_search = {int:can_search}
  360. AND (field_type = {string:field_type_text} OR field_type = {string:field_type_textarea})',
  361. array(
  362. 'active' => 1,
  363. 'can_search' => 1,
  364. 'private_level' => 2,
  365. 'field_type_text' => 'text',
  366. 'field_type_textarea' => 'textarea',
  367. )
  368. );
  369. $context['custom_search_fields'] = array();
  370. while ($row = $smcFunc['db_fetch_assoc']($request))
  371. $context['custom_search_fields'][$row['col_name']] = array(
  372. 'colname' => $row['col_name'],
  373. 'name' => $row['field_name'],
  374. 'desc' => $row['field_desc'],
  375. );
  376. $smcFunc['db_free_result']($request);
  377. // They're searching..
  378. if (isset($_REQUEST['search']) && isset($_REQUEST['fields']))
  379. {
  380. $_POST['search'] = trim(isset($_GET['search']) ? $_GET['search'] : $_POST['search']);
  381. $_POST['fields'] = isset($_GET['fields']) ? explode(',', $_GET['fields']) : $_POST['fields'];
  382. $context['old_search'] = $_REQUEST['search'];
  383. $context['old_search_value'] = urlencode($_REQUEST['search']);
  384. // No fields? Use default...
  385. if (empty($_POST['fields']))
  386. $_POST['fields'] = array('name');
  387. $query_parameters = array(
  388. 'regular_id_group' => 0,
  389. 'is_activated' => 1,
  390. 'blank_string' => '',
  391. 'search' => '%' . strtr($smcFunc['htmlspecialchars']($_POST['search'], ENT_QUOTES), array('_' => '\\_', '%' => '\\%', '*' => '%')) . '%',
  392. );
  393. // Search for a name?
  394. if (in_array('name', $_POST['fields']))
  395. $fields = array('member_name', 'real_name');
  396. else
  397. $fields = array();
  398. // Search for messengers...
  399. if (in_array('messenger', $_POST['fields']) && (!$user_info['is_guest'] || empty($modSettings['guest_hideContacts'])))
  400. $fields += array(3 => 'msn', 'aim', 'icq', 'yim');
  401. // Search for websites.
  402. if (in_array('website', $_POST['fields']))
  403. $fields += array(7 => 'website_title', 'website_url');
  404. // Search for groups.
  405. if (in_array('group', $_POST['fields']))
  406. $fields += array(9 => 'IFNULL(group_name, {string:blank_string})');
  407. // Search for an email address?
  408. if (in_array('email', $_POST['fields']))
  409. {
  410. $fields += array(2 => allowedTo('moderate_forum') ? 'email_address' : '(hide_email = 0 AND email_address');
  411. $condition = allowedTo('moderate_forum') ? '' : ')';
  412. }
  413. else
  414. $condition = '';
  415. $customJoin = array();
  416. $customCount = 10;
  417. // Any custom fields to search for - these being tricky?
  418. foreach ($_POST['fields'] as $field)
  419. {
  420. $curField = substr($field, 5);
  421. if (strpos($field, 'cust_') === 0 && isset($context['custom_search_fields'][$curField]))
  422. {
  423. $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)';
  424. $query_parameters['t' . $curField] = $curField;
  425. $fields += array($customCount++ => 'IFNULL(t' . $curField . '.value, {string:blank_string})');
  426. }
  427. }
  428. $query = $_POST['search'] == '' ? '= {string:blank_string}' : 'LIKE {string:search}';
  429. $request = $smcFunc['db_query']('', '
  430. SELECT COUNT(*)
  431. FROM {db_prefix}members AS mem
  432. 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)' .
  433. (empty($customJoin) ? '' : implode('
  434. ', $customJoin)) . '
  435. WHERE (' . implode( ' ' . $query . ' OR ', $fields) . ' ' . $query . $condition . ')
  436. AND mem.is_activated = {int:is_activated}',
  437. $query_parameters
  438. );
  439. list ($numResults) = $smcFunc['db_fetch_row']($request);
  440. $smcFunc['db_free_result']($request);
  441. $context['page_index'] = constructPageIndex($scripturl . '?action=mlist;sa=search;search=' . $_POST['search'] . ';fields=' . implode(',', $_POST['fields']), $_REQUEST['start'], $numResults, $modSettings['defaultMaxMembers']);
  442. // Find the members from the database.
  443. /**
  444. * @todo SLOW This query is slow.
  445. */
  446. $request = $smcFunc['db_query']('', '
  447. SELECT mem.id_member
  448. FROM {db_prefix}members AS mem
  449. LEFT JOIN {db_prefix}log_online AS lo ON (lo.id_member = mem.id_member)
  450. 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)' .
  451. (empty($customJoin) ? '' : implode('
  452. ', $customJoin)) . '
  453. WHERE (' . implode( ' ' . $query . ' OR ', $fields) . ' ' . $query . $condition . ')
  454. AND mem.is_activated = {int:is_activated}
  455. LIMIT ' . $_REQUEST['start'] . ', ' . $modSettings['defaultMaxMembers'],
  456. $query_parameters
  457. );
  458. printMemberListRows($request);
  459. $smcFunc['db_free_result']($request);
  460. }
  461. else
  462. {
  463. // These are all the possible fields.
  464. $context['search_fields'] = array(
  465. 'name' => $txt['mlist_search_name'],
  466. 'email' => $txt['mlist_search_email'],
  467. 'messenger' => $txt['mlist_search_messenger'],
  468. 'website' => $txt['mlist_search_website'],
  469. 'group' => $txt['mlist_search_group'],
  470. );
  471. foreach ($context['custom_search_fields'] as $field)
  472. $context['search_fields']['cust_' . $field['colname']] = sprintf($txt['mlist_search_by'], $field['name']);
  473. // What do we search for by default?
  474. $context['search_defaults'] = array('name', 'email');
  475. $context['sub_template'] = 'search';
  476. $context['old_search'] = isset($_GET['search']) ? $_GET['search'] : (isset($_POST['search']) ? htmlspecialchars($_POST['search']) : '');
  477. }
  478. $context['linktree'][] = array(
  479. 'url' => $scripturl . '?action=mlist;sa=search',
  480. 'name' => &$context['page_title']
  481. );
  482. }
  483. /**
  484. * Retrieves results of the request passed to it
  485. * Puts results of request into the context for the sub template.
  486. *
  487. * @param resource $request
  488. */
  489. function printMemberListRows($request)
  490. {
  491. global $scripturl, $txt, $user_info, $modSettings;
  492. global $context, $settings, $memberContext, $smcFunc;
  493. // Get the most posts.
  494. $result = $smcFunc['db_query']('', '
  495. SELECT MAX(posts)
  496. FROM {db_prefix}members',
  497. array(
  498. )
  499. );
  500. list ($MOST_POSTS) = $smcFunc['db_fetch_row']($result);
  501. $smcFunc['db_free_result']($result);
  502. // Avoid division by zero...
  503. if ($MOST_POSTS == 0)
  504. $MOST_POSTS = 1;
  505. $members = array();
  506. while ($row = $smcFunc['db_fetch_assoc']($request))
  507. $members[] = $row['id_member'];
  508. // Load all the members for display.
  509. loadMemberData($members);
  510. $context['members'] = array();
  511. foreach ($members as $member)
  512. {
  513. if (!loadMemberContext($member))
  514. continue;
  515. $context['members'][$member] = $memberContext[$member];
  516. $context['members'][$member]['post_percent'] = round(($context['members'][$member]['real_posts'] * 100) / $MOST_POSTS);
  517. $context['members'][$member]['registered_date'] = strftime('%Y-%m-%d', $context['members'][$member]['registered_timestamp']);
  518. }
  519. }
  520. /**
  521. * Used as a popup for searching members.
  522. * Uses sub template find_members of the Help template.
  523. * Also used to add members for PM's sent using wap2/imode protocol.
  524. */
  525. function JSMembers()
  526. {
  527. global $context, $scripturl, $user_info, $smcFunc;
  528. checkSession('get');
  529. if (WIRELESS)
  530. $context['sub_template'] = WIRELESS_PROTOCOL . '_pm';
  531. else
  532. {
  533. // Why is this in the Help template, you ask? Well, erm... it helps you. Does that work?
  534. loadTemplate('Help');
  535. $context['template_layers'] = array();
  536. $context['sub_template'] = 'find_members';
  537. }
  538. if (isset($_REQUEST['search']))
  539. $context['last_search'] = $smcFunc['htmlspecialchars']($_REQUEST['search'], ENT_QUOTES);
  540. else
  541. $_REQUEST['start'] = 0;
  542. // Allow the user to pass the input to be added to to the box.
  543. $context['input_box_name'] = isset($_REQUEST['input']) && preg_match('~^[\w-]+$~', $_REQUEST['input']) === 1 ? $_REQUEST['input'] : 'to';
  544. // Take the delimiter over GET in case it's \n or something.
  545. $context['delimiter'] = isset($_REQUEST['delim']) ? ($_REQUEST['delim'] == 'LB' ? "\n" : $_REQUEST['delim']) : ', ';
  546. $context['quote_results'] = !empty($_REQUEST['quote']);
  547. // List all the results.
  548. $context['results'] = array();
  549. // Some buddy related settings ;)
  550. $context['show_buddies'] = !empty($user_info['buddies']);
  551. $context['buddy_search'] = isset($_REQUEST['buddies']);
  552. // If the user has done a search, well - search.
  553. if (isset($_REQUEST['search']))
  554. {
  555. loadFile('Subs-Members.php');
  556. $_REQUEST['search'] = $smcFunc['htmlspecialchars']($_REQUEST['search'], ENT_QUOTES);
  557. $context['results'] = findMembers(array($_REQUEST['search']), true, $context['buddy_search']);
  558. $total_results = count($context['results']);
  559. $context['page_index'] = constructPageIndex($scripturl . '?action=findmember;search=' . $context['last_search'] . ';' . $context['session_var'] . '=' . $context['session_id'] . ';input=' . $context['input_box_name'] . ($context['quote_results'] ? ';quote=1' : '') . ($context['buddy_search'] ? ';buddies' : ''), $_REQUEST['start'], $total_results, 7);
  560. // Determine the navigation context (especially useful for the wireless template).
  561. $base_url = $scripturl . '?action=findmember;search=' . urlencode($context['last_search']) . (empty($_REQUEST['u']) ? '' : ';u=' . $_REQUEST['u']) . ';' . $context['session_var'] . '=' . $context['session_id'];
  562. $context['links'] = array(
  563. 'first' => $_REQUEST['start'] >= 7 ? $base_url . ';start=0' : '',
  564. 'prev' => $_REQUEST['start'] >= 7 ? $base_url . ';start=' . ($_REQUEST['start'] - 7) : '',
  565. 'next' => $_REQUEST['start'] + 7 < $total_results ? $base_url . ';start=' . ($_REQUEST['start'] + 7) : '',
  566. 'last' => $_REQUEST['start'] + 7 < $total_results ? $base_url . ';start=' . (floor(($total_results - 1) / 7) * 7) : '',
  567. 'up' => $scripturl . '?action=pm;sa=send' . (empty($_REQUEST['u']) ? '' : ';u=' . $_REQUEST['u']),
  568. );
  569. $context['page_info'] = array(
  570. 'current_page' => $_REQUEST['start'] / 7 + 1,
  571. 'num_pages' => floor(($total_results - 1) / 7) + 1
  572. );
  573. $context['results'] = array_slice($context['results'], $_REQUEST['start'], 7);
  574. }
  575. else
  576. $context['links']['up'] = $scripturl . '?action=pm;sa=send' . (empty($_REQUEST['u']) ? '' : ';u=' . $_REQUEST['u']);
  577. }
  578. ?>