Subs-Members.php 44 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426
  1. <?php
  2. /**
  3. * This file contains some useful functions for members and membergroups.
  4. *
  5. * Simple Machines Forum (SMF)
  6. *
  7. * @package SMF
  8. * @author Simple Machines http://www.simplemachines.org
  9. * @copyright 2011 Simple Machines
  10. * @license http://www.simplemachines.org/about/smf/license.php BSD
  11. *
  12. * @version 2.1 Alpha 1
  13. */
  14. if (!defined('SMF'))
  15. die('Hacking attempt...');
  16. /**
  17. * Delete one or more members.
  18. * Requires profile_remove_own or profile_remove_any permission for
  19. * respectively removing your own account or any account.
  20. * Non-admins cannot delete admins.
  21. * The function:
  22. * - changes author of messages, topics and polls to guest authors.
  23. * - removes all log entries concerning the deleted members, except the
  24. * error logs, ban logs and moderation logs.
  25. * - removes these members' personal messages (only the inbox), avatars,
  26. * ban entries, theme settings, moderator positions, poll votes, and
  27. * karma votes.
  28. * - updates member statistics afterwards.
  29. *
  30. * @param array $users
  31. * @param bool $check_not_admin = false
  32. */
  33. function deleteMembers($users, $check_not_admin = false)
  34. {
  35. global $sourcedir, $modSettings, $user_info, $smcFunc;
  36. // Try give us a while to sort this out...
  37. @set_time_limit(600);
  38. // Try to get some more memory.
  39. if (ini_get('memory_limit') < 128)
  40. @ini_set('memory_limit', '128M');
  41. // If it's not an array, make it so!
  42. if (!is_array($users))
  43. $users = array($users);
  44. else
  45. $users = array_unique($users);
  46. // Make sure there's no void user in here.
  47. $users = array_diff($users, array(0));
  48. // How many are they deleting?
  49. if (empty($users))
  50. return;
  51. elseif (count($users) == 1)
  52. {
  53. list ($user) = $users;
  54. if ($user == $user_info['id'])
  55. isAllowedTo('profile_remove_own');
  56. else
  57. isAllowedTo('profile_remove_any');
  58. }
  59. else
  60. {
  61. foreach ($users as $k => $v)
  62. $users[$k] = (int) $v;
  63. // Deleting more than one? You can't have more than one account...
  64. isAllowedTo('profile_remove_any');
  65. }
  66. // Get their names for logging purposes.
  67. $request = $smcFunc['db_query']('', '
  68. SELECT id_member, member_name, CASE WHEN id_group = {int:admin_group} OR FIND_IN_SET({int:admin_group}, additional_groups) != 0 THEN 1 ELSE 0 END AS is_admin
  69. FROM {db_prefix}members
  70. WHERE id_member IN ({array_int:user_list})
  71. LIMIT ' . count($users),
  72. array(
  73. 'user_list' => $users,
  74. 'admin_group' => 1,
  75. )
  76. );
  77. $admins = array();
  78. $user_log_details = array();
  79. while ($row = $smcFunc['db_fetch_assoc']($request))
  80. {
  81. if ($row['is_admin'])
  82. $admins[] = $row['id_member'];
  83. $user_log_details[$row['id_member']] = array($row['id_member'], $row['member_name']);
  84. }
  85. $smcFunc['db_free_result']($request);
  86. if (empty($user_log_details))
  87. return;
  88. // Make sure they aren't trying to delete administrators if they aren't one. But don't bother checking if it's just themself.
  89. if (!empty($admins) && ($check_not_admin || (!allowedTo('admin_forum') && (count($users) != 1 || $users[0] != $user_info['id']))))
  90. {
  91. $users = array_diff($users, $admins);
  92. foreach ($admins as $id)
  93. unset($user_log_details[$id]);
  94. }
  95. // No one left?
  96. if (empty($users))
  97. return;
  98. require_once($sourcedir . 'Logging.php');
  99. // Log the action - regardless of who is deleting it.
  100. $log_inserts = array();
  101. foreach ($user_log_details as $user)
  102. {
  103. // Integration rocks!
  104. call_integration_hook('integrate_delete_member', array($user[0]));
  105. logAction('delete_member', array('member' => $user[0], 'name' => $user[1], 'member_acted' => $user_info['name']), 'admin');
  106. // Remove any cached data if enabled.
  107. if (!empty($modSettings['cache_enable']) && $modSettings['cache_enable'] >= 2)
  108. cache_put_data('user_settings-' . $user[0], null, 60);
  109. }
  110. // Make these peoples' posts guest posts.
  111. $smcFunc['db_query']('', '
  112. UPDATE {db_prefix}messages
  113. SET id_member = {int:guest_id}' . (!empty($modSettings['deleteMembersRemovesEmail']) ? ',
  114. poster_email = {string:blank_email}' : '') . '
  115. WHERE id_member IN ({array_int:users})',
  116. array(
  117. 'guest_id' => 0,
  118. 'blank_email' => '',
  119. 'users' => $users,
  120. )
  121. );
  122. $smcFunc['db_query']('', '
  123. UPDATE {db_prefix}polls
  124. SET id_member = {int:guest_id}
  125. WHERE id_member IN ({array_int:users})',
  126. array(
  127. 'guest_id' => 0,
  128. 'users' => $users,
  129. )
  130. );
  131. // Make these peoples' posts guest first posts and last posts.
  132. $smcFunc['db_query']('', '
  133. UPDATE {db_prefix}topics
  134. SET id_member_started = {int:guest_id}
  135. WHERE id_member_started IN ({array_int:users})',
  136. array(
  137. 'guest_id' => 0,
  138. 'users' => $users,
  139. )
  140. );
  141. $smcFunc['db_query']('', '
  142. UPDATE {db_prefix}topics
  143. SET id_member_updated = {int:guest_id}
  144. WHERE id_member_updated IN ({array_int:users})',
  145. array(
  146. 'guest_id' => 0,
  147. 'users' => $users,
  148. )
  149. );
  150. $smcFunc['db_query']('', '
  151. UPDATE {db_prefix}log_actions
  152. SET id_member = {int:guest_id}
  153. WHERE id_member IN ({array_int:users})',
  154. array(
  155. 'guest_id' => 0,
  156. 'users' => $users,
  157. )
  158. );
  159. $smcFunc['db_query']('', '
  160. UPDATE {db_prefix}log_banned
  161. SET id_member = {int:guest_id}
  162. WHERE id_member IN ({array_int:users})',
  163. array(
  164. 'guest_id' => 0,
  165. 'users' => $users,
  166. )
  167. );
  168. $smcFunc['db_query']('', '
  169. UPDATE {db_prefix}log_errors
  170. SET id_member = {int:guest_id}
  171. WHERE id_member IN ({array_int:users})',
  172. array(
  173. 'guest_id' => 0,
  174. 'users' => $users,
  175. )
  176. );
  177. // Delete the member.
  178. $smcFunc['db_query']('', '
  179. DELETE FROM {db_prefix}members
  180. WHERE id_member IN ({array_int:users})',
  181. array(
  182. 'users' => $users,
  183. )
  184. );
  185. // Delete the logs...
  186. $smcFunc['db_query']('', '
  187. DELETE FROM {db_prefix}log_actions
  188. WHERE id_log = {int:log_type}
  189. AND id_member IN ({array_int:users})',
  190. array(
  191. 'log_type' => 2,
  192. 'users' => $users,
  193. )
  194. );
  195. $smcFunc['db_query']('', '
  196. DELETE FROM {db_prefix}log_boards
  197. WHERE id_member IN ({array_int:users})',
  198. array(
  199. 'users' => $users,
  200. )
  201. );
  202. $smcFunc['db_query']('', '
  203. DELETE FROM {db_prefix}log_comments
  204. WHERE id_recipient IN ({array_int:users})
  205. AND comment_type = {string:warntpl}',
  206. array(
  207. 'users' => $users,
  208. 'warntpl' => 'warntpl',
  209. )
  210. );
  211. $smcFunc['db_query']('', '
  212. DELETE FROM {db_prefix}log_group_requests
  213. WHERE id_member IN ({array_int:users})',
  214. array(
  215. 'users' => $users,
  216. )
  217. );
  218. $smcFunc['db_query']('', '
  219. DELETE FROM {db_prefix}log_karma
  220. WHERE id_target IN ({array_int:users})
  221. OR id_executor IN ({array_int:users})',
  222. array(
  223. 'users' => $users,
  224. )
  225. );
  226. $smcFunc['db_query']('', '
  227. DELETE FROM {db_prefix}log_mark_read
  228. WHERE id_member IN ({array_int:users})',
  229. array(
  230. 'users' => $users,
  231. )
  232. );
  233. $smcFunc['db_query']('', '
  234. DELETE FROM {db_prefix}log_notify
  235. WHERE id_member IN ({array_int:users})',
  236. array(
  237. 'users' => $users,
  238. )
  239. );
  240. $smcFunc['db_query']('', '
  241. DELETE FROM {db_prefix}log_online
  242. WHERE id_member IN ({array_int:users})',
  243. array(
  244. 'users' => $users,
  245. )
  246. );
  247. $smcFunc['db_query']('', '
  248. DELETE FROM {db_prefix}log_subscribed
  249. WHERE id_member IN ({array_int:users})',
  250. array(
  251. 'users' => $users,
  252. )
  253. );
  254. $smcFunc['db_query']('', '
  255. DELETE FROM {db_prefix}log_topics
  256. WHERE id_member IN ({array_int:users})',
  257. array(
  258. 'users' => $users,
  259. )
  260. );
  261. $smcFunc['db_query']('', '
  262. DELETE FROM {db_prefix}collapsed_categories
  263. WHERE id_member IN ({array_int:users})',
  264. array(
  265. 'users' => $users,
  266. )
  267. );
  268. // Make their votes appear as guest votes - at least it keeps the totals right.
  269. // @todo Consider adding back in cookie protection.
  270. $smcFunc['db_query']('', '
  271. UPDATE {db_prefix}log_polls
  272. SET id_member = {int:guest_id}
  273. WHERE id_member IN ({array_int:users})',
  274. array(
  275. 'guest_id' => 0,
  276. 'users' => $users,
  277. )
  278. );
  279. // Delete personal messages.
  280. require_once($sourcedir . '/PersonalMessage.php');
  281. deleteMessages(null, null, $users);
  282. $smcFunc['db_query']('', '
  283. UPDATE {db_prefix}personal_messages
  284. SET id_member_from = {int:guest_id}
  285. WHERE id_member_from IN ({array_int:users})',
  286. array(
  287. 'guest_id' => 0,
  288. 'users' => $users,
  289. )
  290. );
  291. // They no longer exist, so we don't know who it was sent to.
  292. $smcFunc['db_query']('', '
  293. DELETE FROM {db_prefix}pm_recipients
  294. WHERE id_member IN ({array_int:users})',
  295. array(
  296. 'users' => $users,
  297. )
  298. );
  299. // Delete avatar.
  300. require_once($sourcedir . '/ManageAttachments.php');
  301. removeAttachments(array('id_member' => $users));
  302. // It's over, no more moderation for you.
  303. $smcFunc['db_query']('', '
  304. DELETE FROM {db_prefix}moderators
  305. WHERE id_member IN ({array_int:users})',
  306. array(
  307. 'users' => $users,
  308. )
  309. );
  310. $smcFunc['db_query']('', '
  311. DELETE FROM {db_prefix}group_moderators
  312. WHERE id_member IN ({array_int:users})',
  313. array(
  314. 'users' => $users,
  315. )
  316. );
  317. // If you don't exist we can't ban you.
  318. $smcFunc['db_query']('', '
  319. DELETE FROM {db_prefix}ban_items
  320. WHERE id_member IN ({array_int:users})',
  321. array(
  322. 'users' => $users,
  323. )
  324. );
  325. // Remove individual theme settings.
  326. $smcFunc['db_query']('', '
  327. DELETE FROM {db_prefix}themes
  328. WHERE id_member IN ({array_int:users})',
  329. array(
  330. 'users' => $users,
  331. )
  332. );
  333. // These users are nobody's buddy nomore.
  334. $request = $smcFunc['db_query']('', '
  335. SELECT id_member, pm_ignore_list, buddy_list
  336. FROM {db_prefix}members
  337. WHERE FIND_IN_SET({raw:pm_ignore_list}, pm_ignore_list) != 0 OR FIND_IN_SET({raw:buddy_list}, buddy_list) != 0',
  338. array(
  339. 'pm_ignore_list' => implode(', pm_ignore_list) != 0 OR FIND_IN_SET(', $users),
  340. 'buddy_list' => implode(', buddy_list) != 0 OR FIND_IN_SET(', $users),
  341. )
  342. );
  343. while ($row = $smcFunc['db_fetch_assoc']($request))
  344. $smcFunc['db_query']('', '
  345. UPDATE {db_prefix}members
  346. SET
  347. pm_ignore_list = {string:pm_ignore_list},
  348. buddy_list = {string:buddy_list}
  349. WHERE id_member = {int:id_member}',
  350. array(
  351. 'id_member' => $row['id_member'],
  352. 'pm_ignore_list' => implode(',', array_diff(explode(',', $row['pm_ignore_list']), $users)),
  353. 'buddy_list' => implode(',', array_diff(explode(',', $row['buddy_list']), $users)),
  354. )
  355. );
  356. $smcFunc['db_free_result']($request);
  357. // Make sure no member's birthday is still sticking in the calendar...
  358. updateSettings(array(
  359. 'calendar_updated' => time(),
  360. ));
  361. updateStats('member');
  362. }
  363. /**
  364. * Registers a member to the forum.
  365. * Allows two types of interface: 'guest' and 'admin'. The first
  366. * includes hammering protection, the latter can perform the
  367. * registration silently.
  368. * The strings used in the options array are assumed to be escaped.
  369. * Allows to perform several checks on the input, e.g. reserved names.
  370. * The function will adjust member statistics.
  371. * If an error is detected will fatal error on all errors unless return_errors is true.
  372. *
  373. * @param array $regOptions
  374. * @param bool $return_errors - specify whether to return the errors
  375. * @return int, the ID of the newly created member
  376. */
  377. function registerMember(&$regOptions, $return_errors = false)
  378. {
  379. global $scripturl, $txt, $modSettings, $context, $sourcedir;
  380. global $user_info, $options, $settings, $smcFunc;
  381. loadLanguage('Login');
  382. // We'll need some external functions.
  383. require_once($sourcedir . '/Subs-Auth.php');
  384. require_once($sourcedir . '/Subs-Post.php');
  385. // Put any errors in here.
  386. $reg_errors = array();
  387. // Registration from the admin center, let them sweat a little more.
  388. if ($regOptions['interface'] == 'admin')
  389. {
  390. is_not_guest();
  391. isAllowedTo('moderate_forum');
  392. }
  393. // If you're an admin, you're special ;).
  394. elseif ($regOptions['interface'] == 'guest')
  395. {
  396. // You cannot register twice...
  397. if (empty($user_info['is_guest']))
  398. redirectexit();
  399. // Make sure they didn't just register with this session.
  400. if (!empty($_SESSION['just_registered']) && empty($modSettings['disableRegisterCheck']))
  401. fatal_lang_error('register_only_once', false);
  402. }
  403. // What method of authorization are we going to use?
  404. if (empty($regOptions['auth_method']) || !in_array($regOptions['auth_method'], array('password', 'openid')))
  405. {
  406. if (!empty($regOptions['openid']))
  407. $regOptions['auth_method'] = 'openid';
  408. else
  409. $regOptions['auth_method'] = 'password';
  410. }
  411. // No name?! How can you register with no name?
  412. if (empty($regOptions['username']))
  413. $reg_errors[] = array('lang', 'need_username');
  414. // Spaces and other odd characters are evil...
  415. $regOptions['username'] = preg_replace('~[\t\n\r\x0B\0' . ($context['utf8'] ? '\x{A0}' : '\xA0') . ']+~' . ($context['utf8'] ? 'u' : ''), ' ', $regOptions['username']);
  416. // Don't use too long a name.
  417. if ($smcFunc['strlen']($regOptions['username']) > 25)
  418. $reg_errors[] = array('lang', 'error_long_name');
  419. // Only these characters are permitted.
  420. if (preg_match('~[<>&"\'=\\\\]~', preg_replace('~&#(?:\\d{1,7}|x[0-9a-fA-F]{1,6});~', '', $regOptions['username'])) != 0 || $regOptions['username'] == '_' || $regOptions['username'] == '|' || strpos($regOptions['username'], '[code') !== false || strpos($regOptions['username'], '[/code') !== false)
  421. $reg_errors[] = array('lang', 'error_invalid_characters_username');
  422. if ($smcFunc['strtolower']($regOptions['username']) === $smcFunc['strtolower']($txt['guest_title']))
  423. $reg_errors[] = array('lang', 'username_reserved', 'general', array($txt['guest_title']));
  424. // @todo Separate the sprintf?
  425. if (empty($regOptions['email']) || preg_match('~^[0-9A-Za-z=_+\-/][0-9A-Za-z=_\'+\-/\.]*@[\w\-]+(\.[\w\-]+)*(\.[\w]{2,6})$~', $regOptions['email']) === 0 || strlen($regOptions['email']) > 255)
  426. $reg_errors[] = array('done', sprintf($txt['valid_email_needed'], $smcFunc['htmlspecialchars']($regOptions['username'])));
  427. if (!empty($regOptions['check_reserved_name']) && isReservedName($regOptions['username'], 0, false))
  428. {
  429. if ($regOptions['password'] == 'chocolate cake')
  430. $reg_errors[] = array('done', 'Sorry, I don\'t take bribes... you\'ll need to come up with a different name.');
  431. $reg_errors[] = array('done', '(' . htmlspecialchars($regOptions['username']) . ') ' . $txt['name_in_use']);
  432. }
  433. // Generate a validation code if it's supposed to be emailed.
  434. $validation_code = '';
  435. if ($regOptions['require'] == 'activation')
  436. $validation_code = generateValidationCode();
  437. // If you haven't put in a password generate one.
  438. if ($regOptions['interface'] == 'admin' && $regOptions['password'] == '' && $regOptions['auth_method'] == 'password')
  439. {
  440. mt_srand(time() + 1277);
  441. $regOptions['password'] = generateValidationCode();
  442. $regOptions['password_check'] = $regOptions['password'];
  443. }
  444. // Does the first password match the second?
  445. elseif ($regOptions['password'] != $regOptions['password_check'] && $regOptions['auth_method'] == 'password')
  446. $reg_errors[] = array('lang', 'passwords_dont_match');
  447. // That's kind of easy to guess...
  448. if ($regOptions['password'] == '')
  449. {
  450. if ($regOptions['auth_method'] == 'password')
  451. $reg_errors[] = array('lang', 'no_password');
  452. else
  453. $regOptions['password'] = sha1(mt_rand());
  454. }
  455. // Now perform hard password validation as required.
  456. if (!empty($regOptions['check_password_strength']))
  457. {
  458. $passwordError = validatePassword($regOptions['password'], $regOptions['username'], array($regOptions['email']));
  459. // Password isn't legal?
  460. if ($passwordError != null)
  461. $reg_errors[] = array('lang', 'profile_error_password_' . $passwordError);
  462. }
  463. // If they are using an OpenID that hasn't been verified yet error out.
  464. // @todo Change this so they can register without having to attempt a login first
  465. if ($regOptions['auth_method'] == 'openid' && (empty($_SESSION['openid']['verified']) || $_SESSION['openid']['openid_uri'] != $regOptions['openid']))
  466. $reg_errors[] = array('lang', 'openid_not_verified');
  467. // You may not be allowed to register this email.
  468. if (!empty($regOptions['check_email_ban']))
  469. isBannedEmail($regOptions['email'], 'cannot_register', $txt['ban_register_prohibited']);
  470. // Check if the email address is in use.
  471. $request = $smcFunc['db_query']('', '
  472. SELECT id_member
  473. FROM {db_prefix}members
  474. WHERE email_address = {string:email_address}
  475. OR email_address = {string:username}
  476. LIMIT 1',
  477. array(
  478. 'email_address' => $regOptions['email'],
  479. 'username' => $regOptions['username'],
  480. )
  481. );
  482. // @todo Separate the sprintf?
  483. if ($smcFunc['db_num_rows']($request) != 0)
  484. $reg_errors[] = array('lang', 'email_in_use', false, array(htmlspecialchars($regOptions['email'])));
  485. $smcFunc['db_free_result']($request);
  486. // If we found any errors we need to do something about it right away!
  487. foreach ($reg_errors as $key => $error)
  488. {
  489. /* Note for each error:
  490. 0 = 'lang' if it's an index, 'done' if it's clear text.
  491. 1 = The text/index.
  492. 2 = Whether to log.
  493. 3 = sprintf data if necessary. */
  494. if ($error[0] == 'lang')
  495. loadLanguage('Errors');
  496. $message = $error[0] == 'lang' ? (empty($error[3]) ? $txt[$error[1]] : vsprintf($txt[$error[1]], $error[3])) : $error[1];
  497. // What to do, what to do, what to do.
  498. if ($return_errors)
  499. {
  500. if (!empty($error[2]))
  501. log_error($message, $error[2]);
  502. $reg_errors[$key] = $message;
  503. }
  504. else
  505. fatal_error($message, empty($error[2]) ? false : $error[2]);
  506. }
  507. // If there's any errors left return them at once!
  508. if (!empty($reg_errors))
  509. return $reg_errors;
  510. $reservedVars = array(
  511. 'actual_theme_url',
  512. 'actual_images_url',
  513. 'base_theme_dir',
  514. 'base_theme_url',
  515. 'default_images_url',
  516. 'default_theme_dir',
  517. 'default_theme_url',
  518. 'default_template',
  519. 'images_url',
  520. 'number_recent_posts',
  521. 'smiley_sets_default',
  522. 'theme_dir',
  523. 'theme_id',
  524. 'theme_layers',
  525. 'theme_templates',
  526. 'theme_url',
  527. );
  528. // Can't change reserved vars.
  529. if (isset($regOptions['theme_vars']) && count(array_intersect(array_keys($regOptions['theme_vars']), $reservedVars)) != 0)
  530. fatal_lang_error('no_theme');
  531. // Some of these might be overwritten. (the lower ones that are in the arrays below.)
  532. $regOptions['register_vars'] = array(
  533. 'member_name' => $regOptions['username'],
  534. 'email_address' => $regOptions['email'],
  535. 'passwd' => sha1(strtolower($regOptions['username']) . $regOptions['password']),
  536. 'password_salt' => substr(md5(mt_rand()), 0, 4) ,
  537. 'posts' => 0,
  538. 'date_registered' => time(),
  539. 'member_ip' => $regOptions['interface'] == 'admin' ? '127.0.0.1' : $user_info['ip'],
  540. 'member_ip2' => $regOptions['interface'] == 'admin' ? '127.0.0.1' : $_SERVER['BAN_CHECK_IP'],
  541. 'validation_code' => $validation_code,
  542. 'real_name' => $regOptions['username'],
  543. 'personal_text' => $modSettings['default_personal_text'],
  544. 'pm_email_notify' => 1,
  545. 'id_theme' => 0,
  546. 'id_post_group' => 4,
  547. 'lngfile' => '',
  548. 'buddy_list' => '',
  549. 'pm_ignore_list' => '',
  550. 'message_labels' => '',
  551. 'website_title' => '',
  552. 'website_url' => '',
  553. 'location' => '',
  554. 'icq' => '',
  555. 'aim' => '',
  556. 'yim' => '',
  557. 'msn' => '',
  558. 'time_format' => '',
  559. 'signature' => '',
  560. 'avatar' => '',
  561. 'usertitle' => '',
  562. 'secret_question' => '',
  563. 'secret_answer' => '',
  564. 'additional_groups' => '',
  565. 'ignore_boards' => '',
  566. 'smiley_set' => '',
  567. 'openid_uri' => (!empty($regOptions['openid']) ? $regOptions['openid'] : ''),
  568. );
  569. // Setup the activation status on this new account so it is correct - firstly is it an under age account?
  570. if ($regOptions['require'] == 'coppa')
  571. {
  572. $regOptions['register_vars']['is_activated'] = 5;
  573. // @todo This should be changed. To what should be it be changed??
  574. $regOptions['register_vars']['validation_code'] = '';
  575. }
  576. // Maybe it can be activated right away?
  577. elseif ($regOptions['require'] == 'nothing')
  578. $regOptions['register_vars']['is_activated'] = 1;
  579. // Maybe it must be activated by email?
  580. elseif ($regOptions['require'] == 'activation')
  581. $regOptions['register_vars']['is_activated'] = 0;
  582. // Otherwise it must be awaiting approval!
  583. else
  584. $regOptions['register_vars']['is_activated'] = 3;
  585. if (isset($regOptions['memberGroup']))
  586. {
  587. // Make sure the id_group will be valid, if this is an administator.
  588. $regOptions['register_vars']['id_group'] = $regOptions['memberGroup'] == 1 && !allowedTo('admin_forum') ? 0 : $regOptions['memberGroup'];
  589. // Check if this group is assignable.
  590. $unassignableGroups = array(-1, 3);
  591. $request = $smcFunc['db_query']('', '
  592. SELECT id_group
  593. FROM {db_prefix}membergroups
  594. WHERE min_posts != {int:min_posts}' . (allowedTo('admin_forum') ? '' : '
  595. OR group_type = {int:is_protected}'),
  596. array(
  597. 'min_posts' => -1,
  598. 'is_protected' => 1,
  599. )
  600. );
  601. while ($row = $smcFunc['db_fetch_assoc']($request))
  602. $unassignableGroups[] = $row['id_group'];
  603. $smcFunc['db_free_result']($request);
  604. if (in_array($regOptions['register_vars']['id_group'], $unassignableGroups))
  605. $regOptions['register_vars']['id_group'] = 0;
  606. }
  607. // ICQ cannot be zero.
  608. if (isset($regOptions['extra_register_vars']['icq']) && empty($regOptions['extra_register_vars']['icq']))
  609. $regOptions['extra_register_vars']['icq'] = '';
  610. // Integrate optional member settings to be set.
  611. if (!empty($regOptions['extra_register_vars']))
  612. foreach ($regOptions['extra_register_vars'] as $var => $value)
  613. $regOptions['register_vars'][$var] = $value;
  614. // Integrate optional user theme options to be set.
  615. $theme_vars = array();
  616. if (!empty($regOptions['theme_vars']))
  617. foreach ($regOptions['theme_vars'] as $var => $value)
  618. $theme_vars[$var] = $value;
  619. // Call an optional function to validate the users' input.
  620. call_integration_hook('integrate_register', array(&$regOptions, &$theme_vars));
  621. // Right, now let's prepare for insertion.
  622. $knownInts = array(
  623. 'date_registered', 'posts', 'id_group', 'last_login', 'instant_messages', 'unread_messages',
  624. 'new_pm', 'pm_prefs', 'gender', 'hide_email', 'show_online', 'pm_email_notify', 'karma_good', 'karma_bad',
  625. 'notify_announcements', 'notify_send_body', 'notify_regularity', 'notify_types',
  626. 'id_theme', 'is_activated', 'id_msg_last_visit', 'id_post_group', 'total_time_logged_in', 'warning',
  627. );
  628. $knownFloats = array(
  629. 'time_offset',
  630. );
  631. $column_names = array();
  632. $values = array();
  633. foreach ($regOptions['register_vars'] as $var => $val)
  634. {
  635. $type = 'string';
  636. if (in_array($var, $knownInts))
  637. $type = 'int';
  638. elseif (in_array($var, $knownFloats))
  639. $type = 'float';
  640. elseif ($var == 'birthdate')
  641. $type = 'date';
  642. $column_names[$var] = $type;
  643. $values[$var] = $val;
  644. }
  645. // Register them into the database.
  646. $smcFunc['db_insert']('',
  647. '{db_prefix}members',
  648. $column_names,
  649. $values,
  650. array('id_member')
  651. );
  652. $memberID = $smcFunc['db_insert_id']('{db_prefix}members', 'id_member');
  653. // Update the number of members and latest member's info - and pass the name, but remove the 's.
  654. if ($regOptions['register_vars']['is_activated'] == 1)
  655. updateStats('member', $memberID, $regOptions['register_vars']['real_name']);
  656. else
  657. updateStats('member');
  658. // Theme variables too?
  659. if (!empty($theme_vars))
  660. {
  661. $inserts = array();
  662. foreach ($theme_vars as $var => $val)
  663. $inserts[] = array($memberID, $var, $val);
  664. $smcFunc['db_insert']('insert',
  665. '{db_prefix}themes',
  666. array('id_member' => 'int', 'variable' => 'string-255', 'value' => 'string-65534'),
  667. $inserts,
  668. array('id_member', 'variable')
  669. );
  670. }
  671. // If it's enabled, increase the registrations for today.
  672. trackStats(array('registers' => '+'));
  673. // Administrative registrations are a bit different...
  674. if ($regOptions['interface'] == 'admin')
  675. {
  676. if ($regOptions['require'] == 'activation')
  677. $email_message = 'admin_register_activate';
  678. elseif (!empty($regOptions['send_welcome_email']))
  679. $email_message = 'admin_register_immediate';
  680. if (isset($email_message))
  681. {
  682. $replacements = array(
  683. 'REALNAME' => $regOptions['register_vars']['real_name'],
  684. 'USERNAME' => $regOptions['username'],
  685. 'PASSWORD' => $regOptions['password'],
  686. 'FORGOTPASSWORDLINK' => $scripturl . '?action=reminder',
  687. 'ACTIVATIONLINK' => $scripturl . '?action=activate;u=' . $memberID . ';code=' . $validation_code,
  688. 'ACTIVATIONLINKWITHOUTCODE' => $scripturl . '?action=activate;u=' . $memberID,
  689. 'ACTIVATIONCODE' => $validation_code,
  690. );
  691. $emaildata = loadEmailTemplate($email_message, $replacements);
  692. sendmail($regOptions['email'], $emaildata['subject'], $emaildata['body'], null, null, false, 0);
  693. }
  694. // All admins are finished here.
  695. return $memberID;
  696. }
  697. // Can post straight away - welcome them to your fantastic community...
  698. if ($regOptions['require'] == 'nothing')
  699. {
  700. if (!empty($regOptions['send_welcome_email']))
  701. {
  702. $replacements = array(
  703. 'REALNAME' => $regOptions['register_vars']['real_name'],
  704. 'USERNAME' => $regOptions['username'],
  705. 'PASSWORD' => $regOptions['password'],
  706. 'FORGOTPASSWORDLINK' => $scripturl . '?action=reminder',
  707. 'OPENID' => !empty($regOptions['openid']) ? $regOptions['openid'] : '',
  708. );
  709. $emaildata = loadEmailTemplate('register_' . ($regOptions['auth_method'] == 'openid' ? 'openid_' : '') . 'immediate', $replacements);
  710. sendmail($regOptions['email'], $emaildata['subject'], $emaildata['body'], null, null, false, 0);
  711. }
  712. // Send admin their notification.
  713. adminNotify('standard', $memberID, $regOptions['username']);
  714. }
  715. // Need to activate their account - or fall under COPPA.
  716. elseif ($regOptions['require'] == 'activation' || $regOptions['require'] == 'coppa')
  717. {
  718. $replacements = array(
  719. 'REALNAME' => $regOptions['register_vars']['real_name'],
  720. 'USERNAME' => $regOptions['username'],
  721. 'PASSWORD' => $regOptions['password'],
  722. 'FORGOTPASSWORDLINK' => $scripturl . '?action=reminder',
  723. 'OPENID' => !empty($regOptions['openid']) ? $regOptions['openid'] : '',
  724. );
  725. if ($regOptions['require'] == 'activation')
  726. $replacements += array(
  727. 'ACTIVATIONLINK' => $scripturl . '?action=activate;u=' . $memberID . ';code=' . $validation_code,
  728. 'ACTIVATIONLINKWITHOUTCODE' => $scripturl . '?action=activate;u=' . $memberID,
  729. 'ACTIVATIONCODE' => $validation_code,
  730. );
  731. else
  732. $replacements += array(
  733. 'COPPALINK' => $scripturl . '?action=coppa;u=' . $memberID,
  734. );
  735. $emaildata = loadEmailTemplate('register_' . ($regOptions['auth_method'] == 'openid' ? 'openid_' : '') . ($regOptions['require'] == 'activation' ? 'activate' : 'coppa'), $replacements);
  736. sendmail($regOptions['email'], $emaildata['subject'], $emaildata['body'], null, null, false, 0);
  737. }
  738. // Must be awaiting approval.
  739. else
  740. {
  741. $replacements = array(
  742. 'REALNAME' => $regOptions['register_vars']['real_name'],
  743. 'USERNAME' => $regOptions['username'],
  744. 'PASSWORD' => $regOptions['password'],
  745. 'FORGOTPASSWORDLINK' => $scripturl . '?action=reminder',
  746. 'OPENID' => !empty($regOptions['openid']) ? $regOptions['openid'] : '',
  747. );
  748. $emaildata = loadEmailTemplate('register_' . ($regOptions['auth_method'] == 'openid' ? 'openid_' : '') . 'pending', $replacements);
  749. sendmail($regOptions['email'], $emaildata['subject'], $emaildata['body'], null, null, false, 0);
  750. // Admin gets informed here...
  751. adminNotify('approval', $memberID, $regOptions['username']);
  752. }
  753. // Okay, they're for sure registered... make sure the session is aware of this for security. (Just married :P!)
  754. $_SESSION['just_registered'] = 1;
  755. return $memberID;
  756. }
  757. /**
  758. * Check if a name is in the reserved words list.
  759. * (name, current member id, name/username?.)
  760. * - checks if name is a reserved name or username.
  761. * - if is_name is false, the name is assumed to be a username.
  762. * - the id_member variable is used to ignore duplicate matches with the
  763. * current member.
  764. *
  765. * @param string $name
  766. * @param int $current_ID_MEMBER
  767. * @param bool $is_name
  768. * @param bool $fatal
  769. */
  770. function isReservedName($name, $current_ID_MEMBER = 0, $is_name = true, $fatal = true)
  771. {
  772. global $user_info, $modSettings, $smcFunc, $context;
  773. // No cheating with entities please.
  774. $replaceEntities = create_function('$string', '
  775. $num = substr($string, 0, 1) === \'x\' ? hexdec(substr($string, 1)) : (int) $string;' . (empty($context['utf8']) ? '
  776. return $num < 0x20 ? \'\' : ($num < 0x80 ? chr($num) : \'&#\' . $string . \';\');' : '
  777. return $num < 0x20 || $num > 0x10FFFF || ($num >= 0xD800 && $num <= 0xDFFF) ? \'\' : ($num < 0x80 ? chr($num) : ($num < 0x800 ? chr(192 | $num >> 6) . chr(128 | $num & 63) : ($num < 0x10000 ? chr(224 | $num >> 12) . chr(128 | $num >> 6 & 63) . chr(128 | $num & 63) : chr(240 | $num >> 18) . chr(128 | $num >> 12 & 63) . chr(128 | $num >> 6 & 63) . chr(128 | $num & 63))));')
  778. );
  779. $name = preg_replace('~(&#(\d{1,7}|x[0-9a-fA-F]{1,6});)~e', '$replaceEntities(\'\\2\')', $name);
  780. $checkName = $smcFunc['strtolower']($name);
  781. // Administrators are never restricted ;).
  782. if (!allowedTo('moderate_forum') && ((!empty($modSettings['reserveName']) && $is_name) || !empty($modSettings['reserveUser']) && !$is_name))
  783. {
  784. $reservedNames = explode("\n", $modSettings['reserveNames']);
  785. // Case sensitive check?
  786. $checkMe = empty($modSettings['reserveCase']) ? $checkName : $name;
  787. // Check each name in the list...
  788. foreach ($reservedNames as $reserved)
  789. {
  790. if ($reserved == '')
  791. continue;
  792. // The admin might've used entities too, level the playing field.
  793. $reservedCheck = preg_replace('~(&#(\d{1,7}|x[0-9a-fA-F]{1,6});)~e', '$replaceEntities(\'\\2\')', $reserved);
  794. // Case sensitive name?
  795. if (empty($modSettings['reserveCase']))
  796. $reservedCheck = $smcFunc['strtolower']($reservedCheck);
  797. // If it's not just entire word, check for it in there somewhere...
  798. if ($checkMe == $reservedCheck || ($smcFunc['strpos']($checkMe, $reservedCheck) !== false && empty($modSettings['reserveWord'])))
  799. if ($fatal)
  800. fatal_lang_error('username_reserved', 'password', array($reserved));
  801. else
  802. return true;
  803. }
  804. $censor_name = $name;
  805. if (censorText($censor_name) != $name)
  806. if ($fatal)
  807. fatal_lang_error('name_censored', 'password', array($name));
  808. else
  809. return true;
  810. }
  811. // Characters we just shouldn't allow, regardless.
  812. foreach (array('*') as $char)
  813. if (strpos($checkName, $char) !== false)
  814. if ($fatal)
  815. fatal_lang_error('username_reserved', 'password', array($char));
  816. else
  817. return true;
  818. // Get rid of any SQL parts of the reserved name...
  819. $checkName = strtr($name, array('_' => '\\_', '%' => '\\%'));
  820. // Make sure they don't want someone else's name.
  821. $request = $smcFunc['db_query']('', '
  822. SELECT id_member
  823. FROM {db_prefix}members
  824. WHERE ' . (empty($current_ID_MEMBER) ? '' : 'id_member != {int:current_member}
  825. AND ') . '(real_name LIKE {string:check_name} OR member_name LIKE {string:check_name})
  826. LIMIT 1',
  827. array(
  828. 'current_member' => $current_ID_MEMBER,
  829. 'check_name' => $checkName,
  830. )
  831. );
  832. if ($smcFunc['db_num_rows']($request) > 0)
  833. {
  834. $smcFunc['db_free_result']($request);
  835. return true;
  836. }
  837. // Does name case insensitive match a member group name?
  838. $request = $smcFunc['db_query']('', '
  839. SELECT id_group
  840. FROM {db_prefix}membergroups
  841. WHERE group_name LIKE {string:check_name}
  842. LIMIT 1',
  843. array(
  844. 'check_name' => $checkName,
  845. )
  846. );
  847. if ($smcFunc['db_num_rows']($request) > 0)
  848. {
  849. $smcFunc['db_free_result']($request);
  850. return true;
  851. }
  852. // Okay, they passed.
  853. return false;
  854. }
  855. // Get a list of groups that have a given permission (on a given board).
  856. /**
  857. * Retrieves a list of membergroups that are allowed to do the given
  858. * permission. (on the given board)
  859. * If board_id is not null, a board permission is assumed.
  860. * The function takes different permission settings into account.
  861. *
  862. * @param string $permission
  863. * @param int $board_id = null
  864. * @return an array containing an array for the allowed membergroup ID's
  865. * and an array for the denied membergroup ID's.
  866. */
  867. function groupsAllowedTo($permission, $board_id = null)
  868. {
  869. global $modSettings, $board_info, $smcFunc;
  870. // Admins are allowed to do anything.
  871. $member_groups = array(
  872. 'allowed' => array(1),
  873. 'denied' => array(),
  874. );
  875. // Assume we're dealing with regular permissions (like profile_view_own).
  876. if ($board_id === null)
  877. {
  878. $request = $smcFunc['db_query']('', '
  879. SELECT id_group, add_deny
  880. FROM {db_prefix}permissions
  881. WHERE permission = {string:permission}',
  882. array(
  883. 'permission' => $permission,
  884. )
  885. );
  886. while ($row = $smcFunc['db_fetch_assoc']($request))
  887. $member_groups[$row['add_deny'] === '1' ? 'allowed' : 'denied'][] = $row['id_group'];
  888. $smcFunc['db_free_result']($request);
  889. }
  890. // Otherwise it's time to look at the board.
  891. else
  892. {
  893. // First get the profile of the given board.
  894. if (isset($board_info['id']) && $board_info['id'] == $board_id)
  895. $profile_id = $board_info['profile'];
  896. elseif ($board_id !== 0)
  897. {
  898. $request = $smcFunc['db_query']('', '
  899. SELECT id_profile
  900. FROM {db_prefix}boards
  901. WHERE id_board = {int:id_board}
  902. LIMIT 1',
  903. array(
  904. 'id_board' => $board_id,
  905. )
  906. );
  907. if ($smcFunc['db_num_rows']($request) == 0)
  908. fatal_lang_error('no_board');
  909. list ($profile_id) = $smcFunc['db_fetch_row']($request);
  910. $smcFunc['db_free_result']($request);
  911. }
  912. else
  913. $profile_id = 1;
  914. $request = $smcFunc['db_query']('', '
  915. SELECT bp.id_group, bp.add_deny
  916. FROM {db_prefix}board_permissions AS bp
  917. WHERE bp.permission = {string:permission}
  918. AND bp.id_profile = {int:profile_id}',
  919. array(
  920. 'profile_id' => $profile_id,
  921. 'permission' => $permission,
  922. )
  923. );
  924. while ($row = $smcFunc['db_fetch_assoc']($request))
  925. $member_groups[$row['add_deny'] === '1' ? 'allowed' : 'denied'][] = $row['id_group'];
  926. $smcFunc['db_free_result']($request);
  927. }
  928. // Denied is never allowed.
  929. $member_groups['allowed'] = array_diff($member_groups['allowed'], $member_groups['denied']);
  930. return $member_groups;
  931. }
  932. /**
  933. * Retrieves a list of members that have a given permission
  934. * (on a given board).
  935. * If board_id is not null, a board permission is assumed.
  936. * Takes different permission settings into account.
  937. * Takes possible moderators (on board 'board_id') into account.
  938. *
  939. * @param string $permission
  940. * @param int $board_id = null
  941. * @return an array containing member ID's.
  942. */
  943. function membersAllowedTo($permission, $board_id = null)
  944. {
  945. global $smcFunc;
  946. $member_groups = groupsAllowedTo($permission, $board_id);
  947. $include_moderators = in_array(3, $member_groups['allowed']) && $board_id !== null;
  948. $member_groups['allowed'] = array_diff($member_groups['allowed'], array(3));
  949. $exclude_moderators = in_array(3, $member_groups['denied']) && $board_id !== null;
  950. $member_groups['denied'] = array_diff($member_groups['denied'], array(3));
  951. $request = $smcFunc['db_query']('', '
  952. SELECT mem.id_member
  953. FROM {db_prefix}members AS mem' . ($include_moderators || $exclude_moderators ? '
  954. LEFT JOIN {db_prefix}moderators AS mods ON (mods.id_member = mem.id_member AND mods.id_board = {int:board_id})' : '') . '
  955. WHERE (' . ($include_moderators ? 'mods.id_member IS NOT NULL OR ' : '') . 'mem.id_group IN ({array_int:member_groups_allowed}) OR FIND_IN_SET({raw:member_group_allowed_implode}, mem.additional_groups) != 0)' . (empty($member_groups['denied']) ? '' : '
  956. AND NOT (' . ($exclude_moderators ? 'mods.id_member IS NOT NULL OR ' : '') . 'mem.id_group IN ({array_int:member_groups_denied}) OR FIND_IN_SET({raw:member_group_denied_implode}, mem.additional_groups) != 0)'),
  957. array(
  958. 'member_groups_allowed' => $member_groups['allowed'],
  959. 'member_groups_denied' => $member_groups['denied'],
  960. 'board_id' => $board_id,
  961. 'member_group_allowed_implode' => implode(', mem.additional_groups) != 0 OR FIND_IN_SET(', $member_groups['allowed']),
  962. 'member_group_denied_implode' => implode(', mem.additional_groups) != 0 OR FIND_IN_SET(', $member_groups['denied']),
  963. )
  964. );
  965. $members = array();
  966. while ($row = $smcFunc['db_fetch_assoc']($request))
  967. $members[] = $row['id_member'];
  968. $smcFunc['db_free_result']($request);
  969. return $members;
  970. }
  971. /**
  972. * This function is used to reassociate members with relevant posts.
  973. * Reattribute guest posts to a specified member.
  974. * Does not check for any permissions.
  975. * If add_to_post_count is set, the member's post count is increased.
  976. *
  977. * @param int $memID
  978. * @param string $email = false
  979. * @param string $membername = false
  980. * @param bool $post_count = false
  981. * @return the number of successful reattributed posts.
  982. */
  983. function reattributePosts($memID, $email = false, $membername = false, $post_count = false)
  984. {
  985. global $smcFunc;
  986. // Firstly, if email and username aren't passed find out the members email address and name.
  987. if ($email === false && $membername === false)
  988. {
  989. $request = $smcFunc['db_query']('', '
  990. SELECT email_address, member_name
  991. FROM {db_prefix}members
  992. WHERE id_member = {int:memID}
  993. LIMIT 1',
  994. array(
  995. 'memID' => $memID,
  996. )
  997. );
  998. list ($email, $membername) = $smcFunc['db_fetch_row']($request);
  999. $smcFunc['db_free_result']($request);
  1000. }
  1001. // If they want the post count restored then we need to do some research.
  1002. if ($post_count)
  1003. {
  1004. $request = $smcFunc['db_query']('', '
  1005. SELECT COUNT(*)
  1006. FROM {db_prefix}messages AS m
  1007. INNER JOIN {db_prefix}boards AS b ON (b.id_board = m.id_board AND b.count_posts = {int:count_posts})
  1008. WHERE m.id_member = {int:guest_id}
  1009. AND m.approved = {int:is_approved}
  1010. AND m.icon != {string:recycled_icon}' . (empty($email) ? '' : '
  1011. AND m.poster_email = {string:email_address}') . (empty($membername) ? '' : '
  1012. AND m.poster_name = {string:member_name}'),
  1013. array(
  1014. 'count_posts' => 0,
  1015. 'guest_id' => 0,
  1016. 'email_address' => $email,
  1017. 'member_name' => $membername,
  1018. 'is_approved' => 1,
  1019. 'recycled_icon' => 'recycled',
  1020. )
  1021. );
  1022. list ($messageCount) = $smcFunc['db_fetch_row']($request);
  1023. $smcFunc['db_free_result']($request);
  1024. updateMemberData($memID, array('posts' => 'posts + ' . $messageCount));
  1025. }
  1026. $query_parts = array();
  1027. if (!empty($email))
  1028. $query_parts[] = 'poster_email = {string:email_address}';
  1029. if (!empty($membername))
  1030. $query_parts[] = 'poster_name = {string:member_name}';
  1031. $query = implode(' AND ', $query_parts);
  1032. // Finally, update the posts themselves!
  1033. $smcFunc['db_query']('', '
  1034. UPDATE {db_prefix}messages
  1035. SET id_member = {int:memID}
  1036. WHERE ' . $query,
  1037. array(
  1038. 'memID' => $memID,
  1039. 'email_address' => $email,
  1040. 'member_name' => $membername,
  1041. )
  1042. );
  1043. // ...and the topics too!
  1044. $smcFunc['db_query']('', '
  1045. UPDATE {db_prefix}topics as t, {db_prefix}messages as m
  1046. SET t.id_member_started = {int:memID}
  1047. WHERE m.id_member = {int:memID}
  1048. AND t.id_first_msg = m.id_msg',
  1049. array(
  1050. 'memID' => $memID,
  1051. )
  1052. );
  1053. return $smcFunc['db_affected_rows']();
  1054. }
  1055. /**
  1056. * This simple function adds/removes the passed user from the current users buddy list.
  1057. * Requires profile_identity_own permission.
  1058. * Called by ?action=buddy;u=x;session_id=y.
  1059. * Redirects to ?action=profile;u=x.
  1060. */
  1061. function BuddyListToggle()
  1062. {
  1063. global $user_info;
  1064. checkSession('get');
  1065. isAllowedTo('profile_identity_own');
  1066. is_not_guest();
  1067. if (empty($_REQUEST['u']))
  1068. fatal_lang_error('no_access', false);
  1069. $_REQUEST['u'] = (int) $_REQUEST['u'];
  1070. // Remove if it's already there...
  1071. if (in_array($_REQUEST['u'], $user_info['buddies']))
  1072. $user_info['buddies'] = array_diff($user_info['buddies'], array($_REQUEST['u']));
  1073. // ...or add if it's not and if it's not you.
  1074. elseif ($user_info['id'] != $_REQUEST['u'])
  1075. $user_info['buddies'][] = (int) $_REQUEST['u'];
  1076. // Update the settings.
  1077. updateMemberData($user_info['id'], array('buddy_list' => implode(',', $user_info['buddies'])));
  1078. // Redirect back to the profile
  1079. redirectexit('action=profile;u=' . $_REQUEST['u']);
  1080. }
  1081. /**
  1082. * Callback for createList().
  1083. *
  1084. * @param $start
  1085. * @param $items_per_page
  1086. * @param $sort
  1087. * @param $where
  1088. * @param $where_params
  1089. * @param $get_duplicates
  1090. */
  1091. function list_getMembers($start, $items_per_page, $sort, $where, $where_params = array(), $get_duplicates = false)
  1092. {
  1093. global $smcFunc;
  1094. $request = $smcFunc['db_query']('', '
  1095. SELECT
  1096. mem.id_member, mem.member_name, mem.real_name, mem.email_address, mem.icq, mem.aim, mem.yim, mem.msn, mem.member_ip, mem.member_ip2, mem.last_login,
  1097. mem.posts, mem.is_activated, mem.date_registered, mem.id_group, mem.additional_groups, mg.group_name
  1098. FROM {db_prefix}members AS mem
  1099. LEFT JOIN {db_prefix}membergroups AS mg ON (mg.id_group = mem.id_group)
  1100. WHERE ' . $where . '
  1101. ORDER BY {raw:sort}
  1102. LIMIT {int:start}, {int:per_page}',
  1103. array_merge($where_params, array(
  1104. 'sort' => $sort,
  1105. 'start' => $start,
  1106. 'per_page' => $items_per_page,
  1107. ))
  1108. );
  1109. $members = array();
  1110. while ($row = $smcFunc['db_fetch_assoc']($request))
  1111. $members[] = $row;
  1112. $smcFunc['db_free_result']($request);
  1113. // If we want duplicates pass the members array off.
  1114. if ($get_duplicates)
  1115. populateDuplicateMembers($members);
  1116. return $members;
  1117. }
  1118. /**
  1119. * Callback for createList().
  1120. *
  1121. * @param $where
  1122. * @param $where_params
  1123. */
  1124. function list_getNumMembers($where, $where_params = array())
  1125. {
  1126. global $smcFunc, $modSettings;
  1127. // We know how many members there are in total.
  1128. if (empty($where) || $where == '1')
  1129. $num_members = $modSettings['totalMembers'];
  1130. // The database knows the amount when there are extra conditions.
  1131. else
  1132. {
  1133. $request = $smcFunc['db_query']('', '
  1134. SELECT COUNT(*)
  1135. FROM {db_prefix}members AS mem
  1136. WHERE ' . $where,
  1137. array_merge($where_params, array(
  1138. ))
  1139. );
  1140. list ($num_members) = $smcFunc['db_fetch_row']($request);
  1141. $smcFunc['db_free_result']($request);
  1142. }
  1143. return $num_members;
  1144. }
  1145. /**
  1146. *
  1147. * @param $members
  1148. */
  1149. function populateDuplicateMembers(&$members)
  1150. {
  1151. global $smcFunc;
  1152. // This will hold all the ip addresses.
  1153. $ips = array();
  1154. foreach ($members as $key => $member)
  1155. {
  1156. // Create the duplicate_members element.
  1157. $members[$key]['duplicate_members'] = array();
  1158. // Store the IPs.
  1159. if (!empty($member['member_ip']))
  1160. $ips[] = $member['member_ip'];
  1161. if (!empty($member['member_ip2']))
  1162. $ips[] = $member['member_ip2'];
  1163. }
  1164. $ips = array_unique($ips);
  1165. if (empty($ips))
  1166. return false;
  1167. // Fetch all members with this IP address, we'll filter out the current ones in a sec.
  1168. $request = $smcFunc['db_query']('', '
  1169. SELECT
  1170. id_member, member_name, email_address, member_ip, member_ip2, is_activated
  1171. FROM {db_prefix}members
  1172. WHERE member_ip IN ({array_string:ips})
  1173. OR member_ip2 IN ({array_string:ips})',
  1174. array(
  1175. 'ips' => $ips,
  1176. )
  1177. );
  1178. $duplicate_members = array();
  1179. $duplicate_ids = array();
  1180. while ($row = $smcFunc['db_fetch_assoc']($request))
  1181. {
  1182. //$duplicate_ids[] = $row['id_member'];
  1183. $member_context = array(
  1184. 'id' => $row['id_member'],
  1185. 'name' => $row['member_name'],
  1186. 'email' => $row['email_address'],
  1187. 'is_banned' => $row['is_activated'] > 10,
  1188. 'ip' => $row['member_ip'],
  1189. 'ip2' => $row['member_ip2'],
  1190. );
  1191. if (in_array($row['member_ip'], $ips))
  1192. $duplicate_members[$row['member_ip']][] = $member_context;
  1193. if ($row['member_ip'] != $row['member_ip2'] && in_array($row['member_ip2'], $ips))
  1194. $duplicate_members[$row['member_ip2']][] = $member_context;
  1195. }
  1196. $smcFunc['db_free_result']($request);
  1197. // Also try to get a list of messages using these ips.
  1198. $request = $smcFunc['db_query']('', '
  1199. SELECT
  1200. m.poster_ip, mem.id_member, mem.member_name, mem.email_address, mem.is_activated
  1201. FROM {db_prefix}messages AS m
  1202. INNER JOIN {db_prefix}members AS mem ON (mem.id_member = m.id_member)
  1203. WHERE m.id_member != 0
  1204. ' . (!empty($duplicate_ids) ? 'AND m.id_member NOT IN ({array_int:duplicate_ids})' : '') . '
  1205. AND m.poster_ip IN ({array_string:ips})',
  1206. array(
  1207. 'duplicate_ids' => $duplicate_ids,
  1208. 'ips' => $ips,
  1209. )
  1210. );
  1211. $had_ips = array();
  1212. while ($row = $smcFunc['db_fetch_assoc']($request))
  1213. {
  1214. // Don't collect lots of the same.
  1215. if (isset($had_ips[$row['poster_ip']]) && in_array($row['id_member'], $had_ips[$row['poster_ip']]))
  1216. continue;
  1217. $had_ips[$row['poster_ip']][] = $row['id_member'];
  1218. $duplicate_members[$row['poster_ip']][] = array(
  1219. 'id' => $row['id_member'],
  1220. 'name' => $row['member_name'],
  1221. 'email' => $row['email_address'],
  1222. 'is_banned' => $row['is_activated'] > 10,
  1223. 'ip' => $row['poster_ip'],
  1224. 'ip2' => $row['poster_ip'],
  1225. );
  1226. }
  1227. $smcFunc['db_free_result']($request);
  1228. // Now we have all the duplicate members, stick them with their respective member in the list.
  1229. if (!empty($duplicate_members))
  1230. foreach ($members as $key => $member)
  1231. {
  1232. if (isset($duplicate_members[$member['member_ip']]))
  1233. $members[$key]['duplicate_members'] = $duplicate_members[$member['member_ip']];
  1234. if ($member['member_ip'] != $member['member_ip2'] && isset($duplicate_members[$member['member_ip2']]))
  1235. $members[$key]['duplicate_members'] = array_merge($member['duplicate_members'], $duplicate_members[$member['member_ip2']]);
  1236. // Check we don't have lots of the same member.
  1237. $member_track = array($member['id_member']);
  1238. foreach ($members[$key]['duplicate_members'] as $duplicate_id_member => $duplicate_member)
  1239. {
  1240. if (in_array($duplicate_member['id'], $member_track))
  1241. {
  1242. unset($members[$key]['duplicate_members'][$duplicate_id_member]);
  1243. continue;
  1244. }
  1245. $member_track[] = $duplicate_member['id'];
  1246. }
  1247. }
  1248. }
  1249. // Generate a random validation code.
  1250. // @todo Err. Whatcha doin' here.
  1251. function generateValidationCode()
  1252. {
  1253. global $smcFunc, $modSettings;
  1254. $request = $smcFunc['db_query']('get_random_number', '
  1255. SELECT RAND()',
  1256. array(
  1257. )
  1258. );
  1259. list ($dbRand) = $smcFunc['db_fetch_row']($request);
  1260. $smcFunc['db_free_result']($request);
  1261. return substr(preg_replace('/\W/', '', sha1(microtime() . mt_rand() . $dbRand . $modSettings['rand_seed'])), 0, 10);
  1262. }
  1263. ?>