1 | <?php |
||
2 | |||
3 | /** |
||
4 | * This file contains the functions for displaying and searching in the |
||
5 | * members list. |
||
6 | * |
||
7 | * Simple Machines Forum (SMF) |
||
8 | * |
||
9 | * @package SMF |
||
10 | * @author Simple Machines https://www.simplemachines.org |
||
11 | * @copyright 2022 Simple Machines and individual contributors |
||
12 | * @license https://www.simplemachines.org/about/smf/license.php BSD |
||
13 | * |
||
14 | * @version 2.1.2 |
||
15 | */ |
||
16 | |||
17 | if (!defined('SMF')) |
||
18 | die('No direct access...'); |
||
19 | |||
20 | /** |
||
21 | * Shows a listing of registered members. |
||
22 | * - If a subaction is not specified, lists all registered members. |
||
23 | * - It allows searching for members with the 'search' sub action. |
||
24 | * - It calls MLAll or MLSearch depending on the sub action. |
||
25 | * - Requires the view_mlist permission. |
||
26 | * - Accessed via ?action=mlist. |
||
27 | * |
||
28 | * Uses Memberlist template, main sub template. |
||
29 | */ |
||
30 | function Memberlist() |
||
31 | { |
||
32 | global $scripturl, $txt, $modSettings, $context; |
||
33 | |||
34 | // Make sure they can view the memberlist. |
||
35 | isAllowedTo('view_mlist'); |
||
36 | |||
37 | loadTemplate('Memberlist'); |
||
38 | |||
39 | $context['listing_by'] = !empty($_GET['sa']) ? $_GET['sa'] : 'all'; |
||
40 | |||
41 | // $subActions array format: |
||
42 | // 'subaction' => array('label', 'function', 'is_selected') |
||
43 | $subActions = array( |
||
44 | 'all' => array($txt['view_all_members'], 'MLAll', $context['listing_by'] == 'all'), |
||
45 | 'search' => array($txt['mlist_search'], 'MLSearch', $context['listing_by'] == 'search'), |
||
46 | ); |
||
47 | |||
48 | // Set up the sort links. |
||
49 | $context['sort_links'] = array(); |
||
50 | foreach ($subActions as $act => $text) |
||
51 | { |
||
52 | $context['sort_links'][] = array( |
||
53 | 'label' => $text[0], |
||
54 | 'action' => $act, |
||
55 | 'selected' => $text[2], |
||
56 | ); |
||
57 | } |
||
58 | |||
59 | $context['num_members'] = $modSettings['totalMembers']; |
||
60 | |||
61 | // Set up the columns... |
||
62 | $context['columns'] = array( |
||
63 | 'is_online' => array( |
||
64 | 'label' => $txt['status'], |
||
65 | 'sort' => array( |
||
66 | 'down' => allowedTo('moderate_forum') ? 'COALESCE(lo.log_time, 1) ASC, real_name ASC' : 'CASE WHEN mem.show_online THEN COALESCE(lo.log_time, 1) ELSE 1 END ASC, real_name ASC', |
||
67 | 'up' => allowedTo('moderate_forum') ? 'COALESCE(lo.log_time, 1) DESC, real_name DESC' : 'CASE WHEN mem.show_online THEN COALESCE(lo.log_time, 1) ELSE 1 END DESC, real_name DESC' |
||
68 | ), |
||
69 | ), |
||
70 | 'real_name' => array( |
||
71 | 'label' => $txt['name'], |
||
72 | 'class' => 'lefttext', |
||
73 | 'sort' => array( |
||
74 | 'down' => 'mem.real_name DESC', |
||
75 | 'up' => 'mem.real_name ASC' |
||
76 | ), |
||
77 | ), |
||
78 | 'website_url' => array( |
||
79 | 'label' => $txt['website'], |
||
80 | 'link_with' => 'website', |
||
81 | 'sort' => array( |
||
82 | 'down' => $context['user']['is_guest'] ? '1=1' : 'mem.website_url = \'\', mem.website_url is null, mem.website_url DESC', |
||
83 | 'up' => $context['user']['is_guest'] ? ' 1=1' : 'mem.website_url != \'\', mem.website_url is not null, mem.website_url ASC' |
||
84 | ), |
||
85 | ), |
||
86 | 'id_group' => array( |
||
87 | 'label' => $txt['position'], |
||
88 | 'sort' => array( |
||
89 | 'down' => 'mg.group_name is null, mg.group_name DESC', |
||
90 | 'up' => 'mg.group_name is not null, mg.group_name ASC' |
||
91 | ), |
||
92 | ), |
||
93 | 'registered' => array( |
||
94 | 'label' => $txt['date_registered'], |
||
95 | 'sort' => array( |
||
96 | 'down' => 'mem.date_registered DESC', |
||
97 | 'up' => 'mem.date_registered ASC' |
||
98 | ), |
||
99 | ), |
||
100 | 'post_count' => array( |
||
101 | 'label' => $txt['posts'], |
||
102 | 'default_sort_rev' => true, |
||
103 | 'sort' => array( |
||
104 | 'down' => 'mem.posts DESC', |
||
105 | 'up' => 'mem.posts ASC' |
||
106 | ), |
||
107 | ) |
||
108 | ); |
||
109 | |||
110 | $context['custom_profile_fields'] = getCustFieldsMList(); |
||
111 | |||
112 | if (!empty($context['custom_profile_fields']['columns'])) |
||
113 | $context['columns'] += $context['custom_profile_fields']['columns']; |
||
114 | |||
115 | $context['colspan'] = 0; |
||
116 | $context['disabled_fields'] = isset($modSettings['disabled_profile_fields']) ? array_flip(explode(',', $modSettings['disabled_profile_fields'])) : array(); |
||
117 | foreach ($context['columns'] as $key => $column) |
||
118 | { |
||
119 | if (isset($context['disabled_fields'][$key]) || (isset($column['link_with']) && isset($context['disabled_fields'][$column['link_with']]))) |
||
120 | { |
||
121 | unset($context['columns'][$key]); |
||
122 | continue; |
||
123 | } |
||
124 | |||
125 | $context['colspan'] += isset($column['colspan']) ? $column['colspan'] : 1; |
||
126 | } |
||
127 | |||
128 | // Aesthetic stuff. |
||
129 | end($context['columns']); |
||
130 | |||
131 | $context['linktree'][] = array( |
||
132 | 'url' => $scripturl . '?action=mlist', |
||
133 | 'name' => $txt['members_list'] |
||
134 | ); |
||
135 | |||
136 | $context['can_send_pm'] = allowedTo('pm_send'); |
||
137 | $context['can_send_email'] = allowedTo('moderate_forum'); |
||
138 | |||
139 | // Build the memberlist button array. |
||
140 | $context['memberlist_buttons'] = array( |
||
141 | 'view_all_members' => array('text' => 'view_all_members', 'image' => 'mlist.png', 'url' => $scripturl . '?action=mlist' . ';sa=all', 'active' => true), |
||
142 | 'mlist_search' => array('text' => 'mlist_search', 'image' => 'mlist.png', 'url' => $scripturl . '?action=mlist' . ';sa=search'), |
||
143 | ); |
||
144 | |||
145 | // Allow mods to add additional buttons here |
||
146 | call_integration_hook('integrate_memberlist_buttons'); |
||
147 | |||
148 | // Jump to the sub action. |
||
149 | if (isset($subActions[$context['listing_by']])) |
||
150 | call_helper($subActions[$context['listing_by']][1]); |
||
151 | |||
152 | else |
||
153 | call_helper($subActions['all'][1]); |
||
154 | } |
||
155 | |||
156 | /** |
||
157 | * List all members, page by page, with sorting. |
||
158 | * Called from MemberList(). |
||
159 | * Can be passed a sort parameter, to order the display of members. |
||
160 | * Calls printMemberListRows to retrieve the results of the query. |
||
161 | */ |
||
162 | function MLAll() |
||
163 | { |
||
164 | global $txt, $scripturl; |
||
165 | global $modSettings, $context, $smcFunc; |
||
166 | |||
167 | // The chunk size for the cached index. |
||
168 | $cache_step_size = 500; |
||
169 | |||
170 | // Only use caching if: |
||
171 | // 1. there are at least 2k members, |
||
172 | // 2. the default sorting method (real_name) is being used, |
||
173 | // 3. the page shown is high enough to make a DB filesort unprofitable. |
||
174 | $use_cache = $modSettings['totalMembers'] > 2000 && (!isset($_REQUEST['sort']) || $_REQUEST['sort'] === 'real_name') && isset($_REQUEST['start']) && $_REQUEST['start'] > $cache_step_size; |
||
175 | |||
176 | if ($use_cache) |
||
177 | { |
||
178 | // Maybe there's something cached already. |
||
179 | if (!empty($modSettings['memberlist_cache'])) |
||
180 | $memberlist_cache = $smcFunc['json_decode']($modSettings['memberlist_cache'], true); |
||
181 | |||
182 | // The chunk size for the cached index. |
||
183 | $cache_step_size = 500; |
||
184 | |||
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 | |||
198 | $memberlist_cache = array( |
||
199 | 'last_update' => time(), |
||
200 | 'num_members' => $smcFunc['db_num_rows']($request), |
||
201 | 'index' => array(), |
||
202 | ); |
||
203 | |||
204 | for ($i = 0, $n = $smcFunc['db_num_rows']($request); $i < $n; $i += $cache_step_size) |
||
205 | { |
||
206 | $smcFunc['db_data_seek']($request, $i); |
||
207 | list($memberlist_cache['index'][$i]) = $smcFunc['db_fetch_row']($request); |
||
208 | } |
||
209 | $smcFunc['db_data_seek']($request, $memberlist_cache['num_members'] - 1); |
||
210 | list ($memberlist_cache['index'][$memberlist_cache['num_members'] - 1]) = $smcFunc['db_fetch_row']($request); |
||
211 | $smcFunc['db_free_result']($request); |
||
212 | |||
213 | // Now we've got the cache...store it. |
||
214 | updateSettings(array('memberlist_cache' => $smcFunc['json_encode']($memberlist_cache))); |
||
215 | } |
||
216 | |||
217 | $context['num_members'] = $memberlist_cache['num_members']; |
||
218 | } |
||
219 | |||
220 | // Without cache we need an extra query to get the amount of members. |
||
221 | else |
||
222 | { |
||
223 | $request = $smcFunc['db_query']('', ' |
||
224 | SELECT COUNT(*) |
||
225 | FROM {db_prefix}members |
||
226 | WHERE is_activated = {int:is_activated}', |
||
227 | array( |
||
228 | 'is_activated' => 1, |
||
229 | ) |
||
230 | ); |
||
231 | list ($context['num_members']) = $smcFunc['db_fetch_row']($request); |
||
232 | $smcFunc['db_free_result']($request); |
||
233 | } |
||
234 | |||
235 | // Set defaults for sort (real_name) and start. (0) |
||
236 | if (!isset($_REQUEST['sort']) || !isset($context['columns'][$_REQUEST['sort']])) |
||
237 | $_REQUEST['sort'] = 'real_name'; |
||
238 | |||
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 | |||
244 | $_REQUEST['start'] = $match[0]; |
||
245 | |||
246 | $request = $smcFunc['db_query']('substring', ' |
||
247 | SELECT COUNT(*) |
||
248 | FROM {db_prefix}members |
||
249 | WHERE LOWER(SUBSTRING(real_name, 1, 1)) < {string:first_letter} |
||
250 | AND is_activated = {int:is_activated}', |
||
251 | array( |
||
252 | 'is_activated' => 1, |
||
253 | 'first_letter' => $_REQUEST['start'], |
||
254 | ) |
||
255 | ); |
||
256 | list ($_REQUEST['start']) = $smcFunc['db_fetch_row']($request); |
||
257 | $smcFunc['db_free_result']($request); |
||
258 | } |
||
259 | |||
260 | $context['letter_links'] = ''; |
||
261 | for ($i = 97; $i < 123; $i++) |
||
262 | $context['letter_links'] .= '<a href="' . $scripturl . '?action=mlist;sa=all;start=' . chr($i) . '#letter' . chr($i) . '">' . strtoupper(chr($i)) . '</a> '; |
||
263 | |||
264 | // Sort out the column information. |
||
265 | foreach ($context['columns'] as $col => $column_details) |
||
266 | { |
||
267 | $context['columns'][$col]['href'] = $scripturl . '?action=mlist;sort=' . $col . ';start=' . $_REQUEST['start']; |
||
268 | |||
269 | if ((!isset($_REQUEST['desc']) && $col == $_REQUEST['sort']) || ($col != $_REQUEST['sort'] && !empty($column_details['default_sort_rev']))) |
||
270 | $context['columns'][$col]['href'] .= ';desc'; |
||
271 | |||
272 | $context['columns'][$col]['link'] = '<a href="' . $context['columns'][$col]['href'] . '" rel="nofollow">' . $context['columns'][$col]['label'] . '</a>'; |
||
273 | $context['columns'][$col]['selected'] = $_REQUEST['sort'] == $col; |
||
274 | } |
||
275 | |||
276 | // Don't offer website sort to guests |
||
277 | if ($context['user']['is_guest']) |
||
278 | { |
||
279 | $context['columns']['website_url']['href'] = ''; |
||
280 | $context['columns']['website_url']['link'] = $context['columns']['website_url']['label']; |
||
281 | } |
||
282 | |||
283 | // Are we sorting the results |
||
284 | $context['sort_by'] = $_REQUEST['sort']; |
||
285 | $context['sort_direction'] = !isset($_REQUEST['desc']) ? 'up' : 'down'; |
||
286 | |||
287 | // Construct the page index. |
||
288 | $context['page_index'] = constructPageIndex($scripturl . '?action=mlist;sort=' . $_REQUEST['sort'] . (isset($_REQUEST['desc']) ? ';desc' : ''), $_REQUEST['start'], $context['num_members'], $modSettings['defaultMaxMembers']); |
||
289 | |||
290 | // Send the data to the template. |
||
291 | $context['start'] = $_REQUEST['start'] + 1; |
||
292 | $context['end'] = min($_REQUEST['start'] + $modSettings['defaultMaxMembers'], $context['num_members']); |
||
293 | |||
294 | $context['can_moderate_forum'] = allowedTo('moderate_forum'); |
||
295 | $context['page_title'] = sprintf($txt['viewing_members'], $context['start'], $context['end']); |
||
296 | $context['linktree'][] = array( |
||
297 | 'url' => $scripturl . '?action=mlist;sort=' . $_REQUEST['sort'] . ';start=' . $_REQUEST['start'], |
||
298 | 'name' => &$context['page_title'], |
||
299 | 'extra_after' => '(' . sprintf($txt['of_total_members'], $context['num_members']) . ')' |
||
300 | ); |
||
301 | |||
302 | $limit = $_REQUEST['start']; |
||
303 | $query_parameters = array( |
||
304 | 'regular_id_group' => 0, |
||
305 | 'is_activated' => 1, |
||
306 | 'sort' => $context['columns'][$_REQUEST['sort']]['sort'][$context['sort_direction']], |
||
307 | 'blank_string' => '', |
||
308 | ); |
||
309 | |||
310 | // Using cache allows to narrow down the list to be retrieved. |
||
311 | if ($use_cache && $_REQUEST['sort'] === 'real_name' && !isset($_REQUEST['desc'])) |
||
312 | { |
||
313 | $first_offset = $_REQUEST['start'] - ($_REQUEST['start'] % $cache_step_size); |
||
314 | if ($first_offset < 0) |
||
315 | $first_offset = 0; |
||
316 | $second_offset = ceil(($_REQUEST['start'] + $modSettings['defaultMaxMembers']) / $cache_step_size) * $cache_step_size; |
||
317 | if ($second_offset >= $memberlist_cache['num_members']) |
||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
Loading history...
|
|||
318 | $second_offset = $memberlist_cache['num_members'] - 1; |
||
319 | |||
320 | $where = 'mem.real_name BETWEEN {string:real_name_low} AND {string:real_name_high}'; |
||
321 | $query_parameters['real_name_low'] = $memberlist_cache['index'][$first_offset]; |
||
322 | $query_parameters['real_name_high'] = $memberlist_cache['index'][$second_offset]; |
||
323 | $limit -= $first_offset; |
||
324 | } |
||
325 | |||
326 | // Reverse sorting is a bit more complicated... |
||
327 | elseif ($use_cache && $_REQUEST['sort'] === 'real_name') |
||
328 | { |
||
329 | $first_offset = floor(($memberlist_cache['num_members'] - $modSettings['defaultMaxMembers'] - $_REQUEST['start']) / $cache_step_size) * $cache_step_size; |
||
330 | if ($first_offset < 0) |
||
331 | $first_offset = 0; |
||
332 | $second_offset = ceil(($memberlist_cache['num_members'] - $_REQUEST['start']) / $cache_step_size) * $cache_step_size; |
||
333 | if ($second_offset >= $memberlist_cache['num_members']) |
||
334 | $second_offset = $memberlist_cache['num_members'] - 1; |
||
335 | |||
336 | $where = 'mem.real_name BETWEEN {string:real_name_low} AND {string:real_name_high}'; |
||
337 | $query_parameters['real_name_low'] = $memberlist_cache['index'][$first_offset]; |
||
338 | $query_parameters['real_name_high'] = $memberlist_cache['index'][$second_offset]; |
||
339 | $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); |
||
340 | } |
||
341 | |||
342 | $custom_fields_qry = ''; |
||
343 | if (!empty($context['custom_profile_fields']['join'][$_REQUEST['sort']])) |
||
344 | $custom_fields_qry = $context['custom_profile_fields']['join'][$_REQUEST['sort']]; |
||
345 | |||
346 | // Select the members from the database. |
||
347 | $request = $smcFunc['db_query']('', ' |
||
348 | SELECT mem.id_member |
||
349 | FROM {db_prefix}members AS mem' . ($_REQUEST['sort'] === 'is_online' ? ' |
||
350 | LEFT JOIN {db_prefix}log_online AS lo ON (lo.id_member = mem.id_member)' : '') . ($_REQUEST['sort'] === 'id_group' ? ' |
||
351 | 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)' : '') . ' |
||
352 | ' . $custom_fields_qry . ' |
||
353 | WHERE mem.is_activated = {int:is_activated}' . (empty($where) ? '' : ' |
||
354 | AND ' . $where) . ' |
||
355 | ORDER BY {raw:sort} |
||
356 | LIMIT {int:start}, {int:max}', |
||
357 | array_merge($query_parameters, array( |
||
358 | 'sort' => $query_parameters['sort'], |
||
359 | 'start' => $limit, |
||
360 | 'max' => $modSettings['defaultMaxMembers'], |
||
361 | )) |
||
362 | ); |
||
363 | printMemberListRows($request); |
||
364 | $smcFunc['db_free_result']($request); |
||
365 | |||
366 | // Add anchors at the start of each letter. |
||
367 | if ($_REQUEST['sort'] == 'real_name') |
||
368 | { |
||
369 | $last_letter = ''; |
||
370 | foreach ($context['members'] as $i => $dummy) |
||
371 | { |
||
372 | $this_letter = $smcFunc['strtolower']($smcFunc['substr']($context['members'][$i]['name'], 0, 1)); |
||
373 | |||
374 | if ($this_letter != $last_letter && preg_match('~[a-z]~', $this_letter) === 1) |
||
375 | { |
||
376 | $context['members'][$i]['sort_letter'] = $smcFunc['htmlspecialchars']($this_letter); |
||
377 | $last_letter = $this_letter; |
||
378 | } |
||
379 | } |
||
380 | } |
||
381 | } |
||
382 | |||
383 | /** |
||
384 | * Search for members, or display search results. |
||
385 | * - Called by MemberList(). |
||
386 | * - If variable 'search' is empty displays search dialog box, using the search sub template. |
||
387 | * - Calls printMemberListRows to retrieve the results of the query. |
||
388 | */ |
||
389 | function MLSearch() |
||
390 | { |
||
391 | global $txt, $scripturl, $context, $modSettings, $smcFunc; |
||
392 | |||
393 | $context['page_title'] = $txt['mlist_search']; |
||
394 | $context['can_moderate_forum'] = allowedTo('moderate_forum'); |
||
395 | |||
396 | // Can they search custom fields? |
||
397 | $request = $smcFunc['db_query']('', ' |
||
398 | SELECT col_name, field_name, field_desc |
||
399 | FROM {db_prefix}custom_fields |
||
400 | WHERE active = {int:active} |
||
401 | ' . (allowedTo('admin_forum') ? '' : ' AND private < {int:private_level}') . ' |
||
402 | AND can_search = {int:can_search} |
||
403 | AND (field_type = {string:field_type_text} OR field_type = {string:field_type_textarea} OR field_type = {string:field_type_select})', |
||
404 | array( |
||
405 | 'active' => 1, |
||
406 | 'can_search' => 1, |
||
407 | 'private_level' => 2, |
||
408 | 'field_type_text' => 'text', |
||
409 | 'field_type_textarea' => 'textarea', |
||
410 | 'field_type_select' => 'select', |
||
411 | ) |
||
412 | ); |
||
413 | $context['custom_search_fields'] = array(); |
||
414 | while ($row = $smcFunc['db_fetch_assoc']($request)) |
||
415 | $context['custom_search_fields'][$row['col_name']] = array( |
||
416 | 'colname' => $row['col_name'], |
||
417 | 'name' => $row['field_name'], |
||
418 | 'desc' => $row['field_desc'], |
||
419 | ); |
||
420 | $smcFunc['db_free_result']($request); |
||
421 | |||
422 | // They're searching.. |
||
423 | if (isset($_REQUEST['search']) && isset($_REQUEST['fields'])) |
||
424 | { |
||
425 | $_POST['search'] = trim(isset($_GET['search']) ? $_GET['search'] : $_POST['search']); |
||
426 | $_POST['fields'] = isset($_GET['fields']) ? explode(',', $_GET['fields']) : $_POST['fields']; |
||
427 | |||
428 | $context['old_search'] = $_REQUEST['search']; |
||
429 | $context['old_search_value'] = urlencode($_REQUEST['search']); |
||
430 | |||
431 | // No fields? Use default... |
||
432 | if (empty($_POST['fields'])) |
||
433 | $_POST['fields'] = array('name'); |
||
434 | |||
435 | // Set defaults for how the results are sorted |
||
436 | if (!isset($_REQUEST['sort']) || !isset($context['columns'][$_REQUEST['sort']])) |
||
437 | $_REQUEST['sort'] = 'real_name'; |
||
438 | |||
439 | // Build the column link / sort information. |
||
440 | foreach ($context['columns'] as $col => $column_details) |
||
441 | { |
||
442 | $context['columns'][$col]['href'] = $scripturl . '?action=mlist;sa=search;start=' . (int) $_REQUEST['start'] . ';sort=' . $col; |
||
443 | |||
444 | if ((!isset($_REQUEST['desc']) && $col == $_REQUEST['sort']) || ($col != $_REQUEST['sort'] && !empty($column_details['default_sort_rev']))) |
||
445 | $context['columns'][$col]['href'] .= ';desc'; |
||
446 | |||
447 | if (isset($_POST['search']) && isset($_POST['fields'])) |
||
448 | $context['columns'][$col]['href'] .= ';search=' . $_POST['search'] . ';fields=' . implode(',', $_POST['fields']); |
||
449 | |||
450 | $context['columns'][$col]['link'] = '<a href="' . $context['columns'][$col]['href'] . '" rel="nofollow">' . $context['columns'][$col]['label'] . '</a>'; |
||
451 | $context['columns'][$col]['selected'] = $_REQUEST['sort'] == $col; |
||
452 | } |
||
453 | |||
454 | // set up some things for use in the template |
||
455 | $context['sort_direction'] = !isset($_REQUEST['desc']) ? 'up' : 'down'; |
||
456 | $context['sort_by'] = $_REQUEST['sort']; |
||
457 | |||
458 | $query_parameters = array( |
||
459 | 'regular_id_group' => 0, |
||
460 | 'is_activated' => 1, |
||
461 | 'blank_string' => '', |
||
462 | 'search' => '%' . strtr($smcFunc['htmlspecialchars']($_POST['search'], ENT_QUOTES), array('_' => '\\_', '%' => '\\%', '*' => '%')) . '%', |
||
463 | 'sort' => $context['columns'][$_REQUEST['sort']]['sort'][$context['sort_direction']], |
||
464 | ); |
||
465 | |||
466 | // Search for a name |
||
467 | if (in_array('name', $_POST['fields'])) |
||
468 | { |
||
469 | $fields = allowedTo('moderate_forum') ? array('member_name', 'real_name') : array('real_name'); |
||
470 | $search_fields[] = 'name'; |
||
471 | } |
||
472 | else |
||
473 | { |
||
474 | $fields = array(); |
||
475 | $search_fields = array(); |
||
476 | } |
||
477 | |||
478 | // Search for websites. |
||
479 | if (in_array('website', $_POST['fields'])) |
||
480 | { |
||
481 | $fields += array(7 => 'website_title', 'website_url'); |
||
482 | $search_fields[] = 'website'; |
||
483 | } |
||
484 | // Search for groups. |
||
485 | if (in_array('group', $_POST['fields'])) |
||
486 | { |
||
487 | $fields += array(9 => 'COALESCE(group_name, {string:blank_string})'); |
||
488 | $search_fields[] = 'group'; |
||
489 | } |
||
490 | // Search for an email address? |
||
491 | if (in_array('email', $_POST['fields']) && allowedTo('moderate_forum')) |
||
492 | { |
||
493 | $fields += array(2 => 'email_address'); |
||
494 | $search_fields[] = 'email'; |
||
495 | } |
||
496 | |||
497 | if ($smcFunc['db_case_sensitive']) |
||
498 | foreach ($fields as $key => $field) |
||
499 | $fields[$key] = 'LOWER(' . $field . ')'; |
||
500 | |||
501 | $customJoin = array(); |
||
502 | $customCount = 10; |
||
503 | |||
504 | // Any custom fields to search for - these being tricky? |
||
505 | foreach ($_POST['fields'] as $field) |
||
506 | { |
||
507 | $row['col_name'] = substr($field, 5); |
||
508 | if (substr($field, 0, 5) == 'cust_' && isset($context['custom_search_fields'][$row['col_name']])) |
||
509 | { |
||
510 | $customJoin[] = 'LEFT JOIN {db_prefix}themes AS t' . $row['col_name'] . ' ON (t' . $row['col_name'] . '.variable = {string:t' . $row['col_name'] . '} AND t' . $row['col_name'] . '.id_theme = 1 AND t' . $row['col_name'] . '.id_member = mem.id_member)'; |
||
511 | $query_parameters['t' . $row['col_name']] = $row['col_name']; |
||
512 | $fields += array($customCount++ => 'COALESCE(t' . $row['col_name'] . '.value, {string:blank_string})'); |
||
513 | $search_fields[] = $field; |
||
514 | } |
||
515 | } |
||
516 | |||
517 | // No search fields? That means you're trying to hack things |
||
518 | if (empty($search_fields)) |
||
519 | fatal_lang_error('invalid_search_string', false); |
||
520 | |||
521 | $query = $_POST['search'] == '' ? '= {string:blank_string}' : ($smcFunc['db_case_sensitive'] ? 'LIKE LOWER({string:search})' : 'LIKE {string:search}'); |
||
522 | |||
523 | $request = $smcFunc['db_query']('', ' |
||
524 | SELECT COUNT(*) |
||
525 | FROM {db_prefix}members AS mem |
||
526 | 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)' . |
||
527 | (empty($customJoin) ? '' : implode(' |
||
528 | ', $customJoin)) . ' |
||
529 | WHERE (' . implode(' ' . $query . ' OR ', $fields) . ' ' . $query . ') |
||
530 | AND mem.is_activated = {int:is_activated}', |
||
531 | $query_parameters |
||
532 | ); |
||
533 | list ($numResults) = $smcFunc['db_fetch_row']($request); |
||
534 | $smcFunc['db_free_result']($request); |
||
535 | |||
536 | $context['page_index'] = constructPageIndex($scripturl . '?action=mlist;sa=search;search=' . $_POST['search'] . ';fields=' . implode(',', $_POST['fields']), $_REQUEST['start'], $numResults, $modSettings['defaultMaxMembers']); |
||
537 | |||
538 | $custom_fields_qry = ''; |
||
539 | if (array_search('cust_' . $_REQUEST['sort'], $_POST['fields']) === false && !empty($context['custom_profile_fields']['join'][$_REQUEST['sort']])) |
||
540 | $custom_fields_qry = $context['custom_profile_fields']['join'][$_REQUEST['sort']]; |
||
541 | |||
542 | // Find the members from the database. |
||
543 | $request = $smcFunc['db_query']('', ' |
||
544 | SELECT mem.id_member |
||
545 | FROM {db_prefix}members AS mem |
||
546 | LEFT JOIN {db_prefix}log_online AS lo ON (lo.id_member = mem.id_member) |
||
547 | 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)' . |
||
548 | $custom_fields_qry . |
||
549 | (empty($customJoin) ? '' : implode(' |
||
550 | ', $customJoin)) . ' |
||
551 | WHERE (' . implode(' ' . $query . ' OR ', $fields) . ' ' . $query . ') |
||
552 | AND mem.is_activated = {int:is_activated} |
||
553 | ORDER BY {raw:sort} |
||
554 | LIMIT {int:start}, {int:max}', |
||
555 | array_merge($query_parameters, array( |
||
556 | 'start' => $_REQUEST['start'], |
||
557 | 'max' => $modSettings['defaultMaxMembers'], |
||
558 | )) |
||
559 | ); |
||
560 | printMemberListRows($request); |
||
561 | $smcFunc['db_free_result']($request); |
||
562 | } |
||
563 | else |
||
564 | { |
||
565 | // These are all the possible fields. |
||
566 | $context['search_fields'] = array( |
||
567 | 'name' => $txt['mlist_search_name'], |
||
568 | 'email' => $txt['mlist_search_email'], |
||
569 | 'website' => $txt['mlist_search_website'], |
||
570 | 'group' => $txt['mlist_search_group'], |
||
571 | ); |
||
572 | |||
573 | // Sorry, but you can't search by email unless you can view emails |
||
574 | if (!allowedTo('moderate_forum')) |
||
575 | { |
||
576 | unset($context['search_fields']['email']); |
||
577 | $context['search_defaults'] = array('name'); |
||
578 | } |
||
579 | else |
||
580 | { |
||
581 | $context['search_defaults'] = array('name', 'email'); |
||
582 | } |
||
583 | |||
584 | foreach ($context['custom_search_fields'] as $field) |
||
585 | $context['search_fields']['cust_' . $field['colname']] = sprintf($txt['mlist_search_by'], tokenTxtReplace($field['name'])); |
||
586 | |||
587 | $context['sub_template'] = 'search'; |
||
588 | $context['old_search'] = isset($_GET['search']) ? $_GET['search'] : (isset($_POST['search']) ? $smcFunc['htmlspecialchars']($_POST['search']) : ''); |
||
589 | |||
590 | // Since we're nice we also want to default focus on to the search field. |
||
591 | addInlineJavaScript(' |
||
592 | $(\'input[name="search"]\').focus();', true); |
||
593 | } |
||
594 | |||
595 | $context['linktree'][] = array( |
||
596 | 'url' => $scripturl . '?action=mlist;sa=search', |
||
597 | 'name' => &$context['page_title'] |
||
598 | ); |
||
599 | |||
600 | // Highlight the correct button, too! |
||
601 | unset($context['memberlist_buttons']['view_all_members']['active']); |
||
602 | $context['memberlist_buttons']['mlist_search']['active'] = true; |
||
603 | } |
||
604 | |||
605 | /** |
||
606 | * Retrieves results of the request passed to it |
||
607 | * Puts results of request into the context for the sub template. |
||
608 | * |
||
609 | * @param resource $request An SQL result resource |
||
610 | */ |
||
611 | function printMemberListRows($request) |
||
612 | { |
||
613 | global $context, $memberContext, $smcFunc, $txt; |
||
614 | global $scripturl, $settings; |
||
615 | |||
616 | // Get the most posts. |
||
617 | $result = $smcFunc['db_query']('', ' |
||
618 | SELECT MAX(posts) |
||
619 | FROM {db_prefix}members', |
||
620 | array( |
||
621 | ) |
||
622 | ); |
||
623 | list ($most_posts) = $smcFunc['db_fetch_row']($result); |
||
624 | $smcFunc['db_free_result']($result); |
||
625 | |||
626 | // Avoid division by zero... |
||
627 | if ($most_posts == 0) |
||
628 | $most_posts = 1; |
||
629 | |||
630 | $members = array(); |
||
631 | while ($row = $smcFunc['db_fetch_assoc']($request)) |
||
632 | $members[] = $row['id_member']; |
||
633 | |||
634 | // Load all the members for display. |
||
635 | loadMemberData($members); |
||
636 | |||
637 | $context['members'] = array(); |
||
638 | foreach ($members as $member) |
||
639 | { |
||
640 | if (!loadMemberContext($member)) |
||
641 | continue; |
||
642 | |||
643 | $context['members'][$member] = $memberContext[$member]; |
||
644 | $context['members'][$member]['post_percent'] = round(($context['members'][$member]['real_posts'] * 100) / $most_posts); |
||
645 | $context['members'][$member]['registered_date'] = smf_strftime('%Y-%m-%d', $context['members'][$member]['registered_timestamp']); |
||
646 | |||
647 | if (!empty($context['custom_profile_fields']['columns'])) |
||
648 | { |
||
649 | foreach ($context['custom_profile_fields']['columns'] as $key => $column) |
||
650 | { |
||
651 | // Don't show anything if there isn't anything to show. |
||
652 | if (!isset($context['members'][$member]['options'][$key])) |
||
653 | { |
||
654 | $context['members'][$member]['options'][$key] = isset($column['default_value']) ? $column['default_value'] : ''; |
||
655 | continue; |
||
656 | } |
||
657 | |||
658 | $context['members'][$member]['options'][$key] = tokenTxtReplace($context['members'][$member]['options'][$key]); |
||
659 | $currentKey = 0; |
||
660 | if (!empty($column['options'])) |
||
661 | { |
||
662 | $fieldOptions = explode(',', $column['options']); |
||
663 | foreach ($fieldOptions as $k => $v) |
||
664 | { |
||
665 | if (empty($currentKey)) |
||
666 | $currentKey = $v === $context['members'][$member]['options'][$key] ? $k : 0; |
||
667 | } |
||
668 | } |
||
669 | |||
670 | if ($column['bbc'] && !empty($context['members'][$member]['options'][$key])) |
||
671 | $context['members'][$member]['options'][$key] = strip_tags(parse_bbc($context['members'][$member]['options'][$key])); |
||
672 | |||
673 | elseif ($column['type'] == 'check') |
||
674 | $context['members'][$member]['options'][$key] = $context['members'][$member]['options'][$key] == 0 ? $txt['no'] : $txt['yes']; |
||
675 | |||
676 | // Enclosing the user input within some other text? |
||
677 | if (!empty($column['enclose'])) |
||
678 | $context['members'][$member]['options'][$key] = strtr($column['enclose'], array( |
||
679 | '{SCRIPTURL}' => $scripturl, |
||
680 | '{IMAGES_URL}' => $settings['images_url'], |
||
681 | '{DEFAULT_IMAGES_URL}' => $settings['default_images_url'], |
||
682 | '{INPUT}' => tokenTxtReplace($context['members'][$member]['options'][$key]), |
||
683 | '{KEY}' => $currentKey |
||
684 | )); |
||
685 | } |
||
686 | } |
||
687 | } |
||
688 | } |
||
689 | |||
690 | /** |
||
691 | * Sets the label, sort and join info for every custom field column. |
||
692 | * |
||
693 | * @return array An array of info about the custom fields for the member list |
||
694 | */ |
||
695 | function getCustFieldsMList() |
||
696 | { |
||
697 | global $smcFunc; |
||
698 | |||
699 | $cpf = array(); |
||
700 | |||
701 | $request = $smcFunc['db_query']('', ' |
||
702 | SELECT col_name, field_name, field_desc, field_type, field_options, bbc, enclose, default_value |
||
703 | FROM {db_prefix}custom_fields |
||
704 | WHERE active = {int:active} |
||
705 | AND show_mlist = {int:show} |
||
706 | AND private < {int:private_level}', |
||
707 | array( |
||
708 | 'active' => 1, |
||
709 | 'show' => 1, |
||
710 | 'private_level' => 2, |
||
711 | ) |
||
712 | ); |
||
713 | |||
714 | while ($row = $smcFunc['db_fetch_assoc']($request)) |
||
715 | { |
||
716 | // Get all the data we're gonna need. |
||
717 | $cpf['columns'][$row['col_name']] = array( |
||
718 | 'label' => tokenTxtReplace($row['field_name']), |
||
719 | 'type' => $row['field_type'], |
||
720 | 'options' => tokenTxtReplace($row['field_options']), |
||
721 | 'bbc' => !empty($row['bbc']), |
||
722 | 'enclose' => $row['enclose'], |
||
723 | 'default_value' => tokenTxtReplace($row['default_value']), |
||
724 | ); |
||
725 | |||
726 | // Get the right sort method depending on the cust field type. |
||
727 | if ($row['field_type'] != 'check') |
||
728 | $cpf['columns'][$row['col_name']]['sort'] = array( |
||
729 | 'down' => 'LENGTH(t' . $row['col_name'] . '.value) > 0 ASC, COALESCE(t' . $row['col_name'] . '.value, \'\') DESC', |
||
730 | 'up' => 'LENGTH(t' . $row['col_name'] . '.value) > 0 DESC, COALESCE(t' . $row['col_name'] . '.value, \'\') ASC' |
||
731 | ); |
||
732 | |||
733 | else |
||
734 | $cpf['columns'][$row['col_name']]['sort'] = array( |
||
735 | 'down' => 't' . $row['col_name'] . '.value DESC', |
||
736 | 'up' => 't' . $row['col_name'] . '.value ASC' |
||
737 | ); |
||
738 | |||
739 | $cpf['join'][$row['col_name']] = 'LEFT JOIN {db_prefix}themes AS t' . $row['col_name'] . ' ON (t' . $row['col_name'] . '.variable = {literal:' . $row['col_name'] . '} AND t' . $row['col_name'] . '.id_theme = 1 AND t' . $row['col_name'] . '.id_member = mem.id_member)'; |
||
740 | } |
||
741 | $smcFunc['db_free_result']($request); |
||
742 | |||
743 | return $cpf; |
||
744 | } |
||
745 | |||
746 | ?> |