1 | <?php |
||
2 | |||
3 | /** |
||
4 | * @package ElkArte Forum |
||
5 | * @copyright ElkArte Forum contributors |
||
6 | * @license BSD http://opensource.org/licenses/BSD-3-Clause (see accompanying LICENSE.txt file) |
||
7 | * |
||
8 | * This file contains code covered by: |
||
9 | * copyright: 2011 Simple Machines (http://www.simplemachines.org) |
||
10 | * |
||
11 | * @version 2.0 dev |
||
12 | * |
||
13 | */ |
||
14 | |||
15 | namespace ElkArte; |
||
16 | |||
17 | use BBC\ParserWrapper; |
||
18 | use ElkArte\Cache\Cache; |
||
19 | use ElkArte\Database\QueryInterface; |
||
20 | |||
21 | /** |
||
22 | * This class loads all the data related to a certain member or |
||
23 | * set of members taken from the db |
||
24 | */ |
||
25 | class MemberLoader |
||
26 | { |
||
27 | /** @var string Just the bare minimum set of fields */ |
||
28 | public const SET_MINIMAL = 'minimal'; |
||
29 | |||
30 | /** @var string What is needed in most of the cases */ |
||
31 | public const SET_NORMAL = 'normal'; |
||
32 | |||
33 | /** @var string What is required to see a profile page */ |
||
34 | public const SET_PROFILE = 'profile'; |
||
35 | |||
36 | /** @var QueryInterface */ |
||
37 | protected $db; |
||
38 | |||
39 | /** @var Cache */ |
||
40 | protected $cache; |
||
41 | |||
42 | /** @var ParserWrapper */ |
||
43 | protected $bbc_parser; |
||
44 | |||
45 | /** @var MembersList */ |
||
46 | protected $users_list; |
||
47 | |||
48 | /** @var array */ |
||
49 | protected $options = [ |
||
50 | 'titlesEnable' => false, |
||
51 | 'custom_fields' => false, |
||
52 | 'load_moderators' => false, |
||
53 | 'display_fields' => [] |
||
54 | ]; |
||
55 | |||
56 | /** @var bool if to use the cache or not */ |
||
57 | protected $useCache = false; |
||
58 | |||
59 | /** @var string which set to load */ |
||
60 | protected $set = ''; |
||
61 | |||
62 | /** @var string */ |
||
63 | protected $base_select_columns = ''; |
||
64 | |||
65 | /** @var string */ |
||
66 | protected $base_select_tables = ''; |
||
67 | |||
68 | /** @var int[] member ids that have been loaded */ |
||
69 | protected $loaded_ids = []; |
||
70 | |||
71 | /** @var Member[] */ |
||
72 | protected $loaded_members = []; |
||
73 | |||
74 | /** |
||
75 | * Initialize the class |
||
76 | * |
||
77 | * @param QueryInterface $db The object to query the database |
||
78 | * @param Cache $cache Cache object used to... well cache content of each member |
||
79 | * @param ParserWrapper $bbc_parser BBC parser to convert BBC to HTML |
||
80 | * @param MembersList $list the instance of the list of members |
||
81 | * @param array $options Random options useful to the loader to decide what to actually load |
||
82 | */ |
||
83 | public function __construct($db, $cache, $bbc_parser, $list, $options = []) |
||
84 | { |
||
85 | $this->db = $db; |
||
86 | $this->cache = $cache; |
||
87 | $this->bbc_parser = $bbc_parser; |
||
88 | $this->users_list = $list; |
||
89 | $this->options = array_merge($this->options, $options); |
||
90 | |||
91 | $this->useCache = $this->cache->isEnabled() && $this->cache->levelHigherThan(2); |
||
92 | |||
93 | $this->base_select_columns = ' |
||
94 | COALESCE(lo.log_time, 0) AS is_online, COALESCE(a.id_attach, 0) AS id_attach, a.filename, a.attachment_type, |
||
95 | mem.signature, mem.avatar, mem.id_member, mem.member_name, |
||
96 | mem.real_name, mem.email_address, mem.date_registered, mem.website_title, mem.website_url, |
||
97 | mem.birthdate, mem.member_ip, mem.member_ip2, mem.posts, mem.last_login, mem.likes_given, mem.likes_received, |
||
98 | mem.karma_good, mem.id_post_group, mem.karma_bad, mem.lngfile, mem.id_group, mem.time_offset, mem.show_online, |
||
99 | mg.online_color AS member_group_color, COALESCE(mg.group_name, {string:blank_string}) AS member_group, |
||
100 | pg.online_color AS post_group_color, COALESCE(pg.group_name, {string:blank_string}) AS post_group, |
||
101 | mem.is_activated, mem.warning, ' . (empty($this->options['titlesEnable']) ? '' : 'mem.usertitle, ') . ' |
||
102 | CASE WHEN mem.id_group = 0 OR mg.icons = {string:blank_string} THEN pg.icons ELSE mg.icons END AS icons'; |
||
103 | |||
104 | $this->base_select_tables = ' |
||
105 | LEFT JOIN {db_prefix}log_online AS lo ON (lo.id_member = mem.id_member) |
||
106 | LEFT JOIN {db_prefix}attachments AS a ON (a.id_member = mem.id_member) |
||
107 | 1 | LEFT JOIN {db_prefix}membergroups AS pg ON (pg.id_group = mem.id_post_group) |
|
108 | LEFT JOIN {db_prefix}membergroups AS mg ON (mg.id_group = mem.id_group)'; |
||
109 | 1 | } |
|
110 | 1 | ||
111 | 1 | /** |
|
112 | 1 | * Loads users data from a member id |
|
113 | 1 | * |
|
114 | * @param int|int[] $users Single id or list of ids to load |
||
115 | 1 | * @param string $set The data to load (see the constants SET_*) |
|
116 | * @return bool|int[] The ids of the members loaded |
||
117 | 1 | */ |
|
118 | public function loadById($users, $set = MemberLoader::SET_NORMAL) |
||
119 | { |
||
120 | $users = array_filter(array_unique((array) $users)); |
||
121 | |||
122 | // Can't just look for no users :P. |
||
123 | if (empty($users)) |
||
124 | { |
||
125 | 1 | return false; |
|
126 | } |
||
127 | 1 | ||
128 | $this->set = $set; |
||
129 | $this->loaded_ids = []; |
||
130 | $this->loaded_members = []; |
||
131 | |||
132 | 1 | $to_load = $this->loadFromCache($users); |
|
133 | |||
134 | $this->loadByCondition('mem.id_member' . (count($to_load) === 1 ? ' = {int:users}' : ' IN ({array_int:users})'), $to_load); |
||
135 | |||
136 | $this->loadModerators(); |
||
137 | |||
138 | return $this->loaded_ids; |
||
139 | } |
||
140 | |||
141 | 14 | /** |
|
142 | * Retrieve \ElkArte\Member objects from the cache |
||
143 | * |
||
144 | 14 | * @param int|int[] $users Single id or list of ids to load |
|
145 | * @return int[] The ids not found in the cache |
||
146 | */ |
||
147 | protected function loadFromCache($users) |
||
148 | { |
||
149 | 14 | if (!$this->useCache) |
|
150 | 14 | { |
|
151 | 14 | $to_load = $users; |
|
152 | 14 | } |
|
153 | else |
||
154 | 14 | { |
|
155 | $to_load = []; |
||
156 | 14 | foreach ($users as $user) |
|
157 | { |
||
158 | 14 | $data = $this->cache->get('member_data-' . $this->set . '-' . $user, 240); |
|
159 | if ($this->cache->isMiss()) |
||
160 | 14 | { |
|
161 | $to_load[] = $user; |
||
162 | continue; |
||
163 | } |
||
164 | |||
165 | $member = new Member($data['data'], $data['set'], $this->bbc_parser); |
||
166 | foreach ($data['additional_data'] as $key => $values) |
||
167 | { |
||
168 | $member->append($key, $values, $this->options['display_fields']); |
||
169 | 14 | } |
|
170 | |||
171 | 14 | $this->users_list::add($member, $data['data']['id_member']); |
|
172 | $this->loaded_ids[] = $data['data']['id_member']; |
||
173 | 14 | $this->loaded_members[$data['data']['id_member']] = $member; |
|
174 | } |
||
175 | } |
||
176 | |||
177 | return $to_load; |
||
0 ignored issues
–
show
Bug
Best Practice
introduced
by
![]() |
|||
178 | } |
||
179 | |||
180 | /** |
||
181 | * Loads users data provided a where clause and an array of ids |
||
182 | * |
||
183 | * @param string $where_clause The WHERE clause of the query to run |
||
184 | * @param int[] $to_load Array of ids to load |
||
185 | * @return bool If loaded anything or not |
||
186 | */ |
||
187 | protected function loadByCondition($where_clause, $to_load) |
||
188 | { |
||
189 | if (empty($to_load)) |
||
190 | { |
||
191 | return false; |
||
192 | } |
||
193 | |||
194 | $select_columns = $this->base_select_columns; |
||
195 | $select_tables = $this->base_select_tables; |
||
196 | |||
197 | // We add or replace according to the set |
||
198 | 14 | switch ($this->set) |
|
199 | { |
||
200 | case MemberLoader::SET_NORMAL: |
||
201 | $select_columns .= ', mem.buddy_list, mem.pm_ignore_list'; |
||
202 | break; |
||
203 | case MemberLoader::SET_PROFILE: |
||
204 | $select_columns .= ', mem.id_theme, mem.pm_ignore_list, mem.pm_email_notify, mem.receive_from, |
||
205 | mem.time_format, mem.secret_question, mem.additional_groups, |
||
206 | mem.total_time_logged_in, mem.notify_announcements, mem.notify_regularity, mem.notify_send_body, mem.notify_from, |
||
207 | mem.notify_types, lo.url, mem.ignore_boards, mem.password_salt, mem.pm_prefs, mem.buddy_list, mem.otp_secret, mem.enable_otp'; |
||
208 | 14 | break; |
|
209 | case MemberLoader::SET_MINIMAL: |
||
210 | 14 | $select_columns = ' |
|
211 | mem.id_member, mem.member_name, mem.real_name, mem.email_address, mem.date_registered, |
||
212 | mem.posts, mem.last_login, mem.member_ip, mem.member_ip2, mem.lngfile, mem.id_group'; |
||
213 | $select_tables = ''; |
||
214 | break; |
||
215 | 14 | default: |
|
216 | 14 | trigger_error('\ElkArte\MembersList::load(): Invalid member data set \'' . $this->set . "'", E_USER_WARNING); |
|
217 | } |
||
218 | |||
219 | 14 | // Allow addons to easily add to the selected member data |
|
220 | call_integration_hook('integrate_load_member_data', [&$select_columns, &$select_tables, $this->set]); |
||
221 | 7 | ||
222 | 4 | // Load the member's data. |
|
223 | 4 | $new_loaded_ids = []; |
|
224 | 5 | $this->db->fetchQuery(' |
|
225 | 10 | SELECT' . $select_columns . ' |
|
226 | FROM {db_prefix}members AS mem' . $select_tables . ' |
||
227 | WHERE ' . $where_clause, |
||
228 | [ |
||
229 | 10 | 'blank_string' => '', |
|
230 | 'users' => count($to_load) === 1 ? current($to_load) : $to_load, |
||
231 | ] |
||
232 | )->fetch_callback( |
||
233 | function ($row) use (&$new_loaded_ids) { |
||
234 | $row['id_member'] = (int) $row['id_member']; |
||
235 | $row['id_group'] = (int) $row['id_group']; |
||
236 | |||
237 | $new_loaded_ids[] = $row['id_member']; |
||
238 | $this->loaded_ids[] = $row['id_member']; |
||
239 | $row['options'] = []; |
||
240 | $this->loaded_members[$row['id_member']] = new Member($row, $this->set, $this->bbc_parser); |
||
241 | 14 | $this->users_list::add($this->loaded_members[$row['id_member']], $row['id_member']); |
|
242 | } |
||
243 | ); |
||
244 | 14 | $this->loadCustomFields($new_loaded_ids); |
|
245 | 14 | ||
246 | 14 | if (!empty($new_loaded_ids)) |
|
247 | 14 | { |
|
248 | // Anything else integration may want to add to the user_profile array |
||
249 | 14 | call_integration_hook('integrate_add_member_data', [$new_loaded_ids, $this->users_list]); |
|
250 | 14 | ||
251 | $this->storeInCache($new_loaded_ids); |
||
252 | } |
||
253 | |||
254 | 14 | return !empty($new_loaded_ids); |
|
255 | 14 | } |
|
256 | |||
257 | 14 | /** |
|
258 | 14 | * Loads custom fields data for a set of users |
|
259 | 14 | * |
|
260 | 14 | * @param int[] $new_loaded_ids Array of ids to load |
|
261 | 14 | */ |
|
262 | protected function loadCustomFields($new_loaded_ids) |
||
263 | 14 | { |
|
264 | 14 | if (empty($new_loaded_ids) || $this->options['custom_fields'] === false) |
|
265 | { |
||
266 | 14 | return; |
|
267 | } |
||
268 | |||
269 | 14 | $data = []; |
|
270 | $this->db->fetchQuery(' |
||
271 | 14 | SELECT |
|
272 | cfd.id_member, cfd.variable, cfd.value, cf.field_options, cf.field_type |
||
273 | FROM {db_prefix}custom_fields_data AS cfd |
||
274 | 14 | JOIN {db_prefix}custom_fields AS cf ON (cf.col_name = cfd.variable) |
|
275 | WHERE id_member' . (count($new_loaded_ids) === 1 ? ' = {int:loaded_ids}' : ' IN ({array_int:loaded_ids})'), |
||
276 | [ |
||
277 | 'loaded_ids' => count($new_loaded_ids) === 1 ? $new_loaded_ids[0] : $new_loaded_ids, |
||
278 | ] |
||
279 | )->fetch_callback( |
||
280 | static function ($row) use (&$data) { |
||
281 | $key = 0; |
||
282 | 14 | $row['id_member'] = (int) $row['id_member']; |
|
283 | if (!empty($row['field_options'])) |
||
284 | 14 | { |
|
285 | $field_options = explode(',', $row['field_options']); |
||
286 | $key = (int) array_search($row['value'], $field_options, true); |
||
287 | } |
||
288 | $data[$row['id_member']][$row['variable'] . '_key'] = $row['variable'] . '_' . $key; |
||
289 | 14 | $data[$row['id_member']][$row['variable']] = $row['value']; |
|
290 | } |
||
291 | ); |
||
292 | |||
293 | foreach ($data as $id => $val) |
||
294 | 14 | { |
|
295 | $this->users_list->appendTo($val, 'options', $id, $this->options['display_fields']); |
||
296 | 14 | } |
|
297 | } |
||
298 | |||
299 | 14 | /** |
|
300 | 14 | * Stored \ElkArte\Member objects in the cache |
|
301 | * |
||
302 | * @param int[] $new_loaded_ids Ids of members that have been loaded |
||
303 | */ |
||
304 | protected function storeInCache($new_loaded_ids) |
||
305 | { |
||
306 | if ($this->useCache) |
||
307 | { |
||
308 | foreach ($new_loaded_ids as $id) |
||
309 | { |
||
310 | $this->cache->put('member_data-' . $this->set . '-' . $id, $this->users_list->getById($id)->toArray(), 240); |
||
311 | } |
||
312 | } |
||
313 | } |
||
314 | |||
315 | /** |
||
316 | 14 | * Loads moderators data into the \ElkArte\Member objects |
|
317 | */ |
||
318 | protected function loadModerators() |
||
319 | { |
||
320 | 14 | global $board_info; |
|
321 | 14 | ||
322 | if (empty($this->loaded_ids) || $this->options['load_moderators'] === false || $this->set === MemberLoader::SET_NORMAL) |
||
323 | { |
||
324 | return; |
||
325 | } |
||
326 | |||
327 | $temp_mods = array_intersect($this->loaded_ids, array_keys($board_info['moderators'])); |
||
328 | 14 | if ($temp_mods !== []) |
|
329 | { |
||
330 | 14 | $group_info = []; |
|
331 | if ($this->cache->getVar($group_info, 'moderator_group_info', 480) === false) |
||
332 | { |
||
333 | require_once(SUBSDIR . '/Membergroups.subs.php'); |
||
334 | $group_info = membergroupById(3, true); |
||
335 | |||
336 | $this->cache->put('moderator_group_info', $group_info, 480); |
||
337 | 14 | } |
|
338 | |||
339 | foreach ($temp_mods as $id) |
||
340 | { |
||
341 | // By popular demand, don't show admins or global moderators as moderators. |
||
342 | 14 | if ($this->loaded_members[$id]['id_group'] !== 1 && $this->loaded_members[$id]['id_group'] !== 2) |
|
343 | { |
||
344 | 14 | $this->loaded_members[$id]['member_group'] = $group_info['group_name']; |
|
345 | } |
||
346 | 14 | ||
347 | // If the Moderator group has no color or icons, but their group does... don't overwrite. |
||
348 | 14 | if (!empty($group_info['icons'])) |
|
349 | { |
||
350 | $this->loaded_members[$id]['icons'] = $group_info['icons']; |
||
351 | } |
||
352 | |||
353 | if (!empty($group_info['online_color'])) |
||
354 | { |
||
355 | $this->loaded_members[$id]['member_group_color'] = $group_info['online_color']; |
||
356 | } |
||
357 | } |
||
358 | } |
||
359 | } |
||
360 | |||
361 | /** |
||
362 | * Loads users data from a member name |
||
363 | * |
||
364 | * @param string|string[] $name Single name or list of names to load |
||
365 | * @param string $set The data to load (see the constants SET_*) |
||
366 | * @return bool|int[] The ids of the members loaded |
||
367 | */ |
||
368 | public function loadByName($name, $set = MemberLoader::SET_NORMAL) |
||
369 | { |
||
370 | $users = array_filter(array_unique((array) $name)); |
||
371 | |||
372 | // Can't just look for no users :P. |
||
373 | if (empty($users)) |
||
374 | { |
||
375 | return false; |
||
376 | } |
||
377 | |||
378 | $this->set = $set; |
||
379 | $this->loaded_ids = []; |
||
380 | $this->loadByCondition('{column_case_insensitive:mem.member_name}' . (count($users) === 1 ? ' = {string_case_insensitive:users}' : ' IN ({array_string_case_insensitive:users})'), $users); |
||
381 | |||
382 | $this->loadModerators(); |
||
383 | |||
384 | return $this->loaded_ids; |
||
385 | } |
||
386 | |||
387 | /** |
||
388 | * Loads a guest member (i.e. some standard data for guests) |
||
389 | */ |
||
390 | public function loadGuest() |
||
391 | { |
||
392 | global $txt; |
||
393 | |||
394 | if (isset($this->loaded_members[0])) |
||
395 | { |
||
396 | return; |
||
397 | } |
||
398 | |||
399 | $this->loaded_members[0] = new Member([ |
||
400 | 'id' => 0, |
||
401 | 'name' => $txt['guest_title'], |
||
402 | 'group' => $txt['guest_title'], |
||
403 | 'href' => '', |
||
404 | 'link' => $txt['guest_title'], |
||
405 | 'email' => $txt['guest_title'], |
||
406 | 'is_guest' => true |
||
407 | ], $this->set, $this->bbc_parser); |
||
408 | $this->users_list::add($this->loaded_members[0], 0); |
||
409 | } |
||
410 | } |
||
411 |