· 7 years ago · Oct 12, 2018, 07:22 PM
1<?php
2/**
3Â * MyBB 1.8
4Â * Copyright 2014 MyBB Group, All Rights Reserved
5Â *
6Â * Website: MyBB - Free and Open Source Forum Software
7Â * License: License Agreement - MyBB
8Â *
9Â */
10
11/**
12Â * Checks if a user with uid $uid exists in the database.
13Â *
14Â * @param int $uid The uid to check for.
15Â * @return boolean True when exists, false when not.
16Â */
17function user_exists($uid)
18{
19Â Â Â global $db;
20
21Â Â Â $query = $db->simple_select("users", "COUNT(*) as user", "uid='".(int)$uid."'", array('limit' => 1));
22Â Â Â if($db->fetch_field($query, 'user') == 1)
23Â Â Â {
24Â Â Â Â Â Â return true;
25Â Â Â }
26Â Â Â else
27Â Â Â {
28Â Â Â Â Â Â return false;
29Â Â Â }
30}
31
32/**
33Â * Checks if $username already exists in the database.
34Â *
35Â * @param string $username The username for check for.
36Â * @return boolean True when exists, false when not.
37Â */
38function username_exists($username)
39{
40Â Â Â $options = array(
41Â Â Â Â Â Â 'username_method' => 2
42Â Â Â );
43
44Â Â Â return (bool)get_user_by_username($username, $options);
45}
46
47/**
48Â * Checks a password with a supplied username.
49Â *
50Â * @param string $username The username of the user.
51Â * @param string $password The plain-text password.
52Â * @return boolean|array False when no match, array with user info when match.
53Â */
54function validate_password_from_username($username, $password)
55{
56Â Â Â global $mybb;
57
58Â Â Â $options = array(
59Â Â Â Â Â Â 'fields' => '*',
60Â Â Â Â Â Â 'username_method' => $mybb->settings['username_method'],
61Â Â Â );
62
63Â Â Â $user = get_user_by_username($username, $options);
64
65Â Â Â if(!$user['uid'])
66Â Â Â {
67Â Â Â Â Â Â return false;
68Â Â Â }
69
70Â Â Â return validate_password_from_uid($user['uid'], $password, $user);
71}
72
73/**
74Â * Checks a password with a supplied uid.
75Â *
76Â * @param int $uid The user id.
77Â * @param string $password The plain-text password.
78Â * @param array $user An optional user data array.
79Â * @return boolean|array False when not valid, user data array when valid.
80Â */
81function validate_password_from_uid($uid, $password, $user = array())
82{
83Â Â Â global $db, $mybb;
84Â Â Â if(isset($mybb->user['uid']) && $mybb->user['uid'] == $uid)
85Â Â Â {
86Â Â Â Â Â Â $user = $mybb->user;
87Â Â Â }
88Â Â Â if(!$user['password'])
89Â Â Â {
90Â Â Â Â Â Â $user = get_user($uid);
91Â Â Â }
92Â Â Â if(!$user['salt'])
93Â Â Â {
94Â Â Â Â Â Â // Generate a salt for this user and assume the password stored in db is a plain md5 password
95Â Â Â Â Â Â $password_fields = create_password($user['password'], false, $user);
96Â Â Â Â Â Â $db->update_query("users", $password_fields, "uid='".$user['uid']."'");
97Â Â Â }
98
99Â Â Â if(!$user['loginkey'])
100Â Â Â {
101Â Â Â Â Â Â $user['loginkey'] = generate_loginkey();
102Â Â Â Â Â Â $sql_array = array(
103Â Â Â Â Â Â Â Â Â "loginkey" => $user['loginkey']
104Â Â Â Â Â Â );
105Â Â Â Â Â Â $db->update_query("users", $sql_array, "uid = ".$user['uid']);
106Â Â Â }
107Â Â Â if(verify_user_password($user, $password))
108Â Â Â {
109Â Â Â Â Â Â return $user;
110Â Â Â }
111Â Â Â else
112Â Â Â {
113Â Â Â Â Â Â return false;
114Â Â Â }
115}
116
117/**
118Â * Updates a user's password.
119Â *
120Â * @param int $uid The user's id.
121Â * @param string $password The md5()'ed password.
122Â * @param string $salt (Optional) The salt of the user.
123Â * @return array The new password.
124Â * @deprecated deprecated since version 1.8.6 Please use other alternatives.
125Â */
126function update_password($uid, $password, $salt="")
127{
128Â Â Â global $db, $plugins;
129
130Â Â Â $newpassword = array();
131
132Â Â Â // If no salt was specified, check in database first, if still doesn't exist, create one
133Â Â Â if(!$salt)
134Â Â Â {
135Â Â Â Â Â Â $query = $db->simple_select("users", "salt", "uid='$uid'");
136Â Â Â Â Â Â $user = $db->fetch_array($query);
137Â Â Â Â Â Â if($user['salt'])
138Â Â Â Â Â Â {
139Â Â Â Â Â Â Â Â Â $salt = $user['salt'];
140Â Â Â Â Â Â }
141Â Â Â Â Â Â else
142Â Â Â Â Â Â {
143Â Â Â Â Â Â Â Â Â $salt = generate_salt();
144Â Â Â Â Â Â }
145Â Â Â Â Â Â $newpassword['salt'] = $salt;
146Â Â Â }
147
148Â Â Â // Create new password based on salt
149Â Â Â $saltedpw = salt_password($password, $salt);
150
151Â Â Â // Generate new login key
152Â Â Â $loginkey = generate_loginkey();
153
154Â Â Â // Update password and login key in database
155Â Â Â $newpassword['password'] = $saltedpw;
156Â Â Â $newpassword['loginkey'] = $loginkey;
157Â Â Â $db->update_query("users", $newpassword, "uid='$uid'");
158
159Â Â Â $plugins->run_hooks("password_changed");
160
161Â Â Â return $newpassword;
162}
163
164/**
165Â * Salts a password based on a supplied salt.
166Â *
167Â * @param string $password The md5()'ed password.
168Â * @param string $salt The salt.
169Â * @return string The password hash.
170Â * @deprecated deprecated since version 1.8.9 Please use other alternatives.
171Â */
172function salt_password($password, $salt)
173{
174Â Â Â return md5(md5($salt).$password);
175}
176
177/**
178Â * Salts a password based on a supplied salt.
179Â *
180Â * @param string $password The input password.
181Â * @param string $salt (Optional) The salt used by the MyBB algorithm.
182Â * @param string $user (Optional) An array containing password-related data.
183Â * @return array Password-related fields.
184Â */
185function create_password($password, $salt = false, $user = false)
186{
187Â Â Â global $plugins;
188
189Â Â Â $fields = null;
190
191Â Â Â $parameters = compact('password', 'salt', 'user', 'fields');
192
193Â Â Â if(!defined('IN_INSTALL') && !defined('IN_UPGRADE'))
194Â Â Â {
195Â Â Â Â Â Â $plugins->run_hooks('create_password', $parameters);
196Â Â Â }
197
198Â Â Â if(!is_null($parameters['fields']))
199Â Â Â {
200Â Â Â Â Â Â $fields = $parameters['fields'];
201Â Â Â }
202Â Â Â else
203Â Â Â {
204Â Â Â Â Â Â if(!$salt)
205Â Â Â Â Â Â {
206Â Â Â Â Â Â Â Â Â $salt = generate_salt();
207Â Â Â Â Â Â }
208
209Â Â Â Â Â Â $hash = md5(md5($salt).md5($password));
210
211Â Â Â Â Â Â $fields = array(
212Â Â Â Â Â Â Â Â Â 'salt' => $salt,
213Â Â Â Â Â Â Â Â Â 'password' => $hash,
214Â Â Â Â Â Â );
215Â Â Â }
216
217Â Â Â return $fields;
218}
219
220/**
221Â * Compares user's password data against provided input.
222Â *
223Â * @param array $user An array containing password-related data.
224Â * @param string $password The plain-text input password.
225Â * @return bool Result of the comparison.
226Â */
227function verify_user_password($user, $password)
228{
229Â Â Â global $plugins;
230
231Â Â Â $result = null;
232
233Â Â Â $parameters = compact('user', 'password', 'result');
234
235Â Â Â if(!defined('IN_INSTALL') && !defined('IN_UPGRADE'))
236Â Â Â {
237Â Â Â Â Â Â $plugins->run_hooks('verify_user_password', $parameters);
238Â Â Â }
239
240Â Â Â if(!is_null($parameters['result']))
241Â Â Â {
242Â Â Â Â Â Â return $parameters['result'];
243Â Â Â }
244Â Â Â else
245Â Â Â {
246Â Â Â Â Â Â $password_fields = create_password($password, $user['salt'], $user);
247
248Â Â Â Â Â Â return my_hash_equals($user['password'], $password_fields['password']);
249Â Â Â }
250}
251
252/**
253Â * Generates a random salt
254Â *
255Â * @return string The salt.
256Â */
257function generate_salt()
258{
259Â Â Â return random_str(8);
260}
261
262/**
263Â * Generates a 50 character random login key.
264Â *
265Â * @return string The login key.
266Â */
267function generate_loginkey()
268{
269Â Â Â return random_str(50);
270}
271
272/**
273Â * Updates a user's salt in the database (does not update a password).
274Â *
275Â * @param int $uid The uid of the user to update.
276Â * @return string The new salt.
277Â */
278function update_salt($uid)
279{
280Â Â Â global $db;
281
282Â Â Â $salt = generate_salt();
283Â Â Â $sql_array = array(
284Â Â Â Â Â Â "salt" => $salt
285Â Â Â );
286Â Â Â $db->update_query("users", $sql_array, "uid='{$uid}'");
287
288Â Â Â return $salt;
289}
290
291/**
292Â * Generates a new login key for a user.
293Â *
294Â * @param int $uid The uid of the user to update.
295Â * @return string The new login key.
296Â */
297function update_loginkey($uid)
298{
299Â Â Â global $db;
300
301Â Â Â $loginkey = generate_loginkey();
302Â Â Â $sql_array = array(
303Â Â Â Â Â Â "loginkey" => $loginkey
304Â Â Â );
305Â Â Â $db->update_query("users", $sql_array, "uid='{$uid}'");
306
307Â Â Â return $loginkey;
308
309}
310
311/**
312Â * Adds a thread to a user's thread subscription list.
313Â * If no uid is supplied, the currently logged in user's id will be used.
314Â *
315Â * @param int $tid The tid of the thread to add to the list.
316Â * @param int $notification (Optional) The type of notification to receive for replies (0=none, 1=email, 2=pm)
317Â * @param int $uid (Optional) The uid of the user who's list to update.
318Â * @return boolean True when success, false when otherwise.
319Â */
320function add_subscribed_thread($tid, $notification=1, $uid=0)
321{
322Â Â Â global $mybb, $db;
323
324Â Â Â if(!$uid)
325Â Â Â {
326Â Â Â Â Â Â $uid = $mybb->user['uid'];
327Â Â Â }
328
329Â Â Â if(!$uid)
330Â Â Â {
331Â Â Â Â Â Â return false;
332Â Â Â }
333
334Â Â Â $query = $db->simple_select("threadsubscriptions", "*", "tid='".(int)$tid."' AND uid='".(int)$uid."'");
335Â Â Â $subscription = $db->fetch_array($query);
336Â Â Â if(!$subscription['tid'])
337Â Â Â {
338Â Â Â Â Â Â $insert_array = array(
339Â Â Â Â Â Â Â Â Â 'uid' => (int)$uid,
340Â Â Â Â Â Â Â Â Â 'tid' => (int)$tid,
341Â Â Â Â Â Â Â Â Â 'notification' => (int)$notification,
342Â Â Â Â Â Â Â Â Â 'dateline' => TIME_NOW
343Â Â Â Â Â Â );
344Â Â Â Â Â Â $db->insert_query("threadsubscriptions", $insert_array);
345Â Â Â }
346Â Â Â else
347Â Â Â {
348Â Â Â Â Â Â // Subscription exists - simply update notification
349Â Â Â Â Â Â $update_array = array(
350Â Â Â Â Â Â Â Â Â "notification" => (int)$notification
351Â Â Â Â Â Â );
352Â Â Â Â Â Â $db->update_query("threadsubscriptions", $update_array, "uid='{$uid}' AND tid='{$tid}'");
353Â Â Â }
354Â Â Â return true;
355}
356
357/**
358Â * Remove a thread from a user's thread subscription list.
359Â * If no uid is supplied, the currently logged in user's id will be used.
360Â *
361Â * @param int $tid The tid of the thread to remove from the list.
362Â * @param int $uid (Optional) The uid of the user who's list to update.
363Â * @return boolean True when success, false when otherwise.
364Â */
365function remove_subscribed_thread($tid, $uid=0)
366{
367Â Â Â global $mybb, $db;
368
369Â Â Â if(!$uid)
370Â Â Â {
371Â Â Â Â Â Â $uid = $mybb->user['uid'];
372Â Â Â }
373
374Â Â Â if(!$uid)
375Â Â Â {
376Â Â Â Â Â Â return false;
377Â Â Â }
378Â Â Â $db->delete_query("threadsubscriptions", "tid='".$tid."' AND uid='{$uid}'");
379
380Â Â Â return true;
381}
382
383/**
384Â * Adds a forum to a user's forum subscription list.
385Â * If no uid is supplied, the currently logged in user's id will be used.
386Â *
387Â * @param int $fid The fid of the forum to add to the list.
388Â * @param int $uid (Optional) The uid of the user who's list to update.
389Â * @return boolean True when success, false when otherwise.
390Â */
391function add_subscribed_forum($fid, $uid=0)
392{
393Â Â Â global $mybb, $db;
394
395Â Â Â if(!$uid)
396Â Â Â {
397Â Â Â Â Â Â $uid = $mybb->user['uid'];
398Â Â Â }
399
400Â Â Â if(!$uid)
401Â Â Â {
402Â Â Â Â Â Â return false;
403Â Â Â }
404
405Â Â Â $fid = (int)$fid;
406Â Â Â $uid = (int)$uid;
407
408Â Â Â $query = $db->simple_select("forumsubscriptions", "*", "fid='".$fid."' AND uid='{$uid}'", array('limit' => 1));
409Â Â Â $fsubscription = $db->fetch_array($query);
410Â Â Â if(!$fsubscription['fid'])
411Â Â Â {
412Â Â Â Â Â Â $insert_array = array(
413Â Â Â Â Â Â Â Â Â 'fid' => $fid,
414Â Â Â Â Â Â Â Â Â 'uid' => $uid
415Â Â Â Â Â Â );
416Â Â Â Â Â Â $db->insert_query("forumsubscriptions", $insert_array);
417Â Â Â }
418
419Â Â Â return true;
420}
421
422/**
423Â * Removes a forum from a user's forum subscription list.
424Â * If no uid is supplied, the currently logged in user's id will be used.
425Â *
426Â * @param int $fid The fid of the forum to remove from the list.
427Â * @param int $uid (Optional) The uid of the user who's list to update.
428Â * @return boolean True when success, false when otherwise.
429Â */
430function remove_subscribed_forum($fid, $uid=0)
431{
432Â Â Â global $mybb, $db;
433
434Â Â Â if(!$uid)
435Â Â Â {
436Â Â Â Â Â Â $uid = $mybb->user['uid'];
437Â Â Â }
438
439Â Â Â if(!$uid)
440Â Â Â {
441Â Â Â Â Â Â return false;
442Â Â Â }
443Â Â Â $db->delete_query("forumsubscriptions", "fid='".$fid."' AND uid='{$uid}'");
444
445Â Â Â return true;
446}
447
448/**
449Â * Constructs the usercp navigation menu.
450Â *
451Â */
452function usercp_menu()
453{
454Â Â Â global $mybb, $templates, $theme, $plugins, $lang, $usercpnav, $usercpmenu;
455
456Â Â Â $lang->load("usercpnav");
457
458Â Â Â // Add the default items as plugins with separated priorities of 10
459Â Â Â if($mybb->settings['enablepms'] != 0 && $mybb->usergroup['canusepms'] == 1)
460Â Â Â {
461Â Â Â Â Â Â $plugins->add_hook("usercp_menu", "usercp_menu_messenger", 10);
462Â Â Â }
463
464Â Â Â if($mybb->usergroup['canusercp'] == 1)
465Â Â Â {
466Â Â Â Â Â Â $plugins->add_hook("usercp_menu", "usercp_menu_profile", 20);
467Â Â Â Â Â Â $plugins->add_hook("usercp_menu", "usercp_menu_misc", 30);
468Â Â Â }
469
470Â Â Â // Run the plugin hooks
471Â Â Â $plugins->run_hooks("usercp_menu");
472Â Â Â global $usercpmenu;
473
474Â Â Â if($mybb->usergroup['canusercp'] == 1)
475Â Â Â {
476Â Â Â Â Â Â eval("\$ucp_nav_home = \"".$templates->get("usercp_nav_home")."\";");
477Â Â Â }
478
479Â Â Â eval("\$usercpnav = \"".$templates->get("usercp_nav")."\";");
480
481Â Â Â $plugins->run_hooks("usercp_menu_built");
482}
483
484/**
485Â * Constructs the usercp messenger menu.
486Â *
487Â */
488function usercp_menu_messenger()
489{
490Â Â Â global $db, $mybb, $templates, $theme, $usercpmenu, $lang, $collapse, $collapsed, $collapsedimg;
491
492Â Â Â $expaltext = (in_array("usercppms", $collapse)) ? "[+]" : "[-]";
493Â Â Â $usercp_nav_messenger = $templates->get("usercp_nav_messenger");
494Â Â Â // Hide tracking link if no permission
495Â Â Â $tracking = '';
496Â Â Â if($mybb->usergroup['cantrackpms'])
497Â Â Â {
498Â Â Â Â Â Â $tracking = $templates->get("usercp_nav_messenger_tracking");
499Â Â Â }
500Â Â Â eval("\$ucp_nav_tracking = \"". $tracking ."\";");
501
502Â Â Â // Hide compose link if no permission
503Â Â Â $ucp_nav_compose = '';
504Â Â Â if($mybb->usergroup['cansendpms'] == 1)
505Â Â Â {
506Â Â Â Â Â Â eval("\$ucp_nav_compose = \"".$templates->get("usercp_nav_messenger_compose")."\";");
507Â Â Â }
508
509Â Â Â $folderlinks = $folder_id = $folder_name = '';
510Â Â Â $foldersexploded = explode("$%%$", $mybb->user['pmfolders']);
511Â Â Â foreach($foldersexploded as $key => $folders)
512Â Â Â {
513Â Â Â Â Â Â $folderinfo = explode("**", $folders, 2);
514Â Â Â Â Â Â $folderinfo[1] = get_pm_folder_name($folderinfo[0], $folderinfo[1]);
515Â Â Â Â Â Â if($folderinfo[0] == 4)
516Â Â Â Â Â Â {
517Â Â Â Â Â Â Â Â Â $class = "usercp_nav_trash_pmfolder";
518Â Â Â Â Â Â }
519Â Â Â Â Â Â else if($folderlinks)
520Â Â Â Â Â Â {
521Â Â Â Â Â Â Â Â Â $class = "usercp_nav_sub_pmfolder";
522Â Â Â Â Â Â }
523Â Â Â Â Â Â else
524Â Â Â Â Â Â {
525Â Â Â Â Â Â Â Â Â $class = "usercp_nav_pmfolder";
526Â Â Â Â Â Â }
527
528Â Â Â Â Â Â $folder_id = $folderinfo[0];
529Â Â Â Â Â Â $folder_name = $folderinfo[1];
530
531Â Â Â Â Â Â eval("\$folderlinks .= \"".$templates->get("usercp_nav_messenger_folder")."\";");
532Â Â Â }
533
534Â Â Â if(!isset($collapsedimg['usercppms']))
535Â Â Â {
536Â Â Â Â Â Â $collapsedimg['usercppms'] = '';
537Â Â Â }
538
539Â Â Â if(!isset($collapsed['usercppms_e']))
540Â Â Â {
541Â Â Â Â Â Â $collapsed['usercppms_e'] = '';
542Â Â Â }
543
544Â Â Â eval("\$usercpmenu .= \"".$usercp_nav_messenger."\";");
545}
546
547/**
548Â * Constructs the usercp profile menu.
549Â *
550Â */
551function usercp_menu_profile()
552{
553Â Â Â global $db, $mybb, $templates, $theme, $usercpmenu, $lang, $collapse, $collapsed, $collapsedimg;
554
555Â Â Â $changenameop = '';
556Â Â Â if($mybb->usergroup['canchangename'] != 0)
557Â Â Â {
558Â Â Â Â Â Â eval("\$changenameop = \"".$templates->get("usercp_nav_changename")."\";");
559Â Â Â }
560
561Â Â Â $changesigop = '';
562Â Â Â if($mybb->usergroup['canusesig'] == 1 && ($mybb->usergroup['canusesigxposts'] == 0 || $mybb->usergroup['canusesigxposts'] > 0 && $mybb->user['postnum'] > $mybb->usergroup['canusesigxposts']))
563Â Â Â {
564Â Â Â Â Â Â if($mybb->user['suspendsignature'] == 0 || $mybb->user['suspendsignature'] == 1 && $mybb->user['suspendsigtime'] > 0 && $mybb->user['suspendsigtime'] < TIME_NOW)
565Â Â Â Â Â Â {
566Â Â Â Â Â Â Â Â Â eval("\$changesigop = \"".$templates->get("usercp_nav_editsignature")."\";");
567Â Â Â Â Â Â }
568Â Â Â }
569
570Â Â Â if(!isset($collapsedimg['usercpprofile']))
571Â Â Â {
572Â Â Â Â Â Â $collapsedimg['usercpprofile'] = '';
573Â Â Â }
574
575Â Â Â if(!isset($collapsed['usercpprofile_e']))
576Â Â Â {
577Â Â Â Â Â Â $collapsed['usercpprofile_e'] = '';
578Â Â Â }
579
580Â Â Â $expaltext = (in_array("usercpprofile", $collapse)) ? "[+]" : "[-]";
581Â Â Â eval("\$usercpmenu .= \"".$templates->get("usercp_nav_profile")."\";");
582}
583
584/**
585Â * Constructs the usercp misc menu.
586Â *
587Â */
588function usercp_menu_misc()
589{
590Â Â Â global $db, $mybb, $templates, $theme, $usercpmenu, $lang, $collapse, $collapsed, $collapsedimg;
591
592Â Â Â $draftstart = $draftend = '';
593Â Â Â $draftcount = $lang->ucp_nav_drafts;
594
595Â Â Â $query = $db->simple_select("posts", "COUNT(pid) AS draftcount", "visible = '-2' AND uid = '{$mybb->user['uid']}'");
596Â Â Â $count = $db->fetch_field($query, 'draftcount');
597
598Â Â Â if($count > 0)
599Â Â Â {
600Â Â Â Â Â Â $draftcount = $lang->sprintf($lang->ucp_nav_drafts_active, my_number_format($count));
601Â Â Â }
602
603Â Â Â if($mybb->settings['enableattachments'] != 0)
604Â Â Â {
605Â Â Â Â Â Â eval("\$attachmentop = \"".$templates->get("usercp_nav_attachments")."\";");
606Â Â Â }
607
608Â Â Â if(!isset($collapsedimg['usercpmisc']))
609Â Â Â {
610Â Â Â Â Â Â $collapsedimg['usercpmisc'] = '';
611Â Â Â }
612
613Â Â Â if(!isset($collapsed['usercpmisc_e']))
614Â Â Â {
615Â Â Â Â Â Â $collapsed['usercpmisc_e'] = '';
616Â Â Â }
617
618Â Â Â $profile_link = get_profile_link($mybb->user['uid']);
619Â Â Â $expaltext = (in_array("usercpmisc", $collapse)) ? "[+]" : "[-]";
620Â Â Â eval("\$usercpmenu .= \"".$templates->get("usercp_nav_misc")."\";");
621}
622
623/**
624Â * Gets the usertitle for a specific uid.
625Â *
626Â * @param int $uid The uid of the user to get the usertitle of.
627Â * @return string The usertitle of the user.
628Â */
629function get_usertitle($uid=0)
630{
631Â Â Â global $db, $mybb;
632
633Â Â Â if($mybb->user['uid'] == $uid)
634Â Â Â {
635Â Â Â Â Â Â $user = $mybb->user;
636Â Â Â }
637Â Â Â else
638Â Â Â {
639Â Â Â Â Â Â $query = $db->simple_select("users", "usertitle,postnum", "uid='$uid'", array('limit' => 1));
640Â Â Â Â Â Â $user = $db->fetch_array($query);
641Â Â Â }
642
643Â Â Â if($user['usertitle'])
644Â Â Â {
645Â Â Â Â Â Â return $user['usertitle'];
646Â Â Â }
647Â Â Â else
648Â Â Â {
649Â Â Â Â Â Â $usertitles = $mybb->cache->read('usertitles');
650Â Â Â Â Â Â foreach($usertitles as $title)
651Â Â Â Â Â Â {
652Â Â Â Â Â Â Â Â Â if($title['posts'] <= $user['postnum'])
653Â Â Â Â Â Â Â Â Â {
654Â Â Â Â Â Â Â Â Â Â Â Â $usertitle = $title;
655Â Â Â Â Â Â Â Â Â Â Â Â break;
656Â Â Â Â Â Â Â Â Â }
657Â Â Â Â Â Â }
658
659Â Â Â Â Â Â return $usertitle['title'];
660Â Â Â }
661}
662
663/**
664Â * Updates a users private message count in the users table with the number of pms they have.
665Â *
666Â * @param int $uid The user id to update the count for. If none, assumes currently logged in user.
667Â * @param int $count_to_update Bitwise value for what to update. 1 = total, 2 = new, 4 = unread. Combinations accepted.
668Â * @return array The updated counters
669Â */
670function update_pm_count($uid=0, $count_to_update=7)
671{
672Â Â Â global $db, $mybb;
673
674Â Â Â // If no user id, assume that we mean the current logged in user.
675Â Â Â if((int)$uid == 0)
676Â Â Â {
677Â Â Â Â Â Â $uid = $mybb->user['uid'];
678Â Â Â }
679
680Â Â Â $uid = (int)$uid;
681Â Â Â $pmcount = array();
682Â Â Â if($uid == 0)
683Â Â Â {
684Â Â Â Â Â Â return $pmcount;
685Â Â Â }
686
687Â Â Â // Update total number of messages.
688Â Â Â if($count_to_update & 1)
689Â Â Â {
690Â Â Â Â Â Â $query = $db->simple_select("privatemessages", "COUNT(pmid) AS pms_total", "uid='".$uid."'");
691Â Â Â Â Â Â $total = $db->fetch_array($query);
692Â Â Â Â Â Â $pmcount['totalpms'] = $total['pms_total'];
693Â Â Â }
694
695Â Â Â // Update number of unread messages.
696Â Â Â if($count_to_update & 2 && $db->field_exists("unreadpms", "users") == true)
697Â Â Â {
698Â Â Â Â Â Â $query = $db->simple_select("privatemessages", "COUNT(pmid) AS pms_unread", "uid='".$uid."' AND status='0' AND folder='1'");
699Â Â Â Â Â Â $unread = $db->fetch_array($query);
700Â Â Â Â Â Â $pmcount['unreadpms'] = $unread['pms_unread'];
701Â Â Â }
702
703Â Â Â if(!empty($pmcount))
704Â Â Â {
705Â Â Â Â Â Â $db->update_query("users", $pmcount, "uid='".$uid."'");
706Â Â Â }
707Â Â Â return $pmcount;
708}
709
710/**
711Â * Return the language specific name for a PM folder.
712Â *
713Â * @param int $fid The ID of the folder.
714Â * @param string $name The folder name - can be blank, will use language default.
715Â * @return string The name of the folder.
716Â */
717function get_pm_folder_name($fid, $name="")
718{
719Â Â Â global $lang;
720
721Â Â Â if($name != '')
722Â Â Â {
723Â Â Â Â Â Â return $name;
724Â Â Â }
725
726Â Â Â switch($fid)
727Â Â Â {
728Â Â Â Â Â Â case 1:
729Â Â Â Â Â Â Â Â Â return $lang->folder_inbox;
730Â Â Â Â Â Â Â Â Â break;
731Â Â Â Â Â Â case 2:
732Â Â Â Â Â Â Â Â Â return $lang->folder_sent_items;
733Â Â Â Â Â Â Â Â Â break;
734Â Â Â Â Â Â case 3:
735Â Â Â Â Â Â Â Â Â return $lang->folder_drafts;
736Â Â Â Â Â Â Â Â Â break;
737Â Â Â Â Â Â case 4:
738Â Â Â Â Â Â Â Â Â return $lang->folder_trash;
739Â Â Â Â Â Â Â Â Â break;
740Â Â Â Â Â Â default:
741Â Â Â Â Â Â Â Â Â return $lang->folder_untitled;
742Â Â Â }
743}
744
745/**
746Â * Generates a security question for registration.
747Â *
748Â * @param int $old_qid Optional ID of the old question.
749Â * @return string The question session id.
750Â */
751function generate_question($old_qid=0)
752{
753Â Â Â global $db;
754
755Â Â Â if($db->type == 'pgsql' || $db->type == 'sqlite')
756Â Â Â {
757Â Â Â Â Â Â $order_by = 'RANDOM()';
758Â Â Â }
759Â Â Â else
760Â Â Â {
761Â Â Â Â Â Â $order_by = 'RAND()';
762Â Â Â }
763Â Â Â
764Â Â Â if($old_qid)
765Â Â Â {
766Â Â Â Â Â Â $excl_old = ' AND qid != '.(int)$old_qid;
767Â Â Â }
768
769Â Â Â $query = $db->simple_select('questions', 'qid, shown', "active=1{$excl_old}", array('limit' => 1, 'order_by' => $order_by));
770Â Â Â $question = $db->fetch_array($query);
771
772Â Â Â if(!$db->num_rows($query))
773Â Â Â {
774Â Â Â Â Â Â // No active questions exist
775Â Â Â Â Â Â return false;
776Â Â Â }
777Â Â Â else
778Â Â Â {
779Â Â Â Â Â Â $sessionid = random_str(32);
780
781Â Â Â Â Â Â $sql_array = array(
782Â Â Â Â Â Â Â Â Â "sid" => $sessionid,
783Â Â Â Â Â Â Â Â Â "qid" => $question['qid'],
784Â Â Â Â Â Â Â Â Â "dateline" => TIME_NOW
785Â Â Â Â Â Â );
786Â Â Â Â Â Â $db->insert_query("questionsessions", $sql_array);
787
788Â Â Â Â Â Â $update_question = array(
789Â Â Â Â Â Â Â Â Â "shown" => $question['shown'] + 1
790Â Â Â Â Â Â );
791Â Â Â Â Â Â $db->update_query("questions", $update_question, "qid = '{$question['qid']}'");
792
793Â Â Â Â Â Â return $sessionid;
794Â Â Â }
795}
796
797/**
798Â * Check whether we can show the Purge Spammer Feature
799Â *
800Â * @param int $post_count The users post count
801Â * @param int $usergroup The usergroup of our user
802Â * @param int $uid The uid of our user
803Â * @return boolean Whether or not to show the feature
804Â */
805function purgespammer_show($post_count, $usergroup, $uid)
806{
807Â Â Â Â Â Â global $mybb, $cache;
808
809Â Â Â Â Â Â // only show this if the current user has permission to use it and the user has less than the post limit for using this tool
810Â Â Â Â Â Â $bangroup = $mybb->settings['purgespammerbangroup'];
811Â Â Â Â Â Â $usergroups = $cache->read('usergroups');
812
813Â Â Â Â Â Â return ($mybb->user['uid'] != $uid && is_member($mybb->settings['purgespammergroups']) && !is_super_admin($uid)
814Â Â Â Â Â Â Â Â Â && !$usergroups[$usergroup]['cancp'] && !$usergroups[$usergroup]['canmodcp'] && !$usergroups[$usergroup]['issupermod']
815Â Â Â Â Â Â Â Â Â && (str_replace($mybb->settings['thousandssep'], '', $post_count) <= $mybb->settings['purgespammerpostlimit'] || $mybb->settings['purgespammerpostlimit'] == 0)
816Â Â Â Â Â Â Â Â Â && !is_member($bangroup, $uid) && !$usergroups[$usergroup]['isbannedgroup']);
817}