Subs-Members.php 44 KB

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